From nobody Fri Feb 13 15:42:34 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 14BE6CE7A89 for ; Sat, 23 Sep 2023 09:32:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231339AbjIWJcd (ORCPT ); Sat, 23 Sep 2023 05:32:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231271AbjIWJcT (ORCPT ); Sat, 23 Sep 2023 05:32:19 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58EFA136; Sat, 23 Sep 2023 02:32:12 -0700 (PDT) Received: from kwepemm000003.china.huawei.com (unknown [172.30.72.57]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4Rt3jB6BFHzMljc; Sat, 23 Sep 2023 17:28:30 +0800 (CST) Received: from ubuntu2204.huawei.com (10.67.174.22) by kwepemm000003.china.huawei.com (7.193.23.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Sat, 23 Sep 2023 17:32:08 +0800 From: Yang Jihong To: , , , , , , , , , , CC: Subject: [PATCH 4/4] perf bench messaging: Kill child processes when exit abnormally in process mode Date: Sat, 23 Sep 2023 09:30:37 +0000 Message-ID: <20230923093037.961232-5-yangjihong1@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230923093037.961232-1-yangjihong1@huawei.com> References: <20230923093037.961232-1-yangjihong1@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.67.174.22] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm000003.china.huawei.com (7.193.23.66) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 --- tools/perf/bench/sched-messaging.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/tools/perf/bench/sched-messaging.c b/tools/perf/bench/sched-me= ssaging.c index 04ffaabdd45b..e2b8938b7dc3 100644 --- a/tools/perf/bench/sched-messaging.c +++ b/tools/perf/bench/sched-messaging.c @@ -36,6 +36,7 @@ static bool use_pipes =3D false; static unsigned int nr_loops =3D 100; static bool thread_mode =3D false; static unsigned int num_groups =3D 10; +static unsigned int total_children =3D 0; static struct list_head sender_contexts =3D LIST_HEAD_INIT(sender_contexts= ); static struct list_head receiver_contexts =3D LIST_HEAD_INIT(receiver_cont= exts); =20 @@ -60,6 +61,8 @@ union messaging_worker { pid_t pid; }; =20 +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 *work= er, return num_fds * 2; } =20 +static void sig_handler(int sig __maybe_unused) +{ + unsigned int i; + + /* + * When exit abnormally, kill all forked child processes. + */ + for (i =3D 0; i < total_children; i++) + kill(worker_tab[i].pid, SIGKILL); +} + static const struct option options[] =3D { OPT_BOOLEAN('p', "pipe", &use_pipes, "Use pipe() instead of socketpair()"), @@ -277,12 +291,11 @@ static const char * const bench_sched_message_usage[]= =3D { =20 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 =3D 20; int readyfds[2], wakefds[2]; char dummy; - union messaging_worker *worker_tab; struct sender_context *pos, *n; =20 argc =3D parse_options(argc, argv, options, @@ -295,7 +308,11 @@ int bench_sched_messaging(int argc, const char **argv) fdpair(readyfds); fdpair(wakefds); =20 - total_children =3D 0; + if (!thread_mode) { + signal(SIGINT, sig_handler); + signal(SIGTERM, sig_handler); + } + for (i =3D 0; i < num_groups; i++) total_children +=3D group(worker_tab + total_children, num_fds, readyfds[1], wakefds[0]); --=20 2.34.1