1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org> 4 */ 5 6 #define _GNU_SOURCE 7 #include <getopt.h> 8 #include <stdlib.h> 9 #include <string.h> 10 #include <signal.h> 11 #include <unistd.h> 12 #include <errno.h> 13 #include <stdio.h> 14 #include <time.h> 15 #include <sched.h> 16 17 #include "utils.h" 18 #include "osnoise.h" 19 20 struct osnoise_hist_params { 21 char *cpus; 22 cpu_set_t monitored_cpus; 23 char *trace_output; 24 char *cgroup_name; 25 unsigned long long runtime; 26 unsigned long long period; 27 long long threshold; 28 long long stop_us; 29 long long stop_total_us; 30 int sleep_time; 31 int duration; 32 int set_sched; 33 int output_divisor; 34 int cgroup; 35 int hk_cpus; 36 cpu_set_t hk_cpu_set; 37 struct sched_attr sched_param; 38 struct trace_events *events; 39 char no_header; 40 char no_summary; 41 char no_index; 42 char with_zeros; 43 int bucket_size; 44 int entries; 45 int warmup; 46 }; 47 48 struct osnoise_hist_cpu { 49 int *samples; 50 int count; 51 52 unsigned long long min_sample; 53 unsigned long long sum_sample; 54 unsigned long long max_sample; 55 56 }; 57 58 struct osnoise_hist_data { 59 struct tracefs_hist *trace_hist; 60 struct osnoise_hist_cpu *hist; 61 int entries; 62 int bucket_size; 63 int nr_cpus; 64 }; 65 66 /* 67 * osnoise_free_histogram - free runtime data 68 */ 69 static void 70 osnoise_free_histogram(struct osnoise_hist_data *data) 71 { 72 int cpu; 73 74 /* one histogram for IRQ and one for thread, per CPU */ 75 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 76 if (data->hist[cpu].samples) 77 free(data->hist[cpu].samples); 78 } 79 80 /* one set of histograms per CPU */ 81 if (data->hist) 82 free(data->hist); 83 84 free(data); 85 } 86 87 /* 88 * osnoise_alloc_histogram - alloc runtime data 89 */ 90 static struct osnoise_hist_data 91 *osnoise_alloc_histogram(int nr_cpus, int entries, int bucket_size) 92 { 93 struct osnoise_hist_data *data; 94 int cpu; 95 96 data = calloc(1, sizeof(*data)); 97 if (!data) 98 return NULL; 99 100 data->entries = entries; 101 data->bucket_size = bucket_size; 102 data->nr_cpus = nr_cpus; 103 104 data->hist = calloc(1, sizeof(*data->hist) * nr_cpus); 105 if (!data->hist) 106 goto cleanup; 107 108 for (cpu = 0; cpu < nr_cpus; cpu++) { 109 data->hist[cpu].samples = calloc(1, sizeof(*data->hist->samples) * (entries + 1)); 110 if (!data->hist[cpu].samples) 111 goto cleanup; 112 } 113 114 /* set the min to max */ 115 for (cpu = 0; cpu < nr_cpus; cpu++) 116 data->hist[cpu].min_sample = ~0; 117 118 return data; 119 120 cleanup: 121 osnoise_free_histogram(data); 122 return NULL; 123 } 124 125 static void osnoise_hist_update_multiple(struct osnoise_tool *tool, int cpu, 126 unsigned long long duration, int count) 127 { 128 struct osnoise_hist_params *params = tool->params; 129 struct osnoise_hist_data *data = tool->data; 130 unsigned long long total_duration; 131 int entries = data->entries; 132 int bucket; 133 int *hist; 134 135 if (params->output_divisor) 136 duration = duration / params->output_divisor; 137 138 bucket = duration / data->bucket_size; 139 140 total_duration = duration * count; 141 142 hist = data->hist[cpu].samples; 143 data->hist[cpu].count += count; 144 update_min(&data->hist[cpu].min_sample, &duration); 145 update_sum(&data->hist[cpu].sum_sample, &total_duration); 146 update_max(&data->hist[cpu].max_sample, &duration); 147 148 if (bucket < entries) 149 hist[bucket] += count; 150 else 151 hist[entries] += count; 152 } 153 154 /* 155 * osnoise_destroy_trace_hist - disable events used to collect histogram 156 */ 157 static void osnoise_destroy_trace_hist(struct osnoise_tool *tool) 158 { 159 struct osnoise_hist_data *data = tool->data; 160 161 tracefs_hist_pause(tool->trace.inst, data->trace_hist); 162 tracefs_hist_destroy(tool->trace.inst, data->trace_hist); 163 } 164 165 /* 166 * osnoise_init_trace_hist - enable events used to collect histogram 167 */ 168 static int osnoise_init_trace_hist(struct osnoise_tool *tool) 169 { 170 struct osnoise_hist_params *params = tool->params; 171 struct osnoise_hist_data *data = tool->data; 172 int bucket_size; 173 char buff[128]; 174 int retval = 0; 175 176 /* 177 * Set the size of the bucket. 178 */ 179 bucket_size = params->output_divisor * params->bucket_size; 180 snprintf(buff, sizeof(buff), "duration.buckets=%d", bucket_size); 181 182 data->trace_hist = tracefs_hist_alloc(tool->trace.tep, "osnoise", "sample_threshold", 183 buff, TRACEFS_HIST_KEY_NORMAL); 184 if (!data->trace_hist) 185 return 1; 186 187 retval = tracefs_hist_add_key(data->trace_hist, "cpu", 0); 188 if (retval) 189 goto out_err; 190 191 retval = tracefs_hist_start(tool->trace.inst, data->trace_hist); 192 if (retval) 193 goto out_err; 194 195 return 0; 196 197 out_err: 198 osnoise_destroy_trace_hist(tool); 199 return 1; 200 } 201 202 /* 203 * osnoise_read_trace_hist - parse histogram file and file osnoise histogram 204 */ 205 static void osnoise_read_trace_hist(struct osnoise_tool *tool) 206 { 207 struct osnoise_hist_data *data = tool->data; 208 long long cpu, counter, duration; 209 char *content, *position; 210 211 tracefs_hist_pause(tool->trace.inst, data->trace_hist); 212 213 content = tracefs_event_file_read(tool->trace.inst, "osnoise", 214 "sample_threshold", 215 "hist", NULL); 216 if (!content) 217 return; 218 219 position = content; 220 while (true) { 221 position = strstr(position, "duration: ~"); 222 if (!position) 223 break; 224 position += strlen("duration: ~"); 225 duration = get_llong_from_str(position); 226 if (duration == -1) 227 err_msg("error reading duration from histogram\n"); 228 229 position = strstr(position, "cpu:"); 230 if (!position) 231 break; 232 position += strlen("cpu: "); 233 cpu = get_llong_from_str(position); 234 if (cpu == -1) 235 err_msg("error reading cpu from histogram\n"); 236 237 position = strstr(position, "hitcount:"); 238 if (!position) 239 break; 240 position += strlen("hitcount: "); 241 counter = get_llong_from_str(position); 242 if (counter == -1) 243 err_msg("error reading counter from histogram\n"); 244 245 osnoise_hist_update_multiple(tool, cpu, duration, counter); 246 } 247 free(content); 248 } 249 250 /* 251 * osnoise_hist_header - print the header of the tracer to the output 252 */ 253 static void osnoise_hist_header(struct osnoise_tool *tool) 254 { 255 struct osnoise_hist_params *params = tool->params; 256 struct osnoise_hist_data *data = tool->data; 257 struct trace_seq *s = tool->trace.seq; 258 char duration[26]; 259 int cpu; 260 261 if (params->no_header) 262 return; 263 264 get_duration(tool->start_time, duration, sizeof(duration)); 265 trace_seq_printf(s, "# RTLA osnoise histogram\n"); 266 trace_seq_printf(s, "# Time unit is %s (%s)\n", 267 params->output_divisor == 1 ? "nanoseconds" : "microseconds", 268 params->output_divisor == 1 ? "ns" : "us"); 269 270 trace_seq_printf(s, "# Duration: %s\n", duration); 271 272 if (!params->no_index) 273 trace_seq_printf(s, "Index"); 274 275 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 276 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 277 continue; 278 279 if (!data->hist[cpu].count) 280 continue; 281 282 trace_seq_printf(s, " CPU-%03d", cpu); 283 } 284 trace_seq_printf(s, "\n"); 285 286 trace_seq_do_printf(s); 287 trace_seq_reset(s); 288 } 289 290 /* 291 * osnoise_print_summary - print the summary of the hist data to the output 292 */ 293 static void 294 osnoise_print_summary(struct osnoise_hist_params *params, 295 struct trace_instance *trace, 296 struct osnoise_hist_data *data) 297 { 298 int cpu; 299 300 if (params->no_summary) 301 return; 302 303 if (!params->no_index) 304 trace_seq_printf(trace->seq, "count:"); 305 306 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 307 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 308 continue; 309 310 if (!data->hist[cpu].count) 311 continue; 312 313 trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].count); 314 } 315 trace_seq_printf(trace->seq, "\n"); 316 317 if (!params->no_index) 318 trace_seq_printf(trace->seq, "min: "); 319 320 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 321 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 322 continue; 323 324 if (!data->hist[cpu].count) 325 continue; 326 327 trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].min_sample); 328 329 } 330 trace_seq_printf(trace->seq, "\n"); 331 332 if (!params->no_index) 333 trace_seq_printf(trace->seq, "avg: "); 334 335 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 336 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 337 continue; 338 339 if (!data->hist[cpu].count) 340 continue; 341 342 if (data->hist[cpu].count) 343 trace_seq_printf(trace->seq, "%9.2f ", 344 ((double) data->hist[cpu].sum_sample) / data->hist[cpu].count); 345 else 346 trace_seq_printf(trace->seq, " - "); 347 } 348 trace_seq_printf(trace->seq, "\n"); 349 350 if (!params->no_index) 351 trace_seq_printf(trace->seq, "max: "); 352 353 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 354 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 355 continue; 356 357 if (!data->hist[cpu].count) 358 continue; 359 360 trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].max_sample); 361 362 } 363 trace_seq_printf(trace->seq, "\n"); 364 trace_seq_do_printf(trace->seq); 365 trace_seq_reset(trace->seq); 366 } 367 368 /* 369 * osnoise_print_stats - print data for all CPUs 370 */ 371 static void 372 osnoise_print_stats(struct osnoise_hist_params *params, struct osnoise_tool *tool) 373 { 374 struct osnoise_hist_data *data = tool->data; 375 struct trace_instance *trace = &tool->trace; 376 int bucket, cpu; 377 int total; 378 379 osnoise_hist_header(tool); 380 381 for (bucket = 0; bucket < data->entries; bucket++) { 382 total = 0; 383 384 if (!params->no_index) 385 trace_seq_printf(trace->seq, "%-6d", 386 bucket * data->bucket_size); 387 388 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 389 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 390 continue; 391 392 if (!data->hist[cpu].count) 393 continue; 394 395 total += data->hist[cpu].samples[bucket]; 396 trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].samples[bucket]); 397 } 398 399 if (total == 0 && !params->with_zeros) { 400 trace_seq_reset(trace->seq); 401 continue; 402 } 403 404 trace_seq_printf(trace->seq, "\n"); 405 trace_seq_do_printf(trace->seq); 406 trace_seq_reset(trace->seq); 407 } 408 409 if (!params->no_index) 410 trace_seq_printf(trace->seq, "over: "); 411 412 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 413 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 414 continue; 415 416 if (!data->hist[cpu].count) 417 continue; 418 419 trace_seq_printf(trace->seq, "%9d ", 420 data->hist[cpu].samples[data->entries]); 421 } 422 trace_seq_printf(trace->seq, "\n"); 423 trace_seq_do_printf(trace->seq); 424 trace_seq_reset(trace->seq); 425 426 osnoise_print_summary(params, trace, data); 427 } 428 429 /* 430 * osnoise_hist_usage - prints osnoise hist usage message 431 */ 432 static void osnoise_hist_usage(char *usage) 433 { 434 int i; 435 436 static const char * const msg[] = { 437 "", 438 " usage: rtla osnoise hist [-h] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\", 439 " [-T us] [-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\", 440 " [-c cpu-list] [-H cpu-list] [-P priority] [-b N] [-E N] [--no-header] [--no-summary] \\", 441 " [--no-index] [--with-zeros] [-C[=cgroup_name]] [--warm-up]", 442 "", 443 " -h/--help: print this menu", 444 " -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit", 445 " -p/--period us: osnoise period in us", 446 " -r/--runtime us: osnoise runtime in us", 447 " -s/--stop us: stop trace if a single sample is higher than the argument in us", 448 " -S/--stop-total us: stop trace if the total sample is higher than the argument in us", 449 " -T/--threshold us: the minimum delta to be considered a noise", 450 " -c/--cpus cpu-list: list of cpus to run osnoise threads", 451 " -H/--house-keeping cpus: run rtla control threads only on the given cpus", 452 " -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited", 453 " -d/--duration time[s|m|h|d]: duration of the session", 454 " -D/--debug: print debug info", 455 " -t/--trace[=file]: save the stopped trace to [file|osnoise_trace.txt]", 456 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed", 457 " --filter <filter>: enable a trace event filter to the previous -e event", 458 " --trigger <trigger>: enable a trace event trigger to the previous -e event", 459 " -b/--bucket-size N: set the histogram bucket size (default 1)", 460 " -E/--entries N: set the number of entries of the histogram (default 256)", 461 " --no-header: do not print header", 462 " --no-summary: do not print summary", 463 " --no-index: do not print index", 464 " --with-zeros: print zero only entries", 465 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period: set scheduling parameters", 466 " o:prio - use SCHED_OTHER with prio", 467 " r:prio - use SCHED_RR with prio", 468 " f:prio - use SCHED_FIFO with prio", 469 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period", 470 " in nanoseconds", 471 " --warm-up: let the workload run for s seconds before collecting data", 472 NULL, 473 }; 474 475 if (usage) 476 fprintf(stderr, "%s\n", usage); 477 478 fprintf(stderr, "rtla osnoise hist: a per-cpu histogram of the OS noise (version %s)\n", 479 VERSION); 480 481 for (i = 0; msg[i]; i++) 482 fprintf(stderr, "%s\n", msg[i]); 483 484 if (usage) 485 exit(EXIT_FAILURE); 486 487 exit(EXIT_SUCCESS); 488 } 489 490 /* 491 * osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters 492 */ 493 static struct osnoise_hist_params 494 *osnoise_hist_parse_args(int argc, char *argv[]) 495 { 496 struct osnoise_hist_params *params; 497 struct trace_events *tevent; 498 int retval; 499 int c; 500 501 params = calloc(1, sizeof(*params)); 502 if (!params) 503 exit(1); 504 505 /* display data in microseconds */ 506 params->output_divisor = 1000; 507 params->bucket_size = 1; 508 params->entries = 256; 509 510 while (1) { 511 static struct option long_options[] = { 512 {"auto", required_argument, 0, 'a'}, 513 {"bucket-size", required_argument, 0, 'b'}, 514 {"entries", required_argument, 0, 'E'}, 515 {"cpus", required_argument, 0, 'c'}, 516 {"cgroup", optional_argument, 0, 'C'}, 517 {"debug", no_argument, 0, 'D'}, 518 {"duration", required_argument, 0, 'd'}, 519 {"house-keeping", required_argument, 0, 'H'}, 520 {"help", no_argument, 0, 'h'}, 521 {"period", required_argument, 0, 'p'}, 522 {"priority", required_argument, 0, 'P'}, 523 {"runtime", required_argument, 0, 'r'}, 524 {"stop", required_argument, 0, 's'}, 525 {"stop-total", required_argument, 0, 'S'}, 526 {"trace", optional_argument, 0, 't'}, 527 {"event", required_argument, 0, 'e'}, 528 {"threshold", required_argument, 0, 'T'}, 529 {"no-header", no_argument, 0, '0'}, 530 {"no-summary", no_argument, 0, '1'}, 531 {"no-index", no_argument, 0, '2'}, 532 {"with-zeros", no_argument, 0, '3'}, 533 {"trigger", required_argument, 0, '4'}, 534 {"filter", required_argument, 0, '5'}, 535 {"warm-up", required_argument, 0, '6'}, 536 {0, 0, 0, 0} 537 }; 538 539 /* getopt_long stores the option index here. */ 540 int option_index = 0; 541 542 c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:p:P:r:s:S:t::T:01234:5:6:", 543 long_options, &option_index); 544 545 /* detect the end of the options. */ 546 if (c == -1) 547 break; 548 549 switch (c) { 550 case 'a': 551 /* set sample stop to auto_thresh */ 552 params->stop_us = get_llong_from_str(optarg); 553 554 /* set sample threshold to 1 */ 555 params->threshold = 1; 556 557 /* set trace */ 558 params->trace_output = "osnoise_trace.txt"; 559 560 break; 561 case 'b': 562 params->bucket_size = get_llong_from_str(optarg); 563 if ((params->bucket_size == 0) || (params->bucket_size >= 1000000)) 564 osnoise_hist_usage("Bucket size needs to be > 0 and <= 1000000\n"); 565 break; 566 case 'c': 567 retval = parse_cpu_set(optarg, ¶ms->monitored_cpus); 568 if (retval) 569 osnoise_hist_usage("\nInvalid -c cpu list\n"); 570 params->cpus = optarg; 571 break; 572 case 'C': 573 params->cgroup = 1; 574 if (!optarg) { 575 /* will inherit this cgroup */ 576 params->cgroup_name = NULL; 577 } else if (*optarg == '=') { 578 /* skip the = */ 579 params->cgroup_name = ++optarg; 580 } 581 break; 582 case 'D': 583 config_debug = 1; 584 break; 585 case 'd': 586 params->duration = parse_seconds_duration(optarg); 587 if (!params->duration) 588 osnoise_hist_usage("Invalid -D duration\n"); 589 break; 590 case 'e': 591 tevent = trace_event_alloc(optarg); 592 if (!tevent) { 593 err_msg("Error alloc trace event"); 594 exit(EXIT_FAILURE); 595 } 596 597 if (params->events) 598 tevent->next = params->events; 599 600 params->events = tevent; 601 break; 602 case 'E': 603 params->entries = get_llong_from_str(optarg); 604 if ((params->entries < 10) || (params->entries > 9999999)) 605 osnoise_hist_usage("Entries must be > 10 and < 9999999\n"); 606 break; 607 case 'h': 608 case '?': 609 osnoise_hist_usage(NULL); 610 break; 611 case 'H': 612 params->hk_cpus = 1; 613 retval = parse_cpu_set(optarg, ¶ms->hk_cpu_set); 614 if (retval) { 615 err_msg("Error parsing house keeping CPUs\n"); 616 exit(EXIT_FAILURE); 617 } 618 break; 619 case 'p': 620 params->period = get_llong_from_str(optarg); 621 if (params->period > 10000000) 622 osnoise_hist_usage("Period longer than 10 s\n"); 623 break; 624 case 'P': 625 retval = parse_prio(optarg, ¶ms->sched_param); 626 if (retval == -1) 627 osnoise_hist_usage("Invalid -P priority"); 628 params->set_sched = 1; 629 break; 630 case 'r': 631 params->runtime = get_llong_from_str(optarg); 632 if (params->runtime < 100) 633 osnoise_hist_usage("Runtime shorter than 100 us\n"); 634 break; 635 case 's': 636 params->stop_us = get_llong_from_str(optarg); 637 break; 638 case 'S': 639 params->stop_total_us = get_llong_from_str(optarg); 640 break; 641 case 'T': 642 params->threshold = get_llong_from_str(optarg); 643 break; 644 case 't': 645 if (optarg) 646 /* skip = */ 647 params->trace_output = &optarg[1]; 648 else 649 params->trace_output = "osnoise_trace.txt"; 650 break; 651 case '0': /* no header */ 652 params->no_header = 1; 653 break; 654 case '1': /* no summary */ 655 params->no_summary = 1; 656 break; 657 case '2': /* no index */ 658 params->no_index = 1; 659 break; 660 case '3': /* with zeros */ 661 params->with_zeros = 1; 662 break; 663 case '4': /* trigger */ 664 if (params->events) { 665 retval = trace_event_add_trigger(params->events, optarg); 666 if (retval) { 667 err_msg("Error adding trigger %s\n", optarg); 668 exit(EXIT_FAILURE); 669 } 670 } else { 671 osnoise_hist_usage("--trigger requires a previous -e\n"); 672 } 673 break; 674 case '5': /* filter */ 675 if (params->events) { 676 retval = trace_event_add_filter(params->events, optarg); 677 if (retval) { 678 err_msg("Error adding filter %s\n", optarg); 679 exit(EXIT_FAILURE); 680 } 681 } else { 682 osnoise_hist_usage("--filter requires a previous -e\n"); 683 } 684 break; 685 case '6': 686 params->warmup = get_llong_from_str(optarg); 687 break; 688 default: 689 osnoise_hist_usage("Invalid option"); 690 } 691 } 692 693 if (geteuid()) { 694 err_msg("rtla needs root permission\n"); 695 exit(EXIT_FAILURE); 696 } 697 698 if (params->no_index && !params->with_zeros) 699 osnoise_hist_usage("no-index set and with-zeros not set - it does not make sense"); 700 701 return params; 702 } 703 704 /* 705 * osnoise_hist_apply_config - apply the hist configs to the initialized tool 706 */ 707 static int 708 osnoise_hist_apply_config(struct osnoise_tool *tool, struct osnoise_hist_params *params) 709 { 710 int retval; 711 712 if (!params->sleep_time) 713 params->sleep_time = 1; 714 715 if (params->cpus) { 716 retval = osnoise_set_cpus(tool->context, params->cpus); 717 if (retval) { 718 err_msg("Failed to apply CPUs config\n"); 719 goto out_err; 720 } 721 } 722 723 if (params->runtime || params->period) { 724 retval = osnoise_set_runtime_period(tool->context, 725 params->runtime, 726 params->period); 727 if (retval) { 728 err_msg("Failed to set runtime and/or period\n"); 729 goto out_err; 730 } 731 } 732 733 if (params->stop_us) { 734 retval = osnoise_set_stop_us(tool->context, params->stop_us); 735 if (retval) { 736 err_msg("Failed to set stop us\n"); 737 goto out_err; 738 } 739 } 740 741 if (params->stop_total_us) { 742 retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us); 743 if (retval) { 744 err_msg("Failed to set stop total us\n"); 745 goto out_err; 746 } 747 } 748 749 if (params->threshold) { 750 retval = osnoise_set_tracing_thresh(tool->context, params->threshold); 751 if (retval) { 752 err_msg("Failed to set tracing_thresh\n"); 753 goto out_err; 754 } 755 } 756 757 if (params->hk_cpus) { 758 retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set), 759 ¶ms->hk_cpu_set); 760 if (retval == -1) { 761 err_msg("Failed to set rtla to the house keeping CPUs\n"); 762 goto out_err; 763 } 764 } else if (params->cpus) { 765 /* 766 * Even if the user do not set a house-keeping CPU, try to 767 * move rtla to a CPU set different to the one where the user 768 * set the workload to run. 769 * 770 * No need to check results as this is an automatic attempt. 771 */ 772 auto_house_keeping(¶ms->monitored_cpus); 773 } 774 775 return 0; 776 777 out_err: 778 return -1; 779 } 780 781 /* 782 * osnoise_init_hist - initialize a osnoise hist tool with parameters 783 */ 784 static struct osnoise_tool 785 *osnoise_init_hist(struct osnoise_hist_params *params) 786 { 787 struct osnoise_tool *tool; 788 int nr_cpus; 789 790 nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 791 792 tool = osnoise_init_tool("osnoise_hist"); 793 if (!tool) 794 return NULL; 795 796 tool->data = osnoise_alloc_histogram(nr_cpus, params->entries, params->bucket_size); 797 if (!tool->data) 798 goto out_err; 799 800 tool->params = params; 801 802 return tool; 803 804 out_err: 805 osnoise_destroy_tool(tool); 806 return NULL; 807 } 808 809 static int stop_tracing; 810 static void stop_hist(int sig) 811 { 812 stop_tracing = 1; 813 } 814 815 /* 816 * osnoise_hist_set_signals - handles the signal to stop the tool 817 */ 818 static void 819 osnoise_hist_set_signals(struct osnoise_hist_params *params) 820 { 821 signal(SIGINT, stop_hist); 822 if (params->duration) { 823 signal(SIGALRM, stop_hist); 824 alarm(params->duration); 825 } 826 } 827 828 int osnoise_hist_main(int argc, char *argv[]) 829 { 830 struct osnoise_hist_params *params; 831 struct osnoise_tool *record = NULL; 832 struct osnoise_tool *tool = NULL; 833 struct trace_instance *trace; 834 int return_value = 1; 835 int retval; 836 837 params = osnoise_hist_parse_args(argc, argv); 838 if (!params) 839 exit(1); 840 841 tool = osnoise_init_hist(params); 842 if (!tool) { 843 err_msg("Could not init osnoise hist\n"); 844 goto out_exit; 845 } 846 847 retval = osnoise_hist_apply_config(tool, params); 848 if (retval) { 849 err_msg("Could not apply config\n"); 850 goto out_destroy; 851 } 852 853 trace = &tool->trace; 854 855 retval = enable_osnoise(trace); 856 if (retval) { 857 err_msg("Failed to enable osnoise tracer\n"); 858 goto out_destroy; 859 } 860 861 retval = osnoise_init_trace_hist(tool); 862 if (retval) 863 goto out_destroy; 864 865 if (params->set_sched) { 866 retval = set_comm_sched_attr("osnoise/", ¶ms->sched_param); 867 if (retval) { 868 err_msg("Failed to set sched parameters\n"); 869 goto out_free; 870 } 871 } 872 873 if (params->cgroup) { 874 retval = set_comm_cgroup("timerlat/", params->cgroup_name); 875 if (!retval) { 876 err_msg("Failed to move threads to cgroup\n"); 877 goto out_free; 878 } 879 } 880 881 if (params->trace_output) { 882 record = osnoise_init_trace_tool("osnoise"); 883 if (!record) { 884 err_msg("Failed to enable the trace instance\n"); 885 goto out_free; 886 } 887 888 if (params->events) { 889 retval = trace_events_enable(&record->trace, params->events); 890 if (retval) 891 goto out_hist; 892 } 893 894 } 895 896 /* 897 * Start the tracer here, after having set all instances. 898 * 899 * Let the trace instance start first for the case of hitting a stop 900 * tracing while enabling other instances. The trace instance is the 901 * one with most valuable information. 902 */ 903 if (params->trace_output) 904 trace_instance_start(&record->trace); 905 trace_instance_start(trace); 906 907 if (params->warmup > 0) { 908 debug_msg("Warming up for %d seconds\n", params->warmup); 909 sleep(params->warmup); 910 if (stop_tracing) 911 goto out_hist; 912 913 /* 914 * Clean up the buffer. The osnoise workload do not run 915 * with tracing off to avoid creating a performance penalty 916 * when not needed. 917 */ 918 retval = tracefs_instance_file_write(trace->inst, "trace", ""); 919 if (retval < 0) { 920 debug_msg("Error cleaning up the buffer"); 921 goto out_hist; 922 } 923 924 } 925 926 tool->start_time = time(NULL); 927 osnoise_hist_set_signals(params); 928 929 while (!stop_tracing) { 930 sleep(params->sleep_time); 931 932 retval = tracefs_iterate_raw_events(trace->tep, 933 trace->inst, 934 NULL, 935 0, 936 collect_registered_events, 937 trace); 938 if (retval < 0) { 939 err_msg("Error iterating on events\n"); 940 goto out_hist; 941 } 942 943 if (trace_is_off(&tool->trace, &record->trace)) 944 break; 945 } 946 947 osnoise_read_trace_hist(tool); 948 949 osnoise_print_stats(params, tool); 950 951 return_value = 0; 952 953 if (trace_is_off(&tool->trace, &record->trace)) { 954 printf("rtla osnoise hit stop tracing\n"); 955 if (params->trace_output) { 956 printf(" Saving trace to %s\n", params->trace_output); 957 save_trace_to_file(record->trace.inst, params->trace_output); 958 } 959 } 960 961 out_hist: 962 trace_events_destroy(&record->trace, params->events); 963 params->events = NULL; 964 out_free: 965 osnoise_free_histogram(tool->data); 966 out_destroy: 967 osnoise_destroy_tool(record); 968 osnoise_destroy_tool(tool); 969 free(params); 970 out_exit: 971 exit(return_value); 972 } 973