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> |
79a3371b | 13 | #include <api/fs/fs.h> |
c7d9300f | 14 | #include "bench.h" |
79a3371b | 15 | #include "util/cgroup.h" |
c7d9300f HM |
16 | |
17 | #include <unistd.h> | |
18 | #include <stdio.h> | |
19 | #include <stdlib.h> | |
20 | #include <signal.h> | |
21 | #include <sys/wait.h> | |
c7d9300f HM |
22 | #include <string.h> |
23 | #include <errno.h> | |
79a3371b | 24 | #include <fcntl.h> |
c7d9300f | 25 | #include <assert.h> |
3e2d4df5 | 26 | #include <sys/epoll.h> |
c7d9300f | 27 | #include <sys/time.h> |
5ff0cfc6 | 28 | #include <sys/types.h> |
ea1fe3a8 | 29 | #include <sys/syscall.h> |
16633ccf | 30 | #include <linux/time64.h> |
c7d9300f | 31 | |
a9faa0ca IM |
32 | #include <pthread.h> |
33 | ||
34 | struct thread_data { | |
35 | int nr; | |
36 | int pipe_read; | |
37 | int pipe_write; | |
3e2d4df5 BG |
38 | struct epoll_event epoll_ev; |
39 | int epoll_fd; | |
79a3371b | 40 | bool cgroup_failed; |
a9faa0ca IM |
41 | pthread_t pthread; |
42 | }; | |
43 | ||
c7d9300f | 44 | #define LOOPS_DEFAULT 1000000 |
a9faa0ca IM |
45 | static int loops = LOOPS_DEFAULT; |
46 | ||
47 | /* Use processes by default: */ | |
48 | static bool threaded; | |
c7d9300f | 49 | |
3e2d4df5 | 50 | static bool nonblocking; |
79a3371b NK |
51 | static char *cgrp_names[2]; |
52 | static struct cgroup *cgrps[2]; | |
53 | ||
54 | static int parse_two_cgroups(const struct option *opt __maybe_unused, | |
55 | const char *str, int unset __maybe_unused) | |
56 | { | |
57 | char *p = strdup(str); | |
58 | char *q; | |
59 | int ret = -1; | |
60 | ||
61 | if (p == NULL) { | |
62 | fprintf(stderr, "memory allocation failure\n"); | |
63 | return -1; | |
64 | } | |
65 | ||
66 | q = strchr(p, ','); | |
67 | if (q == NULL) { | |
68 | fprintf(stderr, "it should have two cgroup names: %s\n", p); | |
69 | goto out; | |
70 | } | |
71 | *q = '\0'; | |
72 | ||
73 | cgrp_names[0] = strdup(p); | |
74 | cgrp_names[1] = strdup(q + 1); | |
75 | ||
76 | if (cgrp_names[0] == NULL || cgrp_names[1] == NULL) { | |
77 | fprintf(stderr, "memory allocation failure\n"); | |
78 | goto out; | |
79 | } | |
80 | ret = 0; | |
81 | ||
82 | out: | |
83 | free(p); | |
84 | return ret; | |
85 | } | |
86 | ||
c7d9300f | 87 | static const struct option options[] = { |
3e2d4df5 | 88 | OPT_BOOLEAN('n', "nonblocking", &nonblocking, "Use non-blocking operations"), |
a9faa0ca IM |
89 | OPT_INTEGER('l', "loop", &loops, "Specify number of loops"), |
90 | OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"), | |
79a3371b NK |
91 | OPT_CALLBACK('G', "cgroups", NULL, "SEND,RECV", |
92 | "Put sender and receivers in given cgroups", | |
93 | parse_two_cgroups), | |
c7d9300f HM |
94 | OPT_END() |
95 | }; | |
96 | ||
97 | static const char * const bench_sched_pipe_usage[] = { | |
98 | "perf bench sched pipe <options>", | |
99 | NULL | |
100 | }; | |
101 | ||
79a3371b NK |
102 | static int enter_cgroup(int nr) |
103 | { | |
104 | char buf[32]; | |
105 | int fd, len, ret; | |
106 | int saved_errno; | |
107 | struct cgroup *cgrp; | |
108 | pid_t pid; | |
109 | ||
110 | if (cgrp_names[nr] == NULL) | |
111 | return 0; | |
112 | ||
113 | if (cgrps[nr] == NULL) { | |
114 | cgrps[nr] = cgroup__new(cgrp_names[nr], /*do_open=*/true); | |
115 | if (cgrps[nr] == NULL) | |
116 | goto err; | |
117 | } | |
118 | cgrp = cgrps[nr]; | |
119 | ||
120 | if (threaded) | |
121 | pid = syscall(__NR_gettid); | |
122 | else | |
123 | pid = getpid(); | |
124 | ||
125 | snprintf(buf, sizeof(buf), "%d\n", pid); | |
126 | len = strlen(buf); | |
127 | ||
128 | /* try cgroup v2 interface first */ | |
129 | if (threaded) | |
130 | fd = openat(cgrp->fd, "cgroup.threads", O_WRONLY); | |
131 | else | |
132 | fd = openat(cgrp->fd, "cgroup.procs", O_WRONLY); | |
133 | ||
134 | /* try cgroup v1 if failed */ | |
135 | if (fd < 0 && errno == ENOENT) | |
136 | fd = openat(cgrp->fd, "tasks", O_WRONLY); | |
137 | ||
138 | if (fd < 0) | |
139 | goto err; | |
140 | ||
141 | ret = write(fd, buf, len); | |
142 | close(fd); | |
143 | ||
144 | if (ret != len) { | |
145 | printf("Cannot enter to cgroup: %s\n", cgrp->name); | |
146 | return -1; | |
147 | } | |
148 | return 0; | |
149 | ||
150 | err: | |
151 | saved_errno = errno; | |
152 | printf("Failed to open cgroup file in %s\n", cgrp_names[nr]); | |
153 | ||
154 | if (saved_errno == ENOENT) { | |
155 | char mnt[PATH_MAX]; | |
156 | ||
157 | if (cgroupfs_find_mountpoint(mnt, sizeof(mnt), "perf_event") == 0) | |
158 | printf(" Hint: create the cgroup first, like 'mkdir %s/%s'\n", | |
159 | mnt, cgrp_names[nr]); | |
160 | } else if (saved_errno == EACCES && geteuid() > 0) { | |
161 | printf(" Hint: try to run as root\n"); | |
162 | } | |
163 | ||
164 | return -1; | |
165 | } | |
166 | ||
167 | static void exit_cgroup(int nr) | |
168 | { | |
169 | cgroup__put(cgrps[nr]); | |
170 | free(cgrp_names[nr]); | |
171 | } | |
172 | ||
3e2d4df5 BG |
173 | static inline int read_pipe(struct thread_data *td) |
174 | { | |
175 | int ret, m; | |
176 | retry: | |
177 | if (nonblocking) { | |
178 | ret = epoll_wait(td->epoll_fd, &td->epoll_ev, 1, -1); | |
179 | if (ret < 0) | |
180 | return ret; | |
181 | } | |
182 | ret = read(td->pipe_read, &m, sizeof(int)); | |
183 | if (nonblocking && ret < 0 && errno == EWOULDBLOCK) | |
184 | goto retry; | |
185 | return ret; | |
186 | } | |
187 | ||
a9faa0ca | 188 | static void *worker_thread(void *__tdata) |
c7d9300f | 189 | { |
a9faa0ca | 190 | struct thread_data *td = __tdata; |
3e2d4df5 | 191 | int i, ret, m = 0; |
a9faa0ca | 192 | |
79a3371b NK |
193 | ret = enter_cgroup(td->nr); |
194 | if (ret < 0) { | |
195 | td->cgroup_failed = true; | |
196 | return NULL; | |
197 | } | |
198 | ||
3e2d4df5 BG |
199 | if (nonblocking) { |
200 | td->epoll_ev.events = EPOLLIN; | |
201 | td->epoll_fd = epoll_create(1); | |
202 | BUG_ON(td->epoll_fd < 0); | |
203 | BUG_ON(epoll_ctl(td->epoll_fd, EPOLL_CTL_ADD, td->pipe_read, &td->epoll_ev) < 0); | |
204 | } | |
205 | ||
a9faa0ca | 206 | for (i = 0; i < loops; i++) { |
99476fa0 DG |
207 | ret = write(td->pipe_write, &m, sizeof(int)); |
208 | BUG_ON(ret != sizeof(int)); | |
209 | ret = read_pipe(td); | |
210 | BUG_ON(ret != sizeof(int)); | |
a9faa0ca IM |
211 | } |
212 | ||
213 | return NULL; | |
214 | } | |
215 | ||
b0ad8ea6 | 216 | int bench_sched_pipe(int argc, const char **argv) |
a9faa0ca | 217 | { |
79a3371b NK |
218 | struct thread_data threads[2] = {}; |
219 | struct thread_data *td; | |
a9faa0ca | 220 | int pipe_1[2], pipe_2[2]; |
c7d9300f HM |
221 | struct timeval start, stop, diff; |
222 | unsigned long long result_usec = 0; | |
a9faa0ca IM |
223 | int nr_threads = 2; |
224 | int t; | |
c7d9300f HM |
225 | |
226 | /* | |
227 | * why does "ret" exist? | |
228 | * discarding returned value of read(), write() | |
229 | * causes error in building environment for perf | |
230 | */ | |
3e2d4df5 | 231 | int __maybe_unused ret, wait_stat, flags = 0; |
1d037ca1 | 232 | pid_t pid, retpid __maybe_unused; |
c7d9300f | 233 | |
a9faa0ca | 234 | argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0); |
c7d9300f | 235 | |
3e2d4df5 BG |
236 | if (nonblocking) |
237 | flags |= O_NONBLOCK; | |
238 | ||
239 | BUG_ON(pipe2(pipe_1, flags)); | |
240 | BUG_ON(pipe2(pipe_2, flags)); | |
c7d9300f | 241 | |
c7d9300f HM |
242 | gettimeofday(&start, NULL); |
243 | ||
a9faa0ca IM |
244 | for (t = 0; t < nr_threads; t++) { |
245 | td = threads + t; | |
246 | ||
247 | td->nr = t; | |
248 | ||
249 | if (t == 0) { | |
250 | td->pipe_read = pipe_1[0]; | |
251 | td->pipe_write = pipe_2[1]; | |
252 | } else { | |
253 | td->pipe_write = pipe_1[1]; | |
254 | td->pipe_read = pipe_2[0]; | |
c7d9300f HM |
255 | } |
256 | } | |
257 | ||
a9faa0ca | 258 | if (threaded) { |
a9faa0ca IM |
259 | for (t = 0; t < nr_threads; t++) { |
260 | td = threads + t; | |
261 | ||
262 | ret = pthread_create(&td->pthread, NULL, worker_thread, td); | |
263 | BUG_ON(ret); | |
264 | } | |
265 | ||
266 | for (t = 0; t < nr_threads; t++) { | |
267 | td = threads + t; | |
268 | ||
269 | ret = pthread_join(td->pthread, NULL); | |
270 | BUG_ON(ret); | |
271 | } | |
a9faa0ca IM |
272 | } else { |
273 | pid = fork(); | |
274 | assert(pid >= 0); | |
275 | ||
276 | if (!pid) { | |
277 | worker_thread(threads + 0); | |
278 | exit(0); | |
279 | } else { | |
280 | worker_thread(threads + 1); | |
281 | } | |
282 | ||
5ff0cfc6 HM |
283 | retpid = waitpid(pid, &wait_stat, 0); |
284 | assert((retpid == pid) && WIFEXITED(wait_stat)); | |
5ff0cfc6 | 285 | } |
c7d9300f | 286 | |
a9faa0ca IM |
287 | gettimeofday(&stop, NULL); |
288 | timersub(&stop, &start, &diff); | |
289 | ||
79a3371b NK |
290 | exit_cgroup(0); |
291 | exit_cgroup(1); | |
292 | ||
293 | if (threads[0].cgroup_failed || threads[1].cgroup_failed) | |
294 | return 0; | |
295 | ||
158ba827 HM |
296 | switch (bench_format) { |
297 | case BENCH_FORMAT_DEFAULT: | |
a9faa0ca IM |
298 | printf("# Executed %d pipe operations between two %s\n\n", |
299 | loops, threaded ? "threads" : "processes"); | |
c7d9300f | 300 | |
16633ccf | 301 | result_usec = diff.tv_sec * USEC_PER_SEC; |
c7d9300f HM |
302 | result_usec += diff.tv_usec; |
303 | ||
ff676b19 | 304 | printf(" %14s: %lu.%03lu [sec]\n\n", "Total time", |
ded2e511 | 305 | (unsigned long) diff.tv_sec, |
16633ccf | 306 | (unsigned long) (diff.tv_usec / USEC_PER_MSEC)); |
ff676b19 HM |
307 | |
308 | printf(" %14lf usecs/op\n", | |
c7d9300f | 309 | (double)result_usec / (double)loops); |
ff676b19 | 310 | printf(" %14d ops/sec\n", |
c7d9300f | 311 | (int)((double)loops / |
16633ccf | 312 | ((double)result_usec / (double)USEC_PER_SEC))); |
158ba827 HM |
313 | break; |
314 | ||
315 | case BENCH_FORMAT_SIMPLE: | |
316 | printf("%lu.%03lu\n", | |
ded2e511 | 317 | (unsigned long) diff.tv_sec, |
16633ccf | 318 | (unsigned long) (diff.tv_usec / USEC_PER_MSEC)); |
158ba827 HM |
319 | break; |
320 | ||
321 | default: | |
322 | /* reaching here is something disaster */ | |
323 | fprintf(stderr, "Unknown format:%d\n", bench_format); | |
324 | exit(1); | |
325 | break; | |
c7d9300f HM |
326 | } |
327 | ||
328 | return 0; | |
329 | } |