From patchwork Fri Oct 13 23:24:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 152859 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp2207852vqb; Fri, 13 Oct 2023 16:25:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFsI+b7HTepUy5ZEhS02Tataed+ENjH1xxKXiHjUIVLmnTy5Sf+b/3MQj1kd/b9k5xsCIeS X-Received: by 2002:a17:902:d512:b0:1bf:349f:b85c with SMTP id b18-20020a170902d51200b001bf349fb85cmr31153438plg.1.1697239505187; Fri, 13 Oct 2023 16:25:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697239505; cv=none; d=google.com; s=arc-20160816; b=CHS0Fq+dcPJCHOa6eRBRr/BB9MSV4ZNzxQdPgv7QaBj6oQbz7E0w4rc6VvOUNh2OiB dFpzDjHof0DQCKfqqC4x5pObbMcMs0l6K11LZIr7HY2pJ0Np4zJKzxu+mupamgGsZgU7 wWN13ywsIOGvVhS6jlZLCg4mIMGn+k4Vx0pLnpsEOftsAKrUxgyu1NFQqi1Qf2k+yBbj ojVPB58yEqGnIc1H4YBU1jnWN6bGtj7zQcRfS9apLT5YBGCTQQ9inx+2+QCcTGErJwqe GMYlI5iCXhFhRH4Dh61TqBqQUERCuKnVUla07bbSS/9IG/HB4eHxZdvj3zKnCuWOZHMI BBTg== 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 :message-id:date:subject:cc:to:from:sender:dkim-signature; bh=hlPy5ggPstA5Vf2bJ1akF8shrqCTEJcK3rV/vSFC+uY=; fh=/TViaxNXj9Ubl1KVZseJCqJutBKG0dvzf3DPkf/1G64=; b=Acnbd631DAHSRdZ5jZUxKwkgcYkQE0I0wVmbQ0sx0DlWzfGeb6hHDV7iqYcJczmQOc O+unx/Sf5fY4KkRNZSzRsSNUsd48YR9DO60sZzZbgJCrJW8S4QnSK82Jgq5P+KpTC2RA KGEPh1Rrbp10KhGQbvMNMU5RddN4C8967N6blfduecUsIQIBm/jDkCjXAFpCeTtcC6tm /p/8G81Ie4t17McyT39lHu7xsNjcYP9wJ/dWDw6y8wVTEDQYod2Hwp0ybDnjrK4K+xBc X8ECkGrVAwIfZfkd1Z89KDIyqpq8ipF06G6tTFkgOh4DLlzC6bP8Cg8bU+50/HHayc+M xJRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=gqMiRrO1; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id a20-20020a170902ee9400b001c3f96ec6d6si5280454pld.111.2023.10.13.16.25.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 16:25:05 -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; dkim=pass header.i=@gmail.com header.s=20230601 header.b=gqMiRrO1; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 9A2D081BB19B; Fri, 13 Oct 2023 16:25:02 -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 S232417AbjJMXYk (ORCPT + 19 others); Fri, 13 Oct 2023 19:24:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230469AbjJMXYj (ORCPT ); Fri, 13 Oct 2023 19:24:39 -0400 Received: from mail-oo1-xc35.google.com (mail-oo1-xc35.google.com [IPv6:2607:f8b0:4864:20::c35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C3FBB7; Fri, 13 Oct 2023 16:24:38 -0700 (PDT) Received: by mail-oo1-xc35.google.com with SMTP id 006d021491bc7-57b6c7d0cabso1512654eaf.1; Fri, 13 Oct 2023 16:24:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697239477; x=1697844277; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:from:to:cc:subject:date:message-id:reply-to; bh=hlPy5ggPstA5Vf2bJ1akF8shrqCTEJcK3rV/vSFC+uY=; b=gqMiRrO18E5+Ss6jNtm6vo81haH/wnKIrvxtDPXhRGSSXa+56IJO5l3Z1rHvxKbhij xyqDKceC1zYHIQmQb5ATRZdDZgQ8TWDLyzN75w5ZDzDE3602ac/HCSnepn8pPump3hAl krotG62SleDdPVHrmySwdnTlDKJ9EX7fzPPrdhazwS617QwPoELhNMLIZvLh+DhfUCG8 GqB57+IZ7D9DsDXe9mVQNbDXU1wO6wZwv8xvC2AJ0zL4a+YHyyUmT1BveHv3YRnzOt6R Q2k/vFMTpc5jefBsGZmphIZKkshRX8ToE79QmtmxoZxZRlxjUxEAsJwsBHWFcrT1JkO3 w+UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697239477; x=1697844277; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=hlPy5ggPstA5Vf2bJ1akF8shrqCTEJcK3rV/vSFC+uY=; b=UmygjJkK7UibDnHirK4wirUAJexj2cugq7JniKS/fLojN38AycQRqmnve23yWg0svf F7CpQGtPv8VCK3Ap5l2DsoxQaIDo88A6q4XwRyniv8QahvvnVPy1dt5v8NGVnwFJ8CVr 579iCmD/VWhY3sRg1X/xo4FkbH46xQb9spFMiOGwq5ia06G51/AuxsLAP11nzYhEsrmv n5jy8/7xMBqr5Ji4QtLlec+tog/5xw//ohCormeMMAGfmAS7OLf4YCwpnW7H9tdWhAFl KS8Td2oKQ+l1WxzaSbikR5sMQOmgfAGhjSLNHBajzeb6KrUGu07Ablwetyj8IZ9NDxsq 8ORA== X-Gm-Message-State: AOJu0YzFkHSxbrM8Y1NyRyTFyXclgVc8fFzHZLyhWw/Eh7S1WLnL/Y7d j1dDy5QyTA5Z9DaUZToNcig= X-Received: by 2002:a05:6358:72a:b0:139:bbae:1f3 with SMTP id e42-20020a056358072a00b00139bbae01f3mr29316223rwj.3.1697239477136; Fri, 13 Oct 2023 16:24:37 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:42e0:cd97:8dac:f4aa:e506]) by smtp.gmail.com with ESMTPSA id b8-20020a637148000000b00589b7a9ef01sm3938930pgn.36.2023.10.13.16.24.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 16:24:36 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org Subject: [PATCH v2] perf bench sched pipe: Add -G/--cgroups option Date: Fri, 13 Oct 2023 16:24:35 -0700 Message-ID: <20231013232435.1012585-1-namhyung@kernel.org> X-Mailer: git-send-email 2.42.0.655.g421f12c284-goog MIME-Version: 1.0 X-Spam-Status: No, score=-1.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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]); Fri, 13 Oct 2023 16:25:02 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778859357782063570 X-GMAIL-MSGID: 1779684611124591724 The -G/--cgroups option is to put sender and receiver in different cgroups in order to measure cgroup context switch overheads. Users need to make sure the cgroups exist and accessible. # perf stat -e context-switches,cgroup-switches \ > taskset -c 0 perf bench sched pipe -l 10000 > /dev/null Performance counter stats for 'taskset -c 0 perf bench sched pipe -l 10000': 20,001 context-switches 2 cgroup-switches 0.053449651 seconds time elapsed 0.011286000 seconds user 0.041869000 seconds sys # perf stat -e context-switches,cgroup-switches \ > taskset -c 0 perf bench sched pipe -l 10000 -G AAA,BBB > /dev/null Performance counter stats for 'taskset -c 0 perf bench sched pipe -l 10000 -G AAA,BBB': 20,001 context-switches 20,001 cgroup-switches 0.052768627 seconds time elapsed 0.006284000 seconds user 0.046266000 seconds sys Signed-off-by: Namhyung Kim --- tools/perf/Documentation/perf-bench.txt | 19 +++++ tools/perf/bench/sched-pipe.c | 93 +++++++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/tools/perf/Documentation/perf-bench.txt b/tools/perf/Documentation/perf-bench.txt index ca5789625cd2..8331bd28b10e 100644 --- a/tools/perf/Documentation/perf-bench.txt +++ b/tools/perf/Documentation/perf-bench.txt @@ -124,6 +124,14 @@ Options of *pipe* --loop=:: Specify number of loops. +-G:: +--cgroups=:: +Names of cgroups for sender and receiver, separated by a comma. +This is useful to check cgroup context switching overhead. +Note that perf doesn't create nor delete the cgroups, so users should +make sure that the cgroups exist and are accessible before use. + + Example of *pipe* ^^^^^^^^^^^^^^^^^ @@ -141,6 +149,17 @@ Example of *pipe* Total time:0.016 sec 16.948000 usecs/op 59004 ops/sec + +% perf bench sched pipe -G AAA,BBB +(executing 1000000 pipe operations between cgroups) +# Running 'sched/pipe' benchmark: +# Executed 1000000 pipe operations between two processes + + Total time: 6.886 [sec] + + 6.886208 usecs/op + 145217 ops/sec + --------------------- SUITES FOR 'syscall' diff --git a/tools/perf/bench/sched-pipe.c b/tools/perf/bench/sched-pipe.c index a960e7a93aec..a98c2e239908 100644 --- a/tools/perf/bench/sched-pipe.c +++ b/tools/perf/bench/sched-pipe.c @@ -11,6 +11,7 @@ */ #include #include "bench.h" +#include "util/cgroup.h" #include #include @@ -19,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -40,9 +42,55 @@ static int loops = LOOPS_DEFAULT; /* Use processes by default: */ static bool threaded; +static struct cgroup *cgrp_send = NULL; +static struct cgroup *cgrp_recv = NULL; + +static int parse_two_cgroups(const struct option *opt __maybe_unused, + const char *str, int unset __maybe_unused) +{ + char *p = strdup(str); + char *q; + int ret = -1; + + if (p == NULL) { + fprintf(stderr, "memory allocation failure"); + return -1; + } + + q = strchr(p, ','); + if (q == NULL) { + fprintf(stderr, "it should have two cgroup names: %s", p); + goto out; + } + *q = '\0'; + + cgrp_send = cgroup__new(p, /*do_open=*/true); + if (cgrp_send == NULL) { + fprintf(stderr, "cannot open sender cgroup: %s", p); + goto out; + } + + /* skip ',' */ + q++; + + cgrp_recv = cgroup__new(q, /*do_open=*/true); + if (cgrp_recv == NULL) { + fprintf(stderr, "cannot open receiver cgroup: %s", q); + goto out; + } + ret = 0; + +out: + free(p); + return ret; +} + static const struct option options[] = { OPT_INTEGER('l', "loop", &loops, "Specify number of loops"), OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"), + OPT_CALLBACK('G', "cgroups", NULL, "SEND,RECV", + "Put sender and receivers in given cgroups", + parse_two_cgroups), OPT_END() }; @@ -51,12 +99,54 @@ static const char * const bench_sched_pipe_usage[] = { NULL }; +static void enter_cgroup(struct cgroup *cgrp) +{ + char buf[32]; + int fd, len; + pid_t pid; + + if (cgrp == NULL) + return; + + if (threaded) + pid = syscall(__NR_gettid); + else + pid = getpid(); + + snprintf(buf, sizeof(buf), "%d\n", pid); + len = strlen(buf); + + /* try cgroup v2 interface first */ + if (threaded) + fd = openat(cgrp->fd, "cgroup.threads", O_WRONLY); + else + fd = openat(cgrp->fd, "cgroup.procs", O_WRONLY); + + /* try cgroup v1 if failed */ + if (fd < 0) + fd = openat(cgrp->fd, "tasks", O_WRONLY); + + if (fd < 0) { + printf("failed to open cgroup file in %s\n", cgrp->name); + return; + } + + if (write(fd, buf, len) != len) + printf("cannot enter to cgroup: %s\n", cgrp->name); + close(fd); +} + static void *worker_thread(void *__tdata) { struct thread_data *td = __tdata; int m = 0, i; int ret; + if (td->nr) + enter_cgroup(cgrp_send); + else + enter_cgroup(cgrp_recv); + for (i = 0; i < loops; i++) { if (!td->nr) { ret = read(td->pipe_read, &m, sizeof(int)); @@ -147,6 +237,9 @@ int bench_sched_pipe(int argc, const char **argv) gettimeofday(&stop, NULL); timersub(&stop, &start, &diff); + cgroup__put(cgrp_send); + cgroup__put(cgrp_recv); + switch (bench_format) { case BENCH_FORMAT_DEFAULT: printf("# Executed %d pipe operations between two %s\n\n",