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 NULL, 522 }; 523 524 common_usage("timerlat", "top", "a per-cpu summary of the timer latency", 525 msg_start, msg_opts); 526 } 527 528 /* 529 * timerlat_top_parse_args - allocs, parse and fill the cmd line parameters 530 */ 531 static struct common_params 532 *timerlat_top_parse_args(int argc, char **argv) 533 { 534 struct timerlat_params *params; 535 long long auto_thresh; 536 int retval; 537 int c; 538 char *trace_output = NULL; 539 540 params = calloc(1, sizeof(*params)); 541 if (!params) 542 exit(1); 543 544 actions_init(¶ms->common.threshold_actions); 545 actions_init(¶ms->common.end_actions); 546 547 /* disabled by default */ 548 params->dma_latency = -1; 549 550 /* disabled by default */ 551 params->deepest_idle_state = -2; 552 553 /* display data in microseconds */ 554 params->common.output_divisor = 1000; 555 556 /* default to BPF mode */ 557 params->mode = TRACING_MODE_BPF; 558 559 while (1) { 560 static struct option long_options[] = { 561 {"auto", required_argument, 0, 'a'}, 562 {"help", no_argument, 0, 'h'}, 563 {"irq", required_argument, 0, 'i'}, 564 {"nano", no_argument, 0, 'n'}, 565 {"period", required_argument, 0, 'p'}, 566 {"quiet", no_argument, 0, 'q'}, 567 {"stack", required_argument, 0, 's'}, 568 {"thread", required_argument, 0, 'T'}, 569 {"trace", optional_argument, 0, 't'}, 570 {"user-threads", no_argument, 0, 'u'}, 571 {"kernel-threads", no_argument, 0, 'k'}, 572 {"user-load", no_argument, 0, 'U'}, 573 {"trigger", required_argument, 0, '0'}, 574 {"filter", required_argument, 0, '1'}, 575 {"dma-latency", required_argument, 0, '2'}, 576 {"no-aa", no_argument, 0, '3'}, 577 {"dump-tasks", no_argument, 0, '4'}, 578 {"aa-only", required_argument, 0, '5'}, 579 {"warm-up", required_argument, 0, '6'}, 580 {"trace-buffer-size", required_argument, 0, '7'}, 581 {"deepest-idle-state", required_argument, 0, '8'}, 582 {"on-threshold", required_argument, 0, '9'}, 583 {"on-end", required_argument, 0, '\1'}, 584 {"bpf-action", required_argument, 0, '\2'}, 585 {0, 0, 0, 0} 586 }; 587 588 if (common_parse_options(argc, argv, ¶ms->common)) 589 continue; 590 591 c = getopt_long(argc, argv, "a:hi:knp:qs:t::T:uU0:1:2:345:6:7:", 592 long_options, NULL); 593 594 /* detect the end of the options. */ 595 if (c == -1) 596 break; 597 598 switch (c) { 599 case 'a': 600 auto_thresh = get_llong_from_str(optarg); 601 602 /* set thread stop to auto_thresh */ 603 params->common.stop_total_us = auto_thresh; 604 params->common.stop_us = auto_thresh; 605 606 /* get stack trace */ 607 params->print_stack = auto_thresh; 608 609 /* set trace */ 610 if (!trace_output) 611 trace_output = "timerlat_trace.txt"; 612 613 break; 614 case '5': 615 /* it is here because it is similar to -a */ 616 auto_thresh = get_llong_from_str(optarg); 617 618 /* set thread stop to auto_thresh */ 619 params->common.stop_total_us = auto_thresh; 620 params->common.stop_us = auto_thresh; 621 622 /* get stack trace */ 623 params->print_stack = auto_thresh; 624 625 /* set aa_only to avoid parsing the trace */ 626 params->common.aa_only = 1; 627 break; 628 case 'h': 629 case '?': 630 timerlat_top_usage(); 631 break; 632 case 'i': 633 params->common.stop_us = get_llong_from_str(optarg); 634 break; 635 case 'k': 636 params->common.kernel_workload = true; 637 break; 638 case 'n': 639 params->common.output_divisor = 1; 640 break; 641 case 'p': 642 params->timerlat_period_us = get_llong_from_str(optarg); 643 if (params->timerlat_period_us > 1000000) 644 fatal("Period longer than 1 s"); 645 break; 646 case 'q': 647 params->common.quiet = 1; 648 break; 649 case 's': 650 params->print_stack = get_llong_from_str(optarg); 651 break; 652 case 'T': 653 params->common.stop_total_us = get_llong_from_str(optarg); 654 break; 655 case 't': 656 trace_output = parse_optional_arg(argc, argv); 657 if (!trace_output) 658 trace_output = "timerlat_trace.txt"; 659 break; 660 case 'u': 661 params->common.user_workload = true; 662 /* fallback: -u implies -U */ 663 case 'U': 664 params->common.user_data = true; 665 break; 666 case '0': /* trigger */ 667 if (params->common.events) { 668 retval = trace_event_add_trigger(params->common.events, optarg); 669 if (retval) 670 fatal("Error adding trigger %s", optarg); 671 } else { 672 fatal("--trigger requires a previous -e"); 673 } 674 break; 675 case '1': /* filter */ 676 if (params->common.events) { 677 retval = trace_event_add_filter(params->common.events, optarg); 678 if (retval) 679 fatal("Error adding filter %s", optarg); 680 } else { 681 fatal("--filter requires a previous -e"); 682 } 683 break; 684 case '2': /* dma-latency */ 685 params->dma_latency = get_llong_from_str(optarg); 686 if (params->dma_latency < 0 || params->dma_latency > 10000) 687 fatal("--dma-latency needs to be >= 0 and < 10000"); 688 break; 689 case '3': /* no-aa */ 690 params->no_aa = 1; 691 break; 692 case '4': 693 params->dump_tasks = 1; 694 break; 695 case '6': 696 params->common.warmup = get_llong_from_str(optarg); 697 break; 698 case '7': 699 params->common.buffer_size = get_llong_from_str(optarg); 700 break; 701 case '8': 702 params->deepest_idle_state = get_llong_from_str(optarg); 703 break; 704 case '9': 705 retval = actions_parse(¶ms->common.threshold_actions, optarg, 706 "timerlat_trace.txt"); 707 if (retval) 708 fatal("Invalid action %s", optarg); 709 break; 710 case '\1': 711 retval = actions_parse(¶ms->common.end_actions, optarg, 712 "timerlat_trace.txt"); 713 if (retval) 714 fatal("Invalid action %s", optarg); 715 break; 716 case '\2': 717 params->bpf_action_program = optarg; 718 break; 719 default: 720 fatal("Invalid option"); 721 } 722 } 723 724 if (trace_output) 725 actions_add_trace_output(¶ms->common.threshold_actions, trace_output); 726 727 if (geteuid()) 728 fatal("rtla needs root permission"); 729 730 /* 731 * Auto analysis only happens if stop tracing, thus: 732 */ 733 if (!params->common.stop_us && !params->common.stop_total_us) 734 params->no_aa = 1; 735 736 if (params->no_aa && params->common.aa_only) 737 fatal("--no-aa and --aa-only are mutually exclusive!"); 738 739 if (params->common.kernel_workload && params->common.user_workload) 740 fatal("--kernel-threads and --user-threads are mutually exclusive!"); 741 742 /* 743 * If auto-analysis or trace output is enabled, switch from BPF mode to 744 * mixed mode 745 */ 746 if (params->mode == TRACING_MODE_BPF && 747 (params->common.threshold_actions.present[ACTION_TRACE_OUTPUT] || 748 params->common.end_actions.present[ACTION_TRACE_OUTPUT] || 749 !params->no_aa)) 750 params->mode = TRACING_MODE_MIXED; 751 752 return ¶ms->common; 753 } 754 755 /* 756 * timerlat_top_apply_config - apply the top configs to the initialized tool 757 */ 758 static int 759 timerlat_top_apply_config(struct osnoise_tool *top) 760 { 761 struct timerlat_params *params = to_timerlat_params(top->params); 762 int retval; 763 764 retval = timerlat_apply_config(top, params); 765 if (retval) 766 goto out_err; 767 768 if (isatty(STDOUT_FILENO) && !params->common.quiet) 769 params->common.pretty_output = 1; 770 771 return 0; 772 773 out_err: 774 return -1; 775 } 776 777 /* 778 * timerlat_init_top - initialize a timerlat top tool with parameters 779 */ 780 static struct osnoise_tool 781 *timerlat_init_top(struct common_params *params) 782 { 783 struct osnoise_tool *top; 784 int nr_cpus; 785 786 nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 787 788 top = osnoise_init_tool("timerlat_top"); 789 if (!top) 790 return NULL; 791 792 top->data = timerlat_alloc_top(nr_cpus); 793 if (!top->data) 794 goto out_err; 795 796 tep_register_event_handler(top->trace.tep, -1, "ftrace", "timerlat", 797 timerlat_top_handler, top); 798 799 return top; 800 801 out_err: 802 osnoise_destroy_tool(top); 803 return NULL; 804 } 805 806 /* 807 * timerlat_top_bpf_main_loop - main loop to process events (BPF variant) 808 */ 809 static int 810 timerlat_top_bpf_main_loop(struct osnoise_tool *tool) 811 { 812 struct timerlat_params *params = to_timerlat_params(tool->params); 813 int retval, wait_retval; 814 815 if (params->common.aa_only) { 816 /* Auto-analysis only, just wait for stop tracing */ 817 timerlat_bpf_wait(-1); 818 return 0; 819 } 820 821 /* Pull and display data in a loop */ 822 while (!stop_tracing) { 823 wait_retval = timerlat_bpf_wait(params->common.quiet ? -1 : 824 params->common.sleep_time); 825 826 retval = timerlat_top_bpf_pull_data(tool); 827 if (retval) { 828 err_msg("Error pulling BPF data\n"); 829 return retval; 830 } 831 832 if (!params->common.quiet) 833 timerlat_print_stats(tool); 834 835 if (wait_retval != 0) { 836 /* Stopping requested by tracer */ 837 actions_perform(¶ms->common.threshold_actions); 838 839 if (!params->common.threshold_actions.continue_flag) 840 /* continue flag not set, break */ 841 break; 842 843 /* continue action reached, re-enable tracing */ 844 if (tool->record) 845 trace_instance_start(&tool->record->trace); 846 if (tool->aa) 847 trace_instance_start(&tool->aa->trace); 848 timerlat_bpf_restart_tracing(); 849 } 850 851 /* is there still any user-threads ? */ 852 if (params->common.user_workload) { 853 if (params->common.user.stopped_running) { 854 debug_msg("timerlat user space threads stopped!\n"); 855 break; 856 } 857 } 858 } 859 860 return 0; 861 } 862 863 static int timerlat_top_main_loop(struct osnoise_tool *tool) 864 { 865 struct timerlat_params *params = to_timerlat_params(tool->params); 866 int retval; 867 868 if (params->mode == TRACING_MODE_TRACEFS) { 869 retval = top_main_loop(tool); 870 } else { 871 retval = timerlat_top_bpf_main_loop(tool); 872 timerlat_bpf_detach(); 873 } 874 875 return retval; 876 } 877 878 struct tool_ops timerlat_top_ops = { 879 .tracer = "timerlat", 880 .comm_prefix = "timerlat/", 881 .parse_args = timerlat_top_parse_args, 882 .init_tool = timerlat_init_top, 883 .apply_config = timerlat_top_apply_config, 884 .enable = timerlat_enable, 885 .main = timerlat_top_main_loop, 886 .print_stats = timerlat_print_stats, 887 .analyze = timerlat_analyze, 888 .free = timerlat_free_top_tool, 889 }; 890