1 // SPDX-License-Identifier: GPL-2.0
6 * pipe: Benchmark for pipe()
8 * Based on pipe-test-1m.c by Ingo Molnar <mingo@redhat.com>
9 * http://people.redhat.com/mingo/cfs-scheduler/tools/pipe-test-1m.c
10 * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
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++) {
207 ret = write(td->pipe_write, &m, sizeof(int));
208 BUG_ON(ret != sizeof(int));
210 BUG_ON(ret != sizeof(int));
216 int bench_sched_pipe(int argc, const char **argv)
218 struct thread_data threads[2] = {};
219 struct thread_data *td;
220 int pipe_1[2], pipe_2[2];
221 struct timeval start, stop, diff;
222 unsigned long long result_usec = 0;
227 * why does "ret" exist?
228 * discarding returned value of read(), write()
229 * causes error in building environment for perf
231 int __maybe_unused ret, wait_stat, flags = 0;
232 pid_t pid, retpid __maybe_unused;
234 argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
239 BUG_ON(pipe2(pipe_1, flags));
240 BUG_ON(pipe2(pipe_2, flags));
242 gettimeofday(&start, NULL);
244 for (t = 0; t < nr_threads; t++) {
250 td->pipe_read = pipe_1[0];
251 td->pipe_write = pipe_2[1];
253 td->pipe_write = pipe_1[1];
254 td->pipe_read = pipe_2[0];
259 for (t = 0; t < nr_threads; t++) {
262 ret = pthread_create(&td->pthread, NULL, worker_thread, td);
266 for (t = 0; t < nr_threads; t++) {
269 ret = pthread_join(td->pthread, NULL);
277 worker_thread(threads + 0);
280 worker_thread(threads + 1);
283 retpid = waitpid(pid, &wait_stat, 0);
284 assert((retpid == pid) && WIFEXITED(wait_stat));
287 gettimeofday(&stop, NULL);
288 timersub(&stop, &start, &diff);
293 if (threads[0].cgroup_failed || threads[1].cgroup_failed)
296 switch (bench_format) {
297 case BENCH_FORMAT_DEFAULT:
298 printf("# Executed %d pipe operations between two %s\n\n",
299 loops, threaded ? "threads" : "processes");
301 result_usec = diff.tv_sec * USEC_PER_SEC;
302 result_usec += diff.tv_usec;
304 printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
305 (unsigned long) diff.tv_sec,
306 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
308 printf(" %14lf usecs/op\n",
309 (double)result_usec / (double)loops);
310 printf(" %14d ops/sec\n",
311 (int)((double)loops /
312 ((double)result_usec / (double)USEC_PER_SEC)));
315 case BENCH_FORMAT_SIMPLE:
316 printf("%lu.%03lu\n",
317 (unsigned long) diff.tv_sec,
318 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
322 /* reaching here is something disaster */
323 fprintf(stderr, "Unknown format:%d\n", bench_format);