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 40 char no_header; 41 char no_summary; 42 char no_index; 43 char with_zeros; 44 int bucket_size; 45 int entries; 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]]", 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 NULL, 472 }; 473 474 if (usage) 475 fprintf(stderr, "%s\n", usage); 476 477 fprintf(stderr, "rtla osnoise hist: a per-cpu histogram of the OS noise (version %s)\n", 478 VERSION); 479 480 for (i = 0; msg[i]; i++) 481 fprintf(stderr, "%s\n", msg[i]); 482 483 if (usage) 484 exit(EXIT_FAILURE); 485 486 exit(EXIT_SUCCESS); 487 } 488 489 /* 490 * osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters 491 */ 492 static struct osnoise_hist_params 493 *osnoise_hist_parse_args(int argc, char *argv[]) 494 { 495 struct osnoise_hist_params *params; 496 struct trace_events *tevent; 497 int retval; 498 int c; 499 500 params = calloc(1, sizeof(*params)); 501 if (!params) 502 exit(1); 503 504 /* display data in microseconds */ 505 params->output_divisor = 1000; 506 params->bucket_size = 1; 507 params->entries = 256; 508 509 while (1) { 510 static struct option long_options[] = { 511 {"auto", required_argument, 0, 'a'}, 512 {"bucket-size", required_argument, 0, 'b'}, 513 {"entries", required_argument, 0, 'E'}, 514 {"cpus", required_argument, 0, 'c'}, 515 {"cgroup", optional_argument, 0, 'C'}, 516 {"debug", no_argument, 0, 'D'}, 517 {"duration", required_argument, 0, 'd'}, 518 {"house-keeping", required_argument, 0, 'H'}, 519 {"help", no_argument, 0, 'h'}, 520 {"period", required_argument, 0, 'p'}, 521 {"priority", required_argument, 0, 'P'}, 522 {"runtime", required_argument, 0, 'r'}, 523 {"stop", required_argument, 0, 's'}, 524 {"stop-total", required_argument, 0, 'S'}, 525 {"trace", optional_argument, 0, 't'}, 526 {"event", required_argument, 0, 'e'}, 527 {"threshold", required_argument, 0, 'T'}, 528 {"no-header", no_argument, 0, '0'}, 529 {"no-summary", no_argument, 0, '1'}, 530 {"no-index", no_argument, 0, '2'}, 531 {"with-zeros", no_argument, 0, '3'}, 532 {"trigger", required_argument, 0, '4'}, 533 {"filter", required_argument, 0, '5'}, 534 {0, 0, 0, 0} 535 }; 536 537 /* getopt_long stores the option index here. */ 538 int option_index = 0; 539 540 c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:p:P:r:s:S:t::T:01234:5:", 541 long_options, &option_index); 542 543 /* detect the end of the options. */ 544 if (c == -1) 545 break; 546 547 switch (c) { 548 case 'a': 549 /* set sample stop to auto_thresh */ 550 params->stop_us = get_llong_from_str(optarg); 551 552 /* set sample threshold to 1 */ 553 params->threshold = 1; 554 555 /* set trace */ 556 params->trace_output = "osnoise_trace.txt"; 557 558 break; 559 case 'b': 560 params->bucket_size = get_llong_from_str(optarg); 561 if ((params->bucket_size == 0) || (params->bucket_size >= 1000000)) 562 osnoise_hist_usage("Bucket size needs to be > 0 and <= 1000000\n"); 563 break; 564 case 'c': 565 retval = parse_cpu_set(optarg, ¶ms->monitored_cpus); 566 if (retval) 567 osnoise_hist_usage("\nInvalid -c cpu list\n"); 568 params->cpus = optarg; 569 break; 570 case 'C': 571 params->cgroup = 1; 572 if (!optarg) { 573 /* will inherit this cgroup */ 574 params->cgroup_name = NULL; 575 } else if (*optarg == '=') { 576 /* skip the = */ 577 params->cgroup_name = ++optarg; 578 } 579 break; 580 case 'D': 581 config_debug = 1; 582 break; 583 case 'd': 584 params->duration = parse_seconds_duration(optarg); 585 if (!params->duration) 586 osnoise_hist_usage("Invalid -D duration\n"); 587 break; 588 case 'e': 589 tevent = trace_event_alloc(optarg); 590 if (!tevent) { 591 err_msg("Error alloc trace event"); 592 exit(EXIT_FAILURE); 593 } 594 595 if (params->events) 596 tevent->next = params->events; 597 598 params->events = tevent; 599 break; 600 case 'E': 601 params->entries = get_llong_from_str(optarg); 602 if ((params->entries < 10) || (params->entries > 9999999)) 603 osnoise_hist_usage("Entries must be > 10 and < 9999999\n"); 604 break; 605 case 'h': 606 case '?': 607 osnoise_hist_usage(NULL); 608 break; 609 case 'H': 610 params->hk_cpus = 1; 611 retval = parse_cpu_set(optarg, ¶ms->hk_cpu_set); 612 if (retval) { 613 err_msg("Error parsing house keeping CPUs\n"); 614 exit(EXIT_FAILURE); 615 } 616 break; 617 case 'p': 618 params->period = get_llong_from_str(optarg); 619 if (params->period > 10000000) 620 osnoise_hist_usage("Period longer than 10 s\n"); 621 break; 622 case 'P': 623 retval = parse_prio(optarg, ¶ms->sched_param); 624 if (retval == -1) 625 osnoise_hist_usage("Invalid -P priority"); 626 params->set_sched = 1; 627 break; 628 case 'r': 629 params->runtime = get_llong_from_str(optarg); 630 if (params->runtime < 100) 631 osnoise_hist_usage("Runtime shorter than 100 us\n"); 632 break; 633 case 's': 634 params->stop_us = get_llong_from_str(optarg); 635 break; 636 case 'S': 637 params->stop_total_us = get_llong_from_str(optarg); 638 break; 639 case 'T': 640 params->threshold = get_llong_from_str(optarg); 641 break; 642 case 't': 643 if (optarg) 644 /* skip = */ 645 params->trace_output = &optarg[1]; 646 else 647 params->trace_output = "osnoise_trace.txt"; 648 break; 649 case '0': /* no header */ 650 params->no_header = 1; 651 break; 652 case '1': /* no summary */ 653 params->no_summary = 1; 654 break; 655 case '2': /* no index */ 656 params->no_index = 1; 657 break; 658 case '3': /* with zeros */ 659 params->with_zeros = 1; 660 break; 661 case '4': /* trigger */ 662 if (params->events) { 663 retval = trace_event_add_trigger(params->events, optarg); 664 if (retval) { 665 err_msg("Error adding trigger %s\n", optarg); 666 exit(EXIT_FAILURE); 667 } 668 } else { 669 osnoise_hist_usage("--trigger requires a previous -e\n"); 670 } 671 break; 672 case '5': /* filter */ 673 if (params->events) { 674 retval = trace_event_add_filter(params->events, optarg); 675 if (retval) { 676 err_msg("Error adding filter %s\n", optarg); 677 exit(EXIT_FAILURE); 678 } 679 } else { 680 osnoise_hist_usage("--filter requires a previous -e\n"); 681 } 682 break; 683 default: 684 osnoise_hist_usage("Invalid option"); 685 } 686 } 687 688 if (geteuid()) { 689 err_msg("rtla needs root permission\n"); 690 exit(EXIT_FAILURE); 691 } 692 693 if (params->no_index && !params->with_zeros) 694 osnoise_hist_usage("no-index set and with-zeros not set - it does not make sense"); 695 696 return params; 697 } 698 699 /* 700 * osnoise_hist_apply_config - apply the hist configs to the initialized tool 701 */ 702 static int 703 osnoise_hist_apply_config(struct osnoise_tool *tool, struct osnoise_hist_params *params) 704 { 705 int retval; 706 707 if (!params->sleep_time) 708 params->sleep_time = 1; 709 710 if (params->cpus) { 711 retval = osnoise_set_cpus(tool->context, params->cpus); 712 if (retval) { 713 err_msg("Failed to apply CPUs config\n"); 714 goto out_err; 715 } 716 } 717 718 if (params->runtime || params->period) { 719 retval = osnoise_set_runtime_period(tool->context, 720 params->runtime, 721 params->period); 722 if (retval) { 723 err_msg("Failed to set runtime and/or period\n"); 724 goto out_err; 725 } 726 } 727 728 if (params->stop_us) { 729 retval = osnoise_set_stop_us(tool->context, params->stop_us); 730 if (retval) { 731 err_msg("Failed to set stop us\n"); 732 goto out_err; 733 } 734 } 735 736 if (params->stop_total_us) { 737 retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us); 738 if (retval) { 739 err_msg("Failed to set stop total us\n"); 740 goto out_err; 741 } 742 } 743 744 if (params->threshold) { 745 retval = osnoise_set_tracing_thresh(tool->context, params->threshold); 746 if (retval) { 747 err_msg("Failed to set tracing_thresh\n"); 748 goto out_err; 749 } 750 } 751 752 if (params->hk_cpus) { 753 retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set), 754 ¶ms->hk_cpu_set); 755 if (retval == -1) { 756 err_msg("Failed to set rtla to the house keeping CPUs\n"); 757 goto out_err; 758 } 759 } else if (params->cpus) { 760 /* 761 * Even if the user do not set a house-keeping CPU, try to 762 * move rtla to a CPU set different to the one where the user 763 * set the workload to run. 764 * 765 * No need to check results as this is an automatic attempt. 766 */ 767 auto_house_keeping(¶ms->monitored_cpus); 768 } 769 770 return 0; 771 772 out_err: 773 return -1; 774 } 775 776 /* 777 * osnoise_init_hist - initialize a osnoise hist tool with parameters 778 */ 779 static struct osnoise_tool 780 *osnoise_init_hist(struct osnoise_hist_params *params) 781 { 782 struct osnoise_tool *tool; 783 int nr_cpus; 784 785 nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 786 787 tool = osnoise_init_tool("osnoise_hist"); 788 if (!tool) 789 return NULL; 790 791 tool->data = osnoise_alloc_histogram(nr_cpus, params->entries, params->bucket_size); 792 if (!tool->data) 793 goto out_err; 794 795 tool->params = params; 796 797 return tool; 798 799 out_err: 800 osnoise_destroy_tool(tool); 801 return NULL; 802 } 803 804 static int stop_tracing; 805 static void stop_hist(int sig) 806 { 807 stop_tracing = 1; 808 } 809 810 /* 811 * osnoise_hist_set_signals - handles the signal to stop the tool 812 */ 813 static void 814 osnoise_hist_set_signals(struct osnoise_hist_params *params) 815 { 816 signal(SIGINT, stop_hist); 817 if (params->duration) { 818 signal(SIGALRM, stop_hist); 819 alarm(params->duration); 820 } 821 } 822 823 int osnoise_hist_main(int argc, char *argv[]) 824 { 825 struct osnoise_hist_params *params; 826 struct osnoise_tool *record = NULL; 827 struct osnoise_tool *tool = NULL; 828 struct trace_instance *trace; 829 int return_value = 1; 830 int retval; 831 832 params = osnoise_hist_parse_args(argc, argv); 833 if (!params) 834 exit(1); 835 836 tool = osnoise_init_hist(params); 837 if (!tool) { 838 err_msg("Could not init osnoise hist\n"); 839 goto out_exit; 840 } 841 842 retval = osnoise_hist_apply_config(tool, params); 843 if (retval) { 844 err_msg("Could not apply config\n"); 845 goto out_destroy; 846 } 847 848 trace = &tool->trace; 849 850 retval = enable_osnoise(trace); 851 if (retval) { 852 err_msg("Failed to enable osnoise tracer\n"); 853 goto out_destroy; 854 } 855 856 retval = osnoise_init_trace_hist(tool); 857 if (retval) 858 goto out_destroy; 859 860 if (params->set_sched) { 861 retval = set_comm_sched_attr("osnoise/", ¶ms->sched_param); 862 if (retval) { 863 err_msg("Failed to set sched parameters\n"); 864 goto out_free; 865 } 866 } 867 868 if (params->cgroup) { 869 retval = set_comm_cgroup("timerlat/", params->cgroup_name); 870 if (!retval) { 871 err_msg("Failed to move threads to cgroup\n"); 872 goto out_free; 873 } 874 } 875 876 if (params->trace_output) { 877 record = osnoise_init_trace_tool("osnoise"); 878 if (!record) { 879 err_msg("Failed to enable the trace instance\n"); 880 goto out_free; 881 } 882 883 if (params->events) { 884 retval = trace_events_enable(&record->trace, params->events); 885 if (retval) 886 goto out_hist; 887 } 888 889 } 890 891 /* 892 * Start the tracer here, after having set all instances. 893 * 894 * Let the trace instance start first for the case of hitting a stop 895 * tracing while enabling other instances. The trace instance is the 896 * one with most valuable information. 897 */ 898 if (params->trace_output) 899 trace_instance_start(&record->trace); 900 trace_instance_start(trace); 901 902 tool->start_time = time(NULL); 903 osnoise_hist_set_signals(params); 904 905 while (!stop_tracing) { 906 sleep(params->sleep_time); 907 908 retval = tracefs_iterate_raw_events(trace->tep, 909 trace->inst, 910 NULL, 911 0, 912 collect_registered_events, 913 trace); 914 if (retval < 0) { 915 err_msg("Error iterating on events\n"); 916 goto out_hist; 917 } 918 919 if (trace_is_off(&tool->trace, &record->trace)) 920 break; 921 } 922 923 osnoise_read_trace_hist(tool); 924 925 osnoise_print_stats(params, tool); 926 927 return_value = 0; 928 929 if (trace_is_off(&tool->trace, &record->trace)) { 930 printf("rtla osnoise hit stop tracing\n"); 931 if (params->trace_output) { 932 printf(" Saving trace to %s\n", params->trace_output); 933 save_trace_to_file(record->trace.inst, params->trace_output); 934 } 935 } 936 937 out_hist: 938 trace_events_destroy(&record->trace, params->events); 939 params->events = NULL; 940 out_free: 941 osnoise_free_histogram(tool->data); 942 out_destroy: 943 osnoise_destroy_tool(record); 944 osnoise_destroy_tool(tool); 945 free(params); 946 out_exit: 947 exit(return_value); 948 } 949