Commit bb2e04d4 authored by Yang Jihong's avatar Yang Jihong Committed by Namhyung Kim

perf bench messaging: Kill child processes when exit abnormally in process mode

When exit abnormally in process mode, customize SIGINT and SIGTERM signal
handler to kill the forked child processes.

Before:

  # perf bench sched messaging -l 1000000 -g 1 &
  [1] 8519
  # # Running 'sched/messaging' benchmark:

  # pgrep sched-messaging | wc -l
  41
  # kill -15 8519
  [1]+  Terminated              perf bench sched messaging -l 1000000 -g 1
  # pgrep sched-messaging | wc -l
  40

After:

  # perf bench sched messaging -l 1000000 -g 1 &
  [1] 8472
  # # Running 'sched/messaging' benchmark:

  # pgrep sched-messaging | wc -l
  41
  # kill -15 8472
  [1]+  Exit 1                  perf bench sched messaging -l 1000000 -g 1
  # pgrep sched-messaging | wc -l
  0
Signed-off-by: default avatarYang Jihong <yangjihong1@huawei.com>
Reviewed-by: default avatarIan Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20230923093037.961232-5-yangjihong1@huawei.com
[ namhyung: fix a whitespace ]
Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
parent 07f3e6cf
......@@ -36,6 +36,7 @@ static bool use_pipes = false;
static unsigned int nr_loops = 100;
static bool thread_mode = false;
static unsigned int num_groups = 10;
static unsigned int total_children = 0;
static struct list_head sender_contexts = LIST_HEAD_INIT(sender_contexts);
static struct list_head receiver_contexts = LIST_HEAD_INIT(receiver_contexts);
......@@ -60,6 +61,8 @@ union messaging_worker {
pid_t pid;
};
static union messaging_worker *worker_tab;
static void fdpair(int fds[2])
{
if (use_pipes) {
......@@ -260,6 +263,17 @@ static unsigned int group(union messaging_worker *worker,
return num_fds * 2;
}
static void sig_handler(int sig __maybe_unused)
{
unsigned int i;
/*
* When exit abnormally, kill all forked child processes.
*/
for (i = 0; i < total_children; i++)
kill(worker_tab[i].pid, SIGKILL);
}
static const struct option options[] = {
OPT_BOOLEAN('p', "pipe", &use_pipes,
"Use pipe() instead of socketpair()"),
......@@ -277,12 +291,11 @@ static const char * const bench_sched_message_usage[] = {
int bench_sched_messaging(int argc, const char **argv)
{
unsigned int i, total_children;
unsigned int i;
struct timeval start, stop, diff;
unsigned int num_fds = 20;
int readyfds[2], wakefds[2];
char dummy;
union messaging_worker *worker_tab;
struct sender_context *pos, *n;
argc = parse_options(argc, argv, options,
......@@ -295,7 +308,11 @@ int bench_sched_messaging(int argc, const char **argv)
fdpair(readyfds);
fdpair(wakefds);
total_children = 0;
if (!thread_mode) {
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
}
for (i = 0; i < num_groups; i++)
total_children += group(worker_tab + total_children, num_fds,
readyfds[1], wakefds[0]);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment