1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
21 #include "timerlat_aa.h"
22 #include "timerlat_u.h"
24 struct timerlat_top_params {
26 cpu_set_t monitored_cpus;
29 unsigned long long runtime;
31 long long stop_total_us;
32 long long timerlat_period_us;
33 long long print_stack;
47 struct sched_attr sched_param;
48 struct trace_events *events;
51 struct timerlat_top_cpu {
56 unsigned long long cur_irq;
57 unsigned long long min_irq;
58 unsigned long long sum_irq;
59 unsigned long long max_irq;
61 unsigned long long cur_thread;
62 unsigned long long min_thread;
63 unsigned long long sum_thread;
64 unsigned long long max_thread;
66 unsigned long long cur_user;
67 unsigned long long min_user;
68 unsigned long long sum_user;
69 unsigned long long max_user;
72 struct timerlat_top_data {
73 struct timerlat_top_cpu *cpu_data;
78 * timerlat_free_top - free runtime data
81 timerlat_free_top(struct timerlat_top_data *data)
88 * timerlat_alloc_histogram - alloc runtime data
90 static struct timerlat_top_data *timerlat_alloc_top(int nr_cpus)
92 struct timerlat_top_data *data;
95 data = calloc(1, sizeof(*data));
99 data->nr_cpus = nr_cpus;
101 /* one set of histograms per CPU */
102 data->cpu_data = calloc(1, sizeof(*data->cpu_data) * nr_cpus);
106 /* set the min to max */
107 for (cpu = 0; cpu < nr_cpus; cpu++) {
108 data->cpu_data[cpu].min_irq = ~0;
109 data->cpu_data[cpu].min_thread = ~0;
110 data->cpu_data[cpu].min_user = ~0;
116 timerlat_free_top(data);
121 * timerlat_hist_update - record a new timerlat occurent on cpu, updating data
124 timerlat_top_update(struct osnoise_tool *tool, int cpu,
125 unsigned long long thread,
126 unsigned long long latency)
128 struct timerlat_top_data *data = tool->data;
129 struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
132 cpu_data->irq_count++;
133 cpu_data->cur_irq = latency;
134 update_min(&cpu_data->min_irq, &latency);
135 update_sum(&cpu_data->sum_irq, &latency);
136 update_max(&cpu_data->max_irq, &latency);
137 } else if (thread == 1) {
138 cpu_data->thread_count++;
139 cpu_data->cur_thread = latency;
140 update_min(&cpu_data->min_thread, &latency);
141 update_sum(&cpu_data->sum_thread, &latency);
142 update_max(&cpu_data->max_thread, &latency);
144 cpu_data->user_count++;
145 cpu_data->cur_user = latency;
146 update_min(&cpu_data->min_user, &latency);
147 update_sum(&cpu_data->sum_user, &latency);
148 update_max(&cpu_data->max_user, &latency);
153 * timerlat_top_handler - this is the handler for timerlat tracer events
156 timerlat_top_handler(struct trace_seq *s, struct tep_record *record,
157 struct tep_event *event, void *context)
159 struct trace_instance *trace = context;
160 struct timerlat_top_params *params;
161 unsigned long long latency, thread;
162 struct osnoise_tool *top;
163 int cpu = record->cpu;
165 top = container_of(trace, struct osnoise_tool, trace);
166 params = top->params;
168 if (!params->aa_only) {
169 tep_get_field_val(s, event, "context", record, &thread, 1);
170 tep_get_field_val(s, event, "timer_latency", record, &latency, 1);
172 timerlat_top_update(top, cpu, thread, latency);
179 * timerlat_top_header - print the header of the tool output
181 static void timerlat_top_header(struct osnoise_tool *top)
183 struct timerlat_top_params *params = top->params;
184 struct trace_seq *s = top->trace.seq;
187 get_duration(top->start_time, duration, sizeof(duration));
189 trace_seq_printf(s, "\033[2;37;40m");
190 trace_seq_printf(s, " Timer Latency ");
191 if (params->user_top)
192 trace_seq_printf(s, " ");
193 trace_seq_printf(s, "\033[0;0;0m");
194 trace_seq_printf(s, "\n");
196 trace_seq_printf(s, "%-6s | IRQ Timer Latency (%s) | Thread Timer Latency (%s)", duration,
197 params->output_divisor == 1 ? "ns" : "us",
198 params->output_divisor == 1 ? "ns" : "us");
200 if (params->user_top) {
201 trace_seq_printf(s, " | Ret user Timer Latency (%s)",
202 params->output_divisor == 1 ? "ns" : "us");
205 trace_seq_printf(s, "\n");
206 trace_seq_printf(s, "\033[2;30;47m");
207 trace_seq_printf(s, "CPU COUNT | cur min avg max | cur min avg max");
208 if (params->user_top)
209 trace_seq_printf(s, " | cur min avg max");
210 trace_seq_printf(s, "\033[0;0;0m");
211 trace_seq_printf(s, "\n");
215 * timerlat_top_print - prints the output of a given CPU
217 static void timerlat_top_print(struct osnoise_tool *top, int cpu)
220 struct timerlat_top_params *params = top->params;
221 struct timerlat_top_data *data = top->data;
222 struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
223 int divisor = params->output_divisor;
224 struct trace_seq *s = top->trace.seq;
230 * Skip if no data is available: is this cpu offline?
232 if (!cpu_data->irq_count && !cpu_data->thread_count)
236 * Unless trace is being lost, IRQ counter is always the max.
238 trace_seq_printf(s, "%3d #%-9d |", cpu, cpu_data->irq_count);
240 if (!cpu_data->irq_count) {
241 trace_seq_printf(s, " - ");
242 trace_seq_printf(s, " - ");
243 trace_seq_printf(s, " - ");
244 trace_seq_printf(s, " - |");
246 trace_seq_printf(s, "%9llu ", cpu_data->cur_irq / params->output_divisor);
247 trace_seq_printf(s, "%9llu ", cpu_data->min_irq / params->output_divisor);
248 trace_seq_printf(s, "%9llu ", (cpu_data->sum_irq / cpu_data->irq_count) / divisor);
249 trace_seq_printf(s, "%9llu |", cpu_data->max_irq / divisor);
252 if (!cpu_data->thread_count) {
253 trace_seq_printf(s, " - ");
254 trace_seq_printf(s, " - ");
255 trace_seq_printf(s, " - ");
256 trace_seq_printf(s, " -\n");
258 trace_seq_printf(s, "%9llu ", cpu_data->cur_thread / divisor);
259 trace_seq_printf(s, "%9llu ", cpu_data->min_thread / divisor);
260 trace_seq_printf(s, "%9llu ",
261 (cpu_data->sum_thread / cpu_data->thread_count) / divisor);
262 trace_seq_printf(s, "%9llu", cpu_data->max_thread / divisor);
265 if (!params->user_top) {
266 trace_seq_printf(s, "\n");
270 trace_seq_printf(s, " |");
272 if (!cpu_data->user_count) {
273 trace_seq_printf(s, " - ");
274 trace_seq_printf(s, " - ");
275 trace_seq_printf(s, " - ");
276 trace_seq_printf(s, " -\n");
278 trace_seq_printf(s, "%9llu ", cpu_data->cur_user / divisor);
279 trace_seq_printf(s, "%9llu ", cpu_data->min_user / divisor);
280 trace_seq_printf(s, "%9llu ",
281 (cpu_data->sum_user / cpu_data->user_count) / divisor);
282 trace_seq_printf(s, "%9llu\n", cpu_data->max_user / divisor);
287 * clear_terminal - clears the output terminal
289 static void clear_terminal(struct trace_seq *seq)
292 trace_seq_printf(seq, "\033c");
296 * timerlat_print_stats - print data for all cpus
299 timerlat_print_stats(struct timerlat_top_params *params, struct osnoise_tool *top)
301 struct trace_instance *trace = &top->trace;
302 static int nr_cpus = -1;
309 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
312 clear_terminal(trace->seq);
314 timerlat_top_header(top);
316 for (i = 0; i < nr_cpus; i++) {
317 if (params->cpus && !CPU_ISSET(i, ¶ms->monitored_cpus))
319 timerlat_top_print(top, i);
322 trace_seq_do_printf(trace->seq);
323 trace_seq_reset(trace->seq);
327 * timerlat_top_usage - prints timerlat top usage message
329 static void timerlat_top_usage(char *usage)
333 static const char *const msg[] = {
335 " usage: rtla timerlat [top] [-h] [-q] [-a us] [-d s] [-D] [-n] [-p us] [-i us] [-T us] [-s us] \\",
336 " [[-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\",
337 " [-P priority] [--dma-latency us] [--aa-only us] [-C[=cgroup_name]] [-u]",
339 " -h/--help: print this menu",
340 " -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",
341 " --aa-only us: stop if <us> latency is hit, only printing the auto analysis (reduces CPU usage)",
342 " -p/--period us: timerlat period in us",
343 " -i/--irq us: stop trace if the irq latency is higher than the argument in us",
344 " -T/--thread us: stop trace if the thread latency is higher than the argument in us",
345 " -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",
346 " -c/--cpus cpus: run the tracer only on the given cpus",
347 " -H/--house-keeping cpus: run rtla control threads only on the given cpus",
348 " -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",
349 " -d/--duration time[m|h|d]: duration of the session in seconds",
350 " -D/--debug: print debug info",
351 " --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)",
352 " -t/--trace[=file]: save the stopped trace to [file|timerlat_trace.txt]",
353 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
354 " --filter <command>: enable a trace event filter to the previous -e event",
355 " --trigger <command>: enable a trace event trigger to the previous -e event",
356 " -n/--nano: display data in nanoseconds",
357 " --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage",
358 " -q/--quiet print only a summary at the end",
359 " --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency",
360 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
361 " o:prio - use SCHED_OTHER with prio",
362 " r:prio - use SCHED_RR with prio",
363 " f:prio - use SCHED_FIFO with prio",
364 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
366 " -u/--user-threads: use rtla user-space threads instead of in-kernel timerlat threads",
371 fprintf(stderr, "%s\n", usage);
373 fprintf(stderr, "rtla timerlat top: a per-cpu summary of the timer latency (version %s)\n",
376 for (i = 0; msg[i]; i++)
377 fprintf(stderr, "%s\n", msg[i]);
386 * timerlat_top_parse_args - allocs, parse and fill the cmd line parameters
388 static struct timerlat_top_params
389 *timerlat_top_parse_args(int argc, char **argv)
391 struct timerlat_top_params *params;
392 struct trace_events *tevent;
393 long long auto_thresh;
397 params = calloc(1, sizeof(*params));
401 /* disabled by default */
402 params->dma_latency = -1;
404 /* display data in microseconds */
405 params->output_divisor = 1000;
408 static struct option long_options[] = {
409 {"auto", required_argument, 0, 'a'},
410 {"cpus", required_argument, 0, 'c'},
411 {"cgroup", optional_argument, 0, 'C'},
412 {"debug", no_argument, 0, 'D'},
413 {"duration", required_argument, 0, 'd'},
414 {"event", required_argument, 0, 'e'},
415 {"help", no_argument, 0, 'h'},
416 {"house-keeping", required_argument, 0, 'H'},
417 {"irq", required_argument, 0, 'i'},
418 {"nano", no_argument, 0, 'n'},
419 {"period", required_argument, 0, 'p'},
420 {"priority", required_argument, 0, 'P'},
421 {"quiet", no_argument, 0, 'q'},
422 {"stack", required_argument, 0, 's'},
423 {"thread", required_argument, 0, 'T'},
424 {"trace", optional_argument, 0, 't'},
425 {"user-threads", no_argument, 0, 'u'},
426 {"trigger", required_argument, 0, '0'},
427 {"filter", required_argument, 0, '1'},
428 {"dma-latency", required_argument, 0, '2'},
429 {"no-aa", no_argument, 0, '3'},
430 {"dump-tasks", no_argument, 0, '4'},
431 {"aa-only", required_argument, 0, '5'},
435 /* getopt_long stores the option index here. */
436 int option_index = 0;
438 c = getopt_long(argc, argv, "a:c:C::d:De:hH:i:np:P:qs:t::T:u0:1:2:345:",
439 long_options, &option_index);
441 /* detect the end of the options. */
447 auto_thresh = get_llong_from_str(optarg);
449 /* set thread stop to auto_thresh */
450 params->stop_total_us = auto_thresh;
451 params->stop_us = auto_thresh;
453 /* get stack trace */
454 params->print_stack = auto_thresh;
457 params->trace_output = "timerlat_trace.txt";
460 /* it is here because it is similar to -a */
461 auto_thresh = get_llong_from_str(optarg);
463 /* set thread stop to auto_thresh */
464 params->stop_total_us = auto_thresh;
465 params->stop_us = auto_thresh;
467 /* get stack trace */
468 params->print_stack = auto_thresh;
470 /* set aa_only to avoid parsing the trace */
474 retval = parse_cpu_set(optarg, ¶ms->monitored_cpus);
476 timerlat_top_usage("\nInvalid -c cpu list\n");
477 params->cpus = optarg;
482 /* will inherit this cgroup */
483 params->cgroup_name = NULL;
484 } else if (*optarg == '=') {
486 params->cgroup_name = ++optarg;
493 params->duration = parse_seconds_duration(optarg);
494 if (!params->duration)
495 timerlat_top_usage("Invalid -D duration\n");
498 tevent = trace_event_alloc(optarg);
500 err_msg("Error alloc trace event");
505 tevent->next = params->events;
506 params->events = tevent;
510 timerlat_top_usage(NULL);
514 retval = parse_cpu_set(optarg, ¶ms->hk_cpu_set);
516 err_msg("Error parsing house keeping CPUs\n");
521 params->stop_us = get_llong_from_str(optarg);
524 params->output_divisor = 1;
527 params->timerlat_period_us = get_llong_from_str(optarg);
528 if (params->timerlat_period_us > 1000000)
529 timerlat_top_usage("Period longer than 1 s\n");
532 retval = parse_prio(optarg, ¶ms->sched_param);
534 timerlat_top_usage("Invalid -P priority");
535 params->set_sched = 1;
541 params->print_stack = get_llong_from_str(optarg);
544 params->stop_total_us = get_llong_from_str(optarg);
549 params->trace_output = &optarg[1];
551 params->trace_output = "timerlat_trace.txt";
555 params->user_top = true;
557 case '0': /* trigger */
558 if (params->events) {
559 retval = trace_event_add_trigger(params->events, optarg);
561 err_msg("Error adding trigger %s\n", optarg);
565 timerlat_top_usage("--trigger requires a previous -e\n");
568 case '1': /* filter */
569 if (params->events) {
570 retval = trace_event_add_filter(params->events, optarg);
572 err_msg("Error adding filter %s\n", optarg);
576 timerlat_top_usage("--filter requires a previous -e\n");
579 case '2': /* dma-latency */
580 params->dma_latency = get_llong_from_str(optarg);
581 if (params->dma_latency < 0 || params->dma_latency > 10000) {
582 err_msg("--dma-latency needs to be >= 0 and < 10000");
586 case '3': /* no-aa */
590 params->dump_tasks = 1;
593 timerlat_top_usage("Invalid option");
598 err_msg("rtla needs root permission\n");
603 * Auto analysis only happens if stop tracing, thus:
605 if (!params->stop_us && !params->stop_total_us)
608 if (params->no_aa && params->aa_only)
609 timerlat_top_usage("--no-aa and --aa-only are mutually exclusive!");
615 * timerlat_top_apply_config - apply the top configs to the initialized tool
618 timerlat_top_apply_config(struct osnoise_tool *top, struct timerlat_top_params *params)
623 if (!params->sleep_time)
624 params->sleep_time = 1;
627 retval = osnoise_set_cpus(top->context, params->cpus);
629 err_msg("Failed to apply CPUs config\n");
633 for (i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++)
634 CPU_SET(i, ¶ms->monitored_cpus);
637 if (params->stop_us) {
638 retval = osnoise_set_stop_us(top->context, params->stop_us);
640 err_msg("Failed to set stop us\n");
645 if (params->stop_total_us) {
646 retval = osnoise_set_stop_total_us(top->context, params->stop_total_us);
648 err_msg("Failed to set stop total us\n");
654 if (params->timerlat_period_us) {
655 retval = osnoise_set_timerlat_period_us(top->context, params->timerlat_period_us);
657 err_msg("Failed to set timerlat period\n");
663 if (params->print_stack) {
664 retval = osnoise_set_print_stack(top->context, params->print_stack);
666 err_msg("Failed to set print stack\n");
671 if (params->hk_cpus) {
672 retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set),
673 ¶ms->hk_cpu_set);
675 err_msg("Failed to set rtla to the house keeping CPUs\n");
678 } else if (params->cpus) {
680 * Even if the user do not set a house-keeping CPU, try to
681 * move rtla to a CPU set different to the one where the user
682 * set the workload to run.
684 * No need to check results as this is an automatic attempt.
686 auto_house_keeping(¶ms->monitored_cpus);
689 if (params->user_top) {
690 retval = osnoise_set_workload(top->context, 0);
692 err_msg("Failed to set OSNOISE_WORKLOAD option\n");
704 * timerlat_init_top - initialize a timerlat top tool with parameters
706 static struct osnoise_tool
707 *timerlat_init_top(struct timerlat_top_params *params)
709 struct osnoise_tool *top;
712 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
714 top = osnoise_init_tool("timerlat_top");
718 top->data = timerlat_alloc_top(nr_cpus);
722 top->params = params;
724 tep_register_event_handler(top->trace.tep, -1, "ftrace", "timerlat",
725 timerlat_top_handler, top);
730 osnoise_destroy_tool(top);
734 static int stop_tracing;
735 static void stop_top(int sig)
741 * timerlat_top_set_signals - handles the signal to stop the tool
744 timerlat_top_set_signals(struct timerlat_top_params *params)
746 signal(SIGINT, stop_top);
747 if (params->duration) {
748 signal(SIGALRM, stop_top);
749 alarm(params->duration);
753 int timerlat_top_main(int argc, char *argv[])
755 struct timerlat_top_params *params;
756 struct osnoise_tool *record = NULL;
757 struct timerlat_u_params params_u;
758 struct osnoise_tool *top = NULL;
759 struct osnoise_tool *aa = NULL;
760 struct trace_instance *trace;
761 int dma_latency_fd = -1;
762 pthread_t timerlat_u;
763 int return_value = 1;
767 params = timerlat_top_parse_args(argc, argv);
771 top = timerlat_init_top(params);
773 err_msg("Could not init osnoise top\n");
777 retval = timerlat_top_apply_config(top, params);
779 err_msg("Could not apply config\n");
785 retval = enable_timerlat(trace);
787 err_msg("Failed to enable timerlat tracer\n");
791 if (params->set_sched) {
792 retval = set_comm_sched_attr("timerlat/", ¶ms->sched_param);
794 err_msg("Failed to set sched parameters\n");
799 if (params->cgroup && !params->user_top) {
800 retval = set_comm_cgroup("timerlat/", params->cgroup_name);
802 err_msg("Failed to move threads to cgroup\n");
807 if (params->dma_latency >= 0) {
808 dma_latency_fd = set_cpu_dma_latency(params->dma_latency);
809 if (dma_latency_fd < 0) {
810 err_msg("Could not set /dev/cpu_dma_latency.\n");
815 if (params->trace_output) {
816 record = osnoise_init_trace_tool("timerlat");
818 err_msg("Failed to enable the trace instance\n");
822 if (params->events) {
823 retval = trace_events_enable(&record->trace, params->events);
829 if (!params->no_aa) {
830 if (params->aa_only) {
831 /* as top is not used for display, use it for aa */
834 /* otherwise, a new instance is needed */
835 aa = osnoise_init_tool("timerlat_aa");
840 retval = timerlat_aa_init(aa, params->dump_tasks);
842 err_msg("Failed to enable the auto analysis instance\n");
846 /* if it is re-using the main instance, there is no need to start it */
848 retval = enable_timerlat(&aa->trace);
850 err_msg("Failed to enable timerlat tracer\n");
857 * Start the tracers here, after having set all instances.
859 * Let the trace instance start first for the case of hitting a stop
860 * tracing while enabling other instances. The trace instance is the
861 * one with most valuable information.
863 if (params->trace_output)
864 trace_instance_start(&record->trace);
865 if (!params->no_aa && aa != top)
866 trace_instance_start(&aa->trace);
867 trace_instance_start(trace);
869 top->start_time = time(NULL);
870 timerlat_top_set_signals(params);
872 if (params->user_top) {
873 /* rtla asked to stop */
874 params_u.should_run = 1;
875 /* all threads left */
876 params_u.stopped_running = 0;
878 params_u.set = ¶ms->monitored_cpus;
879 if (params->set_sched)
880 params_u.sched_param = ¶ms->sched_param;
882 params_u.sched_param = NULL;
884 params_u.cgroup_name = params->cgroup_name;
886 retval = pthread_create(&timerlat_u, NULL, timerlat_u_dispatcher, ¶ms_u);
888 err_msg("Error creating timerlat user-space threads\n");
891 while (!stop_tracing) {
892 sleep(params->sleep_time);
894 if (params->aa_only && !trace_is_off(&top->trace, &record->trace))
897 retval = tracefs_iterate_raw_events(trace->tep,
901 collect_registered_events,
904 err_msg("Error iterating on events\n");
909 timerlat_print_stats(params, top);
911 if (trace_is_off(&top->trace, &record->trace))
914 /* is there still any user-threads ? */
915 if (params->user_top) {
916 if (params_u.stopped_running) {
917 debug_msg("timerlat user space threads stopped!\n");
923 if (params->user_top && !params_u.stopped_running) {
924 params_u.should_run = 0;
928 timerlat_print_stats(params, top);
932 if (trace_is_off(&top->trace, &record->trace)) {
933 printf("rtla timerlat hit stop tracing\n");
936 timerlat_auto_analysis(params->stop_us, params->stop_total_us);
938 if (params->trace_output) {
939 printf(" Saving trace to %s\n", params->trace_output);
940 save_trace_to_file(record->trace.inst, params->trace_output);
942 } else if (params->aa_only) {
944 * If the trace did not stop with --aa-only, at least print the
947 max_lat = tracefs_instance_file_read(trace->inst, "tracing_max_latency", NULL);
949 printf(" Max latency was %s\n", max_lat);
955 timerlat_aa_destroy();
956 if (dma_latency_fd >= 0)
957 close(dma_latency_fd);
958 trace_events_destroy(&record->trace, params->events);
959 params->events = NULL;
961 timerlat_free_top(top->data);
963 osnoise_destroy_tool(aa);
964 osnoise_destroy_tool(record);
965 osnoise_destroy_tool(top);