1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
18 struct timerlat_top_params {
22 unsigned long long runtime;
24 long long stop_total_us;
25 long long timerlat_period_us;
26 long long print_stack;
33 struct sched_attr sched_param;
34 struct trace_events *events;
37 struct timerlat_top_cpu {
41 unsigned long long cur_irq;
42 unsigned long long min_irq;
43 unsigned long long sum_irq;
44 unsigned long long max_irq;
46 unsigned long long cur_thread;
47 unsigned long long min_thread;
48 unsigned long long sum_thread;
49 unsigned long long max_thread;
52 struct timerlat_top_data {
53 struct timerlat_top_cpu *cpu_data;
58 * timerlat_free_top - free runtime data
61 timerlat_free_top(struct timerlat_top_data *data)
68 * timerlat_alloc_histogram - alloc runtime data
70 static struct timerlat_top_data *timerlat_alloc_top(int nr_cpus)
72 struct timerlat_top_data *data;
75 data = calloc(1, sizeof(*data));
79 data->nr_cpus = nr_cpus;
81 /* one set of histograms per CPU */
82 data->cpu_data = calloc(1, sizeof(*data->cpu_data) * nr_cpus);
86 /* set the min to max */
87 for (cpu = 0; cpu < nr_cpus; cpu++) {
88 data->cpu_data[cpu].min_irq = ~0;
89 data->cpu_data[cpu].min_thread = ~0;
95 timerlat_free_top(data);
100 * timerlat_hist_update - record a new timerlat occurent on cpu, updating data
103 timerlat_top_update(struct osnoise_tool *tool, int cpu,
104 unsigned long long thread,
105 unsigned long long latency)
107 struct timerlat_top_data *data = tool->data;
108 struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
111 cpu_data->irq_count++;
112 cpu_data->cur_irq = latency;
113 update_min(&cpu_data->min_irq, &latency);
114 update_sum(&cpu_data->sum_irq, &latency);
115 update_max(&cpu_data->max_irq, &latency);
117 cpu_data->thread_count++;
118 cpu_data->cur_thread = latency;
119 update_min(&cpu_data->min_thread, &latency);
120 update_sum(&cpu_data->sum_thread, &latency);
121 update_max(&cpu_data->max_thread, &latency);
126 * timerlat_top_handler - this is the handler for timerlat tracer events
129 timerlat_top_handler(struct trace_seq *s, struct tep_record *record,
130 struct tep_event *event, void *context)
132 struct trace_instance *trace = context;
133 unsigned long long latency, thread;
134 struct osnoise_tool *top;
135 int cpu = record->cpu;
137 top = container_of(trace, struct osnoise_tool, trace);
139 tep_get_field_val(s, event, "context", record, &thread, 1);
140 tep_get_field_val(s, event, "timer_latency", record, &latency, 1);
142 timerlat_top_update(top, cpu, thread, latency);
148 * timerlat_top_header - print the header of the tool output
150 static void timerlat_top_header(struct osnoise_tool *top)
152 struct timerlat_top_params *params = top->params;
153 struct trace_seq *s = top->trace.seq;
156 get_duration(top->start_time, duration, sizeof(duration));
158 trace_seq_printf(s, "\033[2;37;40m");
159 trace_seq_printf(s, " Timer Latency ");
160 trace_seq_printf(s, "\033[0;0;0m");
161 trace_seq_printf(s, "\n");
163 trace_seq_printf(s, "%-6s | IRQ Timer Latency (%s) | Thread Timer Latency (%s)\n", duration,
164 params->output_divisor == 1 ? "ns" : "us",
165 params->output_divisor == 1 ? "ns" : "us");
167 trace_seq_printf(s, "\033[2;30;47m");
168 trace_seq_printf(s, "CPU COUNT | cur min avg max | cur min avg max");
169 trace_seq_printf(s, "\033[0;0;0m");
170 trace_seq_printf(s, "\n");
174 * timerlat_top_print - prints the output of a given CPU
176 static void timerlat_top_print(struct osnoise_tool *top, int cpu)
179 struct timerlat_top_params *params = top->params;
180 struct timerlat_top_data *data = top->data;
181 struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
182 int divisor = params->output_divisor;
183 struct trace_seq *s = top->trace.seq;
189 * Skip if no data is available: is this cpu offline?
191 if (!cpu_data->irq_count && !cpu_data->thread_count)
195 * Unless trace is being lost, IRQ counter is always the max.
197 trace_seq_printf(s, "%3d #%-9d |", cpu, cpu_data->irq_count);
199 if (!cpu_data->irq_count) {
200 trace_seq_printf(s, " - ");
201 trace_seq_printf(s, " - ");
202 trace_seq_printf(s, " - ");
203 trace_seq_printf(s, " - |");
205 trace_seq_printf(s, "%9llu ", cpu_data->cur_irq / params->output_divisor);
206 trace_seq_printf(s, "%9llu ", cpu_data->min_irq / params->output_divisor);
207 trace_seq_printf(s, "%9llu ", (cpu_data->sum_irq / cpu_data->irq_count) / divisor);
208 trace_seq_printf(s, "%9llu |", cpu_data->max_irq / divisor);
211 if (!cpu_data->thread_count) {
212 trace_seq_printf(s, " - ");
213 trace_seq_printf(s, " - ");
214 trace_seq_printf(s, " - ");
215 trace_seq_printf(s, " -\n");
217 trace_seq_printf(s, "%9llu ", cpu_data->cur_thread / divisor);
218 trace_seq_printf(s, "%9llu ", cpu_data->min_thread / divisor);
219 trace_seq_printf(s, "%9llu ",
220 (cpu_data->sum_thread / cpu_data->thread_count) / divisor);
221 trace_seq_printf(s, "%9llu\n", cpu_data->max_thread / divisor);
226 * clear_terminal - clears the output terminal
228 static void clear_terminal(struct trace_seq *seq)
231 trace_seq_printf(seq, "\033c");
235 * timerlat_print_stats - print data for all cpus
238 timerlat_print_stats(struct timerlat_top_params *params, struct osnoise_tool *top)
240 struct trace_instance *trace = &top->trace;
241 static int nr_cpus = -1;
245 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
248 clear_terminal(trace->seq);
250 timerlat_top_header(top);
252 for (i = 0; i < nr_cpus; i++) {
253 if (params->cpus && !params->monitored_cpus[i])
255 timerlat_top_print(top, i);
258 trace_seq_do_printf(trace->seq);
259 trace_seq_reset(trace->seq);
263 * timerlat_top_usage - prints timerlat top usage message
265 static void timerlat_top_usage(char *usage)
269 static const char *const msg[] = {
271 " usage: rtla timerlat [top] [-h] [-q] [-a us] [-d s] [-D] [-n] [-p us] [-i us] [-T us] [-s us] \\",
272 " [[-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] \\",
273 " [-P priority] [--dma-latency us]",
275 " -h/--help: print this menu",
276 " -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",
277 " -p/--period us: timerlat period in us",
278 " -i/--irq us: stop trace if the irq latency is higher than the argument in us",
279 " -T/--thread us: stop trace if the thread latency is higher than the argument in us",
280 " -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",
281 " -c/--cpus cpus: run the tracer only on the given cpus",
282 " -d/--duration time[m|h|d]: duration of the session in seconds",
283 " -D/--debug: print debug info",
284 " -t/--trace[=file]: save the stopped trace to [file|timerlat_trace.txt]",
285 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
286 " --filter <command>: enable a trace event filter to the previous -e event",
287 " --trigger <command>: enable a trace event trigger to the previous -e event",
288 " -n/--nano: display data in nanoseconds",
289 " -q/--quiet print only a summary at the end",
290 " --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency",
291 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
292 " o:prio - use SCHED_OTHER with prio",
293 " r:prio - use SCHED_RR with prio",
294 " f:prio - use SCHED_FIFO with prio",
295 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
301 fprintf(stderr, "%s\n", usage);
303 fprintf(stderr, "rtla timerlat top: a per-cpu summary of the timer latency (version %s)\n",
306 for (i = 0; msg[i]; i++)
307 fprintf(stderr, "%s\n", msg[i]);
312 * timerlat_top_parse_args - allocs, parse and fill the cmd line parameters
314 static struct timerlat_top_params
315 *timerlat_top_parse_args(int argc, char **argv)
317 struct timerlat_top_params *params;
318 struct trace_events *tevent;
319 long long auto_thresh;
323 params = calloc(1, sizeof(*params));
327 /* disabled by default */
328 params->dma_latency = -1;
330 /* display data in microseconds */
331 params->output_divisor = 1000;
334 static struct option long_options[] = {
335 {"auto", required_argument, 0, 'a'},
336 {"cpus", required_argument, 0, 'c'},
337 {"debug", no_argument, 0, 'D'},
338 {"duration", required_argument, 0, 'd'},
339 {"event", required_argument, 0, 'e'},
340 {"help", no_argument, 0, 'h'},
341 {"irq", required_argument, 0, 'i'},
342 {"nano", no_argument, 0, 'n'},
343 {"period", required_argument, 0, 'p'},
344 {"priority", required_argument, 0, 'P'},
345 {"quiet", no_argument, 0, 'q'},
346 {"stack", required_argument, 0, 's'},
347 {"thread", required_argument, 0, 'T'},
348 {"trace", optional_argument, 0, 't'},
349 {"trigger", required_argument, 0, '0'},
350 {"filter", required_argument, 0, '1'},
351 {"dma-latency", required_argument, 0, '2'},
355 /* getopt_long stores the option index here. */
356 int option_index = 0;
358 c = getopt_long(argc, argv, "a:c:d:De:hi:np:P:qs:t::T:0:1:2:",
359 long_options, &option_index);
361 /* detect the end of the options. */
367 auto_thresh = get_llong_from_str(optarg);
369 /* set thread stop to auto_thresh */
370 params->stop_total_us = auto_thresh;
372 /* get stack trace */
373 params->print_stack = auto_thresh;
376 params->trace_output = "timerlat_trace.txt";
380 retval = parse_cpu_list(optarg, ¶ms->monitored_cpus);
382 timerlat_top_usage("\nInvalid -c cpu list\n");
383 params->cpus = optarg;
389 params->duration = parse_seconds_duration(optarg);
390 if (!params->duration)
391 timerlat_top_usage("Invalid -D duration\n");
394 tevent = trace_event_alloc(optarg);
396 err_msg("Error alloc trace event");
401 tevent->next = params->events;
402 params->events = tevent;
406 timerlat_top_usage(NULL);
409 params->stop_us = get_llong_from_str(optarg);
412 params->output_divisor = 1;
415 params->timerlat_period_us = get_llong_from_str(optarg);
416 if (params->timerlat_period_us > 1000000)
417 timerlat_top_usage("Period longer than 1 s\n");
420 retval = parse_prio(optarg, ¶ms->sched_param);
422 timerlat_top_usage("Invalid -P priority");
423 params->set_sched = 1;
429 params->print_stack = get_llong_from_str(optarg);
432 params->stop_total_us = get_llong_from_str(optarg);
437 params->trace_output = &optarg[1];
439 params->trace_output = "timerlat_trace.txt";
441 case '0': /* trigger */
442 if (params->events) {
443 retval = trace_event_add_trigger(params->events, optarg);
445 err_msg("Error adding trigger %s\n", optarg);
449 timerlat_top_usage("--trigger requires a previous -e\n");
452 case '1': /* filter */
453 if (params->events) {
454 retval = trace_event_add_filter(params->events, optarg);
456 err_msg("Error adding filter %s\n", optarg);
460 timerlat_top_usage("--filter requires a previous -e\n");
463 case '2': /* dma-latency */
464 params->dma_latency = get_llong_from_str(optarg);
465 if (params->dma_latency < 0 || params->dma_latency > 10000) {
466 err_msg("--dma-latency needs to be >= 0 and < 10000");
471 timerlat_top_usage("Invalid option");
476 err_msg("rtla needs root permission\n");
484 * timerlat_top_apply_config - apply the top configs to the initialized tool
487 timerlat_top_apply_config(struct osnoise_tool *top, struct timerlat_top_params *params)
491 if (!params->sleep_time)
492 params->sleep_time = 1;
495 retval = osnoise_set_cpus(top->context, params->cpus);
497 err_msg("Failed to apply CPUs config\n");
502 if (params->stop_us) {
503 retval = osnoise_set_stop_us(top->context, params->stop_us);
505 err_msg("Failed to set stop us\n");
510 if (params->stop_total_us) {
511 retval = osnoise_set_stop_total_us(top->context, params->stop_total_us);
513 err_msg("Failed to set stop total us\n");
519 if (params->timerlat_period_us) {
520 retval = osnoise_set_timerlat_period_us(top->context, params->timerlat_period_us);
522 err_msg("Failed to set timerlat period\n");
528 if (params->print_stack) {
529 retval = osnoise_set_print_stack(top->context, params->print_stack);
531 err_msg("Failed to set print stack\n");
543 * timerlat_init_top - initialize a timerlat top tool with parameters
545 static struct osnoise_tool
546 *timerlat_init_top(struct timerlat_top_params *params)
548 struct osnoise_tool *top;
551 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
553 top = osnoise_init_tool("timerlat_top");
557 top->data = timerlat_alloc_top(nr_cpus);
561 top->params = params;
563 tep_register_event_handler(top->trace.tep, -1, "ftrace", "timerlat",
564 timerlat_top_handler, top);
569 osnoise_destroy_tool(top);
573 static int stop_tracing;
574 static void stop_top(int sig)
580 * timerlat_top_set_signals - handles the signal to stop the tool
583 timerlat_top_set_signals(struct timerlat_top_params *params)
585 signal(SIGINT, stop_top);
586 if (params->duration) {
587 signal(SIGALRM, stop_top);
588 alarm(params->duration);
592 int timerlat_top_main(int argc, char *argv[])
594 struct timerlat_top_params *params;
595 struct osnoise_tool *record = NULL;
596 struct osnoise_tool *top = NULL;
597 struct trace_instance *trace;
598 int dma_latency_fd = -1;
599 int return_value = 1;
602 params = timerlat_top_parse_args(argc, argv);
606 top = timerlat_init_top(params);
608 err_msg("Could not init osnoise top\n");
612 retval = timerlat_top_apply_config(top, params);
614 err_msg("Could not apply config\n");
620 retval = enable_timerlat(trace);
622 err_msg("Failed to enable timerlat tracer\n");
626 if (params->set_sched) {
627 retval = set_comm_sched_attr("timerlat/", ¶ms->sched_param);
629 err_msg("Failed to set sched parameters\n");
634 if (params->dma_latency >= 0) {
635 dma_latency_fd = set_cpu_dma_latency(params->dma_latency);
636 if (dma_latency_fd < 0) {
637 err_msg("Could not set /dev/cpu_dma_latency.\n");
642 trace_instance_start(trace);
644 if (params->trace_output) {
645 record = osnoise_init_trace_tool("timerlat");
647 err_msg("Failed to enable the trace instance\n");
651 if (params->events) {
652 retval = trace_events_enable(&record->trace, params->events);
657 trace_instance_start(&record->trace);
660 top->start_time = time(NULL);
661 timerlat_top_set_signals(params);
663 while (!stop_tracing) {
664 sleep(params->sleep_time);
666 retval = tracefs_iterate_raw_events(trace->tep,
670 collect_registered_events,
673 err_msg("Error iterating on events\n");
678 timerlat_print_stats(params, top);
680 if (trace_is_off(&top->trace, &record->trace))
685 timerlat_print_stats(params, top);
689 if (trace_is_off(&top->trace, &record->trace)) {
690 printf("rtla timerlat hit stop tracing\n");
691 if (params->trace_output) {
692 printf(" Saving trace to %s\n", params->trace_output);
693 save_trace_to_file(record->trace.inst, params->trace_output);
698 if (dma_latency_fd >= 0)
699 close(dma_latency_fd);
700 trace_events_destroy(&record->trace, params->events);
701 params->events = NULL;
703 timerlat_free_top(top->data);
704 osnoise_destroy_tool(record);
705 osnoise_destroy_tool(top);