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 #include <sched.h> 15 #include <pthread.h> 16 17 #include "utils.h" 18 #include "osnoise.h" 19 #include "timerlat.h" 20 #include "timerlat_aa.h" 21 #include "timerlat_u.h" 22 23 struct timerlat_hist_params { 24 char *cpus; 25 cpu_set_t monitored_cpus; 26 char *trace_output; 27 char *cgroup_name; 28 unsigned long long runtime; 29 long long stop_us; 30 long long stop_total_us; 31 long long timerlat_period_us; 32 long long print_stack; 33 int sleep_time; 34 int output_divisor; 35 int duration; 36 int set_sched; 37 int dma_latency; 38 int cgroup; 39 int hk_cpus; 40 int no_aa; 41 int dump_tasks; 42 int user_workload; 43 int kernel_workload; 44 int user_hist; 45 cpu_set_t hk_cpu_set; 46 struct sched_attr sched_param; 47 struct trace_events *events; 48 char no_irq; 49 char no_thread; 50 char no_header; 51 char no_summary; 52 char no_index; 53 char with_zeros; 54 int bucket_size; 55 int entries; 56 int warmup; 57 int buffer_size; 58 int deepest_idle_state; 59 }; 60 61 struct timerlat_hist_cpu { 62 int *irq; 63 int *thread; 64 int *user; 65 66 unsigned long long irq_count; 67 unsigned long long thread_count; 68 unsigned long long user_count; 69 70 unsigned long long min_irq; 71 unsigned long long sum_irq; 72 unsigned long long max_irq; 73 74 unsigned long long min_thread; 75 unsigned long long sum_thread; 76 unsigned long long max_thread; 77 78 unsigned long long min_user; 79 unsigned long long sum_user; 80 unsigned long long max_user; 81 }; 82 83 struct timerlat_hist_data { 84 struct timerlat_hist_cpu *hist; 85 int entries; 86 int bucket_size; 87 int nr_cpus; 88 }; 89 90 /* 91 * timerlat_free_histogram - free runtime data 92 */ 93 static void 94 timerlat_free_histogram(struct timerlat_hist_data *data) 95 { 96 int cpu; 97 98 /* one histogram for IRQ and one for thread, per CPU */ 99 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 100 if (data->hist[cpu].irq) 101 free(data->hist[cpu].irq); 102 103 if (data->hist[cpu].thread) 104 free(data->hist[cpu].thread); 105 106 if (data->hist[cpu].user) 107 free(data->hist[cpu].user); 108 109 } 110 111 /* one set of histograms per CPU */ 112 if (data->hist) 113 free(data->hist); 114 115 free(data); 116 } 117 118 /* 119 * timerlat_alloc_histogram - alloc runtime data 120 */ 121 static struct timerlat_hist_data 122 *timerlat_alloc_histogram(int nr_cpus, int entries, int bucket_size) 123 { 124 struct timerlat_hist_data *data; 125 int cpu; 126 127 data = calloc(1, sizeof(*data)); 128 if (!data) 129 return NULL; 130 131 data->entries = entries; 132 data->bucket_size = bucket_size; 133 data->nr_cpus = nr_cpus; 134 135 /* one set of histograms per CPU */ 136 data->hist = calloc(1, sizeof(*data->hist) * nr_cpus); 137 if (!data->hist) 138 goto cleanup; 139 140 /* one histogram for IRQ and one for thread, per cpu */ 141 for (cpu = 0; cpu < nr_cpus; cpu++) { 142 data->hist[cpu].irq = calloc(1, sizeof(*data->hist->irq) * (entries + 1)); 143 if (!data->hist[cpu].irq) 144 goto cleanup; 145 146 data->hist[cpu].thread = calloc(1, sizeof(*data->hist->thread) * (entries + 1)); 147 if (!data->hist[cpu].thread) 148 goto cleanup; 149 150 data->hist[cpu].user = calloc(1, sizeof(*data->hist->user) * (entries + 1)); 151 if (!data->hist[cpu].user) 152 goto cleanup; 153 } 154 155 /* set the min to max */ 156 for (cpu = 0; cpu < nr_cpus; cpu++) { 157 data->hist[cpu].min_irq = ~0; 158 data->hist[cpu].min_thread = ~0; 159 data->hist[cpu].min_user = ~0; 160 } 161 162 return data; 163 164 cleanup: 165 timerlat_free_histogram(data); 166 return NULL; 167 } 168 169 /* 170 * timerlat_hist_update - record a new timerlat occurent on cpu, updating data 171 */ 172 static void 173 timerlat_hist_update(struct osnoise_tool *tool, int cpu, 174 unsigned long long context, 175 unsigned long long latency) 176 { 177 struct timerlat_hist_params *params = tool->params; 178 struct timerlat_hist_data *data = tool->data; 179 int entries = data->entries; 180 int bucket; 181 int *hist; 182 183 if (params->output_divisor) 184 latency = latency / params->output_divisor; 185 186 bucket = latency / data->bucket_size; 187 188 if (!context) { 189 hist = data->hist[cpu].irq; 190 data->hist[cpu].irq_count++; 191 update_min(&data->hist[cpu].min_irq, &latency); 192 update_sum(&data->hist[cpu].sum_irq, &latency); 193 update_max(&data->hist[cpu].max_irq, &latency); 194 } else if (context == 1) { 195 hist = data->hist[cpu].thread; 196 data->hist[cpu].thread_count++; 197 update_min(&data->hist[cpu].min_thread, &latency); 198 update_sum(&data->hist[cpu].sum_thread, &latency); 199 update_max(&data->hist[cpu].max_thread, &latency); 200 } else { /* user */ 201 hist = data->hist[cpu].user; 202 data->hist[cpu].user_count++; 203 update_min(&data->hist[cpu].min_user, &latency); 204 update_sum(&data->hist[cpu].sum_user, &latency); 205 update_max(&data->hist[cpu].max_user, &latency); 206 } 207 208 if (bucket < entries) 209 hist[bucket]++; 210 else 211 hist[entries]++; 212 } 213 214 /* 215 * timerlat_hist_handler - this is the handler for timerlat tracer events 216 */ 217 static int 218 timerlat_hist_handler(struct trace_seq *s, struct tep_record *record, 219 struct tep_event *event, void *data) 220 { 221 struct trace_instance *trace = data; 222 unsigned long long context, latency; 223 struct osnoise_tool *tool; 224 int cpu = record->cpu; 225 226 tool = container_of(trace, struct osnoise_tool, trace); 227 228 tep_get_field_val(s, event, "context", record, &context, 1); 229 tep_get_field_val(s, event, "timer_latency", record, &latency, 1); 230 231 timerlat_hist_update(tool, cpu, context, latency); 232 233 return 0; 234 } 235 236 /* 237 * timerlat_hist_header - print the header of the tracer to the output 238 */ 239 static void timerlat_hist_header(struct osnoise_tool *tool) 240 { 241 struct timerlat_hist_params *params = tool->params; 242 struct timerlat_hist_data *data = tool->data; 243 struct trace_seq *s = tool->trace.seq; 244 char duration[26]; 245 int cpu; 246 247 if (params->no_header) 248 return; 249 250 get_duration(tool->start_time, duration, sizeof(duration)); 251 trace_seq_printf(s, "# RTLA timerlat histogram\n"); 252 trace_seq_printf(s, "# Time unit is %s (%s)\n", 253 params->output_divisor == 1 ? "nanoseconds" : "microseconds", 254 params->output_divisor == 1 ? "ns" : "us"); 255 256 trace_seq_printf(s, "# Duration: %s\n", duration); 257 258 if (!params->no_index) 259 trace_seq_printf(s, "Index"); 260 261 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 262 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 263 continue; 264 265 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 266 continue; 267 268 if (!params->no_irq) 269 trace_seq_printf(s, " IRQ-%03d", cpu); 270 271 if (!params->no_thread) 272 trace_seq_printf(s, " Thr-%03d", cpu); 273 274 if (params->user_hist) 275 trace_seq_printf(s, " Usr-%03d", cpu); 276 } 277 trace_seq_printf(s, "\n"); 278 279 280 trace_seq_do_printf(s); 281 trace_seq_reset(s); 282 } 283 284 /* 285 * format_summary_value - format a line of summary value (min, max or avg) 286 * of hist data 287 */ 288 static void format_summary_value(struct trace_seq *seq, 289 int count, 290 unsigned long long val, 291 bool avg) 292 { 293 if (count) 294 trace_seq_printf(seq, "%9llu ", avg ? val / count : val); 295 else 296 trace_seq_printf(seq, "%9c ", '-'); 297 } 298 299 /* 300 * timerlat_print_summary - print the summary of the hist data to the output 301 */ 302 static void 303 timerlat_print_summary(struct timerlat_hist_params *params, 304 struct trace_instance *trace, 305 struct timerlat_hist_data *data) 306 { 307 int cpu; 308 309 if (params->no_summary) 310 return; 311 312 if (!params->no_index) 313 trace_seq_printf(trace->seq, "count:"); 314 315 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 316 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 317 continue; 318 319 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 320 continue; 321 322 if (!params->no_irq) 323 trace_seq_printf(trace->seq, "%9llu ", 324 data->hist[cpu].irq_count); 325 326 if (!params->no_thread) 327 trace_seq_printf(trace->seq, "%9llu ", 328 data->hist[cpu].thread_count); 329 330 if (params->user_hist) 331 trace_seq_printf(trace->seq, "%9llu ", 332 data->hist[cpu].user_count); 333 } 334 trace_seq_printf(trace->seq, "\n"); 335 336 if (!params->no_index) 337 trace_seq_printf(trace->seq, "min: "); 338 339 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 340 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 341 continue; 342 343 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 344 continue; 345 346 if (!params->no_irq) 347 format_summary_value(trace->seq, 348 data->hist[cpu].irq_count, 349 data->hist[cpu].min_irq, 350 false); 351 352 if (!params->no_thread) 353 format_summary_value(trace->seq, 354 data->hist[cpu].thread_count, 355 data->hist[cpu].min_thread, 356 false); 357 358 if (params->user_hist) 359 format_summary_value(trace->seq, 360 data->hist[cpu].user_count, 361 data->hist[cpu].min_user, 362 false); 363 } 364 trace_seq_printf(trace->seq, "\n"); 365 366 if (!params->no_index) 367 trace_seq_printf(trace->seq, "avg: "); 368 369 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 370 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 371 continue; 372 373 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 374 continue; 375 376 if (!params->no_irq) 377 format_summary_value(trace->seq, 378 data->hist[cpu].irq_count, 379 data->hist[cpu].sum_irq, 380 true); 381 382 if (!params->no_thread) 383 format_summary_value(trace->seq, 384 data->hist[cpu].thread_count, 385 data->hist[cpu].sum_thread, 386 true); 387 388 if (params->user_hist) 389 format_summary_value(trace->seq, 390 data->hist[cpu].user_count, 391 data->hist[cpu].sum_user, 392 true); 393 } 394 trace_seq_printf(trace->seq, "\n"); 395 396 if (!params->no_index) 397 trace_seq_printf(trace->seq, "max: "); 398 399 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 400 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 401 continue; 402 403 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 404 continue; 405 406 if (!params->no_irq) 407 format_summary_value(trace->seq, 408 data->hist[cpu].irq_count, 409 data->hist[cpu].max_irq, 410 false); 411 412 if (!params->no_thread) 413 format_summary_value(trace->seq, 414 data->hist[cpu].thread_count, 415 data->hist[cpu].max_thread, 416 false); 417 418 if (params->user_hist) 419 format_summary_value(trace->seq, 420 data->hist[cpu].user_count, 421 data->hist[cpu].max_user, 422 false); 423 } 424 trace_seq_printf(trace->seq, "\n"); 425 trace_seq_do_printf(trace->seq); 426 trace_seq_reset(trace->seq); 427 } 428 429 static void 430 timerlat_print_stats_all(struct timerlat_hist_params *params, 431 struct trace_instance *trace, 432 struct timerlat_hist_data *data) 433 { 434 struct timerlat_hist_cpu *cpu_data; 435 struct timerlat_hist_cpu sum; 436 int cpu; 437 438 if (params->no_summary) 439 return; 440 441 memset(&sum, 0, sizeof(sum)); 442 sum.min_irq = ~0; 443 sum.min_thread = ~0; 444 sum.min_user = ~0; 445 446 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 447 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 448 continue; 449 450 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 451 continue; 452 453 cpu_data = &data->hist[cpu]; 454 455 sum.irq_count += cpu_data->irq_count; 456 update_min(&sum.min_irq, &cpu_data->min_irq); 457 update_sum(&sum.sum_irq, &cpu_data->sum_irq); 458 update_max(&sum.max_irq, &cpu_data->max_irq); 459 460 sum.thread_count += cpu_data->thread_count; 461 update_min(&sum.min_thread, &cpu_data->min_thread); 462 update_sum(&sum.sum_thread, &cpu_data->sum_thread); 463 update_max(&sum.max_thread, &cpu_data->max_thread); 464 465 sum.user_count += cpu_data->user_count; 466 update_min(&sum.min_user, &cpu_data->min_user); 467 update_sum(&sum.sum_user, &cpu_data->sum_user); 468 update_max(&sum.max_user, &cpu_data->max_user); 469 } 470 471 if (!params->no_index) 472 trace_seq_printf(trace->seq, "ALL: "); 473 474 if (!params->no_irq) 475 trace_seq_printf(trace->seq, " IRQ"); 476 477 if (!params->no_thread) 478 trace_seq_printf(trace->seq, " Thr"); 479 480 if (params->user_hist) 481 trace_seq_printf(trace->seq, " Usr"); 482 483 trace_seq_printf(trace->seq, "\n"); 484 485 if (!params->no_index) 486 trace_seq_printf(trace->seq, "count:"); 487 488 if (!params->no_irq) 489 trace_seq_printf(trace->seq, "%9llu ", 490 sum.irq_count); 491 492 if (!params->no_thread) 493 trace_seq_printf(trace->seq, "%9llu ", 494 sum.thread_count); 495 496 if (params->user_hist) 497 trace_seq_printf(trace->seq, "%9llu ", 498 sum.user_count); 499 500 trace_seq_printf(trace->seq, "\n"); 501 502 if (!params->no_index) 503 trace_seq_printf(trace->seq, "min: "); 504 505 if (!params->no_irq) 506 format_summary_value(trace->seq, 507 sum.irq_count, 508 sum.min_irq, 509 false); 510 511 if (!params->no_thread) 512 format_summary_value(trace->seq, 513 sum.thread_count, 514 sum.min_thread, 515 false); 516 517 if (params->user_hist) 518 format_summary_value(trace->seq, 519 sum.user_count, 520 sum.min_user, 521 false); 522 523 trace_seq_printf(trace->seq, "\n"); 524 525 if (!params->no_index) 526 trace_seq_printf(trace->seq, "avg: "); 527 528 if (!params->no_irq) 529 format_summary_value(trace->seq, 530 sum.irq_count, 531 sum.sum_irq, 532 true); 533 534 if (!params->no_thread) 535 format_summary_value(trace->seq, 536 sum.thread_count, 537 sum.sum_thread, 538 true); 539 540 if (params->user_hist) 541 format_summary_value(trace->seq, 542 sum.user_count, 543 sum.sum_user, 544 true); 545 546 trace_seq_printf(trace->seq, "\n"); 547 548 if (!params->no_index) 549 trace_seq_printf(trace->seq, "max: "); 550 551 if (!params->no_irq) 552 format_summary_value(trace->seq, 553 sum.irq_count, 554 sum.max_irq, 555 false); 556 557 if (!params->no_thread) 558 format_summary_value(trace->seq, 559 sum.thread_count, 560 sum.max_thread, 561 false); 562 563 if (params->user_hist) 564 format_summary_value(trace->seq, 565 sum.user_count, 566 sum.max_user, 567 false); 568 569 trace_seq_printf(trace->seq, "\n"); 570 trace_seq_do_printf(trace->seq); 571 trace_seq_reset(trace->seq); 572 } 573 574 /* 575 * timerlat_print_stats - print data for each CPUs 576 */ 577 static void 578 timerlat_print_stats(struct timerlat_hist_params *params, struct osnoise_tool *tool) 579 { 580 struct timerlat_hist_data *data = tool->data; 581 struct trace_instance *trace = &tool->trace; 582 int bucket, cpu; 583 int total; 584 585 timerlat_hist_header(tool); 586 587 for (bucket = 0; bucket < data->entries; bucket++) { 588 total = 0; 589 590 if (!params->no_index) 591 trace_seq_printf(trace->seq, "%-6d", 592 bucket * data->bucket_size); 593 594 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 595 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 596 continue; 597 598 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 599 continue; 600 601 if (!params->no_irq) { 602 total += data->hist[cpu].irq[bucket]; 603 trace_seq_printf(trace->seq, "%9d ", 604 data->hist[cpu].irq[bucket]); 605 } 606 607 if (!params->no_thread) { 608 total += data->hist[cpu].thread[bucket]; 609 trace_seq_printf(trace->seq, "%9d ", 610 data->hist[cpu].thread[bucket]); 611 } 612 613 if (params->user_hist) { 614 total += data->hist[cpu].user[bucket]; 615 trace_seq_printf(trace->seq, "%9d ", 616 data->hist[cpu].user[bucket]); 617 } 618 619 } 620 621 if (total == 0 && !params->with_zeros) { 622 trace_seq_reset(trace->seq); 623 continue; 624 } 625 626 trace_seq_printf(trace->seq, "\n"); 627 trace_seq_do_printf(trace->seq); 628 trace_seq_reset(trace->seq); 629 } 630 631 if (!params->no_index) 632 trace_seq_printf(trace->seq, "over: "); 633 634 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 635 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus)) 636 continue; 637 638 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 639 continue; 640 641 if (!params->no_irq) 642 trace_seq_printf(trace->seq, "%9d ", 643 data->hist[cpu].irq[data->entries]); 644 645 if (!params->no_thread) 646 trace_seq_printf(trace->seq, "%9d ", 647 data->hist[cpu].thread[data->entries]); 648 649 if (params->user_hist) 650 trace_seq_printf(trace->seq, "%9d ", 651 data->hist[cpu].user[data->entries]); 652 } 653 trace_seq_printf(trace->seq, "\n"); 654 trace_seq_do_printf(trace->seq); 655 trace_seq_reset(trace->seq); 656 657 timerlat_print_summary(params, trace, data); 658 timerlat_print_stats_all(params, trace, data); 659 } 660 661 /* 662 * timerlat_hist_usage - prints timerlat top usage message 663 */ 664 static void timerlat_hist_usage(char *usage) 665 { 666 int i; 667 668 char *msg[] = { 669 "", 670 " usage: [rtla] timerlat hist [-h] [-q] [-d s] [-D] [-n] [-a us] [-p us] [-i us] [-T us] [-s us] \\", 671 " [-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\", 672 " [-P priority] [-E N] [-b N] [--no-irq] [--no-thread] [--no-header] [--no-summary] \\", 673 " [--no-index] [--with-zeros] [--dma-latency us] [-C[=cgroup_name]] [--no-aa] [--dump-task] [-u|-k]", 674 " [--warm-up s] [--deepest-idle-state n]", 675 "", 676 " -h/--help: print this menu", 677 " -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit", 678 " -p/--period us: timerlat period in us", 679 " -i/--irq us: stop trace if the irq latency is higher than the argument in us", 680 " -T/--thread us: stop trace if the thread latency is higher than the argument in us", 681 " -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us", 682 " -c/--cpus cpus: run the tracer only on the given cpus", 683 " -H/--house-keeping cpus: run rtla control threads only on the given cpus", 684 " -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited", 685 " -d/--duration time[m|h|d]: duration of the session in seconds", 686 " --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)", 687 " -D/--debug: print debug info", 688 " -t/--trace[file]: save the stopped trace to [file|timerlat_trace.txt]", 689 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed", 690 " --filter <filter>: enable a trace event filter to the previous -e event", 691 " --trigger <trigger>: enable a trace event trigger to the previous -e event", 692 " -n/--nano: display data in nanoseconds", 693 " --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage", 694 " -b/--bucket-size N: set the histogram bucket size (default 1)", 695 " -E/--entries N: set the number of entries of the histogram (default 256)", 696 " --no-irq: ignore IRQ latencies", 697 " --no-thread: ignore thread latencies", 698 " --no-header: do not print header", 699 " --no-summary: do not print summary", 700 " --no-index: do not print index", 701 " --with-zeros: print zero only entries", 702 " --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency", 703 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters", 704 " o:prio - use SCHED_OTHER with prio", 705 " r:prio - use SCHED_RR with prio", 706 " f:prio - use SCHED_FIFO with prio", 707 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period", 708 " in nanoseconds", 709 " -u/--user-threads: use rtla user-space threads instead of kernel-space timerlat threads", 710 " -k/--kernel-threads: use timerlat kernel-space threads instead of rtla user-space threads", 711 " -U/--user-load: enable timerlat for user-defined user-space workload", 712 " --warm-up s: let the workload run for s seconds before collecting data", 713 " --trace-buffer-size kB: set the per-cpu trace buffer size in kB", 714 " --deepest-idle-state n: only go down to idle state n on cpus used by timerlat to reduce exit from idle latency", 715 NULL, 716 }; 717 718 if (usage) 719 fprintf(stderr, "%s\n", usage); 720 721 fprintf(stderr, "rtla timerlat hist: a per-cpu histogram of the timer latency (version %s)\n", 722 VERSION); 723 724 for (i = 0; msg[i]; i++) 725 fprintf(stderr, "%s\n", msg[i]); 726 727 if (usage) 728 exit(EXIT_FAILURE); 729 730 exit(EXIT_SUCCESS); 731 } 732 733 /* 734 * timerlat_hist_parse_args - allocs, parse and fill the cmd line parameters 735 */ 736 static struct timerlat_hist_params 737 *timerlat_hist_parse_args(int argc, char *argv[]) 738 { 739 struct timerlat_hist_params *params; 740 struct trace_events *tevent; 741 int auto_thresh; 742 int retval; 743 int c; 744 745 params = calloc(1, sizeof(*params)); 746 if (!params) 747 exit(1); 748 749 /* disabled by default */ 750 params->dma_latency = -1; 751 752 /* disabled by default */ 753 params->deepest_idle_state = -2; 754 755 /* display data in microseconds */ 756 params->output_divisor = 1000; 757 params->bucket_size = 1; 758 params->entries = 256; 759 760 while (1) { 761 static struct option long_options[] = { 762 {"auto", required_argument, 0, 'a'}, 763 {"cpus", required_argument, 0, 'c'}, 764 {"cgroup", optional_argument, 0, 'C'}, 765 {"bucket-size", required_argument, 0, 'b'}, 766 {"debug", no_argument, 0, 'D'}, 767 {"entries", required_argument, 0, 'E'}, 768 {"duration", required_argument, 0, 'd'}, 769 {"house-keeping", required_argument, 0, 'H'}, 770 {"help", no_argument, 0, 'h'}, 771 {"irq", required_argument, 0, 'i'}, 772 {"nano", no_argument, 0, 'n'}, 773 {"period", required_argument, 0, 'p'}, 774 {"priority", required_argument, 0, 'P'}, 775 {"stack", required_argument, 0, 's'}, 776 {"thread", required_argument, 0, 'T'}, 777 {"trace", optional_argument, 0, 't'}, 778 {"user-threads", no_argument, 0, 'u'}, 779 {"kernel-threads", no_argument, 0, 'k'}, 780 {"user-load", no_argument, 0, 'U'}, 781 {"event", required_argument, 0, 'e'}, 782 {"no-irq", no_argument, 0, '0'}, 783 {"no-thread", no_argument, 0, '1'}, 784 {"no-header", no_argument, 0, '2'}, 785 {"no-summary", no_argument, 0, '3'}, 786 {"no-index", no_argument, 0, '4'}, 787 {"with-zeros", no_argument, 0, '5'}, 788 {"trigger", required_argument, 0, '6'}, 789 {"filter", required_argument, 0, '7'}, 790 {"dma-latency", required_argument, 0, '8'}, 791 {"no-aa", no_argument, 0, '9'}, 792 {"dump-task", no_argument, 0, '\1'}, 793 {"warm-up", required_argument, 0, '\2'}, 794 {"trace-buffer-size", required_argument, 0, '\3'}, 795 {"deepest-idle-state", required_argument, 0, '\4'}, 796 {0, 0, 0, 0} 797 }; 798 799 /* getopt_long stores the option index here. */ 800 int option_index = 0; 801 802 c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:i:knp:P:s:t::T:uU0123456:7:8:9\1\2:\3:", 803 long_options, &option_index); 804 805 /* detect the end of the options. */ 806 if (c == -1) 807 break; 808 809 switch (c) { 810 case 'a': 811 auto_thresh = get_llong_from_str(optarg); 812 813 /* set thread stop to auto_thresh */ 814 params->stop_total_us = auto_thresh; 815 params->stop_us = auto_thresh; 816 817 /* get stack trace */ 818 params->print_stack = auto_thresh; 819 820 /* set trace */ 821 params->trace_output = "timerlat_trace.txt"; 822 823 break; 824 case 'c': 825 retval = parse_cpu_set(optarg, ¶ms->monitored_cpus); 826 if (retval) 827 timerlat_hist_usage("\nInvalid -c cpu list\n"); 828 params->cpus = optarg; 829 break; 830 case 'C': 831 params->cgroup = 1; 832 if (!optarg) { 833 /* will inherit this cgroup */ 834 params->cgroup_name = NULL; 835 } else if (*optarg == '=') { 836 /* skip the = */ 837 params->cgroup_name = ++optarg; 838 } 839 break; 840 case 'b': 841 params->bucket_size = get_llong_from_str(optarg); 842 if ((params->bucket_size == 0) || (params->bucket_size >= 1000000)) 843 timerlat_hist_usage("Bucket size needs to be > 0 and <= 1000000\n"); 844 break; 845 case 'D': 846 config_debug = 1; 847 break; 848 case 'd': 849 params->duration = parse_seconds_duration(optarg); 850 if (!params->duration) 851 timerlat_hist_usage("Invalid -D duration\n"); 852 break; 853 case 'e': 854 tevent = trace_event_alloc(optarg); 855 if (!tevent) { 856 err_msg("Error alloc trace event"); 857 exit(EXIT_FAILURE); 858 } 859 860 if (params->events) 861 tevent->next = params->events; 862 863 params->events = tevent; 864 break; 865 case 'E': 866 params->entries = get_llong_from_str(optarg); 867 if ((params->entries < 10) || (params->entries > 9999999)) 868 timerlat_hist_usage("Entries must be > 10 and < 9999999\n"); 869 break; 870 case 'h': 871 case '?': 872 timerlat_hist_usage(NULL); 873 break; 874 case 'H': 875 params->hk_cpus = 1; 876 retval = parse_cpu_set(optarg, ¶ms->hk_cpu_set); 877 if (retval) { 878 err_msg("Error parsing house keeping CPUs\n"); 879 exit(EXIT_FAILURE); 880 } 881 break; 882 case 'i': 883 params->stop_us = get_llong_from_str(optarg); 884 break; 885 case 'k': 886 params->kernel_workload = 1; 887 break; 888 case 'n': 889 params->output_divisor = 1; 890 break; 891 case 'p': 892 params->timerlat_period_us = get_llong_from_str(optarg); 893 if (params->timerlat_period_us > 1000000) 894 timerlat_hist_usage("Period longer than 1 s\n"); 895 break; 896 case 'P': 897 retval = parse_prio(optarg, ¶ms->sched_param); 898 if (retval == -1) 899 timerlat_hist_usage("Invalid -P priority"); 900 params->set_sched = 1; 901 break; 902 case 's': 903 params->print_stack = get_llong_from_str(optarg); 904 break; 905 case 'T': 906 params->stop_total_us = get_llong_from_str(optarg); 907 break; 908 case 't': 909 if (optarg) { 910 if (optarg[0] == '=') 911 params->trace_output = &optarg[1]; 912 else 913 params->trace_output = &optarg[0]; 914 } else if (optind < argc && argv[optind][0] != '-') 915 params->trace_output = argv[optind]; 916 else 917 params->trace_output = "timerlat_trace.txt"; 918 break; 919 case 'u': 920 params->user_workload = 1; 921 /* fallback: -u implies in -U */ 922 case 'U': 923 params->user_hist = 1; 924 break; 925 case '0': /* no irq */ 926 params->no_irq = 1; 927 break; 928 case '1': /* no thread */ 929 params->no_thread = 1; 930 break; 931 case '2': /* no header */ 932 params->no_header = 1; 933 break; 934 case '3': /* no summary */ 935 params->no_summary = 1; 936 break; 937 case '4': /* no index */ 938 params->no_index = 1; 939 break; 940 case '5': /* with zeros */ 941 params->with_zeros = 1; 942 break; 943 case '6': /* trigger */ 944 if (params->events) { 945 retval = trace_event_add_trigger(params->events, optarg); 946 if (retval) { 947 err_msg("Error adding trigger %s\n", optarg); 948 exit(EXIT_FAILURE); 949 } 950 } else { 951 timerlat_hist_usage("--trigger requires a previous -e\n"); 952 } 953 break; 954 case '7': /* filter */ 955 if (params->events) { 956 retval = trace_event_add_filter(params->events, optarg); 957 if (retval) { 958 err_msg("Error adding filter %s\n", optarg); 959 exit(EXIT_FAILURE); 960 } 961 } else { 962 timerlat_hist_usage("--filter requires a previous -e\n"); 963 } 964 break; 965 case '8': 966 params->dma_latency = get_llong_from_str(optarg); 967 if (params->dma_latency < 0 || params->dma_latency > 10000) { 968 err_msg("--dma-latency needs to be >= 0 and < 10000"); 969 exit(EXIT_FAILURE); 970 } 971 break; 972 case '9': 973 params->no_aa = 1; 974 break; 975 case '\1': 976 params->dump_tasks = 1; 977 break; 978 case '\2': 979 params->warmup = get_llong_from_str(optarg); 980 break; 981 case '\3': 982 params->buffer_size = get_llong_from_str(optarg); 983 break; 984 case '\4': 985 params->deepest_idle_state = get_llong_from_str(optarg); 986 break; 987 default: 988 timerlat_hist_usage("Invalid option"); 989 } 990 } 991 992 if (geteuid()) { 993 err_msg("rtla needs root permission\n"); 994 exit(EXIT_FAILURE); 995 } 996 997 if (params->no_irq && params->no_thread) 998 timerlat_hist_usage("no-irq and no-thread set, there is nothing to do here"); 999 1000 if (params->no_index && !params->with_zeros) 1001 timerlat_hist_usage("no-index set with with-zeros is not set - it does not make sense"); 1002 1003 /* 1004 * Auto analysis only happens if stop tracing, thus: 1005 */ 1006 if (!params->stop_us && !params->stop_total_us) 1007 params->no_aa = 1; 1008 1009 if (params->kernel_workload && params->user_workload) 1010 timerlat_hist_usage("--kernel-threads and --user-threads are mutually exclusive!"); 1011 1012 return params; 1013 } 1014 1015 /* 1016 * timerlat_hist_apply_config - apply the hist configs to the initialized tool 1017 */ 1018 static int 1019 timerlat_hist_apply_config(struct osnoise_tool *tool, struct timerlat_hist_params *params) 1020 { 1021 int retval, i; 1022 1023 if (!params->sleep_time) 1024 params->sleep_time = 1; 1025 1026 if (params->cpus) { 1027 retval = osnoise_set_cpus(tool->context, params->cpus); 1028 if (retval) { 1029 err_msg("Failed to apply CPUs config\n"); 1030 goto out_err; 1031 } 1032 } else { 1033 for (i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++) 1034 CPU_SET(i, ¶ms->monitored_cpus); 1035 } 1036 1037 if (params->stop_us) { 1038 retval = osnoise_set_stop_us(tool->context, params->stop_us); 1039 if (retval) { 1040 err_msg("Failed to set stop us\n"); 1041 goto out_err; 1042 } 1043 } 1044 1045 if (params->stop_total_us) { 1046 retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us); 1047 if (retval) { 1048 err_msg("Failed to set stop total us\n"); 1049 goto out_err; 1050 } 1051 } 1052 1053 if (params->timerlat_period_us) { 1054 retval = osnoise_set_timerlat_period_us(tool->context, params->timerlat_period_us); 1055 if (retval) { 1056 err_msg("Failed to set timerlat period\n"); 1057 goto out_err; 1058 } 1059 } 1060 1061 if (params->print_stack) { 1062 retval = osnoise_set_print_stack(tool->context, params->print_stack); 1063 if (retval) { 1064 err_msg("Failed to set print stack\n"); 1065 goto out_err; 1066 } 1067 } 1068 1069 if (params->hk_cpus) { 1070 retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set), 1071 ¶ms->hk_cpu_set); 1072 if (retval == -1) { 1073 err_msg("Failed to set rtla to the house keeping CPUs\n"); 1074 goto out_err; 1075 } 1076 } else if (params->cpus) { 1077 /* 1078 * Even if the user do not set a house-keeping CPU, try to 1079 * move rtla to a CPU set different to the one where the user 1080 * set the workload to run. 1081 * 1082 * No need to check results as this is an automatic attempt. 1083 */ 1084 auto_house_keeping(¶ms->monitored_cpus); 1085 } 1086 1087 /* 1088 * If the user did not specify a type of thread, try user-threads first. 1089 * Fall back to kernel threads otherwise. 1090 */ 1091 if (!params->kernel_workload && !params->user_hist) { 1092 retval = tracefs_file_exists(NULL, "osnoise/per_cpu/cpu0/timerlat_fd"); 1093 if (retval) { 1094 debug_msg("User-space interface detected, setting user-threads\n"); 1095 params->user_workload = 1; 1096 params->user_hist = 1; 1097 } else { 1098 debug_msg("User-space interface not detected, setting kernel-threads\n"); 1099 params->kernel_workload = 1; 1100 } 1101 } 1102 1103 if (params->user_hist) { 1104 retval = osnoise_set_workload(tool->context, 0); 1105 if (retval) { 1106 err_msg("Failed to set OSNOISE_WORKLOAD option\n"); 1107 goto out_err; 1108 } 1109 } 1110 1111 return 0; 1112 1113 out_err: 1114 return -1; 1115 } 1116 1117 /* 1118 * timerlat_init_hist - initialize a timerlat hist tool with parameters 1119 */ 1120 static struct osnoise_tool 1121 *timerlat_init_hist(struct timerlat_hist_params *params) 1122 { 1123 struct osnoise_tool *tool; 1124 int nr_cpus; 1125 1126 nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 1127 1128 tool = osnoise_init_tool("timerlat_hist"); 1129 if (!tool) 1130 return NULL; 1131 1132 tool->data = timerlat_alloc_histogram(nr_cpus, params->entries, params->bucket_size); 1133 if (!tool->data) 1134 goto out_err; 1135 1136 tool->params = params; 1137 1138 tep_register_event_handler(tool->trace.tep, -1, "ftrace", "timerlat", 1139 timerlat_hist_handler, tool); 1140 1141 return tool; 1142 1143 out_err: 1144 osnoise_destroy_tool(tool); 1145 return NULL; 1146 } 1147 1148 static int stop_tracing; 1149 static void stop_hist(int sig) 1150 { 1151 stop_tracing = 1; 1152 } 1153 1154 /* 1155 * timerlat_hist_set_signals - handles the signal to stop the tool 1156 */ 1157 static void 1158 timerlat_hist_set_signals(struct timerlat_hist_params *params) 1159 { 1160 signal(SIGINT, stop_hist); 1161 if (params->duration) { 1162 signal(SIGALRM, stop_hist); 1163 alarm(params->duration); 1164 } 1165 } 1166 1167 int timerlat_hist_main(int argc, char *argv[]) 1168 { 1169 struct timerlat_hist_params *params; 1170 struct osnoise_tool *record = NULL; 1171 struct timerlat_u_params params_u; 1172 struct osnoise_tool *tool = NULL; 1173 struct osnoise_tool *aa = NULL; 1174 struct trace_instance *trace; 1175 int dma_latency_fd = -1; 1176 int return_value = 1; 1177 pthread_t timerlat_u; 1178 int retval; 1179 int nr_cpus, i; 1180 1181 params = timerlat_hist_parse_args(argc, argv); 1182 if (!params) 1183 exit(1); 1184 1185 tool = timerlat_init_hist(params); 1186 if (!tool) { 1187 err_msg("Could not init osnoise hist\n"); 1188 goto out_exit; 1189 } 1190 1191 retval = timerlat_hist_apply_config(tool, params); 1192 if (retval) { 1193 err_msg("Could not apply config\n"); 1194 goto out_free; 1195 } 1196 1197 trace = &tool->trace; 1198 1199 retval = enable_timerlat(trace); 1200 if (retval) { 1201 err_msg("Failed to enable timerlat tracer\n"); 1202 goto out_free; 1203 } 1204 1205 if (params->set_sched) { 1206 retval = set_comm_sched_attr("timerlat/", ¶ms->sched_param); 1207 if (retval) { 1208 err_msg("Failed to set sched parameters\n"); 1209 goto out_free; 1210 } 1211 } 1212 1213 if (params->cgroup && !params->user_workload) { 1214 retval = set_comm_cgroup("timerlat/", params->cgroup_name); 1215 if (!retval) { 1216 err_msg("Failed to move threads to cgroup\n"); 1217 goto out_free; 1218 } 1219 } 1220 1221 if (params->dma_latency >= 0) { 1222 dma_latency_fd = set_cpu_dma_latency(params->dma_latency); 1223 if (dma_latency_fd < 0) { 1224 err_msg("Could not set /dev/cpu_dma_latency.\n"); 1225 goto out_free; 1226 } 1227 } 1228 1229 if (params->deepest_idle_state >= -1) { 1230 if (!have_libcpupower_support()) { 1231 err_msg("rtla built without libcpupower, --deepest-idle-state is not supported\n"); 1232 goto out_free; 1233 } 1234 1235 nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 1236 1237 for (i = 0; i < nr_cpus; i++) { 1238 if (params->cpus && !CPU_ISSET(i, ¶ms->monitored_cpus)) 1239 continue; 1240 if (save_cpu_idle_disable_state(i) < 0) { 1241 err_msg("Could not save cpu idle state.\n"); 1242 goto out_free; 1243 } 1244 if (set_deepest_cpu_idle_state(i, params->deepest_idle_state) < 0) { 1245 err_msg("Could not set deepest cpu idle state.\n"); 1246 goto out_free; 1247 } 1248 } 1249 } 1250 1251 if (params->trace_output) { 1252 record = osnoise_init_trace_tool("timerlat"); 1253 if (!record) { 1254 err_msg("Failed to enable the trace instance\n"); 1255 goto out_free; 1256 } 1257 1258 if (params->events) { 1259 retval = trace_events_enable(&record->trace, params->events); 1260 if (retval) 1261 goto out_hist; 1262 } 1263 1264 if (params->buffer_size > 0) { 1265 retval = trace_set_buffer_size(&record->trace, params->buffer_size); 1266 if (retval) 1267 goto out_hist; 1268 } 1269 } 1270 1271 if (!params->no_aa) { 1272 aa = osnoise_init_tool("timerlat_aa"); 1273 if (!aa) 1274 goto out_hist; 1275 1276 retval = timerlat_aa_init(aa, params->dump_tasks); 1277 if (retval) { 1278 err_msg("Failed to enable the auto analysis instance\n"); 1279 goto out_hist; 1280 } 1281 1282 retval = enable_timerlat(&aa->trace); 1283 if (retval) { 1284 err_msg("Failed to enable timerlat tracer\n"); 1285 goto out_hist; 1286 } 1287 } 1288 1289 if (params->user_workload) { 1290 /* rtla asked to stop */ 1291 params_u.should_run = 1; 1292 /* all threads left */ 1293 params_u.stopped_running = 0; 1294 1295 params_u.set = ¶ms->monitored_cpus; 1296 if (params->set_sched) 1297 params_u.sched_param = ¶ms->sched_param; 1298 else 1299 params_u.sched_param = NULL; 1300 1301 params_u.cgroup_name = params->cgroup_name; 1302 1303 retval = pthread_create(&timerlat_u, NULL, timerlat_u_dispatcher, ¶ms_u); 1304 if (retval) 1305 err_msg("Error creating timerlat user-space threads\n"); 1306 } 1307 1308 if (params->warmup > 0) { 1309 debug_msg("Warming up for %d seconds\n", params->warmup); 1310 sleep(params->warmup); 1311 if (stop_tracing) 1312 goto out_hist; 1313 } 1314 1315 /* 1316 * Start the tracers here, after having set all instances. 1317 * 1318 * Let the trace instance start first for the case of hitting a stop 1319 * tracing while enabling other instances. The trace instance is the 1320 * one with most valuable information. 1321 */ 1322 if (params->trace_output) 1323 trace_instance_start(&record->trace); 1324 if (!params->no_aa) 1325 trace_instance_start(&aa->trace); 1326 trace_instance_start(trace); 1327 1328 tool->start_time = time(NULL); 1329 timerlat_hist_set_signals(params); 1330 1331 while (!stop_tracing) { 1332 sleep(params->sleep_time); 1333 1334 retval = tracefs_iterate_raw_events(trace->tep, 1335 trace->inst, 1336 NULL, 1337 0, 1338 collect_registered_events, 1339 trace); 1340 if (retval < 0) { 1341 err_msg("Error iterating on events\n"); 1342 goto out_hist; 1343 } 1344 1345 if (trace_is_off(&tool->trace, &record->trace)) 1346 break; 1347 1348 /* is there still any user-threads ? */ 1349 if (params->user_workload) { 1350 if (params_u.stopped_running) { 1351 debug_msg("timerlat user-space threads stopped!\n"); 1352 break; 1353 } 1354 } 1355 } 1356 1357 if (params->user_workload && !params_u.stopped_running) { 1358 params_u.should_run = 0; 1359 sleep(1); 1360 } 1361 1362 timerlat_print_stats(params, tool); 1363 1364 return_value = 0; 1365 1366 if (trace_is_off(&tool->trace, &record->trace)) { 1367 printf("rtla timerlat hit stop tracing\n"); 1368 1369 if (!params->no_aa) 1370 timerlat_auto_analysis(params->stop_us, params->stop_total_us); 1371 1372 if (params->trace_output) { 1373 printf(" Saving trace to %s\n", params->trace_output); 1374 save_trace_to_file(record->trace.inst, params->trace_output); 1375 } 1376 } 1377 1378 out_hist: 1379 timerlat_aa_destroy(); 1380 if (dma_latency_fd >= 0) 1381 close(dma_latency_fd); 1382 if (params->deepest_idle_state >= -1) { 1383 for (i = 0; i < nr_cpus; i++) { 1384 if (params->cpus && !CPU_ISSET(i, ¶ms->monitored_cpus)) 1385 continue; 1386 restore_cpu_idle_disable_state(i); 1387 } 1388 } 1389 trace_events_destroy(&record->trace, params->events); 1390 params->events = NULL; 1391 out_free: 1392 timerlat_free_histogram(tool->data); 1393 osnoise_destroy_tool(aa); 1394 osnoise_destroy_tool(record); 1395 osnoise_destroy_tool(tool); 1396 free(params); 1397 free_cpu_idle_disable_states(); 1398 out_exit: 1399 exit(return_value); 1400 } 1401