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 "timerlat.h" 18 #include "timerlat_aa.h" 19 #include "timerlat_bpf.h" 20 21 struct timerlat_hist_cpu { 22 int *irq; 23 int *thread; 24 int *user; 25 26 unsigned long long irq_count; 27 unsigned long long thread_count; 28 unsigned long long user_count; 29 30 unsigned long long min_irq; 31 unsigned long long sum_irq; 32 unsigned long long max_irq; 33 34 unsigned long long min_thread; 35 unsigned long long sum_thread; 36 unsigned long long max_thread; 37 38 unsigned long long min_user; 39 unsigned long long sum_user; 40 unsigned long long max_user; 41 }; 42 43 struct timerlat_hist_data { 44 struct timerlat_hist_cpu *hist; 45 int entries; 46 int bucket_size; 47 int nr_cpus; 48 }; 49 50 /* 51 * timerlat_free_histogram - free runtime data 52 */ 53 static void 54 timerlat_free_histogram(struct timerlat_hist_data *data) 55 { 56 int cpu; 57 58 /* one histogram for IRQ and one for thread, per CPU */ 59 for (cpu = 0; cpu < data->nr_cpus; cpu++) { 60 if (data->hist[cpu].irq) 61 free(data->hist[cpu].irq); 62 63 if (data->hist[cpu].thread) 64 free(data->hist[cpu].thread); 65 66 if (data->hist[cpu].user) 67 free(data->hist[cpu].user); 68 69 } 70 71 /* one set of histograms per CPU */ 72 if (data->hist) 73 free(data->hist); 74 } 75 76 static void timerlat_free_histogram_tool(struct osnoise_tool *tool) 77 { 78 timerlat_free_histogram(tool->data); 79 timerlat_free(tool); 80 } 81 82 /* 83 * timerlat_alloc_histogram - alloc runtime data 84 */ 85 static struct timerlat_hist_data 86 *timerlat_alloc_histogram(int entries, int bucket_size) 87 { 88 struct timerlat_hist_data *data; 89 int cpu; 90 91 data = calloc(1, sizeof(*data)); 92 if (!data) 93 return NULL; 94 95 data->entries = entries; 96 data->bucket_size = bucket_size; 97 data->nr_cpus = nr_cpus; 98 99 /* one set of histograms per CPU */ 100 data->hist = calloc(1, sizeof(*data->hist) * nr_cpus); 101 if (!data->hist) 102 goto cleanup; 103 104 /* one histogram for IRQ and one for thread, per cpu */ 105 for (cpu = 0; cpu < nr_cpus; cpu++) { 106 data->hist[cpu].irq = calloc(1, sizeof(*data->hist->irq) * (entries + 1)); 107 if (!data->hist[cpu].irq) 108 goto cleanup; 109 110 data->hist[cpu].thread = calloc(1, sizeof(*data->hist->thread) * (entries + 1)); 111 if (!data->hist[cpu].thread) 112 goto cleanup; 113 114 data->hist[cpu].user = calloc(1, sizeof(*data->hist->user) * (entries + 1)); 115 if (!data->hist[cpu].user) 116 goto cleanup; 117 } 118 119 /* set the min to max */ 120 for (cpu = 0; cpu < nr_cpus; cpu++) { 121 data->hist[cpu].min_irq = ~0; 122 data->hist[cpu].min_thread = ~0; 123 data->hist[cpu].min_user = ~0; 124 } 125 126 return data; 127 128 cleanup: 129 timerlat_free_histogram(data); 130 return NULL; 131 } 132 133 /* 134 * timerlat_hist_update - record a new timerlat occurent on cpu, updating data 135 */ 136 static void 137 timerlat_hist_update(struct osnoise_tool *tool, int cpu, 138 unsigned long long context, 139 unsigned long long latency) 140 { 141 struct timerlat_params *params = to_timerlat_params(tool->params); 142 struct timerlat_hist_data *data = tool->data; 143 int entries = data->entries; 144 int bucket; 145 int *hist; 146 147 if (params->common.output_divisor) 148 latency = latency / params->common.output_divisor; 149 150 bucket = latency / data->bucket_size; 151 152 if (!context) { 153 hist = data->hist[cpu].irq; 154 data->hist[cpu].irq_count++; 155 update_min(&data->hist[cpu].min_irq, &latency); 156 update_sum(&data->hist[cpu].sum_irq, &latency); 157 update_max(&data->hist[cpu].max_irq, &latency); 158 } else if (context == 1) { 159 hist = data->hist[cpu].thread; 160 data->hist[cpu].thread_count++; 161 update_min(&data->hist[cpu].min_thread, &latency); 162 update_sum(&data->hist[cpu].sum_thread, &latency); 163 update_max(&data->hist[cpu].max_thread, &latency); 164 } else { /* user */ 165 hist = data->hist[cpu].user; 166 data->hist[cpu].user_count++; 167 update_min(&data->hist[cpu].min_user, &latency); 168 update_sum(&data->hist[cpu].sum_user, &latency); 169 update_max(&data->hist[cpu].max_user, &latency); 170 } 171 172 if (bucket < entries) 173 hist[bucket]++; 174 else 175 hist[entries]++; 176 } 177 178 /* 179 * timerlat_hist_handler - this is the handler for timerlat tracer events 180 */ 181 static int 182 timerlat_hist_handler(struct trace_seq *s, struct tep_record *record, 183 struct tep_event *event, void *data) 184 { 185 struct trace_instance *trace = data; 186 unsigned long long context, latency; 187 struct osnoise_tool *tool; 188 int cpu = record->cpu; 189 190 tool = container_of(trace, struct osnoise_tool, trace); 191 192 tep_get_field_val(s, event, "context", record, &context, 1); 193 tep_get_field_val(s, event, "timer_latency", record, &latency, 1); 194 195 timerlat_hist_update(tool, cpu, context, latency); 196 197 return 0; 198 } 199 200 /* 201 * timerlat_hist_bpf_pull_data - copy data from BPF maps into userspace 202 */ 203 static int timerlat_hist_bpf_pull_data(struct osnoise_tool *tool) 204 { 205 struct timerlat_hist_data *data = tool->data; 206 int i, j, err; 207 long long value_irq[data->nr_cpus], 208 value_thread[data->nr_cpus], 209 value_user[data->nr_cpus]; 210 211 /* Pull histogram */ 212 for (i = 0; i < data->entries; i++) { 213 err = timerlat_bpf_get_hist_value(i, value_irq, value_thread, 214 value_user); 215 if (err) 216 return err; 217 for (j = 0; j < data->nr_cpus; j++) { 218 data->hist[j].irq[i] = value_irq[j]; 219 data->hist[j].thread[i] = value_thread[j]; 220 data->hist[j].user[i] = value_user[j]; 221 } 222 } 223 224 /* Pull summary */ 225 err = timerlat_bpf_get_summary_value(SUMMARY_COUNT, 226 value_irq, value_thread, value_user); 227 if (err) 228 return err; 229 for (i = 0; i < data->nr_cpus; i++) { 230 data->hist[i].irq_count = value_irq[i]; 231 data->hist[i].thread_count = value_thread[i]; 232 data->hist[i].user_count = value_user[i]; 233 } 234 235 err = timerlat_bpf_get_summary_value(SUMMARY_MIN, 236 value_irq, value_thread, value_user); 237 if (err) 238 return err; 239 for (i = 0; i < data->nr_cpus; i++) { 240 data->hist[i].min_irq = value_irq[i]; 241 data->hist[i].min_thread = value_thread[i]; 242 data->hist[i].min_user = value_user[i]; 243 } 244 245 err = timerlat_bpf_get_summary_value(SUMMARY_MAX, 246 value_irq, value_thread, value_user); 247 if (err) 248 return err; 249 for (i = 0; i < data->nr_cpus; i++) { 250 data->hist[i].max_irq = value_irq[i]; 251 data->hist[i].max_thread = value_thread[i]; 252 data->hist[i].max_user = value_user[i]; 253 } 254 255 err = timerlat_bpf_get_summary_value(SUMMARY_SUM, 256 value_irq, value_thread, value_user); 257 if (err) 258 return err; 259 for (i = 0; i < data->nr_cpus; i++) { 260 data->hist[i].sum_irq = value_irq[i]; 261 data->hist[i].sum_thread = value_thread[i]; 262 data->hist[i].sum_user = value_user[i]; 263 } 264 265 err = timerlat_bpf_get_summary_value(SUMMARY_OVERFLOW, 266 value_irq, value_thread, value_user); 267 if (err) 268 return err; 269 for (i = 0; i < data->nr_cpus; i++) { 270 data->hist[i].irq[data->entries] = value_irq[i]; 271 data->hist[i].thread[data->entries] = value_thread[i]; 272 data->hist[i].user[data->entries] = value_user[i]; 273 } 274 275 return 0; 276 } 277 278 /* 279 * timerlat_hist_header - print the header of the tracer to the output 280 */ 281 static void timerlat_hist_header(struct osnoise_tool *tool) 282 { 283 struct timerlat_params *params = to_timerlat_params(tool->params); 284 struct timerlat_hist_data *data = tool->data; 285 struct trace_seq *s = tool->trace.seq; 286 char duration[26]; 287 int cpu; 288 289 if (params->common.hist.no_header) 290 return; 291 292 get_duration(tool->start_time, duration, sizeof(duration)); 293 trace_seq_printf(s, "# RTLA timerlat histogram\n"); 294 trace_seq_printf(s, "# Time unit is %s (%s)\n", 295 params->common.output_divisor == 1 ? "nanoseconds" : "microseconds", 296 params->common.output_divisor == 1 ? "ns" : "us"); 297 298 trace_seq_printf(s, "# Duration: %s\n", duration); 299 300 if (!params->common.hist.no_index) 301 trace_seq_printf(s, "Index"); 302 303 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { 304 305 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 306 continue; 307 308 if (!params->common.hist.no_irq) 309 trace_seq_printf(s, " IRQ-%03d", cpu); 310 311 if (!params->common.hist.no_thread) 312 trace_seq_printf(s, " Thr-%03d", cpu); 313 314 if (params->common.user_data) 315 trace_seq_printf(s, " Usr-%03d", cpu); 316 } 317 trace_seq_printf(s, "\n"); 318 319 320 trace_seq_do_printf(s); 321 trace_seq_reset(s); 322 } 323 324 /* 325 * format_summary_value - format a line of summary value (min, max or avg) 326 * of hist data 327 */ 328 static void format_summary_value(struct trace_seq *seq, 329 int count, 330 unsigned long long val, 331 bool avg) 332 { 333 if (count) 334 trace_seq_printf(seq, "%9llu ", avg ? val / count : val); 335 else 336 trace_seq_printf(seq, "%9c ", '-'); 337 } 338 339 /* 340 * timerlat_print_summary - print the summary of the hist data to the output 341 */ 342 static void 343 timerlat_print_summary(struct timerlat_params *params, 344 struct trace_instance *trace, 345 struct timerlat_hist_data *data) 346 { 347 int cpu; 348 349 if (params->common.hist.no_summary) 350 return; 351 352 if (!params->common.hist.no_index) 353 trace_seq_printf(trace->seq, "count:"); 354 355 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { 356 357 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 358 continue; 359 360 if (!params->common.hist.no_irq) 361 trace_seq_printf(trace->seq, "%9llu ", 362 data->hist[cpu].irq_count); 363 364 if (!params->common.hist.no_thread) 365 trace_seq_printf(trace->seq, "%9llu ", 366 data->hist[cpu].thread_count); 367 368 if (params->common.user_data) 369 trace_seq_printf(trace->seq, "%9llu ", 370 data->hist[cpu].user_count); 371 } 372 trace_seq_printf(trace->seq, "\n"); 373 374 if (!params->common.hist.no_index) 375 trace_seq_printf(trace->seq, "min: "); 376 377 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { 378 379 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 380 continue; 381 382 if (!params->common.hist.no_irq) 383 format_summary_value(trace->seq, 384 data->hist[cpu].irq_count, 385 data->hist[cpu].min_irq, 386 false); 387 388 if (!params->common.hist.no_thread) 389 format_summary_value(trace->seq, 390 data->hist[cpu].thread_count, 391 data->hist[cpu].min_thread, 392 false); 393 394 if (params->common.user_data) 395 format_summary_value(trace->seq, 396 data->hist[cpu].user_count, 397 data->hist[cpu].min_user, 398 false); 399 } 400 trace_seq_printf(trace->seq, "\n"); 401 402 if (!params->common.hist.no_index) 403 trace_seq_printf(trace->seq, "avg: "); 404 405 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { 406 407 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 408 continue; 409 410 if (!params->common.hist.no_irq) 411 format_summary_value(trace->seq, 412 data->hist[cpu].irq_count, 413 data->hist[cpu].sum_irq, 414 true); 415 416 if (!params->common.hist.no_thread) 417 format_summary_value(trace->seq, 418 data->hist[cpu].thread_count, 419 data->hist[cpu].sum_thread, 420 true); 421 422 if (params->common.user_data) 423 format_summary_value(trace->seq, 424 data->hist[cpu].user_count, 425 data->hist[cpu].sum_user, 426 true); 427 } 428 trace_seq_printf(trace->seq, "\n"); 429 430 if (!params->common.hist.no_index) 431 trace_seq_printf(trace->seq, "max: "); 432 433 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { 434 435 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 436 continue; 437 438 if (!params->common.hist.no_irq) 439 format_summary_value(trace->seq, 440 data->hist[cpu].irq_count, 441 data->hist[cpu].max_irq, 442 false); 443 444 if (!params->common.hist.no_thread) 445 format_summary_value(trace->seq, 446 data->hist[cpu].thread_count, 447 data->hist[cpu].max_thread, 448 false); 449 450 if (params->common.user_data) 451 format_summary_value(trace->seq, 452 data->hist[cpu].user_count, 453 data->hist[cpu].max_user, 454 false); 455 } 456 trace_seq_printf(trace->seq, "\n"); 457 trace_seq_do_printf(trace->seq); 458 trace_seq_reset(trace->seq); 459 } 460 461 static void 462 timerlat_print_stats_all(struct timerlat_params *params, 463 struct trace_instance *trace, 464 struct timerlat_hist_data *data) 465 { 466 struct timerlat_hist_cpu *cpu_data; 467 struct timerlat_hist_cpu sum; 468 int cpu; 469 470 if (params->common.hist.no_summary) 471 return; 472 473 memset(&sum, 0, sizeof(sum)); 474 sum.min_irq = ~0; 475 sum.min_thread = ~0; 476 sum.min_user = ~0; 477 478 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { 479 480 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 481 continue; 482 483 cpu_data = &data->hist[cpu]; 484 485 sum.irq_count += cpu_data->irq_count; 486 update_min(&sum.min_irq, &cpu_data->min_irq); 487 update_sum(&sum.sum_irq, &cpu_data->sum_irq); 488 update_max(&sum.max_irq, &cpu_data->max_irq); 489 490 sum.thread_count += cpu_data->thread_count; 491 update_min(&sum.min_thread, &cpu_data->min_thread); 492 update_sum(&sum.sum_thread, &cpu_data->sum_thread); 493 update_max(&sum.max_thread, &cpu_data->max_thread); 494 495 sum.user_count += cpu_data->user_count; 496 update_min(&sum.min_user, &cpu_data->min_user); 497 update_sum(&sum.sum_user, &cpu_data->sum_user); 498 update_max(&sum.max_user, &cpu_data->max_user); 499 } 500 501 if (!params->common.hist.no_index) 502 trace_seq_printf(trace->seq, "ALL: "); 503 504 if (!params->common.hist.no_irq) 505 trace_seq_printf(trace->seq, " IRQ"); 506 507 if (!params->common.hist.no_thread) 508 trace_seq_printf(trace->seq, " Thr"); 509 510 if (params->common.user_data) 511 trace_seq_printf(trace->seq, " Usr"); 512 513 trace_seq_printf(trace->seq, "\n"); 514 515 if (!params->common.hist.no_index) 516 trace_seq_printf(trace->seq, "count:"); 517 518 if (!params->common.hist.no_irq) 519 trace_seq_printf(trace->seq, "%9llu ", 520 sum.irq_count); 521 522 if (!params->common.hist.no_thread) 523 trace_seq_printf(trace->seq, "%9llu ", 524 sum.thread_count); 525 526 if (params->common.user_data) 527 trace_seq_printf(trace->seq, "%9llu ", 528 sum.user_count); 529 530 trace_seq_printf(trace->seq, "\n"); 531 532 if (!params->common.hist.no_index) 533 trace_seq_printf(trace->seq, "min: "); 534 535 if (!params->common.hist.no_irq) 536 format_summary_value(trace->seq, 537 sum.irq_count, 538 sum.min_irq, 539 false); 540 541 if (!params->common.hist.no_thread) 542 format_summary_value(trace->seq, 543 sum.thread_count, 544 sum.min_thread, 545 false); 546 547 if (params->common.user_data) 548 format_summary_value(trace->seq, 549 sum.user_count, 550 sum.min_user, 551 false); 552 553 trace_seq_printf(trace->seq, "\n"); 554 555 if (!params->common.hist.no_index) 556 trace_seq_printf(trace->seq, "avg: "); 557 558 if (!params->common.hist.no_irq) 559 format_summary_value(trace->seq, 560 sum.irq_count, 561 sum.sum_irq, 562 true); 563 564 if (!params->common.hist.no_thread) 565 format_summary_value(trace->seq, 566 sum.thread_count, 567 sum.sum_thread, 568 true); 569 570 if (params->common.user_data) 571 format_summary_value(trace->seq, 572 sum.user_count, 573 sum.sum_user, 574 true); 575 576 trace_seq_printf(trace->seq, "\n"); 577 578 if (!params->common.hist.no_index) 579 trace_seq_printf(trace->seq, "max: "); 580 581 if (!params->common.hist.no_irq) 582 format_summary_value(trace->seq, 583 sum.irq_count, 584 sum.max_irq, 585 false); 586 587 if (!params->common.hist.no_thread) 588 format_summary_value(trace->seq, 589 sum.thread_count, 590 sum.max_thread, 591 false); 592 593 if (params->common.user_data) 594 format_summary_value(trace->seq, 595 sum.user_count, 596 sum.max_user, 597 false); 598 599 trace_seq_printf(trace->seq, "\n"); 600 trace_seq_do_printf(trace->seq); 601 trace_seq_reset(trace->seq); 602 } 603 604 /* 605 * timerlat_print_stats - print data for each CPUs 606 */ 607 static void 608 timerlat_print_stats(struct osnoise_tool *tool) 609 { 610 struct timerlat_params *params = to_timerlat_params(tool->params); 611 struct timerlat_hist_data *data = tool->data; 612 struct trace_instance *trace = &tool->trace; 613 int bucket, cpu; 614 int total; 615 616 timerlat_hist_header(tool); 617 618 for (bucket = 0; bucket < data->entries; bucket++) { 619 total = 0; 620 621 if (!params->common.hist.no_index) 622 trace_seq_printf(trace->seq, "%-6d", 623 bucket * data->bucket_size); 624 625 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { 626 627 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 628 continue; 629 630 if (!params->common.hist.no_irq) { 631 total += data->hist[cpu].irq[bucket]; 632 trace_seq_printf(trace->seq, "%9d ", 633 data->hist[cpu].irq[bucket]); 634 } 635 636 if (!params->common.hist.no_thread) { 637 total += data->hist[cpu].thread[bucket]; 638 trace_seq_printf(trace->seq, "%9d ", 639 data->hist[cpu].thread[bucket]); 640 } 641 642 if (params->common.user_data) { 643 total += data->hist[cpu].user[bucket]; 644 trace_seq_printf(trace->seq, "%9d ", 645 data->hist[cpu].user[bucket]); 646 } 647 648 } 649 650 if (total == 0 && !params->common.hist.with_zeros) { 651 trace_seq_reset(trace->seq); 652 continue; 653 } 654 655 trace_seq_printf(trace->seq, "\n"); 656 trace_seq_do_printf(trace->seq); 657 trace_seq_reset(trace->seq); 658 } 659 660 if (!params->common.hist.no_index) 661 trace_seq_printf(trace->seq, "over: "); 662 663 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) { 664 665 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count) 666 continue; 667 668 if (!params->common.hist.no_irq) 669 trace_seq_printf(trace->seq, "%9d ", 670 data->hist[cpu].irq[data->entries]); 671 672 if (!params->common.hist.no_thread) 673 trace_seq_printf(trace->seq, "%9d ", 674 data->hist[cpu].thread[data->entries]); 675 676 if (params->common.user_data) 677 trace_seq_printf(trace->seq, "%9d ", 678 data->hist[cpu].user[data->entries]); 679 } 680 trace_seq_printf(trace->seq, "\n"); 681 trace_seq_do_printf(trace->seq); 682 trace_seq_reset(trace->seq); 683 684 timerlat_print_summary(params, trace, data); 685 timerlat_print_stats_all(params, trace, data); 686 osnoise_report_missed_events(tool); 687 } 688 689 /* 690 * timerlat_hist_usage - prints timerlat top usage message 691 */ 692 static void timerlat_hist_usage(void) 693 { 694 static const char * const msg_start[] = { 695 "[-d s] [-D] [-n] [-a us] [-p us] [-i us] [-T us] [-s us] \\", 696 " [-t [file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\", 697 " [-P priority] [-E N] [-b N] [--no-irq] [--no-thread] [--no-header] [--no-summary] \\", 698 " [--no-index] [--with-zeros] [--dma-latency us] [-C [cgroup_name]] [--no-aa] [--dump-task] [-u|-k]", 699 " [--warm-up s] [--deepest-idle-state n]", 700 NULL, 701 }; 702 703 static const char * const msg_opts[] = { 704 " -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit", 705 " -p/--period us: timerlat period in us", 706 " -i/--irq us: stop trace if the irq latency is higher than the argument in us", 707 " -T/--thread us: stop trace if the thread latency is higher than the argument in us", 708 " -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us", 709 " -c/--cpus cpus: run the tracer only on the given cpus", 710 " -H/--house-keeping cpus: run rtla control threads only on the given cpus", 711 " -C/--cgroup [cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited", 712 " -d/--duration time[m|h|d]: duration of the session in seconds", 713 " --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)", 714 " -D/--debug: print debug info", 715 " -t/--trace [file]: save the stopped trace to [file|timerlat_trace.txt]", 716 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed", 717 " --filter <filter>: enable a trace event filter to the previous -e event", 718 " --trigger <trigger>: enable a trace event trigger to the previous -e event", 719 " -n/--nano: display data in nanoseconds", 720 " --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage", 721 " -b/--bucket-size N: set the histogram bucket size (default 1)", 722 " -E/--entries N: set the number of entries of the histogram (default 256)", 723 " --no-irq: ignore IRQ latencies", 724 " --no-thread: ignore thread latencies", 725 " --no-header: do not print header", 726 " --no-summary: do not print summary", 727 " --no-index: do not print index", 728 " --with-zeros: print zero only entries", 729 " --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency", 730 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters", 731 " o:prio - use SCHED_OTHER with prio", 732 " r:prio - use SCHED_RR with prio", 733 " f:prio - use SCHED_FIFO with prio", 734 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period", 735 " in nanoseconds", 736 " -u/--user-threads: use rtla user-space threads instead of kernel-space timerlat threads", 737 " -k/--kernel-threads: use timerlat kernel-space threads instead of rtla user-space threads", 738 " -U/--user-load: enable timerlat for user-defined user-space workload", 739 " --warm-up s: let the workload run for s seconds before collecting data", 740 " --trace-buffer-size kB: set the per-cpu trace buffer size in kB", 741 " --deepest-idle-state n: only go down to idle state n on cpus used by timerlat to reduce exit from idle latency", 742 " --on-threshold <action>: define action to be executed at latency threshold, multiple are allowed", 743 " --on-end <action>: define action to be executed at measurement end, multiple are allowed", 744 " --bpf-action <program>: load and execute BPF program when latency threshold is exceeded", 745 " --stack-format <format>: set the stack format (truncate, skip, full)", 746 NULL, 747 }; 748 749 common_usage("timerlat", "hist", "a per-cpu histogram of the timer latency", 750 msg_start, msg_opts); 751 } 752 753 /* 754 * timerlat_hist_parse_args - allocs, parse and fill the cmd line parameters 755 */ 756 static struct common_params 757 *timerlat_hist_parse_args(int argc, char *argv[]) 758 { 759 struct timerlat_params *params; 760 int auto_thresh; 761 int retval; 762 int c; 763 char *trace_output = NULL; 764 765 params = calloc(1, sizeof(*params)); 766 if (!params) 767 exit(1); 768 769 actions_init(¶ms->common.threshold_actions); 770 actions_init(¶ms->common.end_actions); 771 772 /* disabled by default */ 773 params->dma_latency = -1; 774 775 /* disabled by default */ 776 params->deepest_idle_state = -2; 777 778 /* display data in microseconds */ 779 params->common.output_divisor = 1000; 780 params->common.hist.bucket_size = 1; 781 params->common.hist.entries = 256; 782 783 /* default to BPF mode */ 784 params->mode = TRACING_MODE_BPF; 785 786 /* default to truncate stack format */ 787 params->stack_format = STACK_FORMAT_TRUNCATE; 788 789 while (1) { 790 static struct option long_options[] = { 791 {"auto", required_argument, 0, 'a'}, 792 {"bucket-size", required_argument, 0, 'b'}, 793 {"entries", required_argument, 0, 'E'}, 794 {"help", no_argument, 0, 'h'}, 795 {"irq", required_argument, 0, 'i'}, 796 {"nano", no_argument, 0, 'n'}, 797 {"period", required_argument, 0, 'p'}, 798 {"stack", required_argument, 0, 's'}, 799 {"thread", required_argument, 0, 'T'}, 800 {"trace", optional_argument, 0, 't'}, 801 {"user-threads", no_argument, 0, 'u'}, 802 {"kernel-threads", no_argument, 0, 'k'}, 803 {"user-load", no_argument, 0, 'U'}, 804 {"no-irq", no_argument, 0, '0'}, 805 {"no-thread", no_argument, 0, '1'}, 806 {"no-header", no_argument, 0, '2'}, 807 {"no-summary", no_argument, 0, '3'}, 808 {"no-index", no_argument, 0, '4'}, 809 {"with-zeros", no_argument, 0, '5'}, 810 {"trigger", required_argument, 0, '6'}, 811 {"filter", required_argument, 0, '7'}, 812 {"dma-latency", required_argument, 0, '8'}, 813 {"no-aa", no_argument, 0, '9'}, 814 {"dump-task", no_argument, 0, '\1'}, 815 {"warm-up", required_argument, 0, '\2'}, 816 {"trace-buffer-size", required_argument, 0, '\3'}, 817 {"deepest-idle-state", required_argument, 0, '\4'}, 818 {"on-threshold", required_argument, 0, '\5'}, 819 {"on-end", required_argument, 0, '\6'}, 820 {"bpf-action", required_argument, 0, '\7'}, 821 {"stack-format", required_argument, 0, '\10'}, 822 {0, 0, 0, 0} 823 }; 824 825 if (common_parse_options(argc, argv, ¶ms->common)) 826 continue; 827 828 c = getopt_auto(argc, argv, long_options); 829 830 /* detect the end of the options. */ 831 if (c == -1) 832 break; 833 834 switch (c) { 835 case 'a': 836 auto_thresh = get_llong_from_str(optarg); 837 838 /* set thread stop to auto_thresh */ 839 params->common.stop_total_us = auto_thresh; 840 params->common.stop_us = auto_thresh; 841 842 /* get stack trace */ 843 params->print_stack = auto_thresh; 844 845 /* set trace */ 846 if (!trace_output) 847 trace_output = "timerlat_trace.txt"; 848 849 break; 850 case 'b': 851 params->common.hist.bucket_size = get_llong_from_str(optarg); 852 if (params->common.hist.bucket_size == 0 || 853 params->common.hist.bucket_size >= 1000000) 854 fatal("Bucket size needs to be > 0 and <= 1000000"); 855 break; 856 case 'E': 857 params->common.hist.entries = get_llong_from_str(optarg); 858 if (params->common.hist.entries < 10 || 859 params->common.hist.entries > 9999999) 860 fatal("Entries must be > 10 and < 9999999"); 861 break; 862 case 'h': 863 case '?': 864 timerlat_hist_usage(); 865 break; 866 case 'i': 867 params->common.stop_us = get_llong_from_str(optarg); 868 break; 869 case 'k': 870 params->common.kernel_workload = 1; 871 break; 872 case 'n': 873 params->common.output_divisor = 1; 874 break; 875 case 'p': 876 params->timerlat_period_us = get_llong_from_str(optarg); 877 if (params->timerlat_period_us > 1000000) 878 fatal("Period longer than 1 s"); 879 break; 880 case 's': 881 params->print_stack = get_llong_from_str(optarg); 882 break; 883 case 'T': 884 params->common.stop_total_us = get_llong_from_str(optarg); 885 break; 886 case 't': 887 trace_output = parse_optional_arg(argc, argv); 888 if (!trace_output) 889 trace_output = "timerlat_trace.txt"; 890 break; 891 case 'u': 892 params->common.user_workload = 1; 893 /* fallback: -u implies in -U */ 894 case 'U': 895 params->common.user_data = 1; 896 break; 897 case '0': /* no irq */ 898 params->common.hist.no_irq = 1; 899 break; 900 case '1': /* no thread */ 901 params->common.hist.no_thread = 1; 902 break; 903 case '2': /* no header */ 904 params->common.hist.no_header = 1; 905 break; 906 case '3': /* no summary */ 907 params->common.hist.no_summary = 1; 908 break; 909 case '4': /* no index */ 910 params->common.hist.no_index = 1; 911 break; 912 case '5': /* with zeros */ 913 params->common.hist.with_zeros = 1; 914 break; 915 case '6': /* trigger */ 916 if (params->common.events) { 917 retval = trace_event_add_trigger(params->common.events, optarg); 918 if (retval) 919 fatal("Error adding trigger %s", optarg); 920 } else { 921 fatal("--trigger requires a previous -e"); 922 } 923 break; 924 case '7': /* filter */ 925 if (params->common.events) { 926 retval = trace_event_add_filter(params->common.events, optarg); 927 if (retval) 928 fatal("Error adding filter %s", optarg); 929 } else { 930 fatal("--filter requires a previous -e"); 931 } 932 break; 933 case '8': 934 params->dma_latency = get_llong_from_str(optarg); 935 if (params->dma_latency < 0 || params->dma_latency > 10000) 936 fatal("--dma-latency needs to be >= 0 and < 10000"); 937 break; 938 case '9': 939 params->no_aa = 1; 940 break; 941 case '\1': 942 params->dump_tasks = 1; 943 break; 944 case '\2': 945 params->common.warmup = get_llong_from_str(optarg); 946 break; 947 case '\3': 948 params->common.buffer_size = get_llong_from_str(optarg); 949 break; 950 case '\4': 951 params->deepest_idle_state = get_llong_from_str(optarg); 952 break; 953 case '\5': 954 retval = actions_parse(¶ms->common.threshold_actions, optarg, 955 "timerlat_trace.txt"); 956 if (retval) 957 fatal("Invalid action %s", optarg); 958 break; 959 case '\6': 960 retval = actions_parse(¶ms->common.end_actions, optarg, 961 "timerlat_trace.txt"); 962 if (retval) 963 fatal("Invalid action %s", optarg); 964 break; 965 case '\7': 966 params->bpf_action_program = optarg; 967 break; 968 case '\10': 969 params->stack_format = parse_stack_format(optarg); 970 if (params->stack_format == -1) 971 fatal("Invalid --stack-format option"); 972 break; 973 default: 974 fatal("Invalid option"); 975 } 976 } 977 978 if (trace_output) 979 actions_add_trace_output(¶ms->common.threshold_actions, trace_output); 980 981 if (geteuid()) 982 fatal("rtla needs root permission"); 983 984 if (params->common.hist.no_irq && params->common.hist.no_thread) 985 fatal("no-irq and no-thread set, there is nothing to do here"); 986 987 if (params->common.hist.no_index && !params->common.hist.with_zeros) 988 fatal("no-index set with with-zeros is not set - it does not make sense"); 989 990 /* 991 * Auto analysis only happens if stop tracing, thus: 992 */ 993 if (!params->common.stop_us && !params->common.stop_total_us) 994 params->no_aa = 1; 995 996 if (params->common.kernel_workload && params->common.user_workload) 997 fatal("--kernel-threads and --user-threads are mutually exclusive!"); 998 999 /* 1000 * If auto-analysis or trace output is enabled, switch from BPF mode to 1001 * mixed mode 1002 */ 1003 if (params->mode == TRACING_MODE_BPF && 1004 (params->common.threshold_actions.present[ACTION_TRACE_OUTPUT] || 1005 params->common.end_actions.present[ACTION_TRACE_OUTPUT] || 1006 !params->no_aa)) 1007 params->mode = TRACING_MODE_MIXED; 1008 1009 return ¶ms->common; 1010 } 1011 1012 /* 1013 * timerlat_hist_apply_config - apply the hist configs to the initialized tool 1014 */ 1015 static int 1016 timerlat_hist_apply_config(struct osnoise_tool *tool) 1017 { 1018 struct timerlat_params *params = to_timerlat_params(tool->params); 1019 int retval; 1020 1021 retval = timerlat_apply_config(tool, params); 1022 if (retval) 1023 goto out_err; 1024 1025 return 0; 1026 1027 out_err: 1028 return -1; 1029 } 1030 1031 /* 1032 * timerlat_init_hist - initialize a timerlat hist tool with parameters 1033 */ 1034 static struct osnoise_tool 1035 *timerlat_init_hist(struct common_params *params) 1036 { 1037 struct osnoise_tool *tool; 1038 1039 tool = osnoise_init_tool("timerlat_hist"); 1040 if (!tool) 1041 return NULL; 1042 1043 tool->data = timerlat_alloc_histogram(params->hist.entries, 1044 params->hist.bucket_size); 1045 if (!tool->data) 1046 goto out_err; 1047 1048 tep_register_event_handler(tool->trace.tep, -1, "ftrace", "timerlat", 1049 timerlat_hist_handler, tool); 1050 1051 return tool; 1052 1053 out_err: 1054 osnoise_destroy_tool(tool); 1055 return NULL; 1056 } 1057 1058 static int timerlat_hist_bpf_main_loop(struct osnoise_tool *tool) 1059 { 1060 struct timerlat_params *params = to_timerlat_params(tool->params); 1061 int retval; 1062 1063 while (!stop_tracing) { 1064 timerlat_bpf_wait(-1); 1065 1066 if (!stop_tracing) { 1067 /* Threshold overflow, perform actions on threshold */ 1068 actions_perform(¶ms->common.threshold_actions); 1069 1070 if (!params->common.threshold_actions.continue_flag) 1071 /* continue flag not set, break */ 1072 break; 1073 1074 /* continue action reached, re-enable tracing */ 1075 if (tool->record) 1076 trace_instance_start(&tool->record->trace); 1077 if (tool->aa) 1078 trace_instance_start(&tool->aa->trace); 1079 timerlat_bpf_restart_tracing(); 1080 } 1081 } 1082 timerlat_bpf_detach(); 1083 1084 retval = timerlat_hist_bpf_pull_data(tool); 1085 if (retval) 1086 err_msg("Error pulling BPF data\n"); 1087 1088 return retval; 1089 } 1090 1091 static int timerlat_hist_main(struct osnoise_tool *tool) 1092 { 1093 struct timerlat_params *params = to_timerlat_params(tool->params); 1094 int retval; 1095 1096 if (params->mode == TRACING_MODE_TRACEFS) 1097 retval = hist_main_loop(tool); 1098 else 1099 retval = timerlat_hist_bpf_main_loop(tool); 1100 1101 return retval; 1102 } 1103 1104 struct tool_ops timerlat_hist_ops = { 1105 .tracer = "timerlat", 1106 .comm_prefix = "timerlat/", 1107 .parse_args = timerlat_hist_parse_args, 1108 .init_tool = timerlat_init_hist, 1109 .apply_config = timerlat_hist_apply_config, 1110 .enable = timerlat_enable, 1111 .main = timerlat_hist_main, 1112 .print_stats = timerlat_print_stats, 1113 .analyze = timerlat_analyze, 1114 .free = timerlat_free_histogram_tool, 1115 }; 1116