Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
c7d9300f HM |
2 | /* |
3 | * | |
2044279d | 4 | * sched-pipe.c |
c7d9300f HM |
5 | * |
6 | * pipe: Benchmark for pipe() | |
7 | * | |
8 | * Based on pipe-test-1m.c by Ingo Molnar <mingo@redhat.com> | |
9 | * http://people.redhat.com/mingo/cfs-scheduler/tools/pipe-test-1m.c | |
10 | * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> | |
c7d9300f | 11 | */ |
4b6ab94e | 12 | #include <subcmd/parse-options.h> |
c7d9300f HM |
13 | #include "bench.h" |
14 | ||
15 | #include <unistd.h> | |
16 | #include <stdio.h> | |
17 | #include <stdlib.h> | |
18 | #include <signal.h> | |
19 | #include <sys/wait.h> | |
c7d9300f HM |
20 | #include <string.h> |
21 | #include <errno.h> | |
22 | #include <assert.h> | |
23 | #include <sys/time.h> | |
5ff0cfc6 | 24 | #include <sys/types.h> |
ea1fe3a8 | 25 | #include <sys/syscall.h> |
16633ccf | 26 | #include <linux/time64.h> |
c7d9300f | 27 | |
a9faa0ca IM |
28 | #include <pthread.h> |
29 | ||
30 | struct thread_data { | |
31 | int nr; | |
32 | int pipe_read; | |
33 | int pipe_write; | |
34 | pthread_t pthread; | |
35 | }; | |
36 | ||
c7d9300f | 37 | #define LOOPS_DEFAULT 1000000 |
a9faa0ca IM |
38 | static int loops = LOOPS_DEFAULT; |
39 | ||
40 | /* Use processes by default: */ | |
41 | static bool threaded; | |
c7d9300f HM |
42 | |
43 | static const struct option options[] = { | |
a9faa0ca IM |
44 | OPT_INTEGER('l', "loop", &loops, "Specify number of loops"), |
45 | OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"), | |
c7d9300f HM |
46 | OPT_END() |
47 | }; | |
48 | ||
49 | static const char * const bench_sched_pipe_usage[] = { | |
50 | "perf bench sched pipe <options>", | |
51 | NULL | |
52 | }; | |
53 | ||
a9faa0ca | 54 | static void *worker_thread(void *__tdata) |
c7d9300f | 55 | { |
a9faa0ca | 56 | struct thread_data *td = __tdata; |
c7d9300f | 57 | int m = 0, i; |
a9faa0ca IM |
58 | int ret; |
59 | ||
60 | for (i = 0; i < loops; i++) { | |
61 | if (!td->nr) { | |
62 | ret = read(td->pipe_read, &m, sizeof(int)); | |
63 | BUG_ON(ret != sizeof(int)); | |
64 | ret = write(td->pipe_write, &m, sizeof(int)); | |
65 | BUG_ON(ret != sizeof(int)); | |
66 | } else { | |
67 | ret = write(td->pipe_write, &m, sizeof(int)); | |
68 | BUG_ON(ret != sizeof(int)); | |
69 | ret = read(td->pipe_read, &m, sizeof(int)); | |
70 | BUG_ON(ret != sizeof(int)); | |
71 | } | |
72 | } | |
73 | ||
74 | return NULL; | |
75 | } | |
76 | ||
b0ad8ea6 | 77 | int bench_sched_pipe(int argc, const char **argv) |
a9faa0ca IM |
78 | { |
79 | struct thread_data threads[2], *td; | |
80 | int pipe_1[2], pipe_2[2]; | |
c7d9300f HM |
81 | struct timeval start, stop, diff; |
82 | unsigned long long result_usec = 0; | |
a9faa0ca IM |
83 | int nr_threads = 2; |
84 | int t; | |
c7d9300f HM |
85 | |
86 | /* | |
87 | * why does "ret" exist? | |
88 | * discarding returned value of read(), write() | |
89 | * causes error in building environment for perf | |
90 | */ | |
1d037ca1 IT |
91 | int __maybe_unused ret, wait_stat; |
92 | pid_t pid, retpid __maybe_unused; | |
c7d9300f | 93 | |
a9faa0ca | 94 | argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0); |
c7d9300f | 95 | |
8bf98b89 IT |
96 | BUG_ON(pipe(pipe_1)); |
97 | BUG_ON(pipe(pipe_2)); | |
c7d9300f | 98 | |
c7d9300f HM |
99 | gettimeofday(&start, NULL); |
100 | ||
a9faa0ca IM |
101 | for (t = 0; t < nr_threads; t++) { |
102 | td = threads + t; | |
103 | ||
104 | td->nr = t; | |
105 | ||
106 | if (t == 0) { | |
107 | td->pipe_read = pipe_1[0]; | |
108 | td->pipe_write = pipe_2[1]; | |
109 | } else { | |
110 | td->pipe_write = pipe_1[1]; | |
111 | td->pipe_read = pipe_2[0]; | |
c7d9300f HM |
112 | } |
113 | } | |
114 | ||
c7d9300f | 115 | |
a9faa0ca IM |
116 | if (threaded) { |
117 | ||
118 | for (t = 0; t < nr_threads; t++) { | |
119 | td = threads + t; | |
120 | ||
121 | ret = pthread_create(&td->pthread, NULL, worker_thread, td); | |
122 | BUG_ON(ret); | |
123 | } | |
124 | ||
125 | for (t = 0; t < nr_threads; t++) { | |
126 | td = threads + t; | |
127 | ||
128 | ret = pthread_join(td->pthread, NULL); | |
129 | BUG_ON(ret); | |
130 | } | |
131 | ||
132 | } else { | |
133 | pid = fork(); | |
134 | assert(pid >= 0); | |
135 | ||
136 | if (!pid) { | |
137 | worker_thread(threads + 0); | |
138 | exit(0); | |
139 | } else { | |
140 | worker_thread(threads + 1); | |
141 | } | |
142 | ||
5ff0cfc6 HM |
143 | retpid = waitpid(pid, &wait_stat, 0); |
144 | assert((retpid == pid) && WIFEXITED(wait_stat)); | |
5ff0cfc6 | 145 | } |
c7d9300f | 146 | |
a9faa0ca IM |
147 | gettimeofday(&stop, NULL); |
148 | timersub(&stop, &start, &diff); | |
149 | ||
158ba827 HM |
150 | switch (bench_format) { |
151 | case BENCH_FORMAT_DEFAULT: | |
a9faa0ca IM |
152 | printf("# Executed %d pipe operations between two %s\n\n", |
153 | loops, threaded ? "threads" : "processes"); | |
c7d9300f | 154 | |
16633ccf | 155 | result_usec = diff.tv_sec * USEC_PER_SEC; |
c7d9300f HM |
156 | result_usec += diff.tv_usec; |
157 | ||
ff676b19 | 158 | printf(" %14s: %lu.%03lu [sec]\n\n", "Total time", |
2cd9046c | 159 | diff.tv_sec, |
16633ccf | 160 | (unsigned long) (diff.tv_usec / USEC_PER_MSEC)); |
ff676b19 HM |
161 | |
162 | printf(" %14lf usecs/op\n", | |
c7d9300f | 163 | (double)result_usec / (double)loops); |
ff676b19 | 164 | printf(" %14d ops/sec\n", |
c7d9300f | 165 | (int)((double)loops / |
16633ccf | 166 | ((double)result_usec / (double)USEC_PER_SEC))); |
158ba827 HM |
167 | break; |
168 | ||
169 | case BENCH_FORMAT_SIMPLE: | |
170 | printf("%lu.%03lu\n", | |
2cd9046c | 171 | diff.tv_sec, |
16633ccf | 172 | (unsigned long) (diff.tv_usec / USEC_PER_MSEC)); |
158ba827 HM |
173 | break; |
174 | ||
175 | default: | |
176 | /* reaching here is something disaster */ | |
177 | fprintf(stderr, "Unknown format:%d\n", bench_format); | |
178 | exit(1); | |
179 | break; | |
c7d9300f HM |
180 | } |
181 | ||
182 | return 0; | |
183 | } |