| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | |
| 3 | /* |
| 4 | * svsematest.c |
| 5 | * |
| 6 | * Copyright (C) 2009 Carsten Emde <[email protected]> |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <fcntl.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <unistd.h> |
| 14 | #include <limits.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <getopt.h> |
| 17 | #include <pthread.h> |
| 18 | #include <signal.h> |
| 19 | #include <sched.h> |
| 20 | #include <string.h> |
| 21 | #include <time.h> |
| 22 | #include <utmpx.h> |
| 23 | #include <inttypes.h> |
| 24 | |
| 25 | #include <linux/unistd.h> |
| 26 | |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/sem.h> |
| 30 | #include <sys/time.h> |
| 31 | #include <sys/mman.h> |
| 32 | |
| 33 | #include "rt-utils.h" |
| 34 | #include "rt-get_cpu.h" |
| 35 | #include "rt-error.h" |
| 36 | |
| 37 | #define SEM_WAIT_FOR_RECEIVER 0 |
| 38 | #define SEM_WAIT_FOR_SENDER 1 |
| 39 | |
| 40 | #define SEM_LOCK -1 |
| 41 | #define SEM_UNLOCK 1 |
| 42 | |
| 43 | enum { |
| 44 | AFFINITY_UNSPECIFIED, |
| 45 | AFFINITY_SPECIFIED, |
| 46 | AFFINITY_USEALL |
| 47 | }; |
| 48 | |
| 49 | struct params { |
| 50 | int num; |
| 51 | int num_threads; |
| 52 | int cpu; |
| 53 | int priority; |
| 54 | int affinity; |
| 55 | int semid; |
| 56 | int sender; |
| 57 | int samples; |
| 58 | int max_cycles; |
| 59 | int tracelimit; |
| 60 | int tid; |
| 61 | pid_t pid; |
| 62 | int shutdown; |
| 63 | int stopped; |
| 64 | struct timespec delay; |
| 65 | unsigned int mindiff, maxdiff; |
| 66 | double sumdiff; |
| 67 | struct timeval unblocked, received, diff; |
| 68 | pthread_t threadid; |
| 69 | struct params *neighbor; |
| 70 | }; |
| 71 | |
| 72 | static int mustfork; |
| 73 | static int wasforked; |
| 74 | static int wasforked_sender = -1; |
| 75 | static int wasforked_threadno = -1; |
| 76 | static int tracelimit; |
| 77 | |
| 78 | void *semathread(void *param) |
| 79 | { |
| 80 | int mustgetcpu = 0; |
| 81 | struct params *par = param; |
| 82 | cpu_set_t mask; |
| 83 | int policy = SCHED_FIFO; |
| 84 | struct sched_param schedp; |
| 85 | struct sembuf sb = { 0, 0, 0}; |
| 86 | sigset_t sigset; |
| 87 | int ret; |
| 88 | |
| 89 | sigemptyset(&sigset); |
| 90 | pthread_sigmask(SIG_SETMASK, &sigset, NULL); |
| 91 | |
| 92 | memset(&schedp, 0, sizeof(schedp)); |
| 93 | schedp.sched_priority = par->priority; |
| 94 | sched_setscheduler(0, policy, &schedp); |
| 95 | |
| 96 | if (par->cpu != -1) { |
| 97 | CPU_ZERO(&mask); |
| 98 | CPU_SET(par->cpu, &mask); |
| 99 | if (sched_setaffinity(0, sizeof(mask), &mask) == -1) |
| 100 | fatal("Could not set CPU affinity " |
| 101 | "to CPU #%d\n", par->cpu); |
| 102 | } else { |
| 103 | int max_cpus = sysconf(_SC_NPROCESSORS_CONF); |
| 104 | |
| 105 | if (max_cpus > 1) |
| 106 | mustgetcpu = 1; |
| 107 | else |
| 108 | par->cpu = 0; |
| 109 | } |
| 110 | |
| 111 | if (!wasforked) |
| 112 | par->tid = gettid(); |
| 113 | |
| 114 | while (!par->shutdown) { |
| 115 | if (par->sender) { |
| 116 | sb.sem_num = SEM_WAIT_FOR_SENDER; |
| 117 | sb.sem_op = SEM_UNLOCK; |
| 118 | /* |
| 119 | * Unlocking the semaphore: |
| 120 | * Start of latency measurement ... |
| 121 | */ |
| 122 | gettimeofday(&par->unblocked, NULL); |
| 123 | semop(par->semid, &sb, 1); |
| 124 | par->samples++; |
| 125 | if (par->max_cycles && par->samples >= par->max_cycles) |
| 126 | par->shutdown = 1; |
| 127 | |
| 128 | if (mustgetcpu) |
| 129 | par->cpu = get_cpu(); |
| 130 | |
| 131 | sb.sem_num = SEM_WAIT_FOR_RECEIVER; |
| 132 | sb.sem_op = SEM_LOCK; |
| 133 | semop(par->semid, &sb, 1); |
| 134 | |
| 135 | sb.sem_num = SEM_WAIT_FOR_SENDER; |
| 136 | sb.sem_op = SEM_LOCK; |
| 137 | semop(par->semid, &sb, 1); |
| 138 | } else { |
| 139 | /* Receiver */ |
| 140 | struct params *neighbor; |
| 141 | |
| 142 | if (wasforked) |
| 143 | neighbor = par + par->num_threads; |
| 144 | else |
| 145 | neighbor = par->neighbor; |
| 146 | |
| 147 | sb.sem_num = SEM_WAIT_FOR_SENDER; |
| 148 | sb.sem_op = SEM_LOCK; |
| 149 | semop(par->semid, &sb, 1); |
| 150 | |
| 151 | /* |
| 152 | * ... We got the lock: |
| 153 | * End of latency measurement |
| 154 | */ |
| 155 | gettimeofday(&par->received, NULL); |
| 156 | par->samples++; |
| 157 | if (par->max_cycles && par->samples >= par->max_cycles) |
| 158 | par->shutdown = 1; |
| 159 | |
| 160 | if (mustgetcpu) |
| 161 | par->cpu = get_cpu(); |
| 162 | |
| 163 | timersub(&par->received, &neighbor->unblocked, |
| 164 | &par->diff); |
| 165 | |
| 166 | if (par->diff.tv_usec < par->mindiff) |
| 167 | par->mindiff = par->diff.tv_usec; |
| 168 | if (par->diff.tv_usec > par->maxdiff) |
| 169 | par->maxdiff = par->diff.tv_usec; |
| 170 | par->sumdiff += (double) par->diff.tv_usec; |
| 171 | if (par->tracelimit && par->maxdiff > par->tracelimit) { |
| 172 | char tracing_enabled_file[MAX_PATH]; |
| 173 | |
| 174 | strcpy(tracing_enabled_file, get_tracefs_prefix()); |
| 175 | strcat(tracing_enabled_file, "tracing_on"); |
| 176 | int tracing_enabled = |
| 177 | open(tracing_enabled_file, O_WRONLY); |
| 178 | if (tracing_enabled >= 0) { |
| 179 | ret = write(tracing_enabled, "0", 1); |
| 180 | if (ret < 0) |
| 181 | fatal("Could not write to %s: %s\n", |
| 182 | tracing_enabled_file, strerror(errno)); |
| 183 | close(tracing_enabled); |
| 184 | } else |
| 185 | fatal("Could not access %s\n", |
| 186 | tracing_enabled_file); |
| 187 | par->shutdown = 1; |
| 188 | neighbor->shutdown = 1; |
| 189 | } |
| 190 | |
| 191 | sb.sem_num = SEM_WAIT_FOR_RECEIVER; |
| 192 | sb.sem_op = SEM_UNLOCK; |
| 193 | semop(par->semid, &sb, 1); |
| 194 | |
| 195 | nanosleep(&par->delay, NULL); |
| 196 | |
| 197 | sb.sem_num = SEM_WAIT_FOR_SENDER; |
| 198 | sb.sem_op = SEM_UNLOCK; |
| 199 | semop(par->semid, &sb, 1); |
| 200 | } |
| 201 | } |
| 202 | if (par->sender) { |
| 203 | sb.sem_num = SEM_WAIT_FOR_SENDER; |
| 204 | sb.sem_op = SEM_UNLOCK; |
| 205 | semop(par->semid, &sb, 1); |
| 206 | |
| 207 | sb.sem_num = SEM_WAIT_FOR_RECEIVER; |
| 208 | sb.sem_op = SEM_UNLOCK; |
| 209 | semop(par->semid, &sb, 1); |
| 210 | } |
| 211 | par->stopped = 1; |
| 212 | return NULL; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | union semun { |
| 217 | int val; /* Value for SETVAL */ |
| 218 | struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ |
| 219 | unsigned short *array; /* Array for GETALL, SETALL */ |
| 220 | struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */ |
| 221 | }; |
| 222 | |
| 223 | |
| 224 | static void display_help(int error) |
| 225 | { |
| 226 | printf("svsematest V %1.2f\n", VERSION); |
| 227 | printf("Usage:\n" |
| 228 | "svsematest <options>\n\n" |
| 229 | "Function: test SYSV semaphore latency\n\n" |
| 230 | "Avaiable options:\n" |
| 231 | "-a [NUM] --affinity run thread #N on processor #N, if possible\n" |
| 232 | " with NUM pin all threads to the processor NUM\n" |
| 233 | "-b USEC --breaktrace=USEC send break trace command when latency > USEC\n" |
| 234 | "-d DIST --distance=DIST distance of thread intervals in us default=500\n" |
| 235 | "-D --duration=TIME specify a length for the test run.\n" |
| 236 | " Append 'm', 'h', or 'd' to specify minutes, hours or\n" |
| 237 | " days.\n" |
| 238 | "-f [OPT] --fork[=OPT] fork new processes instead of creating threads\n" |
| 239 | "-i INTV --interval=INTV base interval of thread in us default=1000\n" |
| 240 | " --json=FILENAME write final results into FILENAME, JSON formatted\n" |
| 241 | "-l LOOPS --loops=LOOPS number of loops: default=0(endless)\n" |
| 242 | "-p PRIO --prio=PRIO priority\n" |
| 243 | "-S --smp SMP testing: options -a -t and same priority\n" |
| 244 | " of all threads\n" |
| 245 | "-t --threads one thread per available processor\n" |
| 246 | "-t [NUM] --threads[=NUM] number of threads:\n" |
| 247 | " without NUM, threads = max_cpus\n" |
| 248 | " without -t default = 1\n" |
| 249 | ); |
| 250 | exit(error); |
| 251 | } |
| 252 | |
| 253 | static int setaffinity = AFFINITY_UNSPECIFIED; |
| 254 | static int affinity; |
| 255 | static int priority; |
| 256 | static int num_threads = 1; |
| 257 | static int max_cycles; |
| 258 | static int duration; |
| 259 | static int interval = 1000; |
| 260 | static int distance = 500; |
| 261 | static int smp; |
| 262 | static int sameprio; |
| 263 | static int quiet; |
| 264 | static char jsonfile[MAX_PATH]; |
| 265 | |
| 266 | enum option_value { |
| 267 | OPT_AFFINITY=1, OPT_BREAKTRACE, OPT_DISTANCE, OPT_DURATION, |
| 268 | OPT_FORK, OPT_HELP, OPT_INTERVAL, OPT_JSON, OPT_LOOPS, |
| 269 | OPT_PRIORITY, OPT_QUIET, OPT_SMP, OPT_THREADS |
| 270 | }; |
| 271 | |
| 272 | static void process_options(int argc, char *argv[]) |
| 273 | { |
| 274 | int error = 0; |
| 275 | int max_cpus = sysconf(_SC_NPROCESSORS_CONF); |
| 276 | int thistracelimit = 0; |
| 277 | |
| 278 | for (;;) { |
| 279 | int option_index = 0; |
| 280 | /** Options for getopt */ |
| 281 | static struct option long_options[] = { |
| 282 | {"affinity", optional_argument, NULL, OPT_AFFINITY}, |
| 283 | {"breaktrace", required_argument, NULL, OPT_BREAKTRACE}, |
| 284 | {"distance", required_argument, NULL, OPT_DISTANCE}, |
| 285 | {"duration", required_argument, NULL, OPT_DURATION}, |
| 286 | {"fork", optional_argument, NULL, OPT_FORK}, |
| 287 | {"help", no_argument, NULL, OPT_HELP}, |
| 288 | {"interval", required_argument, NULL, OPT_INTERVAL}, |
| 289 | {"json", required_argument, NULL, OPT_JSON}, |
| 290 | {"loops", required_argument, NULL, OPT_LOOPS}, |
| 291 | {"priority", required_argument, NULL, OPT_PRIORITY}, |
| 292 | {"quiet", no_argument, NULL, OPT_QUIET}, |
| 293 | {"smp", no_argument, NULL, OPT_SMP}, |
| 294 | {"threads", optional_argument, NULL, OPT_THREADS}, |
| 295 | {NULL, 0, NULL, 0} |
| 296 | }; |
| 297 | int c = getopt_long (argc, argv, "a::b:d:D:f::hi:l:p:qSt::", |
| 298 | long_options, &option_index); |
| 299 | if (c == -1) |
| 300 | break; |
| 301 | switch (c) { |
| 302 | case OPT_AFFINITY: |
| 303 | case 'a': |
| 304 | if (smp) { |
| 305 | warn("-a ignored due to --smp\n"); |
| 306 | break; |
| 307 | } |
| 308 | if (optarg != NULL) { |
| 309 | affinity = atoi(optarg); |
| 310 | setaffinity = AFFINITY_SPECIFIED; |
| 311 | } else if (optind < argc && atoi(argv[optind])) { |
| 312 | affinity = atoi(argv[optind]); |
| 313 | setaffinity = AFFINITY_SPECIFIED; |
| 314 | } else { |
| 315 | setaffinity = AFFINITY_USEALL; |
| 316 | } |
| 317 | break; |
| 318 | case OPT_BREAKTRACE: |
| 319 | case 'b': |
| 320 | thistracelimit = atoi(optarg); |
| 321 | break; |
| 322 | case OPT_DISTANCE: |
| 323 | case 'd': |
| 324 | distance = atoi(optarg); |
| 325 | break; |
| 326 | case OPT_DURATION: |
| 327 | case 'D': |
| 328 | duration = parse_time_string(optarg); |
| 329 | break; |
| 330 | case OPT_FORK: |
| 331 | case 'f': |
| 332 | if (optarg != NULL) { |
| 333 | wasforked = 1; |
| 334 | if (optarg[0] == 's') |
| 335 | wasforked_sender = 1; |
| 336 | else if (optarg[0] == 'r') |
| 337 | wasforked_sender = 0; |
| 338 | wasforked_threadno = atoi(optarg+1); |
| 339 | } else |
| 340 | mustfork = 1; |
| 341 | break; |
| 342 | case OPT_HELP: |
| 343 | case 'h': |
| 344 | display_help(0); |
| 345 | break; |
| 346 | case OPT_INTERVAL: |
| 347 | case 'i': |
| 348 | interval = atoi(optarg); |
| 349 | break; |
| 350 | case OPT_JSON: |
| 351 | strncpy(jsonfile, optarg, strnlen(optarg, MAX_PATH-1)); |
| 352 | break; |
| 353 | case OPT_LOOPS: |
| 354 | case 'l': |
| 355 | max_cycles = atoi(optarg); |
| 356 | break; |
| 357 | case OPT_PRIORITY: |
| 358 | case 'p': |
| 359 | priority = atoi(optarg); |
| 360 | break; |
| 361 | case OPT_QUIET: |
| 362 | case 'q': |
| 363 | quiet = 1; |
| 364 | break; |
| 365 | case OPT_SMP: |
| 366 | case 'S': |
| 367 | smp = 1; |
| 368 | num_threads = max_cpus; |
| 369 | setaffinity = AFFINITY_USEALL; |
| 370 | break; |
| 371 | case OPT_THREADS: |
| 372 | case 't': |
| 373 | if (smp) { |
| 374 | warn("-t ignored due to --smp\n"); |
| 375 | break; |
| 376 | } |
| 377 | if (optarg != NULL) |
| 378 | num_threads = atoi(optarg); |
| 379 | else if (optind < argc && atoi(argv[optind])) |
| 380 | num_threads = atoi(argv[optind]); |
| 381 | else |
| 382 | num_threads = max_cpus; |
| 383 | break; |
| 384 | default: |
| 385 | display_help(1); |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if (!wasforked) { |
| 391 | if (setaffinity == AFFINITY_SPECIFIED) { |
| 392 | if (affinity < 0) |
| 393 | error = 1; |
| 394 | if (affinity >= max_cpus) { |
| 395 | fprintf(stderr, "ERROR: CPU #%d not found, " |
| 396 | "only %d CPUs available\n", |
| 397 | affinity, max_cpus); |
| 398 | error = 1; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if (duration < 0) |
| 403 | error = 0; |
| 404 | |
| 405 | if (num_threads < 1) |
| 406 | error = 1; |
| 407 | |
| 408 | if (priority < 0 || priority > 99) |
| 409 | error = 1; |
| 410 | |
| 411 | if (priority && smp) |
| 412 | sameprio = 1; |
| 413 | |
| 414 | tracelimit = thistracelimit; |
| 415 | } |
| 416 | if (error) |
| 417 | display_help(error); |
| 418 | } |
| 419 | |
| 420 | |
| 421 | static int volatile mustshutdown; |
| 422 | |
| 423 | static void sighand(int sig) |
| 424 | { |
| 425 | mustshutdown = 1; |
| 426 | } |
| 427 | |
| 428 | struct params_stats { |
| 429 | struct params *receiver; |
| 430 | struct params *sender; |
| 431 | }; |
| 432 | |
| 433 | static void write_stats(FILE *f, void *data) |
| 434 | { |
| 435 | struct params_stats *ps = data; |
| 436 | struct params *s, *r; |
| 437 | int i; |
| 438 | |
| 439 | fprintf(f, " \"num_threads\": %d,\n", num_threads); |
| 440 | fprintf(f, " \"thread\": {\n"); |
| 441 | for (i = 0; i < num_threads; i++) { |
| 442 | s = &ps->sender[i]; |
| 443 | r = &ps->receiver[i]; |
| 444 | fprintf(f, " \"%u\": {\n", i); |
| 445 | fprintf(f, " \"sender\": {\n"); |
| 446 | fprintf(f, " \"cpu\": %d,\n", s->cpu); |
| 447 | fprintf(f, " \"priority\": %d,\n", s->priority); |
| 448 | fprintf(f, " \"samples\": %d,\n", s->samples); |
| 449 | fprintf(f, " \"interval\": %ld\n", r->delay.tv_nsec/1000); |
| 450 | fprintf(f, " },\n"); |
| 451 | fprintf(f, " \"receiver\": {\n"); |
| 452 | fprintf(f, " \"cpu\": %d,\n", r->cpu); |
| 453 | fprintf(f, " \"priority\": %d,\n", r->priority); |
| 454 | fprintf(f, " \"min\": %d,\n", r->mindiff); |
| 455 | fprintf(f, " \"avg\": %.2f,\n", r->sumdiff/r->samples); |
| 456 | fprintf(f, " \"max\": %d\n", r->maxdiff); |
| 457 | fprintf(f, " }\n"); |
| 458 | fprintf(f, " }%s\n", i == num_threads - 1 ? "" : ","); |
| 459 | } |
| 460 | fprintf(f, " }\n"); |
| 461 | } |
| 462 | |
| 463 | static void print_stat(FILE *fp, struct params *receiver, struct params *sender, |
| 464 | int verbose, int quiet) |
| 465 | { |
| 466 | int i; |
| 467 | |
| 468 | if (quiet) |
| 469 | return; |
| 470 | |
| 471 | for (i = 0; i < num_threads; i++) { |
| 472 | int receiver_pid, sender_pid; |
| 473 | |
| 474 | if (mustfork) { |
| 475 | receiver_pid = receiver[i].pid; |
| 476 | sender_pid = sender[i].pid; |
| 477 | } else { |
| 478 | receiver_pid = receiver[i].tid; |
| 479 | sender_pid = sender[i].tid; |
| 480 | } |
| 481 | printf("#%1d: ID%d, P%d, CPU%d, I%ld; #%1d: ID%d, P%d, CPU%d, Cycles %d\n", |
| 482 | i*2, receiver_pid, receiver[i].priority, |
| 483 | receiver[i].cpu, receiver[i].delay.tv_nsec / |
| 484 | 1000, i*2+1, sender_pid, sender[i].priority, |
| 485 | sender[i].cpu, sender[i].samples); |
| 486 | } |
| 487 | |
| 488 | for (i = 0; i < num_threads; i++) { |
| 489 | if (receiver[i].mindiff == -1) |
| 490 | printf("#%d -> #%d (not yet ready)\n", |
| 491 | i*2+1, i*2); |
| 492 | else |
| 493 | printf("#%d -> #%d, Min %4d, Cur %4d, Avg %4d, Max %4d\n", |
| 494 | i*2+1, i*2, receiver[i].mindiff, |
| 495 | (int) receiver[i].diff.tv_usec, |
| 496 | (int) ((receiver[i].sumdiff / |
| 497 | receiver[i].samples) + 0.5), |
| 498 | receiver[i].maxdiff); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | int main(int argc, char *argv[]) |
| 503 | { |
| 504 | char *myfile; |
| 505 | int i, totalsize = 0; |
| 506 | int max_cpus = sysconf(_SC_NPROCESSORS_CONF); |
| 507 | int oldsamples = 1; |
| 508 | key_t key; |
| 509 | union semun args; |
| 510 | struct params *receiver = NULL; |
| 511 | struct params *sender = NULL; |
| 512 | sigset_t sigset; |
| 513 | void *param = NULL; |
| 514 | char f_opt[14]; |
| 515 | struct timespec launchdelay, maindelay; |
| 516 | |
| 517 | myfile = getenv("_"); |
| 518 | if (myfile == NULL) |
| 519 | myfile = argv[0]; |
| 520 | |
| 521 | rt_init(argc, argv); |
| 522 | process_options(argc, argv); |
| 523 | |
| 524 | if (check_privs()) |
| 525 | return 1; |
| 526 | |
| 527 | if (mlockall(MCL_CURRENT|MCL_FUTURE) == -1) { |
| 528 | perror("mlockall"); |
| 529 | return 1; |
| 530 | } |
| 531 | |
| 532 | get_cpu_setup(); |
| 533 | |
| 534 | if (mustfork) { |
| 535 | int shmem; |
| 536 | |
| 537 | /* |
| 538 | * In fork mode (-f), the shared memory contains two |
| 539 | * subsequent arrays, receiver[num_threads] and |
| 540 | * sender[num_threads]. |
| 541 | */ |
| 542 | totalsize = num_threads * sizeof(struct params) * 2; |
| 543 | |
| 544 | shm_unlink("/sigwaittest"); |
| 545 | shmem = shm_open("/sigwaittest", O_CREAT|O_EXCL|O_RDWR, |
| 546 | S_IRUSR|S_IWUSR); |
| 547 | if (shmem < 0) { |
| 548 | fprintf(stderr, "Could not create shared memory\n"); |
| 549 | return 1; |
| 550 | } |
| 551 | if (ftruncate(shmem, totalsize) == -1) { |
| 552 | perror("ftruncate failed"); |
| 553 | close(shmem); |
| 554 | shm_unlink("/sigwaittest"); |
| 555 | return 1; |
| 556 | } |
| 557 | param = mmap(0, totalsize, PROT_READ|PROT_WRITE, MAP_SHARED, |
| 558 | shmem, 0); |
| 559 | if (param == MAP_FAILED) { |
| 560 | fprintf(stderr, "Could not map shared memory\n"); |
| 561 | close(shmem); |
| 562 | return 1; |
| 563 | } |
| 564 | |
| 565 | receiver = (struct params *) param; |
| 566 | sender = receiver + num_threads; |
| 567 | } else if (wasforked) { |
| 568 | struct stat buf; |
| 569 | int shmem, totalsize, expect_totalsize; |
| 570 | |
| 571 | if (wasforked_threadno == -1 || wasforked_sender == -1) { |
| 572 | fprintf(stderr, "Invalid fork option\n"); |
| 573 | return 1; |
| 574 | } |
| 575 | shmem = shm_open("/sigwaittest", O_RDWR, S_IRUSR|S_IWUSR); |
| 576 | if (fstat(shmem, &buf)) { |
| 577 | fprintf(stderr, |
| 578 | "Could not determine shared memory size\n"); |
| 579 | close(shmem); |
| 580 | return 1; |
| 581 | } |
| 582 | totalsize = buf.st_size; |
| 583 | param = mmap(0, totalsize, PROT_READ|PROT_WRITE, MAP_SHARED, |
| 584 | shmem, 0); |
| 585 | close(shmem); |
| 586 | if (param == MAP_FAILED) { |
| 587 | fprintf(stderr, "Could not map shared memory\n"); |
| 588 | return 1; |
| 589 | } |
| 590 | |
| 591 | receiver = (struct params *) param; |
| 592 | expect_totalsize = receiver->num_threads * |
| 593 | sizeof(struct params) * 2; |
| 594 | if (totalsize != expect_totalsize) { |
| 595 | fprintf(stderr, "Memory size problem (expected %d, " |
| 596 | "found %d\n", expect_totalsize, totalsize); |
| 597 | munmap(param, totalsize); |
| 598 | return 1; |
| 599 | } |
| 600 | sender = receiver + receiver->num_threads; |
| 601 | if (wasforked_sender) |
| 602 | semathread(sender + wasforked_threadno); |
| 603 | else |
| 604 | semathread(receiver + wasforked_threadno); |
| 605 | munmap(param, totalsize); |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | signal(SIGINT, sighand); |
| 610 | signal(SIGTERM, sighand); |
| 611 | signal(SIGALRM, sighand); |
| 612 | |
| 613 | sigemptyset(&sigset); |
| 614 | pthread_sigmask(SIG_SETMASK, &sigset, NULL); |
| 615 | |
| 616 | if (duration) |
| 617 | alarm(duration); |
| 618 | |
| 619 | if (!mustfork && !wasforked) { |
| 620 | receiver = calloc(num_threads, sizeof(struct params)); |
| 621 | sender = calloc(num_threads, sizeof(struct params)); |
| 622 | if (receiver == NULL || sender == NULL) |
| 623 | goto nomem; |
| 624 | } |
| 625 | |
| 626 | launchdelay.tv_sec = 0; |
| 627 | launchdelay.tv_nsec = 10000000; /* 10 ms */ |
| 628 | |
| 629 | maindelay.tv_sec = 0; |
| 630 | maindelay.tv_nsec = 50000000; /* 50 ms */ |
| 631 | |
| 632 | for (i = 0; i < num_threads; i++) { |
| 633 | struct sembuf sb = { 0, 0, 0}; |
| 634 | |
| 635 | receiver[i].mindiff = UINT_MAX; |
| 636 | receiver[i].maxdiff = 0; |
| 637 | receiver[i].sumdiff = 0.0; |
| 638 | |
| 639 | if ((key = ftok(myfile, i)) == -1) { |
| 640 | perror("ftok"); |
| 641 | goto nosem; |
| 642 | } |
| 643 | |
| 644 | if ((receiver[i].semid = semget(key, 2, 0666 | IPC_CREAT)) == -1) { |
| 645 | perror("semget"); |
| 646 | goto nosem; |
| 647 | } |
| 648 | |
| 649 | args.val = 1; |
| 650 | if (semctl(receiver[i].semid, SEM_WAIT_FOR_RECEIVER, SETVAL, args) == -1) { |
| 651 | perror("semctl sema #0"); |
| 652 | goto nosem; |
| 653 | } |
| 654 | |
| 655 | if (semctl(receiver[i].semid, SEM_WAIT_FOR_SENDER, SETVAL, args) == -1) { |
| 656 | perror("semctl sema #1"); |
| 657 | goto nosem; |
| 658 | } |
| 659 | |
| 660 | sb.sem_num = SEM_WAIT_FOR_RECEIVER; |
| 661 | sb.sem_op = SEM_LOCK; |
| 662 | semop(receiver[i].semid, &sb, 1); |
| 663 | |
| 664 | sb.sem_num = SEM_WAIT_FOR_SENDER; |
| 665 | sb.sem_op = SEM_LOCK; |
| 666 | semop(receiver[i].semid, &sb, 1); |
| 667 | |
| 668 | receiver[i].cpu = i; |
| 669 | switch (setaffinity) { |
| 670 | case AFFINITY_UNSPECIFIED: receiver[i].cpu = -1; break; |
| 671 | case AFFINITY_SPECIFIED: receiver[i].cpu = affinity; break; |
| 672 | case AFFINITY_USEALL: receiver[i].cpu = i % max_cpus; break; |
| 673 | } |
| 674 | receiver[i].priority = priority; |
| 675 | receiver[i].tracelimit = tracelimit; |
| 676 | if (priority > 1 && !sameprio) |
| 677 | priority--; |
| 678 | receiver[i].delay.tv_sec = interval / USEC_PER_SEC; |
| 679 | receiver[i].delay.tv_nsec = (interval % USEC_PER_SEC) * 1000; |
| 680 | interval += distance; |
| 681 | receiver[i].max_cycles = max_cycles; |
| 682 | receiver[i].sender = 0; |
| 683 | receiver[i].neighbor = &sender[i]; |
| 684 | if (mustfork) { |
| 685 | pid_t pid = fork(); |
| 686 | if (pid == -1) { |
| 687 | fprintf(stderr, "Could not fork\n"); |
| 688 | return 1; |
| 689 | } else if (pid == 0) { |
| 690 | char *args[3]; |
| 691 | |
| 692 | receiver[i].num_threads = num_threads; |
| 693 | receiver[i].pid = getpid(); |
| 694 | sprintf(f_opt, "-fr%d", i); |
| 695 | args[0] = argv[0]; |
| 696 | args[1] = f_opt; |
| 697 | args[2] = NULL; |
| 698 | execvp(args[0], args); |
| 699 | fprintf(stderr, |
| 700 | "Could not execute receiver child process " |
| 701 | "#%d\n", i); |
| 702 | } |
| 703 | } else |
| 704 | pthread_create(&receiver[i].threadid, NULL, |
| 705 | semathread, &receiver[i]); |
| 706 | |
| 707 | nanosleep(&launchdelay, NULL); |
| 708 | |
| 709 | memcpy(&sender[i], &receiver[i], sizeof(receiver[0])); |
| 710 | sender[i].sender = 1; |
| 711 | sender[i].neighbor = &receiver[i]; |
| 712 | if (mustfork) { |
| 713 | pid_t pid = fork(); |
| 714 | if (pid == -1) { |
| 715 | fprintf(stderr, "Could not fork\n"); |
| 716 | return 1; |
| 717 | } else if (pid == 0) { |
| 718 | char *args[3]; |
| 719 | |
| 720 | sender[i].num_threads = num_threads; |
| 721 | sender[i].pid = getpid(); |
| 722 | sprintf(f_opt, "-fs%d", i); |
| 723 | args[0] = argv[0]; |
| 724 | args[1] = f_opt; |
| 725 | args[2] = NULL; |
| 726 | execvp(args[0], args); |
| 727 | fprintf(stderr, |
| 728 | "Could not execute sender child process " |
| 729 | "#%d\n", i); |
| 730 | } |
| 731 | } else |
| 732 | pthread_create(&sender[i].threadid, NULL, semathread, |
| 733 | &sender[i]); |
| 734 | } |
| 735 | |
| 736 | while (!mustshutdown) { |
| 737 | for (i = 0; i < num_threads; i++) |
| 738 | mustshutdown |= receiver[i].shutdown | |
| 739 | sender[i].shutdown; |
| 740 | |
| 741 | if (receiver[0].samples > oldsamples || mustshutdown) { |
| 742 | print_stat(stdout, receiver, sender, 0, quiet); |
| 743 | if (!quiet) |
| 744 | printf("\033[%dA", num_threads*2); |
| 745 | } |
| 746 | |
| 747 | sigemptyset(&sigset); |
| 748 | sigaddset(&sigset, SIGTERM); |
| 749 | sigaddset(&sigset, SIGINT); |
| 750 | sigaddset(&sigset, SIGALRM); |
| 751 | pthread_sigmask(SIG_SETMASK, &sigset, NULL); |
| 752 | |
| 753 | nanosleep(&maindelay, NULL); |
| 754 | |
| 755 | sigemptyset(&sigset); |
| 756 | pthread_sigmask(SIG_SETMASK, &sigset, NULL); |
| 757 | } |
| 758 | |
| 759 | if (!quiet) |
| 760 | printf("\033[%dB", num_threads*2 + 2); |
| 761 | else |
| 762 | print_stat(stdout, receiver, sender, 0, 0); |
| 763 | |
| 764 | for (i = 0; i < num_threads; i++) { |
| 765 | receiver[i].shutdown = 1; |
| 766 | sender[i].shutdown = 1; |
| 767 | } |
| 768 | nanosleep(&receiver[0].delay, NULL); |
| 769 | |
| 770 | for (i = 0; i < num_threads; i++) { |
| 771 | if (!receiver[i].stopped) { |
| 772 | if (mustfork) |
| 773 | kill(receiver[i].pid, SIGTERM); |
| 774 | else |
| 775 | pthread_kill(receiver[i].threadid, SIGTERM); |
| 776 | } |
| 777 | if (!sender[i].stopped) { |
| 778 | if (mustfork) |
| 779 | kill(sender[i].pid, SIGTERM); |
| 780 | else |
| 781 | pthread_kill(sender[i].threadid, SIGTERM); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | if (strlen(jsonfile) != 0) { |
| 786 | struct params_stats ps = { |
| 787 | .receiver = receiver, |
| 788 | .sender = sender, |
| 789 | }; |
| 790 | rt_write_json(jsonfile, 0, write_stats, &ps); |
| 791 | } |
| 792 | |
| 793 | nosem: |
| 794 | for (i = 0; i < num_threads; i++) |
| 795 | semctl(receiver[i].semid, -1, IPC_RMID); |
| 796 | |
| 797 | nomem: |
| 798 | if (mustfork) { |
| 799 | munmap(param, totalsize); |
| 800 | shm_unlink("/sigwaittest"); |
| 801 | } |
| 802 | |
| 803 | return 0; |
| 804 | } |