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