Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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. The following
example should the effect of this change. Please don't forget taskset
before the perf bench to measure cgroup switches properly. Otherwise
each task would run on a different CPU and generate cgroup switches
regardless of this change.

# 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 <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Link: https://lore.kernel.org/r/20231017202342.1353124-1-namhyung@kernel.org

+147 -4
+19
tools/perf/Documentation/perf-bench.txt
··· 124 124 --loop=:: 125 125 Specify number of loops. 126 126 127 + -G:: 128 + --cgroups=:: 129 + Names of cgroups for sender and receiver, separated by a comma. 130 + This is useful to check cgroup context switching overhead. 131 + Note that perf doesn't create nor delete the cgroups, so users should 132 + make sure that the cgroups exist and are accessible before use. 133 + 134 + 127 135 Example of *pipe* 128 136 ^^^^^^^^^^^^^^^^^ 129 137 ··· 149 141 Total time:0.016 sec 150 142 16.948000 usecs/op 151 143 59004 ops/sec 144 + 145 + % perf bench sched pipe -G AAA,BBB 146 + (executing 1000000 pipe operations between cgroups) 147 + # Running 'sched/pipe' benchmark: 148 + # Executed 1000000 pipe operations between two processes 149 + 150 + Total time: 6.886 [sec] 151 + 152 + 6.886208 usecs/op 153 + 145217 ops/sec 154 + 152 155 --------------------- 153 156 154 157 SUITES FOR 'syscall'
+128 -4
tools/perf/bench/sched-pipe.c
··· 10 10 * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> 11 11 */ 12 12 #include <subcmd/parse-options.h> 13 + #include <api/fs/fs.h> 13 14 #include "bench.h" 15 + #include "util/cgroup.h" 14 16 15 17 #include <unistd.h> 16 18 #include <stdio.h> ··· 21 19 #include <sys/wait.h> 22 20 #include <string.h> 23 21 #include <errno.h> 22 + #include <fcntl.h> 24 23 #include <assert.h> 25 24 #include <sys/time.h> 26 25 #include <sys/types.h> ··· 34 31 int nr; 35 32 int pipe_read; 36 33 int pipe_write; 34 + bool cgroup_failed; 37 35 pthread_t pthread; 38 36 }; 39 37 ··· 44 40 /* Use processes by default: */ 45 41 static bool threaded; 46 42 43 + static char *cgrp_names[2]; 44 + static struct cgroup *cgrps[2]; 45 + 46 + static int parse_two_cgroups(const struct option *opt __maybe_unused, 47 + const char *str, int unset __maybe_unused) 48 + { 49 + char *p = strdup(str); 50 + char *q; 51 + int ret = -1; 52 + 53 + if (p == NULL) { 54 + fprintf(stderr, "memory allocation failure\n"); 55 + return -1; 56 + } 57 + 58 + q = strchr(p, ','); 59 + if (q == NULL) { 60 + fprintf(stderr, "it should have two cgroup names: %s\n", p); 61 + goto out; 62 + } 63 + *q = '\0'; 64 + 65 + cgrp_names[0] = strdup(p); 66 + cgrp_names[1] = strdup(q + 1); 67 + 68 + if (cgrp_names[0] == NULL || cgrp_names[1] == NULL) { 69 + fprintf(stderr, "memory allocation failure\n"); 70 + goto out; 71 + } 72 + ret = 0; 73 + 74 + out: 75 + free(p); 76 + return ret; 77 + } 78 + 47 79 static const struct option options[] = { 48 80 OPT_INTEGER('l', "loop", &loops, "Specify number of loops"), 49 81 OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"), 82 + OPT_CALLBACK('G', "cgroups", NULL, "SEND,RECV", 83 + "Put sender and receivers in given cgroups", 84 + parse_two_cgroups), 50 85 OPT_END() 51 86 }; 52 87 ··· 94 51 NULL 95 52 }; 96 53 54 + static int enter_cgroup(int nr) 55 + { 56 + char buf[32]; 57 + int fd, len, ret; 58 + int saved_errno; 59 + struct cgroup *cgrp; 60 + pid_t pid; 61 + 62 + if (cgrp_names[nr] == NULL) 63 + return 0; 64 + 65 + if (cgrps[nr] == NULL) { 66 + cgrps[nr] = cgroup__new(cgrp_names[nr], /*do_open=*/true); 67 + if (cgrps[nr] == NULL) 68 + goto err; 69 + } 70 + cgrp = cgrps[nr]; 71 + 72 + if (threaded) 73 + pid = syscall(__NR_gettid); 74 + else 75 + pid = getpid(); 76 + 77 + snprintf(buf, sizeof(buf), "%d\n", pid); 78 + len = strlen(buf); 79 + 80 + /* try cgroup v2 interface first */ 81 + if (threaded) 82 + fd = openat(cgrp->fd, "cgroup.threads", O_WRONLY); 83 + else 84 + fd = openat(cgrp->fd, "cgroup.procs", O_WRONLY); 85 + 86 + /* try cgroup v1 if failed */ 87 + if (fd < 0 && errno == ENOENT) 88 + fd = openat(cgrp->fd, "tasks", O_WRONLY); 89 + 90 + if (fd < 0) 91 + goto err; 92 + 93 + ret = write(fd, buf, len); 94 + close(fd); 95 + 96 + if (ret != len) { 97 + printf("Cannot enter to cgroup: %s\n", cgrp->name); 98 + return -1; 99 + } 100 + return 0; 101 + 102 + err: 103 + saved_errno = errno; 104 + printf("Failed to open cgroup file in %s\n", cgrp_names[nr]); 105 + 106 + if (saved_errno == ENOENT) { 107 + char mnt[PATH_MAX]; 108 + 109 + if (cgroupfs_find_mountpoint(mnt, sizeof(mnt), "perf_event") == 0) 110 + printf(" Hint: create the cgroup first, like 'mkdir %s/%s'\n", 111 + mnt, cgrp_names[nr]); 112 + } else if (saved_errno == EACCES && geteuid() > 0) { 113 + printf(" Hint: try to run as root\n"); 114 + } 115 + 116 + return -1; 117 + } 118 + 119 + static void exit_cgroup(int nr) 120 + { 121 + cgroup__put(cgrps[nr]); 122 + free(cgrp_names[nr]); 123 + } 124 + 97 125 static void *worker_thread(void *__tdata) 98 126 { 99 127 struct thread_data *td = __tdata; 100 128 int m = 0, i; 101 129 int ret; 130 + 131 + ret = enter_cgroup(td->nr); 132 + if (ret < 0) { 133 + td->cgroup_failed = true; 134 + return NULL; 135 + } 102 136 103 137 for (i = 0; i < loops; i++) { 104 138 if (!td->nr) { ··· 196 76 197 77 int bench_sched_pipe(int argc, const char **argv) 198 78 { 199 - struct thread_data threads[2], *td; 79 + struct thread_data threads[2] = {}; 80 + struct thread_data *td; 200 81 int pipe_1[2], pipe_2[2]; 201 82 struct timeval start, stop, diff; 202 83 unsigned long long result_usec = 0; ··· 233 112 } 234 113 } 235 114 236 - 237 115 if (threaded) { 238 - 239 116 for (t = 0; t < nr_threads; t++) { 240 117 td = threads + t; 241 118 ··· 247 128 ret = pthread_join(td->pthread, NULL); 248 129 BUG_ON(ret); 249 130 } 250 - 251 131 } else { 252 132 pid = fork(); 253 133 assert(pid >= 0); ··· 264 146 265 147 gettimeofday(&stop, NULL); 266 148 timersub(&stop, &start, &diff); 149 + 150 + exit_cgroup(0); 151 + exit_cgroup(1); 152 + 153 + if (threads[0].cgroup_failed || threads[1].cgroup_failed) 154 + return 0; 267 155 268 156 switch (bench_format) { 269 157 case BENCH_FORMAT_DEFAULT: