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