Skip to contentSkip to conversation

perf bench sched pipe: Add -G/--cgroups option

PATCH Superseded Namhyung Kim 2 participants 3 messages Oct 13, 2023 – Oct 15, 2023 perf events

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH v2 1/1 perf bench sched pipe: Add -G/--cgroups option

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 \
Quoted 1 line

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 \
Quoted 1 line

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
---
 tools/perf/Documentation/perf-bench.txt | 19 +++++
 tools/perf/bench/sched-pipe.c           | 93 +++++++++++++++++++++++++
 2 files changed, 112 insertions(+)
tools/perf/Documentation/perf-bench.txt +19 -0
index ca5789625cd2..8331bd28b10e 100644
@@ -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'
tools/perf/bench/sched-pipe.c +93 -0

132 lines not on this page · raw message

Signature · 1 line
2.42.0.655.g421f12c284-goog
#1 lore raw

tools/perf/bench/sched-pipe.c static int loops = LOOPS_DEFAULT; · line 71

Quoted 5 lines · Namhyung Kim
tools/perf/bench/sched-pipe.c +5 -0
+	cgrp_send = cgroup__new(p, /*do_open=*/true);
+	if (cgrp_send == NULL) {
+		fprintf(stderr, "cannot open sender cgroup: %s", p);
+		goto out;
+	}

Maybe in this case print out a small suggestion of how to create this particular cgroup?

Most distro users and even kernel developers don't ever have to create new cgroups.

Maybe even allow the creation of new cgroups for this testing, if they don't already exist? As long as we don't delete any cgroups I don't think much harm can be done - and the increase in usability is substantial.

Quoted 24 lines
tools/perf/bench/sched-pipe.c +24 -0
+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 */

tools/perf/bench/sched-pipe.c static const char * const bench_sched_pipe_usage[] = { · line 135

Quoted 10 lines
tools/perf/bench/sched-pipe.c +10 -0
+	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);

The failures here should probably result in termination of the run with an error code, not just messages which are easy to skip in automated tests?

Thanks,

Ingo

author maintainer
#2 lore raw
Quoted 13 lines · Ingo Molnar

tools/perf/bench/sched-pipe.c static int loops = LOOPS_DEFAULT; · line 71

Quoted 5 lines · Namhyung Kim
tools/perf/bench/sched-pipe.c +5 -0
+     cgrp_send = cgroup__new(p, /*do_open=*/true);
+     if (cgrp_send == NULL) {
+             fprintf(stderr, "cannot open sender cgroup: %s", p);
+             goto out;
+     }

Maybe in this case print out a small suggestion of how to create this particular cgroup?

Quoted 6 lines

Most distro users and even kernel developers don't ever have to create new cgroups.

Maybe even allow the creation of new cgroups for this testing, if they don't already exist? As long as we don't delete any cgroups I don't think much harm can be done - and the increase in usability is substantial.

I'm not sure if it's ok create a new cgroup and leave it after the use. Anyway, none of the existing subcommands create new cgroups IIUC and I think it'd be ok to print a message on how to create one.

Quoted 30 lines
Quoted 30 lines
tools/perf/bench/sched-pipe.c +29 -0
+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);
Quoted 8 lines

tools/perf/bench/sched-pipe.c static const char * const bench_sched_pipe_usage[] = { · line 135

Quoted 5 lines
tools/perf/bench/sched-pipe.c +5 -0
+             return;
+     }
+
+     if (write(fd, buf, len) != len)
+             printf("cannot enter to cgroup: %s\n", cgrp->name);

The failures here should probably result in termination of the run with an error code, not just messages which are easy to skip in automated tests?

Right, I'll make the change.

Thanks,
Namhyung