5 * pipe: Benchmark for pipe()
7 * Based on pipe-test-1m.c by Ingo Molnar <mingo@redhat.com>
8 * http://people.redhat.com/mingo/cfs-scheduler/tools/pipe-test-1m.c
9 * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
12 #include "../util/util.h"
13 #include <subcmd/parse-options.h>
14 #include "../builtin.h"
26 #include <sys/types.h>
27 #include <sys/syscall.h>
28 #include <linux/time64.h>
39 #define LOOPS_DEFAULT 1000000
40 static int loops = LOOPS_DEFAULT;
42 /* Use processes by default: */
45 static const struct option options[] = {
46 OPT_INTEGER('l', "loop", &loops, "Specify number of loops"),
47 OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"),
51 static const char * const bench_sched_pipe_usage[] = {
52 "perf bench sched pipe <options>",
56 static void *worker_thread(void *__tdata)
58 struct thread_data *td = __tdata;
62 for (i = 0; i < loops; i++) {
64 ret = read(td->pipe_read, &m, sizeof(int));
65 BUG_ON(ret != sizeof(int));
66 ret = write(td->pipe_write, &m, sizeof(int));
67 BUG_ON(ret != sizeof(int));
69 ret = write(td->pipe_write, &m, sizeof(int));
70 BUG_ON(ret != sizeof(int));
71 ret = read(td->pipe_read, &m, sizeof(int));
72 BUG_ON(ret != sizeof(int));
79 int bench_sched_pipe(int argc, const char **argv, const char *prefix __maybe_unused)
81 struct thread_data threads[2], *td;
82 int pipe_1[2], pipe_2[2];
83 struct timeval start, stop, diff;
84 unsigned long long result_usec = 0;
89 * why does "ret" exist?
90 * discarding returned value of read(), write()
91 * causes error in building environment for perf
93 int __maybe_unused ret, wait_stat;
94 pid_t pid, retpid __maybe_unused;
96 argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
101 gettimeofday(&start, NULL);
103 for (t = 0; t < nr_threads; t++) {
109 td->pipe_read = pipe_1[0];
110 td->pipe_write = pipe_2[1];
112 td->pipe_write = pipe_1[1];
113 td->pipe_read = pipe_2[0];
120 for (t = 0; t < nr_threads; t++) {
123 ret = pthread_create(&td->pthread, NULL, worker_thread, td);
127 for (t = 0; t < nr_threads; t++) {
130 ret = pthread_join(td->pthread, NULL);
139 worker_thread(threads + 0);
142 worker_thread(threads + 1);
145 retpid = waitpid(pid, &wait_stat, 0);
146 assert((retpid == pid) && WIFEXITED(wait_stat));
149 gettimeofday(&stop, NULL);
150 timersub(&stop, &start, &diff);
152 switch (bench_format) {
153 case BENCH_FORMAT_DEFAULT:
154 printf("# Executed %d pipe operations between two %s\n\n",
155 loops, threaded ? "threads" : "processes");
157 result_usec = diff.tv_sec * USEC_PER_SEC;
158 result_usec += diff.tv_usec;
160 printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
162 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
164 printf(" %14lf usecs/op\n",
165 (double)result_usec / (double)loops);
166 printf(" %14d ops/sec\n",
167 (int)((double)loops /
168 ((double)result_usec / (double)USEC_PER_SEC)));
171 case BENCH_FORMAT_SIMPLE:
172 printf("%lu.%03lu\n",
174 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
178 /* reaching here is something disaster */
179 fprintf(stderr, "Unknown format:%d\n", bench_format);