xref: /linux/tools/tracing/rtla/src/timerlat_top.c (revision 84b9b44b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
4  */
5 
6 #include <getopt.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <signal.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <time.h>
13 #include <errno.h>
14 
15 #include "utils.h"
16 #include "osnoise.h"
17 #include "timerlat.h"
18 #include "timerlat_aa.h"
19 
20 struct timerlat_top_params {
21 	char			*cpus;
22 	char			*monitored_cpus;
23 	char			*trace_output;
24 	unsigned long long	runtime;
25 	long long		stop_us;
26 	long long		stop_total_us;
27 	long long		timerlat_period_us;
28 	long long		print_stack;
29 	int			sleep_time;
30 	int			output_divisor;
31 	int			duration;
32 	int			quiet;
33 	int			set_sched;
34 	int			dma_latency;
35 	int			no_aa;
36 	int			aa_only;
37 	int			dump_tasks;
38 	struct sched_attr	sched_param;
39 	struct trace_events	*events;
40 };
41 
42 struct timerlat_top_cpu {
43 	int			irq_count;
44 	int			thread_count;
45 
46 	unsigned long long	cur_irq;
47 	unsigned long long	min_irq;
48 	unsigned long long	sum_irq;
49 	unsigned long long	max_irq;
50 
51 	unsigned long long	cur_thread;
52 	unsigned long long	min_thread;
53 	unsigned long long	sum_thread;
54 	unsigned long long	max_thread;
55 };
56 
57 struct timerlat_top_data {
58 	struct timerlat_top_cpu	*cpu_data;
59 	int			nr_cpus;
60 };
61 
62 /*
63  * timerlat_free_top - free runtime data
64  */
65 static void
66 timerlat_free_top(struct timerlat_top_data *data)
67 {
68 	free(data->cpu_data);
69 	free(data);
70 }
71 
72 /*
73  * timerlat_alloc_histogram - alloc runtime data
74  */
75 static struct timerlat_top_data *timerlat_alloc_top(int nr_cpus)
76 {
77 	struct timerlat_top_data *data;
78 	int cpu;
79 
80 	data = calloc(1, sizeof(*data));
81 	if (!data)
82 		return NULL;
83 
84 	data->nr_cpus = nr_cpus;
85 
86 	/* one set of histograms per CPU */
87 	data->cpu_data = calloc(1, sizeof(*data->cpu_data) * nr_cpus);
88 	if (!data->cpu_data)
89 		goto cleanup;
90 
91 	/* set the min to max */
92 	for (cpu = 0; cpu < nr_cpus; cpu++) {
93 		data->cpu_data[cpu].min_irq = ~0;
94 		data->cpu_data[cpu].min_thread = ~0;
95 	}
96 
97 	return data;
98 
99 cleanup:
100 	timerlat_free_top(data);
101 	return NULL;
102 }
103 
104 /*
105  * timerlat_hist_update - record a new timerlat occurent on cpu, updating data
106  */
107 static void
108 timerlat_top_update(struct osnoise_tool *tool, int cpu,
109 		    unsigned long long thread,
110 		    unsigned long long latency)
111 {
112 	struct timerlat_top_data *data = tool->data;
113 	struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
114 
115 	if (!thread) {
116 		cpu_data->irq_count++;
117 		cpu_data->cur_irq = latency;
118 		update_min(&cpu_data->min_irq, &latency);
119 		update_sum(&cpu_data->sum_irq, &latency);
120 		update_max(&cpu_data->max_irq, &latency);
121 	} else {
122 		cpu_data->thread_count++;
123 		cpu_data->cur_thread = latency;
124 		update_min(&cpu_data->min_thread, &latency);
125 		update_sum(&cpu_data->sum_thread, &latency);
126 		update_max(&cpu_data->max_thread, &latency);
127 	}
128 }
129 
130 /*
131  * timerlat_top_handler - this is the handler for timerlat tracer events
132  */
133 static int
134 timerlat_top_handler(struct trace_seq *s, struct tep_record *record,
135 		     struct tep_event *event, void *context)
136 {
137 	struct trace_instance *trace = context;
138 	struct timerlat_top_params *params;
139 	unsigned long long latency, thread;
140 	struct osnoise_tool *top;
141 	int cpu = record->cpu;
142 
143 	top = container_of(trace, struct osnoise_tool, trace);
144 	params = top->params;
145 
146 	if (!params->aa_only) {
147 		tep_get_field_val(s, event, "context", record, &thread, 1);
148 		tep_get_field_val(s, event, "timer_latency", record, &latency, 1);
149 
150 		timerlat_top_update(top, cpu, thread, latency);
151 	}
152 
153 	if (!params->no_aa)
154 		timerlat_aa_handler(s, record, event, context);
155 
156 	return 0;
157 }
158 
159 /*
160  * timerlat_top_header - print the header of the tool output
161  */
162 static void timerlat_top_header(struct osnoise_tool *top)
163 {
164 	struct timerlat_top_params *params = top->params;
165 	struct trace_seq *s = top->trace.seq;
166 	char duration[26];
167 
168 	get_duration(top->start_time, duration, sizeof(duration));
169 
170 	trace_seq_printf(s, "\033[2;37;40m");
171 	trace_seq_printf(s, "                                     Timer Latency                                              ");
172 	trace_seq_printf(s, "\033[0;0;0m");
173 	trace_seq_printf(s, "\n");
174 
175 	trace_seq_printf(s, "%-6s   |          IRQ Timer Latency (%s)        |         Thread Timer Latency (%s)\n", duration,
176 			params->output_divisor == 1 ? "ns" : "us",
177 			params->output_divisor == 1 ? "ns" : "us");
178 
179 	trace_seq_printf(s, "\033[2;30;47m");
180 	trace_seq_printf(s, "CPU COUNT      |      cur       min       avg       max |      cur       min       avg       max");
181 	trace_seq_printf(s, "\033[0;0;0m");
182 	trace_seq_printf(s, "\n");
183 }
184 
185 /*
186  * timerlat_top_print - prints the output of a given CPU
187  */
188 static void timerlat_top_print(struct osnoise_tool *top, int cpu)
189 {
190 
191 	struct timerlat_top_params *params = top->params;
192 	struct timerlat_top_data *data = top->data;
193 	struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
194 	int divisor = params->output_divisor;
195 	struct trace_seq *s = top->trace.seq;
196 
197 	if (divisor == 0)
198 		return;
199 
200 	/*
201 	 * Skip if no data is available: is this cpu offline?
202 	 */
203 	if (!cpu_data->irq_count && !cpu_data->thread_count)
204 		return;
205 
206 	/*
207 	 * Unless trace is being lost, IRQ counter is always the max.
208 	 */
209 	trace_seq_printf(s, "%3d #%-9d |", cpu, cpu_data->irq_count);
210 
211 	if (!cpu_data->irq_count) {
212 		trace_seq_printf(s, "        - ");
213 		trace_seq_printf(s, "        - ");
214 		trace_seq_printf(s, "        - ");
215 		trace_seq_printf(s, "        - |");
216 	} else {
217 		trace_seq_printf(s, "%9llu ", cpu_data->cur_irq / params->output_divisor);
218 		trace_seq_printf(s, "%9llu ", cpu_data->min_irq / params->output_divisor);
219 		trace_seq_printf(s, "%9llu ", (cpu_data->sum_irq / cpu_data->irq_count) / divisor);
220 		trace_seq_printf(s, "%9llu |", cpu_data->max_irq / divisor);
221 	}
222 
223 	if (!cpu_data->thread_count) {
224 		trace_seq_printf(s, "        - ");
225 		trace_seq_printf(s, "        - ");
226 		trace_seq_printf(s, "        - ");
227 		trace_seq_printf(s, "        -\n");
228 	} else {
229 		trace_seq_printf(s, "%9llu ", cpu_data->cur_thread / divisor);
230 		trace_seq_printf(s, "%9llu ", cpu_data->min_thread / divisor);
231 		trace_seq_printf(s, "%9llu ",
232 				(cpu_data->sum_thread / cpu_data->thread_count) / divisor);
233 		trace_seq_printf(s, "%9llu\n", cpu_data->max_thread / divisor);
234 	}
235 }
236 
237 /*
238  * clear_terminal - clears the output terminal
239  */
240 static void clear_terminal(struct trace_seq *seq)
241 {
242 	if (!config_debug)
243 		trace_seq_printf(seq, "\033c");
244 }
245 
246 /*
247  * timerlat_print_stats - print data for all cpus
248  */
249 static void
250 timerlat_print_stats(struct timerlat_top_params *params, struct osnoise_tool *top)
251 {
252 	struct trace_instance *trace = &top->trace;
253 	static int nr_cpus = -1;
254 	int i;
255 
256 	if (params->aa_only)
257 		return;
258 
259 	if (nr_cpus == -1)
260 		nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
261 
262 	if (!params->quiet)
263 		clear_terminal(trace->seq);
264 
265 	timerlat_top_header(top);
266 
267 	for (i = 0; i < nr_cpus; i++) {
268 		if (params->cpus && !params->monitored_cpus[i])
269 			continue;
270 		timerlat_top_print(top, i);
271 	}
272 
273 	trace_seq_do_printf(trace->seq);
274 	trace_seq_reset(trace->seq);
275 }
276 
277 /*
278  * timerlat_top_usage - prints timerlat top usage message
279  */
280 static void timerlat_top_usage(char *usage)
281 {
282 	int i;
283 
284 	static const char *const msg[] = {
285 		"",
286 		"  usage: rtla timerlat [top] [-h] [-q] [-a us] [-d s] [-D] [-n] [-p us] [-i us] [-T us] [-s us] \\",
287 		"	  [[-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] \\",
288 		"	  [-P priority] [--dma-latency us] [--aa-only us]",
289 		"",
290 		"	  -h/--help: print this menu",
291 		"	  -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",
292 		"	     --aa-only us: stop if <us> latency is hit, only printing the auto analysis (reduces CPU usage)",
293 		"	  -p/--period us: timerlat period in us",
294 		"	  -i/--irq us: stop trace if the irq latency is higher than the argument in us",
295 		"	  -T/--thread us: stop trace if the thread latency is higher than the argument in us",
296 		"	  -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",
297 		"	  -c/--cpus cpus: run the tracer only on the given cpus",
298 		"	  -d/--duration time[m|h|d]: duration of the session in seconds",
299 		"	  -D/--debug: print debug info",
300 		"	     --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)",
301 		"	  -t/--trace[=file]: save the stopped trace to [file|timerlat_trace.txt]",
302 		"	  -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
303 		"	     --filter <command>: enable a trace event filter to the previous -e event",
304 		"	     --trigger <command>: enable a trace event trigger to the previous -e event",
305 		"	  -n/--nano: display data in nanoseconds",
306 		"	     --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage",
307 		"	  -q/--quiet print only a summary at the end",
308 		"	     --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency",
309 		"	  -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
310 		"		o:prio - use SCHED_OTHER with prio",
311 		"		r:prio - use SCHED_RR with prio",
312 		"		f:prio - use SCHED_FIFO with prio",
313 		"		d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
314 		"						       in nanoseconds",
315 		NULL,
316 	};
317 
318 	if (usage)
319 		fprintf(stderr, "%s\n", usage);
320 
321 	fprintf(stderr, "rtla timerlat top: a per-cpu summary of the timer latency (version %s)\n",
322 			VERSION);
323 
324 	for (i = 0; msg[i]; i++)
325 		fprintf(stderr, "%s\n", msg[i]);
326 	exit(1);
327 }
328 
329 /*
330  * timerlat_top_parse_args - allocs, parse and fill the cmd line parameters
331  */
332 static struct timerlat_top_params
333 *timerlat_top_parse_args(int argc, char **argv)
334 {
335 	struct timerlat_top_params *params;
336 	struct trace_events *tevent;
337 	long long auto_thresh;
338 	int retval;
339 	int c;
340 
341 	params = calloc(1, sizeof(*params));
342 	if (!params)
343 		exit(1);
344 
345 	/* disabled by default */
346 	params->dma_latency = -1;
347 
348 	/* display data in microseconds */
349 	params->output_divisor = 1000;
350 
351 	while (1) {
352 		static struct option long_options[] = {
353 			{"auto",		required_argument,	0, 'a'},
354 			{"cpus",		required_argument,	0, 'c'},
355 			{"debug",		no_argument,		0, 'D'},
356 			{"duration",		required_argument,	0, 'd'},
357 			{"event",		required_argument,	0, 'e'},
358 			{"help",		no_argument,		0, 'h'},
359 			{"irq",			required_argument,	0, 'i'},
360 			{"nano",		no_argument,		0, 'n'},
361 			{"period",		required_argument,	0, 'p'},
362 			{"priority",		required_argument,	0, 'P'},
363 			{"quiet",		no_argument,		0, 'q'},
364 			{"stack",		required_argument,	0, 's'},
365 			{"thread",		required_argument,	0, 'T'},
366 			{"trace",		optional_argument,	0, 't'},
367 			{"trigger",		required_argument,	0, '0'},
368 			{"filter",		required_argument,	0, '1'},
369 			{"dma-latency",		required_argument,	0, '2'},
370 			{"no-aa",		no_argument,		0, '3'},
371 			{"dump-tasks",		no_argument,		0, '4'},
372 			{"aa-only",		required_argument,	0, '5'},
373 			{0, 0, 0, 0}
374 		};
375 
376 		/* getopt_long stores the option index here. */
377 		int option_index = 0;
378 
379 		c = getopt_long(argc, argv, "a:c:d:De:hi:np:P:qs:t::T:0:1:2:345:",
380 				 long_options, &option_index);
381 
382 		/* detect the end of the options. */
383 		if (c == -1)
384 			break;
385 
386 		switch (c) {
387 		case 'a':
388 			auto_thresh = get_llong_from_str(optarg);
389 
390 			/* set thread stop to auto_thresh */
391 			params->stop_total_us = auto_thresh;
392 			params->stop_us = auto_thresh;
393 
394 			/* get stack trace */
395 			params->print_stack = auto_thresh;
396 
397 			/* set trace */
398 			params->trace_output = "timerlat_trace.txt";
399 			break;
400 		case '5':
401 			/* it is here because it is similar to -a */
402 			auto_thresh = get_llong_from_str(optarg);
403 
404 			/* set thread stop to auto_thresh */
405 			params->stop_total_us = auto_thresh;
406 			params->stop_us = auto_thresh;
407 
408 			/* get stack trace */
409 			params->print_stack = auto_thresh;
410 
411 			/* set aa_only to avoid parsing the trace */
412 			params->aa_only = 1;
413 			break;
414 		case 'c':
415 			retval = parse_cpu_list(optarg, &params->monitored_cpus);
416 			if (retval)
417 				timerlat_top_usage("\nInvalid -c cpu list\n");
418 			params->cpus = optarg;
419 			break;
420 		case 'D':
421 			config_debug = 1;
422 			break;
423 		case 'd':
424 			params->duration = parse_seconds_duration(optarg);
425 			if (!params->duration)
426 				timerlat_top_usage("Invalid -D duration\n");
427 			break;
428 		case 'e':
429 			tevent = trace_event_alloc(optarg);
430 			if (!tevent) {
431 				err_msg("Error alloc trace event");
432 				exit(EXIT_FAILURE);
433 			}
434 
435 			if (params->events)
436 				tevent->next = params->events;
437 			params->events = tevent;
438 			break;
439 		case 'h':
440 		case '?':
441 			timerlat_top_usage(NULL);
442 			break;
443 		case 'i':
444 			params->stop_us = get_llong_from_str(optarg);
445 			break;
446 		case 'n':
447 			params->output_divisor = 1;
448 			break;
449 		case 'p':
450 			params->timerlat_period_us = get_llong_from_str(optarg);
451 			if (params->timerlat_period_us > 1000000)
452 				timerlat_top_usage("Period longer than 1 s\n");
453 			break;
454 		case 'P':
455 			retval = parse_prio(optarg, &params->sched_param);
456 			if (retval == -1)
457 				timerlat_top_usage("Invalid -P priority");
458 			params->set_sched = 1;
459 			break;
460 		case 'q':
461 			params->quiet = 1;
462 			break;
463 		case 's':
464 			params->print_stack = get_llong_from_str(optarg);
465 			break;
466 		case 'T':
467 			params->stop_total_us = get_llong_from_str(optarg);
468 			break;
469 		case 't':
470 			if (optarg)
471 				/* skip = */
472 				params->trace_output = &optarg[1];
473 			else
474 				params->trace_output = "timerlat_trace.txt";
475 
476 			break;
477 		case '0': /* trigger */
478 			if (params->events) {
479 				retval = trace_event_add_trigger(params->events, optarg);
480 				if (retval) {
481 					err_msg("Error adding trigger %s\n", optarg);
482 					exit(EXIT_FAILURE);
483 				}
484 			} else {
485 				timerlat_top_usage("--trigger requires a previous -e\n");
486 			}
487 			break;
488 		case '1': /* filter */
489 			if (params->events) {
490 				retval = trace_event_add_filter(params->events, optarg);
491 				if (retval) {
492 					err_msg("Error adding filter %s\n", optarg);
493 					exit(EXIT_FAILURE);
494 				}
495 			} else {
496 				timerlat_top_usage("--filter requires a previous -e\n");
497 			}
498 			break;
499 		case '2': /* dma-latency */
500 			params->dma_latency = get_llong_from_str(optarg);
501 			if (params->dma_latency < 0 || params->dma_latency > 10000) {
502 				err_msg("--dma-latency needs to be >= 0 and < 10000");
503 				exit(EXIT_FAILURE);
504 			}
505 			break;
506 		case '3': /* no-aa */
507 			params->no_aa = 1;
508 			break;
509 		case '4':
510 			params->dump_tasks = 1;
511 			break;
512 		default:
513 			timerlat_top_usage("Invalid option");
514 		}
515 	}
516 
517 	if (geteuid()) {
518 		err_msg("rtla needs root permission\n");
519 		exit(EXIT_FAILURE);
520 	}
521 
522 	/*
523 	 * Auto analysis only happens if stop tracing, thus:
524 	 */
525 	if (!params->stop_us && !params->stop_total_us)
526 		params->no_aa = 1;
527 
528 	if (params->no_aa && params->aa_only)
529 		timerlat_top_usage("--no-aa and --aa-only are mutually exclusive!");
530 
531 	return params;
532 }
533 
534 /*
535  * timerlat_top_apply_config - apply the top configs to the initialized tool
536  */
537 static int
538 timerlat_top_apply_config(struct osnoise_tool *top, struct timerlat_top_params *params)
539 {
540 	int retval;
541 
542 	if (!params->sleep_time)
543 		params->sleep_time = 1;
544 
545 	if (params->cpus) {
546 		retval = osnoise_set_cpus(top->context, params->cpus);
547 		if (retval) {
548 			err_msg("Failed to apply CPUs config\n");
549 			goto out_err;
550 		}
551 	}
552 
553 	if (params->stop_us) {
554 		retval = osnoise_set_stop_us(top->context, params->stop_us);
555 		if (retval) {
556 			err_msg("Failed to set stop us\n");
557 			goto out_err;
558 		}
559 	}
560 
561 	if (params->stop_total_us) {
562 		retval = osnoise_set_stop_total_us(top->context, params->stop_total_us);
563 		if (retval) {
564 			err_msg("Failed to set stop total us\n");
565 			goto out_err;
566 		}
567 	}
568 
569 
570 	if (params->timerlat_period_us) {
571 		retval = osnoise_set_timerlat_period_us(top->context, params->timerlat_period_us);
572 		if (retval) {
573 			err_msg("Failed to set timerlat period\n");
574 			goto out_err;
575 		}
576 	}
577 
578 
579 	if (params->print_stack) {
580 		retval = osnoise_set_print_stack(top->context, params->print_stack);
581 		if (retval) {
582 			err_msg("Failed to set print stack\n");
583 			goto out_err;
584 		}
585 	}
586 
587 	return 0;
588 
589 out_err:
590 	return -1;
591 }
592 
593 /*
594  * timerlat_init_top - initialize a timerlat top tool with parameters
595  */
596 static struct osnoise_tool
597 *timerlat_init_top(struct timerlat_top_params *params)
598 {
599 	struct osnoise_tool *top;
600 	int nr_cpus;
601 	int retval;
602 
603 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
604 
605 	top = osnoise_init_tool("timerlat_top");
606 	if (!top)
607 		return NULL;
608 
609 	top->data = timerlat_alloc_top(nr_cpus);
610 	if (!top->data)
611 		goto out_err;
612 
613 	top->params = params;
614 
615 	tep_register_event_handler(top->trace.tep, -1, "ftrace", "timerlat",
616 				   timerlat_top_handler, top);
617 
618 	/*
619 	 * If no auto analysis, we are ready.
620 	 */
621 	if (params->no_aa)
622 		return top;
623 
624 	retval = timerlat_aa_init(top, nr_cpus, params->dump_tasks);
625 	if (retval)
626 		goto out_err;
627 
628 	return top;
629 
630 out_err:
631 	osnoise_destroy_tool(top);
632 	return NULL;
633 }
634 
635 static int stop_tracing;
636 static void stop_top(int sig)
637 {
638 	stop_tracing = 1;
639 }
640 
641 /*
642  * timerlat_top_set_signals - handles the signal to stop the tool
643  */
644 static void
645 timerlat_top_set_signals(struct timerlat_top_params *params)
646 {
647 	signal(SIGINT, stop_top);
648 	if (params->duration) {
649 		signal(SIGALRM, stop_top);
650 		alarm(params->duration);
651 	}
652 }
653 
654 int timerlat_top_main(int argc, char *argv[])
655 {
656 	struct timerlat_top_params *params;
657 	struct osnoise_tool *record = NULL;
658 	struct osnoise_tool *top = NULL;
659 	struct trace_instance *trace;
660 	int dma_latency_fd = -1;
661 	int return_value = 1;
662 	char *max_lat;
663 	int retval;
664 
665 	params = timerlat_top_parse_args(argc, argv);
666 	if (!params)
667 		exit(1);
668 
669 	top = timerlat_init_top(params);
670 	if (!top) {
671 		err_msg("Could not init osnoise top\n");
672 		goto out_exit;
673 	}
674 
675 	retval = timerlat_top_apply_config(top, params);
676 	if (retval) {
677 		err_msg("Could not apply config\n");
678 		goto out_free;
679 	}
680 
681 	trace = &top->trace;
682 
683 	retval = enable_timerlat(trace);
684 	if (retval) {
685 		err_msg("Failed to enable timerlat tracer\n");
686 		goto out_free;
687 	}
688 
689 	if (params->set_sched) {
690 		retval = set_comm_sched_attr("timerlat/", &params->sched_param);
691 		if (retval) {
692 			err_msg("Failed to set sched parameters\n");
693 			goto out_free;
694 		}
695 	}
696 
697 	if (params->dma_latency >= 0) {
698 		dma_latency_fd = set_cpu_dma_latency(params->dma_latency);
699 		if (dma_latency_fd < 0) {
700 			err_msg("Could not set /dev/cpu_dma_latency.\n");
701 			goto out_free;
702 		}
703 	}
704 
705 	trace_instance_start(trace);
706 
707 	if (params->trace_output) {
708 		record = osnoise_init_trace_tool("timerlat");
709 		if (!record) {
710 			err_msg("Failed to enable the trace instance\n");
711 			goto out_free;
712 		}
713 
714 		if (params->events) {
715 			retval = trace_events_enable(&record->trace, params->events);
716 			if (retval)
717 				goto out_top;
718 		}
719 
720 		trace_instance_start(&record->trace);
721 	}
722 
723 	top->start_time = time(NULL);
724 	timerlat_top_set_signals(params);
725 
726 	while (!stop_tracing) {
727 		sleep(params->sleep_time);
728 
729 		if (params->aa_only && !trace_is_off(&top->trace, &record->trace))
730 			continue;
731 
732 		retval = tracefs_iterate_raw_events(trace->tep,
733 						    trace->inst,
734 						    NULL,
735 						    0,
736 						    collect_registered_events,
737 						    trace);
738 		if (retval < 0) {
739 			err_msg("Error iterating on events\n");
740 			goto out_top;
741 		}
742 
743 		if (!params->quiet)
744 			timerlat_print_stats(params, top);
745 
746 		if (trace_is_off(&top->trace, &record->trace))
747 			break;
748 
749 	}
750 
751 	timerlat_print_stats(params, top);
752 
753 	return_value = 0;
754 
755 	if (trace_is_off(&top->trace, &record->trace)) {
756 		printf("rtla timerlat hit stop tracing\n");
757 
758 		if (!params->no_aa)
759 			timerlat_auto_analysis(params->stop_us, params->stop_total_us);
760 
761 		if (params->trace_output) {
762 			printf("  Saving trace to %s\n", params->trace_output);
763 			save_trace_to_file(record->trace.inst, params->trace_output);
764 		}
765 	} else if (params->aa_only) {
766 		/*
767 		 * If the trace did not stop with --aa-only, at least print the
768 		 * max known latency.
769 		 */
770 		max_lat = tracefs_instance_file_read(trace->inst, "tracing_max_latency", NULL);
771 		if (max_lat) {
772 			printf("  Max latency was %s\n", max_lat);
773 			free(max_lat);
774 		}
775 	}
776 
777 out_top:
778 	if (dma_latency_fd >= 0)
779 		close(dma_latency_fd);
780 	trace_events_destroy(&record->trace, params->events);
781 	params->events = NULL;
782 out_free:
783 	timerlat_free_top(top->data);
784 	timerlat_aa_destroy();
785 	osnoise_destroy_tool(record);
786 	osnoise_destroy_tool(top);
787 	free(params);
788 out_exit:
789 	exit(return_value);
790 }
791