From patchwork Sat Sep 23 09:30:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Jihong X-Patchwork-Id: 143857 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp76143vqu; Sat, 23 Sep 2023 02:33:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEzqtYkzO0ij0ti5sc+ZC6tlvl+nii0WAQV5otlvvG0vqL+oD4ud7s/D3x3YIoAi+i3RJSK X-Received: by 2002:a05:6870:291:b0:1d6:5649:a88e with SMTP id q17-20020a056870029100b001d65649a88emr2111698oaf.37.1695461597065; Sat, 23 Sep 2023 02:33:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695461597; cv=none; d=google.com; s=arc-20160816; b=lfFZBDZAkd0WjTaP22I97fESPQgZ1PxCDsZPj+AhRA74HXR08jquAS0M+JYCrR501f JdZrHr6xfQa+s0/U6ATPNjUtI/IHP3JssJcLkB6gyqnyAhBE9A0jJCp5a6JsaTh9TU4R 0ypkhN+WOKEHPEkPGc3zvy/QMbF2aQBb5cnJx3+zG383pZ8/RI2wpx583R0bixRAbEwc 2IdhmJ+8hHUeQhCwOKbkC4uZilg3OSx/4SjlJ7XWxmjfskw3b0bl3fVai5WpakFWPRzo rpFvMJa2PBj8cJPujHQjDObk52E9AMuPrEfi+PIIXUrLTsib0u38dfjpcUzsOeioxzUc o7Vg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=gmL06ZMpmAZcvjI7+9mA+sZqLuR+U1EnAjpISAOeglc=; fh=JXx1mv1fB650HoTkc5GIgpDQ5Ey550GcDyaNZ/s6bYc=; b=JhI8gyg4jdf5No/OYSPjvrSMcEdszgEyOXKCU6KkbRXWnRSICbcJt2zuoop7Q+bCVs tr+Us+Tc1Nw8P1w3erzAL2/0zyM8IeOoiSAOFYR++4hGh7Fm2DVxtlPWBS6tNGN9eABL K9DZwlR65P5+1qojO6g2xPq6gWO4UB3+LXlPQ32mUSuZpxTOQFFKjTNjEmVNfATiZgoc LVnlDwmdJIRqkzbErxmMV5mlg/s8C/1EoOBKJvWGX5E6otIOYKrjtTD3nMJZGZ5M4vYO 6SzDtOA/uzwASj+g+oC1bkaQ1qFv33ZXk1+2DySfg1O6EVY8DfcOFPNymICsGBQHPmz3 lpUQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id cw10-20020a056a00450a00b0068c0042d3c9si5434676pfb.239.2023.09.23.02.33.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 02:33:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 8F5FD839A541; Sat, 23 Sep 2023 02:32:26 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231280AbjIWJcU (ORCPT + 29 others); Sat, 23 Sep 2023 05:32:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231213AbjIWJcQ (ORCPT ); Sat, 23 Sep 2023 05:32:16 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DDA4196; Sat, 23 Sep 2023 02:32:10 -0700 (PDT) Received: from kwepemm000003.china.huawei.com (unknown [172.30.72.57]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4Rt3ks6Pzxz15NJV; Sat, 23 Sep 2023 17:29:57 +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:07 +0800 From: Yang Jihong To: , , , , , , , , , , CC: Subject: [PATCH 1/4] perf bench messaging: Fix coding style issues for sched-messaging Date: Sat, 23 Sep 2023 09:30:34 +0000 Message-ID: <20230923093037.961232-2-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 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 X-Spam-Status: No, score=2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Sat, 23 Sep 2023 02:32:26 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777820339584991101 X-GMAIL-MSGID: 1777820339584991101 Fixed several code style issues in sched-messaging: 1. Use one space around "-" and "+" operators. 2. When a long line is broken, the operator is at the end of the line. Signed-off-by: Yang Jihong Reviewed-by: Ian Rogers --- tools/perf/bench/sched-messaging.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/perf/bench/sched-messaging.c b/tools/perf/bench/sched-messaging.c index fa1f8f998814..6a33118c8f9b 100644 --- a/tools/perf/bench/sched-messaging.c +++ b/tools/perf/bench/sched-messaging.c @@ -98,7 +98,7 @@ static void *sender(struct sender_context *ctx) again: ret = write(ctx->out_fds[j], data + done, - sizeof(data)-done); + sizeof(data) - done); if (ret < 0) err(EXIT_FAILURE, "SENDER: write"); done += ret; @@ -201,8 +201,8 @@ static unsigned int group(pthread_t *pth, int wakefd) { unsigned int i; - struct sender_context *snd_ctx = malloc(sizeof(struct sender_context) - + num_fds * sizeof(int)); + struct sender_context *snd_ctx = malloc(sizeof(struct sender_context) + + num_fds * sizeof(int)); if (!snd_ctx) err(EXIT_FAILURE, "malloc()"); @@ -239,7 +239,7 @@ static unsigned int group(pthread_t *pth, snd_ctx->wakefd = wakefd; snd_ctx->num_fds = num_fds; - pth[num_fds+i] = create_worker(snd_ctx, (void *)sender); + pth[num_fds + i] = create_worker(snd_ctx, (void *)sender); } /* Close the fds we have left */ @@ -288,7 +288,7 @@ int bench_sched_messaging(int argc, const char **argv) total_children = 0; for (i = 0; i < num_groups; i++) - total_children += group(pth_tab+total_children, num_fds, + total_children += group(pth_tab + total_children, num_fds, readyfds[1], wakefds[0]); /* Wait for everyone to be ready */ From patchwork Sat Sep 23 09:30:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Jihong X-Patchwork-Id: 143859 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp76648vqu; Sat, 23 Sep 2023 02:34:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFaFC+2Ps8fFSgn0UXerY7AkEt+g/szwr655dp12T4kq3GMRFFnI9o1/GaFvXIj2wgYFukb X-Received: by 2002:a17:903:2344:b0:1c2:1c9f:6bd8 with SMTP id c4-20020a170903234400b001c21c9f6bd8mr1750002plh.27.1695461691798; Sat, 23 Sep 2023 02:34:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695461691; cv=none; d=google.com; s=arc-20160816; b=sS7kRVIBDQyTMP+DXNweH+V2DThU5+nemxzopDGA6Wvwvqgref2mwoG0iWyKTE5U5k oLklfRozYqQhSnRp4zEli3xrgxzKsNpMq34kNZoDG+Ss8mrEdb6qZMaxcMxWqavvh2kg nuY4qKmpjJtSDREpj1dRDtCILQoqw4ufu7Dbf0fHCs/PnXC/OTuN3fNdp/22MQcaJTwC 8FcpM5o9BRb9V4BDKA1nD1VGflI9i8uRvy4zJoKllfHUFscvkNXHKhUHHkE5OrKvQ5Dt RVjWsaYFMxUv1lzsoGbzhS5XOSllWKVzOpwKBRnTLffFOfCN8f+EXQcG455/MqYdrrXG ALuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=LKmK615so8foqZ45OWDrnrblQGamk2iZoj3xFcsTCYU=; fh=JXx1mv1fB650HoTkc5GIgpDQ5Ey550GcDyaNZ/s6bYc=; b=Y1rwZR05VXKleqOPqHq9hI0X9ACo0M6cAWbnSgbQrLQqUw1hHd0hZosN4Wh3dpJrom jrfp6k5G0R3UyIyQGT7jk+Drj5uk0OZgvIkM24VBVsBX0t/eJ5nSWehE6UD2jjzlH7uo BH0dyf8YkeYzH/m0SPRFdK5RzlEnvGtDSAZD4Eb4vLUJAzkcz31GqxxStcPIEFvBq+6a MoK9ATQ9Za/WggSn//VqNHX1DJJjKDJ54Nm2TnmRZrUHWuF6+m8SVEYPFkay97Vg091a EY4kO0DJeJzzjwl6kOa/Bx2K+tYO+W01eJoduxutMQKbINoiJlVkwoBljG58YO1MzIT7 Fqng== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id t11-20020a170902e1cb00b001c442309e77si5436101pla.392.2023.09.23.02.34.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 02:34:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 729AA809C4E3; Sat, 23 Sep 2023 02:32:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231296AbjIWJcY (ORCPT + 29 others); Sat, 23 Sep 2023 05:32:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231219AbjIWJcQ (ORCPT ); Sat, 23 Sep 2023 05:32:16 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1EBA9197; Sat, 23 Sep 2023 02:32:10 -0700 (PDT) Received: from kwepemm000003.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Rt3j02QvyzNngT; Sat, 23 Sep 2023 17:28:20 +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:07 +0800 From: Yang Jihong To: , , , , , , , , , , CC: Subject: [PATCH 2/4] perf bench messaging: Factor out create_worker() Date: Sat, 23 Sep 2023 09:30:35 +0000 Message-ID: <20230923093037.961232-3-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 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 X-Spam-Status: No, score=2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Sat, 23 Sep 2023 02:32:43 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777820438683042329 X-GMAIL-MSGID: 1777820438683042329 Refactor the create_worker() helper: 1. Modify the return value and use pthread pointer as a parameter to facilitate value assignment in create_worker(). 2. The thread worker creation and process worker creation are abstracted into independent helpers. No functional change. Test result: # perf bench sched messaging # Running 'sched/messaging' benchmark: # 20 sender and receiver processes per group # 10 groups == 400 processes run Total time: 6.332 [sec] # perf bench sched messaging -t # Running 'sched/messaging' benchmark: # 20 sender and receiver threads per group # 10 groups == 400 threads run Total time: 5.545 [sec] Signed-off-by: Yang Jihong --- tools/perf/bench/sched-messaging.c | 50 ++++++++++++++++-------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/tools/perf/bench/sched-messaging.c b/tools/perf/bench/sched-messaging.c index 6a33118c8f9b..ad8596bed77a 100644 --- a/tools/perf/bench/sched-messaging.c +++ b/tools/perf/bench/sched-messaging.c @@ -139,30 +139,12 @@ static void *receiver(struct receiver_context* ctx) return NULL; } -static pthread_t create_worker(void *ctx, void *(*func)(void *)) +static void create_thread_worker(pthread_t *thread, + void *ctx, void *(*func)(void *)) { pthread_attr_t attr; - pthread_t childid; int ret; - if (!thread_mode) { - /* process mode */ - /* Fork the receiver. */ - switch (fork()) { - case -1: - err(EXIT_FAILURE, "fork()"); - break; - case 0: - (*func) (ctx); - exit(0); - break; - default: - break; - } - - return (pthread_t)0; - } - if (pthread_attr_init(&attr) != 0) err(EXIT_FAILURE, "pthread_attr_init:"); @@ -171,12 +153,32 @@ static pthread_t create_worker(void *ctx, void *(*func)(void *)) err(EXIT_FAILURE, "pthread_attr_setstacksize"); #endif - ret = pthread_create(&childid, &attr, func, ctx); + ret = pthread_create(thread, &attr, func, ctx); if (ret != 0) err(EXIT_FAILURE, "pthread_create failed"); pthread_attr_destroy(&attr); - return childid; +} + +static void create_process_worker(void *ctx, void *(*func)(void *)) +{ + /* Fork the receiver. */ + pid_t pid = fork(); + + if (pid == -1) { + err(EXIT_FAILURE, "fork()"); + } else if (pid == 0) { + (*func) (ctx); + exit(0); + } +} + +static void create_worker(pthread_t *thread, void *ctx, void *(*func)(void *)) +{ + if (!thread_mode) + return create_process_worker(ctx, func); + else + return create_thread_worker(thread, ctx, func); } static void reap_worker(pthread_t id) @@ -226,7 +228,7 @@ static unsigned int group(pthread_t *pth, ctx->ready_out = ready_out; ctx->wakefd = wakefd; - pth[i] = create_worker(ctx, (void *)receiver); + create_worker(pth + i, ctx, (void *)receiver); snd_ctx->out_fds[i] = fds[1]; if (!thread_mode) @@ -239,7 +241,7 @@ static unsigned int group(pthread_t *pth, snd_ctx->wakefd = wakefd; snd_ctx->num_fds = num_fds; - pth[num_fds + i] = create_worker(snd_ctx, (void *)sender); + create_worker(pth + num_fds + i, snd_ctx, (void *)sender); } /* Close the fds we have left */ From patchwork Sat Sep 23 09:30:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Jihong X-Patchwork-Id: 143860 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp76649vqu; Sat, 23 Sep 2023 02:34:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFjzJ2c3AqrMHZxoMFljsqp7uSDWRUzvLyUTz98ejMKOIHWp76w45V6buKJTrgSO/BUAI3K X-Received: by 2002:a05:6a20:8e08:b0:13e:90aa:8c8b with SMTP id y8-20020a056a208e0800b0013e90aa8c8bmr2929039pzj.4.1695461691891; Sat, 23 Sep 2023 02:34:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695461691; cv=none; d=google.com; s=arc-20160816; b=UBYEKZLJNdqfspf6RYr6RO0cXs0Dn9TlipaH0kPeX7e2Oz/nq9GagIzSnrCNwBJHJX rbaLO4XlrqdS4peqBr5ggwwAQYpTF4RcIYHK0IcWivUwM0mq6RGvqfhV7NvoaVx9Qd2Z 2E1PlQLvRIH8c9ddXtjgKKL7hzOgrVIQy8Wy1p0FvslnaP/Tt3znGh2SKPgM1M4xx1HR XtpxFiirQjUouuyqKp+KwUl14glWcLGnjDNE1qoV0c04GBEDRcmk9IM0LtyO78xrB7zz ZqwimESC5Io0/LhVoV/oyJhF/qnqL8HgkKZmDvesYPgeUUTxuSv3Q375/tDlQaZ38hCu 3gaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=e6Y2D/0oTvyYLH7g2V0JfjdR4L0sY/xBgdqAfy1T1pE=; fh=JXx1mv1fB650HoTkc5GIgpDQ5Ey550GcDyaNZ/s6bYc=; b=Upn+psRrIa1nZ5u6olFjffWv2DOE6C3kEqMM2RNo7qqREIgGajfUL+lIpLXu+i0hPN yqUb/rlQ/n/o5V60mOfY0A92aFF8BvyUWKAkr5OXd69AV8uUgu8NjpROLLi+Gz2Gq1ti CMYRxFt9BTAalpsl6PdIXCXzE0Z1wVlnLJlJ+MsBB1nTpioBgMzUP4yFw/9+vCD6nFiB zDUhYhC85z248GJObGxjdn8ZlXcJ7r7vESQ1L/1qJpS+i+12V+vhtlliSPAq+OPFd+Da d6H89ZYRA+1R1hQyR8q/oL2t/jpjrRB/3pzVLfN7DYMWaMvQIGyL57nafD+Dx/QbETcD BBYA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id s7-20020a056a00178700b00690258a9766si6039696pfg.373.2023.09.23.02.34.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 02:34:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id E414D80D542A; Sat, 23 Sep 2023 02:32:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231304AbjIWJc1 (ORCPT + 29 others); Sat, 23 Sep 2023 05:32:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231246AbjIWJcR (ORCPT ); Sat, 23 Sep 2023 05:32:17 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A81F219E; Sat, 23 Sep 2023 02:32:10 -0700 (PDT) Received: from kwepemm000003.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4Rt3kt6Xkfz15NKS; Sat, 23 Sep 2023 17:29:58 +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 3/4] perf bench messaging: Store chlid process pid when creating worker for process mode Date: Sat, 23 Sep 2023 09:30:36 +0000 Message-ID: <20230923093037.961232-4-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 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 X-Spam-Status: No, score=2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Sat, 23 Sep 2023 02:32:44 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777820438904937455 X-GMAIL-MSGID: 1777820438904937455 To save pid of child processes when creating worker: 1. The messaging worker is changed to `union` type to store thread id and process pid. 2. Save child process pid in create_process_worker(). 3. Rename `pth_tab` as `work_tab`. Test result: # perf bench sched messaging # Running 'sched/messaging' benchmark: # 20 sender and receiver processes per group # 10 groups == 400 processes run Total time: 6.744 [sec] # perf bench sched messaging -t # Running 'sched/messaging' benchmark: # 20 sender and receiver threads per group # 10 groups == 400 threads run Total time: 5.788 [sec] Signed-off-by: Yang Jihong --- tools/perf/bench/sched-messaging.c | 47 +++++++++++++++++------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/tools/perf/bench/sched-messaging.c b/tools/perf/bench/sched-messaging.c index ad8596bed77a..04ffaabdd45b 100644 --- a/tools/perf/bench/sched-messaging.c +++ b/tools/perf/bench/sched-messaging.c @@ -55,6 +55,11 @@ struct receiver_context { int wakefd; }; +union messaging_worker { + pthread_t thread; + pid_t pid; +}; + static void fdpair(int fds[2]) { if (use_pipes) { @@ -139,7 +144,7 @@ static void *receiver(struct receiver_context* ctx) return NULL; } -static void create_thread_worker(pthread_t *thread, +static void create_thread_worker(union messaging_worker *worker, void *ctx, void *(*func)(void *)) { pthread_attr_t attr; @@ -153,35 +158,37 @@ static void create_thread_worker(pthread_t *thread, err(EXIT_FAILURE, "pthread_attr_setstacksize"); #endif - ret = pthread_create(thread, &attr, func, ctx); + ret = pthread_create(&worker->thread, &attr, func, ctx); if (ret != 0) err(EXIT_FAILURE, "pthread_create failed"); pthread_attr_destroy(&attr); } -static void create_process_worker(void *ctx, void *(*func)(void *)) +static void create_process_worker(union messaging_worker *worker, + void *ctx, void *(*func)(void *)) { /* Fork the receiver. */ - pid_t pid = fork(); + worker->pid = fork(); - if (pid == -1) { + if (worker->pid == -1) { err(EXIT_FAILURE, "fork()"); - } else if (pid == 0) { + } else if (worker->pid == 0) { (*func) (ctx); exit(0); } } -static void create_worker(pthread_t *thread, void *ctx, void *(*func)(void *)) +static void create_worker(union messaging_worker *worker, + void *ctx, void *(*func)(void *)) { if (!thread_mode) - return create_process_worker(ctx, func); + return create_process_worker(worker, ctx, func); else - return create_thread_worker(thread, ctx, func); + return create_thread_worker(worker, ctx, func); } -static void reap_worker(pthread_t id) +static void reap_worker(union messaging_worker *worker) { int proc_status; void *thread_status; @@ -192,12 +199,12 @@ static void reap_worker(pthread_t id) if (!WIFEXITED(proc_status)) exit(1); } else { - pthread_join(id, &thread_status); + pthread_join(worker->thread, &thread_status); } } /* One group of senders and receivers */ -static unsigned int group(pthread_t *pth, +static unsigned int group(union messaging_worker *worker, unsigned int num_fds, int ready_out, int wakefd) @@ -228,7 +235,7 @@ static unsigned int group(pthread_t *pth, ctx->ready_out = ready_out; ctx->wakefd = wakefd; - create_worker(pth + i, ctx, (void *)receiver); + create_worker(worker + i, ctx, (void *)receiver); snd_ctx->out_fds[i] = fds[1]; if (!thread_mode) @@ -241,7 +248,7 @@ static unsigned int group(pthread_t *pth, snd_ctx->wakefd = wakefd; snd_ctx->num_fds = num_fds; - create_worker(pth + num_fds + i, snd_ctx, (void *)sender); + create_worker(worker + num_fds + i, snd_ctx, (void *)sender); } /* Close the fds we have left */ @@ -275,14 +282,14 @@ int bench_sched_messaging(int argc, const char **argv) unsigned int num_fds = 20; int readyfds[2], wakefds[2]; char dummy; - pthread_t *pth_tab; + union messaging_worker *worker_tab; struct sender_context *pos, *n; argc = parse_options(argc, argv, options, bench_sched_message_usage, 0); - pth_tab = malloc(num_fds * 2 * num_groups * sizeof(pthread_t)); - if (!pth_tab) + worker_tab = malloc(num_fds * 2 * num_groups * sizeof(union messaging_worker)); + if (!worker_tab) err(EXIT_FAILURE, "main:malloc()"); fdpair(readyfds); @@ -290,7 +297,7 @@ int bench_sched_messaging(int argc, const char **argv) total_children = 0; for (i = 0; i < num_groups; i++) - total_children += group(pth_tab + total_children, num_fds, + total_children += group(worker_tab + total_children, num_fds, readyfds[1], wakefds[0]); /* Wait for everyone to be ready */ @@ -306,7 +313,7 @@ int bench_sched_messaging(int argc, const char **argv) /* Reap them all */ for (i = 0; i < total_children; i++) - reap_worker(pth_tab[i]); + reap_worker(worker_tab + i); gettimeofday(&stop, NULL); @@ -334,7 +341,7 @@ int bench_sched_messaging(int argc, const char **argv) break; } - free(pth_tab); + free(worker_tab); list_for_each_entry_safe(pos, n, &sender_contexts, list) { list_del_init(&pos->list); free(pos); From patchwork Sat Sep 23 09:30:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Jihong X-Patchwork-Id: 143858 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp76324vqu; Sat, 23 Sep 2023 02:33:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHxEfIW3oitimzYKKe04fCI+cqNACSm4PcbuAjevLB4WHWKOpBHVMhsAkAtKjlmDIQqq7gD X-Received: by 2002:a05:6830:61b:b0:6bf:12:5189 with SMTP id w27-20020a056830061b00b006bf00125189mr2069323oti.26.1695461624179; Sat, 23 Sep 2023 02:33:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695461624; cv=none; d=google.com; s=arc-20160816; b=XDPJOVgErX8Eiw6MraZ4kVkxhbuRhSJTfhQRRGeiDRYSqGreFpc2osGyHc3xiCDWhD Dd4aciW/3a+UOq1/tCx5g9W7NmrikqLV8dpFyuJgcyzH1wXNqp0S/7dHE5tZnh/bLbAi 0kn6VBviLwOi4XqCvYgEoYVJA7stwInTV0VQfnkA4LoRApZr1SPZswodf5ITgjVSqo4k JYcHibkfizm81FGlcQfEqQTGGVa22VkhfW3R/lSImfUsYD5pZYJJ199HDOz01YIsabdS qPrI8CbwxUb4hUKyUZYev+2ZLrEZdMUswqczWGIvFtWVBr/AQADkAAatasQSHApEsJjc oHFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=HybSFhU2w0PY+hVqZWhLWFKqDaI2kOyz9uVMdJG6/rE=; fh=JXx1mv1fB650HoTkc5GIgpDQ5Ey550GcDyaNZ/s6bYc=; b=A553b3lo5AqHRY/0menkGfB+1Wc79zbJi14oaXyHeCfRyaYfvbOtRU4G2wjUSchVK8 bqBJREx1KmsOxgR6GCtPngK1Md4Il0ISlG2NsdY5RJ6XN8k5s1/Hbccar2qkWsLulF+0 G2tkOXBK6k5hrq1tJ/WjOzwG/3MqZ8Rmxbuz0Wq7cPqk2bbGzrJEO8Jm5cWrArUQhjGX TvC43oY8L9W3GF9xq/6a3a73PAVgq1mnfdZZ+mdZliYkDdYHUzrbMoBo+ExxTlIg9i87 Sqh+MZjgrNlSKyL16tmRbjyvK1aSSdWawnr6acRqmyjOTXfvpLur6NuN6J+f2YxgC9NW 3gWA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id ce11-20020a056a002a0b00b00690f9ca0f99si5653050pfb.12.2023.09.23.02.33.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 02:33:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id D1EE480944DC; Sat, 23 Sep 2023 02:33:03 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231325AbjIWJcc (ORCPT + 29 others); Sat, 23 Sep 2023 05:32:32 -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 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 X-Spam-Status: No, score=2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Sat, 23 Sep 2023 02:33:03 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777820367667259273 X-GMAIL-MSGID: 1777820367667259273 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-messaging.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 = 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]);