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