| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * rt-migrate-test.c |
| 4 | * Copyright (C) 2007-2009 Steven Rostedt <[email protected]> |
| 5 | */ |
| 6 | #include <stdio.h> |
| 7 | #ifndef __USE_XOPEN2K |
| 8 | # define __USE_XOPEN2K |
| 9 | #endif |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <getopt.h> |
| 13 | #include <stdarg.h> |
| 14 | #include <unistd.h> |
| 15 | #include <ctype.h> |
| 16 | #include <time.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/stat.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <signal.h> |
| 21 | #include <sys/time.h> |
| 22 | #include <sys/syscall.h> |
| 23 | #include <errno.h> |
| 24 | #include <sched.h> |
| 25 | #include <pthread.h> |
| 26 | #include <inttypes.h> |
| 27 | |
| 28 | #include <linux/unistd.h> |
| 29 | |
| 30 | #include "rt-utils.h" |
| 31 | |
| 32 | int nr_tasks; |
| 33 | int lfd; |
| 34 | |
| 35 | static int mark_fd = -1; |
| 36 | static __thread char buff[BUFSIZ+1]; |
| 37 | |
| 38 | static void setup_ftrace_marker(void) |
| 39 | { |
| 40 | struct stat st; |
| 41 | char *files[] = { |
| 42 | "/sys/kernel/tracing/trace_marker", |
| 43 | }; |
| 44 | int ret; |
| 45 | unsigned int i; |
| 46 | |
| 47 | for (i = 0; i < (sizeof(files) / sizeof(char *)); i++) { |
| 48 | ret = stat(files[i], &st); |
| 49 | if (ret >= 0) |
| 50 | goto found; |
| 51 | } |
| 52 | /* todo, check mounts system */ |
| 53 | return; |
| 54 | found: |
| 55 | mark_fd = open(files[i], O_WRONLY); |
| 56 | } |
| 57 | |
| 58 | static void ftrace_write(const char *fmt, ...) |
| 59 | { |
| 60 | va_list ap; |
| 61 | int n; |
| 62 | int ret; |
| 63 | |
| 64 | if (mark_fd < 0) |
| 65 | return; |
| 66 | |
| 67 | va_start(ap, fmt); |
| 68 | n = vsnprintf(buff, BUFSIZ, fmt, ap); |
| 69 | va_end(ap); |
| 70 | |
| 71 | ret = write(mark_fd, buff, n); |
| 72 | if (ret < 0) |
| 73 | fprintf(stderr, "%s: write failed\n", __func__); |
| 74 | } |
| 75 | |
| 76 | #define nano2sec(nan) (nan / 1000000000ULL) |
| 77 | #define nano2ms(nan) (nan / 1000000ULL) |
| 78 | #define nano2usec(nan) (nan / 1000ULL) |
| 79 | #define usec2nano(sec) (sec * 1000ULL) |
| 80 | #define ms2nano(ms) (ms * 1000000ULL) |
| 81 | #define sec2nano(sec) (sec * 1000000000ULL) |
| 82 | #define INTERVAL ms2nano(100ULL) |
| 83 | #define RUN_INTERVAL ms2nano(20ULL) |
| 84 | #define NR_RUNS 50 |
| 85 | #define PRIO_START 2 |
| 86 | /* 1 millisec off */ |
| 87 | #define MAX_ERR usec2nano(1000) |
| 88 | |
| 89 | #define PROGRESS_CHARS 70 |
| 90 | |
| 91 | static unsigned long long interval = INTERVAL; |
| 92 | static unsigned long long run_interval = RUN_INTERVAL; |
| 93 | static unsigned long long max_err = MAX_ERR; |
| 94 | static int nr_runs = NR_RUNS; |
| 95 | static int prio_start = PRIO_START; |
| 96 | static int check; |
| 97 | static int stop; |
| 98 | static int equal; |
| 99 | |
| 100 | static unsigned long long now; |
| 101 | |
| 102 | static int done; |
| 103 | static int loop; |
| 104 | static int duration; |
| 105 | static int quiet; |
| 106 | static char jsonfile[MAX_PATH]; |
| 107 | |
| 108 | static pthread_barrier_t start_barrier; |
| 109 | static pthread_barrier_t end_barrier; |
| 110 | static unsigned long long **intervals; |
| 111 | static unsigned long long **intervals_length; |
| 112 | static unsigned long **intervals_loops; |
| 113 | static long *thread_pids; |
| 114 | |
| 115 | static char buffer[BUFSIZ]; |
| 116 | |
| 117 | static void perr(char *fmt, ...) |
| 118 | { |
| 119 | va_list ap; |
| 120 | |
| 121 | va_start(ap, fmt); |
| 122 | vsnprintf(buffer, BUFSIZ, fmt, ap); |
| 123 | va_end(ap); |
| 124 | |
| 125 | perror(buffer); |
| 126 | fflush(stderr); |
| 127 | exit(-1); |
| 128 | } |
| 129 | |
| 130 | static void print_progress_bar(int percent) |
| 131 | { |
| 132 | int i; |
| 133 | int p; |
| 134 | |
| 135 | if (percent > 100) |
| 136 | percent = 100; |
| 137 | |
| 138 | /* Use stderr, so we don't capture it */ |
| 139 | putc('\r', stderr); |
| 140 | putc('|', stderr); |
| 141 | for (i=0; i < PROGRESS_CHARS; i++) |
| 142 | putc(' ', stderr); |
| 143 | putc('|', stderr); |
| 144 | putc('\r', stderr); |
| 145 | putc('|', stderr); |
| 146 | |
| 147 | p = PROGRESS_CHARS * percent / 100; |
| 148 | |
| 149 | for (i=0; i < p; i++) |
| 150 | putc('-', stderr); |
| 151 | |
| 152 | fflush(stderr); |
| 153 | } |
| 154 | |
| 155 | static void usage(int error) |
| 156 | { |
| 157 | printf("rt-migrate-test %1.2f\n", VERSION); |
| 158 | printf("Usage:\n" |
| 159 | "rt-migrate-test <options> [NR_TASKS]\n\n" |
| 160 | "-c --check Stop if lower prio task is quicker than higher (off)\n" |
| 161 | "-D TIME --duration=TIME Specify a length for the test run.\n" |
| 162 | " Append 'm', 'h', or 'd' to specify minutes, hours or\n" |
| 163 | " days.\n" |
| 164 | "-e --equal Use equal prio for #CPU-1 tasks (requires > 2 CPUS)\n" |
| 165 | "-h --help Print this help message\n" |
| 166 | " --json=FILENAME write final results into FILENAME, JSON formatted\n" |
| 167 | "-l LOOPS --loops=LOOPS Number of iterations to run (50)\n" |
| 168 | "-m TIME --maxerr=TIME Max allowed error (microsecs)\n" |
| 169 | "-p PRIO --prio=PRIO base priority to start RT tasks with (2)\n" |
| 170 | "-q --quiet print a summary only on exit\n" |
| 171 | "-r TIME --run-time=TIME Run time (ms) to busy loop the threads (20)\n" |
| 172 | "-s TIME --sleep-time=TIME Sleep time (ms) between intervals (100)\n\n" |
| 173 | " () above are defaults \n" |
| 174 | ); |
| 175 | exit(error); |
| 176 | } |
| 177 | |
| 178 | enum option_value { |
| 179 | OPT_CHECK=1, OPT_DURATION, OPT_EQUAL, OPT_HELP, OPT_JSON, |
| 180 | OPT_LOOPS, OPT_MAXERR, OPT_PRIO, OPT_QUIET, OPT_RUN_TIME, |
| 181 | OPT_SLEEP_TIME |
| 182 | }; |
| 183 | |
| 184 | static void parse_options(int argc, char *argv[]) |
| 185 | { |
| 186 | for (;;) { |
| 187 | int option_index = 0; |
| 188 | /** Options for getopt */ |
| 189 | static struct option long_options[] = { |
| 190 | {"check", no_argument, NULL, OPT_CHECK}, |
| 191 | {"duration", required_argument, NULL, OPT_DURATION}, |
| 192 | {"equal", no_argument, NULL, OPT_EQUAL}, |
| 193 | {"help", no_argument, NULL, OPT_HELP}, |
| 194 | {"json", required_argument, NULL, OPT_JSON}, |
| 195 | {"loops", required_argument, NULL, OPT_LOOPS}, |
| 196 | {"maxerr", required_argument, NULL, OPT_MAXERR}, |
| 197 | {"prio", required_argument, NULL, OPT_PRIO}, |
| 198 | {"quiet", no_argument, NULL, OPT_QUIET}, |
| 199 | {"run-time", required_argument, NULL, OPT_RUN_TIME}, |
| 200 | {"sleep-time", required_argument, NULL, OPT_SLEEP_TIME}, |
| 201 | {NULL, 0, NULL, 0} |
| 202 | }; |
| 203 | int c = getopt_long(argc, argv, "cD:ehl:m:p:qr:s:", |
| 204 | long_options, &option_index); |
| 205 | if (c == -1) |
| 206 | break; |
| 207 | switch (c) { |
| 208 | case OPT_CHECK: |
| 209 | case 'c': |
| 210 | check = 1; |
| 211 | break; |
| 212 | case OPT_DURATION: |
| 213 | case 'D': |
| 214 | duration = parse_time_string(optarg); |
| 215 | break; |
| 216 | case OPT_EQUAL: |
| 217 | case 'e': |
| 218 | equal = 1; |
| 219 | break; |
| 220 | case OPT_HELP: |
| 221 | case '?': |
| 222 | case 'h': |
| 223 | usage(0); |
| 224 | break; |
| 225 | case OPT_JSON: |
| 226 | strncpy(jsonfile, optarg, strnlen(optarg, MAX_PATH-1)); |
| 227 | break; |
| 228 | case OPT_LOOPS: |
| 229 | case 'l': |
| 230 | nr_runs = atoi(optarg); |
| 231 | break; |
| 232 | case OPT_MAXERR: |
| 233 | case 'm': |
| 234 | max_err = usec2nano(atoi(optarg)); |
| 235 | break; |
| 236 | case OPT_PRIO: |
| 237 | case 'p': |
| 238 | prio_start = atoi(optarg); |
| 239 | break; |
| 240 | case OPT_QUIET: |
| 241 | case 'q': |
| 242 | quiet = 1; |
| 243 | break; |
| 244 | case OPT_RUN_TIME: |
| 245 | case 'r': |
| 246 | run_interval = atoi(optarg); |
| 247 | break; |
| 248 | case OPT_SLEEP_TIME: |
| 249 | case 's': |
| 250 | interval = atoi(optarg); |
| 251 | break; |
| 252 | default: |
| 253 | usage(1); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | static unsigned long long get_time(void) |
| 259 | { |
| 260 | struct timeval tv; |
| 261 | unsigned long long time; |
| 262 | |
| 263 | gettimeofday(&tv, NULL); |
| 264 | |
| 265 | time = sec2nano(tv.tv_sec); |
| 266 | time += usec2nano(tv.tv_usec); |
| 267 | |
| 268 | return time; |
| 269 | } |
| 270 | |
| 271 | static void record_time(int id, unsigned long long time, unsigned long l) |
| 272 | { |
| 273 | unsigned long long ltime; |
| 274 | |
| 275 | if (loop >= nr_runs) |
| 276 | return; |
| 277 | time -= now; |
| 278 | ltime = get_time(); |
| 279 | ltime -= now; |
| 280 | intervals[loop][id] = time; |
| 281 | intervals_length[loop][id] = ltime; |
| 282 | intervals_loops[loop][id] = l; |
| 283 | } |
| 284 | |
| 285 | static int calc_prio(int id) |
| 286 | { |
| 287 | int prio = equal && id && (id < nr_tasks - 1) ? 1 : id; |
| 288 | return prio + prio_start; |
| 289 | } |
| 290 | |
| 291 | static void print_results(void) |
| 292 | { |
| 293 | int i; |
| 294 | int t; |
| 295 | unsigned long long tasks_max[nr_tasks]; |
| 296 | unsigned long long tasks_min[nr_tasks]; |
| 297 | unsigned long long tasks_avg[nr_tasks]; |
| 298 | |
| 299 | memset(tasks_max, 0, sizeof(tasks_max[0])*nr_tasks); |
| 300 | memset(tasks_min, 0xff, sizeof(tasks_min[0])*nr_tasks); |
| 301 | memset(tasks_avg, 0, sizeof(tasks_avg[0])*nr_tasks); |
| 302 | |
| 303 | printf("Iter: "); |
| 304 | for (t=0; t < nr_tasks; t++) |
| 305 | printf("%6d ", t); |
| 306 | printf("\n"); |
| 307 | |
| 308 | for (i=0; i < nr_runs; i++) { |
| 309 | printf("%4d: ", i); |
| 310 | for (t=0; t < nr_tasks; t++) { |
| 311 | unsigned long long itv = intervals[i][t]; |
| 312 | |
| 313 | if (tasks_max[t] < itv) |
| 314 | tasks_max[t] = itv; |
| 315 | if (tasks_min[t] > itv) |
| 316 | tasks_min[t] = itv; |
| 317 | tasks_avg[t] += itv; |
| 318 | printf("%6lld ", nano2usec(itv)); |
| 319 | } |
| 320 | printf("\n"); |
| 321 | printf(" len: "); |
| 322 | for (t=0; t < nr_tasks; t++) { |
| 323 | unsigned long long len = intervals_length[i][t]; |
| 324 | |
| 325 | printf("%6lld ", nano2usec(len)); |
| 326 | } |
| 327 | printf("\n"); |
| 328 | printf(" loops: "); |
| 329 | for (t=0; t < nr_tasks; t++) { |
| 330 | unsigned long loops = intervals_loops[i][t]; |
| 331 | |
| 332 | printf("%6ld ", loops); |
| 333 | } |
| 334 | printf("\n"); |
| 335 | printf("\n"); |
| 336 | } |
| 337 | |
| 338 | printf("Parent pid: %d\n", getpid()); |
| 339 | |
| 340 | for (t=0; t < nr_tasks; t++) { |
| 341 | printf(" Task %d (prio %d) (pid %ld):\n", t, calc_prio(t), |
| 342 | thread_pids[t]); |
| 343 | printf(" Max: %lld us\n", nano2usec(tasks_max[t])); |
| 344 | printf(" Min: %lld us\n", nano2usec(tasks_min[t])); |
| 345 | printf(" Tot: %lld us\n", nano2usec(tasks_avg[t])); |
| 346 | printf(" Avg: %lld us\n", nano2usec(tasks_avg[t] / nr_runs)); |
| 347 | printf("\n"); |
| 348 | } |
| 349 | |
| 350 | if (check) { |
| 351 | if (check < 0) |
| 352 | printf(" Failed!\n"); |
| 353 | else |
| 354 | printf(" Passed!\n"); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | static void write_stats(FILE *f, void *data) |
| 359 | { |
| 360 | int i; |
| 361 | int t; |
| 362 | unsigned long long tasks_max[nr_tasks]; |
| 363 | unsigned long long tasks_min[nr_tasks]; |
| 364 | unsigned long long tasks_avg[nr_tasks]; |
| 365 | |
| 366 | memset(tasks_max, 0, sizeof(tasks_max[0])*nr_tasks); |
| 367 | memset(tasks_min, 0xff, sizeof(tasks_min[0])*nr_tasks); |
| 368 | memset(tasks_avg, 0, sizeof(tasks_avg[0])*nr_tasks); |
| 369 | |
| 370 | for (i=0; i < nr_runs; i++) { |
| 371 | for (t=0; t < nr_tasks; t++) { |
| 372 | unsigned long long itv = intervals[i][t]; |
| 373 | |
| 374 | if (tasks_max[t] < itv) |
| 375 | tasks_max[t] = itv; |
| 376 | if (tasks_min[t] > itv) |
| 377 | tasks_min[t] = itv; |
| 378 | tasks_avg[t] += itv; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | fprintf(f, " \"num_threads\": %d,\n", nr_tasks); |
| 383 | fprintf(f, " \"thread\": {\n"); |
| 384 | for (i = 0; i < nr_tasks; i++) { |
| 385 | fprintf(f, " \"%u\": {\n", i); |
| 386 | fprintf(f, " \"prio\": %d,\n", calc_prio(i)); |
| 387 | fprintf(f, " \"min\": %lld,\n", nano2usec(tasks_min[i])); |
| 388 | fprintf(f, " \"avg\": %lld,\n", nano2usec(tasks_avg[i]) / nr_runs); |
| 389 | fprintf(f, " \"max\": %lld,\n", nano2usec(tasks_max[i])); |
| 390 | fprintf(f, " \"total\": %lld\n", nano2usec(tasks_avg[i])); |
| 391 | fprintf(f, " }%s\n", i == nr_tasks - 1 ? "" : ","); |
| 392 | } |
| 393 | fprintf(f, " }\n"); |
| 394 | } |
| 395 | |
| 396 | static unsigned long busy_loop(unsigned long long start_time) |
| 397 | { |
| 398 | unsigned long long time; |
| 399 | unsigned long l = 0; |
| 400 | |
| 401 | do { |
| 402 | l++; |
| 403 | time = get_time(); |
| 404 | } while ((time - start_time) < RUN_INTERVAL); |
| 405 | |
| 406 | return l; |
| 407 | } |
| 408 | |
| 409 | void *start_task(void *data) |
| 410 | { |
| 411 | long id = (long)data; |
| 412 | unsigned long long start_time; |
| 413 | int prio = calc_prio(id); |
| 414 | struct sched_param param = { |
| 415 | .sched_priority = prio, |
| 416 | }; |
| 417 | int ret; |
| 418 | int high = 0; |
| 419 | cpu_set_t cpumask; |
| 420 | cpu_set_t save_cpumask; |
| 421 | int cpu = 0; |
| 422 | unsigned long l; |
| 423 | long pid; |
| 424 | |
| 425 | ret = sched_getaffinity(0, sizeof(save_cpumask), &save_cpumask); |
| 426 | if (ret < 0) |
| 427 | perr("getting affinity"); |
| 428 | |
| 429 | pid = gettid(); |
| 430 | thread_pids[id] = pid; |
| 431 | |
| 432 | /* Check if we are the highest prio task */ |
| 433 | if (id == nr_tasks-1) |
| 434 | high = 1; |
| 435 | |
| 436 | ret = sched_setscheduler(0, SCHED_FIFO, ¶m); |
| 437 | if (ret < 0 && !id) |
| 438 | fprintf(stderr, "Warning, can't set priorities\n"); |
| 439 | |
| 440 | while (!done) { |
| 441 | if (high) { |
| 442 | /* rotate around the CPUS */ |
| 443 | if (!CPU_ISSET(cpu, &save_cpumask)) |
| 444 | cpu = 0; |
| 445 | CPU_ZERO(&cpumask); |
| 446 | CPU_SET(cpu, &cpumask); cpu++; |
| 447 | sched_setaffinity(0, sizeof(cpumask), &cpumask); |
| 448 | } |
| 449 | pthread_barrier_wait(&start_barrier); |
| 450 | start_time = get_time(); |
| 451 | ftrace_write("Thread %d: started %lld diff %lld\n", |
| 452 | pid, start_time, start_time - now); |
| 453 | l = busy_loop(start_time); |
| 454 | record_time(id, start_time, l); |
| 455 | pthread_barrier_wait(&end_barrier); |
| 456 | } |
| 457 | |
| 458 | return (void*)pid; |
| 459 | } |
| 460 | |
| 461 | static int check_times(int l) |
| 462 | { |
| 463 | int i; |
| 464 | unsigned long long last; |
| 465 | unsigned long long last_loops; |
| 466 | unsigned long long last_length; |
| 467 | |
| 468 | for (i=0; i < nr_tasks; i++) { |
| 469 | if (i && last < intervals[l][i] && |
| 470 | ((intervals[l][i] - last) > max_err)) { |
| 471 | /* |
| 472 | * May be a false positive. |
| 473 | * Make sure that we did more loops |
| 474 | * our start is before the end |
| 475 | * and the end should be tested. |
| 476 | */ |
| 477 | if (intervals_loops[l][i] < last_loops || |
| 478 | intervals[l][i] > last_length || |
| 479 | (intervals_length[l][i] > last_length && |
| 480 | intervals_length[l][i] - last_length > max_err)) { |
| 481 | ftrace_write("Task %d FAILED\n", thread_pids[i]); |
| 482 | check = -1; |
| 483 | return 1; |
| 484 | } |
| 485 | } |
| 486 | last = intervals[l][i]; |
| 487 | last_loops = intervals_loops[l][i]; |
| 488 | last_length = intervals_length[l][i]; |
| 489 | } |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | static void stop_log(int sig) |
| 494 | { |
| 495 | stop = 1; |
| 496 | } |
| 497 | |
| 498 | static int count_cpus(void) |
| 499 | { |
| 500 | FILE *fp; |
| 501 | char buf[2048]; |
| 502 | int cpus = 0; |
| 503 | char *pbuf; |
| 504 | size_t *pn; |
| 505 | size_t n; |
| 506 | int r; |
| 507 | |
| 508 | n = 2048; |
| 509 | pn = &n; |
| 510 | pbuf = buf; |
| 511 | |
| 512 | fp = fopen("/proc/cpuinfo", "r"); |
| 513 | if (!fp) |
| 514 | perr("Can not read cpuinfo"); |
| 515 | |
| 516 | while ((r = getline(&pbuf, pn, fp)) >= 0) { |
| 517 | char *p; |
| 518 | |
| 519 | if (strncmp(buf, "processor", 9) != 0) |
| 520 | continue; |
| 521 | for (p = buf+9; isspace(*p); p++) |
| 522 | ; |
| 523 | if (*p == ':') |
| 524 | cpus++; |
| 525 | } |
| 526 | fclose(fp); |
| 527 | |
| 528 | return cpus; |
| 529 | } |
| 530 | |
| 531 | int main (int argc, char **argv) |
| 532 | { |
| 533 | pthread_t *threads; |
| 534 | long i; |
| 535 | int ret; |
| 536 | struct timespec intv; |
| 537 | struct sched_param param; |
| 538 | |
| 539 | rt_init(argc, argv); |
| 540 | parse_options(argc, argv); |
| 541 | |
| 542 | signal(SIGINT, stop_log); |
| 543 | signal(SIGALRM, stop_log); |
| 544 | |
| 545 | if (duration) |
| 546 | alarm(duration); |
| 547 | |
| 548 | if (argc >= (optind + 1)) |
| 549 | nr_tasks = atoi(argv[optind]); |
| 550 | else |
| 551 | nr_tasks = count_cpus() + 1; |
| 552 | |
| 553 | threads = malloc(sizeof(*threads) * nr_tasks); |
| 554 | if (!threads) |
| 555 | perr("malloc"); |
| 556 | memset(threads, 0, sizeof(*threads) * nr_tasks); |
| 557 | |
| 558 | ret = pthread_barrier_init(&start_barrier, NULL, nr_tasks + 1); |
| 559 | ret = pthread_barrier_init(&end_barrier, NULL, nr_tasks + 1); |
| 560 | if (ret < 0) |
| 561 | perr("pthread_barrier_init"); |
| 562 | |
| 563 | intervals = malloc(sizeof(void*) * nr_runs); |
| 564 | if (!intervals) |
| 565 | perr("malloc intervals array"); |
| 566 | |
| 567 | intervals_length = malloc(sizeof(void*) * nr_runs); |
| 568 | if (!intervals_length) |
| 569 | perr("malloc intervals length array"); |
| 570 | |
| 571 | intervals_loops = malloc(sizeof(void*) * nr_runs); |
| 572 | if (!intervals_loops) |
| 573 | perr("malloc intervals loops array"); |
| 574 | |
| 575 | thread_pids = malloc(sizeof(long) * nr_tasks); |
| 576 | if (!thread_pids) |
| 577 | perr("malloc thread_pids"); |
| 578 | |
| 579 | for (i=0; i < nr_runs; i++) { |
| 580 | intervals[i] = malloc(sizeof(unsigned long long)*nr_tasks); |
| 581 | if (!intervals[i]) |
| 582 | perr("malloc intervals"); |
| 583 | memset(intervals[i], 0, sizeof(unsigned long long)*nr_tasks); |
| 584 | |
| 585 | intervals_length[i] = malloc(sizeof(unsigned long long)*nr_tasks); |
| 586 | if (!intervals_length[i]) |
| 587 | perr("malloc length intervals"); |
| 588 | memset(intervals_length[i], 0, sizeof(unsigned long long)*nr_tasks); |
| 589 | |
| 590 | intervals_loops[i] = malloc(sizeof(unsigned long)*nr_tasks); |
| 591 | if (!intervals_loops[i]) |
| 592 | perr("malloc loops intervals"); |
| 593 | memset(intervals_loops[i], 0, sizeof(unsigned long)*nr_tasks); |
| 594 | } |
| 595 | |
| 596 | for (i=0; i < nr_tasks; i++) { |
| 597 | if (pthread_create(&threads[i], NULL, start_task, (void *)i)) |
| 598 | perr("pthread_create"); |
| 599 | |
| 600 | } |
| 601 | |
| 602 | /* |
| 603 | * Progress bar uses stderr to let users see it when |
| 604 | * redirecting output. So we convert stderr to use line |
| 605 | * buffering so the progress bar doesn't flicker. |
| 606 | */ |
| 607 | setlinebuf(stderr); |
| 608 | |
| 609 | /* up our prio above all tasks */ |
| 610 | memset(¶m, 0, sizeof(param)); |
| 611 | param.sched_priority = nr_tasks + prio_start; |
| 612 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) |
| 613 | fprintf(stderr, "Warning, can't set priority of main thread!\n"); |
| 614 | |
| 615 | |
| 616 | |
| 617 | intv.tv_sec = nano2sec(INTERVAL); |
| 618 | intv.tv_nsec = INTERVAL % sec2nano(1); |
| 619 | |
| 620 | if (!quiet) |
| 621 | print_progress_bar(0); |
| 622 | |
| 623 | setup_ftrace_marker(); |
| 624 | |
| 625 | for (loop=0; loop < nr_runs; loop++) { |
| 626 | unsigned long long end; |
| 627 | |
| 628 | /* Release the CPU so all can get to the next barrier */ |
| 629 | nanosleep(&intv, NULL); |
| 630 | |
| 631 | now = get_time(); |
| 632 | |
| 633 | ftrace_write("Loop %d now=%lld\n", loop, now); |
| 634 | |
| 635 | pthread_barrier_wait(&start_barrier); |
| 636 | |
| 637 | ftrace_write("All running!!!\n"); |
| 638 | |
| 639 | nanosleep(&intv, NULL); |
| 640 | |
| 641 | if (!quiet) |
| 642 | print_progress_bar((loop * 100)/nr_runs); |
| 643 | |
| 644 | end = get_time(); |
| 645 | ftrace_write("Loop %d end now=%lld diff=%lld\n", loop, end, end - now); |
| 646 | |
| 647 | pthread_barrier_wait(&end_barrier); |
| 648 | |
| 649 | if (stop || (check && check_times(loop))) { |
| 650 | loop++; |
| 651 | nr_runs = loop; |
| 652 | break; |
| 653 | } |
| 654 | } |
| 655 | putc('\n', stderr); |
| 656 | |
| 657 | pthread_barrier_wait(&start_barrier); |
| 658 | done = 1; |
| 659 | pthread_barrier_wait(&end_barrier); |
| 660 | |
| 661 | for (i=0; i < nr_tasks; i++) |
| 662 | pthread_join(threads[i], (void*)&thread_pids[i]); |
| 663 | |
| 664 | print_results(); |
| 665 | |
| 666 | if (strlen(jsonfile) != 0) |
| 667 | rt_write_json(jsonfile, check < 0, write_stats, NULL); |
| 668 | |
| 669 | if (stop) { |
| 670 | /* |
| 671 | * We use this test in bash while loops |
| 672 | * So if we hit Ctrl-C then let the while |
| 673 | * loop know to break. |
| 674 | */ |
| 675 | if (check < 0) |
| 676 | exit(-1); |
| 677 | else |
| 678 | exit(1); |
| 679 | } |
| 680 | if (check < 0) |
| 681 | exit(-1); |
| 682 | else |
| 683 | exit(0); |
| 684 | |
| 685 | return 0; |
| 686 | } |