0001
0002
0003
0004
0005
0006 #include <getopt.h>
0007 #include <stdlib.h>
0008 #include <string.h>
0009 #include <signal.h>
0010 #include <unistd.h>
0011 #include <stdio.h>
0012 #include <time.h>
0013
0014 #include "utils.h"
0015 #include "osnoise.h"
0016 #include "timerlat.h"
0017
0018 struct timerlat_top_params {
0019 char *cpus;
0020 char *monitored_cpus;
0021 char *trace_output;
0022 unsigned long long runtime;
0023 long long stop_us;
0024 long long stop_total_us;
0025 long long timerlat_period_us;
0026 long long print_stack;
0027 int sleep_time;
0028 int output_divisor;
0029 int duration;
0030 int quiet;
0031 int set_sched;
0032 int dma_latency;
0033 struct sched_attr sched_param;
0034 struct trace_events *events;
0035 };
0036
0037 struct timerlat_top_cpu {
0038 int irq_count;
0039 int thread_count;
0040
0041 unsigned long long cur_irq;
0042 unsigned long long min_irq;
0043 unsigned long long sum_irq;
0044 unsigned long long max_irq;
0045
0046 unsigned long long cur_thread;
0047 unsigned long long min_thread;
0048 unsigned long long sum_thread;
0049 unsigned long long max_thread;
0050 };
0051
0052 struct timerlat_top_data {
0053 struct timerlat_top_cpu *cpu_data;
0054 int nr_cpus;
0055 };
0056
0057
0058
0059
0060 static void
0061 timerlat_free_top(struct timerlat_top_data *data)
0062 {
0063 free(data->cpu_data);
0064 free(data);
0065 }
0066
0067
0068
0069
0070 static struct timerlat_top_data *timerlat_alloc_top(int nr_cpus)
0071 {
0072 struct timerlat_top_data *data;
0073 int cpu;
0074
0075 data = calloc(1, sizeof(*data));
0076 if (!data)
0077 return NULL;
0078
0079 data->nr_cpus = nr_cpus;
0080
0081
0082 data->cpu_data = calloc(1, sizeof(*data->cpu_data) * nr_cpus);
0083 if (!data->cpu_data)
0084 goto cleanup;
0085
0086
0087 for (cpu = 0; cpu < nr_cpus; cpu++) {
0088 data->cpu_data[cpu].min_irq = ~0;
0089 data->cpu_data[cpu].min_thread = ~0;
0090 }
0091
0092 return data;
0093
0094 cleanup:
0095 timerlat_free_top(data);
0096 return NULL;
0097 }
0098
0099
0100
0101
0102 static void
0103 timerlat_top_update(struct osnoise_tool *tool, int cpu,
0104 unsigned long long thread,
0105 unsigned long long latency)
0106 {
0107 struct timerlat_top_data *data = tool->data;
0108 struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
0109
0110 if (!thread) {
0111 cpu_data->irq_count++;
0112 cpu_data->cur_irq = latency;
0113 update_min(&cpu_data->min_irq, &latency);
0114 update_sum(&cpu_data->sum_irq, &latency);
0115 update_max(&cpu_data->max_irq, &latency);
0116 } else {
0117 cpu_data->thread_count++;
0118 cpu_data->cur_thread = latency;
0119 update_min(&cpu_data->min_thread, &latency);
0120 update_sum(&cpu_data->sum_thread, &latency);
0121 update_max(&cpu_data->max_thread, &latency);
0122 }
0123 }
0124
0125
0126
0127
0128 static int
0129 timerlat_top_handler(struct trace_seq *s, struct tep_record *record,
0130 struct tep_event *event, void *context)
0131 {
0132 struct trace_instance *trace = context;
0133 unsigned long long latency, thread;
0134 struct osnoise_tool *top;
0135 int cpu = record->cpu;
0136
0137 top = container_of(trace, struct osnoise_tool, trace);
0138
0139 tep_get_field_val(s, event, "context", record, &thread, 1);
0140 tep_get_field_val(s, event, "timer_latency", record, &latency, 1);
0141
0142 timerlat_top_update(top, cpu, thread, latency);
0143
0144 return 0;
0145 }
0146
0147
0148
0149
0150 static void timerlat_top_header(struct osnoise_tool *top)
0151 {
0152 struct timerlat_top_params *params = top->params;
0153 struct trace_seq *s = top->trace.seq;
0154 char duration[26];
0155
0156 get_duration(top->start_time, duration, sizeof(duration));
0157
0158 trace_seq_printf(s, "\033[2;37;40m");
0159 trace_seq_printf(s, " Timer Latency ");
0160 trace_seq_printf(s, "\033[0;0;0m");
0161 trace_seq_printf(s, "\n");
0162
0163 trace_seq_printf(s, "%-6s | IRQ Timer Latency (%s) | Thread Timer Latency (%s)\n", duration,
0164 params->output_divisor == 1 ? "ns" : "us",
0165 params->output_divisor == 1 ? "ns" : "us");
0166
0167 trace_seq_printf(s, "\033[2;30;47m");
0168 trace_seq_printf(s, "CPU COUNT | cur min avg max | cur min avg max");
0169 trace_seq_printf(s, "\033[0;0;0m");
0170 trace_seq_printf(s, "\n");
0171 }
0172
0173
0174
0175
0176 static void timerlat_top_print(struct osnoise_tool *top, int cpu)
0177 {
0178
0179 struct timerlat_top_params *params = top->params;
0180 struct timerlat_top_data *data = top->data;
0181 struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
0182 int divisor = params->output_divisor;
0183 struct trace_seq *s = top->trace.seq;
0184
0185 if (divisor == 0)
0186 return;
0187
0188
0189
0190
0191 if (!cpu_data->irq_count && !cpu_data->thread_count)
0192 return;
0193
0194
0195
0196
0197 trace_seq_printf(s, "%3d #%-9d |", cpu, cpu_data->irq_count);
0198
0199 if (!cpu_data->irq_count) {
0200 trace_seq_printf(s, " - ");
0201 trace_seq_printf(s, " - ");
0202 trace_seq_printf(s, " - ");
0203 trace_seq_printf(s, " - |");
0204 } else {
0205 trace_seq_printf(s, "%9llu ", cpu_data->cur_irq / params->output_divisor);
0206 trace_seq_printf(s, "%9llu ", cpu_data->min_irq / params->output_divisor);
0207 trace_seq_printf(s, "%9llu ", (cpu_data->sum_irq / cpu_data->irq_count) / divisor);
0208 trace_seq_printf(s, "%9llu |", cpu_data->max_irq / divisor);
0209 }
0210
0211 if (!cpu_data->thread_count) {
0212 trace_seq_printf(s, " - ");
0213 trace_seq_printf(s, " - ");
0214 trace_seq_printf(s, " - ");
0215 trace_seq_printf(s, " -\n");
0216 } else {
0217 trace_seq_printf(s, "%9llu ", cpu_data->cur_thread / divisor);
0218 trace_seq_printf(s, "%9llu ", cpu_data->min_thread / divisor);
0219 trace_seq_printf(s, "%9llu ",
0220 (cpu_data->sum_thread / cpu_data->thread_count) / divisor);
0221 trace_seq_printf(s, "%9llu\n", cpu_data->max_thread / divisor);
0222 }
0223 }
0224
0225
0226
0227
0228 static void clear_terminal(struct trace_seq *seq)
0229 {
0230 if (!config_debug)
0231 trace_seq_printf(seq, "\033c");
0232 }
0233
0234
0235
0236
0237 static void
0238 timerlat_print_stats(struct timerlat_top_params *params, struct osnoise_tool *top)
0239 {
0240 struct trace_instance *trace = &top->trace;
0241 static int nr_cpus = -1;
0242 int i;
0243
0244 if (nr_cpus == -1)
0245 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
0246
0247 if (!params->quiet)
0248 clear_terminal(trace->seq);
0249
0250 timerlat_top_header(top);
0251
0252 for (i = 0; i < nr_cpus; i++) {
0253 if (params->cpus && !params->monitored_cpus[i])
0254 continue;
0255 timerlat_top_print(top, i);
0256 }
0257
0258 trace_seq_do_printf(trace->seq);
0259 trace_seq_reset(trace->seq);
0260 }
0261
0262
0263
0264
0265 static void timerlat_top_usage(char *usage)
0266 {
0267 int i;
0268
0269 static const char *const msg[] = {
0270 "",
0271 " usage: rtla timerlat [top] [-h] [-q] [-a us] [-d s] [-D] [-n] [-p us] [-i us] [-T us] [-s us] \\",
0272 " [[-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] \\",
0273 " [-P priority] [--dma-latency us]",
0274 "",
0275 " -h/--help: print this menu",
0276 " -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",
0277 " -p/--period us: timerlat period in us",
0278 " -i/--irq us: stop trace if the irq latency is higher than the argument in us",
0279 " -T/--thread us: stop trace if the thread latency is higher than the argument in us",
0280 " -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",
0281 " -c/--cpus cpus: run the tracer only on the given cpus",
0282 " -d/--duration time[m|h|d]: duration of the session in seconds",
0283 " -D/--debug: print debug info",
0284 " -t/--trace[=file]: save the stopped trace to [file|timerlat_trace.txt]",
0285 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
0286 " --filter <command>: enable a trace event filter to the previous -e event",
0287 " --trigger <command>: enable a trace event trigger to the previous -e event",
0288 " -n/--nano: display data in nanoseconds",
0289 " -q/--quiet print only a summary at the end",
0290 " --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency",
0291 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
0292 " o:prio - use SCHED_OTHER with prio",
0293 " r:prio - use SCHED_RR with prio",
0294 " f:prio - use SCHED_FIFO with prio",
0295 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
0296 " in nanoseconds",
0297 NULL,
0298 };
0299
0300 if (usage)
0301 fprintf(stderr, "%s\n", usage);
0302
0303 fprintf(stderr, "rtla timerlat top: a per-cpu summary of the timer latency (version %s)\n",
0304 VERSION);
0305
0306 for (i = 0; msg[i]; i++)
0307 fprintf(stderr, "%s\n", msg[i]);
0308 exit(1);
0309 }
0310
0311
0312
0313
0314 static struct timerlat_top_params
0315 *timerlat_top_parse_args(int argc, char **argv)
0316 {
0317 struct timerlat_top_params *params;
0318 struct trace_events *tevent;
0319 long long auto_thresh;
0320 int retval;
0321 int c;
0322
0323 params = calloc(1, sizeof(*params));
0324 if (!params)
0325 exit(1);
0326
0327
0328 params->dma_latency = -1;
0329
0330
0331 params->output_divisor = 1000;
0332
0333 while (1) {
0334 static struct option long_options[] = {
0335 {"auto", required_argument, 0, 'a'},
0336 {"cpus", required_argument, 0, 'c'},
0337 {"debug", no_argument, 0, 'D'},
0338 {"duration", required_argument, 0, 'd'},
0339 {"event", required_argument, 0, 'e'},
0340 {"help", no_argument, 0, 'h'},
0341 {"irq", required_argument, 0, 'i'},
0342 {"nano", no_argument, 0, 'n'},
0343 {"period", required_argument, 0, 'p'},
0344 {"priority", required_argument, 0, 'P'},
0345 {"quiet", no_argument, 0, 'q'},
0346 {"stack", required_argument, 0, 's'},
0347 {"thread", required_argument, 0, 'T'},
0348 {"trace", optional_argument, 0, 't'},
0349 {"trigger", required_argument, 0, '0'},
0350 {"filter", required_argument, 0, '1'},
0351 {"dma-latency", required_argument, 0, '2'},
0352 {0, 0, 0, 0}
0353 };
0354
0355
0356 int option_index = 0;
0357
0358 c = getopt_long(argc, argv, "a:c:d:De:hi:np:P:qs:t::T:0:1:2:",
0359 long_options, &option_index);
0360
0361
0362 if (c == -1)
0363 break;
0364
0365 switch (c) {
0366 case 'a':
0367 auto_thresh = get_llong_from_str(optarg);
0368
0369
0370 params->stop_total_us = auto_thresh;
0371
0372
0373 params->print_stack = auto_thresh;
0374
0375
0376 params->trace_output = "timerlat_trace.txt";
0377
0378 break;
0379 case 'c':
0380 retval = parse_cpu_list(optarg, ¶ms->monitored_cpus);
0381 if (retval)
0382 timerlat_top_usage("\nInvalid -c cpu list\n");
0383 params->cpus = optarg;
0384 break;
0385 case 'D':
0386 config_debug = 1;
0387 break;
0388 case 'd':
0389 params->duration = parse_seconds_duration(optarg);
0390 if (!params->duration)
0391 timerlat_top_usage("Invalid -D duration\n");
0392 break;
0393 case 'e':
0394 tevent = trace_event_alloc(optarg);
0395 if (!tevent) {
0396 err_msg("Error alloc trace event");
0397 exit(EXIT_FAILURE);
0398 }
0399
0400 if (params->events)
0401 tevent->next = params->events;
0402 params->events = tevent;
0403 break;
0404 case 'h':
0405 case '?':
0406 timerlat_top_usage(NULL);
0407 break;
0408 case 'i':
0409 params->stop_us = get_llong_from_str(optarg);
0410 break;
0411 case 'n':
0412 params->output_divisor = 1;
0413 break;
0414 case 'p':
0415 params->timerlat_period_us = get_llong_from_str(optarg);
0416 if (params->timerlat_period_us > 1000000)
0417 timerlat_top_usage("Period longer than 1 s\n");
0418 break;
0419 case 'P':
0420 retval = parse_prio(optarg, ¶ms->sched_param);
0421 if (retval == -1)
0422 timerlat_top_usage("Invalid -P priority");
0423 params->set_sched = 1;
0424 break;
0425 case 'q':
0426 params->quiet = 1;
0427 break;
0428 case 's':
0429 params->print_stack = get_llong_from_str(optarg);
0430 break;
0431 case 'T':
0432 params->stop_total_us = get_llong_from_str(optarg);
0433 break;
0434 case 't':
0435 if (optarg)
0436
0437 params->trace_output = &optarg[1];
0438 else
0439 params->trace_output = "timerlat_trace.txt";
0440 break;
0441 case '0':
0442 if (params->events) {
0443 retval = trace_event_add_trigger(params->events, optarg);
0444 if (retval) {
0445 err_msg("Error adding trigger %s\n", optarg);
0446 exit(EXIT_FAILURE);
0447 }
0448 } else {
0449 timerlat_top_usage("--trigger requires a previous -e\n");
0450 }
0451 break;
0452 case '1':
0453 if (params->events) {
0454 retval = trace_event_add_filter(params->events, optarg);
0455 if (retval) {
0456 err_msg("Error adding filter %s\n", optarg);
0457 exit(EXIT_FAILURE);
0458 }
0459 } else {
0460 timerlat_top_usage("--filter requires a previous -e\n");
0461 }
0462 break;
0463 case '2':
0464 params->dma_latency = get_llong_from_str(optarg);
0465 if (params->dma_latency < 0 || params->dma_latency > 10000) {
0466 err_msg("--dma-latency needs to be >= 0 and < 10000");
0467 exit(EXIT_FAILURE);
0468 }
0469 break;
0470 default:
0471 timerlat_top_usage("Invalid option");
0472 }
0473 }
0474
0475 if (geteuid()) {
0476 err_msg("rtla needs root permission\n");
0477 exit(EXIT_FAILURE);
0478 }
0479
0480 return params;
0481 }
0482
0483
0484
0485
0486 static int
0487 timerlat_top_apply_config(struct osnoise_tool *top, struct timerlat_top_params *params)
0488 {
0489 int retval;
0490
0491 if (!params->sleep_time)
0492 params->sleep_time = 1;
0493
0494 if (params->cpus) {
0495 retval = osnoise_set_cpus(top->context, params->cpus);
0496 if (retval) {
0497 err_msg("Failed to apply CPUs config\n");
0498 goto out_err;
0499 }
0500 }
0501
0502 if (params->stop_us) {
0503 retval = osnoise_set_stop_us(top->context, params->stop_us);
0504 if (retval) {
0505 err_msg("Failed to set stop us\n");
0506 goto out_err;
0507 }
0508 }
0509
0510 if (params->stop_total_us) {
0511 retval = osnoise_set_stop_total_us(top->context, params->stop_total_us);
0512 if (retval) {
0513 err_msg("Failed to set stop total us\n");
0514 goto out_err;
0515 }
0516 }
0517
0518
0519 if (params->timerlat_period_us) {
0520 retval = osnoise_set_timerlat_period_us(top->context, params->timerlat_period_us);
0521 if (retval) {
0522 err_msg("Failed to set timerlat period\n");
0523 goto out_err;
0524 }
0525 }
0526
0527
0528 if (params->print_stack) {
0529 retval = osnoise_set_print_stack(top->context, params->print_stack);
0530 if (retval) {
0531 err_msg("Failed to set print stack\n");
0532 goto out_err;
0533 }
0534 }
0535
0536 return 0;
0537
0538 out_err:
0539 return -1;
0540 }
0541
0542
0543
0544
0545 static struct osnoise_tool
0546 *timerlat_init_top(struct timerlat_top_params *params)
0547 {
0548 struct osnoise_tool *top;
0549 int nr_cpus;
0550
0551 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
0552
0553 top = osnoise_init_tool("timerlat_top");
0554 if (!top)
0555 return NULL;
0556
0557 top->data = timerlat_alloc_top(nr_cpus);
0558 if (!top->data)
0559 goto out_err;
0560
0561 top->params = params;
0562
0563 tep_register_event_handler(top->trace.tep, -1, "ftrace", "timerlat",
0564 timerlat_top_handler, top);
0565
0566 return top;
0567
0568 out_err:
0569 osnoise_destroy_tool(top);
0570 return NULL;
0571 }
0572
0573 static int stop_tracing;
0574 static void stop_top(int sig)
0575 {
0576 stop_tracing = 1;
0577 }
0578
0579
0580
0581
0582 static void
0583 timerlat_top_set_signals(struct timerlat_top_params *params)
0584 {
0585 signal(SIGINT, stop_top);
0586 if (params->duration) {
0587 signal(SIGALRM, stop_top);
0588 alarm(params->duration);
0589 }
0590 }
0591
0592 int timerlat_top_main(int argc, char *argv[])
0593 {
0594 struct timerlat_top_params *params;
0595 struct osnoise_tool *record = NULL;
0596 struct osnoise_tool *top = NULL;
0597 struct trace_instance *trace;
0598 int dma_latency_fd = -1;
0599 int return_value = 1;
0600 int retval;
0601
0602 params = timerlat_top_parse_args(argc, argv);
0603 if (!params)
0604 exit(1);
0605
0606 top = timerlat_init_top(params);
0607 if (!top) {
0608 err_msg("Could not init osnoise top\n");
0609 goto out_exit;
0610 }
0611
0612 retval = timerlat_top_apply_config(top, params);
0613 if (retval) {
0614 err_msg("Could not apply config\n");
0615 goto out_free;
0616 }
0617
0618 trace = &top->trace;
0619
0620 retval = enable_timerlat(trace);
0621 if (retval) {
0622 err_msg("Failed to enable timerlat tracer\n");
0623 goto out_free;
0624 }
0625
0626 if (params->set_sched) {
0627 retval = set_comm_sched_attr("timerlat/", ¶ms->sched_param);
0628 if (retval) {
0629 err_msg("Failed to set sched parameters\n");
0630 goto out_free;
0631 }
0632 }
0633
0634 if (params->dma_latency >= 0) {
0635 dma_latency_fd = set_cpu_dma_latency(params->dma_latency);
0636 if (dma_latency_fd < 0) {
0637 err_msg("Could not set /dev/cpu_dma_latency.\n");
0638 goto out_free;
0639 }
0640 }
0641
0642 trace_instance_start(trace);
0643
0644 if (params->trace_output) {
0645 record = osnoise_init_trace_tool("timerlat");
0646 if (!record) {
0647 err_msg("Failed to enable the trace instance\n");
0648 goto out_free;
0649 }
0650
0651 if (params->events) {
0652 retval = trace_events_enable(&record->trace, params->events);
0653 if (retval)
0654 goto out_top;
0655 }
0656
0657 trace_instance_start(&record->trace);
0658 }
0659
0660 top->start_time = time(NULL);
0661 timerlat_top_set_signals(params);
0662
0663 while (!stop_tracing) {
0664 sleep(params->sleep_time);
0665
0666 retval = tracefs_iterate_raw_events(trace->tep,
0667 trace->inst,
0668 NULL,
0669 0,
0670 collect_registered_events,
0671 trace);
0672 if (retval < 0) {
0673 err_msg("Error iterating on events\n");
0674 goto out_top;
0675 }
0676
0677 if (!params->quiet)
0678 timerlat_print_stats(params, top);
0679
0680 if (trace_is_off(&top->trace, &record->trace))
0681 break;
0682
0683 }
0684
0685 timerlat_print_stats(params, top);
0686
0687 return_value = 0;
0688
0689 if (trace_is_off(&top->trace, &record->trace)) {
0690 printf("rtla timerlat hit stop tracing\n");
0691 if (params->trace_output) {
0692 printf(" Saving trace to %s\n", params->trace_output);
0693 save_trace_to_file(record->trace.inst, params->trace_output);
0694 }
0695 }
0696
0697 out_top:
0698 if (dma_latency_fd >= 0)
0699 close(dma_latency_fd);
0700 trace_events_destroy(&record->trace, params->events);
0701 params->events = NULL;
0702 out_free:
0703 timerlat_free_top(top->data);
0704 osnoise_destroy_tool(record);
0705 osnoise_destroy_tool(top);
0706 free(params);
0707 out_exit:
0708 exit(return_value);
0709 }