Skip to content

Commit 553d5ef

Browse files
captain5050namhyung
authored andcommitted
perf test: Add a signal handler to kill forked child processes
If the `perf test` process is killed the child tests continue running and may run indefinitely. Propagate SIGINT (ctrl-C) and SIGTERM (kill) signals to the running child processes so that they terminate when the parent is killed. Signed-off-by: Ian Rogers <[email protected]> Cc: Colin Ian King <[email protected]> Cc: Howard Chu <[email protected]> Cc: Weilin Wang <[email protected]> Cc: James Clark <[email protected]> Cc: Ilya Leoshkevich <[email protected]> Cc: Thomas Richter <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Dapeng Mi <[email protected]> Cc: Athira Jajeev <[email protected]> Cc: Michael Petlan <[email protected]> Cc: Veronika Molnarova <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent 94d1a91 commit 553d5ef

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

tools/perf/tests/builtin-test.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,22 @@ static int start_test(struct test_suite *test, int i, int subi, struct child_tes
472472
for (j = 0, k = 0; j < ARRAY_SIZE(tests); j++, k = 0) \
473473
while ((t = tests[j][k++]) != NULL)
474474

475+
/* State outside of __cmd_test for the sake of the signal handler. */
476+
477+
static size_t num_tests;
478+
static struct child_test **child_tests;
479+
static jmp_buf cmd_test_jmp_buf;
480+
481+
static void cmd_test_sig_handler(int sig)
482+
{
483+
siglongjmp(cmd_test_jmp_buf, sig);
484+
}
485+
475486
static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
476487
{
477488
struct test_suite *t;
478-
int width = 0;
489+
static int width = 0;
479490
unsigned int j, k;
480-
size_t num_tests = 0;
481-
struct child_test **child_tests;
482491
int err = 0;
483492

484493
for_each_test(j, k, t) {
@@ -502,6 +511,26 @@ static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
502511
if (!child_tests)
503512
return -ENOMEM;
504513

514+
err = sigsetjmp(cmd_test_jmp_buf, 1);
515+
if (err) {
516+
pr_err("\nSignal (%d) while running tests.\nTerminating tests with the same signal\n",
517+
err);
518+
for (size_t x = 0; x < num_tests; x++) {
519+
struct child_test *child_test = child_tests[x];
520+
521+
if (!child_test)
522+
continue;
523+
524+
pr_debug3("Killing %d pid %d\n",
525+
child_test->test_num + 1,
526+
child_test->process.pid);
527+
kill(child_test->process.pid, err);
528+
}
529+
goto err_out;
530+
}
531+
signal(SIGINT, cmd_test_sig_handler);
532+
signal(SIGTERM, cmd_test_sig_handler);
533+
505534
/*
506535
* In parallel mode pass 1 runs non-exclusive tests in parallel, pass 2
507536
* runs the exclusive tests sequentially. In other modes all tests are
@@ -562,6 +591,8 @@ static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
562591
}
563592
}
564593
err_out:
594+
signal(SIGINT, SIG_DFL);
595+
signal(SIGTERM, SIG_DFL);
565596
if (err) {
566597
pr_err("Internal test harness failure. Completing any started tests:\n:");
567598
for (size_t x = 0; x < num_tests; x++)

0 commit comments

Comments
 (0)