| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * pi_stress - Priority Inheritance stress test |
| 4 | * Copyright (C) 2006, 2007 Clark Williams <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This program stress tests pthreads priority inheritance mutexes |
| 9 | |
| 10 | * The logic is built upon the state machine that performs the "classic_pi" |
| 11 | * deadlock scenario. A state machine or "inversion group" is a group of three |
| 12 | * threads as described below. |
| 13 | * |
| 14 | * The basic premise here is to set up a deadlock scenario and confirm that PI |
| 15 | * mutexes resolve the situation. Three worker threads will be created from the |
| 16 | * main thread: low, medium and high priority threads that use SCHED_FIFO as |
| 17 | * their scheduling policy. The low priority thread claims a mutex and then |
| 18 | * starts "working". The medium priority thread starts and preempts the low |
| 19 | * priority thread. Then the high priority thread runs and attempts to claim |
| 20 | * the mutex owned by the low priority thread. Without priority inheritance, |
| 21 | * this will deadlock the program. With priority inheritance, the low priority |
| 22 | * thread receives a priority boost, finishes it's "work" and releases the mutex |
| 23 | * which allows the high priority thread to run and finish and then the medium |
| 24 | * priority thread finishes. |
| 25 | * |
| 26 | * That's the theory, anyway... |
| 27 | * |
| 28 | * CW - 2006 |
| 29 | */ |
| 30 | |
| 31 | #include "pi_stress.h" |
| 32 | |
| 33 | /* the length of the test */ |
| 34 | /* default is infinite */ |
| 35 | int duration = -1; |
| 36 | |
| 37 | /* times for starting and finishing the stress test */ |
| 38 | time_t start, finish; |
| 39 | |
| 40 | /* the number of groups to create */ |
| 41 | int ngroups; |
| 42 | |
| 43 | /* the number of times a group causes a priority inversion situation */ |
| 44 | /* default to infinite */ |
| 45 | int inversions = -1; |
| 46 | |
| 47 | /* turn on lots of prints */ |
| 48 | int verbose; |
| 49 | |
| 50 | /* turn on pi_debugging prints */ |
| 51 | int debugging; |
| 52 | |
| 53 | /* turn off all prints, default = 0 (off) */ |
| 54 | int quiet; |
| 55 | |
| 56 | /* filename for JSON output */ |
| 57 | char jsonfile[MAX_PATH]; |
| 58 | |
| 59 | /* prompt to start test */ |
| 60 | int prompt; |
| 61 | |
| 62 | /* report interval */ |
| 63 | unsigned long report_interval = (unsigned long)SEC_TO_USEC(0.75); |
| 64 | |
| 65 | int shutdown; /* global indicating we should shut down */ |
| 66 | pthread_mutex_t shutdown_mtx; /* associated mutex */ |
| 67 | |
| 68 | /* indicate if errors have occurred */ |
| 69 | int have_errors; |
| 70 | |
| 71 | /* indicated that keyboard interrupt has happened */ |
| 72 | int interrupted; |
| 73 | |
| 74 | /* force running on one cpu */ |
| 75 | int uniprocessor; |
| 76 | |
| 77 | /* lock all memory */ |
| 78 | int lockall; |
| 79 | |
| 80 | pthread_barrier_t all_threads_ready; |
| 81 | pthread_barrier_t all_threads_done; |
| 82 | |
| 83 | cpu_set_t test_cpu_mask, admin_cpu_mask; |
| 84 | |
| 85 | int policy = SCHED_FIFO; |
| 86 | |
| 87 | /* scheduling attributes per thread */ |
| 88 | struct sched_attr low_sa; |
| 89 | struct sched_attr med_sa; |
| 90 | struct sched_attr high_sa; |
| 91 | struct sched_attr admin_sa; |
| 92 | |
| 93 | unsigned int sa_initialized; |
| 94 | |
| 95 | struct group_parameters *groups; |
| 96 | |
| 97 | /* number of online processors */ |
| 98 | long num_processors; |
| 99 | |
| 100 | int main(int argc, char **argv) |
| 101 | { |
| 102 | int status; |
| 103 | struct sched_param thread_param; |
| 104 | int i; |
| 105 | int retval = FAILURE; |
| 106 | int core; |
| 107 | int nthreads; |
| 108 | |
| 109 | /* Make sure we see all message, even those on stdout. */ |
| 110 | setvbuf(stdout, NULL, _IONBF, 0); |
| 111 | |
| 112 | /* get the number of processors */ |
| 113 | num_processors = sysconf(_SC_NPROCESSORS_ONLN); |
| 114 | |
| 115 | /* calculate the number of inversion groups to run */ |
| 116 | ngroups = num_processors == 1 ? 1 : num_processors - 1; |
| 117 | |
| 118 | |
| 119 | /* process command line arguments */ |
| 120 | rt_init(argc, argv); |
| 121 | process_command_line(argc, argv); |
| 122 | |
| 123 | /* set default sched attributes */ |
| 124 | setup_sched_config(policy); |
| 125 | |
| 126 | /* lock memory */ |
| 127 | if (lockall) |
| 128 | if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { |
| 129 | pi_error("mlockall failed\n"); |
| 130 | return FAILURE; |
| 131 | } |
| 132 | /* boost main's priority (so we keep running) :) */ |
| 133 | thread_param.sched_priority = admin_sa.sched_priority; |
| 134 | status = pthread_setschedparam(pthread_self(), admin_sa.sched_policy, |
| 135 | &thread_param); |
| 136 | if (status) { |
| 137 | pi_error("%s: boosting to max priority: 0x%x\n", __func__, |
| 138 | status); |
| 139 | return FAILURE; |
| 140 | } |
| 141 | /* block unwanted signals */ |
| 142 | block_signals(); |
| 143 | |
| 144 | /* allocate our groups array */ |
| 145 | groups = calloc(ngroups, sizeof(struct group_parameters)); |
| 146 | if (groups == NULL) { |
| 147 | pi_error("%s: failed to allocate %d groups\n", __func__, |
| 148 | ngroups); |
| 149 | return FAILURE; |
| 150 | } |
| 151 | /* set up CPU affinity masks */ |
| 152 | if (set_cpu_affinity(&test_cpu_mask, &admin_cpu_mask)) |
| 153 | return FAILURE; |
| 154 | |
| 155 | nthreads = ngroups * NUM_TEST_THREADS + NUM_ADMIN_THREADS; |
| 156 | |
| 157 | /* set up our ready barrier */ |
| 158 | if (barrier_init(&all_threads_ready, NULL, nthreads, |
| 159 | "all_threads_ready")) |
| 160 | return FAILURE; |
| 161 | |
| 162 | /* set up our done barrier */ |
| 163 | if (barrier_init(&all_threads_done, NULL, nthreads, "all_threads_done")) |
| 164 | return FAILURE; |
| 165 | |
| 166 | /* create the groups */ |
| 167 | pi_info("Creating %d test groups\n", ngroups); |
| 168 | for (core = 0; core < num_processors; core++) |
| 169 | if (CPU_ISSET(core, &test_cpu_mask)) |
| 170 | break; |
| 171 | for (i = 0; i < ngroups; i++) { |
| 172 | groups[i].id = i; |
| 173 | groups[i].cpu = core++; |
| 174 | if (core >= num_processors) |
| 175 | core = 0; |
| 176 | if (create_group(&groups[i]) != SUCCESS) |
| 177 | return FAILURE; |
| 178 | } |
| 179 | |
| 180 | /* prompt if requested */ |
| 181 | if (prompt) { |
| 182 | printf("Press return to start test: "); |
| 183 | getchar(); |
| 184 | } |
| 185 | /* report */ |
| 186 | banner(); |
| 187 | start = time(NULL); |
| 188 | |
| 189 | /* turn loose the threads */ |
| 190 | pi_info("Releasing all threads\n"); |
| 191 | status = pthread_barrier_wait(&all_threads_ready); |
| 192 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 193 | pi_error("%s: pthread_barrier_wait(all_threads_ready): 0x%x\n", |
| 194 | __func__, status); |
| 195 | set_shutdown_flag(); |
| 196 | return FAILURE; |
| 197 | } |
| 198 | |
| 199 | reporter(NULL); |
| 200 | |
| 201 | if (!quiet) { |
| 202 | fputs(DOWN_ONE, stdout); |
| 203 | printf("Stopping test\n"); |
| 204 | } |
| 205 | set_shutdown_flag(); |
| 206 | |
| 207 | /* wait for all threads to notice the shutdown flag */ |
| 208 | if (have_errors == 0 && interrupted == 0) { |
| 209 | pi_info("waiting for all threads to complete\n"); |
| 210 | status = pthread_barrier_wait(&all_threads_done); |
| 211 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 212 | pi_error("%s: pthread_barrier_wait(all_threads_ready): 0x%x\n", __func__, status); |
| 213 | return FAILURE; |
| 214 | } |
| 215 | pi_info("All threads terminated!\n"); |
| 216 | retval = SUCCESS; |
| 217 | } else |
| 218 | kill(0, SIGTERM); |
| 219 | finish = time(NULL); |
| 220 | summary(); |
| 221 | |
| 222 | if (strlen(jsonfile) != 0) |
| 223 | rt_write_json(jsonfile, retval, write_stats, NULL); |
| 224 | |
| 225 | if (lockall) |
| 226 | munlockall(); |
| 227 | exit(retval); |
| 228 | } |
| 229 | |
| 230 | int |
| 231 | setup_thread_attr(pthread_attr_t *attr, struct sched_attr *sa, |
| 232 | cpu_set_t *mask) |
| 233 | { |
| 234 | int status; |
| 235 | struct sched_param thread_param; |
| 236 | |
| 237 | status = pthread_attr_init(attr); |
| 238 | if (status) { |
| 239 | pi_error |
| 240 | ("%s: initializing thread attribute: 0x%x\n", __func__, |
| 241 | status); |
| 242 | return FAILURE; |
| 243 | } |
| 244 | status = pthread_attr_setaffinity_np(attr, sizeof(cpu_set_t), mask); |
| 245 | if (status) { |
| 246 | pi_error("%s: setting affinity attribute: 0x%x\n", __func__, |
| 247 | status); |
| 248 | return FAILURE; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * The pthread API does not yet support SCHED_DEADLINE, defer the |
| 253 | * thread configuration to setup_thread() |
| 254 | */ |
| 255 | if (sa->sched_policy == SCHED_DEADLINE) |
| 256 | return SUCCESS; |
| 257 | |
| 258 | status = pthread_attr_setschedpolicy(attr, sa->sched_policy); |
| 259 | if (status) { |
| 260 | pi_error |
| 261 | ("%s: setting attribute policy to %s: 0x%x\n", __func__, |
| 262 | policy_to_string(sa->sched_policy), |
| 263 | status); |
| 264 | return FAILURE; |
| 265 | } |
| 266 | status = pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); |
| 267 | if (status) { |
| 268 | pi_error |
| 269 | ("%s: setting explicit scheduling inheritance: 0x%x\n", |
| 270 | __func__, status); |
| 271 | return FAILURE; |
| 272 | } |
| 273 | thread_param.sched_priority = sa->sched_priority; |
| 274 | status = pthread_attr_setschedparam(attr, &thread_param); |
| 275 | if (status) { |
| 276 | pi_error("%s: setting scheduler param: 0x%x\n", __func__, |
| 277 | status); |
| 278 | return FAILURE; |
| 279 | } |
| 280 | return SUCCESS; |
| 281 | } |
| 282 | |
| 283 | int set_cpu_affinity(cpu_set_t *test_mask, cpu_set_t *admin_mask) |
| 284 | { |
| 285 | int status, i, admin_proc; |
| 286 | cpu_set_t current_mask; |
| 287 | |
| 288 | /* handle uniprocessor case */ |
| 289 | if (num_processors == 1 || uniprocessor) { |
| 290 | CPU_ZERO(admin_mask); |
| 291 | CPU_ZERO(test_mask); |
| 292 | CPU_SET(0, admin_mask); |
| 293 | CPU_SET(0, test_mask); |
| 294 | pi_info("admin and test threads running on one processor\n"); |
| 295 | return SUCCESS; |
| 296 | } |
| 297 | /* |
| 298 | * first set our main thread to run on the first |
| 299 | * scheduleable processor we can find |
| 300 | */ |
| 301 | status = sched_getaffinity(0, sizeof(cpu_set_t), ¤t_mask); |
| 302 | if (status) { |
| 303 | pi_error("failed getting CPU affinity mask: 0x%x\n", status); |
| 304 | return FAILURE; |
| 305 | } |
| 306 | for (i = 0; i < num_processors; i++) { |
| 307 | if (CPU_ISSET(i, ¤t_mask)) |
| 308 | break; |
| 309 | } |
| 310 | if (i >= num_processors) { |
| 311 | pi_error("No schedulable CPU found for main!\n"); |
| 312 | return FAILURE; |
| 313 | } |
| 314 | admin_proc = i; |
| 315 | CPU_ZERO(admin_mask); |
| 316 | CPU_SET(admin_proc, admin_mask); |
| 317 | status = sched_setaffinity(0, sizeof(cpu_set_t), admin_mask); |
| 318 | if (status) { |
| 319 | pi_error("%s: setting CPU affinity mask: 0x%x\n", __func__, |
| 320 | status); |
| 321 | return FAILURE; |
| 322 | } |
| 323 | pi_info("Admin thread running on processor: %d\n", i); |
| 324 | |
| 325 | /* Set test affinity so that tests run on the non-admin processors */ |
| 326 | CPU_ZERO(test_mask); |
| 327 | for (i = admin_proc + 1; i < num_processors; i++) |
| 328 | CPU_SET(i, test_mask); |
| 329 | |
| 330 | if (admin_proc + 1 == num_processors - 1) |
| 331 | pi_info("Test threads running on processor: %ld\n", |
| 332 | num_processors - 1); |
| 333 | else |
| 334 | pi_info("Test threads running on processors: %d-%d\n", |
| 335 | admin_proc + 1, (int)num_processors - 1); |
| 336 | |
| 337 | return SUCCESS; |
| 338 | } |
| 339 | |
| 340 | /* clear all watchdog counters */ |
| 341 | void watchdog_clear(void) |
| 342 | { |
| 343 | int i; |
| 344 | |
| 345 | for (i = 0; i < ngroups; i++) |
| 346 | groups[i].watchdog = 0; |
| 347 | } |
| 348 | |
| 349 | /* check for zero watchdog counters */ |
| 350 | int watchdog_check(void) |
| 351 | { |
| 352 | int i; |
| 353 | int failures = 0; |
| 354 | struct group_parameters *g; |
| 355 | |
| 356 | for (i = 0; i < ngroups; i++) { |
| 357 | g = &groups[i]; |
| 358 | if (g->watchdog == 0) { |
| 359 | /* don't report deadlock if group is finished */ |
| 360 | if (g->inversions == g->total) |
| 361 | continue; |
| 362 | if (++g->watchdog_hits >= WATCHDOG_LIMIT) { |
| 363 | pi_error |
| 364 | ("WATCHDOG triggered: group %d is deadlocked!\n", |
| 365 | i); |
| 366 | failures++; |
| 367 | } |
| 368 | } else |
| 369 | g->watchdog_hits = 0; |
| 370 | } |
| 371 | return failures ? FAILURE : SUCCESS; |
| 372 | } |
| 373 | |
| 374 | int pending_interrupt(void) |
| 375 | { |
| 376 | sigset_t pending; |
| 377 | |
| 378 | if (sigpending(&pending) < 0) { |
| 379 | pi_error("from sigpending: %s\n", strerror(errno)); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | return interrupted = sigismember(&pending, SIGINT); |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * this routine serves two purposes: |
| 388 | * 1. report progress |
| 389 | * 2. check for deadlocks |
| 390 | */ |
| 391 | void *reporter(void *arg) |
| 392 | { |
| 393 | int status; |
| 394 | int end = 0; |
| 395 | struct timespec ts; |
| 396 | |
| 397 | ts.tv_sec = 0; |
| 398 | ts.tv_nsec = USEC_TO_NSEC(report_interval); |
| 399 | |
| 400 | tsnorm(&ts); |
| 401 | |
| 402 | if (duration >= 0) |
| 403 | end = duration + time(NULL); |
| 404 | |
| 405 | /* sleep initially to let everything get up and running */ |
| 406 | status = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL); |
| 407 | if (status) { |
| 408 | pi_error("from clock_nanosleep: %s\n", strerror(status)); |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | pi_debug("%s: starting report loop\n", __func__); |
| 413 | pi_info("Press Control-C to stop test\nCurrent Inversions:\n"); |
| 414 | |
| 415 | for (;;) { |
| 416 | pthread_mutex_lock(&shutdown_mtx); |
| 417 | if (shutdown) { |
| 418 | pthread_mutex_unlock(&shutdown_mtx); |
| 419 | break; |
| 420 | } |
| 421 | pthread_mutex_unlock(&shutdown_mtx); |
| 422 | |
| 423 | /* wait for our reporting interval */ |
| 424 | status = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL); |
| 425 | if (status) { |
| 426 | pi_error("from clock_nanosleep: %s\n", strerror(status)); |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | /* check for signaled shutdown */ |
| 431 | if (!quiet) { |
| 432 | pthread_mutex_lock(&shutdown_mtx); |
| 433 | if (shutdown == 0) { |
| 434 | fputs(UP_ONE, stdout); |
| 435 | printf("Current Inversions: %lu\n", |
| 436 | total_inversions()); |
| 437 | } |
| 438 | pthread_mutex_unlock(&shutdown_mtx); |
| 439 | } |
| 440 | |
| 441 | /* if we specified a duration, see if it has expired */ |
| 442 | if (end && time(NULL) > end) { |
| 443 | pi_info("duration reached (%d seconds)\n", duration); |
| 444 | set_shutdown_flag(); |
| 445 | continue; |
| 446 | } |
| 447 | /* check for a pending SIGINT */ |
| 448 | if (pending_interrupt()) { |
| 449 | pi_info("Keyboard Interrupt!\n"); |
| 450 | break; |
| 451 | } |
| 452 | /* check watchdog stuff */ |
| 453 | if ((watchdog_check())) { |
| 454 | pi_error("%s stopping due to watchdog event\n", __func__); |
| 455 | set_shutdown_flag(); |
| 456 | break; |
| 457 | } |
| 458 | /* clear watchdog counters */ |
| 459 | watchdog_clear(); |
| 460 | |
| 461 | } |
| 462 | pi_debug("%s: finished\n", __func__); |
| 463 | set_shutdown_flag(); |
| 464 | return NULL; |
| 465 | } |
| 466 | |
| 467 | int verify_cpu(int cpu) |
| 468 | { |
| 469 | int status; |
| 470 | int err; |
| 471 | cpu_set_t mask; |
| 472 | |
| 473 | CPU_ZERO(&mask); |
| 474 | |
| 475 | status = sched_getaffinity(0, sizeof(cpu_set_t), &mask); |
| 476 | if (status == -1) { |
| 477 | err = errno; |
| 478 | fprintf(stderr, "sched_getaffinity %s\n", strerror(err)); |
| 479 | exit(1); |
| 480 | } |
| 481 | |
| 482 | if (CPU_ISSET(cpu, &mask)) |
| 483 | return SUCCESS; |
| 484 | return FAILURE; |
| 485 | } |
| 486 | |
| 487 | void *low_priority(void *arg) |
| 488 | { |
| 489 | int status; |
| 490 | int unbounded; |
| 491 | unsigned long count = 0; |
| 492 | struct group_parameters *p = (struct group_parameters *)arg; |
| 493 | pthread_barrier_t *loop_barr = &p->loop_barr; |
| 494 | pthread_mutex_t *loop_mtx = &p->loop_mtx; |
| 495 | int *loop = &p->loop; |
| 496 | |
| 497 | allow_sigterm(); |
| 498 | |
| 499 | if (verify_cpu(p->cpu) != SUCCESS) { |
| 500 | pi_error("low_priority[%d]: not bound to %ld\n", p->id, p->cpu); |
| 501 | return NULL; |
| 502 | } |
| 503 | |
| 504 | pi_debug("low_priority[%d]: entering ready state\n", p->id); |
| 505 | /* wait for all threads to be ready */ |
| 506 | status = pthread_barrier_wait(&all_threads_ready); |
| 507 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 508 | pi_error |
| 509 | ("low_priority[%d]: pthread_barrier_wait(all_threads_ready): %x", |
| 510 | p->id, status); |
| 511 | return NULL; |
| 512 | } |
| 513 | |
| 514 | unbounded = (p->inversions < 0); |
| 515 | |
| 516 | pi_debug("low_priority[%d]: starting inversion loop\n", p->id); |
| 517 | |
| 518 | for (;;) { |
| 519 | /* |
| 520 | * We can't set the 'loop' boolean here, because some flags |
| 521 | * may have already reached the loop_barr |
| 522 | */ |
| 523 | if (!unbounded && (p->total >= (unsigned int)p->inversions)) |
| 524 | set_shutdown_flag(); |
| 525 | |
| 526 | /* Either all threads go through the loop_barr, or none do */ |
| 527 | pthread_mutex_lock(loop_mtx); |
| 528 | if (*loop == 0) { |
| 529 | pthread_mutex_unlock(loop_mtx); |
| 530 | break; |
| 531 | } |
| 532 | pthread_mutex_unlock(loop_mtx); |
| 533 | |
| 534 | status = pthread_barrier_wait(loop_barr); |
| 535 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 536 | pi_error("%s[%d]: pthread_barrier_wait(loop): %x\n", |
| 537 | __func__, p->id, status); |
| 538 | return NULL; |
| 539 | } |
| 540 | |
| 541 | /* Only one Thread needs to check the shutdown status */ |
| 542 | if (status == PTHREAD_BARRIER_SERIAL_THREAD) { |
| 543 | pthread_mutex_lock(&shutdown_mtx); |
| 544 | if (shutdown) { |
| 545 | pthread_mutex_lock(loop_mtx); |
| 546 | *loop = 0; |
| 547 | pthread_mutex_unlock(loop_mtx); |
| 548 | } |
| 549 | pthread_mutex_unlock(&shutdown_mtx); |
| 550 | } |
| 551 | |
| 552 | /* initial state */ |
| 553 | pi_debug("low_priority[%d]: entering start wait (%ld)\n", p->id, |
| 554 | count++); |
| 555 | status = pthread_barrier_wait(&p->start_barrier); |
| 556 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 557 | pi_error |
| 558 | ("low_priority[%d]: pthread_barrier_wait(start): %x\n", |
| 559 | p->id, status); |
| 560 | return NULL; |
| 561 | } |
| 562 | |
| 563 | pi_debug("low_priority[%d]: claiming mutex\n", p->id); |
| 564 | pthread_mutex_lock(&p->mutex); |
| 565 | pi_debug("low_priority[%d]: mutex locked\n", p->id); |
| 566 | |
| 567 | pi_debug("low_priority[%d]: entering locked wait\n", p->id); |
| 568 | status = pthread_barrier_wait(&p->locked_barrier); |
| 569 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 570 | pi_error |
| 571 | ("low_priority[%d]: pthread_barrier_wait(locked): %x\n", |
| 572 | p->id, status); |
| 573 | /* release the mutex */ |
| 574 | pi_debug("low_priority[%d]: unlocking mutex\n", p->id); |
| 575 | pthread_mutex_unlock(&p->mutex); |
| 576 | return NULL; |
| 577 | } |
| 578 | |
| 579 | /* wait for priority boost */ |
| 580 | pi_debug("low_priority[%d]: entering elevated wait\n", p->id); |
| 581 | status = pthread_barrier_wait(&p->elevate_barrier); |
| 582 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 583 | pi_error |
| 584 | ("low_priority[%d]: pthread_barrier_wait(elevate): %x\n", |
| 585 | p->id, status); |
| 586 | /* release the mutex */ |
| 587 | pi_debug("low_priority[%d]: unlocking mutex\n", p->id); |
| 588 | pthread_mutex_unlock(&p->mutex); |
| 589 | return NULL; |
| 590 | } |
| 591 | |
| 592 | /* release the mutex */ |
| 593 | pi_debug("low_priority[%d]: unlocking mutex\n", p->id); |
| 594 | pthread_mutex_unlock(&p->mutex); |
| 595 | |
| 596 | /* finish state */ |
| 597 | pi_debug("low_priority[%d]: entering finish wait\n", p->id); |
| 598 | status = pthread_barrier_wait(&p->finish_barrier); |
| 599 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 600 | pi_error |
| 601 | ("low_priority[%d]: pthread_barrier_wait(elevate): %x\n", |
| 602 | p->id, status); |
| 603 | return NULL; |
| 604 | } |
| 605 | } |
| 606 | set_shutdown_flag(); |
| 607 | pi_debug("low_priority[%d]: entering done barrier\n", p->id); |
| 608 | /* wait for all threads to finish */ |
| 609 | status = pthread_barrier_wait(&all_threads_done); |
| 610 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 611 | pi_error |
| 612 | ("low_priority[%d]: pthread_barrier_wait(all_threads_done): %x", |
| 613 | p->id, status); |
| 614 | return NULL; |
| 615 | } |
| 616 | pi_debug("low_priority[%d]: exiting\n", p->id); |
| 617 | return NULL; |
| 618 | } |
| 619 | |
| 620 | void *med_priority(void *arg) |
| 621 | { |
| 622 | int status; |
| 623 | int unbounded; |
| 624 | unsigned long count = 0; |
| 625 | struct group_parameters *p = (struct group_parameters *)arg; |
| 626 | pthread_barrier_t *loop_barr = &p->loop_barr; |
| 627 | pthread_mutex_t *loop_mtx = &p->loop_mtx; |
| 628 | int *loop = &p->loop; |
| 629 | |
| 630 | allow_sigterm(); |
| 631 | |
| 632 | if (verify_cpu(p->cpu) != SUCCESS) { |
| 633 | pi_error("med_priority[%d]: not bound to %ld\n", p->id, p->cpu); |
| 634 | return NULL; |
| 635 | } |
| 636 | |
| 637 | pi_debug("med_priority[%d]: entering ready state\n", p->id); |
| 638 | /* wait for all threads to be ready */ |
| 639 | status = pthread_barrier_wait(&all_threads_ready); |
| 640 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 641 | pi_error |
| 642 | ("med_priority[%d]: pthread_barrier_wait(all_threads_ready): %x", |
| 643 | p->id, status); |
| 644 | return NULL; |
| 645 | } |
| 646 | |
| 647 | unbounded = (p->inversions < 0); |
| 648 | |
| 649 | pi_debug("med_priority[%d]: starting inversion loop\n", p->id); |
| 650 | for (;;) { |
| 651 | if (!unbounded && (p->total >= (unsigned int)p->inversions)) |
| 652 | set_shutdown_flag(); |
| 653 | |
| 654 | /* Either all threads go through the loop_barr, or none do */ |
| 655 | pthread_mutex_lock(loop_mtx); |
| 656 | if (*loop == 0) { |
| 657 | pthread_mutex_unlock(loop_mtx); |
| 658 | break; |
| 659 | } |
| 660 | pthread_mutex_unlock(loop_mtx); |
| 661 | |
| 662 | status = pthread_barrier_wait(loop_barr); |
| 663 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 664 | pi_error("%s[%d]: pthread_barrier_wait(loop): %x\n", |
| 665 | __func__, p->id, status); |
| 666 | return NULL; |
| 667 | } |
| 668 | |
| 669 | /* Only one Thread needs to check the shutdown status */ |
| 670 | if (status == PTHREAD_BARRIER_SERIAL_THREAD) { |
| 671 | pthread_mutex_lock(&shutdown_mtx); |
| 672 | if (shutdown) { |
| 673 | pthread_mutex_lock(loop_mtx); |
| 674 | *loop = 0; |
| 675 | pthread_mutex_unlock(loop_mtx); |
| 676 | } |
| 677 | pthread_mutex_unlock(&shutdown_mtx); |
| 678 | } |
| 679 | |
| 680 | /* start state */ |
| 681 | pi_debug("med_priority[%d]: entering start state (%ld)\n", p->id, |
| 682 | count++); |
| 683 | status = pthread_barrier_wait(&p->start_barrier); |
| 684 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 685 | pi_error |
| 686 | ("med_priority[%d]: pthread_barrier_wait(start): %x", |
| 687 | p->id, status); |
| 688 | return NULL; |
| 689 | } |
| 690 | pi_debug("med_priority[%d]: entering elevate state\n", p->id); |
| 691 | status = pthread_barrier_wait(&p->elevate_barrier); |
| 692 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 693 | pi_error ("med_priority[%d]: pthread_barrier_wait(elevate): %x", p->id, status); |
| 694 | return NULL; |
| 695 | } |
| 696 | |
| 697 | pi_debug("med_priority[%d]: entering finish state\n", p->id); |
| 698 | status = pthread_barrier_wait(&p->finish_barrier); |
| 699 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 700 | pi_error |
| 701 | ("med_priority[%d]: pthread_barrier_wait(finished): %x", |
| 702 | p->id, status); |
| 703 | return NULL; |
| 704 | } |
| 705 | } |
| 706 | set_shutdown_flag(); |
| 707 | |
| 708 | pi_debug("med_priority[%d]: entering done barrier\n", p->id); |
| 709 | /* wait for all threads to finish */ |
| 710 | if (have_errors == 0) { |
| 711 | status = pthread_barrier_wait(&all_threads_done); |
| 712 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 713 | pi_error |
| 714 | ("med_priority[%d]: pthread_barrier_wait(all_threads_done): %x", |
| 715 | p->id, status); |
| 716 | return NULL; |
| 717 | } |
| 718 | } |
| 719 | /* exit */ |
| 720 | pi_debug("med_priority[%d]: exiting\n", p->id); |
| 721 | return NULL; |
| 722 | } |
| 723 | |
| 724 | void *high_priority(void *arg) |
| 725 | { |
| 726 | int status; |
| 727 | int unbounded; |
| 728 | unsigned long count = 0; |
| 729 | struct group_parameters *p = (struct group_parameters *)arg; |
| 730 | pthread_barrier_t *loop_barr = &p->loop_barr; |
| 731 | pthread_mutex_t *loop_mtx = &p->loop_mtx; |
| 732 | int *loop = &p->loop; |
| 733 | cpu_set_t cpu_mask; |
| 734 | int i; |
| 735 | |
| 736 | if (high_sa.sched_policy == SCHED_DEADLINE) { |
| 737 | CPU_ZERO(&cpu_mask); |
| 738 | for (i = 0; i < num_processors; i++) |
| 739 | CPU_SET(i, &cpu_mask); |
| 740 | status = sched_setaffinity(0, sizeof(cpu_set_t), &cpu_mask); |
| 741 | if (status < 0) { |
| 742 | pi_error |
| 743 | ("high_priority[%d]: set cpu affinity*dl): %x\n", |
| 744 | p->id, status); |
| 745 | return NULL; |
| 746 | } |
| 747 | |
| 748 | status = sched_setattr(gettid(), &high_sa, 0); |
| 749 | if (status < 0) { |
| 750 | pi_error |
| 751 | ("high_priority[%d]: sched_setattr(dl): %x\n", |
| 752 | p->id, status); |
| 753 | return NULL; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | allow_sigterm(); |
| 758 | if (verify_cpu(p->cpu) != SUCCESS) { |
| 759 | pi_error("high_priority[%d]: not bound to %ld\n", p->id, p->cpu); |
| 760 | return NULL; |
| 761 | } |
| 762 | |
| 763 | pi_debug("high_priority[%d]: entering ready state\n", p->id); |
| 764 | |
| 765 | /* wait for all threads to be ready */ |
| 766 | status = pthread_barrier_wait(&all_threads_ready); |
| 767 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 768 | pi_error |
| 769 | ("high_priority[%d]: pthread_barrier_wait(all_threads_ready): %x", |
| 770 | p->id, status); |
| 771 | return NULL; |
| 772 | } |
| 773 | unbounded = (p->inversions < 0); |
| 774 | pi_debug("high_priority[%d]: starting inversion loop\n", p->id); |
| 775 | for (;;) { |
| 776 | if (!unbounded && (p->total >= (unsigned int)p->inversions)) { |
| 777 | set_shutdown_flag(); |
| 778 | } |
| 779 | |
| 780 | /* Either all threads go through the loop_barr, or none do */ |
| 781 | pthread_mutex_lock(loop_mtx); |
| 782 | if (*loop == 0) { |
| 783 | pthread_mutex_unlock(loop_mtx); |
| 784 | break; |
| 785 | } |
| 786 | pthread_mutex_unlock(loop_mtx); |
| 787 | |
| 788 | status = pthread_barrier_wait(loop_barr); |
| 789 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 790 | pi_error("%s[%d]: pthread_barrier_wait(loop): %x\n", |
| 791 | __func__, p->id, status); |
| 792 | return NULL; |
| 793 | } |
| 794 | |
| 795 | /* Only one Thread needs to check the shutdown status */ |
| 796 | if (status == PTHREAD_BARRIER_SERIAL_THREAD) { |
| 797 | pthread_mutex_lock(&shutdown_mtx); |
| 798 | if (shutdown) { |
| 799 | pthread_mutex_lock(loop_mtx); |
| 800 | *loop = 0; |
| 801 | pthread_mutex_unlock(loop_mtx); |
| 802 | } |
| 803 | pthread_mutex_unlock(&shutdown_mtx); |
| 804 | } |
| 805 | pi_debug("high_priority[%d]: entering start state (%d)\n", p->id, |
| 806 | count++); |
| 807 | status = pthread_barrier_wait(&p->start_barrier); |
| 808 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 809 | pi_error |
| 810 | ("high_priority[%d]: pthread_barrier_wait(start): %x", |
| 811 | p->id, status); |
| 812 | return NULL; |
| 813 | } |
| 814 | |
| 815 | pi_debug("high_priority[%d]: entering running state\n", p->id); |
| 816 | status = pthread_barrier_wait(&p->locked_barrier); |
| 817 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 818 | pi_error |
| 819 | ("high_priority[%d]: pthread_barrier_wait(running): %x", |
| 820 | p->id, status); |
| 821 | return NULL; |
| 822 | } |
| 823 | pi_debug("high_priority[%d]: locking mutex\n", p->id); |
| 824 | pthread_mutex_lock(&p->mutex); |
| 825 | pi_debug("high_priority[%d]: got mutex\n", p->id); |
| 826 | |
| 827 | pi_debug("high_priority[%d]: unlocking mutex\n", p->id); |
| 828 | pthread_mutex_unlock(&p->mutex); |
| 829 | pi_debug("high_priority[%d]: entering finish state\n", p->id); |
| 830 | |
| 831 | status = pthread_barrier_wait(&p->finish_barrier); |
| 832 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 833 | pi_error |
| 834 | ("high_priority[%d]: pthread_barrier_wait(finish): %x", p->id, status); |
| 835 | return NULL; |
| 836 | } |
| 837 | /* update the group stats */ |
| 838 | p->total++; |
| 839 | |
| 840 | /* update the watchdog counter */ |
| 841 | p->watchdog++; |
| 842 | |
| 843 | } |
| 844 | set_shutdown_flag(); |
| 845 | |
| 846 | pi_debug("high_priority[%d]: entering done barrier\n", p->id); |
| 847 | |
| 848 | if (have_errors == 0) { |
| 849 | /* wait for all threads to finish */ |
| 850 | status = pthread_barrier_wait(&all_threads_done); |
| 851 | if (status && status != PTHREAD_BARRIER_SERIAL_THREAD) { |
| 852 | pi_error |
| 853 | ("high_priority[%d]: pthread_barrier_wait(all_threads_done): %x", |
| 854 | p->id, status); |
| 855 | return NULL; |
| 856 | } |
| 857 | } |
| 858 | /* exit */ |
| 859 | pi_debug("high_priority[%d]: exiting\n", p->id); |
| 860 | return NULL; |
| 861 | } |
| 862 | |
| 863 | void usage(int error) |
| 864 | { |
| 865 | printf("pi_stress V %1.2f\n", VERSION); |
| 866 | printf("Usage:\n" |
| 867 | "pi_stress <options>\n\n" |
| 868 | "-d --debug turn on debug prints\n" |
| 869 | "-D TIME --duration=TIME length of test run in seconds (default is infinite)\n" |
| 870 | " Append 'm', 'h', or 'd'\n" |
| 871 | " to specify minutes, hours or days.\n" |
| 872 | "-g N --groups=N set the number of inversion groups\n" |
| 873 | "-h --help print this message\n" |
| 874 | "-i INV --inversions=INV number of inversions per group (default is infinite)\n" |
| 875 | " --json=FILENAME write final results into FILENAME, JSON formatted\n" |
| 876 | "-m --mlockall lock current and future memory\n" |
| 877 | "-p --prompt prompt before starting the test\n" |
| 878 | "-q --quiet suppress running output\n" |
| 879 | "-r --rr use SCHED_RR for test threads [SCHED_FIFO]\n" |
| 880 | "-s OPTS --sched OPTS scheduling options per thread type:\n" |
| 881 | " id=[high|med|low] select thread\n" |
| 882 | " ,policy=[fifo,rr] scheduling class [SCHED_FIFO, SCHED_RR]\n" |
| 883 | " ,priority=N scheduling priority\n" |
| 884 | " ,policy=[deadline] scheduling class [DEADLINE]\n" |
| 885 | " ,runtime=N\n" |
| 886 | " ,deadline=N\n" |
| 887 | " ,period=N\n" |
| 888 | "-u --uniprocessor force all threads to run on one processor\n" |
| 889 | "-v --verbose lots of output\n" |
| 890 | "-V --version print version number on output\n" |
| 891 | ); |
| 892 | exit(error); |
| 893 | } |
| 894 | |
| 895 | /* block all signals (called from main) */ |
| 896 | int block_signals(void) |
| 897 | { |
| 898 | int status; |
| 899 | sigset_t sigset; |
| 900 | |
| 901 | /* mask off all signals */ |
| 902 | status = sigfillset(&sigset); |
| 903 | if (status) { |
| 904 | pi_error("setting up full signal set %s\n", strerror(status)); |
| 905 | return FAILURE; |
| 906 | } |
| 907 | status = pthread_sigmask(SIG_BLOCK, &sigset, NULL); |
| 908 | if (status) { |
| 909 | pi_error("setting signal mask: %s\n", strerror(status)); |
| 910 | return FAILURE; |
| 911 | } |
| 912 | return SUCCESS; |
| 913 | } |
| 914 | |
| 915 | /* allow SIGTERM delivery (called from worker threads) */ |
| 916 | int allow_sigterm(void) |
| 917 | { |
| 918 | int status; |
| 919 | sigset_t sigset; |
| 920 | |
| 921 | status = sigemptyset(&sigset); |
| 922 | if (status) { |
| 923 | pi_error("creating empty signal set: %s\n", strerror(status)); |
| 924 | return FAILURE; |
| 925 | } |
| 926 | status = sigaddset(&sigset, SIGTERM); |
| 927 | if (status) { |
| 928 | pi_error("adding SIGTERM to signal set: %s\n", strerror(status)); |
| 929 | return FAILURE; |
| 930 | } |
| 931 | status = pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); |
| 932 | if (status) { |
| 933 | pi_error("unblocking SIGTERM: %s\n", strerror(status)); |
| 934 | return FAILURE; |
| 935 | } |
| 936 | return SUCCESS; |
| 937 | } |
| 938 | |
| 939 | /* clean up before exiting */ |
| 940 | void set_shutdown_flag(void) |
| 941 | { |
| 942 | pthread_mutex_lock(&shutdown_mtx); |
| 943 | if (shutdown == 0) { |
| 944 | /* tell anyone that's looking that we're done */ |
| 945 | pi_info("setting shutdown flag\n"); |
| 946 | shutdown = 1; |
| 947 | } |
| 948 | pthread_mutex_unlock(&shutdown_mtx); |
| 949 | } |
| 950 | |
| 951 | /* set up a test group */ |
| 952 | int initialize_group(struct group_parameters *group) |
| 953 | { |
| 954 | int status; |
| 955 | pthread_mutexattr_t mutex_attr; |
| 956 | |
| 957 | group->inversions = inversions; |
| 958 | |
| 959 | /* setup default attributes for the group mutex */ |
| 960 | /* (make it a PI mutex) */ |
| 961 | status = pthread_mutexattr_init(&mutex_attr); |
| 962 | if (status) { |
| 963 | pi_error("initializing mutex attribute: %s\n", strerror(status)); |
| 964 | return FAILURE; |
| 965 | } |
| 966 | |
| 967 | /* set priority inheritance attribute for mutex */ |
| 968 | status = pthread_mutexattr_setprotocol(&mutex_attr, |
| 969 | PTHREAD_PRIO_INHERIT); |
| 970 | if (status) { |
| 971 | pi_error("setting mutex attribute policy: %s\n", strerror(status)); |
| 972 | return FAILURE; |
| 973 | } |
| 974 | /* initialize the group mutex */ |
| 975 | status = pthread_mutex_init(&group->mutex, &mutex_attr); |
| 976 | if (status) { |
| 977 | pi_error("initializing mutex: %s\n", strerror(status)); |
| 978 | return FAILURE; |
| 979 | } |
| 980 | |
| 981 | /* initialize the group barriers */ |
| 982 | if (barrier_init(&group->start_barrier, NULL, NUM_TEST_THREADS, |
| 983 | "start_barrier")) |
| 984 | return FAILURE; |
| 985 | |
| 986 | if (barrier_init(&group->locked_barrier, NULL, 2, "locked_barrier")) |
| 987 | return FAILURE; |
| 988 | |
| 989 | if (barrier_init(&group->elevate_barrier, NULL, 2, "elevate_barrier")) |
| 990 | return FAILURE; |
| 991 | |
| 992 | if (barrier_init |
| 993 | (&group->finish_barrier, NULL, NUM_TEST_THREADS, "finish_barrier")) |
| 994 | return FAILURE; |
| 995 | |
| 996 | if (barrier_init(&group->loop_barr, NULL, NUM_TEST_THREADS, |
| 997 | "loop_barrier")) |
| 998 | return FAILURE; |
| 999 | |
| 1000 | status = pthread_mutex_init(&group->loop_mtx, NULL); |
| 1001 | if (status) { |
| 1002 | pi_error("pthread_mutex_init, status = %d\n", status); |
| 1003 | return FAILURE; |
| 1004 | } |
| 1005 | |
| 1006 | status = pthread_mutex_lock(&group->loop_mtx); |
| 1007 | if (status) { |
| 1008 | pi_error("pthread_mutex_lock, status = %d\n", status); |
| 1009 | return FAILURE; |
| 1010 | } |
| 1011 | |
| 1012 | group->loop = 1; |
| 1013 | |
| 1014 | status = pthread_mutex_unlock(&group->loop_mtx); |
| 1015 | if (status) { |
| 1016 | pi_error("pthread_mutex_unlock, status = %d\n", status); |
| 1017 | return FAILURE; |
| 1018 | } |
| 1019 | |
| 1020 | return SUCCESS; |
| 1021 | } |
| 1022 | |
| 1023 | /* setup and create a groups threads */ |
| 1024 | int create_group(struct group_parameters *group) |
| 1025 | { |
| 1026 | int status; |
| 1027 | pthread_attr_t thread_attr; |
| 1028 | cpu_set_t mask; |
| 1029 | |
| 1030 | /* initialize group structure */ |
| 1031 | status = initialize_group(group); |
| 1032 | if (status) { |
| 1033 | pi_error("initializing group %d\n", group->id); |
| 1034 | return FAILURE; |
| 1035 | } |
| 1036 | |
| 1037 | CPU_ZERO(&mask); |
| 1038 | CPU_SET(group->cpu, &mask); |
| 1039 | |
| 1040 | pi_debug("group %d bound to cpu %ld\n", group->id, group->cpu); |
| 1041 | |
| 1042 | /* start the low priority thread */ |
| 1043 | pi_debug("creating low priority thread\n"); |
| 1044 | if (setup_thread_attr(&thread_attr, &low_sa, &mask)) |
| 1045 | return FAILURE; |
| 1046 | status = pthread_create(&group->low_tid, |
| 1047 | &thread_attr, low_priority, group); |
| 1048 | if (status != 0) { |
| 1049 | pi_error("creating low_priority thread: %s\n", strerror(status)); |
| 1050 | return FAILURE; |
| 1051 | } |
| 1052 | |
| 1053 | /* create the medium priority thread */ |
| 1054 | pi_debug("creating medium priority thread\n"); |
| 1055 | if (setup_thread_attr(&thread_attr, &med_sa, &mask)) |
| 1056 | return FAILURE; |
| 1057 | status = pthread_create(&group->med_tid, |
| 1058 | &thread_attr, med_priority, group); |
| 1059 | if (status != 0) { |
| 1060 | pi_error("creating med_priority thread: %s\n", strerror(status)); |
| 1061 | return FAILURE; |
| 1062 | } |
| 1063 | |
| 1064 | /* create the high priority thread */ |
| 1065 | pi_debug("creating high priority thread\n"); |
| 1066 | if (setup_thread_attr(&thread_attr, &high_sa, &mask)) |
| 1067 | return FAILURE; |
| 1068 | status = pthread_create(&group->high_tid, |
| 1069 | &thread_attr, high_priority, group); |
| 1070 | if (status != 0) { |
| 1071 | pi_error("creating high_priority thread: %s\n", strerror(status)); |
| 1072 | set_shutdown_flag(); |
| 1073 | return FAILURE; |
| 1074 | } |
| 1075 | return SUCCESS; |
| 1076 | } |
| 1077 | |
| 1078 | unsigned long parse_unsigned(const char *str) |
| 1079 | { |
| 1080 | unsigned long n; |
| 1081 | char *p; |
| 1082 | |
| 1083 | errno = 0; |
| 1084 | n = strtoul(str, &p, 10); |
| 1085 | |
| 1086 | if ((errno == ERANGE && n == ULONG_MAX) |
| 1087 | || (errno != 0 && n == 0)) { |
| 1088 | pi_error("parsing number failed: %s\n", str); |
| 1089 | exit(EXIT_FAILURE); |
| 1090 | } |
| 1091 | |
| 1092 | return n; |
| 1093 | } |
| 1094 | |
| 1095 | long parse_signed(const char *str) |
| 1096 | { |
| 1097 | long n; |
| 1098 | char *p; |
| 1099 | |
| 1100 | errno = 0; |
| 1101 | n = strtol(str, &p, 10); |
| 1102 | |
| 1103 | if ((errno == ERANGE && (n == LONG_MAX || n == LONG_MIN)) |
| 1104 | || (errno != 0 && n == 0)) { |
| 1105 | pi_error("parsing number failed: %s\n", str); |
| 1106 | exit(EXIT_FAILURE); |
| 1107 | } |
| 1108 | |
| 1109 | return n; |
| 1110 | } |
| 1111 | |
| 1112 | int process_sched_line(const char *arg) |
| 1113 | { |
| 1114 | char *buf, *k, *v; |
| 1115 | const char del[] = ",="; |
| 1116 | struct sched_attr sa = { 0, }; |
| 1117 | char *id = NULL; |
| 1118 | int retval = SUCCESS; |
| 1119 | |
| 1120 | buf = strdupa(arg); |
| 1121 | |
| 1122 | k = strsep(&buf, del); |
| 1123 | while (k) { |
| 1124 | v = strsep(&buf, del); |
| 1125 | if (!v) |
| 1126 | break; |
| 1127 | |
| 1128 | if (!strcmp(k, "id")) |
| 1129 | id = v; |
| 1130 | else if (!strcmp(k, "policy")) |
| 1131 | sa.sched_policy = string_to_policy(v); |
| 1132 | else if (!strcmp(k, "nice")) |
| 1133 | sa.sched_nice = parse_signed(v); |
| 1134 | else if (!strcmp(k, "priority")) |
| 1135 | sa.sched_priority = parse_unsigned(v); |
| 1136 | else if (!strcmp(k, "runtime")) |
| 1137 | sa.sched_runtime = parse_unsigned(v); |
| 1138 | else if (!strcmp(k, "deadline")) |
| 1139 | sa.sched_deadline = parse_unsigned(v); |
| 1140 | else if (!strcmp(k, "period")) |
| 1141 | sa.sched_period = parse_unsigned(v); |
| 1142 | |
| 1143 | k = strsep(&buf, del); |
| 1144 | } |
| 1145 | |
| 1146 | if (!id) |
| 1147 | return FAILURE; |
| 1148 | |
| 1149 | /* |
| 1150 | * We do not validate the options, instead we pass all garbage |
| 1151 | * to the kernel and see what's happening |
| 1152 | */ |
| 1153 | |
| 1154 | if (!strcmp(id, "low")) { |
| 1155 | memcpy(&low_sa, &sa, sizeof(struct sched_attr)); |
| 1156 | sa_initialized |= SA_INIT_LOW; |
| 1157 | } else if (!strcmp(id, "med")) { |
| 1158 | memcpy(&med_sa, &sa, sizeof(struct sched_attr)); |
| 1159 | sa_initialized |= SA_INIT_MED; |
| 1160 | } else if (!strcmp(id, "high")) { |
| 1161 | memcpy(&high_sa, &sa, sizeof(struct sched_attr)); |
| 1162 | sa_initialized |= SA_INIT_HIGH; |
| 1163 | } else { |
| 1164 | retval = FAILURE; |
| 1165 | } |
| 1166 | |
| 1167 | return retval; |
| 1168 | } |
| 1169 | |
| 1170 | enum option_values { |
| 1171 | OPT_DEBUG=1, OPT_DURATION, OPT_GROUPS, OPT_HELP, OPT_INVERSIONS, |
| 1172 | OPT_JSON, OPT_MLOCKALL, OPT_PROMPT, OPT_QUIET, OPT_RR, OPT_SCHED, |
| 1173 | OPT_UNIPROCESSOR, OPT_VERBOSE, OPT_VERSION, |
| 1174 | }; |
| 1175 | |
| 1176 | void process_command_line(int argc, char **argv) |
| 1177 | { |
| 1178 | for (;;) { |
| 1179 | struct option options[] = { |
| 1180 | {"debug", no_argument, NULL, OPT_DEBUG}, |
| 1181 | {"duration", required_argument, NULL, OPT_DURATION}, |
| 1182 | {"groups", required_argument, NULL, OPT_GROUPS}, |
| 1183 | {"help", no_argument, NULL, OPT_HELP}, |
| 1184 | {"inversions", required_argument, NULL, OPT_INVERSIONS}, |
| 1185 | {"json", required_argument, NULL, OPT_JSON}, |
| 1186 | {"mlockall", no_argument, NULL, OPT_MLOCKALL}, |
| 1187 | {"prompt", no_argument, NULL, OPT_PROMPT}, |
| 1188 | {"quiet", no_argument, NULL, OPT_QUIET}, |
| 1189 | {"rr", no_argument, NULL, OPT_RR}, |
| 1190 | {"sched", required_argument, NULL, OPT_SCHED}, |
| 1191 | {"uniprocessor", no_argument, NULL, OPT_UNIPROCESSOR}, |
| 1192 | {"verbose", no_argument, NULL, OPT_VERBOSE}, |
| 1193 | {"version", no_argument, NULL, OPT_VERSION}, |
| 1194 | {NULL, 0, NULL, 0}, |
| 1195 | }; |
| 1196 | |
| 1197 | int c = getopt_long(argc, argv, "+hD:vqi:g:rs:pdVum", options, NULL); |
| 1198 | if (c == -1) |
| 1199 | break; |
| 1200 | switch (c) { |
| 1201 | case OPT_DEBUG: |
| 1202 | case 'd': |
| 1203 | debugging = 1; |
| 1204 | break; |
| 1205 | case OPT_DURATION: |
| 1206 | case 'D': |
| 1207 | duration = parse_time_string(optarg); |
| 1208 | break; |
| 1209 | case OPT_GROUPS: |
| 1210 | case 'g': |
| 1211 | ngroups = strtol(optarg, NULL, 10); |
| 1212 | if (ngroups > num_processors) { |
| 1213 | pi_error("the number of groups cannot exceed the number of online processors (%ld)\n", num_processors); |
| 1214 | exit(-1); |
| 1215 | } |
| 1216 | pi_info("number of groups set to %d\n", ngroups); |
| 1217 | break; |
| 1218 | case OPT_HELP: |
| 1219 | case 'h': |
| 1220 | usage(0); |
| 1221 | break; |
| 1222 | case OPT_INVERSIONS: |
| 1223 | case 'i': |
| 1224 | inversions = strtol(optarg, NULL, 10); |
| 1225 | pi_info("doing %d inversion per group\n", inversions); |
| 1226 | break; |
| 1227 | case OPT_JSON: |
| 1228 | strncpy(jsonfile, optarg, strnlen(optarg, MAX_PATH-1)); |
| 1229 | break; |
| 1230 | case OPT_MLOCKALL: |
| 1231 | case 'm': |
| 1232 | lockall = 1; |
| 1233 | break; |
| 1234 | case OPT_PROMPT: |
| 1235 | case 'p': |
| 1236 | prompt = 1; |
| 1237 | break; |
| 1238 | case OPT_QUIET: |
| 1239 | case 'q': |
| 1240 | verbose = 0; |
| 1241 | quiet = 1; |
| 1242 | break; |
| 1243 | case OPT_RR: |
| 1244 | case 'r': |
| 1245 | policy = SCHED_RR; |
| 1246 | break; |
| 1247 | case OPT_SCHED: |
| 1248 | case 's': |
| 1249 | if (process_sched_line(optarg)) |
| 1250 | pi_error("ignoring invalid options '%s'\n", optarg); |
| 1251 | break; |
| 1252 | case OPT_UNIPROCESSOR: |
| 1253 | case 'u': |
| 1254 | uniprocessor = 1; |
| 1255 | break; |
| 1256 | case OPT_VERBOSE: |
| 1257 | case 'v': |
| 1258 | verbose = 1; |
| 1259 | quiet = 0; |
| 1260 | break; |
| 1261 | case OPT_VERSION: |
| 1262 | case 'V': |
| 1263 | printf("pi_stress v%1.2f ", VERSION); |
| 1264 | exit(0); |
| 1265 | default: |
| 1266 | usage(1); |
| 1267 | break; |
| 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | /* total the number of inversions that have been performed */ |
| 1273 | unsigned long total_inversions(void) |
| 1274 | { |
| 1275 | int i; |
| 1276 | unsigned long total = 0; |
| 1277 | |
| 1278 | for (i = 0; i < ngroups; i++) |
| 1279 | total += groups[i].total; |
| 1280 | return total; |
| 1281 | } |
| 1282 | |
| 1283 | void print_sched_attr(const char *name, struct sched_attr *sa) |
| 1284 | { |
| 1285 | printf(" %6s thread", name); |
| 1286 | printf(" %s", policy_to_string(sa->sched_policy)); |
| 1287 | |
| 1288 | switch (sa->sched_policy) { |
| 1289 | case SCHED_OTHER: |
| 1290 | printf(" nice %d\n", sa->sched_nice); |
| 1291 | break; |
| 1292 | case SCHED_FIFO: |
| 1293 | case SCHED_RR: |
| 1294 | printf(" priority %d\n", sa->sched_priority); |
| 1295 | break; |
| 1296 | case SCHED_DEADLINE: |
| 1297 | printf(" runtime %llu deadline %llu period %llu\n", |
| 1298 | sa->sched_runtime, sa->sched_deadline, |
| 1299 | sa->sched_period); |
| 1300 | break; |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | void banner(void) |
| 1305 | { |
| 1306 | if (quiet) |
| 1307 | return; |
| 1308 | |
| 1309 | printf("Starting PI Stress Test\n"); |
| 1310 | printf("Number of thread groups: %d\n", ngroups); |
| 1311 | if (duration >= 0) |
| 1312 | printf("Duration of test run: %d seconds\n", duration); |
| 1313 | else |
| 1314 | printf("Duration of test run: infinite\n"); |
| 1315 | if (inversions < 0) |
| 1316 | printf("Number of inversions per group: unlimited\n"); |
| 1317 | else |
| 1318 | printf("Number of inversions per group: %d\n", inversions); |
| 1319 | print_sched_attr("Admin", &admin_sa); |
| 1320 | printf("%d groups of 3 threads will be created\n", ngroups); |
| 1321 | print_sched_attr("High", &high_sa); |
| 1322 | print_sched_attr("Med", &med_sa); |
| 1323 | print_sched_attr("Low", &low_sa); |
| 1324 | printf("\n"); |
| 1325 | } |
| 1326 | |
| 1327 | void summary(void) |
| 1328 | { |
| 1329 | time_t interval = finish - start; |
| 1330 | struct tm *t = gmtime(&interval); |
| 1331 | |
| 1332 | printf("Total inversion performed: %lu\n", total_inversions()); |
| 1333 | printf("Test Duration: %d days, %d hours, %d minutes, %d seconds\n", |
| 1334 | t->tm_yday, t->tm_hour, t->tm_min, t->tm_sec); |
| 1335 | } |
| 1336 | |
| 1337 | void write_stats(FILE *f, void *data) |
| 1338 | { |
| 1339 | fprintf(f, " \"inversion\": %lu\n", total_inversions()); |
| 1340 | } |
| 1341 | |
| 1342 | int |
| 1343 | barrier_init(pthread_barrier_t *b, const pthread_barrierattr_t *attr, |
| 1344 | unsigned int count, const char *name) |
| 1345 | { |
| 1346 | int status; |
| 1347 | |
| 1348 | status = pthread_barrier_init(b, attr, count); |
| 1349 | if (status != 0) { |
| 1350 | pi_error("barrier_init: failed to initialize: %s\n", name); |
| 1351 | pi_error("status = %d\n", status); |
| 1352 | return FAILURE; |
| 1353 | } |
| 1354 | |
| 1355 | return SUCCESS; |
| 1356 | } |
| 1357 | |
| 1358 | void setup_sched_attr(struct sched_attr *attr, int policy, int prio) |
| 1359 | { |
| 1360 | attr->sched_policy = policy; |
| 1361 | attr->sched_priority = prio; |
| 1362 | } |
| 1363 | |
| 1364 | void setup_sched_config(int policy) |
| 1365 | { |
| 1366 | int prio_min; |
| 1367 | |
| 1368 | prio_min = sched_get_priority_min(policy); |
| 1369 | |
| 1370 | if (!(sa_initialized & SA_INIT_LOW)) |
| 1371 | setup_sched_attr(&low_sa, policy, prio_min + 0); |
| 1372 | if (!(sa_initialized & SA_INIT_MED)) |
| 1373 | setup_sched_attr(&med_sa, policy, prio_min + 1); |
| 1374 | if (!(sa_initialized & SA_INIT_HIGH)) |
| 1375 | setup_sched_attr(&high_sa, policy, prio_min + 2); |
| 1376 | if (!(sa_initialized & SA_INIT_ADMIN)) |
| 1377 | setup_sched_attr(&admin_sa, policy, prio_min + 3); |
| 1378 | } |