Loading...
1/*
2 *
3 * sched-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#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>
27#include <sys/types.h>
28
29#include <pthread.h>
30
31struct thread_data {
32 int nr;
33 int pipe_read;
34 int pipe_write;
35 pthread_t pthread;
36};
37
38#define LOOPS_DEFAULT 1000000
39static int loops = LOOPS_DEFAULT;
40
41/* Use processes by default: */
42static bool threaded;
43
44static const struct option options[] = {
45 OPT_INTEGER('l', "loop", &loops, "Specify number of loops"),
46 OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"),
47 OPT_END()
48};
49
50static const char * const bench_sched_pipe_usage[] = {
51 "perf bench sched pipe <options>",
52 NULL
53};
54
55static void *worker_thread(void *__tdata)
56{
57 struct thread_data *td = __tdata;
58 int m = 0, i;
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
78int 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];
82 struct timeval start, stop, diff;
83 unsigned long long result_usec = 0;
84 int nr_threads = 2;
85 int t;
86
87 /*
88 * why does "ret" exist?
89 * discarding returned value of read(), write()
90 * causes error in building environment for perf
91 */
92 int __maybe_unused ret, wait_stat;
93 pid_t pid, retpid __maybe_unused;
94
95 argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
96
97 BUG_ON(pipe(pipe_1));
98 BUG_ON(pipe(pipe_2));
99
100 gettimeofday(&start, NULL);
101
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];
113 }
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
133 } else {
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);
142 }
143
144 retpid = waitpid(pid, &wait_stat, 0);
145 assert((retpid == pid) && WIFEXITED(wait_stat));
146 }
147
148 gettimeofday(&stop, NULL);
149 timersub(&stop, &start, &diff);
150
151 switch (bench_format) {
152 case BENCH_FORMAT_DEFAULT:
153 printf("# Executed %d pipe operations between two %s\n\n",
154 loops, threaded ? "threads" : "processes");
155
156 result_usec = diff.tv_sec * 1000000;
157 result_usec += diff.tv_usec;
158
159 printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
160 diff.tv_sec,
161 (unsigned long) (diff.tv_usec/1000));
162
163 printf(" %14lf usecs/op\n",
164 (double)result_usec / (double)loops);
165 printf(" %14d ops/sec\n",
166 (int)((double)loops /
167 ((double)result_usec / (double)1000000)));
168 break;
169
170 case BENCH_FORMAT_SIMPLE:
171 printf("%lu.%03lu\n",
172 diff.tv_sec,
173 (unsigned long) (diff.tv_usec / 1000));
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;
181 }
182
183 return 0;
184}
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}