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 #define _GNU_SOURCE
7 #include <getopt.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <signal.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <time.h>
14 #include <sched.h>
15 #include <pthread.h>
16 
17 #include "utils.h"
18 #include "osnoise.h"
19 #include "timerlat.h"
20 #include "timerlat_aa.h"
21 #include "timerlat_u.h"
22 
23 struct timerlat_hist_params {
24 	char			*cpus;
25 	cpu_set_t		monitored_cpus;
26 	char			*trace_output;
27 	char			*cgroup_name;
28 	unsigned long long	runtime;
29 	long long		stop_us;
30 	long long		stop_total_us;
31 	long long		timerlat_period_us;
32 	long long		print_stack;
33 	int			sleep_time;
34 	int			output_divisor;
35 	int			duration;
36 	int			set_sched;
37 	int			dma_latency;
38 	int			cgroup;
39 	int			hk_cpus;
40 	int			no_aa;
41 	int			dump_tasks;
42 	int			user_workload;
43 	int			kernel_workload;
44 	int			user_hist;
45 	cpu_set_t		hk_cpu_set;
46 	struct sched_attr	sched_param;
47 	struct trace_events	*events;
48 	char			no_irq;
49 	char			no_thread;
50 	char			no_header;
51 	char			no_summary;
52 	char			no_index;
53 	char			with_zeros;
54 	int			bucket_size;
55 	int			entries;
56 	int			warmup;
57 	int			buffer_size;
58 };
59 
60 struct timerlat_hist_cpu {
61 	int			*irq;
62 	int			*thread;
63 	int			*user;
64 
65 	unsigned long long	irq_count;
66 	unsigned long long	thread_count;
67 	unsigned long long	user_count;
68 
69 	unsigned long long	min_irq;
70 	unsigned long long	sum_irq;
71 	unsigned long long	max_irq;
72 
73 	unsigned long long	min_thread;
74 	unsigned long long	sum_thread;
75 	unsigned long long	max_thread;
76 
77 	unsigned long long	min_user;
78 	unsigned long long	sum_user;
79 	unsigned long long	max_user;
80 };
81 
82 struct timerlat_hist_data {
83 	struct timerlat_hist_cpu	*hist;
84 	int				entries;
85 	int				bucket_size;
86 	int				nr_cpus;
87 };
88 
89 /*
90  * timerlat_free_histogram - free runtime data
91  */
92 static void
timerlat_free_histogram(struct timerlat_hist_data * data)93 timerlat_free_histogram(struct timerlat_hist_data *data)
94 {
95 	int cpu;
96 
97 	/* one histogram for IRQ and one for thread, per CPU */
98 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
99 		if (data->hist[cpu].irq)
100 			free(data->hist[cpu].irq);
101 
102 		if (data->hist[cpu].thread)
103 			free(data->hist[cpu].thread);
104 
105 		if (data->hist[cpu].user)
106 			free(data->hist[cpu].user);
107 
108 	}
109 
110 	/* one set of histograms per CPU */
111 	if (data->hist)
112 		free(data->hist);
113 
114 	free(data);
115 }
116 
117 /*
118  * timerlat_alloc_histogram - alloc runtime data
119  */
120 static struct timerlat_hist_data
timerlat_alloc_histogram(int nr_cpus,int entries,int bucket_size)121 *timerlat_alloc_histogram(int nr_cpus, int entries, int bucket_size)
122 {
123 	struct timerlat_hist_data *data;
124 	int cpu;
125 
126 	data = calloc(1, sizeof(*data));
127 	if (!data)
128 		return NULL;
129 
130 	data->entries = entries;
131 	data->bucket_size = bucket_size;
132 	data->nr_cpus = nr_cpus;
133 
134 	/* one set of histograms per CPU */
135 	data->hist = calloc(1, sizeof(*data->hist) * nr_cpus);
136 	if (!data->hist)
137 		goto cleanup;
138 
139 	/* one histogram for IRQ and one for thread, per cpu */
140 	for (cpu = 0; cpu < nr_cpus; cpu++) {
141 		data->hist[cpu].irq = calloc(1, sizeof(*data->hist->irq) * (entries + 1));
142 		if (!data->hist[cpu].irq)
143 			goto cleanup;
144 
145 		data->hist[cpu].thread = calloc(1, sizeof(*data->hist->thread) * (entries + 1));
146 		if (!data->hist[cpu].thread)
147 			goto cleanup;
148 
149 		data->hist[cpu].user = calloc(1, sizeof(*data->hist->user) * (entries + 1));
150 		if (!data->hist[cpu].user)
151 			goto cleanup;
152 	}
153 
154 	/* set the min to max */
155 	for (cpu = 0; cpu < nr_cpus; cpu++) {
156 		data->hist[cpu].min_irq = ~0;
157 		data->hist[cpu].min_thread = ~0;
158 		data->hist[cpu].min_user = ~0;
159 	}
160 
161 	return data;
162 
163 cleanup:
164 	timerlat_free_histogram(data);
165 	return NULL;
166 }
167 
168 /*
169  * timerlat_hist_update - record a new timerlat occurent on cpu, updating data
170  */
171 static void
timerlat_hist_update(struct osnoise_tool * tool,int cpu,unsigned long long context,unsigned long long latency)172 timerlat_hist_update(struct osnoise_tool *tool, int cpu,
173 		     unsigned long long context,
174 		     unsigned long long latency)
175 {
176 	struct timerlat_hist_params *params = tool->params;
177 	struct timerlat_hist_data *data = tool->data;
178 	int entries = data->entries;
179 	int bucket;
180 	int *hist;
181 
182 	if (params->output_divisor)
183 		latency = latency / params->output_divisor;
184 
185 	bucket = latency / data->bucket_size;
186 
187 	if (!context) {
188 		hist = data->hist[cpu].irq;
189 		data->hist[cpu].irq_count++;
190 		update_min(&data->hist[cpu].min_irq, &latency);
191 		update_sum(&data->hist[cpu].sum_irq, &latency);
192 		update_max(&data->hist[cpu].max_irq, &latency);
193 	} else if (context == 1) {
194 		hist = data->hist[cpu].thread;
195 		data->hist[cpu].thread_count++;
196 		update_min(&data->hist[cpu].min_thread, &latency);
197 		update_sum(&data->hist[cpu].sum_thread, &latency);
198 		update_max(&data->hist[cpu].max_thread, &latency);
199 	} else { /* user */
200 		hist = data->hist[cpu].user;
201 		data->hist[cpu].user_count++;
202 		update_min(&data->hist[cpu].min_user, &latency);
203 		update_sum(&data->hist[cpu].sum_user, &latency);
204 		update_max(&data->hist[cpu].max_user, &latency);
205 	}
206 
207 	if (bucket < entries)
208 		hist[bucket]++;
209 	else
210 		hist[entries]++;
211 }
212 
213 /*
214  * timerlat_hist_handler - this is the handler for timerlat tracer events
215  */
216 static int
timerlat_hist_handler(struct trace_seq * s,struct tep_record * record,struct tep_event * event,void * data)217 timerlat_hist_handler(struct trace_seq *s, struct tep_record *record,
218 		     struct tep_event *event, void *data)
219 {
220 	struct trace_instance *trace = data;
221 	unsigned long long context, latency;
222 	struct osnoise_tool *tool;
223 	int cpu = record->cpu;
224 
225 	tool = container_of(trace, struct osnoise_tool, trace);
226 
227 	tep_get_field_val(s, event, "context", record, &context, 1);
228 	tep_get_field_val(s, event, "timer_latency", record, &latency, 1);
229 
230 	timerlat_hist_update(tool, cpu, context, latency);
231 
232 	return 0;
233 }
234 
235 /*
236  * timerlat_hist_header - print the header of the tracer to the output
237  */
timerlat_hist_header(struct osnoise_tool * tool)238 static void timerlat_hist_header(struct osnoise_tool *tool)
239 {
240 	struct timerlat_hist_params *params = tool->params;
241 	struct timerlat_hist_data *data = tool->data;
242 	struct trace_seq *s = tool->trace.seq;
243 	char duration[26];
244 	int cpu;
245 
246 	if (params->no_header)
247 		return;
248 
249 	get_duration(tool->start_time, duration, sizeof(duration));
250 	trace_seq_printf(s, "# RTLA timerlat histogram\n");
251 	trace_seq_printf(s, "# Time unit is %s (%s)\n",
252 			params->output_divisor == 1 ? "nanoseconds" : "microseconds",
253 			params->output_divisor == 1 ? "ns" : "us");
254 
255 	trace_seq_printf(s, "# Duration: %s\n", duration);
256 
257 	if (!params->no_index)
258 		trace_seq_printf(s, "Index");
259 
260 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
261 		if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
262 			continue;
263 
264 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
265 			continue;
266 
267 		if (!params->no_irq)
268 			trace_seq_printf(s, "   IRQ-%03d", cpu);
269 
270 		if (!params->no_thread)
271 			trace_seq_printf(s, "   Thr-%03d", cpu);
272 
273 		if (params->user_hist)
274 			trace_seq_printf(s, "   Usr-%03d", cpu);
275 	}
276 	trace_seq_printf(s, "\n");
277 
278 
279 	trace_seq_do_printf(s);
280 	trace_seq_reset(s);
281 }
282 
283 /*
284  * format_summary_value - format a line of summary value (min, max or avg)
285  * of hist data
286  */
format_summary_value(struct trace_seq * seq,int count,unsigned long long val,bool avg)287 static void format_summary_value(struct trace_seq *seq,
288 				 int count,
289 				 unsigned long long val,
290 				 bool avg)
291 {
292 	if (count)
293 		trace_seq_printf(seq, "%9llu ", avg ? val / count : val);
294 	else
295 		trace_seq_printf(seq, "%9c ", '-');
296 }
297 
298 /*
299  * timerlat_print_summary - print the summary of the hist data to the output
300  */
301 static void
timerlat_print_summary(struct timerlat_hist_params * params,struct trace_instance * trace,struct timerlat_hist_data * data)302 timerlat_print_summary(struct timerlat_hist_params *params,
303 		       struct trace_instance *trace,
304 		       struct timerlat_hist_data *data)
305 {
306 	int cpu;
307 
308 	if (params->no_summary)
309 		return;
310 
311 	if (!params->no_index)
312 		trace_seq_printf(trace->seq, "count:");
313 
314 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
315 		if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
316 			continue;
317 
318 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
319 			continue;
320 
321 		if (!params->no_irq)
322 			trace_seq_printf(trace->seq, "%9llu ",
323 					data->hist[cpu].irq_count);
324 
325 		if (!params->no_thread)
326 			trace_seq_printf(trace->seq, "%9llu ",
327 					data->hist[cpu].thread_count);
328 
329 		if (params->user_hist)
330 			trace_seq_printf(trace->seq, "%9llu ",
331 					 data->hist[cpu].user_count);
332 	}
333 	trace_seq_printf(trace->seq, "\n");
334 
335 	if (!params->no_index)
336 		trace_seq_printf(trace->seq, "min:  ");
337 
338 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
339 		if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
340 			continue;
341 
342 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
343 			continue;
344 
345 		if (!params->no_irq)
346 			format_summary_value(trace->seq,
347 					     data->hist[cpu].irq_count,
348 					     data->hist[cpu].min_irq,
349 					     false);
350 
351 		if (!params->no_thread)
352 			format_summary_value(trace->seq,
353 					     data->hist[cpu].thread_count,
354 					     data->hist[cpu].min_thread,
355 					     false);
356 
357 		if (params->user_hist)
358 			format_summary_value(trace->seq,
359 					     data->hist[cpu].user_count,
360 					     data->hist[cpu].min_user,
361 					     false);
362 	}
363 	trace_seq_printf(trace->seq, "\n");
364 
365 	if (!params->no_index)
366 		trace_seq_printf(trace->seq, "avg:  ");
367 
368 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
369 		if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
370 			continue;
371 
372 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
373 			continue;
374 
375 		if (!params->no_irq)
376 			format_summary_value(trace->seq,
377 					     data->hist[cpu].irq_count,
378 					     data->hist[cpu].sum_irq,
379 					     true);
380 
381 		if (!params->no_thread)
382 			format_summary_value(trace->seq,
383 					     data->hist[cpu].thread_count,
384 					     data->hist[cpu].sum_thread,
385 					     true);
386 
387 		if (params->user_hist)
388 			format_summary_value(trace->seq,
389 					     data->hist[cpu].user_count,
390 					     data->hist[cpu].sum_user,
391 					     true);
392 	}
393 	trace_seq_printf(trace->seq, "\n");
394 
395 	if (!params->no_index)
396 		trace_seq_printf(trace->seq, "max:  ");
397 
398 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
399 		if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
400 			continue;
401 
402 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
403 			continue;
404 
405 		if (!params->no_irq)
406 			format_summary_value(trace->seq,
407 					     data->hist[cpu].irq_count,
408 					     data->hist[cpu].max_irq,
409 					     false);
410 
411 		if (!params->no_thread)
412 			format_summary_value(trace->seq,
413 					     data->hist[cpu].thread_count,
414 					     data->hist[cpu].max_thread,
415 					     false);
416 
417 		if (params->user_hist)
418 			format_summary_value(trace->seq,
419 					     data->hist[cpu].user_count,
420 					     data->hist[cpu].max_user,
421 					     false);
422 	}
423 	trace_seq_printf(trace->seq, "\n");
424 	trace_seq_do_printf(trace->seq);
425 	trace_seq_reset(trace->seq);
426 }
427 
428 static void
timerlat_print_stats_all(struct timerlat_hist_params * params,struct trace_instance * trace,struct timerlat_hist_data * data)429 timerlat_print_stats_all(struct timerlat_hist_params *params,
430 			 struct trace_instance *trace,
431 			 struct timerlat_hist_data *data)
432 {
433 	struct timerlat_hist_cpu *cpu_data;
434 	struct timerlat_hist_cpu sum;
435 	int cpu;
436 
437 	if (params->no_summary)
438 		return;
439 
440 	memset(&sum, 0, sizeof(sum));
441 	sum.min_irq = ~0;
442 	sum.min_thread = ~0;
443 	sum.min_user = ~0;
444 
445 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
446 		if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
447 			continue;
448 
449 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
450 			continue;
451 
452 		cpu_data = &data->hist[cpu];
453 
454 		sum.irq_count += cpu_data->irq_count;
455 		update_min(&sum.min_irq, &cpu_data->min_irq);
456 		update_sum(&sum.sum_irq, &cpu_data->sum_irq);
457 		update_max(&sum.max_irq, &cpu_data->max_irq);
458 
459 		sum.thread_count += cpu_data->thread_count;
460 		update_min(&sum.min_thread, &cpu_data->min_thread);
461 		update_sum(&sum.sum_thread, &cpu_data->sum_thread);
462 		update_max(&sum.max_thread, &cpu_data->max_thread);
463 
464 		sum.user_count += cpu_data->user_count;
465 		update_min(&sum.min_user, &cpu_data->min_user);
466 		update_sum(&sum.sum_user, &cpu_data->sum_user);
467 		update_max(&sum.max_user, &cpu_data->max_user);
468 	}
469 
470 	if (!params->no_index)
471 		trace_seq_printf(trace->seq, "ALL:  ");
472 
473 	if (!params->no_irq)
474 		trace_seq_printf(trace->seq, "      IRQ");
475 
476 	if (!params->no_thread)
477 		trace_seq_printf(trace->seq, "       Thr");
478 
479 	if (params->user_hist)
480 		trace_seq_printf(trace->seq, "       Usr");
481 
482 	trace_seq_printf(trace->seq, "\n");
483 
484 	if (!params->no_index)
485 		trace_seq_printf(trace->seq, "count:");
486 
487 	if (!params->no_irq)
488 		trace_seq_printf(trace->seq, "%9llu ",
489 				 sum.irq_count);
490 
491 	if (!params->no_thread)
492 		trace_seq_printf(trace->seq, "%9llu ",
493 				 sum.thread_count);
494 
495 	if (params->user_hist)
496 		trace_seq_printf(trace->seq, "%9llu ",
497 				 sum.user_count);
498 
499 	trace_seq_printf(trace->seq, "\n");
500 
501 	if (!params->no_index)
502 		trace_seq_printf(trace->seq, "min:  ");
503 
504 	if (!params->no_irq)
505 		format_summary_value(trace->seq,
506 				     sum.irq_count,
507 				     sum.min_irq,
508 				     false);
509 
510 	if (!params->no_thread)
511 		format_summary_value(trace->seq,
512 				     sum.thread_count,
513 				     sum.min_thread,
514 				     false);
515 
516 	if (params->user_hist)
517 		format_summary_value(trace->seq,
518 				     sum.user_count,
519 				     sum.min_user,
520 				     false);
521 
522 	trace_seq_printf(trace->seq, "\n");
523 
524 	if (!params->no_index)
525 		trace_seq_printf(trace->seq, "avg:  ");
526 
527 	if (!params->no_irq)
528 		format_summary_value(trace->seq,
529 				     sum.irq_count,
530 				     sum.sum_irq,
531 				     true);
532 
533 	if (!params->no_thread)
534 		format_summary_value(trace->seq,
535 				     sum.thread_count,
536 				     sum.sum_thread,
537 				     true);
538 
539 	if (params->user_hist)
540 		format_summary_value(trace->seq,
541 				     sum.user_count,
542 				     sum.sum_user,
543 				     true);
544 
545 	trace_seq_printf(trace->seq, "\n");
546 
547 	if (!params->no_index)
548 		trace_seq_printf(trace->seq, "max:  ");
549 
550 	if (!params->no_irq)
551 		format_summary_value(trace->seq,
552 				     sum.irq_count,
553 				     sum.max_irq,
554 				     false);
555 
556 	if (!params->no_thread)
557 		format_summary_value(trace->seq,
558 				     sum.thread_count,
559 				     sum.max_thread,
560 				     false);
561 
562 	if (params->user_hist)
563 		format_summary_value(trace->seq,
564 				     sum.user_count,
565 				     sum.max_user,
566 				     false);
567 
568 	trace_seq_printf(trace->seq, "\n");
569 	trace_seq_do_printf(trace->seq);
570 	trace_seq_reset(trace->seq);
571 }
572 
573 /*
574  * timerlat_print_stats - print data for each CPUs
575  */
576 static void
timerlat_print_stats(struct timerlat_hist_params * params,struct osnoise_tool * tool)577 timerlat_print_stats(struct timerlat_hist_params *params, struct osnoise_tool *tool)
578 {
579 	struct timerlat_hist_data *data = tool->data;
580 	struct trace_instance *trace = &tool->trace;
581 	int bucket, cpu;
582 	int total;
583 
584 	timerlat_hist_header(tool);
585 
586 	for (bucket = 0; bucket < data->entries; bucket++) {
587 		total = 0;
588 
589 		if (!params->no_index)
590 			trace_seq_printf(trace->seq, "%-6d",
591 					 bucket * data->bucket_size);
592 
593 		for (cpu = 0; cpu < data->nr_cpus; cpu++) {
594 			if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
595 				continue;
596 
597 			if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
598 				continue;
599 
600 			if (!params->no_irq) {
601 				total += data->hist[cpu].irq[bucket];
602 				trace_seq_printf(trace->seq, "%9d ",
603 						data->hist[cpu].irq[bucket]);
604 			}
605 
606 			if (!params->no_thread) {
607 				total += data->hist[cpu].thread[bucket];
608 				trace_seq_printf(trace->seq, "%9d ",
609 						data->hist[cpu].thread[bucket]);
610 			}
611 
612 			if (params->user_hist) {
613 				total += data->hist[cpu].user[bucket];
614 				trace_seq_printf(trace->seq, "%9d ",
615 						data->hist[cpu].user[bucket]);
616 			}
617 
618 		}
619 
620 		if (total == 0 && !params->with_zeros) {
621 			trace_seq_reset(trace->seq);
622 			continue;
623 		}
624 
625 		trace_seq_printf(trace->seq, "\n");
626 		trace_seq_do_printf(trace->seq);
627 		trace_seq_reset(trace->seq);
628 	}
629 
630 	if (!params->no_index)
631 		trace_seq_printf(trace->seq, "over: ");
632 
633 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
634 		if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
635 			continue;
636 
637 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
638 			continue;
639 
640 		if (!params->no_irq)
641 			trace_seq_printf(trace->seq, "%9d ",
642 					 data->hist[cpu].irq[data->entries]);
643 
644 		if (!params->no_thread)
645 			trace_seq_printf(trace->seq, "%9d ",
646 					 data->hist[cpu].thread[data->entries]);
647 
648 		if (params->user_hist)
649 			trace_seq_printf(trace->seq, "%9d ",
650 					 data->hist[cpu].user[data->entries]);
651 	}
652 	trace_seq_printf(trace->seq, "\n");
653 	trace_seq_do_printf(trace->seq);
654 	trace_seq_reset(trace->seq);
655 
656 	timerlat_print_summary(params, trace, data);
657 	timerlat_print_stats_all(params, trace, data);
658 }
659 
660 /*
661  * timerlat_hist_usage - prints timerlat top usage message
662  */
timerlat_hist_usage(char * usage)663 static void timerlat_hist_usage(char *usage)
664 {
665 	int i;
666 
667 	char *msg[] = {
668 		"",
669 		"  usage: [rtla] timerlat hist [-h] [-q] [-d s] [-D] [-n] [-a us] [-p us] [-i us] [-T us] [-s us] \\",
670 		"         [-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\",
671 		"	  [-P priority] [-E N] [-b N] [--no-irq] [--no-thread] [--no-header] [--no-summary] \\",
672 		"	  [--no-index] [--with-zeros] [--dma-latency us] [-C[=cgroup_name]] [--no-aa] [--dump-task] [-u|-k]",
673 		"	  [--warm-up s]",
674 		"",
675 		"	  -h/--help: print this menu",
676 		"	  -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",
677 		"	  -p/--period us: timerlat period in us",
678 		"	  -i/--irq us: stop trace if the irq latency is higher than the argument in us",
679 		"	  -T/--thread us: stop trace if the thread latency is higher than the argument in us",
680 		"	  -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",
681 		"	  -c/--cpus cpus: run the tracer only on the given cpus",
682 		"	  -H/--house-keeping cpus: run rtla control threads only on the given cpus",
683 		"	  -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",
684 		"	  -d/--duration time[m|h|d]: duration of the session in seconds",
685 		"	     --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)",
686 		"	  -D/--debug: print debug info",
687 		"	  -t/--trace[file]: save the stopped trace to [file|timerlat_trace.txt]",
688 		"	  -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
689 		"	     --filter <filter>: enable a trace event filter to the previous -e event",
690 		"	     --trigger <trigger>: enable a trace event trigger to the previous -e event",
691 		"	  -n/--nano: display data in nanoseconds",
692 		"	     --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage",
693 		"	  -b/--bucket-size N: set the histogram bucket size (default 1)",
694 		"	  -E/--entries N: set the number of entries of the histogram (default 256)",
695 		"	     --no-irq: ignore IRQ latencies",
696 		"	     --no-thread: ignore thread latencies",
697 		"	     --no-header: do not print header",
698 		"	     --no-summary: do not print summary",
699 		"	     --no-index: do not print index",
700 		"	     --with-zeros: print zero only entries",
701 		"	     --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency",
702 		"	  -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
703 		"		o:prio - use SCHED_OTHER with prio",
704 		"		r:prio - use SCHED_RR with prio",
705 		"		f:prio - use SCHED_FIFO with prio",
706 		"		d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
707 		"						       in nanoseconds",
708 		"	  -u/--user-threads: use rtla user-space threads instead of kernel-space timerlat threads",
709 		"	  -k/--kernel-threads: use timerlat kernel-space threads instead of rtla user-space threads",
710 		"	  -U/--user-load: enable timerlat for user-defined user-space workload",
711 		"	     --warm-up s: let the workload run for s seconds before collecting data",
712 		"	     --trace-buffer-size kB: set the per-cpu trace buffer size in kB",
713 		NULL,
714 	};
715 
716 	if (usage)
717 		fprintf(stderr, "%s\n", usage);
718 
719 	fprintf(stderr, "rtla timerlat hist: a per-cpu histogram of the timer latency (version %s)\n",
720 			VERSION);
721 
722 	for (i = 0; msg[i]; i++)
723 		fprintf(stderr, "%s\n", msg[i]);
724 
725 	if (usage)
726 		exit(EXIT_FAILURE);
727 
728 	exit(EXIT_SUCCESS);
729 }
730 
731 /*
732  * timerlat_hist_parse_args - allocs, parse and fill the cmd line parameters
733  */
734 static struct timerlat_hist_params
timerlat_hist_parse_args(int argc,char * argv[])735 *timerlat_hist_parse_args(int argc, char *argv[])
736 {
737 	struct timerlat_hist_params *params;
738 	struct trace_events *tevent;
739 	int auto_thresh;
740 	int retval;
741 	int c;
742 
743 	params = calloc(1, sizeof(*params));
744 	if (!params)
745 		exit(1);
746 
747 	/* disabled by default */
748 	params->dma_latency = -1;
749 
750 	/* display data in microseconds */
751 	params->output_divisor = 1000;
752 	params->bucket_size = 1;
753 	params->entries = 256;
754 
755 	while (1) {
756 		static struct option long_options[] = {
757 			{"auto",		required_argument,	0, 'a'},
758 			{"cpus",		required_argument,	0, 'c'},
759 			{"cgroup",		optional_argument,	0, 'C'},
760 			{"bucket-size",		required_argument,	0, 'b'},
761 			{"debug",		no_argument,		0, 'D'},
762 			{"entries",		required_argument,	0, 'E'},
763 			{"duration",		required_argument,	0, 'd'},
764 			{"house-keeping",	required_argument,	0, 'H'},
765 			{"help",		no_argument,		0, 'h'},
766 			{"irq",			required_argument,	0, 'i'},
767 			{"nano",		no_argument,		0, 'n'},
768 			{"period",		required_argument,	0, 'p'},
769 			{"priority",		required_argument,	0, 'P'},
770 			{"stack",		required_argument,	0, 's'},
771 			{"thread",		required_argument,	0, 'T'},
772 			{"trace",		optional_argument,	0, 't'},
773 			{"user-threads",	no_argument,		0, 'u'},
774 			{"kernel-threads",	no_argument,		0, 'k'},
775 			{"user-load",		no_argument,		0, 'U'},
776 			{"event",		required_argument,	0, 'e'},
777 			{"no-irq",		no_argument,		0, '0'},
778 			{"no-thread",		no_argument,		0, '1'},
779 			{"no-header",		no_argument,		0, '2'},
780 			{"no-summary",		no_argument,		0, '3'},
781 			{"no-index",		no_argument,		0, '4'},
782 			{"with-zeros",		no_argument,		0, '5'},
783 			{"trigger",		required_argument,	0, '6'},
784 			{"filter",		required_argument,	0, '7'},
785 			{"dma-latency",		required_argument,	0, '8'},
786 			{"no-aa",		no_argument,		0, '9'},
787 			{"dump-task",		no_argument,		0, '\1'},
788 			{"warm-up",		required_argument,	0, '\2'},
789 			{"trace-buffer-size",	required_argument,	0, '\3'},
790 			{0, 0, 0, 0}
791 		};
792 
793 		/* getopt_long stores the option index here. */
794 		int option_index = 0;
795 
796 		c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:i:knp:P:s:t::T:uU0123456:7:8:9\1\2:\3:",
797 				 long_options, &option_index);
798 
799 		/* detect the end of the options. */
800 		if (c == -1)
801 			break;
802 
803 		switch (c) {
804 		case 'a':
805 			auto_thresh = get_llong_from_str(optarg);
806 
807 			/* set thread stop to auto_thresh */
808 			params->stop_total_us = auto_thresh;
809 			params->stop_us = auto_thresh;
810 
811 			/* get stack trace */
812 			params->print_stack = auto_thresh;
813 
814 			/* set trace */
815 			params->trace_output = "timerlat_trace.txt";
816 
817 			break;
818 		case 'c':
819 			retval = parse_cpu_set(optarg, ¶ms->monitored_cpus);
820 			if (retval)
821 				timerlat_hist_usage("\nInvalid -c cpu list\n");
822 			params->cpus = optarg;
823 			break;
824 		case 'C':
825 			params->cgroup = 1;
826 			if (!optarg) {
827 				/* will inherit this cgroup */
828 				params->cgroup_name = NULL;
829 			} else if (*optarg == '=') {
830 				/* skip the = */
831 				params->cgroup_name = ++optarg;
832 			}
833 			break;
834 		case 'b':
835 			params->bucket_size = get_llong_from_str(optarg);
836 			if ((params->bucket_size == 0) || (params->bucket_size >= 1000000))
837 				timerlat_hist_usage("Bucket size needs to be > 0 and <= 1000000\n");
838 			break;
839 		case 'D':
840 			config_debug = 1;
841 			break;
842 		case 'd':
843 			params->duration = parse_seconds_duration(optarg);
844 			if (!params->duration)
845 				timerlat_hist_usage("Invalid -D duration\n");
846 			break;
847 		case 'e':
848 			tevent = trace_event_alloc(optarg);
849 			if (!tevent) {
850 				err_msg("Error alloc trace event");
851 				exit(EXIT_FAILURE);
852 			}
853 
854 			if (params->events)
855 				tevent->next = params->events;
856 
857 			params->events = tevent;
858 			break;
859 		case 'E':
860 			params->entries = get_llong_from_str(optarg);
861 			if ((params->entries < 10) || (params->entries > 9999999))
862 					timerlat_hist_usage("Entries must be > 10 and < 9999999\n");
863 			break;
864 		case 'h':
865 		case '?':
866 			timerlat_hist_usage(NULL);
867 			break;
868 		case 'H':
869 			params->hk_cpus = 1;
870 			retval = parse_cpu_set(optarg, ¶ms->hk_cpu_set);
871 			if (retval) {
872 				err_msg("Error parsing house keeping CPUs\n");
873 				exit(EXIT_FAILURE);
874 			}
875 			break;
876 		case 'i':
877 			params->stop_us = get_llong_from_str(optarg);
878 			break;
879 		case 'k':
880 			params->kernel_workload = 1;
881 			break;
882 		case 'n':
883 			params->output_divisor = 1;
884 			break;
885 		case 'p':
886 			params->timerlat_period_us = get_llong_from_str(optarg);
887 			if (params->timerlat_period_us > 1000000)
888 				timerlat_hist_usage("Period longer than 1 s\n");
889 			break;
890 		case 'P':
891 			retval = parse_prio(optarg, ¶ms->sched_param);
892 			if (retval == -1)
893 				timerlat_hist_usage("Invalid -P priority");
894 			params->set_sched = 1;
895 			break;
896 		case 's':
897 			params->print_stack = get_llong_from_str(optarg);
898 			break;
899 		case 'T':
900 			params->stop_total_us = get_llong_from_str(optarg);
901 			break;
902 		case 't':
903 			if (optarg) {
904 				if (optarg[0] == '=')
905 					params->trace_output = &optarg[1];
906 				else
907 					params->trace_output = &optarg[0];
908 			} else if (optind < argc && argv[optind][0] != '-')
909 				params->trace_output = argv[optind];
910 			else
911 				params->trace_output = "timerlat_trace.txt";
912 			break;
913 		case 'u':
914 			params->user_workload = 1;
915 			/* fallback: -u implies in -U */
916 		case 'U':
917 			params->user_hist = 1;
918 			break;
919 		case '0': /* no irq */
920 			params->no_irq = 1;
921 			break;
922 		case '1': /* no thread */
923 			params->no_thread = 1;
924 			break;
925 		case '2': /* no header */
926 			params->no_header = 1;
927 			break;
928 		case '3': /* no summary */
929 			params->no_summary = 1;
930 			break;
931 		case '4': /* no index */
932 			params->no_index = 1;
933 			break;
934 		case '5': /* with zeros */
935 			params->with_zeros = 1;
936 			break;
937 		case '6': /* trigger */
938 			if (params->events) {
939 				retval = trace_event_add_trigger(params->events, optarg);
940 				if (retval) {
941 					err_msg("Error adding trigger %s\n", optarg);
942 					exit(EXIT_FAILURE);
943 				}
944 			} else {
945 				timerlat_hist_usage("--trigger requires a previous -e\n");
946 			}
947 			break;
948 		case '7': /* filter */
949 			if (params->events) {
950 				retval = trace_event_add_filter(params->events, optarg);
951 				if (retval) {
952 					err_msg("Error adding filter %s\n", optarg);
953 					exit(EXIT_FAILURE);
954 				}
955 			} else {
956 				timerlat_hist_usage("--filter requires a previous -e\n");
957 			}
958 			break;
959 		case '8':
960 			params->dma_latency = get_llong_from_str(optarg);
961 			if (params->dma_latency < 0 || params->dma_latency > 10000) {
962 				err_msg("--dma-latency needs to be >= 0 and < 10000");
963 				exit(EXIT_FAILURE);
964 			}
965 			break;
966 		case '9':
967 			params->no_aa = 1;
968 			break;
969 		case '\1':
970 			params->dump_tasks = 1;
971 			break;
972 		case '\2':
973 			params->warmup = get_llong_from_str(optarg);
974 			break;
975 		case '\3':
976 			params->buffer_size = get_llong_from_str(optarg);
977 			break;
978 		default:
979 			timerlat_hist_usage("Invalid option");
980 		}
981 	}
982 
983 	if (geteuid()) {
984 		err_msg("rtla needs root permission\n");
985 		exit(EXIT_FAILURE);
986 	}
987 
988 	if (params->no_irq && params->no_thread)
989 		timerlat_hist_usage("no-irq and no-thread set, there is nothing to do here");
990 
991 	if (params->no_index && !params->with_zeros)
992 		timerlat_hist_usage("no-index set with with-zeros is not set - it does not make sense");
993 
994 	/*
995 	 * Auto analysis only happens if stop tracing, thus:
996 	 */
997 	if (!params->stop_us && !params->stop_total_us)
998 		params->no_aa = 1;
999 
1000 	if (params->kernel_workload && params->user_workload)
1001 		timerlat_hist_usage("--kernel-threads and --user-threads are mutually exclusive!");
1002 
1003 	return params;
1004 }
1005 
1006 /*
1007  * timerlat_hist_apply_config - apply the hist configs to the initialized tool
1008  */
1009 static int
timerlat_hist_apply_config(struct osnoise_tool * tool,struct timerlat_hist_params * params)1010 timerlat_hist_apply_config(struct osnoise_tool *tool, struct timerlat_hist_params *params)
1011 {
1012 	int retval, i;
1013 
1014 	if (!params->sleep_time)
1015 		params->sleep_time = 1;
1016 
1017 	if (params->cpus) {
1018 		retval = osnoise_set_cpus(tool->context, params->cpus);
1019 		if (retval) {
1020 			err_msg("Failed to apply CPUs config\n");
1021 			goto out_err;
1022 		}
1023 	} else {
1024 		for (i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++)
1025 			CPU_SET(i, ¶ms->monitored_cpus);
1026 	}
1027 
1028 	if (params->stop_us) {
1029 		retval = osnoise_set_stop_us(tool->context, params->stop_us);
1030 		if (retval) {
1031 			err_msg("Failed to set stop us\n");
1032 			goto out_err;
1033 		}
1034 	}
1035 
1036 	if (params->stop_total_us) {
1037 		retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us);
1038 		if (retval) {
1039 			err_msg("Failed to set stop total us\n");
1040 			goto out_err;
1041 		}
1042 	}
1043 
1044 	if (params->timerlat_period_us) {
1045 		retval = osnoise_set_timerlat_period_us(tool->context, params->timerlat_period_us);
1046 		if (retval) {
1047 			err_msg("Failed to set timerlat period\n");
1048 			goto out_err;
1049 		}
1050 	}
1051 
1052 	if (params->print_stack) {
1053 		retval = osnoise_set_print_stack(tool->context, params->print_stack);
1054 		if (retval) {
1055 			err_msg("Failed to set print stack\n");
1056 			goto out_err;
1057 		}
1058 	}
1059 
1060 	if (params->hk_cpus) {
1061 		retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set),
1062 					   ¶ms->hk_cpu_set);
1063 		if (retval == -1) {
1064 			err_msg("Failed to set rtla to the house keeping CPUs\n");
1065 			goto out_err;
1066 		}
1067 	} else if (params->cpus) {
1068 		/*
1069 		 * Even if the user do not set a house-keeping CPU, try to
1070 		 * move rtla to a CPU set different to the one where the user
1071 		 * set the workload to run.
1072 		 *
1073 		 * No need to check results as this is an automatic attempt.
1074 		 */
1075 		auto_house_keeping(¶ms->monitored_cpus);
1076 	}
1077 
1078 	/*
1079 	 * If the user did not specify a type of thread, try user-threads first.
1080 	 * Fall back to kernel threads otherwise.
1081 	 */
1082 	if (!params->kernel_workload && !params->user_hist) {
1083 		retval = tracefs_file_exists(NULL, "osnoise/per_cpu/cpu0/timerlat_fd");
1084 		if (retval) {
1085 			debug_msg("User-space interface detected, setting user-threads\n");
1086 			params->user_workload = 1;
1087 			params->user_hist = 1;
1088 		} else {
1089 			debug_msg("User-space interface not detected, setting kernel-threads\n");
1090 			params->kernel_workload = 1;
1091 		}
1092 	}
1093 
1094 	/*
1095 	* Set workload according to type of thread if the kernel supports it.
1096 	* On kernels without support, user threads will have already failed
1097 	* on missing timerlat_fd, and kernel threads do not need it.
1098 	*/
1099 	retval = osnoise_set_workload(tool->context, params->kernel_workload);
1100 	if (retval < -1) {
1101 		err_msg("Failed to set OSNOISE_WORKLOAD option\n");
1102 		goto out_err;
1103 	}
1104 
1105 	return 0;
1106 
1107 out_err:
1108 	return -1;
1109 }
1110 
1111 /*
1112  * timerlat_init_hist - initialize a timerlat hist tool with parameters
1113  */
1114 static struct osnoise_tool
timerlat_init_hist(struct timerlat_hist_params * params)1115 *timerlat_init_hist(struct timerlat_hist_params *params)
1116 {
1117 	struct osnoise_tool *tool;
1118 	int nr_cpus;
1119 
1120 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
1121 
1122 	tool = osnoise_init_tool("timerlat_hist");
1123 	if (!tool)
1124 		return NULL;
1125 
1126 	tool->data = timerlat_alloc_histogram(nr_cpus, params->entries, params->bucket_size);
1127 	if (!tool->data)
1128 		goto out_err;
1129 
1130 	tool->params = params;
1131 
1132 	tep_register_event_handler(tool->trace.tep, -1, "ftrace", "timerlat",
1133 				   timerlat_hist_handler, tool);
1134 
1135 	return tool;
1136 
1137 out_err:
1138 	osnoise_destroy_tool(tool);
1139 	return NULL;
1140 }
1141 
1142 static int stop_tracing;
1143 static struct trace_instance *hist_inst = NULL;
stop_hist(int sig)1144 static void stop_hist(int sig)
1145 {
1146 	if (stop_tracing) {
1147 		/*
1148 		 * Stop requested twice in a row; abort event processing and
1149 		 * exit immediately
1150 		 */
1151 		tracefs_iterate_stop(hist_inst->inst);
1152 		return;
1153 	}
1154 	stop_tracing = 1;
1155 	if (hist_inst)
1156 		trace_instance_stop(hist_inst);
1157 }
1158 
1159 /*
1160  * timerlat_hist_set_signals - handles the signal to stop the tool
1161  */
1162 static void
timerlat_hist_set_signals(struct timerlat_hist_params * params)1163 timerlat_hist_set_signals(struct timerlat_hist_params *params)
1164 {
1165 	signal(SIGINT, stop_hist);
1166 	if (params->duration) {
1167 		signal(SIGALRM, stop_hist);
1168 		alarm(params->duration);
1169 	}
1170 }
1171 
timerlat_hist_main(int argc,char * argv[])1172 int timerlat_hist_main(int argc, char *argv[])
1173 {
1174 	struct timerlat_hist_params *params;
1175 	struct osnoise_tool *record = NULL;
1176 	struct timerlat_u_params params_u;
1177 	struct osnoise_tool *tool = NULL;
1178 	struct osnoise_tool *aa = NULL;
1179 	struct trace_instance *trace;
1180 	int dma_latency_fd = -1;
1181 	int return_value = 1;
1182 	pthread_t timerlat_u;
1183 	int retval;
1184 
1185 	params = timerlat_hist_parse_args(argc, argv);
1186 	if (!params)
1187 		exit(1);
1188 
1189 	tool = timerlat_init_hist(params);
1190 	if (!tool) {
1191 		err_msg("Could not init osnoise hist\n");
1192 		goto out_exit;
1193 	}
1194 
1195 	retval = timerlat_hist_apply_config(tool, params);
1196 	if (retval) {
1197 		err_msg("Could not apply config\n");
1198 		goto out_free;
1199 	}
1200 
1201 	trace = &tool->trace;
1202 	/*
1203 	 * Save trace instance into global variable so that SIGINT can stop
1204 	 * the timerlat tracer.
1205 	 * Otherwise, rtla could loop indefinitely when overloaded.
1206 	 */
1207 	hist_inst = trace;
1208 
1209 	retval = enable_timerlat(trace);
1210 	if (retval) {
1211 		err_msg("Failed to enable timerlat tracer\n");
1212 		goto out_free;
1213 	}
1214 
1215 	if (params->set_sched) {
1216 		retval = set_comm_sched_attr("timerlat/", ¶ms->sched_param);
1217 		if (retval) {
1218 			err_msg("Failed to set sched parameters\n");
1219 			goto out_free;
1220 		}
1221 	}
1222 
1223 	if (params->cgroup && !params->user_workload) {
1224 		retval = set_comm_cgroup("timerlat/", params->cgroup_name);
1225 		if (!retval) {
1226 			err_msg("Failed to move threads to cgroup\n");
1227 			goto out_free;
1228 		}
1229 	}
1230 
1231 	if (params->dma_latency >= 0) {
1232 		dma_latency_fd = set_cpu_dma_latency(params->dma_latency);
1233 		if (dma_latency_fd < 0) {
1234 			err_msg("Could not set /dev/cpu_dma_latency.\n");
1235 			goto out_free;
1236 		}
1237 	}
1238 
1239 	if (params->trace_output) {
1240 		record = osnoise_init_trace_tool("timerlat");
1241 		if (!record) {
1242 			err_msg("Failed to enable the trace instance\n");
1243 			goto out_free;
1244 		}
1245 
1246 		if (params->events) {
1247 			retval = trace_events_enable(&record->trace, params->events);
1248 			if (retval)
1249 				goto out_hist;
1250 		}
1251 
1252 		if (params->buffer_size > 0) {
1253 			retval = trace_set_buffer_size(&record->trace, params->buffer_size);
1254 			if (retval)
1255 				goto out_hist;
1256 		}
1257 	}
1258 
1259 	if (!params->no_aa) {
1260 		aa = osnoise_init_tool("timerlat_aa");
1261 		if (!aa)
1262 			goto out_hist;
1263 
1264 		retval = timerlat_aa_init(aa, params->dump_tasks);
1265 		if (retval) {
1266 			err_msg("Failed to enable the auto analysis instance\n");
1267 			goto out_hist;
1268 		}
1269 
1270 		retval = enable_timerlat(&aa->trace);
1271 		if (retval) {
1272 			err_msg("Failed to enable timerlat tracer\n");
1273 			goto out_hist;
1274 		}
1275 	}
1276 
1277 	if (params->user_workload) {
1278 		/* rtla asked to stop */
1279 		params_u.should_run = 1;
1280 		/* all threads left */
1281 		params_u.stopped_running = 0;
1282 
1283 		params_u.set = ¶ms->monitored_cpus;
1284 		if (params->set_sched)
1285 			params_u.sched_param = ¶ms->sched_param;
1286 		else
1287 			params_u.sched_param = NULL;
1288 
1289 		params_u.cgroup_name = params->cgroup_name;
1290 
1291 		retval = pthread_create(&timerlat_u, NULL, timerlat_u_dispatcher, ¶ms_u);
1292 		if (retval)
1293 			err_msg("Error creating timerlat user-space threads\n");
1294 	}
1295 
1296 	if (params->warmup > 0) {
1297 		debug_msg("Warming up for %d seconds\n", params->warmup);
1298 		sleep(params->warmup);
1299 		if (stop_tracing)
1300 			goto out_hist;
1301 	}
1302 
1303 	/*
1304 	 * Start the tracers here, after having set all instances.
1305 	 *
1306 	 * Let the trace instance start first for the case of hitting a stop
1307 	 * tracing while enabling other instances. The trace instance is the
1308 	 * one with most valuable information.
1309 	 */
1310 	if (params->trace_output)
1311 		trace_instance_start(&record->trace);
1312 	if (!params->no_aa)
1313 		trace_instance_start(&aa->trace);
1314 	trace_instance_start(trace);
1315 
1316 	tool->start_time = time(NULL);
1317 	timerlat_hist_set_signals(params);
1318 
1319 	while (!stop_tracing) {
1320 		sleep(params->sleep_time);
1321 
1322 		retval = tracefs_iterate_raw_events(trace->tep,
1323 						    trace->inst,
1324 						    NULL,
1325 						    0,
1326 						    collect_registered_events,
1327 						    trace);
1328 		if (retval < 0) {
1329 			err_msg("Error iterating on events\n");
1330 			goto out_hist;
1331 		}
1332 
1333 		if (trace_is_off(&tool->trace, &record->trace))
1334 			break;
1335 
1336 		/* is there still any user-threads ? */
1337 		if (params->user_workload) {
1338 			if (params_u.stopped_running) {
1339 				debug_msg("timerlat user-space threads stopped!\n");
1340 				break;
1341 			}
1342 		}
1343 	}
1344 
1345 	if (params->user_workload && !params_u.stopped_running) {
1346 		params_u.should_run = 0;
1347 		sleep(1);
1348 	}
1349 
1350 	timerlat_print_stats(params, tool);
1351 
1352 	return_value = 0;
1353 
1354 	if (trace_is_off(&tool->trace, &record->trace) && !stop_tracing) {
1355 		printf("rtla timerlat hit stop tracing\n");
1356 
1357 		if (!params->no_aa)
1358 			timerlat_auto_analysis(params->stop_us, params->stop_total_us);
1359 
1360 		if (params->trace_output) {
1361 			printf("  Saving trace to %s\n", params->trace_output);
1362 			save_trace_to_file(record->trace.inst, params->trace_output);
1363 		}
1364 	}
1365 
1366 out_hist:
1367 	timerlat_aa_destroy();
1368 	if (dma_latency_fd >= 0)
1369 		close(dma_latency_fd);
1370 	trace_events_destroy(&record->trace, params->events);
1371 	params->events = NULL;
1372 out_free:
1373 	timerlat_free_histogram(tool->data);
1374 	osnoise_destroy_tool(aa);
1375 	osnoise_destroy_tool(record);
1376 	osnoise_destroy_tool(tool);
1377 	free(params);
1378 out_exit:
1379 	exit(return_value);
1380 }
1381