Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 1 | /* |
| 2 | * |
Hitoshi Mitake | 2044279 | 2009-12-13 17:01:59 +0900 | [diff] [blame] | 3 | * sched-pipe.c |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 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> |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 10 | */ |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 11 | #include "../perf.h" |
| 12 | #include "../util/util.h" |
| 13 | #include "../util/parse-options.h" |
| 14 | #include "../builtin.h" |
| 15 | #include "bench.h" |
| 16 | |
| 17 | #include <unistd.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <signal.h> |
| 21 | #include <sys/wait.h> |
| 22 | #include <linux/unistd.h> |
| 23 | #include <string.h> |
| 24 | #include <errno.h> |
| 25 | #include <assert.h> |
| 26 | #include <sys/time.h> |
Hitoshi Mitake | 5ff0cfc | 2009-11-09 12:31:05 +0900 | [diff] [blame] | 27 | #include <sys/types.h> |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 28 | |
Ingo Molnar | a9faa0c | 2013-09-23 10:17:35 +0200 | [diff] [blame] | 29 | #include <pthread.h> |
| 30 | |
| 31 | struct thread_data { |
| 32 | int nr; |
| 33 | int pipe_read; |
| 34 | int pipe_write; |
| 35 | pthread_t pthread; |
| 36 | }; |
| 37 | |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 38 | #define LOOPS_DEFAULT 1000000 |
Ingo Molnar | a9faa0c | 2013-09-23 10:17:35 +0200 | [diff] [blame] | 39 | static int loops = LOOPS_DEFAULT; |
| 40 | |
| 41 | /* Use processes by default: */ |
| 42 | static bool threaded; |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 43 | |
| 44 | static const struct option options[] = { |
Ingo Molnar | a9faa0c | 2013-09-23 10:17:35 +0200 | [diff] [blame] | 45 | OPT_INTEGER('l', "loop", &loops, "Specify number of loops"), |
| 46 | OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"), |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 47 | OPT_END() |
| 48 | }; |
| 49 | |
| 50 | static const char * const bench_sched_pipe_usage[] = { |
| 51 | "perf bench sched pipe <options>", |
| 52 | NULL |
| 53 | }; |
| 54 | |
Ingo Molnar | a9faa0c | 2013-09-23 10:17:35 +0200 | [diff] [blame] | 55 | static void *worker_thread(void *__tdata) |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 56 | { |
Ingo Molnar | a9faa0c | 2013-09-23 10:17:35 +0200 | [diff] [blame] | 57 | struct thread_data *td = __tdata; |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 58 | int m = 0, i; |
Ingo Molnar | a9faa0c | 2013-09-23 10:17:35 +0200 | [diff] [blame] | 59 | int ret; |
| 60 | |
| 61 | for (i = 0; i < loops; i++) { |
| 62 | if (!td->nr) { |
| 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)); |
| 67 | } else { |
| 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)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | int bench_sched_pipe(int argc, const char **argv, const char *prefix __maybe_unused) |
| 79 | { |
| 80 | struct thread_data threads[2], *td; |
| 81 | int pipe_1[2], pipe_2[2]; |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 82 | struct timeval start, stop, diff; |
| 83 | unsigned long long result_usec = 0; |
Ingo Molnar | a9faa0c | 2013-09-23 10:17:35 +0200 | [diff] [blame] | 84 | int nr_threads = 2; |
| 85 | int t; |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * why does "ret" exist? |
| 89 | * discarding returned value of read(), write() |
| 90 | * causes error in building environment for perf |
| 91 | */ |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 92 | int __maybe_unused ret, wait_stat; |
| 93 | pid_t pid, retpid __maybe_unused; |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 94 | |
Ingo Molnar | a9faa0c | 2013-09-23 10:17:35 +0200 | [diff] [blame] | 95 | argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0); |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 96 | |
Irina Tirdea | 8bf98b8 | 2012-09-08 08:35:51 +0300 | [diff] [blame] | 97 | BUG_ON(pipe(pipe_1)); |
| 98 | BUG_ON(pipe(pipe_2)); |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 99 | |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 100 | gettimeofday(&start, NULL); |
| 101 | |
Ingo Molnar | a9faa0c | 2013-09-23 10:17:35 +0200 | [diff] [blame] | 102 | for (t = 0; t < nr_threads; t++) { |
| 103 | td = threads + t; |
| 104 | |
| 105 | td->nr = t; |
| 106 | |
| 107 | if (t == 0) { |
| 108 | td->pipe_read = pipe_1[0]; |
| 109 | td->pipe_write = pipe_2[1]; |
| 110 | } else { |
| 111 | td->pipe_write = pipe_1[1]; |
| 112 | td->pipe_read = pipe_2[0]; |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 113 | } |
Ingo Molnar | a9faa0c | 2013-09-23 10:17:35 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
| 117 | if (threaded) { |
| 118 | |
| 119 | for (t = 0; t < nr_threads; t++) { |
| 120 | td = threads + t; |
| 121 | |
| 122 | ret = pthread_create(&td->pthread, NULL, worker_thread, td); |
| 123 | BUG_ON(ret); |
| 124 | } |
| 125 | |
| 126 | for (t = 0; t < nr_threads; t++) { |
| 127 | td = threads + t; |
| 128 | |
| 129 | ret = pthread_join(td->pthread, NULL); |
| 130 | BUG_ON(ret); |
| 131 | } |
| 132 | |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 133 | } else { |
Ingo Molnar | a9faa0c | 2013-09-23 10:17:35 +0200 | [diff] [blame] | 134 | pid = fork(); |
| 135 | assert(pid >= 0); |
| 136 | |
| 137 | if (!pid) { |
| 138 | worker_thread(threads + 0); |
| 139 | exit(0); |
| 140 | } else { |
| 141 | worker_thread(threads + 1); |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 142 | } |
Ingo Molnar | a9faa0c | 2013-09-23 10:17:35 +0200 | [diff] [blame] | 143 | |
| 144 | retpid = waitpid(pid, &wait_stat, 0); |
| 145 | assert((retpid == pid) && WIFEXITED(wait_stat)); |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | gettimeofday(&stop, NULL); |
| 149 | timersub(&stop, &start, &diff); |
| 150 | |
Hitoshi Mitake | 158ba82 | 2009-11-10 08:20:02 +0900 | [diff] [blame] | 151 | switch (bench_format) { |
| 152 | case BENCH_FORMAT_DEFAULT: |
Ingo Molnar | a9faa0c | 2013-09-23 10:17:35 +0200 | [diff] [blame] | 153 | printf("# Executed %d pipe operations between two %s\n\n", |
| 154 | loops, threaded ? "threads" : "processes"); |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 155 | |
| 156 | result_usec = diff.tv_sec * 1000000; |
| 157 | result_usec += diff.tv_usec; |
| 158 | |
Hitoshi Mitake | ff676b1 | 2009-11-11 00:04:01 +0900 | [diff] [blame] | 159 | printf(" %14s: %lu.%03lu [sec]\n\n", "Total time", |
David Miller | 2cd9046 | 2009-12-13 23:56:22 -0800 | [diff] [blame] | 160 | diff.tv_sec, |
| 161 | (unsigned long) (diff.tv_usec/1000)); |
Hitoshi Mitake | ff676b1 | 2009-11-11 00:04:01 +0900 | [diff] [blame] | 162 | |
| 163 | printf(" %14lf usecs/op\n", |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 164 | (double)result_usec / (double)loops); |
Hitoshi Mitake | ff676b1 | 2009-11-11 00:04:01 +0900 | [diff] [blame] | 165 | printf(" %14d ops/sec\n", |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 166 | (int)((double)loops / |
| 167 | ((double)result_usec / (double)1000000))); |
Hitoshi Mitake | 158ba82 | 2009-11-10 08:20:02 +0900 | [diff] [blame] | 168 | break; |
| 169 | |
| 170 | case BENCH_FORMAT_SIMPLE: |
| 171 | printf("%lu.%03lu\n", |
David Miller | 2cd9046 | 2009-12-13 23:56:22 -0800 | [diff] [blame] | 172 | diff.tv_sec, |
| 173 | (unsigned long) (diff.tv_usec / 1000)); |
Hitoshi Mitake | 158ba82 | 2009-11-10 08:20:02 +0900 | [diff] [blame] | 174 | break; |
| 175 | |
| 176 | default: |
| 177 | /* reaching here is something disaster */ |
| 178 | fprintf(stderr, "Unknown format:%d\n", bench_format); |
| 179 | exit(1); |
| 180 | break; |
Hitoshi Mitake | c7d9300 | 2009-11-05 09:31:33 +0900 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | } |