Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp85716rdh; Sat, 23 Sep 2023 02:33:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGZmw33zchcPL8+MR4y0eZS/5F/neTmCPDb4oHt0Xr3qL5EWqhTwhG1vn1AK0cAZF4EiZD8 X-Received: by 2002:a17:902:cec3:b0:1bf:fcc:d047 with SMTP id d3-20020a170902cec300b001bf0fccd047mr1634796plg.17.1695461622189; Sat, 23 Sep 2023 02:33:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695461622; cv=none; d=google.com; s=arc-20160816; b=KP6SnCe0axlOXlofu3PwL0OOtaKZkGGT372sLGKUoz5ya4ICHsPTntvtL8S7VTXoBe B3e+MSPdhqem6XtqpIQC0M6AvkKsfAsBhixlnxJ8XGGgveXk2O+gH2B5B+broCy8Z4Qr 5TlZ3FcXzxJG77QRtLApByPDEq/idTwBUI2mPmj4opr1LT5qYLqH4y6+DkwfdWdactss 1xaxp4sUrVYZbT0FGS9ILUsjnZRtwJ75I8GJCBKY2YGcfQOj4V754hKAstfu8dRLqUmc jR6APfI4WOMMSN6ds0S6ZCl6n0gnbRfhGb1KxEW/9fU8p4p9mCkuldUENFFIWQsea1cr Ic8A== 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:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id a4-20020a170902ecc400b001bbb83a182csi6174289plh.519.2023.09.23.02.33.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 02:33:42 -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 242168241979; Sat, 23 Sep 2023 02:32:54 -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 S231289AbjIWJcX (ORCPT + 99 others); Sat, 23 Sep 2023 05:32:23 -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 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII 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:32:54 -0700 (PDT) X-Spam-Level: ** 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 */ -- 2.34.1