1 // SPDX-License-Identifier: GPL-2.0
6 * pipe: Benchmark for pipe()
9 * http://people.redhat.com/mingo/cfs-scheduler/tools/pipe-test-1m.c
12 #include <subcmd/parse-options.h>
13 #include <api/fs/fs.h>
15 #include "util/cgroup.h"
26 #include <sys/epoll.h>
28 #include <sys/types.h>
29 #include <sys/syscall.h>
30 #include <linux/time64.h>
38 struct epoll_event epoll_ev;
44 #define LOOPS_DEFAULT 1000000
45 static int loops = LOOPS_DEFAULT;
47 /* Use processes by default: */
50 static bool nonblocking;
51 static char *cgrp_names[2];
52 static struct cgroup *cgrps[2];
54 static int parse_two_cgroups(const struct option *opt __maybe_unused,
55 const char *str, int unset __maybe_unused)
57 char *p = strdup(str);
62 fprintf(stderr, "memory allocation failure\n");
68 fprintf(stderr, "it should have two cgroup names: %s\n", p);
73 cgrp_names[0] = strdup(p);
74 cgrp_names[1] = strdup(q + 1);
76 if (cgrp_names[0] == NULL || cgrp_names[1] == NULL) {
77 fprintf(stderr, "memory allocation failure\n");
87 static const struct option options[] = {
88 OPT_BOOLEAN('n', "nonblocking", &nonblocking, "Use non-blocking operations"),
89 OPT_INTEGER('l', "loop", &loops, "Specify number of loops"),
90 OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"),
91 OPT_CALLBACK('G', "cgroups", NULL, "SEND,RECV",
92 "Put sender and receivers in given cgroups",
97 static const char * const bench_sched_pipe_usage[] = {
98 "perf bench sched pipe <options>",
102 static int enter_cgroup(int nr)
110 if (cgrp_names[nr] == NULL)
113 if (cgrps[nr] == NULL) {
114 cgrps[nr] = cgroup__new(cgrp_names[nr], /*do_open=*/true);
115 if (cgrps[nr] == NULL)
121 pid = syscall(__NR_gettid);
125 snprintf(buf, sizeof(buf), "%d\n", pid);
128 /* try cgroup v2 interface first */
130 fd = openat(cgrp->fd, "cgroup.threads", O_WRONLY);
132 fd = openat(cgrp->fd, "cgroup.procs", O_WRONLY);
134 /* try cgroup v1 if failed */
135 if (fd < 0 && errno == ENOENT)
136 fd = openat(cgrp->fd, "tasks", O_WRONLY);
141 ret = write(fd, buf, len);
145 printf("Cannot enter to cgroup: %s\n", cgrp->name);
152 printf("Failed to open cgroup file in %s\n", cgrp_names[nr]);
154 if (saved_errno == ENOENT) {
157 if (cgroupfs_find_mountpoint(mnt, sizeof(mnt), "perf_event") == 0)
158 printf(" Hint: create the cgroup first, like 'mkdir %s/%s'\n",
159 mnt, cgrp_names[nr]);
160 } else if (saved_errno == EACCES && geteuid() > 0) {
161 printf(" Hint: try to run as root\n");
167 static void exit_cgroup(int nr)
169 cgroup__put(cgrps[nr]);
170 free(cgrp_names[nr]);
173 static inline int read_pipe(struct thread_data *td)
178 ret = epoll_wait(td->epoll_fd, &td->epoll_ev, 1, -1);
182 ret = read(td->pipe_read, &m, sizeof(int));
183 if (nonblocking && ret < 0 && errno == EWOULDBLOCK)
188 static void *worker_thread(void *__tdata)
190 struct thread_data *td = __tdata;
193 ret = enter_cgroup(td->nr);
195 td->cgroup_failed = true;
200 td->epoll_ev.events = EPOLLIN;
201 td->epoll_fd = epoll_create(1);
202 BUG_ON(td->epoll_fd < 0);
203 BUG_ON(epoll_ctl(td->epoll_fd, EPOLL_CTL_ADD, td->pipe_read, &td->epoll_ev) < 0);
206 for (i = 0; i < loops; i++) {
209 BUG_ON(ret != sizeof(int));
210 ret = write(td->pipe_write, &m, sizeof(int));
211 BUG_ON(ret != sizeof(int));
213 ret = write(td->pipe_write, &m, sizeof(int));
214 BUG_ON(ret != sizeof(int));
216 BUG_ON(ret != sizeof(int));
223 int bench_sched_pipe(int argc, const char **argv)
225 struct thread_data threads[2] = {};
226 struct thread_data *td;
227 int pipe_1[2], pipe_2[2];
228 struct timeval start, stop, diff;
229 unsigned long long result_usec = 0;
234 * why does "ret" exist?
235 * discarding returned value of read(), write()
236 * causes error in building environment for perf
238 int __maybe_unused ret, wait_stat, flags = 0;
239 pid_t pid, retpid __maybe_unused;
241 argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
246 BUG_ON(pipe2(pipe_1, flags));
247 BUG_ON(pipe2(pipe_2, flags));
249 gettimeofday(&start, NULL);
251 for (t = 0; t < nr_threads; t++) {
257 td->pipe_read = pipe_1[0];
258 td->pipe_write = pipe_2[1];
260 td->pipe_write = pipe_1[1];
261 td->pipe_read = pipe_2[0];
266 for (t = 0; t < nr_threads; t++) {
269 ret = pthread_create(&td->pthread, NULL, worker_thread, td);
273 for (t = 0; t < nr_threads; t++) {
276 ret = pthread_join(td->pthread, NULL);
284 worker_thread(threads + 0);
287 worker_thread(threads + 1);
290 retpid = waitpid(pid, &wait_stat, 0);
291 assert((retpid == pid) && WIFEXITED(wait_stat));
294 gettimeofday(&stop, NULL);
295 timersub(&stop, &start, &diff);
300 if (threads[0].cgroup_failed || threads[1].cgroup_failed)
303 switch (bench_format) {
304 case BENCH_FORMAT_DEFAULT:
305 printf("# Executed %d pipe operations between two %s\n\n",
306 loops, threaded ? "threads" : "processes");
308 result_usec = diff.tv_sec * USEC_PER_SEC;
309 result_usec += diff.tv_usec;
311 printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
312 (unsigned long) diff.tv_sec,
313 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
315 printf(" %14lf usecs/op\n",
316 (double)result_usec / (double)loops);
317 printf(" %14d ops/sec\n",
318 (int)((double)loops /
319 ((double)result_usec / (double)USEC_PER_SEC)));
322 case BENCH_FORMAT_SIMPLE:
323 printf("%lu.%03lu\n",
324 (unsigned long) diff.tv_sec,
325 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
329 /* reaching here is something disaster */
330 fprintf(stderr, "Unknown format:%d\n", bench_format);