Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * builtin-bench-pipe.c |
| 4 | * |
| 5 | * pipe: Benchmark for pipe() |
| 6 | * |
| 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> |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include "../perf.h" |
| 14 | #include "../util/util.h" |
| 15 | #include "../util/parse-options.h" |
| 16 | #include "../builtin.h" |
| 17 | #include "bench.h" |
| 18 | |
| 19 | #include <unistd.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <signal.h> |
| 23 | #include <sys/wait.h> |
| 24 | #include <linux/unistd.h> |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
| 27 | #include <assert.h> |
| 28 | #include <sys/time.h> |
Hitoshi Mitake | 5ff0cfc | 2009-11-09 12:31:05 +0900 | [diff] [blame] | 29 | #include <sys/types.h> |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 30 | |
| 31 | #define LOOPS_DEFAULT 1000000 |
| 32 | static int loops = LOOPS_DEFAULT; |
| 33 | static int simple = 0; |
| 34 | |
| 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)"), |
| 41 | OPT_END() |
| 42 | }; |
| 43 | |
| 44 | static const char * const bench_sched_pipe_usage[] = { |
| 45 | "perf bench sched pipe <options>", |
| 46 | NULL |
| 47 | }; |
| 48 | |
| 49 | int bench_sched_pipe(int argc, const char **argv, |
| 50 | const char *prefix __used) |
| 51 | { |
| 52 | int pipe_1[2], pipe_2[2]; |
| 53 | int m = 0, i; |
| 54 | struct timeval start, stop, diff; |
| 55 | unsigned long long result_usec = 0; |
| 56 | |
| 57 | /* |
| 58 | * why does "ret" exist? |
| 59 | * discarding returned value of read(), write() |
| 60 | * causes error in building environment for perf |
| 61 | */ |
Hitoshi Mitake | 5ff0cfc | 2009-11-09 12:31:05 +0900 | [diff] [blame] | 62 | int ret, wait_stat; |
| 63 | pid_t pid, retpid; |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 64 | |
| 65 | argc = parse_options(argc, argv, options, |
| 66 | bench_sched_pipe_usage, 0); |
| 67 | |
| 68 | assert(!pipe(pipe_1)); |
| 69 | assert(!pipe(pipe_2)); |
| 70 | |
| 71 | pid = fork(); |
| 72 | assert(pid >= 0); |
| 73 | |
| 74 | gettimeofday(&start, NULL); |
| 75 | |
| 76 | if (!pid) { |
| 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)); |
| 80 | } |
| 81 | } else { |
| 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)); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | gettimeofday(&stop, NULL); |
| 89 | timersub(&stop, &start, &diff); |
| 90 | |
Hitoshi Mitake | 5ff0cfc | 2009-11-09 12:31:05 +0900 | [diff] [blame] | 91 | if (pid) { |
| 92 | retpid = waitpid(pid, &wait_stat, 0); |
| 93 | assert((retpid == pid) && WIFEXITED(wait_stat)); |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 94 | return 0; |
Hitoshi Mitake | 5ff0cfc | 2009-11-09 12:31:05 +0900 | [diff] [blame] | 95 | } |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 96 | |
| 97 | if (simple) |
| 98 | printf("%lu.%03lu\n", |
| 99 | diff.tv_sec, diff.tv_usec / 1000); |
| 100 | else { |
| 101 | printf("(executing %d pipe operations between two tasks)\n\n", |
| 102 | loops); |
| 103 | |
| 104 | result_usec = diff.tv_sec * 1000000; |
| 105 | result_usec += diff.tv_usec; |
| 106 | |
| 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))); |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |