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 <stdio.h> 13 #include <time.h> 14 15 #include "osnoise.h" 16 17 struct osnoise_hist_cpu { 18 int *samples; 19 int count; 20 21 unsigned long long min_sample; 22 unsigned long long sum_sample; 23 unsigned long long max_sample; 24 25 }; 26 27 struct osnoise_hist_data { 28 struct tracefs_hist *trace_hist; 29 struct osnoise_hist_cpu *hist; 30 int entries; 31 int bucket_size; 32 }; 33 34 /* 35 * osnoise_free_histogram - free runtime data 36 */ 37 static void 38 osnoise_free_histogram(struct osnoise_hist_data *data) 39 { 40 int cpu; 41 42 /* one histogram for IRQ and one for thread, per CPU */ 43 for (cpu = 0; cpu < nr_cpus; cpu++) { 44 if (data->hist[cpu].samples) 45 free(data->hist[cpu].samples); 46 } 47 48 /* one set of histograms per CPU */ 49 if (data->hist) 50 free(data->hist); 51 52 free(data); 53 } 54 55 static void osnoise_free_hist_tool(struct osnoise_tool *tool) 56 { 57 osnoise_free_histogram(tool->data); 58 } 59 60 /* 61 * osnoise_alloc_histogram - alloc runtime data 62 */ 63 static struct osnoise_hist_data 64 *osnoise_alloc_histogram(int entries, int bucket_size) 65 { 66 struct osnoise_hist_data *data; 67 int cpu; 68 69 data = calloc(1, sizeof(*data)); 70 if (!data) 71 return NULL; 72 73 data->entries = entries; 74 data->bucket_size = bucket_size; 75 76 data->hist = calloc(1, sizeof(*data->hist) * nr_cpus); 77 if (!data->hist) 78 goto cleanup; 79 80 for (cpu = 0; cpu < nr_cpus; cpu++) { 81 data->hist[cpu].samples = calloc(1, sizeof(*data->hist->samples) * (entries + 1)); 82 if (!data->hist[cpu].samples) 83 goto cleanup; 84 } 85 86 /* set the min to max */ 87 for (cpu = 0; cpu < nr_cpus; cpu++) 88 data->hist[cpu].min_sample = ~0; 89 90 return data; 91 92 cleanup: 93 osnoise_free_histogram(data); 94 return NULL; 95 } 96 97 static void osnoise_hist_update_multiple(struct osnoise_tool *tool, int cpu, 98 unsigned long long duration, int count) 99 { 100 struct osnoise_params *params = to_osnoise_params(tool->params); 101 struct osnoise_hist_data *data = tool->data; 102 unsigned long long total_duration; 103 int entries = data->entries; 104 int bucket; 105 int *hist; 106 107 if (params->common.output_divisor) 108 duration = duration / params->common.output_divisor; 109 110 bucket = duration / data->bucket_size; 111 112 total_duration = duration * count; 113 114 hist = data->hist[cpu].samples; 115 data->hist[cpu].count += count; 116 update_min(&data->hist[cpu].min_sample, &duration); 117 update_sum(&data->hist[cpu].sum_sample, &total_duration); 118 update_max(&data->hist[cpu].max_sample, &duration); 119 120 if (bucket < entries) 121 hist[bucket] += count; 122 else 123 hist[entries] += count; 124 } 125 126 /* 127 * osnoise_destroy_trace_hist - disable events used to collect histogram 128 */ 129 static void osnoise_destroy_trace_hist(struct osnoise_tool *tool) 130 { 131 struct osnoise_hist_data *data = tool->data; 132 133 tracefs_hist_pause(tool->trace.inst, data->trace_hist); 134 tracefs_hist_destroy(tool->trace.inst, data->trace_hist); 135 } 136 137 /* 138 * osnoise_init_trace_hist - enable events used to collect histogram 139 */ 140 static int osnoise_init_trace_hist(struct osnoise_tool *tool) 141 { 142 struct osnoise_params *params = to_osnoise_params(tool->params); 143 struct osnoise_hist_data *data = tool->data; 144 int bucket_size; 145 char buff[128]; 146 int retval = 0; 147 148 /* 149 * Set the size of the bucket. 150 */ 151 bucket_size = params->common.output_divisor * params->common.hist.bucket_size; 152 snprintf(buff, sizeof(buff), "duration.buckets=%d", bucket_size); 153 154 data->trace_hist = tracefs_hist_alloc(tool->trace.tep, "osnoise", "sample_threshold", 155 buff, TRACEFS_HIST_KEY_NORMAL); 156 if (!data->trace_hist) 157 return 1; 158 159 retval = tracefs_hist_add_key(data->trace_hist, "cpu", 0); 160 if (retval) 161 goto out_err; 162 163 retval = tracefs_hist_start(tool->trace.inst, data->trace_hist); 164 if (retval) 165 goto out_err; 166 167 return 0; 168 169 out_err: 170 osnoise_destroy_trace_hist(tool); 171 return 1; 172 } 173 174 /* 175 * osnoise_read_trace_hist - parse histogram file and file osnoise histogram 176 */ 177 static void osnoise_read_trace_hist(struct osnoise_tool *tool) 178 { 179 struct osnoise_hist_data *data = tool->data; 180 long long cpu, counter, duration; 181 char *content, *position; 182 183 tracefs_hist_pause(tool->trace.inst, data->trace_hist); 184 185 content = tracefs_event_file_read(tool->trace.inst, "osnoise", 186 "sample_threshold", 187 "hist", NULL); 188 if (!content) 189 return; 190 191 position = content; 192 while (true) { 193 position = strstr(position, "duration: ~"); 194 if (!position) 195 break; 196 position += strlen("duration: ~"); 197 duration = get_llong_from_str(position); 198 if (duration == -1) 199 err_msg("error reading duration from histogram\n"); 200 201 position = strstr(position, "cpu:"); 202 if (!position) 203 break; 204 position += strlen("cpu: "); 205 cpu = get_llong_from_str(position); 206 if (cpu == -1) 207 err_msg("error reading cpu from histogram\n"); 208 209 position = strstr(position, "hitcount:"); 210 if (!position) 211 break; 212 position += strlen("hitcount: "); 213 counter = get_llong_from_str(position); 214 if (counter == -1) 215 err_msg("error reading counter from histogram\n"); 216 217 osnoise_hist_update_multiple(tool, cpu, duration, counter); 218 } 219 free(content); 220 } 221 222 /* 223 * osnoise_hist_header - print the header of the tracer to the output 224 */ 225 static void osnoise_hist_header(struct osnoise_tool *tool) 226 { 227 struct osnoise_params *params = to_osnoise_params(tool->params); 228 struct osnoise_hist_data *data = tool->data; 229 struct trace_seq *s = tool->trace.seq; 230 char duration[26]; 231 int cpu; 232 233 if (params->common.hist.no_header) 234 return; 235 236 get_duration(tool->start_time, duration, sizeof(duration)); 237 trace_seq_printf(s, "# RTLA osnoise histogram\n"); 238 trace_seq_printf(s, "# Time unit is %s (%s)\n", 239 params->common.output_divisor == 1 ? "nanoseconds" : "microseconds", 240 params->common.output_divisor == 1 ? "ns" : "us"); 241 242 trace_seq_printf(s, "# Duration: %s\n", duration); 243 244 if (!params->common.hist.no_index) 245 trace_seq_printf(s, "Index"); 246 247 for_each_monitored_cpu(cpu, ¶ms->common) { 248 249 if (!data->hist[cpu].count) 250 continue; 251 252 trace_seq_printf(s, " CPU-%03d", cpu); 253 } 254 trace_seq_printf(s, "\n"); 255 256 trace_seq_do_printf(s); 257 trace_seq_reset(s); 258 } 259 260 /* 261 * osnoise_print_summary - print the summary of the hist data to the output 262 */ 263 static void 264 osnoise_print_summary(struct osnoise_params *params, 265 struct trace_instance *trace, 266 struct osnoise_hist_data *data) 267 { 268 int cpu; 269 270 if (params->common.hist.no_summary) 271 return; 272 273 if (!params->common.hist.no_index) 274 trace_seq_printf(trace->seq, "count:"); 275 276 for_each_monitored_cpu(cpu, ¶ms->common) { 277 if (!data->hist[cpu].count) 278 continue; 279 280 trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].count); 281 } 282 trace_seq_printf(trace->seq, "\n"); 283 284 if (!params->common.hist.no_index) 285 trace_seq_printf(trace->seq, "min: "); 286 287 for_each_monitored_cpu(cpu, ¶ms->common) { 288 289 if (!data->hist[cpu].count) 290 continue; 291 292 trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].min_sample); 293 294 } 295 trace_seq_printf(trace->seq, "\n"); 296 297 if (!params->common.hist.no_index) 298 trace_seq_printf(trace->seq, "avg: "); 299 300 for_each_monitored_cpu(cpu, ¶ms->common) { 301 302 if (!data->hist[cpu].count) 303 continue; 304 305 if (data->hist[cpu].count) 306 trace_seq_printf(trace->seq, "%9.2f ", 307 ((double) data->hist[cpu].sum_sample) / data->hist[cpu].count); 308 else 309 trace_seq_printf(trace->seq, " - "); 310 } 311 trace_seq_printf(trace->seq, "\n"); 312 313 if (!params->common.hist.no_index) 314 trace_seq_printf(trace->seq, "max: "); 315 316 for_each_monitored_cpu(cpu, ¶ms->common) { 317 318 if (!data->hist[cpu].count) 319 continue; 320 321 trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].max_sample); 322 323 } 324 trace_seq_printf(trace->seq, "\n"); 325 trace_seq_do_printf(trace->seq); 326 trace_seq_reset(trace->seq); 327 } 328 329 /* 330 * osnoise_print_stats - print data for all CPUs 331 */ 332 static void 333 osnoise_print_stats(struct osnoise_tool *tool) 334 { 335 struct osnoise_params *params = to_osnoise_params(tool->params); 336 struct osnoise_hist_data *data = tool->data; 337 struct trace_instance *trace = &tool->trace; 338 int has_samples = 0; 339 int bucket, cpu; 340 int total; 341 342 osnoise_hist_header(tool); 343 344 for (bucket = 0; bucket < data->entries; bucket++) { 345 total = 0; 346 347 if (!params->common.hist.no_index) 348 trace_seq_printf(trace->seq, "%-6d", 349 bucket * data->bucket_size); 350 351 for_each_monitored_cpu(cpu, ¶ms->common) { 352 353 if (!data->hist[cpu].count) 354 continue; 355 356 total += data->hist[cpu].samples[bucket]; 357 trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].samples[bucket]); 358 } 359 360 if (total == 0 && !params->common.hist.with_zeros) { 361 trace_seq_reset(trace->seq); 362 continue; 363 } 364 365 /* There are samples above the threshold */ 366 has_samples = 1; 367 trace_seq_printf(trace->seq, "\n"); 368 trace_seq_do_printf(trace->seq); 369 trace_seq_reset(trace->seq); 370 } 371 372 /* 373 * If no samples were recorded, skip calculations, print zeroed statistics 374 * and return. 375 */ 376 if (!has_samples) { 377 trace_seq_reset(trace->seq); 378 trace_seq_printf(trace->seq, "over: 0\ncount: 0\nmin: 0\navg: 0\nmax: 0\n"); 379 trace_seq_do_printf(trace->seq); 380 trace_seq_reset(trace->seq); 381 return; 382 } 383 384 if (!params->common.hist.no_index) 385 trace_seq_printf(trace->seq, "over: "); 386 387 for_each_monitored_cpu(cpu, ¶ms->common) { 388 389 if (!data->hist[cpu].count) 390 continue; 391 392 trace_seq_printf(trace->seq, "%9d ", 393 data->hist[cpu].samples[data->entries]); 394 } 395 trace_seq_printf(trace->seq, "\n"); 396 trace_seq_do_printf(trace->seq); 397 trace_seq_reset(trace->seq); 398 399 osnoise_print_summary(params, trace, data); 400 osnoise_report_missed_events(tool); 401 } 402 403 /* 404 * osnoise_hist_usage - prints osnoise hist usage message 405 */ 406 static void osnoise_hist_usage(void) 407 { 408 static const char * const msg_start[] = { 409 "[-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\", 410 " [-T us] [-t [file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\", 411 " [-c cpu-list] [-H cpu-list] [-P priority] [-b N] [-E N] [--no-header] [--no-summary] \\", 412 " [--no-index] [--with-zeros] [-C [cgroup_name]] [--warm-up]", 413 NULL, 414 }; 415 416 static const char * const msg_opts[] = { 417 " -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit", 418 " -p/--period us: osnoise period in us", 419 " -r/--runtime us: osnoise runtime in us", 420 " -s/--stop us: stop trace if a single sample is higher than the argument in us", 421 " -S/--stop-total us: stop trace if the total sample is higher than the argument in us", 422 " -T/--threshold us: the minimum delta to be considered a noise", 423 " -c/--cpus cpu-list: list of cpus to run osnoise threads", 424 " -H/--house-keeping cpus: run rtla control threads only on the given cpus", 425 " -C/--cgroup [cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited", 426 " -d/--duration time[s|m|h|d]: duration of the session", 427 " -D/--debug: print debug info", 428 " -t/--trace [file]: save the stopped trace to [file|osnoise_trace.txt]", 429 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed", 430 " --filter <filter>: enable a trace event filter to the previous -e event", 431 " --trigger <trigger>: enable a trace event trigger to the previous -e event", 432 " -b/--bucket-size N: set the histogram bucket size (default 1)", 433 " -E/--entries N: set the number of entries of the histogram (default 256)", 434 " --no-header: do not print header", 435 " --no-summary: do not print summary", 436 " --no-index: do not print index", 437 " --with-zeros: print zero only entries", 438 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period: set scheduling parameters", 439 " o:prio - use SCHED_OTHER with prio", 440 " r:prio - use SCHED_RR with prio", 441 " f:prio - use SCHED_FIFO with prio", 442 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period", 443 " in nanoseconds", 444 " --warm-up: let the workload run for s seconds before collecting data", 445 " --trace-buffer-size kB: set the per-cpu trace buffer size in kB", 446 " --on-threshold <action>: define action to be executed at stop-total threshold, multiple are allowed", 447 " --on-end <action>: define action to be executed at measurement end, multiple are allowed", 448 NULL, 449 }; 450 451 common_usage("osnoise", "hist", "a per-cpu histogram of the OS noise", 452 msg_start, msg_opts); 453 } 454 455 /* 456 * osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters 457 */ 458 static struct common_params 459 *osnoise_hist_parse_args(int argc, char *argv[]) 460 { 461 struct osnoise_params *params; 462 int retval; 463 int c; 464 char *trace_output = NULL; 465 466 params = calloc_fatal(1, sizeof(*params)); 467 468 actions_init(¶ms->common.threshold_actions); 469 actions_init(¶ms->common.end_actions); 470 471 /* display data in microseconds */ 472 params->common.output_divisor = 1000; 473 params->common.hist.bucket_size = 1; 474 params->common.hist.entries = 256; 475 476 while (1) { 477 static struct option long_options[] = { 478 {"auto", required_argument, 0, 'a'}, 479 {"bucket-size", required_argument, 0, 'b'}, 480 {"entries", required_argument, 0, 'E'}, 481 {"help", no_argument, 0, 'h'}, 482 {"period", required_argument, 0, 'p'}, 483 {"runtime", required_argument, 0, 'r'}, 484 {"stop", required_argument, 0, 's'}, 485 {"stop-total", required_argument, 0, 'S'}, 486 {"trace", optional_argument, 0, 't'}, 487 {"threshold", required_argument, 0, 'T'}, 488 {"no-header", no_argument, 0, '0'}, 489 {"no-summary", no_argument, 0, '1'}, 490 {"no-index", no_argument, 0, '2'}, 491 {"with-zeros", no_argument, 0, '3'}, 492 {"trigger", required_argument, 0, '4'}, 493 {"filter", required_argument, 0, '5'}, 494 {"warm-up", required_argument, 0, '6'}, 495 {"trace-buffer-size", required_argument, 0, '7'}, 496 {"on-threshold", required_argument, 0, '8'}, 497 {"on-end", required_argument, 0, '9'}, 498 {0, 0, 0, 0} 499 }; 500 501 if (common_parse_options(argc, argv, ¶ms->common)) 502 continue; 503 504 c = getopt_auto(argc, argv, long_options); 505 506 /* detect the end of the options. */ 507 if (c == -1) 508 break; 509 510 switch (c) { 511 case 'a': 512 /* set sample stop to auto_thresh */ 513 params->common.stop_us = get_llong_from_str(optarg); 514 515 /* set sample threshold to 1 */ 516 params->threshold = 1; 517 518 /* set trace */ 519 if (!trace_output) 520 trace_output = "osnoise_trace.txt"; 521 522 break; 523 case 'b': 524 params->common.hist.bucket_size = get_llong_from_str(optarg); 525 if (params->common.hist.bucket_size == 0 || 526 params->common.hist.bucket_size >= 1000000) 527 fatal("Bucket size needs to be > 0 and <= 1000000"); 528 break; 529 case 'E': 530 params->common.hist.entries = get_llong_from_str(optarg); 531 if (params->common.hist.entries < 10 || 532 params->common.hist.entries > 9999999) 533 fatal("Entries must be > 10 and < 9999999"); 534 break; 535 case 'h': 536 case '?': 537 osnoise_hist_usage(); 538 break; 539 case 'p': 540 params->period = get_llong_from_str(optarg); 541 if (params->period > 10000000) 542 fatal("Period longer than 10 s"); 543 break; 544 case 'r': 545 params->runtime = get_llong_from_str(optarg); 546 if (params->runtime < 100) 547 fatal("Runtime shorter than 100 us"); 548 break; 549 case 's': 550 params->common.stop_us = get_llong_from_str(optarg); 551 break; 552 case 'S': 553 params->common.stop_total_us = get_llong_from_str(optarg); 554 break; 555 case 'T': 556 params->threshold = get_llong_from_str(optarg); 557 break; 558 case 't': 559 trace_output = parse_optional_arg(argc, argv); 560 if (!trace_output) 561 trace_output = "osnoise_trace.txt"; 562 break; 563 case '0': /* no header */ 564 params->common.hist.no_header = 1; 565 break; 566 case '1': /* no summary */ 567 params->common.hist.no_summary = 1; 568 break; 569 case '2': /* no index */ 570 params->common.hist.no_index = 1; 571 break; 572 case '3': /* with zeros */ 573 params->common.hist.with_zeros = 1; 574 break; 575 case '4': /* trigger */ 576 if (params->common.events) 577 trace_event_add_trigger(params->common.events, optarg); 578 else 579 fatal("--trigger requires a previous -e"); 580 break; 581 case '5': /* filter */ 582 if (params->common.events) 583 trace_event_add_filter(params->common.events, optarg); 584 else 585 fatal("--filter requires a previous -e"); 586 break; 587 case '6': 588 params->common.warmup = get_llong_from_str(optarg); 589 break; 590 case '7': 591 params->common.buffer_size = get_llong_from_str(optarg); 592 break; 593 case '8': 594 retval = actions_parse(¶ms->common.threshold_actions, optarg, 595 "osnoise_trace.txt"); 596 if (retval) 597 fatal("Invalid action %s", optarg); 598 break; 599 case '9': 600 retval = actions_parse(¶ms->common.end_actions, optarg, 601 "osnoise_trace.txt"); 602 if (retval) 603 fatal("Invalid action %s", optarg); 604 break; 605 default: 606 fatal("Invalid option"); 607 } 608 } 609 610 if (trace_output) 611 actions_add_trace_output(¶ms->common.threshold_actions, trace_output); 612 613 if (geteuid()) 614 fatal("rtla needs root permission"); 615 616 if (params->common.hist.no_index && !params->common.hist.with_zeros) 617 fatal("no-index set and with-zeros not set - it does not make sense"); 618 619 return ¶ms->common; 620 } 621 622 /* 623 * osnoise_hist_apply_config - apply the hist configs to the initialized tool 624 */ 625 static int 626 osnoise_hist_apply_config(struct osnoise_tool *tool) 627 { 628 return osnoise_apply_config(tool, to_osnoise_params(tool->params)); 629 } 630 631 /* 632 * osnoise_init_hist - initialize a osnoise hist tool with parameters 633 */ 634 static struct osnoise_tool 635 *osnoise_init_hist(struct common_params *params) 636 { 637 struct osnoise_tool *tool; 638 639 tool = osnoise_init_tool("osnoise_hist"); 640 if (!tool) 641 return NULL; 642 643 tool->data = osnoise_alloc_histogram(params->hist.entries, 644 params->hist.bucket_size); 645 if (!tool->data) 646 goto out_err; 647 648 return tool; 649 650 out_err: 651 osnoise_destroy_tool(tool); 652 return NULL; 653 } 654 655 static int osnoise_hist_enable(struct osnoise_tool *tool) 656 { 657 int retval; 658 659 retval = osnoise_init_trace_hist(tool); 660 if (retval) 661 return retval; 662 663 return osnoise_enable(tool); 664 } 665 666 static int osnoise_hist_main_loop(struct osnoise_tool *tool) 667 { 668 int retval; 669 670 retval = hist_main_loop(tool); 671 osnoise_read_trace_hist(tool); 672 673 return retval; 674 } 675 676 struct tool_ops osnoise_hist_ops = { 677 .tracer = "osnoise", 678 .comm_prefix = "osnoise/", 679 .parse_args = osnoise_hist_parse_args, 680 .init_tool = osnoise_init_hist, 681 .apply_config = osnoise_hist_apply_config, 682 .enable = osnoise_hist_enable, 683 .main = osnoise_hist_main_loop, 684 .print_stats = osnoise_print_stats, 685 .free = osnoise_free_hist_tool, 686 }; 687