Skip to content

Commit 79a3371

Browse files
committed
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
1 parent cbf5f58 commit 79a3371

2 files changed

Lines changed: 147 additions & 4 deletions

File tree

tools/perf/Documentation/perf-bench.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ Options of *pipe*
124124
--loop=::
125125
Specify number of loops.
126126

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+
127135
Example of *pipe*
128136
^^^^^^^^^^^^^^^^^
129137

@@ -141,6 +149,17 @@ Example of *pipe*
141149
Total time:0.016 sec
142150
16.948000 usecs/op
143151
59004 ops/sec
152+
153+
% perf bench sched pipe -G AAA,BBB
154+
(executing 1000000 pipe operations between cgroups)
155+
# Running 'sched/pipe' benchmark:
156+
# Executed 1000000 pipe operations between two processes
157+
158+
Total time: 6.886 [sec]
159+
160+
6.886208 usecs/op
161+
145217 ops/sec
162+
144163
---------------------
145164

146165
SUITES FOR 'syscall'

tools/perf/bench/sched-pipe.c

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
* Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
1111
*/
1212
#include <subcmd/parse-options.h>
13+
#include <api/fs/fs.h>
1314
#include "bench.h"
15+
#include "util/cgroup.h"
1416

1517
#include <unistd.h>
1618
#include <stdio.h>
@@ -19,6 +21,7 @@
1921
#include <sys/wait.h>
2022
#include <string.h>
2123
#include <errno.h>
24+
#include <fcntl.h>
2225
#include <assert.h>
2326
#include <sys/time.h>
2427
#include <sys/types.h>
@@ -31,6 +34,7 @@ struct thread_data {
3134
int nr;
3235
int pipe_read;
3336
int pipe_write;
37+
bool cgroup_failed;
3438
pthread_t pthread;
3539
};
3640

@@ -40,9 +44,48 @@ static int loops = LOOPS_DEFAULT;
4044
/* Use processes by default: */
4145
static bool threaded;
4246

47+
static char *cgrp_names[2];
48+
static struct cgroup *cgrps[2];
49+
50+
static int parse_two_cgroups(const struct option *opt __maybe_unused,
51+
const char *str, int unset __maybe_unused)
52+
{
53+
char *p = strdup(str);
54+
char *q;
55+
int ret = -1;
56+
57+
if (p == NULL) {
58+
fprintf(stderr, "memory allocation failure\n");
59+
return -1;
60+
}
61+
62+
q = strchr(p, ',');
63+
if (q == NULL) {
64+
fprintf(stderr, "it should have two cgroup names: %s\n", p);
65+
goto out;
66+
}
67+
*q = '\0';
68+
69+
cgrp_names[0] = strdup(p);
70+
cgrp_names[1] = strdup(q + 1);
71+
72+
if (cgrp_names[0] == NULL || cgrp_names[1] == NULL) {
73+
fprintf(stderr, "memory allocation failure\n");
74+
goto out;
75+
}
76+
ret = 0;
77+
78+
out:
79+
free(p);
80+
return ret;
81+
}
82+
4383
static const struct option options[] = {
4484
OPT_INTEGER('l', "loop", &loops, "Specify number of loops"),
4585
OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"),
86+
OPT_CALLBACK('G', "cgroups", NULL, "SEND,RECV",
87+
"Put sender and receivers in given cgroups",
88+
parse_two_cgroups),
4689
OPT_END()
4790
};
4891

@@ -51,12 +94,89 @@ static const char * const bench_sched_pipe_usage[] = {
5194
NULL
5295
};
5396

