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>
24 #include <sys/types.h>
25 #include <sys/syscall.h>
26 #include <linux/time64.h>
37 #define LOOPS_DEFAULT 1000000
38 static int loops = LOOPS_DEFAULT;
40 /* Use processes by default: */
43 static const struct option options[] = {
44 OPT_INTEGER('l', "loop", &loops, "Specify number of loops"),
45 OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"),
49 static const char * const bench_sched_pipe_usage[] = {
50 "perf bench sched pipe <options>",
54 static void *worker_thread(void *__tdata)
56 struct thread_data *td = __tdata;
60 for (i = 0; i < loops; i++) {
62 ret = read(td->pipe_read, &m, sizeof(int));
63 BUG_ON(ret != sizeof(int));
64 ret = write(td->pipe_write, &m, sizeof(int));
65 BUG_ON(ret != sizeof(int));
67 ret = write(td->pipe_write, &m, sizeof(int));
68 BUG_ON(ret != sizeof(int));
69 ret = read(td->pipe_read, &m, sizeof(int));
70 BUG_ON(ret != sizeof(int));
77 int bench_sched_pipe(int argc, const char **argv)
79 struct thread_data threads[2], *td;
80 int pipe_1[2], pipe_2[2];
81 struct timeval start, stop, diff;
82 unsigned long long result_usec = 0;
87 * why does "ret" exist?
88 * discarding returned value of read(), write()
89 * causes error in building environment for perf
91 int __maybe_unused ret, wait_stat;
92 pid_t pid, retpid __maybe_unused;
94 argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
99 gettimeofday(&start, NULL);
101 for (t = 0; t < nr_threads; t++) {
107 td->pipe_read = pipe_1[0];
108 td->pipe_write = pipe_2[1];
110 td->pipe_write = pipe_1[1];
111 td->pipe_read = pipe_2[0];
118 for (t = 0; t < nr_threads; t++) {
121 ret = pthread_create(&td->pthread, NULL, worker_thread, td);
125 for (t = 0; t < nr_threads; t++) {
128 ret = pthread_join(td->pthread, NULL);
137 worker_thread(threads + 0);
140 worker_thread(threads + 1);
143 retpid = waitpid(pid, &wait_stat, 0);
144 assert((retpid == pid) && WIFEXITED(wait_stat));
147 gettimeofday(&stop, NULL);
148 timersub(&stop, &start, &diff);
150 switch (bench_format) {
151 case BENCH_FORMAT_DEFAULT:
152 printf("# Executed %d pipe operations between two %s\n\n",
153 loops, threaded ? "threads" : "processes");
155 result_usec = diff.tv_sec * USEC_PER_SEC;
156 result_usec += diff.tv_usec;
158 printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
159 (unsigned long) diff.tv_sec,
160 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
162 printf(" %14lf usecs/op\n",
163 (double)result_usec / (double)loops);
164 printf(" %14d ops/sec\n",
165 (int)((double)loops /
166 ((double)result_usec / (double)USEC_PER_SEC)));
169 case BENCH_FORMAT_SIMPLE:
170 printf("%lu.%03lu\n",
171 (unsigned long) diff.tv_sec,
172 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
176 /* reaching here is something disaster */
177 fprintf(stderr, "Unknown format:%d\n", bench_format);