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