| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * This is the latest version of hackbench.c, that tests scheduler and |
| 4 | * unix-socket (or pipe) performance. |
| 5 | * |
| 6 | * Usage: hackbench [-pipe] <num groups> [process|thread] [loops] |
| 7 | * |
| 8 | * Build it with: |
| 9 | * gcc -g -Wall -O2 -o hackbench hackbench.c -lpthread |
| 10 | * |
| 11 | * Downloaded from http://people.redhat.com/mingo/cfs-scheduler/tools/hackbench.c |
| 12 | * February 19 2010. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | /* Test groups of 20 processes spraying to 20 receivers */ |
| 17 | #include <pthread.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <errno.h> |
| 22 | #include <unistd.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/wait.h> |
| 27 | #include <sys/time.h> |
| 28 | #include <sys/poll.h> |
| 29 | #include <netinet/in.h> |
| 30 | #include <arpa/inet.h> |
| 31 | #include <limits.h> |
| 32 | #include <getopt.h> |
| 33 | #include <signal.h> |
| 34 | #include <setjmp.h> |
| 35 | #include <sched.h> |
| 36 | |
| 37 | static unsigned int datasize = 100; |
| 38 | static unsigned int loops = 100; |
| 39 | static unsigned int num_groups = 10; |
| 40 | static unsigned int num_fds = 20; |
| 41 | static unsigned int fifo = 0; |
| 42 | |
| 43 | /* |
| 44 | * 0 means thread mode and others mean process (default) |
| 45 | */ |
| 46 | #define THREAD_MODE 0 |
| 47 | #define PROCESS_MODE 1 |
| 48 | |
| 49 | static unsigned int process_mode = PROCESS_MODE; |
| 50 | |
| 51 | static int use_pipes = 0; |
| 52 | static int use_inet = 0; |
| 53 | |
| 54 | struct sender_context { |
| 55 | unsigned int num_fds; |
| 56 | int ready_out; |
| 57 | int wakefd; |
| 58 | int out_fds[0]; |
| 59 | }; |
| 60 | |
| 61 | struct receiver_context { |
| 62 | unsigned int num_packets; |
| 63 | int in_fds[2]; |
| 64 | int ready_out; |
| 65 | int wakefd; |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | typedef union { |
| 70 | pthread_t threadid; |
| 71 | pid_t pid; |
| 72 | } childinfo_t; |
| 73 | |
| 74 | childinfo_t *child_tab = NULL; |
| 75 | unsigned int total_children = 0; |
| 76 | unsigned int signal_caught = 0; |
| 77 | |
| 78 | static jmp_buf jmpbuf; |
| 79 | |
| 80 | inline static void sneeze(const char *msg) { |
| 81 | /* Avoid calling these functions when called from a code path |
| 82 | * which involves sigcatcher(), as they are not reentrant safe. |
| 83 | */ |
| 84 | if( !signal_caught ) { |
| 85 | fprintf(stderr, "%s (error: %s)\n", msg, strerror(errno)); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static void barf(const char *msg) |
| 90 | { |
| 91 | sneeze(msg); |
| 92 | exit(1); |
| 93 | } |
| 94 | |
| 95 | static void print_usage_exit(int error) |
| 96 | { |
| 97 | printf("hackbench V %1.2f\n", VERSION); |
| 98 | printf("Usage:\n" |
| 99 | "hackbench <options>\n\n" |
| 100 | "-f --fds=NUM number of fds\n" |
| 101 | "-F --fifo use SCHED_FIFO for main thread\n" |
| 102 | "-g --groups=NUM number of groups to be used\n" |
| 103 | "-h --help print this message\n" |
| 104 | "-l --loops=LOOPS how many message should be send\n" |
| 105 | "-p --pipe send data via a pipe\n" |
| 106 | "-i --inet send data via a inet tcp connection\n" |
| 107 | "-s --datasize=SIZE message size\n" |
| 108 | "-T --threads use POSIX threads\n" |
| 109 | "-P --process use fork (default)\n" |
| 110 | ); |
| 111 | exit(error); |
| 112 | } |
| 113 | |
| 114 | static int inet_socketpair(int fds[2]) |
| 115 | { |
| 116 | int s1, s2; |
| 117 | struct sockaddr_in sin; |
| 118 | unsigned long ul = 1; |
| 119 | |
| 120 | if ((s1 = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
| 121 | barf("socket"); |
| 122 | if ((s2 = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
| 123 | barf("socket"); |
| 124 | |
| 125 | socklen_t len = sizeof(sin); |
| 126 | bzero(&sin, len); |
| 127 | sin.sin_family = AF_INET; |
| 128 | sin.sin_port = 0; |
| 129 | sin.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 130 | |
| 131 | if (bind(s1, &sin, len) < 0) |
| 132 | barf("bind"); |
| 133 | if (getsockname(s1, &sin, &len) < 0) |
| 134 | barf("getsockname"); |
| 135 | if (listen(s1, 10) < 0) |
| 136 | barf("listen"); |
| 137 | if (ioctl(s2, FIONBIO, &ul) < 0) |
| 138 | barf("ioctl"); |
| 139 | if (ioctl(s1, FIONBIO, &ul) < 0) |
| 140 | barf("ioctl"); |
| 141 | if (connect(s2, &sin, len) < 0) |
| 142 | barf("connect"); |
| 143 | if ((fds[0] = accept(s1, &sin, &len)) < 0) |
| 144 | barf("accept"); |
| 145 | ul = 0; |
| 146 | if (ioctl(s2, FIONBIO, &ul) < 0) |
| 147 | barf("ioctl"); |
| 148 | fds[1] = s2; |
| 149 | close(s1); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static void fdpair(int fds[2]) |
| 155 | { |
| 156 | if (use_pipes) { |
| 157 | if (pipe(fds) == 0) |
| 158 | return; |
| 159 | } else if (use_inet) { |
| 160 | if (inet_socketpair(fds) == 0) |
| 161 | return; |
| 162 | } else { |
| 163 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0) |
| 164 | return; |
| 165 | } |
| 166 | barf("Creating fdpair"); |
| 167 | } |
| 168 | |
| 169 | /* Block until we're ready to go */ |
| 170 | static void ready(int ready_out, int wakefd) |
| 171 | { |
| 172 | char dummy = '*'; |
| 173 | struct pollfd pollfd = { .fd = wakefd, .events = POLLIN }; |
| 174 | |
| 175 | /* Tell them we're ready. */ |
| 176 | if (write(ready_out, &dummy, 1) != 1) |
| 177 | barf("CLIENT: ready write"); |
| 178 | |
| 179 | /* Wait for "GO" signal */ |
| 180 | if (poll(&pollfd, 1, -1) != 1) |
| 181 | barf("poll"); |
| 182 | } |
| 183 | |
| 184 | static void reset_worker_signals(void) |
| 185 | { |
| 186 | signal(SIGTERM, SIG_DFL); |
| 187 | signal(SIGINT, SIG_DFL); |
| 188 | } |
| 189 | |
| 190 | /* Sender sprays loops messages down each file descriptor */ |
| 191 | static void *sender(struct sender_context *ctx) |
| 192 | { |
| 193 | char data[datasize]; |
| 194 | unsigned int i, j; |
| 195 | |
| 196 | reset_worker_signals(); |
| 197 | ready(ctx->ready_out, ctx->wakefd); |
| 198 | memset(&data, '-', datasize); |
| 199 | |
| 200 | /* Now pump to every receiver. */ |
| 201 | for (i = 0; i < loops; i++) { |
| 202 | for (j = 0; j < ctx->num_fds; j++) { |
| 203 | int ret; |
| 204 | size_t done = 0; |
| 205 | |
| 206 | again: |
| 207 | ret = write(ctx->out_fds[j], data + done, sizeof(data)-done); |
| 208 | if (ret < 0) |
| 209 | barf("SENDER: write"); |
| 210 | done += ret; |
| 211 | if (done < sizeof(data)) |
| 212 | goto again; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return NULL; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /* One receiver per fd */ |
| 221 | static void *receiver(struct receiver_context* ctx) |
| 222 | { |
| 223 | unsigned int i; |
| 224 | |
| 225 | reset_worker_signals(); |
| 226 | if (process_mode == PROCESS_MODE) |
| 227 | close(ctx->in_fds[1]); |
| 228 | |
| 229 | /* Wait for start... */ |
| 230 | ready(ctx->ready_out, ctx->wakefd); |
| 231 | |
| 232 | /* Receive them all */ |
| 233 | for (i = 0; i < ctx->num_packets; i++) { |
| 234 | char data[datasize]; |
| 235 | int ret; |
| 236 | size_t done = 0; |
| 237 | |
| 238 | again: |
| 239 | ret = read(ctx->in_fds[0], data + done, datasize - done); |
| 240 | if (ret < 0) |
| 241 | barf("SERVER: read"); |
| 242 | done += ret; |
| 243 | if (done < datasize) |
| 244 | goto again; |
| 245 | } |
| 246 | if (ctx) { |
| 247 | free(ctx); |
| 248 | } |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | static int create_worker(childinfo_t *child, void *ctx, void *(*func)(void *)) |
| 253 | { |
| 254 | pthread_attr_t attr; |
| 255 | int err; |
| 256 | |
| 257 | switch (process_mode) { |
| 258 | case PROCESS_MODE: /* process mode */ |
| 259 | /* Fork the sender/receiver child. */ |
| 260 | switch ((child->pid = fork())) { |
| 261 | case -1: |
| 262 | sneeze("fork()"); |
| 263 | return -1; |
| 264 | case 0: |
| 265 | (*func) (ctx); |
| 266 | exit(0); |
| 267 | } |
| 268 | break; |
| 269 | |
| 270 | case THREAD_MODE: /* threaded mode */ |
| 271 | if (pthread_attr_init(&attr) != 0) { |
| 272 | sneeze("pthread_attr_init()"); |
| 273 | return -1; |
| 274 | } |
| 275 | |
| 276 | #ifndef __ia64__ |
| 277 | if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0) { |
| 278 | sneeze("pthread_attr_setstacksize()"); |
| 279 | return -1; |
| 280 | } |
| 281 | #endif |
| 282 | |
| 283 | if ((err=pthread_create(&child->threadid, &attr, func, ctx)) != 0) { |
| 284 | sneeze("pthread_create failed()"); |
| 285 | return -1; |
| 286 | } |
| 287 | break; |
| 288 | } |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | void signal_workers(childinfo_t *children, unsigned int num_children) |
| 293 | { |
| 294 | unsigned int i; |
| 295 | printf("signaling %d worker threads to terminate\n", num_children); |
| 296 | for (i=0; i < num_children; i++) { |
| 297 | kill(children[i].pid, SIGTERM); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | unsigned int reap_workers(childinfo_t *child, unsigned int totchld, unsigned int dokill) |
| 302 | { |
| 303 | unsigned int i, rc = 0; |
| 304 | int status, err; |
| 305 | void *thr_status; |
| 306 | |
| 307 | if (dokill) { |
| 308 | fprintf(stderr, "sending SIGTERM to all child processes\n"); |
| 309 | signal(SIGTERM, SIG_IGN); |
| 310 | signal_workers(child, totchld); |
| 311 | } |
| 312 | |
| 313 | for( i = 0; i < totchld; i++ ) { |
| 314 | int pid; |
| 315 | switch( process_mode ) { |
| 316 | case PROCESS_MODE: /* process mode */ |
| 317 | fflush(stdout); |
| 318 | pid = wait(&status); |
| 319 | if (pid == -1 && errno == ECHILD) |
| 320 | break; |
| 321 | if (!WIFEXITED(status)) |
| 322 | rc++; |
| 323 | break; |
| 324 | case THREAD_MODE: /* threaded mode */ |
| 325 | err = pthread_join(child[i].threadid, &thr_status); |
| 326 | if( err != 0 ) { |
| 327 | sneeze("pthread_join()"); |
| 328 | rc++; |
| 329 | } |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | return rc; |
| 334 | } |
| 335 | |
| 336 | /* One group of senders and receivers */ |
| 337 | static unsigned int group(childinfo_t *child, |
| 338 | unsigned int tab_offset, |
| 339 | unsigned int num_fds, |
| 340 | int ready_out, |
| 341 | int wakefd) |
| 342 | { |
| 343 | unsigned int i; |
| 344 | struct sender_context* snd_ctx = malloc (sizeof(struct sender_context) |
| 345 | +num_fds*sizeof(int)); |
| 346 | int err; |
| 347 | |
| 348 | if (!snd_ctx) { |
| 349 | sneeze("malloc() [sender ctx]"); |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | for (i = 0; i < num_fds; i++) { |
| 355 | int fds[2]; |
| 356 | struct receiver_context* ctx = malloc (sizeof(*ctx)); |
| 357 | |
| 358 | if (!ctx) { |
| 359 | sneeze("malloc() [receiver ctx]"); |
| 360 | return (i > 0 ? i-1 : 0); |
| 361 | } |
| 362 | |
| 363 | |
| 364 | /* Create the pipe between client and server */ |
| 365 | fdpair(fds); |
| 366 | |
| 367 | ctx->num_packets = num_fds*loops; |
| 368 | ctx->in_fds[0] = fds[0]; |
| 369 | ctx->in_fds[1] = fds[1]; |
| 370 | ctx->ready_out = ready_out; |
| 371 | ctx->wakefd = wakefd; |
| 372 | |
| 373 | err = create_worker(&child[tab_offset+i], ctx, |
| 374 | (void *)(void *)receiver); |
| 375 | if(err) { |
| 376 | return (i > 0 ? i-1 : 0); |
| 377 | } |
| 378 | snd_ctx->out_fds[i] = fds[1]; |
| 379 | if (process_mode == PROCESS_MODE) |
| 380 | close(fds[0]); |
| 381 | } |
| 382 | |
| 383 | snd_ctx->ready_out = ready_out; |
| 384 | snd_ctx->wakefd = wakefd; |
| 385 | snd_ctx->num_fds = num_fds; |
| 386 | |
| 387 | /* Now we have all the fds, fork the senders */ |
| 388 | for (i = 0; i < num_fds; i++) { |
| 389 | err = create_worker(&child[tab_offset+num_fds+i], snd_ctx, |
| 390 | (void *)(void *)sender); |
| 391 | if(err) { |
| 392 | return (num_fds+i)-1; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /* Close the fds we have left */ |
| 397 | if (process_mode == PROCESS_MODE) |
| 398 | for (i = 0; i < num_fds; i++) |
| 399 | close(snd_ctx->out_fds[i]); |
| 400 | |
| 401 | /* Return number of children to reap */ |
| 402 | return num_fds * 2; |
| 403 | } |
| 404 | |
| 405 | static void process_options(int argc, char *argv[]) |
| 406 | { |
| 407 | for(;;) { |
| 408 | static struct option longopts[] = { |
| 409 | {"fds", required_argument, NULL, 'f'}, |
| 410 | {"fifo", no_argument, NULL, 'F'}, |
| 411 | {"groups", required_argument, NULL, 'g'}, |
| 412 | {"help", no_argument, NULL, 'h'}, |
| 413 | {"loops", required_argument, NULL, 'l'}, |
| 414 | {"pipe", no_argument, NULL, 'p'}, |
| 415 | {"inet", no_argument, NULL, 'i'}, |
| 416 | {"datasize", required_argument, NULL, 's'}, |
| 417 | {"threads", no_argument, NULL, 'T'}, |
| 418 | {"processes", no_argument, NULL, 'P'}, |
| 419 | {NULL, 0, NULL, 0} |
| 420 | }; |
| 421 | |
| 422 | int c = getopt_long(argc, argv, "f:Fg:hl:pis:TP", |
| 423 | longopts, NULL); |
| 424 | if (c == -1) { |
| 425 | break; |
| 426 | } |
| 427 | switch (c) { |
| 428 | case 'f': |
| 429 | num_fds = atoi(optarg); |
| 430 | if (atoi(optarg) <= 0) { |
| 431 | fprintf(stderr, "%s: --fds|-f requires an integer > 0\n", argv[0]); |
| 432 | print_usage_exit(1); |
| 433 | } |
| 434 | break; |
| 435 | case 'F': |
| 436 | fifo = 1; |
| 437 | break; |
| 438 | case 'g': |
| 439 | num_groups = atoi(optarg); |
| 440 | if (atoi(optarg) <= 0) { |
| 441 | fprintf(stderr, "%s: --groups|-g requires an integer > 0\n", argv[0]); |
| 442 | print_usage_exit(1); |
| 443 | } |
| 444 | break; |
| 445 | case 'h': |
| 446 | print_usage_exit(0); |
| 447 | break; |
| 448 | case 'l': |
| 449 | loops = atoi(optarg); |
| 450 | if (atoi(optarg) <= 0) { |
| 451 | fprintf(stderr, "%s: --loops|-l requires an integer > 0\n", argv[0]); |
| 452 | print_usage_exit(1); |
| 453 | } |
| 454 | break; |
| 455 | case 'p': |
| 456 | use_pipes = 1; |
| 457 | break; |
| 458 | case 'i': |
| 459 | use_inet = 1; |
| 460 | break; |
| 461 | case 's': |
| 462 | datasize = atoi(optarg); |
| 463 | if (atoi(optarg) <= 0) { |
| 464 | fprintf(stderr, "%s: --datasize|-s requires an integer > 0\n", argv[0]); |
| 465 | print_usage_exit(1); |
| 466 | } |
| 467 | break; |
| 468 | case 'T': |
| 469 | process_mode = THREAD_MODE; |
| 470 | break; |
| 471 | case 'P': |
| 472 | process_mode = PROCESS_MODE; |
| 473 | break; |
| 474 | default: |
| 475 | print_usage_exit(1); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | if (use_pipes && use_inet) { |
| 480 | fprintf(stderr, "%s: --pipe|-p and --inet|-i cannot be used together\n", argv[0]); |
| 481 | print_usage_exit(1); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | void sigcatcher(int sig) { |
| 486 | /* All caught signals will cause the program to exit */ |
| 487 | signal_caught = 1; |
| 488 | fprintf(stderr, "Signal %d caught, longjmp'ing out!\n", sig); |
| 489 | signal(sig, SIG_IGN); |
| 490 | longjmp(jmpbuf, 1); |
| 491 | } |
| 492 | |
| 493 | int main(int argc, char *argv[]) |
| 494 | { |
| 495 | unsigned int i; |
| 496 | struct timeval start, stop, diff; |
| 497 | int readyfds[2], wakefds[2]; |
| 498 | char dummy; |
| 499 | volatile int timer_started = 0; |
| 500 | struct sched_param sp; |
| 501 | |
| 502 | process_options (argc, argv); |
| 503 | |
| 504 | printf("Running in %s mode with %d groups using %d file descriptors each (== %d tasks)\n", |
| 505 | (process_mode == THREAD_MODE ? "threaded" : "process"), |
| 506 | num_groups, 2*num_fds, num_groups*(num_fds*2)); |
| 507 | printf("Each sender will pass %d messages of %d bytes\n", loops, datasize); |
| 508 | fflush(NULL); |
| 509 | |
| 510 | child_tab = calloc(num_fds * 2 * num_groups, sizeof(childinfo_t)); |
| 511 | if (!child_tab) |
| 512 | barf("main:malloc()"); |
| 513 | |
| 514 | fdpair(readyfds); |
| 515 | fdpair(wakefds); |
| 516 | |
| 517 | /* Catch some signals */ |
| 518 | signal(SIGINT, sigcatcher); |
| 519 | signal(SIGTERM, sigcatcher); |
| 520 | signal(SIGHUP, SIG_IGN); |
| 521 | |
| 522 | if (setjmp(jmpbuf) == 0) { |
| 523 | total_children = 0; |
| 524 | for (i = 0; i < num_groups; i++) { |
| 525 | unsigned int c = group(child_tab, total_children, num_fds, readyfds[1], wakefds[0]); |
| 526 | if( c != (num_fds*2) ) { |
| 527 | fprintf(stderr, "%i children started. Expected %i\n", c, num_fds*2); |
| 528 | reap_workers(child_tab, total_children + c, 1); |
| 529 | barf("Creating workers"); |
| 530 | } |
| 531 | total_children += c; |
| 532 | } |
| 533 | if (fifo) { |
| 534 | /* make main a realtime task so that we can manage the workers */ |
| 535 | sp.sched_priority = 1; |
| 536 | if (sched_setscheduler(0, SCHED_FIFO, &sp) < 0) |
| 537 | barf("can't change to fifo in main"); |
| 538 | } |
| 539 | |
| 540 | /* Wait for everyone to be ready */ |
| 541 | for (i = 0; i < total_children; i++) |
| 542 | if (read(readyfds[0], &dummy, 1) != 1) { |
| 543 | reap_workers(child_tab, total_children, 1); |
| 544 | barf("Reading for readyfds"); |
| 545 | } |
| 546 | |
| 547 | gettimeofday(&start, NULL); |
| 548 | timer_started = 1; |
| 549 | |
| 550 | /* Kick them off */ |
| 551 | if (write(wakefds[1], &dummy, 1) != 1) { |
| 552 | reap_workers(child_tab, total_children, 1); |
| 553 | barf("Writing to start senders"); |
| 554 | } |
| 555 | } |
| 556 | else { |
| 557 | fprintf(stderr, "longjmp'ed out, reaping children\n"); |
| 558 | signal(SIGINT, SIG_IGN); |
| 559 | signal(SIGTERM, SIG_IGN); |
| 560 | } |
| 561 | |
| 562 | /* Reap them all */ |
| 563 | reap_workers(child_tab, total_children, signal_caught); |
| 564 | |
| 565 | gettimeofday(&stop, NULL); |
| 566 | |
| 567 | /* Print time... */ |
| 568 | if (timer_started) { |
| 569 | timersub(&stop, &start, &diff); |
| 570 | printf("Time: %lu.%03lu\n", diff.tv_sec, diff.tv_usec/1000); |
| 571 | } |
| 572 | else |
| 573 | fprintf(stderr, "No measurements available\n"); |
| 574 | free(child_tab); |
| 575 | exit(0); |
| 576 | } |