perf bench messaging: Kill child processes when exit abnormally in process mode
authorYang Jihong <yangjihong1@huawei.com>
Sat, 23 Sep 2023 09:30:37 +0000 (09:30 +0000)
committerNamhyung Kim <namhyung@kernel.org>
Wed, 27 Sep 2023 04:47:12 +0000 (21:47 -0700)
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: Yang Jihong <yangjihong1@huawei.com>
Reviewed-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20230923093037.961232-5-yangjihong1@huawei.com
[ namhyung: fix a whitespace ]
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/bench/sched-messaging.c

index 04ffaabdd45b8f7c9a46223f5a27ae45f3931bee..93dcd9dba3d0dcb291b17b86a25fe411b3ddfb26 100644 (file)
@@ -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]);