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>
14 #include "../util/util.h"
15 #include "../util/parse-options.h"
16 #include "../builtin.h"
24 #include <linux/unistd.h>
29 #include <sys/types.h>
31 #define LOOPS_DEFAULT 1000000
32 static int loops = LOOPS_DEFAULT;
33 static int simple = 0;
35 static const struct option options[] = {
36 OPT_INTEGER('l', "loop", &loops,
37 "Specify number of loops"),
38 OPT_BOOLEAN('s', "simple-output", &simple,
39 "Do simple output (this maybe useful for"
40 "processing by scripts or graph tools like gnuplot)"),
44 static const char * const bench_sched_pipe_usage[] = {
45 "perf bench sched pipe <options>",
49 int bench_sched_pipe(int argc, const char **argv,
50 const char *prefix __used)
52 int pipe_1[2], pipe_2[2];
54 struct timeval start, stop, diff;
55 unsigned long long result_usec = 0;
58 * why does "ret" exist?
59 * discarding returned value of read(), write()
60 * causes error in building environment for perf
65 argc = parse_options(argc, argv, options,
66 bench_sched_pipe_usage, 0);
68 assert(!pipe(pipe_1));
69 assert(!pipe(pipe_2));
74 gettimeofday(&start, NULL);
77 for (i = 0; i < loops; i++) {
78 ret = read(pipe_1[0], &m, sizeof(int));
79 ret = write(pipe_2[1], &m, sizeof(int));
82 for (i = 0; i < loops; i++) {
83 ret = write(pipe_1[1], &m, sizeof(int));
84 ret = read(pipe_2[0], &m, sizeof(int));
88 gettimeofday(&stop, NULL);
89 timersub(&stop, &start, &diff);
92 retpid = waitpid(pid, &wait_stat, 0);
93 assert((retpid == pid) && WIFEXITED(wait_stat));
99 diff.tv_sec, diff.tv_usec / 1000);
101 printf("(executing %d pipe operations between two tasks)\n\n",
104 result_usec = diff.tv_sec * 1000000;
105 result_usec += diff.tv_usec;
107 printf("\tTotal time:%lu.%03lu sec\n",
108 diff.tv_sec, diff.tv_usec / 1000);
109 printf("\t\t%lf usecs/op\n",
110 (double)result_usec / (double)loops);
111 printf("\t\t%d ops/sec\n",
112 (int)((double)loops /
113 ((double)result_usec / (double)1000000)));