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