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;
32 struct sched_attr sched_param;
35 struct timerlat_top_cpu {
39 unsigned long long cur_irq;
40 unsigned long long min_irq;
41 unsigned long long sum_irq;
42 unsigned long long max_irq;
44 unsigned long long cur_thread;
45 unsigned long long min_thread;
46 unsigned long long sum_thread;
47 unsigned long long max_thread;
50 struct timerlat_top_data {
51 struct timerlat_top_cpu *cpu_data;
56 * timerlat_free_top - free runtime data
59 timerlat_free_top(struct timerlat_top_data *data)
66 * timerlat_alloc_histogram - alloc runtime data
68 static struct timerlat_top_data *timerlat_alloc_top(int nr_cpus)
70 struct timerlat_top_data *data;
73 data = calloc(1, sizeof(*data));
77 data->nr_cpus = nr_cpus;
79 /* one set of histograms per CPU */
80 data->cpu_data = calloc(1, sizeof(*data->cpu_data) * nr_cpus);
84 /* set the min to max */
85 for (cpu = 0; cpu < nr_cpus; cpu++) {
86 data->cpu_data[cpu].min_irq = ~0;
87 data->cpu_data[cpu].min_thread = ~0;
93 timerlat_free_top(data);
98 * timerlat_hist_update - record a new timerlat occurent on cpu, updating data
101 timerlat_top_update(struct osnoise_tool *tool, int cpu,
102 unsigned long long thread,
103 unsigned long long latency)
105 struct timerlat_top_data *data = tool->data;
106 struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
109 cpu_data->irq_count++;
110 cpu_data->cur_irq = latency;
111 update_min(&cpu_data->min_irq, &latency);
112 update_sum(&cpu_data->sum_irq, &latency);
113 update_max(&cpu_data->max_irq, &latency);
115 cpu_data->thread_count++;
116 cpu_data->cur_thread = latency;
117 update_min(&cpu_data->min_thread, &latency);
118 update_sum(&cpu_data->sum_thread, &latency);
119 update_max(&cpu_data->max_thread, &latency);
124 * timerlat_top_handler - this is the handler for timerlat tracer events
127 timerlat_top_handler(struct trace_seq *s, struct tep_record *record,
128 struct tep_event *event, void *context)
130 struct trace_instance *trace = context;
131 unsigned long long latency, thread;
132 struct osnoise_tool *top;
133 int cpu = record->cpu;
135 top = container_of(trace, struct osnoise_tool, trace);
137 tep_get_field_val(s, event, "context", record, &thread, 1);
138 tep_get_field_val(s, event, "timer_latency", record, &latency, 1);
140 timerlat_top_update(top, cpu, thread, latency);
146 * timerlat_top_header - print the header of the tool output
148 static void timerlat_top_header(struct osnoise_tool *top)
150 struct timerlat_top_params *params = top->params;
151 struct trace_seq *s = top->trace.seq;
154 get_duration(top->start_time, duration, sizeof(duration));
156 trace_seq_printf(s, "\033[2;37;40m");
157 trace_seq_printf(s, " Timer Latency ");
158 trace_seq_printf(s, "\033[0;0;0m");
159 trace_seq_printf(s, "\n");
161 trace_seq_printf(s, "%-6s | IRQ Timer Latency (%s) | Thread Timer Latency (%s)\n", duration,
162 params->output_divisor == 1 ? "ns" : "us",
163 params->output_divisor == 1 ? "ns" : "us");
165 trace_seq_printf(s, "\033[2;30;47m");
166 trace_seq_printf(s, "CPU COUNT | cur min avg max | cur min avg max");
167 trace_seq_printf(s, "\033[0;0;0m");
168 trace_seq_printf(s, "\n");
172 * timerlat_top_print - prints the output of a given CPU
174 static void timerlat_top_print(struct osnoise_tool *top, int cpu)
177 struct timerlat_top_params *params = top->params;
178 struct timerlat_top_data *data = top->data;
179 struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
180 int divisor = params->output_divisor;
181 struct trace_seq *s = top->trace.seq;
187 * Skip if no data is available: is this cpu offline?
189 if (!cpu_data->irq_count && !cpu_data->thread_count)
193 * Unless trace is being lost, IRQ counter is always the max.
195 trace_seq_printf(s, "%3d #%-9d |", cpu, cpu_data->irq_count);
197 if (!cpu_data->irq_count) {
198 trace_seq_printf(s, " - ");
199 trace_seq_printf(s, " - ");
200 trace_seq_printf(s, " - ");
201 trace_seq_printf(s, " - |");
203 trace_seq_printf(s, "%9llu ", cpu_data->cur_irq / params->output_divisor);
204 trace_seq_printf(s, "%9llu ", cpu_data->min_irq / params->output_divisor);
205 trace_seq_printf(s, "%9llu ", (cpu_data->sum_irq / cpu_data->irq_count) / divisor);
206 trace_seq_printf(s, "%9llu |", cpu_data->max_irq / divisor);
209 if (!cpu_data->thread_count) {
210 trace_seq_printf(s, " - ");
211 trace_seq_printf(s, " - ");
212 trace_seq_printf(s, " - ");
213 trace_seq_printf(s, " -\n");
215 trace_seq_printf(s, "%9llu ", cpu_data->cur_thread / divisor);
216 trace_seq_printf(s, "%9llu ", cpu_data->min_thread / divisor);
217 trace_seq_printf(s, "%9llu ",
218 (cpu_data->sum_thread / cpu_data->thread_count) / divisor);
219 trace_seq_printf(s, "%9llu\n", cpu_data->max_thread / divisor);
224 * clear_terminal - clears the output terminal
226 static void clear_terminal(struct trace_seq *seq)
229 trace_seq_printf(seq, "\033c");
233 * timerlat_print_stats - print data for all cpus
236 timerlat_print_stats(struct timerlat_top_params *params, struct osnoise_tool *top)
238 struct trace_instance *trace = &top->trace;
239 static int nr_cpus = -1;
243 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
246 clear_terminal(trace->seq);
248 timerlat_top_header(top);
250 for (i = 0; i < nr_cpus; i++) {
251 if (params->cpus && !params->monitored_cpus[i])
253 timerlat_top_print(top, i);
256 trace_seq_do_printf(trace->seq);
257 trace_seq_reset(trace->seq);
261 * timerlat_top_usage - prints timerlat top usage message
263 static void timerlat_top_usage(char *usage)
267 static const char *const msg[] = {
269 " usage: rtla timerlat [top] [-h] [-q] [-d s] [-D] [-n] [-p us] [-i us] [-T us] [-s us] [-t[=file]] \\",
270 " [-c cpu-list] [-P priority]",
272 " -h/--help: print this menu",
273 " -p/--period us: timerlat period in us",
274 " -i/--irq us: stop trace if the irq latency is higher than the argument in us",
275 " -T/--thread us: stop trace if the thread latency is higher than the argument in us",
276 " -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",
277 " -c/--cpus cpus: run the tracer only on the given cpus",
278 " -d/--duration time[m|h|d]: duration of the session in seconds",
279 " -D/--debug: print debug info",
280 " -t/--trace[=file]: save the stopped trace to [file|timerlat_trace.txt]",
281 " -n/--nano: display data in nanoseconds",
282 " -q/--quiet print only a summary at the end",
283 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
284 " o:prio - use SCHED_OTHER with prio",
285 " r:prio - use SCHED_RR with prio",
286 " f:prio - use SCHED_FIFO with prio",
287 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
293 fprintf(stderr, "%s\n", usage);
295 fprintf(stderr, "rtla timerlat top: a per-cpu summary of the timer latency (version %s)\n",
298 for (i = 0; msg[i]; i++)
299 fprintf(stderr, "%s\n", msg[i]);
304 * timerlat_top_parse_args - allocs, parse and fill the cmd line parameters
306 static struct timerlat_top_params
307 *timerlat_top_parse_args(int argc, char **argv)
309 struct timerlat_top_params *params;
313 params = calloc(1, sizeof(*params));
317 /* display data in microseconds */
318 params->output_divisor = 1000;
321 static struct option long_options[] = {
322 {"cpus", required_argument, 0, 'c'},
323 {"debug", no_argument, 0, 'D'},
324 {"duration", required_argument, 0, 'd'},
325 {"help", no_argument, 0, 'h'},
326 {"irq", required_argument, 0, 'i'},
327 {"nano", no_argument, 0, 'n'},
328 {"period", required_argument, 0, 'p'},
329 {"priority", required_argument, 0, 'P'},
330 {"quiet", no_argument, 0, 'q'},
331 {"stack", required_argument, 0, 's'},
332 {"thread", required_argument, 0, 'T'},
333 {"trace", optional_argument, 0, 't'},
337 /* getopt_long stores the option index here. */
338 int option_index = 0;
340 c = getopt_long(argc, argv, "c:d:Dhi:np:P:qs:t::T:",
341 long_options, &option_index);
343 /* detect the end of the options. */
349 retval = parse_cpu_list(optarg, ¶ms->monitored_cpus);
351 timerlat_top_usage("\nInvalid -c cpu list\n");
352 params->cpus = optarg;
358 params->duration = parse_seconds_duration(optarg);
359 if (!params->duration)
360 timerlat_top_usage("Invalid -D duration\n");
364 timerlat_top_usage(NULL);
367 params->stop_us = get_llong_from_str(optarg);
370 params->output_divisor = 1;
373 params->timerlat_period_us = get_llong_from_str(optarg);
374 if (params->timerlat_period_us > 1000000)
375 timerlat_top_usage("Period longer than 1 s\n");
378 retval = parse_prio(optarg, ¶ms->sched_param);
380 timerlat_top_usage("Invalid -P priority");
381 params->set_sched = 1;
387 params->print_stack = get_llong_from_str(optarg);
390 params->stop_total_us = get_llong_from_str(optarg);
395 params->trace_output = &optarg[1];
397 params->trace_output = "timerlat_trace.txt";
400 timerlat_top_usage("Invalid option");
405 err_msg("rtla needs root permission\n");
413 * timerlat_top_apply_config - apply the top configs to the initialized tool
416 timerlat_top_apply_config(struct osnoise_tool *top, struct timerlat_top_params *params)
420 if (!params->sleep_time)
421 params->sleep_time = 1;
424 retval = osnoise_set_cpus(top->context, params->cpus);
426 err_msg("Failed to apply CPUs config\n");
431 if (params->stop_us) {
432 retval = osnoise_set_stop_us(top->context, params->stop_us);
434 err_msg("Failed to set stop us\n");
439 if (params->stop_total_us) {
440 retval = osnoise_set_stop_total_us(top->context, params->stop_total_us);
442 err_msg("Failed to set stop total us\n");
448 if (params->timerlat_period_us) {
449 retval = osnoise_set_timerlat_period_us(top->context, params->timerlat_period_us);
451 err_msg("Failed to set timerlat period\n");
457 if (params->print_stack) {
458 retval = osnoise_set_print_stack(top->context, params->print_stack);
460 err_msg("Failed to set print stack\n");
472 * timerlat_init_top - initialize a timerlat top tool with parameters
474 static struct osnoise_tool
475 *timerlat_init_top(struct timerlat_top_params *params)
477 struct osnoise_tool *top;
480 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
482 top = osnoise_init_tool("timerlat_top");
486 top->data = timerlat_alloc_top(nr_cpus);
490 top->params = params;
492 tep_register_event_handler(top->trace.tep, -1, "ftrace", "timerlat",
493 timerlat_top_handler, top);
498 osnoise_destroy_tool(top);
502 static int stop_tracing;
503 static void stop_top(int sig)
509 * timerlat_top_set_signals - handles the signal to stop the tool
512 timerlat_top_set_signals(struct timerlat_top_params *params)
514 signal(SIGINT, stop_top);
515 if (params->duration) {
516 signal(SIGALRM, stop_top);
517 alarm(params->duration);
521 int timerlat_top_main(int argc, char *argv[])
523 struct timerlat_top_params *params;
524 struct osnoise_tool *record = NULL;
525 struct osnoise_tool *top = NULL;
526 struct trace_instance *trace;
527 int return_value = 1;
530 params = timerlat_top_parse_args(argc, argv);
534 top = timerlat_init_top(params);
536 err_msg("Could not init osnoise top\n");
540 retval = timerlat_top_apply_config(top, params);
542 err_msg("Could not apply config\n");
548 retval = enable_timerlat(trace);
550 err_msg("Failed to enable timerlat tracer\n");
554 if (params->set_sched) {
555 retval = set_comm_sched_attr("timerlat/", ¶ms->sched_param);
557 err_msg("Failed to set sched parameters\n");
562 trace_instance_start(trace);
564 if (params->trace_output) {
565 record = osnoise_init_trace_tool("timerlat");
567 err_msg("Failed to enable the trace instance\n");
570 trace_instance_start(&record->trace);
573 top->start_time = time(NULL);
574 timerlat_top_set_signals(params);
576 while (!stop_tracing) {
577 sleep(params->sleep_time);
579 retval = tracefs_iterate_raw_events(trace->tep,
583 collect_registered_events,
586 err_msg("Error iterating on events\n");
591 timerlat_print_stats(params, top);
593 if (!tracefs_trace_is_on(trace->inst))
598 timerlat_print_stats(params, top);
602 if (!tracefs_trace_is_on(trace->inst)) {
603 printf("rtla timelat hit stop tracing\n");
604 if (params->trace_output) {
605 printf(" Saving trace to %s\n", params->trace_output);
606 save_trace_to_file(record->trace.inst, params->trace_output);
611 timerlat_free_top(top->data);
612 osnoise_destroy_tool(record);
613 osnoise_destroy_tool(top);