From bb2e04d4499c611382aeb75063cf7f185cf20197 Mon Sep 17 00:00:00 2001 From: Yang Jihong Date: Sat, 23 Sep 2023 09:30:37 +0000 Subject: 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: Yang Jihong Reviewed-by: Ian Rogers Link: https://lore.kernel.org/r/20230923093037.961232-5-yangjihong1@huawei.com [ namhyung: fix a whitespace ] Signed-off-by: Namhyung Kim --- tools/perf/bench/sched-messaging.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'tools/perf/bench') diff --git a/tools/perf/bench/sched-messaging.c b/tools/perf/bench/sched-messaging.c index 04ffaabdd45b..93dcd9dba3d0 100644 --- a/tools/perf/bench/sched-messaging.c +++ b/tools/perf/bench/sched-messaging.c @@ -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]); -- cgit v1.2.3