diff options
| author | Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> | 2009-11-04 19:31:32 -0500 |
|---|---|---|
| committer | Ingo Molnar <mingo@elte.hu> | 2009-11-08 04:19:17 -0500 |
| commit | e27454cc6352c4226ddc76f5e3a5dedd7dff456a (patch) | |
| tree | 36639fba877907ac8a2f991fc3bfde7c241b6a0f | |
| parent | c426bba069e65ea438880a04aa4e7c5b880e1728 (diff) | |
perf bench: Add sched-messaging.c: Benchmark for scheduler and IPC mechanisms based on hackbench
This patch adds bench/sched-messaging.c.
This benchmark measures performance of scheduler and IPC
mechanisms, and is based on hackbench by Rusty Russell.
Example of usage:
% perf bench sched messaging -g 20 -l 1000 -s
5.432 # in sec
% perf bench sched messaging # run with default
options (20 sender and receiver processes per group)
(10 groups == 400 processes run)
Total time:0.308 sec
% perf bench sched messaging -t -g 20 # # be multi-thread,
with 20 groups (20 sender and receiver threads per group)
(20 groups == 800 threads run)
Total time:0.582 sec
( Rusty is the original author of hackbench.c and he said the code is
and was under the GPLv2 so fine to be merged. )
Signed-off-by: Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
Acked-by: Rusty Russell <rusty@rustcorp.com.au>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: fweisbec@gmail.com
Cc: Jiri Kosina <jkosina@suse.cz>
LKML-Reference: <1257381097-4743-3-git-send-email-mitake@dcl.info.waseda.ac.jp>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
| -rw-r--r-- | tools/perf/bench/sched-messaging.c | 332 |
1 files changed, 332 insertions, 0 deletions
diff --git a/tools/perf/bench/sched-messaging.c b/tools/perf/bench/sched-messaging.c new file mode 100644 index 000000000000..36b62c549e28 --- /dev/null +++ b/tools/perf/bench/sched-messaging.c | |||
| @@ -0,0 +1,332 @@ | |||
| 1 | /* | ||
| 2 | * | ||
| 3 | * builtin-bench-messaging.c | ||
| 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" | ||
| 14 | #include "../util/parse-options.h" | ||
| 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> | ||
| 29 | #include <sys/poll.h> | ||
| 30 | #include <limits.h> | ||
| 31 | |||
| 32 | #define DATASIZE 100 | ||
| 33 | |||
| 34 | static int use_pipes = 0; | ||
| 35 | static unsigned int loops = 100; | ||
| 36 | static unsigned int thread_mode = 0; | ||
| 37 | static unsigned int num_groups = 10; | ||
| 38 | static int simple = 0; | ||
| 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 | |||
| 54 | static void barf(const char *msg) | ||
| 55 | { | ||
| 56 | fprintf(stderr, "%s (error: %s)\n", msg, strerror(errno)); | ||
| 57 | exit(1); | ||
| 58 | } | ||
| 59 | |||
| 60 | static void fdpair(int fds[2]) | ||
| 61 | { | ||
| 62 | if (use_pipes) { | ||
| 63 | if (pipe(fds) == 0) | ||
| 64 | return; | ||
| 65 | } else { | ||
| 66 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0) | ||
| 67 | return; | ||
| 68 | } | ||
| 69 | |||
| 70 | barf(use_pipes ? "pipe()" : "socketpair()"); | ||
| 71 | } | ||
| 72 | |||
| 73 | /* Block until we're ready to go */ | ||
| 74 | static void ready(int ready_out, int wakefd) | ||
| 75 | { | ||
| 76 | char dummy; | ||
| 77 | struct pollfd pollfd = { .fd = wakefd, .events = POLLIN }; | ||
| 78 | |||
| 79 | /* Tell them we're ready. */ | ||
| 80 | if (write(ready_out, &dummy, 1) != 1) | ||
| 81 | barf("CLIENT: ready write"); | ||
| 82 | |||
| 83 | /* Wait for "GO" signal */ | ||
| 84 | if (poll(&pollfd, 1, -1) != 1) | ||
| 85 | barf("poll"); | ||
| 86 | } | ||
| 87 | |||
| 88 | /* Sender sprays loops messages down each file descriptor */ | ||
| 89 | static void *sender(struct sender_context *ctx) | ||
| 90 | { | ||
| 91 | char data[DATASIZE]; | ||
| 92 | unsigned int i, j; | ||
| 93 | |||
| 94 | ready(ctx->ready_out, ctx->wakefd); | ||
| 95 | |||
| 96 | /* Now pump to every receiver. */ | ||
| 97 | for (i = 0; i < loops; i++) { | ||
| 98 | for (j = 0; j < ctx->num_fds; j++) { | ||
| 99 | int ret, done = 0; | ||
| 100 | |||
| 101 | again: | ||
| 102 | ret = write(ctx->out_fds[j], data + done, | ||
| 103 | sizeof(data)-done); | ||
| 104 | if (ret < 0) | ||
| 105 | barf("SENDER: write"); | ||
| 106 | done += ret; | ||
| 107 | if (done < DATASIZE) | ||
| 108 | goto again; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | return NULL; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | /* One receiver per fd */ | ||
| 117 | static void *receiver(struct receiver_context* ctx) | ||
| 118 | { | ||
| 119 | unsigned int i; | ||
| 120 | |||
| 121 | if (!thread_mode) | ||
| 122 | close(ctx->in_fds[1]); | ||
| 123 | |||
| 124 | /* Wait for start... */ | ||
| 125 | ready(ctx->ready_out, ctx->wakefd); | ||
| 126 | |||
| 127 | /* Receive them all */ | ||
| 128 | for (i = 0; i < ctx->num_packets; i++) { | ||
| 129 | char data[DATASIZE]; | ||
| 130 | int ret, done = 0; | ||
| 131 | |||
| 132 | again: | ||
| 133 | ret = read(ctx->in_fds[0], data + done, DATASIZE - done); | ||
| 134 | if (ret < 0) | ||
| 135 | barf("SERVER: read"); | ||
| 136 | done += ret; | ||
| 137 | if (done < DATASIZE) | ||
| 138 | goto again; | ||
| 139 | } | ||
| 140 | |||
| 141 | return NULL; | ||
| 142 | } | ||
| 143 | |||
| 144 | static pthread_t create_worker(void *ctx, void *(*func)(void *)) | ||
| 145 | { | ||
| 146 | pthread_attr_t attr; | ||
| 147 | pthread_t childid; | ||
| 148 | int err; | ||
| 149 | |||
| 150 | if (!thread_mode) { | ||
| 151 | /* process mode */ | ||
| 152 | /* Fork the receiver. */ | ||
| 153 | switch (fork()) { | ||
| 154 | case -1: | ||
| 155 | barf("fork()"); | ||
| 156 | break; | ||
| 157 | case 0: | ||
| 158 | (*func) (ctx); | ||
| 159 | exit(0); | ||
| 160 | break; | ||
| 161 | default: | ||
| 162 | break; | ||
| 163 | } | ||
| 164 | |||
| 165 | return (pthread_t)0; | ||
| 166 | } | ||
| 167 | |||
| 168 | if (pthread_attr_init(&attr) != 0) | ||
| 169 | barf("pthread_attr_init:"); | ||
| 170 | |||
| 171 | #ifndef __ia64__ | ||
| 172 | if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0) | ||
| 173 | barf("pthread_attr_setstacksize"); | ||
| 174 | #endif | ||
| 175 | |||
| 176 | err = pthread_create(&childid, &attr, func, ctx); | ||
| 177 | if (err != 0) { | ||
| 178 | fprintf(stderr, "pthread_create failed: %s (%d)\n", | ||
| 179 | strerror(err), err); | ||
| 180 | exit(-1); | ||
| 181 | } | ||
| 182 | return childid; | ||
| 183 | } | ||
| 184 | |||
| 185 | static void reap_worker(pthread_t id) | ||
| 186 | { | ||
| 187 | int proc_status; | ||
| 188 | void *thread_status; | ||
| 189 | |||
| 190 | if (!thread_mode) { | ||
| 191 | /* process mode */ | ||
| 192 | wait(&proc_status); | ||
| 193 | if (!WIFEXITED(proc_status)) | ||
| 194 | exit(1); | ||
| 195 | } else { | ||
| 196 | pthread_join(id, &thread_status); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | /* One group of senders and receivers */ | ||
| 201 | static unsigned int group(pthread_t *pth, | ||
| 202 | unsigned int num_fds, | ||
| 203 | int ready_out, | ||
| 204 | int wakefd) | ||
| 205 | { | ||
| 206 | unsigned int i; | ||
| 207 | struct sender_context *snd_ctx = malloc(sizeof(struct sender_context) | ||
| 208 | + num_fds * sizeof(int)); | ||
| 209 | |||
| 210 | if (!snd_ctx) | ||
| 211 | barf("malloc()"); | ||
| 212 | |||
| 213 | for (i = 0; i < num_fds; i++) { | ||
| 214 | int fds[2]; | ||
| 215 | struct receiver_context *ctx = malloc(sizeof(*ctx)); | ||
| 216 | |||
| 217 | if (!ctx) | ||
| 218 | barf("malloc()"); | ||
| 219 | |||
| 220 | |||
| 221 | /* Create the pipe between client and server */ | ||
| 222 | fdpair(fds); | ||
| 223 | |||
| 224 | ctx->num_packets = num_fds * loops; | ||
| 225 | ctx->in_fds[0] = fds[0]; | ||
| 226 | ctx->in_fds[1] = fds[1]; | ||
| 227 | ctx->ready_out = ready_out; | ||
| 228 | ctx->wakefd = wakefd; | ||
| 229 | |||
| 230 | pth[i] = create_worker(ctx, (void *)receiver); | ||
| 231 | |||
| 232 | snd_ctx->out_fds[i] = fds[1]; | ||
| 233 | if (!thread_mode) | ||
| 234 | close(fds[0]); | ||
| 235 | } | ||
| 236 | |||
| 237 | /* Now we have all the fds, fork the senders */ | ||
| 238 | for (i = 0; i < num_fds; i++) { | ||
| 239 | snd_ctx->ready_out = ready_out; | ||
| 240 | snd_ctx->wakefd = wakefd; | ||
| 241 | snd_ctx->num_fds = num_fds; | ||
| 242 | |||
| 243 | pth[num_fds+i] = create_worker(snd_ctx, (void *)sender); | ||
| 244 | } | ||
| 245 | |||
| 246 | /* Close the fds we have left */ | ||
| 247 | if (!thread_mode) | ||
| 248 | for (i = 0; i < num_fds; i++) | ||
| 249 | close(snd_ctx->out_fds[i]); | ||
| 250 | |||
| 251 | /* Return number of children to reap */ | ||
| 252 | return num_fds * 2; | ||
| 253 | } | ||
| 254 | |||
| 255 | static const struct option options[] = { | ||
| 256 | OPT_BOOLEAN('p', "pipe", &use_pipes, | ||
| 257 | "Use pipe() instead of socketpair()"), | ||
| 258 | OPT_BOOLEAN('t', "thread", &thread_mode, | ||
| 259 | "Be multi thread instead of multi process"), | ||
| 260 | OPT_INTEGER('g', "group", &num_groups, | ||
| 261 | "Specify number of groups"), | ||
| 262 | OPT_INTEGER('l', "loop", &loops, | ||
| 263 | "Specify number of loops"), | ||
| 264 | OPT_BOOLEAN('s', "simple-output", &simple, | ||
| 265 | "Do simple output (this maybe useful for" | ||
| 266 | "processing by scripts or graph tools like gnuplot)"), | ||
| 267 | OPT_END() | ||
| 268 | }; | ||
| 269 | |||
| 270 | static const char * const bench_sched_message_usage[] = { | ||
| 271 | "perf bench sched messaging <options>", | ||
| 272 | NULL | ||
| 273 | }; | ||
| 274 | |||
| 275 | int bench_sched_messaging(int argc, const char **argv, | ||
| 276 | const char *prefix __used) | ||
| 277 | { | ||
| 278 | unsigned int i, total_children; | ||
| 279 | struct timeval start, stop, diff; | ||
| 280 | unsigned int num_fds = 20; | ||
| 281 | int readyfds[2], wakefds[2]; | ||
| 282 | char dummy; | ||
| 283 | pthread_t *pth_tab; | ||
| 284 | |||
| 285 | argc = parse_options(argc, argv, options, | ||
| 286 | bench_sched_message_usage, 0); | ||
| 287 | |||
| 288 | pth_tab = malloc(num_fds * 2 * num_groups * sizeof(pthread_t)); | ||
| 289 | if (!pth_tab) | ||
| 290 | barf("main:malloc()"); | ||
| 291 | |||
| 292 | fdpair(readyfds); | ||
| 293 | fdpair(wakefds); | ||
| 294 | |||
| 295 | total_children = 0; | ||
| 296 | for (i = 0; i < num_groups; i++) | ||
| 297 | total_children += group(pth_tab+total_children, num_fds, | ||
| 298 | readyfds[1], wakefds[0]); | ||
| 299 | |||
| 300 | /* Wait for everyone to be ready */ | ||
| 301 | for (i = 0; i < total_children; i++) | ||
| 302 | if (read(readyfds[0], &dummy, 1) != 1) | ||
| 303 | barf("Reading for readyfds"); | ||
| 304 | |||
| 305 | gettimeofday(&start, NULL); | ||
| 306 | |||
| 307 | /* Kick them off */ | ||
| 308 | if (write(wakefds[1], &dummy, 1) != 1) | ||
| 309 | barf("Writing to start them"); | ||
| 310 | |||
| 311 | /* Reap them all */ | ||
| 312 | for (i = 0; i < total_children; i++) | ||
| 313 | reap_worker(pth_tab[i]); | ||
| 314 | |||
| 315 | gettimeofday(&stop, NULL); | ||
| 316 | |||
| 317 | timersub(&stop, &start, &diff); | ||
| 318 | |||
| 319 | if (simple) | ||
| 320 | printf("%lu.%03lu\n", diff.tv_sec, diff.tv_usec/1000); | ||
| 321 | else { | ||
| 322 | printf("(%d sender and receiver %s per group)\n", | ||
| 323 | num_fds, thread_mode ? "threads" : "processes"); | ||
| 324 | printf("(%d groups == %d %s run)\n\n", | ||
| 325 | num_groups, num_groups * 2 * num_fds, | ||
| 326 | thread_mode ? "threads" : "processes"); | ||
| 327 | printf("\tTotal time:%lu.%03lu sec\n", | ||
| 328 | diff.tv_sec, diff.tv_usec/1000); | ||
| 329 | } | ||
| 330 | |||
| 331 | return 0; | ||
| 332 | } | ||
