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; |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 33 | |
| 34 | static const struct option options[] = { |
| 35 | OPT_INTEGER('l', "loop", &loops, |
| 36 | "Specify number of loops"), |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 37 | OPT_END() |
| 38 | }; |
| 39 | |
| 40 | static const char * const bench_sched_pipe_usage[] = { |
| 41 | "perf bench sched pipe <options>", |
| 42 | NULL |
| 43 | }; |
| 44 | |
| 45 | int bench_sched_pipe(int argc, const char **argv, |
| 46 | const char *prefix __used) |
| 47 | { |
| 48 | int pipe_1[2], pipe_2[2]; |
| 49 | int m = 0, i; |
| 50 | struct timeval start, stop, diff; |
| 51 | unsigned long long result_usec = 0; |
| 52 | |
| 53 | /* |
| 54 | * why does "ret" exist? |
| 55 | * discarding returned value of read(), write() |
| 56 | * causes error in building environment for perf |
| 57 | */ |
Hitoshi Mitake | 5ff0cfc | 2009-11-09 12:31:05 +0900 | [diff] [blame] | 58 | int ret, wait_stat; |
| 59 | pid_t pid, retpid; |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 60 | |
| 61 | argc = parse_options(argc, argv, options, |
| 62 | bench_sched_pipe_usage, 0); |
| 63 | |
| 64 | assert(!pipe(pipe_1)); |
| 65 | assert(!pipe(pipe_2)); |
| 66 | |
| 67 | pid = fork(); |
| 68 | assert(pid >= 0); |
| 69 | |
| 70 | gettimeofday(&start, NULL); |
| 71 | |
| 72 | if (!pid) { |
| 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)); |
| 76 | } |
| 77 | } else { |
| 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)); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | gettimeofday(&stop, NULL); |
| 85 | timersub(&stop, &start, &diff); |
| 86 | |
Hitoshi Mitake | 5ff0cfc | 2009-11-09 12:31:05 +0900 | [diff] [blame] | 87 | if (pid) { |
| 88 | retpid = waitpid(pid, &wait_stat, 0); |
| 89 | assert((retpid == pid) && WIFEXITED(wait_stat)); |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 90 | return 0; |
Hitoshi Mitake | 5ff0cfc | 2009-11-09 12:31:05 +0900 | [diff] [blame] | 91 | } |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 92 | |
Hitoshi Mitake | 158ba82 | 2009-11-10 08:20:02 +0900 | [diff] [blame^] | 93 | switch (bench_format) { |
| 94 | case BENCH_FORMAT_DEFAULT: |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 95 | printf("(executing %d pipe operations between two tasks)\n\n", |
| 96 | loops); |
| 97 | |
| 98 | result_usec = diff.tv_sec * 1000000; |
| 99 | result_usec += diff.tv_usec; |
| 100 | |
| 101 | printf("\tTotal time:%lu.%03lu sec\n", |
| 102 | diff.tv_sec, diff.tv_usec / 1000); |
| 103 | printf("\t\t%lf usecs/op\n", |
| 104 | (double)result_usec / (double)loops); |
| 105 | printf("\t\t%d ops/sec\n", |
| 106 | (int)((double)loops / |
| 107 | ((double)result_usec / (double)1000000))); |
Hitoshi Mitake | 158ba82 | 2009-11-10 08:20:02 +0900 | [diff] [blame^] | 108 | break; |
| 109 | |
| 110 | case BENCH_FORMAT_SIMPLE: |
| 111 | printf("%lu.%03lu\n", |
| 112 | diff.tv_sec, diff.tv_usec / 1000); |
| 113 | break; |
| 114 | |
| 115 | default: |
| 116 | /* reaching here is something disaster */ |
| 117 | fprintf(stderr, "Unknown format:%d\n", bench_format); |
| 118 | exit(1); |
| 119 | break; |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |