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