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 #include "common.h" 21 22 struct timerlat_top_cpu { 23 unsigned long long irq_count; 24 unsigned long long thread_count; 25 unsigned long long user_count; 26 27 unsigned long long cur_irq; 28 unsigned long long min_irq; 29 unsigned long long sum_irq; 30 unsigned long long max_irq; 31 32 unsigned long long cur_thread; 33 unsigned long long min_thread; 34 unsigned long long sum_thread; 35 unsigned long long max_thread; 36 37 unsigned long long cur_user; 38 unsigned long long min_user; 39 unsigned long long sum_user; 40 unsigned long long max_user; 41 }; 42 43 struct timerlat_top_data { 44 struct timerlat_top_cpu *cpu_data; 45 }; 46 47 /* 48 * timerlat_free_top - free runtime data 49 */ 50 static void timerlat_free_top(struct timerlat_top_data *data) 51 { 52 free(data->cpu_data); 53 free(data); 54 } 55 56 static void timerlat_free_top_tool(struct osnoise_tool *tool) 57 { 58 timerlat_free_top(tool->data); 59 timerlat_free(tool); 60 } 61 62 /* 63 * timerlat_alloc_histogram - alloc runtime data 64 */ 65 static struct timerlat_top_data *timerlat_alloc_top(void) 66 { 67 struct timerlat_top_data *data; 68 int cpu; 69 70 data = calloc(1, sizeof(*data)); 71 if (!data) 72 return NULL; 73 74 /* one set of histograms per CPU */ 75 data->cpu_data = calloc(1, sizeof(*data->cpu_data) * nr_cpus); 76 if (!data->cpu_data) 77 goto cleanup; 78 79 /* set the min to max */ 80 for (cpu = 0; cpu < nr_cpus; cpu++) { 81 data->cpu_data[cpu].min_irq = ~0; 82 data->cpu_data[cpu].min_thread = ~0; 83 data->cpu_data[cpu].min_user = ~0; 84 } 85 86 return data; 87 88 cleanup: 89 timerlat_free_top(data); 90 return NULL; 91 } 92 93 static void 94 timerlat_top_reset_sum(struct timerlat_top_cpu *summary) 95 { 96 memset(summary, 0, sizeof(*summary)); 97 summary->min_irq = ~0; 98 summary->min_thread = ~0; 99 summary->min_user = ~0; 100 } 101 102 static void 103 timerlat_top_update_sum(struct osnoise_tool *tool, int cpu, struct timerlat_top_cpu *sum) 104 { 105 struct timerlat_top_data *data = tool->data; 106 struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu]; 107 108 sum->irq_count += cpu_data->irq_count; 109 update_min(&sum->min_irq, &cpu_data->min_irq); 110 update_sum(&sum->sum_irq, &cpu_data->sum_irq); 111 update_max(&sum->max_irq, &cpu_data->max_irq); 112 113 sum->thread_count += cpu_data->thread_count; 114 update_min(&sum->min_thread, &cpu_data->min_thread); 115 update_sum(&sum->sum_thread, &cpu_data->sum_thread); 116 update_max(&sum->max_thread, &cpu_data->max_thread); 117 118 sum->user_count += cpu_data->user_count; 119 update_min(&sum->min_user, &cpu_data->min_user); 120 update_sum(&sum->sum_user, &cpu_data->sum_user); 121 update_max(&sum->max_user, &cpu_data->max_user); 122 } 123 124 /* 125 * timerlat_hist_update - record a new timerlat occurent on cpu, updating data 126 */ 127 static void 128 timerlat_top_update(struct osnoise_tool *tool, int cpu, 129 unsigned long long thread, 130 unsigned long long latency) 131 { 132 struct timerlat_params *params = to_timerlat_params(tool->params); 133 struct timerlat_top_data *data = tool->data; 134 struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu]; 135 136 if (params->common.output_divisor) 137 latency = latency / params->common.output_divisor; 138 139 if (!thread) { 140 cpu_data->irq_count++; 141 cpu_data->cur_irq = latency; 142 update_min(&cpu_data->min_irq, &latency); 143 update_sum(&cpu_data->sum_irq, &latency); 144 update_max(&cpu_data->max_irq, &latency); 145 } else if (thread == 1) { 146 cpu_data->thread_count++; 147 cpu_data->cur_thread = latency; 148 update_min(&cpu_data->min_thread, &latency); 149 update_sum(&cpu_data->sum_thread, &latency); 150 update_max(&cpu_data->max_thread, &latency); 151 } else { 152 cpu_data->user_count++; 153 cpu_data->cur_user = latency; 154 update_min(&cpu_data->min_user, &latency); 155 update_sum(&cpu_data->sum_user, &latency); 156 update_max(&cpu_data->max_user, &latency); 157 } 158 } 159 160 /* 161 * timerlat_top_handler - this is the handler for timerlat tracer events 162 */ 163 static int 164 timerlat_top_handler(struct trace_seq *s, struct tep_record *record, 165 struct tep_event *event, void *context) 166 { 167 struct trace_instance *trace = context; 168 unsigned long long latency, thread; 169 struct osnoise_tool *top; 170 int cpu = record->cpu; 171 172 top = container_of(trace, struct osnoise_tool, trace); 173 174 if (!top->params->aa_only) { 175 tep_get_field_val(s, event, "context", record, &thread, 1); 176 tep_get_field_val(s, event, "timer_latency", record, &latency, 1); 177 178 timerlat_top_update(top, cpu, thread, latency); 179 } 180 181 return 0; 182 } 183 184 /* 185 * timerlat_top_bpf_pull_data - copy data from BPF maps into userspace 186 */ 187 static int timerlat_top_bpf_pull_data(struct osnoise_tool *tool) 188 { 189 struct timerlat_top_data *data = tool->data; 190 int i, err; 191 long long value_irq[nr_cpus], 192 value_thread[nr_cpus], 193 value_user[nr_cpus]; 194 195 /* Pull summary */ 196 err = timerlat_bpf_get_summary_value(SUMMARY_CURRENT, 197 value_irq, value_thread, value_user); 198 if (err) 199 return err; 200 for (i = 0; i < nr_cpus; i++) { 201 data->cpu_data[i].cur_irq = value_irq[i]; 202 data->cpu_data[i].cur_thread = value_thread[i]; 203 data->cpu_data[i].cur_user = value_user[i]; 204 } 205 206 err = timerlat_bpf_get_summary_value(SUMMARY_COUNT, 207 value_irq, value_thread, value_user); 208 if (err) 209 return err; 210 for (i = 0; i < nr_cpus; i++) { 211 data->cpu_data[i].irq_count = value_irq[i]; 212 data->cpu_data[i].thread_count = value_thread[i]; 213 data->cpu_data[i].user_count = value_user[i]; 214 } 215 216 err = timerlat_bpf_get_summary_value(SUMMARY_MIN, 217 value_irq, value_thread, value_user); 218 if (err) 219 return err; 220 for (i = 0; i < nr_cpus; i++) { 221 data->cpu_data[i].min_irq = value_irq[i]; 222 data->cpu_data[i].min_thread = value_thread[i]; 223 data->cpu_data[i].min_user = value_user[i]; 224 } 225 226 err = timerlat_bpf_get_summary_value(SUMMARY_MAX, 227 value_irq, value_thread, value_user); 228 if (err) 229 return err; 230 for (i = 0; i < nr_cpus; i++) { 231 data->cpu_data[i].max_irq = value_irq[i]; 232 data->cpu_data[i].max_thread = value_thread[i]; 233 data->cpu_data[i].max_user = value_user[i]; 234 } 235 236 err = timerlat_bpf_get_summary_value(SUMMARY_SUM, 237 value_irq, value_thread, value_user); 238 if (err) 239 return err; 240 for (i = 0; i < nr_cpus; i++) { 241 data->cpu_data[i].sum_irq = value_irq[i]; 242 data->cpu_data[i].sum_thread = value_thread[i]; 243 data->cpu_data[i].sum_user = value_user[i]; 244 } 245 246 return 0; 247 } 248 249 /* 250 * timerlat_top_header - print the header of the tool output 251 */ 252 static void timerlat_top_header(struct timerlat_params *params, struct osnoise_tool *top) 253 { 254 struct trace_seq *s = top->trace.seq; 255 bool pretty = params->common.pretty_output; 256 char duration[26]; 257 258 get_duration(top->start_time, duration, sizeof(duration)); 259 260 if (pretty) 261 trace_seq_printf(s, "\033[2;37;40m"); 262 263 trace_seq_printf(s, " Timer Latency "); 264 if (params->common.user_data) 265 trace_seq_printf(s, " "); 266 267 if (pretty) 268 trace_seq_printf(s, "\033[0;0;0m"); 269 trace_seq_printf(s, "\n"); 270 271 trace_seq_printf(s, "%-6s | IRQ Timer Latency (%s) | Thread Timer Latency (%s)", duration, 272 params->common.output_divisor == 1 ? "ns" : "us", 273 params->common.output_divisor == 1 ? "ns" : "us"); 274 275 if (params->common.user_data) { 276 trace_seq_printf(s, " | Ret user Timer Latency (%s)", 277 params->common.output_divisor == 1 ? "ns" : "us"); 278 } 279 280 trace_seq_printf(s, "\n"); 281 if (pretty) 282 trace_seq_printf(s, "\033[2;30;47m"); 283 284 trace_seq_printf(s, "CPU COUNT | cur min avg max | cur min avg max"); 285 if (params->common.user_data) 286 trace_seq_printf(s, " | cur min avg max"); 287 288 if (pretty) 289 trace_seq_printf(s, "\033[0;0;0m"); 290 trace_seq_printf(s, "\n"); 291 } 292 293 static const char *no_value = " -"; 294 295 /* 296 * timerlat_top_print - prints the output of a given CPU 297 */ 298 static void timerlat_top_print(struct osnoise_tool *top, int cpu) 299 { 300 struct timerlat_params *params = to_timerlat_params(top->params); 301 struct timerlat_top_data *data = top->data; 302 struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu]; 303 struct trace_seq *s = top->trace.seq; 304 305 /* 306 * Skip if no data is available: is this cpu offline? 307 */ 308 if (!cpu_data->irq_count && !cpu_data->thread_count) 309 return; 310 311 /* 312 * Unless trace is being lost, IRQ counter is always the max. 313 */ 314 trace_seq_printf(s, "%3d #%-9llu |", cpu, cpu_data->irq_count); 315 316 if (!cpu_data->irq_count) { 317 trace_seq_printf(s, "%s %s %s %s |", no_value, no_value, no_value, no_value); 318 } else { 319 trace_seq_printf(s, "%9llu ", cpu_data->cur_irq); 320 trace_seq_printf(s, "%9llu ", cpu_data->min_irq); 321 trace_seq_printf(s, "%9llu ", cpu_data->sum_irq / cpu_data->irq_count); 322 trace_seq_printf(s, "%9llu |", cpu_data->max_irq); 323 } 324 325 if (!cpu_data->thread_count) { 326 trace_seq_printf(s, "%s %s %s %s", no_value, no_value, no_value, no_value); 327 } else { 328 trace_seq_printf(s, "%9llu ", cpu_data->cur_thread); 329 trace_seq_printf(s, "%9llu ", cpu_data->min_thread); 330 trace_seq_printf(s, "%9llu ", 331 cpu_data->sum_thread / cpu_data->thread_count); 332 trace_seq_printf(s, "%9llu", cpu_data->max_thread); 333 } 334 335 if (!params->common.user_data) { 336 trace_seq_printf(s, "\n"); 337 return; 338 } 339 340 trace_seq_printf(s, " |"); 341 342 if (!cpu_data->user_count) { 343 trace_seq_printf(s, "%s %s %s %s\n", no_value, no_value, no_value, no_value); 344 } else { 345 trace_seq_printf(s, "%9llu ", cpu_data->cur_user); 346 trace_seq_printf(s, "%9llu ", cpu_data->min_user); 347 trace_seq_printf(s, "%9llu ", 348 cpu_data->sum_user / cpu_data->user_count); 349 trace_seq_printf(s, "%9llu\n", cpu_data->max_user); 350 } 351 } 352 353 /* 354 * timerlat_top_print_sum - prints the summary output 355 */ 356 static void 357 timerlat_top_print_sum(struct osnoise_tool *top, struct timerlat_top_cpu *summary) 358 { 359 const char *split = "----------------------------------------"; 360 struct timerlat_params *params = to_timerlat_params(top->params); 361 unsigned long long count = summary->irq_count; 362 struct trace_seq *s = top->trace.seq; 363 int e = 0; 364 365 /* 366 * Skip if no data is available: is this cpu offline? 367 */ 368 if (!summary->irq_count && !summary->thread_count) 369 return; 370 371 while (count > 999999) { 372 e++; 373 count /= 10; 374 } 375 376 trace_seq_printf(s, "%.*s|%.*s|%.*s", 15, split, 40, split, 39, split); 377 if (params->common.user_data) 378 trace_seq_printf(s, "-|%.*s", 39, split); 379 trace_seq_printf(s, "\n"); 380 381 trace_seq_printf(s, "ALL #%-6llu e%d |", count, e); 382 383 if (!summary->irq_count) { 384 trace_seq_printf(s, " %s %s %s |", no_value, no_value, no_value); 385 } else { 386 trace_seq_printf(s, " "); 387 trace_seq_printf(s, "%9llu ", summary->min_irq); 388 trace_seq_printf(s, "%9llu ", summary->sum_irq / summary->irq_count); 389 trace_seq_printf(s, "%9llu |", summary->max_irq); 390 } 391 392 if (!summary->thread_count) { 393 trace_seq_printf(s, "%s %s %s %s", no_value, no_value, no_value, no_value); 394 } else { 395 trace_seq_printf(s, " "); 396 trace_seq_printf(s, "%9llu ", summary->min_thread); 397 trace_seq_printf(s, "%9llu ", 398 summary->sum_thread / summary->thread_count); 399 trace_seq_printf(s, "%9llu", summary->max_thread); 400 } 401 402 if (!params->common.user_data) { 403 trace_seq_printf(s, "\n"); 404 return; 405 } 406 407 trace_seq_printf(s, " |"); 408 409 if (!summary->user_count) { 410 trace_seq_printf(s, " %s %s %s |", no_value, no_value, no_value); 411 } else { 412 trace_seq_printf(s, " "); 413 trace_seq_printf(s, "%9llu ", summary->min_user); 414 trace_seq_printf(s, "%9llu ", 415 summary->sum_user / summary->user_count); 416 trace_seq_printf(s, "%9llu\n", summary->max_user); 417 } 418 } 419 420 /* 421 * clear_terminal - clears the output terminal 422 */ 423 static void clear_terminal(struct trace_seq *seq) 424 { 425 if (!config_debug) 426 trace_seq_printf(seq, "\033c"); 427 } 428 429 /* 430 * timerlat_print_stats - print data for all cpus 431 */ 432 static void 433 timerlat_print_stats(struct osnoise_tool *top) 434 { 435 struct timerlat_params *params = to_timerlat_params(top->params); 436 struct trace_instance *trace = &top->trace; 437 struct timerlat_top_cpu summary; 438 int i; 439 440 if (params->common.aa_only) 441 return; 442 443 if (!params->common.quiet) 444 clear_terminal(trace->seq); 445 446 timerlat_top_reset_sum(&summary); 447 448 timerlat_top_header(params, top); 449 450 for_each_monitored_cpu(i, ¶ms->common) { 451 timerlat_top_print(top, i); 452 timerlat_top_update_sum(top, i, &summary); 453 } 454 455 timerlat_top_print_sum(top, &summary); 456 457 trace_seq_do_printf(trace->seq); 458 trace_seq_reset(trace->seq); 459 osnoise_report_missed_events(top); 460 } 461 462 /* 463 * timerlat_top_usage - prints timerlat top usage message 464 */ 465 static void timerlat_top_usage(void) 466 { 467 static const char *const msg_start[] = { 468 "[-q] [-a us] [-d s] [-D] [-n] [-p us] [-i us] [-T us] [-s us] \\", 469 " [[-t [file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\", 470 " [-P priority] [--dma-latency us] [--aa-only us] [-C [cgroup_name]] [-u|-k] [--warm-up s] [--deepest-idle-state n]", 471 NULL, 472 }; 473 474 static const char *const msg_opts[] = { 475 " -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit", 476 " --aa-only us: stop if <us> latency is hit, only printing the auto analysis (reduces CPU usage)", 477 " -p/--period us: timerlat period in us", 478 " -i/--irq us: stop trace if the irq latency is higher than the argument in us", 479 " -T/--thread us: stop trace if the thread latency is higher than the argument in us", 480 " -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us", 481 " -c/--cpus cpus: run the tracer only on the given cpus", 482 " -H/--house-keeping cpus: run rtla control threads only on the given cpus", 483 " -C/--cgroup [cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited", 484 " -d/--duration time[s|m|h|d]: duration of the session", 485 " -D/--debug: print debug info", 486 " --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)", 487 " -t/--trace [file]: save the stopped trace to [file|timerlat_trace.txt]", 488 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed", 489 " --filter <command>: enable a trace event filter to the previous -e event", 490 " --trigger <command>: enable a trace event trigger to the previous -e event", 491 " -n/--nano: display data in nanoseconds", 492 " --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage", 493 " -q/--quiet print only a summary at the end", 494 " --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency", 495 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters", 496 " o:prio - use SCHED_OTHER with prio", 497 " r:prio - use SCHED_RR with prio", 498 " f:prio - use SCHED_FIFO with prio", 499 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period", 500 " in nanoseconds", 501 " -u/--user-threads: use rtla user-space threads instead of kernel-space timerlat threads", 502 " -k/--kernel-threads: use timerlat kernel-space threads instead of rtla user-space threads", 503 " -U/--user-load: enable timerlat for user-defined user-space workload", 504 " --warm-up s: let the workload run for s seconds before collecting data", 505 " --trace-buffer-size kB: set the per-cpu trace buffer size in kB", 506 " --deepest-idle-state n: only go down to idle state n on cpus used by timerlat to reduce exit from idle latency", 507 " --on-threshold <action>: define action to be executed at latency threshold, multiple are allowed", 508 " --on-end: define action to be executed at measurement end, multiple are allowed", 509 " --bpf-action <program>: load and execute BPF program when latency threshold is exceeded", 510 " --stack-format <format>: set the stack format (truncate, skip, full)", 511 NULL, 512 }; 513 514 common_usage("timerlat", "top", "a per-cpu summary of the timer latency", 515 msg_start, msg_opts); 516 } 517 518 /* 519 * timerlat_top_parse_args - allocs, parse and fill the cmd line parameters 520 */ 521 static struct common_params 522 *timerlat_top_parse_args(int argc, char **argv) 523 { 524 struct timerlat_params *params; 525 long long auto_thresh; 526 int retval; 527 int c; 528 char *trace_output = NULL; 529 530 params = calloc_fatal(1, sizeof(*params)); 531 532 actions_init(¶ms->common.threshold_actions); 533 actions_init(¶ms->common.end_actions); 534 535 /* disabled by default */ 536 params->dma_latency = -1; 537 538 /* disabled by default */ 539 params->deepest_idle_state = -2; 540 541 /* display data in microseconds */ 542 params->common.output_divisor = 1000; 543 544 /* default to BPF mode */ 545 params->mode = TRACING_MODE_BPF; 546 547 /* default to truncate stack format */ 548 params->stack_format = STACK_FORMAT_TRUNCATE; 549 550 while (1) { 551 static struct option long_options[] = { 552 COMMON_OPTIONS, 553 {"auto", required_argument, 0, 'a'}, 554 {"help", no_argument, 0, 'h'}, 555 {"irq", required_argument, 0, 'i'}, 556 {"nano", no_argument, 0, 'n'}, 557 {"period", required_argument, 0, 'p'}, 558 {"quiet", no_argument, 0, 'q'}, 559 {"stack", required_argument, 0, 's'}, 560 {"thread", required_argument, 0, 'T'}, 561 {"trace", optional_argument, 0, 't'}, 562 {"user-threads", no_argument, 0, 'u'}, 563 {"kernel-threads", no_argument, 0, 'k'}, 564 {"user-load", no_argument, 0, 'U'}, 565 {"trigger", required_argument, 0, '0'}, 566 {"filter", required_argument, 0, '1'}, 567 {"dma-latency", required_argument, 0, '2'}, 568 {"no-aa", no_argument, 0, '3'}, 569 {"dump-tasks", no_argument, 0, '4'}, 570 {"aa-only", required_argument, 0, '5'}, 571 {"warm-up", required_argument, 0, '6'}, 572 {"trace-buffer-size", required_argument, 0, '7'}, 573 {"deepest-idle-state", required_argument, 0, '8'}, 574 {"on-threshold", required_argument, 0, '9'}, 575 {"on-end", required_argument, 0, '\1'}, 576 {"bpf-action", required_argument, 0, '\2'}, 577 {"stack-format", required_argument, 0, '\3'}, 578 {0, 0, 0, 0} 579 }; 580 581 c = getopt_auto(argc, argv, long_options); 582 583 if (set_common_option(c, argc, argv, ¶ms->common)) 584 continue; 585 586 /* detect the end of the options. */ 587 if (c == -1) 588 break; 589 590 switch (c) { 591 case 'a': 592 auto_thresh = get_llong_from_str(optarg); 593 594 /* set thread stop to auto_thresh */ 595 params->common.stop_total_us = auto_thresh; 596 params->common.stop_us = auto_thresh; 597 598 /* get stack trace */ 599 params->print_stack = auto_thresh; 600 601 /* set trace */ 602 if (!trace_output) 603 trace_output = "timerlat_trace.txt"; 604 605 break; 606 case '5': 607 /* it is here because it is similar to -a */ 608 auto_thresh = get_llong_from_str(optarg); 609 610 /* set thread stop to auto_thresh */ 611 params->common.stop_total_us = auto_thresh; 612 params->common.stop_us = auto_thresh; 613 614 /* get stack trace */ 615 params->print_stack = auto_thresh; 616 617 /* set aa_only to avoid parsing the trace */ 618 params->common.aa_only = 1; 619 break; 620 case 'h': 621 case '?': 622 timerlat_top_usage(); 623 break; 624 case 'i': 625 params->common.stop_us = get_llong_from_str(optarg); 626 break; 627 case 'k': 628 params->common.kernel_workload = true; 629 break; 630 case 'n': 631 params->common.output_divisor = 1; 632 break; 633 case 'p': 634 params->timerlat_period_us = get_llong_from_str(optarg); 635 if (params->timerlat_period_us > 1000000) 636 fatal("Period longer than 1 s"); 637 break; 638 case 'q': 639 params->common.quiet = 1; 640 break; 641 case 's': 642 params->print_stack = get_llong_from_str(optarg); 643 break; 644 case 'T': 645 params->common.stop_total_us = get_llong_from_str(optarg); 646 break; 647 case 't': 648 trace_output = parse_optional_arg(argc, argv); 649 if (!trace_output) 650 trace_output = "timerlat_trace.txt"; 651 break; 652 case 'u': 653 params->common.user_workload = true; 654 /* fallback: -u implies -U */ 655 case 'U': 656 params->common.user_data = true; 657 break; 658 case '0': /* trigger */ 659 if (params->common.events) 660 trace_event_add_trigger(params->common.events, optarg); 661 else 662 fatal("--trigger requires a previous -e"); 663 break; 664 case '1': /* filter */ 665 if (params->common.events) 666 trace_event_add_filter(params->common.events, optarg); 667 else 668 fatal("--filter requires a previous -e"); 669 break; 670 case '2': /* dma-latency */ 671 params->dma_latency = get_llong_from_str(optarg); 672 if (params->dma_latency < 0 || params->dma_latency > 10000) 673 fatal("--dma-latency needs to be >= 0 and < 10000"); 674 break; 675 case '3': /* no-aa */ 676 params->no_aa = 1; 677 break; 678 case '4': 679 params->dump_tasks = 1; 680 break; 681 case '6': 682 params->common.warmup = get_llong_from_str(optarg); 683 break; 684 case '7': 685 params->common.buffer_size = get_llong_from_str(optarg); 686 break; 687 case '8': 688 params->deepest_idle_state = get_llong_from_str(optarg); 689 break; 690 case '9': 691 retval = actions_parse(¶ms->common.threshold_actions, optarg, 692 "timerlat_trace.txt"); 693 if (retval) 694 fatal("Invalid action %s", optarg); 695 break; 696 case '\1': 697 retval = actions_parse(¶ms->common.end_actions, optarg, 698 "timerlat_trace.txt"); 699 if (retval) 700 fatal("Invalid action %s", optarg); 701 break; 702 case '\2': 703 params->bpf_action_program = optarg; 704 break; 705 case '\3': 706 params->stack_format = parse_stack_format(optarg); 707 if (params->stack_format == -1) 708 fatal("Invalid --stack-format option"); 709 break; 710 default: 711 fatal("Invalid option"); 712 } 713 } 714 715 if (trace_output) 716 actions_add_trace_output(¶ms->common.threshold_actions, trace_output); 717 718 if (geteuid()) 719 fatal("rtla needs root permission"); 720 721 /* 722 * Auto analysis only happens if stop tracing, thus: 723 */ 724 if (!params->common.stop_us && !params->common.stop_total_us) 725 params->no_aa = 1; 726 727 if (params->no_aa && params->common.aa_only) 728 fatal("--no-aa and --aa-only are mutually exclusive!"); 729 730 if (params->common.kernel_workload && params->common.user_workload) 731 fatal("--kernel-threads and --user-threads are mutually exclusive!"); 732 733 /* 734 * If auto-analysis or trace output is enabled, switch from BPF mode to 735 * mixed mode 736 */ 737 if (params->mode == TRACING_MODE_BPF && 738 (params->common.threshold_actions.present[ACTION_TRACE_OUTPUT] || 739 params->common.end_actions.present[ACTION_TRACE_OUTPUT] || 740 !params->no_aa)) 741 params->mode = TRACING_MODE_MIXED; 742 743 return ¶ms->common; 744 } 745 746 /* 747 * timerlat_top_apply_config - apply the top configs to the initialized tool 748 */ 749 static int 750 timerlat_top_apply_config(struct osnoise_tool *top) 751 { 752 struct timerlat_params *params = to_timerlat_params(top->params); 753 int retval; 754 755 retval = timerlat_apply_config(top, params); 756 if (retval) 757 goto out_err; 758 759 if (isatty(STDOUT_FILENO) && !params->common.quiet) 760 params->common.pretty_output = 1; 761 762 return 0; 763 764 out_err: 765 return -1; 766 } 767 768 /* 769 * timerlat_init_top - initialize a timerlat top tool with parameters 770 */ 771 static struct osnoise_tool 772 *timerlat_init_top(struct common_params *params) 773 { 774 struct osnoise_tool *top; 775 776 top = osnoise_init_tool("timerlat_top"); 777 if (!top) 778 return NULL; 779 780 top->data = timerlat_alloc_top(); 781 if (!top->data) 782 goto out_err; 783 784 tep_register_event_handler(top->trace.tep, -1, "ftrace", "timerlat", 785 timerlat_top_handler, top); 786 787 return top; 788 789 out_err: 790 osnoise_destroy_tool(top); 791 return NULL; 792 } 793 794 /* 795 * timerlat_top_bpf_main_loop - main loop to process events (BPF variant) 796 */ 797 static int 798 timerlat_top_bpf_main_loop(struct osnoise_tool *tool) 799 { 800 const struct common_params *params = tool->params; 801 int retval, wait_retval; 802 803 if (params->aa_only) { 804 /* Auto-analysis only, just wait for stop tracing */ 805 timerlat_bpf_wait(-1); 806 return 0; 807 } 808 809 /* Pull and display data in a loop */ 810 while (!stop_tracing) { 811 wait_retval = timerlat_bpf_wait(params->quiet ? -1 : 812 params->sleep_time); 813 814 retval = timerlat_top_bpf_pull_data(tool); 815 if (retval) { 816 err_msg("Error pulling BPF data\n"); 817 return retval; 818 } 819 820 if (!params->quiet) 821 timerlat_print_stats(tool); 822 823 if (wait_retval != 0) { 824 /* Stopping requested by tracer */ 825 retval = common_threshold_handler(tool); 826 if (retval) 827 return retval; 828 829 if (!should_continue_tracing(tool->params)) 830 break; 831 832 if (timerlat_bpf_restart_tracing()) { 833 err_msg("Error restarting BPF trace\n"); 834 return -1; 835 } 836 } 837 838 /* is there still any user-threads ? */ 839 if (params->user_workload) { 840 if (params->user.stopped_running) { 841 debug_msg("timerlat user space threads stopped!\n"); 842 break; 843 } 844 } 845 } 846 847 return 0; 848 } 849 850 static int timerlat_top_main_loop(struct osnoise_tool *tool) 851 { 852 struct timerlat_params *params = to_timerlat_params(tool->params); 853 int retval; 854 855 if (params->mode == TRACING_MODE_TRACEFS) { 856 retval = top_main_loop(tool); 857 } else { 858 retval = timerlat_top_bpf_main_loop(tool); 859 timerlat_bpf_detach(); 860 } 861 862 return retval; 863 } 864 865 struct tool_ops timerlat_top_ops = { 866 .tracer = "timerlat", 867 .comm_prefix = "timerlat/", 868 .parse_args = timerlat_top_parse_args, 869 .init_tool = timerlat_init_top, 870 .apply_config = timerlat_top_apply_config, 871 .enable = timerlat_enable, 872 .main = timerlat_top_main_loop, 873 .print_stats = timerlat_print_stats, 874 .analyze = timerlat_analyze, 875 .free = timerlat_free_top_tool, 876 }; 877