1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
20 #include "timerlat_aa.h"
21 #include "timerlat_u.h"
23 struct timerlat_hist_params {
25 cpu_set_t monitored_cpus;
28 unsigned long long runtime;
30 long long stop_total_us;
31 long long timerlat_period_us;
32 long long print_stack;
44 struct sched_attr sched_param;
45 struct trace_events *events;
56 struct timerlat_hist_cpu {
65 unsigned long long min_irq;
66 unsigned long long sum_irq;
67 unsigned long long max_irq;
69 unsigned long long min_thread;
70 unsigned long long sum_thread;
71 unsigned long long max_thread;
73 unsigned long long min_user;
74 unsigned long long sum_user;
75 unsigned long long max_user;
78 struct timerlat_hist_data {
79 struct timerlat_hist_cpu *hist;
86 * timerlat_free_histogram - free runtime data
89 timerlat_free_histogram(struct timerlat_hist_data *data)
93 /* one histogram for IRQ and one for thread, per CPU */
94 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
95 if (data->hist[cpu].irq)
96 free(data->hist[cpu].irq);
98 if (data->hist[cpu].thread)
99 free(data->hist[cpu].thread);
101 if (data->hist[cpu].user)
102 free(data->hist[cpu].user);
106 /* one set of histograms per CPU */
114 * timerlat_alloc_histogram - alloc runtime data
116 static struct timerlat_hist_data
117 *timerlat_alloc_histogram(int nr_cpus, int entries, int bucket_size)
119 struct timerlat_hist_data *data;
122 data = calloc(1, sizeof(*data));
126 data->entries = entries;
127 data->bucket_size = bucket_size;
128 data->nr_cpus = nr_cpus;
130 /* one set of histograms per CPU */
131 data->hist = calloc(1, sizeof(*data->hist) * nr_cpus);
135 /* one histogram for IRQ and one for thread, per cpu */
136 for (cpu = 0; cpu < nr_cpus; cpu++) {
137 data->hist[cpu].irq = calloc(1, sizeof(*data->hist->irq) * (entries + 1));
138 if (!data->hist[cpu].irq)
141 data->hist[cpu].thread = calloc(1, sizeof(*data->hist->thread) * (entries + 1));
142 if (!data->hist[cpu].thread)
145 data->hist[cpu].user = calloc(1, sizeof(*data->hist->user) * (entries + 1));
146 if (!data->hist[cpu].user)
150 /* set the min to max */
151 for (cpu = 0; cpu < nr_cpus; cpu++) {
152 data->hist[cpu].min_irq = ~0;
153 data->hist[cpu].min_thread = ~0;
154 data->hist[cpu].min_user = ~0;
160 timerlat_free_histogram(data);
165 * timerlat_hist_update - record a new timerlat occurent on cpu, updating data
168 timerlat_hist_update(struct osnoise_tool *tool, int cpu,
169 unsigned long long context,
170 unsigned long long latency)
172 struct timerlat_hist_params *params = tool->params;
173 struct timerlat_hist_data *data = tool->data;
174 int entries = data->entries;
178 if (params->output_divisor)
179 latency = latency / params->output_divisor;
181 if (data->bucket_size)
182 bucket = latency / data->bucket_size;
185 hist = data->hist[cpu].irq;
186 data->hist[cpu].irq_count++;
187 update_min(&data->hist[cpu].min_irq, &latency);
188 update_sum(&data->hist[cpu].sum_irq, &latency);
189 update_max(&data->hist[cpu].max_irq, &latency);
190 } else if (context == 1) {
191 hist = data->hist[cpu].thread;
192 data->hist[cpu].thread_count++;
193 update_min(&data->hist[cpu].min_thread, &latency);
194 update_sum(&data->hist[cpu].sum_thread, &latency);
195 update_max(&data->hist[cpu].max_thread, &latency);
197 hist = data->hist[cpu].user;
198 data->hist[cpu].user_count++;
199 update_min(&data->hist[cpu].min_user, &latency);
200 update_sum(&data->hist[cpu].sum_user, &latency);
201 update_max(&data->hist[cpu].max_user, &latency);
204 if (bucket < entries)
211 * timerlat_hist_handler - this is the handler for timerlat tracer events
214 timerlat_hist_handler(struct trace_seq *s, struct tep_record *record,
215 struct tep_event *event, void *data)
217 struct trace_instance *trace = data;
218 unsigned long long context, latency;
219 struct osnoise_tool *tool;
220 int cpu = record->cpu;
222 tool = container_of(trace, struct osnoise_tool, trace);
224 tep_get_field_val(s, event, "context", record, &context, 1);
225 tep_get_field_val(s, event, "timer_latency", record, &latency, 1);
227 timerlat_hist_update(tool, cpu, context, latency);
233 * timerlat_hist_header - print the header of the tracer to the output
235 static void timerlat_hist_header(struct osnoise_tool *tool)
237 struct timerlat_hist_params *params = tool->params;
238 struct timerlat_hist_data *data = tool->data;
239 struct trace_seq *s = tool->trace.seq;
243 if (params->no_header)
246 get_duration(tool->start_time, duration, sizeof(duration));
247 trace_seq_printf(s, "# RTLA timerlat histogram\n");
248 trace_seq_printf(s, "# Time unit is %s (%s)\n",
249 params->output_divisor == 1 ? "nanoseconds" : "microseconds",
250 params->output_divisor == 1 ? "ns" : "us");
252 trace_seq_printf(s, "# Duration: %s\n", duration);
254 if (!params->no_index)
255 trace_seq_printf(s, "Index");
257 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
258 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
261 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
265 trace_seq_printf(s, " IRQ-%03d", cpu);
267 if (!params->no_thread)
268 trace_seq_printf(s, " Thr-%03d", cpu);
270 if (params->user_hist)
271 trace_seq_printf(s, " Usr-%03d", cpu);
273 trace_seq_printf(s, "\n");
276 trace_seq_do_printf(s);
281 * timerlat_print_summary - print the summary of the hist data to the output
284 timerlat_print_summary(struct timerlat_hist_params *params,
285 struct trace_instance *trace,
286 struct timerlat_hist_data *data)
290 if (params->no_summary)
293 if (!params->no_index)
294 trace_seq_printf(trace->seq, "count:");
296 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
297 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
300 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
304 trace_seq_printf(trace->seq, "%9d ",
305 data->hist[cpu].irq_count);
307 if (!params->no_thread)
308 trace_seq_printf(trace->seq, "%9d ",
309 data->hist[cpu].thread_count);
311 if (params->user_hist)
312 trace_seq_printf(trace->seq, "%9d ",
313 data->hist[cpu].user_count);
315 trace_seq_printf(trace->seq, "\n");
317 if (!params->no_index)
318 trace_seq_printf(trace->seq, "min: ");
320 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
321 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
324 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
328 trace_seq_printf(trace->seq, "%9llu ",
329 data->hist[cpu].min_irq);
331 if (!params->no_thread)
332 trace_seq_printf(trace->seq, "%9llu ",
333 data->hist[cpu].min_thread);
335 if (params->user_hist)
336 trace_seq_printf(trace->seq, "%9llu ",
337 data->hist[cpu].min_user);
339 trace_seq_printf(trace->seq, "\n");
341 if (!params->no_index)
342 trace_seq_printf(trace->seq, "avg: ");
344 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
345 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
348 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
351 if (!params->no_irq) {
352 if (data->hist[cpu].irq_count)
353 trace_seq_printf(trace->seq, "%9llu ",
354 data->hist[cpu].sum_irq / data->hist[cpu].irq_count);
356 trace_seq_printf(trace->seq, " - ");
359 if (!params->no_thread) {
360 if (data->hist[cpu].thread_count)
361 trace_seq_printf(trace->seq, "%9llu ",
362 data->hist[cpu].sum_thread / data->hist[cpu].thread_count);
364 trace_seq_printf(trace->seq, " - ");
367 if (params->user_hist) {
368 if (data->hist[cpu].user_count)
369 trace_seq_printf(trace->seq, "%9llu ",
370 data->hist[cpu].sum_user / data->hist[cpu].user_count);
372 trace_seq_printf(trace->seq, " - ");
375 trace_seq_printf(trace->seq, "\n");
377 if (!params->no_index)
378 trace_seq_printf(trace->seq, "max: ");
380 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
381 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
384 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
388 trace_seq_printf(trace->seq, "%9llu ",
389 data->hist[cpu].max_irq);
391 if (!params->no_thread)
392 trace_seq_printf(trace->seq, "%9llu ",
393 data->hist[cpu].max_thread);
395 if (params->user_hist)
396 trace_seq_printf(trace->seq, "%9llu ",
397 data->hist[cpu].max_user);
399 trace_seq_printf(trace->seq, "\n");
400 trace_seq_do_printf(trace->seq);
401 trace_seq_reset(trace->seq);
405 * timerlat_print_stats - print data for all CPUs
408 timerlat_print_stats(struct timerlat_hist_params *params, struct osnoise_tool *tool)
410 struct timerlat_hist_data *data = tool->data;
411 struct trace_instance *trace = &tool->trace;
415 timerlat_hist_header(tool);
417 for (bucket = 0; bucket < data->entries; bucket++) {
420 if (!params->no_index)
421 trace_seq_printf(trace->seq, "%-6d",
422 bucket * data->bucket_size);
424 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
425 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
428 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
431 if (!params->no_irq) {
432 total += data->hist[cpu].irq[bucket];
433 trace_seq_printf(trace->seq, "%9d ",
434 data->hist[cpu].irq[bucket]);
437 if (!params->no_thread) {
438 total += data->hist[cpu].thread[bucket];
439 trace_seq_printf(trace->seq, "%9d ",
440 data->hist[cpu].thread[bucket]);
443 if (params->user_hist) {
444 total += data->hist[cpu].user[bucket];
445 trace_seq_printf(trace->seq, "%9d ",
446 data->hist[cpu].user[bucket]);
451 if (total == 0 && !params->with_zeros) {
452 trace_seq_reset(trace->seq);
456 trace_seq_printf(trace->seq, "\n");
457 trace_seq_do_printf(trace->seq);
458 trace_seq_reset(trace->seq);
461 if (!params->no_index)
462 trace_seq_printf(trace->seq, "over: ");
464 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
465 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
468 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
472 trace_seq_printf(trace->seq, "%9d ",
473 data->hist[cpu].irq[data->entries]);
475 if (!params->no_thread)
476 trace_seq_printf(trace->seq, "%9d ",
477 data->hist[cpu].thread[data->entries]);
479 if (params->user_hist)
480 trace_seq_printf(trace->seq, "%9d ",
481 data->hist[cpu].user[data->entries]);
483 trace_seq_printf(trace->seq, "\n");
484 trace_seq_do_printf(trace->seq);
485 trace_seq_reset(trace->seq);
487 timerlat_print_summary(params, trace, data);
491 * timerlat_hist_usage - prints timerlat top usage message
493 static void timerlat_hist_usage(char *usage)
499 " usage: [rtla] timerlat hist [-h] [-q] [-d s] [-D] [-n] [-a us] [-p us] [-i us] [-T us] [-s us] \\",
500 " [-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\",
501 " [-P priority] [-E N] [-b N] [--no-irq] [--no-thread] [--no-header] [--no-summary] \\",
502 " [--no-index] [--with-zeros] [--dma-latency us] [-C[=cgroup_name]] [--no-aa] [--dump-task] [-u]",
504 " -h/--help: print this menu",
505 " -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",
506 " -p/--period us: timerlat period in us",
507 " -i/--irq us: stop trace if the irq latency is higher than the argument in us",
508 " -T/--thread us: stop trace if the thread latency is higher than the argument in us",
509 " -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",
510 " -c/--cpus cpus: run the tracer only on the given cpus",
511 " -H/--house-keeping cpus: run rtla control threads only on the given cpus",
512 " -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",
513 " -d/--duration time[m|h|d]: duration of the session in seconds",
514 " --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)",
515 " -D/--debug: print debug info",
516 " -t/--trace[=file]: save the stopped trace to [file|timerlat_trace.txt]",
517 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
518 " --filter <filter>: enable a trace event filter to the previous -e event",
519 " --trigger <trigger>: enable a trace event trigger to the previous -e event",
520 " -n/--nano: display data in nanoseconds",
521 " --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage",
522 " -b/--bucket-size N: set the histogram bucket size (default 1)",
523 " -E/--entries N: set the number of entries of the histogram (default 256)",
524 " --no-irq: ignore IRQ latencies",
525 " --no-thread: ignore thread latencies",
526 " --no-header: do not print header",
527 " --no-summary: do not print summary",
528 " --no-index: do not print index",
529 " --with-zeros: print zero only entries",
530 " --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency",
531 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
532 " o:prio - use SCHED_OTHER with prio",
533 " r:prio - use SCHED_RR with prio",
534 " f:prio - use SCHED_FIFO with prio",
535 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
537 " -u/--user-threads: use rtla user-space threads instead of in-kernel timerlat threads",
542 fprintf(stderr, "%s\n", usage);
544 fprintf(stderr, "rtla timerlat hist: a per-cpu histogram of the timer latency (version %s)\n",
547 for (i = 0; msg[i]; i++)
548 fprintf(stderr, "%s\n", msg[i]);
553 * timerlat_hist_parse_args - allocs, parse and fill the cmd line parameters
555 static struct timerlat_hist_params
556 *timerlat_hist_parse_args(int argc, char *argv[])
558 struct timerlat_hist_params *params;
559 struct trace_events *tevent;
564 params = calloc(1, sizeof(*params));
568 /* disabled by default */
569 params->dma_latency = -1;
571 /* display data in microseconds */
572 params->output_divisor = 1000;
573 params->bucket_size = 1;
574 params->entries = 256;
577 static struct option long_options[] = {
578 {"auto", required_argument, 0, 'a'},
579 {"cpus", required_argument, 0, 'c'},
580 {"cgroup", optional_argument, 0, 'C'},
581 {"bucket-size", required_argument, 0, 'b'},
582 {"debug", no_argument, 0, 'D'},
583 {"entries", required_argument, 0, 'E'},
584 {"duration", required_argument, 0, 'd'},
585 {"house-keeping", required_argument, 0, 'H'},
586 {"help", no_argument, 0, 'h'},
587 {"irq", required_argument, 0, 'i'},
588 {"nano", no_argument, 0, 'n'},
589 {"period", required_argument, 0, 'p'},
590 {"priority", required_argument, 0, 'P'},
591 {"stack", required_argument, 0, 's'},
592 {"thread", required_argument, 0, 'T'},
593 {"trace", optional_argument, 0, 't'},
594 {"user-threads", no_argument, 0, 'u'},
595 {"event", required_argument, 0, 'e'},
596 {"no-irq", no_argument, 0, '0'},
597 {"no-thread", no_argument, 0, '1'},
598 {"no-header", no_argument, 0, '2'},
599 {"no-summary", no_argument, 0, '3'},
600 {"no-index", no_argument, 0, '4'},
601 {"with-zeros", no_argument, 0, '5'},
602 {"trigger", required_argument, 0, '6'},
603 {"filter", required_argument, 0, '7'},
604 {"dma-latency", required_argument, 0, '8'},
605 {"no-aa", no_argument, 0, '9'},
606 {"dump-task", no_argument, 0, '\1'},
610 /* getopt_long stores the option index here. */
611 int option_index = 0;
613 c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:i:np:P:s:t::T:u0123456:7:8:9\1",
614 long_options, &option_index);
616 /* detect the end of the options. */
622 auto_thresh = get_llong_from_str(optarg);
624 /* set thread stop to auto_thresh */
625 params->stop_total_us = auto_thresh;
626 params->stop_us = auto_thresh;
628 /* get stack trace */
629 params->print_stack = auto_thresh;
632 params->trace_output = "timerlat_trace.txt";
636 retval = parse_cpu_set(optarg, ¶ms->monitored_cpus);
638 timerlat_hist_usage("\nInvalid -c cpu list\n");
639 params->cpus = optarg;
644 /* will inherit this cgroup */
645 params->cgroup_name = NULL;
646 } else if (*optarg == '=') {
648 params->cgroup_name = ++optarg;
652 params->bucket_size = get_llong_from_str(optarg);
653 if ((params->bucket_size == 0) || (params->bucket_size >= 1000000))
654 timerlat_hist_usage("Bucket size needs to be > 0 and <= 1000000\n");
660 params->duration = parse_seconds_duration(optarg);
661 if (!params->duration)
662 timerlat_hist_usage("Invalid -D duration\n");
665 tevent = trace_event_alloc(optarg);
667 err_msg("Error alloc trace event");
672 tevent->next = params->events;
674 params->events = tevent;
677 params->entries = get_llong_from_str(optarg);
678 if ((params->entries < 10) || (params->entries > 9999999))
679 timerlat_hist_usage("Entries must be > 10 and < 9999999\n");
683 timerlat_hist_usage(NULL);
687 retval = parse_cpu_set(optarg, ¶ms->hk_cpu_set);
689 err_msg("Error parsing house keeping CPUs\n");
694 params->stop_us = get_llong_from_str(optarg);
697 params->output_divisor = 1;
700 params->timerlat_period_us = get_llong_from_str(optarg);
701 if (params->timerlat_period_us > 1000000)
702 timerlat_hist_usage("Period longer than 1 s\n");
705 retval = parse_prio(optarg, ¶ms->sched_param);
707 timerlat_hist_usage("Invalid -P priority");
708 params->set_sched = 1;
711 params->print_stack = get_llong_from_str(optarg);
714 params->stop_total_us = get_llong_from_str(optarg);
719 params->trace_output = &optarg[1];
721 params->trace_output = "timerlat_trace.txt";
724 params->user_hist = 1;
726 case '0': /* no irq */
729 case '1': /* no thread */
730 params->no_thread = 1;
732 case '2': /* no header */
733 params->no_header = 1;
735 case '3': /* no summary */
736 params->no_summary = 1;
738 case '4': /* no index */
739 params->no_index = 1;
741 case '5': /* with zeros */
742 params->with_zeros = 1;
744 case '6': /* trigger */
745 if (params->events) {
746 retval = trace_event_add_trigger(params->events, optarg);
748 err_msg("Error adding trigger %s\n", optarg);
752 timerlat_hist_usage("--trigger requires a previous -e\n");
755 case '7': /* filter */
756 if (params->events) {
757 retval = trace_event_add_filter(params->events, optarg);
759 err_msg("Error adding filter %s\n", optarg);
763 timerlat_hist_usage("--filter requires a previous -e\n");
767 params->dma_latency = get_llong_from_str(optarg);
768 if (params->dma_latency < 0 || params->dma_latency > 10000) {
769 err_msg("--dma-latency needs to be >= 0 and < 10000");
777 params->dump_tasks = 1;
780 timerlat_hist_usage("Invalid option");
785 err_msg("rtla needs root permission\n");
789 if (params->no_irq && params->no_thread)
790 timerlat_hist_usage("no-irq and no-thread set, there is nothing to do here");
792 if (params->no_index && !params->with_zeros)
793 timerlat_hist_usage("no-index set with with-zeros is not set - it does not make sense");
796 * Auto analysis only happens if stop tracing, thus:
798 if (!params->stop_us && !params->stop_total_us)
805 * timerlat_hist_apply_config - apply the hist configs to the initialized tool
808 timerlat_hist_apply_config(struct osnoise_tool *tool, struct timerlat_hist_params *params)
812 if (!params->sleep_time)
813 params->sleep_time = 1;
816 retval = osnoise_set_cpus(tool->context, params->cpus);
818 err_msg("Failed to apply CPUs config\n");
822 for (i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++)
823 CPU_SET(i, ¶ms->monitored_cpus);
826 if (params->stop_us) {
827 retval = osnoise_set_stop_us(tool->context, params->stop_us);
829 err_msg("Failed to set stop us\n");
834 if (params->stop_total_us) {
835 retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us);
837 err_msg("Failed to set stop total us\n");
842 if (params->timerlat_period_us) {
843 retval = osnoise_set_timerlat_period_us(tool->context, params->timerlat_period_us);
845 err_msg("Failed to set timerlat period\n");
850 if (params->print_stack) {
851 retval = osnoise_set_print_stack(tool->context, params->print_stack);
853 err_msg("Failed to set print stack\n");
858 if (params->hk_cpus) {
859 retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set),
860 ¶ms->hk_cpu_set);
862 err_msg("Failed to set rtla to the house keeping CPUs\n");
865 } else if (params->cpus) {
867 * Even if the user do not set a house-keeping CPU, try to
868 * move rtla to a CPU set different to the one where the user
869 * set the workload to run.
871 * No need to check results as this is an automatic attempt.
873 auto_house_keeping(¶ms->monitored_cpus);
876 if (params->user_hist) {
877 retval = osnoise_set_workload(tool->context, 0);
879 err_msg("Failed to set OSNOISE_WORKLOAD option\n");
891 * timerlat_init_hist - initialize a timerlat hist tool with parameters
893 static struct osnoise_tool
894 *timerlat_init_hist(struct timerlat_hist_params *params)
896 struct osnoise_tool *tool;
899 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
901 tool = osnoise_init_tool("timerlat_hist");
905 tool->data = timerlat_alloc_histogram(nr_cpus, params->entries, params->bucket_size);
909 tool->params = params;
911 tep_register_event_handler(tool->trace.tep, -1, "ftrace", "timerlat",
912 timerlat_hist_handler, tool);
917 osnoise_destroy_tool(tool);
921 static int stop_tracing;
922 static void stop_hist(int sig)
928 * timerlat_hist_set_signals - handles the signal to stop the tool
931 timerlat_hist_set_signals(struct timerlat_hist_params *params)
933 signal(SIGINT, stop_hist);
934 if (params->duration) {
935 signal(SIGALRM, stop_hist);
936 alarm(params->duration);
940 int timerlat_hist_main(int argc, char *argv[])
942 struct timerlat_hist_params *params;
943 struct osnoise_tool *record = NULL;
944 struct timerlat_u_params params_u;
945 struct osnoise_tool *tool = NULL;
946 struct osnoise_tool *aa = NULL;
947 struct trace_instance *trace;
948 int dma_latency_fd = -1;
949 int return_value = 1;
950 pthread_t timerlat_u;
953 params = timerlat_hist_parse_args(argc, argv);
957 tool = timerlat_init_hist(params);
959 err_msg("Could not init osnoise hist\n");
963 retval = timerlat_hist_apply_config(tool, params);
965 err_msg("Could not apply config\n");
969 trace = &tool->trace;
971 retval = enable_timerlat(trace);
973 err_msg("Failed to enable timerlat tracer\n");
977 if (params->set_sched) {
978 retval = set_comm_sched_attr("timerlat/", ¶ms->sched_param);
980 err_msg("Failed to set sched parameters\n");
985 if (params->cgroup && !params->user_hist) {
986 retval = set_comm_cgroup("timerlat/", params->cgroup_name);
988 err_msg("Failed to move threads to cgroup\n");
993 if (params->dma_latency >= 0) {
994 dma_latency_fd = set_cpu_dma_latency(params->dma_latency);
995 if (dma_latency_fd < 0) {
996 err_msg("Could not set /dev/cpu_dma_latency.\n");
1001 if (params->trace_output) {
1002 record = osnoise_init_trace_tool("timerlat");
1004 err_msg("Failed to enable the trace instance\n");
1008 if (params->events) {
1009 retval = trace_events_enable(&record->trace, params->events);
1015 if (!params->no_aa) {
1016 aa = osnoise_init_tool("timerlat_aa");
1020 retval = timerlat_aa_init(aa, params->dump_tasks);
1022 err_msg("Failed to enable the auto analysis instance\n");
1026 retval = enable_timerlat(&aa->trace);
1028 err_msg("Failed to enable timerlat tracer\n");
1034 * Start the tracers here, after having set all instances.
1036 * Let the trace instance start first for the case of hitting a stop
1037 * tracing while enabling other instances. The trace instance is the
1038 * one with most valuable information.
1040 if (params->trace_output)
1041 trace_instance_start(&record->trace);
1043 trace_instance_start(&aa->trace);
1044 trace_instance_start(trace);
1046 tool->start_time = time(NULL);
1047 timerlat_hist_set_signals(params);
1049 if (params->user_hist) {
1050 /* rtla asked to stop */
1051 params_u.should_run = 1;
1052 /* all threads left */
1053 params_u.stopped_running = 0;
1055 params_u.set = ¶ms->monitored_cpus;
1056 if (params->set_sched)
1057 params_u.sched_param = ¶ms->sched_param;
1059 params_u.sched_param = NULL;
1061 params_u.cgroup_name = params->cgroup_name;
1063 retval = pthread_create(&timerlat_u, NULL, timerlat_u_dispatcher, ¶ms_u);
1065 err_msg("Error creating timerlat user-space threads\n");
1068 while (!stop_tracing) {
1069 sleep(params->sleep_time);
1071 retval = tracefs_iterate_raw_events(trace->tep,
1075 collect_registered_events,
1078 err_msg("Error iterating on events\n");
1082 if (trace_is_off(&tool->trace, &record->trace))
1085 /* is there still any user-threads ? */
1086 if (params->user_hist) {
1087 if (params_u.stopped_running) {
1088 debug_msg("timerlat user-space threads stopped!\n");
1093 if (params->user_hist && !params_u.stopped_running) {
1094 params_u.should_run = 0;
1098 timerlat_print_stats(params, tool);
1102 if (trace_is_off(&tool->trace, &record->trace)) {
1103 printf("rtla timerlat hit stop tracing\n");
1106 timerlat_auto_analysis(params->stop_us, params->stop_total_us);
1108 if (params->trace_output) {
1109 printf(" Saving trace to %s\n", params->trace_output);
1110 save_trace_to_file(record->trace.inst, params->trace_output);
1115 timerlat_aa_destroy();
1116 if (dma_latency_fd >= 0)
1117 close(dma_latency_fd);
1118 trace_events_destroy(&record->trace, params->events);
1119 params->events = NULL;
1121 timerlat_free_histogram(tool->data);
1122 osnoise_destroy_tool(aa);
1123 osnoise_destroy_tool(record);
1124 osnoise_destroy_tool(tool);