97+
static int enter_cgroup(int nr)
98+
{
99+
char buf[32];
100+
int fd, len, ret;
101+
int saved_errno;
102+
struct cgroup *cgrp;
103+
pid_t pid;
104+
105+
if (cgrp_names[nr] == NULL)
106+
return 0;
107+
108+
if (cgrps[nr] == NULL) {
109+
cgrps[nr] = cgroup__new(cgrp_names[nr], /*do_open=*/true);
110+
if (cgrps[nr] == NULL)
111+
goto err;
112+
}
113+
cgrp = cgrps[nr];
114+
115+
if (threaded)
116+
pid = syscall(__NR_gettid);
117+
else
118+
pid = getpid();
119+
120+
snprintf(buf, sizeof(buf), "%d\n", pid);
121+
len = strlen(buf);
122+
123+
/* try cgroup v2 interface first */
124+
if (threaded)
125+
fd = openat(cgrp->fd, "cgroup.threads", O_WRONLY);
126+
else
127+
fd = openat(cgrp->fd, "cgroup.procs", O_WRONLY);
128+
129+
/* try cgroup v1 if failed */
130+
if (fd < 0 && errno == ENOENT)
131+
fd = openat(cgrp->fd, "tasks", O_WRONLY);
132+
133+
if (fd < 0)
134+
goto err;
135+
136+
ret = write(fd, buf, len);
137+
close(fd);
138+
139+
if (ret != len) {
140+
printf("Cannot enter to cgroup: %s\n", cgrp->name);
141+
return -1;
142+
}
143+
return 0;
144+
145+
err:
146+
saved_errno = errno;
147+
printf("Failed to open cgroup file in %s\n", cgrp_names[nr]);
148+
149+
if (saved_errno == ENOENT) {
150+
char mnt[PATH_MAX];
151+
152+
if (cgroupfs_find_mountpoint(mnt, sizeof(mnt), "perf_event") == 0)
153+
printf(" Hint: create the cgroup first, like 'mkdir %s/%s'\n",
154+
mnt, cgrp_names[nr]);
155+
} else if (saved_errno == EACCES && geteuid() > 0) {
156+
printf(" Hint: try to run as root\n");
157+
}
158+
159+
return -1;
160+
}
161+
162+
static void exit_cgroup(int nr)
163+
{
164+
cgroup__put(cgrps[nr]);
165+
free(cgrp_names[nr]);
166+
}
167+
54168
static void *worker_thread(void *__tdata)
55169
{
56170
struct thread_data *td = __tdata;
57171
int m = 0, i;
58172
int ret;
59173

174+
ret = enter_cgroup(td->nr);
175+
if (ret < 0) {
176+
td->cgroup_failed = true;
177+
return NULL;
178+
}
179+
60180
for (i = 0; i < loops; i++) {
61181
if (!td->nr) {
62182
ret = read(td->pipe_read, &m, sizeof(int));
@@ -76,7 +196,8 @@ static void *worker_thread(void *__tdata)
76196

77197
int bench_sched_pipe(int argc, const char **argv)
78198
{
79-
struct thread_data threads[2], *td;
199+
struct thread_data threads[2] = {};
200+
struct thread_data *td;
80201
int pipe_1[2], pipe_2[2];
81202
struct timeval start, stop, diff;
82203
unsigned long long result_usec = 0;
@@ -112,9 +233,7 @@ int bench_sched_pipe(int argc, const char **argv)
112233
}
113234
}
114235

115-
116236
if (threaded) {
117-
118237
for (t = 0; t < nr_threads; t++) {
119238
td = threads + t;
120239

@@ -128,7 +247,6 @@ int bench_sched_pipe(int argc, const char **argv)
128247
ret = pthread_join(td->pthread, NULL);
129248
BUG_ON(ret);
130249
}
131-
132250
} else {
133251
pid = fork();
134252
assert(pid >= 0);
@@ -147,6 +265,12 @@ int bench_sched_pipe(int argc, const char **argv)
147265
gettimeofday(&stop, NULL);
148266
timersub(&stop, &start, &diff);
149267

268+
exit_cgroup(0);
269+
exit_cgroup(1);
270+
271+
if (threads[0].cgroup_failed || threads[1].cgroup_failed)
272+
return 0;
273+
150274
switch (bench_format) {
151275
case BENCH_FORMAT_DEFAULT:
152276
printf("# Executed %d pipe operations between two %s\n\n",

0 commit comments

Comments
 (0)