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
;
34 static const struct option options
[] = {
35 OPT_INTEGER('l', "loop", &loops
,
36 "Specify number of loops"),
40 static const char * const bench_sched_pipe_usage
[] = {
41 "perf bench sched pipe <options>",
45 int bench_sched_pipe(int argc
, const char **argv
,
46 const char *prefix __maybe_unused
)
48 int pipe_1
[2], pipe_2
[2];
50 struct timeval start
, stop
, diff
;
51 unsigned long long result_usec
= 0;
54 * why does "ret" exist?
55 * discarding returned value of read(), write()
56 * causes error in building environment for perf
58 int __maybe_unused ret
, wait_stat
;
59 pid_t pid
, retpid __maybe_unused
;
61 argc
= parse_options(argc
, argv
, options
,
62 bench_sched_pipe_usage
, 0);
70 gettimeofday(&start
, NULL
);
73 for (i
= 0; i
< loops
; i
++) {
74 ret
= read(pipe_1
[0], &m
, sizeof(int));
75 ret
= write(pipe_2
[1], &m
, sizeof(int));
78 for (i
= 0; i
< loops
; i
++) {
79 ret
= write(pipe_1
[1], &m
, sizeof(int));
80 ret
= read(pipe_2
[0], &m
, sizeof(int));
84 gettimeofday(&stop
, NULL
);
85 timersub(&stop
, &start
, &diff
);
88 retpid
= waitpid(pid
, &wait_stat
, 0);
89 assert((retpid
== pid
) && WIFEXITED(wait_stat
));
94 switch (bench_format
) {
95 case BENCH_FORMAT_DEFAULT
:
96 printf("# Executed %d pipe operations between two tasks\n\n",
99 result_usec
= diff
.tv_sec
* 1000000;
100 result_usec
+= diff
.tv_usec
;
102 printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
104 (unsigned long) (diff
.tv_usec
/1000));
106 printf(" %14lf usecs/op\n",
107 (double)result_usec
/ (double)loops
);
108 printf(" %14d ops/sec\n",
109 (int)((double)loops
/
110 ((double)result_usec
/ (double)1000000)));
113 case BENCH_FORMAT_SIMPLE
:
114 printf("%lu.%03lu\n",
116 (unsigned long) (diff
.tv_usec
/ 1000));
120 /* reaching here is something disaster */
121 fprintf(stderr
, "Unknown format:%d\n", bench_format
);