5 * pipe: Benchmark for pipe()
8 * http://people.redhat.com/mingo/cfs-scheduler/tools/pipe-test-1m.c
12 #include "../util/util.h"
13 #include "../util/parse-options.h"
14 #include "../builtin.h"
22 #include <linux/unistd.h>
27 #include <sys/types.h>
38 #define LOOPS_DEFAULT 1000000
39 static int loops = LOOPS_DEFAULT;
41 /* Use processes by default: */
44 static const struct option options[] = {
45 OPT_INTEGER('l', "loop", &loops, "Specify number of loops"),
46 OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"),
50 static const char * const bench_sched_pipe_usage[] = {
51 "perf bench sched pipe <options>",
55 static void *worker_thread(void *__tdata)
57 struct thread_data *td = __tdata;
61 for (i = 0; i < loops; i++) {
63 ret = read(td->pipe_read, &m, sizeof(int));
64 BUG_ON(ret != sizeof(int));
65 ret = write(td->pipe_write, &m, sizeof(int));
66 BUG_ON(ret != sizeof(int));
68 ret = write(td->pipe_write, &m, sizeof(int));
69 BUG_ON(ret != sizeof(int));
70 ret = read(td->pipe_read, &m, sizeof(int));
71 BUG_ON(ret != sizeof(int));
78 int bench_sched_pipe(int argc, const char **argv, const char *prefix __maybe_unused)
80 struct thread_data threads[2], *td;
81 int pipe_1[2], pipe_2[2];
82 struct timeval start, stop, diff;
83 unsigned long long result_usec = 0;
88 * why does "ret" exist?
89 * discarding returned value of read(), write()
90 * causes error in building environment for perf
92 int __maybe_unused ret, wait_stat;
93 pid_t pid, retpid __maybe_unused;
95 argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
100 gettimeofday(&start, NULL);
102 for (t = 0; t < nr_threads; t++) {
108 td->pipe_read = pipe_1[0];
109 td->pipe_write = pipe_2[1];
111 td->pipe_write = pipe_1[1];
112 td->pipe_read = pipe_2[0];
119 for (t = 0; t < nr_threads; t++) {
122 ret = pthread_create(&td->pthread, NULL, worker_thread, td);
126 for (t = 0; t < nr_threads; t++) {
129 ret = pthread_join(td->pthread, NULL);
138 worker_thread(threads + 0);
141 worker_thread(threads + 1);
144 retpid = waitpid(pid, &wait_stat, 0);
145 assert((retpid == pid) && WIFEXITED(wait_stat));
148 gettimeofday(&stop, NULL);
149 timersub(&stop, &start, &diff);
151 switch (bench_format) {
152 case BENCH_FORMAT_DEFAULT:
153 printf("# Executed %d pipe operations between two %s\n\n",
154 loops, threaded ? "threads" : "processes");
156 result_usec = diff.tv_sec * 1000000;
157 result_usec += diff.tv_usec;
159 printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
161 (unsigned long) (diff.tv_usec/1000));
163 printf(" %14lf usecs/op\n",
164 (double)result_usec / (double)loops);
165 printf(" %14d ops/sec\n",
166 (int)((double)loops /
167 ((double)result_usec / (double)1000000)));
170 case BENCH_FORMAT_SIMPLE:
171 printf("%lu.%03lu\n",
173 (unsigned long) (diff.tv_usec / 1000));
177 /* reaching here is something disaster */
178 fprintf(stderr, "Unknown format:%d\n", bench_format);