Commit | Line | Data |
---|---|---|
e27454cc HM |
1 | /* |
2 | * | |
2044279d | 3 | * sched-messaging.c |
e27454cc HM |
4 | * |
5 | * messaging: Benchmark for scheduler and IPC mechanisms | |
6 | * | |
7 | * Based on hackbench by Rusty Russell <rusty@rustcorp.com.au> | |
8 | * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> | |
9 | * | |
10 | */ | |
11 | ||
12 | #include "../perf.h" | |
13 | #include "../util/util.h" | |
4b6ab94e | 14 | #include <subcmd/parse-options.h> |
e27454cc HM |
15 | #include "../builtin.h" |
16 | #include "bench.h" | |
17 | ||
18 | /* Test groups of 20 processes spraying to 20 receivers */ | |
19 | #include <pthread.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <errno.h> | |
24 | #include <unistd.h> | |
25 | #include <sys/types.h> | |
26 | #include <sys/socket.h> | |
27 | #include <sys/wait.h> | |
28 | #include <sys/time.h> | |
a8fa4960 | 29 | #include <poll.h> |
e27454cc | 30 | #include <limits.h> |
ecdac968 | 31 | #include <err.h> |
af15e67e | 32 | #include <linux/time64.h> |
e27454cc HM |
33 | |
34 | #define DATASIZE 100 | |
35 | ||
c0555642 | 36 | static bool use_pipes = false; |
b0d22e52 | 37 | static unsigned int nr_loops = 100; |
c0555642 | 38 | static bool thread_mode = false; |
e27454cc | 39 | static unsigned int num_groups = 10; |
e27454cc HM |
40 | |
41 | struct sender_context { | |
42 | unsigned int num_fds; | |
43 | int ready_out; | |
44 | int wakefd; | |
45 | int out_fds[0]; | |
46 | }; | |
47 | ||
48 | struct receiver_context { | |
49 | unsigned int num_packets; | |
50 | int in_fds[2]; | |
51 | int ready_out; | |
52 | int wakefd; | |
53 | }; | |
54 | ||
e27454cc HM |
55 | static void fdpair(int fds[2]) |
56 | { | |
57 | if (use_pipes) { | |
58 | if (pipe(fds) == 0) | |
59 | return; | |
60 | } else { | |
61 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0) | |
62 | return; | |
63 | } | |
64 | ||
ecdac968 | 65 | err(EXIT_FAILURE, use_pipes ? "pipe()" : "socketpair()"); |
e27454cc HM |
66 | } |
67 | ||
68 | /* Block until we're ready to go */ | |
69 | static void ready(int ready_out, int wakefd) | |
70 | { | |
71 | char dummy; | |
72 | struct pollfd pollfd = { .fd = wakefd, .events = POLLIN }; | |
73 | ||
74 | /* Tell them we're ready. */ | |
75 | if (write(ready_out, &dummy, 1) != 1) | |
ecdac968 | 76 | err(EXIT_FAILURE, "CLIENT: ready write"); |
e27454cc HM |
77 | |
78 | /* Wait for "GO" signal */ | |
79 | if (poll(&pollfd, 1, -1) != 1) | |
ecdac968 | 80 | err(EXIT_FAILURE, "poll"); |
e27454cc HM |
81 | } |
82 | ||
b0d22e52 | 83 | /* Sender sprays nr_loops messages down each file descriptor */ |
e27454cc HM |
84 | static void *sender(struct sender_context *ctx) |
85 | { | |
86 | char data[DATASIZE]; | |
87 | unsigned int i, j; | |
88 | ||
89 | ready(ctx->ready_out, ctx->wakefd); | |
90 | ||
91 | /* Now pump to every receiver. */ | |
b0d22e52 | 92 | for (i = 0; i < nr_loops; i++) { |
e27454cc HM |
93 | for (j = 0; j < ctx->num_fds; j++) { |
94 | int ret, done = 0; | |
95 | ||
96 | again: | |
97 | ret = write(ctx->out_fds[j], data + done, | |
98 | sizeof(data)-done); | |
99 | if (ret < 0) | |
ecdac968 | 100 | err(EXIT_FAILURE, "SENDER: write"); |
e27454cc HM |
101 | done += ret; |
102 | if (done < DATASIZE) | |
103 | goto again; | |
104 | } | |
105 | } | |
106 | ||
107 | return NULL; | |
108 | } | |
109 | ||
110 | ||
111 | /* One receiver per fd */ | |
112 | static void *receiver(struct receiver_context* ctx) | |
113 | { | |
114 | unsigned int i; | |
115 | ||
116 | if (!thread_mode) | |
117 | close(ctx->in_fds[1]); | |
118 | ||
119 | /* Wait for start... */ | |
120 | ready(ctx->ready_out, ctx->wakefd); | |
121 | ||
122 | /* Receive them all */ | |
123 | for (i = 0; i < ctx->num_packets; i++) { | |
124 | char data[DATASIZE]; | |
125 | int ret, done = 0; | |
126 | ||
127 | again: | |
128 | ret = read(ctx->in_fds[0], data + done, DATASIZE - done); | |
129 | if (ret < 0) | |
ecdac968 | 130 | err(EXIT_FAILURE, "SERVER: read"); |
e27454cc HM |
131 | done += ret; |
132 | if (done < DATASIZE) | |
133 | goto again; | |
134 | } | |
135 | ||
136 | return NULL; | |
137 | } | |
138 | ||
139 | static pthread_t create_worker(void *ctx, void *(*func)(void *)) | |
140 | { | |
141 | pthread_attr_t attr; | |
142 | pthread_t childid; | |
ecdac968 | 143 | int ret; |
e27454cc HM |
144 | |
145 | if (!thread_mode) { | |
146 | /* process mode */ | |
147 | /* Fork the receiver. */ | |
148 | switch (fork()) { | |
149 | case -1: | |
ecdac968 | 150 | err(EXIT_FAILURE, "fork()"); |
e27454cc HM |
151 | break; |
152 | case 0: | |
153 | (*func) (ctx); | |
154 | exit(0); | |
155 | break; | |
156 | default: | |
157 | break; | |
158 | } | |
159 | ||
160 | return (pthread_t)0; | |
161 | } | |
162 | ||
163 | if (pthread_attr_init(&attr) != 0) | |
ecdac968 | 164 | err(EXIT_FAILURE, "pthread_attr_init:"); |
e27454cc HM |
165 | |
166 | #ifndef __ia64__ | |
167 | if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0) | |
ecdac968 | 168 | err(EXIT_FAILURE, "pthread_attr_setstacksize"); |
e27454cc HM |
169 | #endif |
170 | ||
ecdac968 DB |
171 | ret = pthread_create(&childid, &attr, func, ctx); |
172 | if (ret != 0) | |
173 | err(EXIT_FAILURE, "pthread_create failed"); | |
174 | ||
e27454cc HM |
175 | return childid; |
176 | } | |
177 | ||
178 | static void reap_worker(pthread_t id) | |
179 | { | |
180 | int proc_status; | |
181 | void *thread_status; | |
182 | ||
183 | if (!thread_mode) { | |
184 | /* process mode */ | |
185 | wait(&proc_status); | |
186 | if (!WIFEXITED(proc_status)) | |
187 | exit(1); | |
188 | } else { | |
189 | pthread_join(id, &thread_status); | |
190 | } | |
191 | } | |
192 | ||
193 | /* One group of senders and receivers */ | |
194 | static unsigned int group(pthread_t *pth, | |
195 | unsigned int num_fds, | |
196 | int ready_out, | |
197 | int wakefd) | |
198 | { | |
199 | unsigned int i; | |
200 | struct sender_context *snd_ctx = malloc(sizeof(struct sender_context) | |
201 | + num_fds * sizeof(int)); | |
202 | ||
203 | if (!snd_ctx) | |
ecdac968 | 204 | err(EXIT_FAILURE, "malloc()"); |
e27454cc HM |
205 | |
206 | for (i = 0; i < num_fds; i++) { | |
207 | int fds[2]; | |
208 | struct receiver_context *ctx = malloc(sizeof(*ctx)); | |
209 | ||
210 | if (!ctx) | |
ecdac968 | 211 | err(EXIT_FAILURE, "malloc()"); |
e27454cc HM |
212 | |
213 | ||
214 | /* Create the pipe between client and server */ | |
215 | fdpair(fds); | |
216 | ||
b0d22e52 | 217 | ctx->num_packets = num_fds * nr_loops; |
e27454cc HM |
218 | ctx->in_fds[0] = fds[0]; |
219 | ctx->in_fds[1] = fds[1]; | |
220 | ctx->ready_out = ready_out; | |
221 | ctx->wakefd = wakefd; | |
222 | ||
223 | pth[i] = create_worker(ctx, (void *)receiver); | |
224 | ||
225 | snd_ctx->out_fds[i] = fds[1]; | |
226 | if (!thread_mode) | |
227 | close(fds[0]); | |
228 | } | |
229 | ||
230 | /* Now we have all the fds, fork the senders */ | |
231 | for (i = 0; i < num_fds; i++) { | |
232 | snd_ctx->ready_out = ready_out; | |
233 | snd_ctx->wakefd = wakefd; | |
234 | snd_ctx->num_fds = num_fds; | |
235 | ||
236 | pth[num_fds+i] = create_worker(snd_ctx, (void *)sender); | |
237 | } | |
238 | ||
239 | /* Close the fds we have left */ | |
240 | if (!thread_mode) | |
241 | for (i = 0; i < num_fds; i++) | |
242 | close(snd_ctx->out_fds[i]); | |
243 | ||
244 | /* Return number of children to reap */ | |
245 | return num_fds * 2; | |
246 | } | |
247 | ||
248 | static const struct option options[] = { | |
249 | OPT_BOOLEAN('p', "pipe", &use_pipes, | |
250 | "Use pipe() instead of socketpair()"), | |
251 | OPT_BOOLEAN('t', "thread", &thread_mode, | |
252 | "Be multi thread instead of multi process"), | |
1967936d | 253 | OPT_UINTEGER('g', "group", &num_groups, "Specify number of groups"), |
b0d22e52 | 254 | OPT_UINTEGER('l', "nr_loops", &nr_loops, "Specify the number of loops to run (default: 100)"), |
e27454cc HM |
255 | OPT_END() |
256 | }; | |
257 | ||
258 | static const char * const bench_sched_message_usage[] = { | |
259 | "perf bench sched messaging <options>", | |
260 | NULL | |
261 | }; | |
262 | ||
263 | int bench_sched_messaging(int argc, const char **argv, | |
1d037ca1 | 264 | const char *prefix __maybe_unused) |
e27454cc HM |
265 | { |
266 | unsigned int i, total_children; | |
267 | struct timeval start, stop, diff; | |
268 | unsigned int num_fds = 20; | |
269 | int readyfds[2], wakefds[2]; | |
270 | char dummy; | |
271 | pthread_t *pth_tab; | |
272 | ||
273 | argc = parse_options(argc, argv, options, | |
274 | bench_sched_message_usage, 0); | |
275 | ||
276 | pth_tab = malloc(num_fds * 2 * num_groups * sizeof(pthread_t)); | |
277 | if (!pth_tab) | |
ecdac968 | 278 | err(EXIT_FAILURE, "main:malloc()"); |
e27454cc HM |
279 | |
280 | fdpair(readyfds); | |
281 | fdpair(wakefds); | |
282 | ||
283 | total_children = 0; | |
284 | for (i = 0; i < num_groups; i++) | |
285 | total_children += group(pth_tab+total_children, num_fds, | |
286 | readyfds[1], wakefds[0]); | |
287 | ||
288 | /* Wait for everyone to be ready */ | |
289 | for (i = 0; i < total_children; i++) | |
290 | if (read(readyfds[0], &dummy, 1) != 1) | |
ecdac968 | 291 | err(EXIT_FAILURE, "Reading for readyfds"); |
e27454cc HM |
292 | |
293 | gettimeofday(&start, NULL); | |
294 | ||
295 | /* Kick them off */ | |
296 | if (write(wakefds[1], &dummy, 1) != 1) | |
ecdac968 | 297 | err(EXIT_FAILURE, "Writing to start them"); |
e27454cc HM |
298 | |
299 | /* Reap them all */ | |
300 | for (i = 0; i < total_children; i++) | |
301 | reap_worker(pth_tab[i]); | |
302 | ||
303 | gettimeofday(&stop, NULL); | |
304 | ||
305 | timersub(&stop, &start, &diff); | |
306 | ||
cced06c6 HM |
307 | switch (bench_format) { |
308 | case BENCH_FORMAT_DEFAULT: | |
c5659b74 | 309 | printf("# %d sender and receiver %s per group\n", |
e27454cc | 310 | num_fds, thread_mode ? "threads" : "processes"); |
c5659b74 | 311 | printf("# %d groups == %d %s run\n\n", |
e27454cc HM |
312 | num_groups, num_groups * 2 * num_fds, |
313 | thread_mode ? "threads" : "processes"); | |
c5659b74 | 314 | printf(" %14s: %lu.%03lu [sec]\n", "Total time", |
2cd9046c | 315 | diff.tv_sec, |
af15e67e | 316 | (unsigned long) (diff.tv_usec / USEC_PER_MSEC)); |
cced06c6 HM |
317 | break; |
318 | case BENCH_FORMAT_SIMPLE: | |
2cd9046c | 319 | printf("%lu.%03lu\n", diff.tv_sec, |
af15e67e | 320 | (unsigned long) (diff.tv_usec / USEC_PER_MSEC)); |
cced06c6 HM |
321 | break; |
322 | default: | |
323 | /* reaching here is something disaster */ | |
324 | fprintf(stderr, "Unknown format:%d\n", bench_format); | |
325 | exit(1); | |
326 | break; | |
e27454cc HM |
327 | } |
328 | ||
b094c99e DB |
329 | free(pth_tab); |
330 | ||
e27454cc HM |
331 | return 0; |
332 | } |