• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
14 #include "utils.h"
15 #include "osnoise.h"
16 #include "timerlat.h"
17 
18 struct timerlat_hist_params {
19 	char			*cpus;
20 	char			*monitored_cpus;
21 	char			*trace_output;
22 	unsigned long long	runtime;
23 	long long		stop_us;
24 	long long		stop_total_us;
25 	long long		timerlat_period_us;
26 	long long		print_stack;
27 	int			sleep_time;
28 	int			output_divisor;
29 	int			duration;
30 	int			set_sched;
31 	int			dma_latency;
32 	struct sched_attr	sched_param;
33 	struct trace_events	*events;
34 
35 	char			no_irq;
36 	char			no_thread;
37 	char			no_header;
38 	char			no_summary;
39 	char			no_index;
40 	char			with_zeros;
41 	int			bucket_size;
42 	int			entries;
43 };
44 
45 struct timerlat_hist_cpu {
46 	int			*irq;
47 	int			*thread;
48 
49 	int			irq_count;
50 	int			thread_count;
51 
52 	unsigned long long	min_irq;
53 	unsigned long long	sum_irq;
54 	unsigned long long	max_irq;
55 
56 	unsigned long long	min_thread;
57 	unsigned long long	sum_thread;
58 	unsigned long long	max_thread;
59 };
60 
61 struct timerlat_hist_data {
62 	struct timerlat_hist_cpu	*hist;
63 	int				entries;
64 	int				bucket_size;
65 	int				nr_cpus;
66 };
67 
68 /*
69  * timerlat_free_histogram - free runtime data
70  */
71 static void
timerlat_free_histogram(struct timerlat_hist_data * data)72 timerlat_free_histogram(struct timerlat_hist_data *data)
73 {
74 	int cpu;
75 
76 	/* one histogram for IRQ and one for thread, per CPU */
77 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
78 		if (data->hist[cpu].irq)
79 			free(data->hist[cpu].irq);
80 
81 		if (data->hist[cpu].thread)
82 			free(data->hist[cpu].thread);
83 	}
84 
85 	/* one set of histograms per CPU */
86 	if (data->hist)
87 		free(data->hist);
88 
89 	free(data);
90 }
91 
92 /*
93  * timerlat_alloc_histogram - alloc runtime data
94  */
95 static struct timerlat_hist_data
timerlat_alloc_histogram(int nr_cpus,int entries,int bucket_size)96 *timerlat_alloc_histogram(int nr_cpus, int entries, int bucket_size)
97 {
98 	struct timerlat_hist_data *data;
99 	int cpu;
100 
101 	data = calloc(1, sizeof(*data));
102 	if (!data)
103 		return NULL;
104 
105 	data->entries = entries;
106 	data->bucket_size = bucket_size;
107 	data->nr_cpus = nr_cpus;
108 
109 	/* one set of histograms per CPU */
110 	data->hist = calloc(1, sizeof(*data->hist) * nr_cpus);
111 	if (!data->hist)
112 		goto cleanup;
113 
114 	/* one histogram for IRQ and one for thread, per cpu */
115 	for (cpu = 0; cpu < nr_cpus; cpu++) {
116 		data->hist[cpu].irq = calloc(1, sizeof(*data->hist->irq) * (entries + 1));
117 		if (!data->hist[cpu].irq)
118 			goto cleanup;
119 		data->hist[cpu].thread = calloc(1, sizeof(*data->hist->thread) * (entries + 1));
120 		if (!data->hist[cpu].thread)
121 			goto cleanup;
122 	}
123 
124 	/* set the min to max */
125 	for (cpu = 0; cpu < nr_cpus; cpu++) {
126 		data->hist[cpu].min_irq = ~0;
127 		data->hist[cpu].min_thread = ~0;
128 	}
129 
130 	return data;
131 
132 cleanup:
133 	timerlat_free_histogram(data);
134 	return NULL;
135 }
136 
137 /*
138  * timerlat_hist_update - record a new timerlat occurent on cpu, updating data
139  */
140 static void
timerlat_hist_update(struct osnoise_tool * tool,int cpu,unsigned long long thread,unsigned long long latency)141 timerlat_hist_update(struct osnoise_tool *tool, int cpu,
142 		     unsigned long long thread,
143 		     unsigned long long latency)
144 {
145 	struct timerlat_hist_params *params = tool->params;
146 	struct timerlat_hist_data *data = tool->data;
147 	int entries = data->entries;
148 	int bucket;
149 	int *hist;
150 
151 	if (params->output_divisor)
152 		latency = latency / params->output_divisor;
153 
154 	bucket = latency / data->bucket_size;
155 
156 	if (!thread) {
157 		hist = data->hist[cpu].irq;
158 		data->hist[cpu].irq_count++;
159 		update_min(&data->hist[cpu].min_irq, &latency);
160 		update_sum(&data->hist[cpu].sum_irq, &latency);
161 		update_max(&data->hist[cpu].max_irq, &latency);
162 	} else {
163 		hist = data->hist[cpu].thread;
164 		data->hist[cpu].thread_count++;
165 		update_min(&data->hist[cpu].min_thread, &latency);
166 		update_sum(&data->hist[cpu].sum_thread, &latency);
167 		update_max(&data->hist[cpu].max_thread, &latency);
168 	}
169 
170 	if (bucket < entries)
171 		hist[bucket]++;
172 	else
173 		hist[entries]++;
174 }
175 
176 /*
177  * timerlat_hist_handler - this is the handler for timerlat tracer events
178  */
179 static int
timerlat_hist_handler(struct trace_seq * s,struct tep_record * record,struct tep_event * event,void * data)180 timerlat_hist_handler(struct trace_seq *s, struct tep_record *record,
181 		     struct tep_event *event, void *data)
182 {
183 	struct trace_instance *trace = data;
184 	unsigned long long thread, latency;
185 	struct osnoise_tool *tool;
186 	int cpu = record->cpu;
187 
188 	tool = container_of(trace, struct osnoise_tool, trace);
189 
190 	tep_get_field_val(s, event, "context", record, &thread, 1);
191 	tep_get_field_val(s, event, "timer_latency", record, &latency, 1);
192 
193 	timerlat_hist_update(tool, cpu, thread, latency);
194 
195 	return 0;
196 }
197 
198 /*
199  * timerlat_hist_header - print the header of the tracer to the output
200  */
timerlat_hist_header(struct osnoise_tool * tool)201 static void timerlat_hist_header(struct osnoise_tool *tool)
202 {
203 	struct timerlat_hist_params *params = tool->params;
204 	struct timerlat_hist_data *data = tool->data;
205 	struct trace_seq *s = tool->trace.seq;
206 	char duration[26];
207 	int cpu;
208 
209 	if (params->no_header)
210 		return;
211 
212 	get_duration(tool->start_time, duration, sizeof(duration));
213 	trace_seq_printf(s, "# RTLA timerlat histogram\n");
214 	trace_seq_printf(s, "# Time unit is %s (%s)\n",
215 			params->output_divisor == 1 ? "nanoseconds" : "microseconds",
216 			params->output_divisor == 1 ? "ns" : "us");
217 
218 	trace_seq_printf(s, "# Duration: %s\n", duration);
219 
220 	if (!params->no_index)
221 		trace_seq_printf(s, "Index");
222 
223 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
224 		if (params->cpus && !params->monitored_cpus[cpu])
225 			continue;
226 
227 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
228 			continue;
229 
230 		if (!params->no_irq)
231 			trace_seq_printf(s, "   IRQ-%03d", cpu);
232 
233 		if (!params->no_thread)
234 			trace_seq_printf(s, "   Thr-%03d", cpu);
235 	}
236 	trace_seq_printf(s, "\n");
237 
238 
239 	trace_seq_do_printf(s);
240 	trace_seq_reset(s);
241 }
242 
243 /*
244  * timerlat_print_summary - print the summary of the hist data to the output
245  */
246 static void
timerlat_print_summary(struct timerlat_hist_params * params,struct trace_instance * trace,struct timerlat_hist_data * data)247 timerlat_print_summary(struct timerlat_hist_params *params,
248 		       struct trace_instance *trace,
249 		       struct timerlat_hist_data *data)
250 {
251 	int cpu;
252 
253 	if (params->no_summary)
254 		return;
255 
256 	if (!params->no_index)
257 		trace_seq_printf(trace->seq, "count:");
258 
259 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
260 		if (params->cpus && !params->monitored_cpus[cpu])
261 			continue;
262 
263 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
264 			continue;
265 
266 		if (!params->no_irq)
267 			trace_seq_printf(trace->seq, "%9d ",
268 					data->hist[cpu].irq_count);
269 
270 		if (!params->no_thread)
271 			trace_seq_printf(trace->seq, "%9d ",
272 					data->hist[cpu].thread_count);
273 	}
274 	trace_seq_printf(trace->seq, "\n");
275 
276 	if (!params->no_index)
277 		trace_seq_printf(trace->seq, "min:  ");
278 
279 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
280 		if (params->cpus && !params->monitored_cpus[cpu])
281 			continue;
282 
283 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
284 			continue;
285 
286 		if (!params->no_irq)
287 			trace_seq_printf(trace->seq, "%9llu ",
288 					data->hist[cpu].min_irq);
289 
290 		if (!params->no_thread)
291 			trace_seq_printf(trace->seq, "%9llu ",
292 					data->hist[cpu].min_thread);
293 	}
294 	trace_seq_printf(trace->seq, "\n");
295 
296 	if (!params->no_index)
297 		trace_seq_printf(trace->seq, "avg:  ");
298 
299 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
300 		if (params->cpus && !params->monitored_cpus[cpu])
301 			continue;
302 
303 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
304 			continue;
305 
306 		if (!params->no_irq) {
307 			if (data->hist[cpu].irq_count)
308 				trace_seq_printf(trace->seq, "%9llu ",
309 						 data->hist[cpu].sum_irq / data->hist[cpu].irq_count);
310 			else
311 				trace_seq_printf(trace->seq, "        - ");
312 		}
313 
314 		if (!params->no_thread) {
315 			if (data->hist[cpu].thread_count)
316 				trace_seq_printf(trace->seq, "%9llu ",
317 						data->hist[cpu].sum_thread / data->hist[cpu].thread_count);
318 			else
319 				trace_seq_printf(trace->seq, "        - ");
320 		}
321 	}
322 	trace_seq_printf(trace->seq, "\n");
323 
324 	if (!params->no_index)
325 		trace_seq_printf(trace->seq, "max:  ");
326 
327 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
328 		if (params->cpus && !params->monitored_cpus[cpu])
329 			continue;
330 
331 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
332 			continue;
333 
334 		if (!params->no_irq)
335 			trace_seq_printf(trace->seq, "%9llu ",
336 					data->hist[cpu].max_irq);
337 
338 		if (!params->no_thread)
339 			trace_seq_printf(trace->seq, "%9llu ",
340 					data->hist[cpu].max_thread);
341 	}
342 	trace_seq_printf(trace->seq, "\n");
343 	trace_seq_do_printf(trace->seq);
344 	trace_seq_reset(trace->seq);
345 }
346 
347 /*
348  * timerlat_print_stats - print data for all CPUs
349  */
350 static void
timerlat_print_stats(struct timerlat_hist_params * params,struct osnoise_tool * tool)351 timerlat_print_stats(struct timerlat_hist_params *params, struct osnoise_tool *tool)
352 {
353 	struct timerlat_hist_data *data = tool->data;
354 	struct trace_instance *trace = &tool->trace;
355 	int bucket, cpu;
356 	int total;
357 
358 	timerlat_hist_header(tool);
359 
360 	for (bucket = 0; bucket < data->entries; bucket++) {
361 		total = 0;
362 
363 		if (!params->no_index)
364 			trace_seq_printf(trace->seq, "%-6d",
365 					 bucket * data->bucket_size);
366 
367 		for (cpu = 0; cpu < data->nr_cpus; cpu++) {
368 			if (params->cpus && !params->monitored_cpus[cpu])
369 				continue;
370 
371 			if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
372 				continue;
373 
374 			if (!params->no_irq) {
375 				total += data->hist[cpu].irq[bucket];
376 				trace_seq_printf(trace->seq, "%9d ",
377 						data->hist[cpu].irq[bucket]);
378 			}
379 
380 			if (!params->no_thread) {
381 				total += data->hist[cpu].thread[bucket];
382 				trace_seq_printf(trace->seq, "%9d ",
383 						data->hist[cpu].thread[bucket]);
384 			}
385 
386 		}
387 
388 		if (total == 0 && !params->with_zeros) {
389 			trace_seq_reset(trace->seq);
390 			continue;
391 		}
392 
393 		trace_seq_printf(trace->seq, "\n");
394 		trace_seq_do_printf(trace->seq);
395 		trace_seq_reset(trace->seq);
396 	}
397 
398 	if (!params->no_index)
399 		trace_seq_printf(trace->seq, "over: ");
400 
401 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
402 		if (params->cpus && !params->monitored_cpus[cpu])
403 			continue;
404 
405 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
406 			continue;
407 
408 		if (!params->no_irq)
409 			trace_seq_printf(trace->seq, "%9d ",
410 					 data->hist[cpu].irq[data->entries]);
411 
412 		if (!params->no_thread)
413 			trace_seq_printf(trace->seq, "%9d ",
414 					 data->hist[cpu].thread[data->entries]);
415 	}
416 	trace_seq_printf(trace->seq, "\n");
417 	trace_seq_do_printf(trace->seq);
418 	trace_seq_reset(trace->seq);
419 
420 	timerlat_print_summary(params, trace, data);
421 }
422 
423 /*
424  * timerlat_hist_usage - prints timerlat top usage message
425  */
timerlat_hist_usage(char * usage)426 static void timerlat_hist_usage(char *usage)
427 {
428 	int i;
429 
430 	char *msg[] = {
431 		"",
432 		"  usage: [rtla] timerlat hist [-h] [-q] [-d s] [-D] [-n] [-a us] [-p us] [-i us] [-T us] [-s us] \\",
433 		"         [-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] \\",
434 		"	  [-P priority] [-E N] [-b N] [--no-irq] [--no-thread] [--no-header] [--no-summary] \\",
435 		"	  [--no-index] [--with-zeros] [--dma-latency us]",
436 		"",
437 		"	  -h/--help: print this menu",
438 		"	  -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",
439 		"	  -p/--period us: timerlat period in us",
440 		"	  -i/--irq us: stop trace if the irq latency is higher than the argument in us",
441 		"	  -T/--thread us: stop trace if the thread latency is higher than the argument in us",
442 		"	  -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",
443 		"	  -c/--cpus cpus: run the tracer only on the given cpus",
444 		"	  -d/--duration time[m|h|d]: duration of the session in seconds",
445 		"	  -D/--debug: print debug info",
446 		"	  -t/--trace[=file]: save the stopped trace to [file|timerlat_trace.txt]",
447 		"	  -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
448 		"	     --filter <filter>: enable a trace event filter to the previous -e event",
449 		"	     --trigger <trigger>: enable a trace event trigger to the previous -e event",
450 		"	  -n/--nano: display data in nanoseconds",
451 		"	  -b/--bucket-size N: set the histogram bucket size (default 1)",
452 		"	  -E/--entries N: set the number of entries of the histogram (default 256)",
453 		"	     --no-irq: ignore IRQ latencies",
454 		"	     --no-thread: ignore thread latencies",
455 		"	     --no-header: do not print header",
456 		"	     --no-summary: do not print summary",
457 		"	     --no-index: do not print index",
458 		"	     --with-zeros: print zero only entries",
459 		"	     --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency",
460 		"	  -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
461 		"		o:prio - use SCHED_OTHER with prio",
462 		"		r:prio - use SCHED_RR with prio",
463 		"		f:prio - use SCHED_FIFO with prio",
464 		"		d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
465 		"						       in nanoseconds",
466 		NULL,
467 	};
468 
469 	if (usage)
470 		fprintf(stderr, "%s\n", usage);
471 
472 	fprintf(stderr, "rtla timerlat hist: a per-cpu histogram of the timer latency (version %s)\n",
473 			VERSION);
474 
475 	for (i = 0; msg[i]; i++)
476 		fprintf(stderr, "%s\n", msg[i]);
477 
478 	if (usage)
479 		exit(EXIT_FAILURE);
480 
481 	exit(EXIT_SUCCESS);
482 }
483 
484 /*
485  * timerlat_hist_parse_args - allocs, parse and fill the cmd line parameters
486  */
487 static struct timerlat_hist_params
timerlat_hist_parse_args(int argc,char * argv[])488 *timerlat_hist_parse_args(int argc, char *argv[])
489 {
490 	struct timerlat_hist_params *params;
491 	struct trace_events *tevent;
492 	int auto_thresh;
493 	int retval;
494 	int c;
495 
496 	params = calloc(1, sizeof(*params));
497 	if (!params)
498 		exit(1);
499 
500 	/* disabled by default */
501 	params->dma_latency = -1;
502 
503 	/* display data in microseconds */
504 	params->output_divisor = 1000;
505 	params->bucket_size = 1;
506 	params->entries = 256;
507 
508 	while (1) {
509 		static struct option long_options[] = {
510 			{"auto",		required_argument,	0, 'a'},
511 			{"cpus",		required_argument,	0, 'c'},
512 			{"bucket-size",		required_argument,	0, 'b'},
513 			{"debug",		no_argument,		0, 'D'},
514 			{"entries",		required_argument,	0, 'E'},
515 			{"duration",		required_argument,	0, 'd'},
516 			{"help",		no_argument,		0, 'h'},
517 			{"irq",			required_argument,	0, 'i'},
518 			{"nano",		no_argument,		0, 'n'},
519 			{"period",		required_argument,	0, 'p'},
520 			{"priority",		required_argument,	0, 'P'},
521 			{"stack",		required_argument,	0, 's'},
522 			{"thread",		required_argument,	0, 'T'},
523 			{"trace",		optional_argument,	0, 't'},
524 			{"event",		required_argument,	0, 'e'},
525 			{"no-irq",		no_argument,		0, '0'},
526 			{"no-thread",		no_argument,		0, '1'},
527 			{"no-header",		no_argument,		0, '2'},
528 			{"no-summary",		no_argument,		0, '3'},
529 			{"no-index",		no_argument,		0, '4'},
530 			{"with-zeros",		no_argument,		0, '5'},
531 			{"trigger",		required_argument,	0, '6'},
532 			{"filter",		required_argument,	0, '7'},
533 			{"dma-latency",		required_argument,	0, '8'},
534 			{0, 0, 0, 0}
535 		};
536 
537 		/* getopt_long stores the option index here. */
538 		int option_index = 0;
539 
540 		c = getopt_long(argc, argv, "a:c:b:d:e:E:Dhi:np:P:s:t::T:0123456:7:8:",
541 				 long_options, &option_index);
542 
543 		/* detect the end of the options. */
544 		if (c == -1)
545 			break;
546 
547 		switch (c) {
548 		case 'a':
549 			auto_thresh = get_llong_from_str(optarg);
550 
551 			/* set thread stop to auto_thresh */
552 			params->stop_total_us = auto_thresh;
553 
554 			/* get stack trace */
555 			params->print_stack = auto_thresh;
556 
557 			/* set trace */
558 			params->trace_output = "timerlat_trace.txt";
559 
560 			break;
561 		case 'c':
562 			retval = parse_cpu_list(optarg, &params->monitored_cpus);
563 			if (retval)
564 				timerlat_hist_usage("\nInvalid -c cpu list\n");
565 			params->cpus = optarg;
566 			break;
567 		case 'b':
568 			params->bucket_size = get_llong_from_str(optarg);
569 			if ((params->bucket_size == 0) || (params->bucket_size >= 1000000))
570 				timerlat_hist_usage("Bucket size needs to be > 0 and <= 1000000\n");
571 			break;
572 		case 'D':
573 			config_debug = 1;
574 			break;
575 		case 'd':
576 			params->duration = parse_seconds_duration(optarg);
577 			if (!params->duration)
578 				timerlat_hist_usage("Invalid -D duration\n");
579 			break;
580 		case 'e':
581 			tevent = trace_event_alloc(optarg);
582 			if (!tevent) {
583 				err_msg("Error alloc trace event");
584 				exit(EXIT_FAILURE);
585 			}
586 
587 			if (params->events)
588 				tevent->next = params->events;
589 
590 			params->events = tevent;
591 			break;
592 		case 'E':
593 			params->entries = get_llong_from_str(optarg);
594 			if ((params->entries < 10) || (params->entries > 9999999))
595 					timerlat_hist_usage("Entries must be > 10 and < 9999999\n");
596 			break;
597 		case 'h':
598 		case '?':
599 			timerlat_hist_usage(NULL);
600 			break;
601 		case 'i':
602 			params->stop_us = get_llong_from_str(optarg);
603 			break;
604 		case 'n':
605 			params->output_divisor = 1;
606 			break;
607 		case 'p':
608 			params->timerlat_period_us = get_llong_from_str(optarg);
609 			if (params->timerlat_period_us > 1000000)
610 				timerlat_hist_usage("Period longer than 1 s\n");
611 			break;
612 		case 'P':
613 			retval = parse_prio(optarg, &params->sched_param);
614 			if (retval == -1)
615 				timerlat_hist_usage("Invalid -P priority");
616 			params->set_sched = 1;
617 			break;
618 		case 's':
619 			params->print_stack = get_llong_from_str(optarg);
620 			break;
621 		case 'T':
622 			params->stop_total_us = get_llong_from_str(optarg);
623 			break;
624 		case 't':
625 			if (optarg)
626 				/* skip = */
627 				params->trace_output = &optarg[1];
628 			else
629 				params->trace_output = "timerlat_trace.txt";
630 			break;
631 		case '0': /* no irq */
632 			params->no_irq = 1;
633 			break;
634 		case '1': /* no thread */
635 			params->no_thread = 1;
636 			break;
637 		case '2': /* no header */
638 			params->no_header = 1;
639 			break;
640 		case '3': /* no summary */
641 			params->no_summary = 1;
642 			break;
643 		case '4': /* no index */
644 			params->no_index = 1;
645 			break;
646 		case '5': /* with zeros */
647 			params->with_zeros = 1;
648 			break;
649 		case '6': /* trigger */
650 			if (params->events) {
651 				retval = trace_event_add_trigger(params->events, optarg);
652 				if (retval) {
653 					err_msg("Error adding trigger %s\n", optarg);
654 					exit(EXIT_FAILURE);
655 				}
656 			} else {
657 				timerlat_hist_usage("--trigger requires a previous -e\n");
658 			}
659 			break;
660 		case '7': /* filter */
661 			if (params->events) {
662 				retval = trace_event_add_filter(params->events, optarg);
663 				if (retval) {
664 					err_msg("Error adding filter %s\n", optarg);
665 					exit(EXIT_FAILURE);
666 				}
667 			} else {
668 				timerlat_hist_usage("--filter requires a previous -e\n");
669 			}
670 			break;
671 		case '8':
672 			params->dma_latency = get_llong_from_str(optarg);
673 			if (params->dma_latency < 0 || params->dma_latency > 10000) {
674 				err_msg("--dma-latency needs to be >= 0 and < 10000");
675 				exit(EXIT_FAILURE);
676 			}
677 			break;
678 		default:
679 			timerlat_hist_usage("Invalid option");
680 		}
681 	}
682 
683 	if (geteuid()) {
684 		err_msg("rtla needs root permission\n");
685 		exit(EXIT_FAILURE);
686 	}
687 
688 	if (params->no_irq && params->no_thread)
689 		timerlat_hist_usage("no-irq and no-thread set, there is nothing to do here");
690 
691 	if (params->no_index && !params->with_zeros)
692 		timerlat_hist_usage("no-index set with with-zeros is not set - it does not make sense");
693 
694 	return params;
695 }
696 
697 /*
698  * timerlat_hist_apply_config - apply the hist configs to the initialized tool
699  */
700 static int
timerlat_hist_apply_config(struct osnoise_tool * tool,struct timerlat_hist_params * params)701 timerlat_hist_apply_config(struct osnoise_tool *tool, struct timerlat_hist_params *params)
702 {
703 	int retval;
704 
705 	if (!params->sleep_time)
706 		params->sleep_time = 1;
707 
708 	if (params->cpus) {
709 		retval = osnoise_set_cpus(tool->context, params->cpus);
710 		if (retval) {
711 			err_msg("Failed to apply CPUs config\n");
712 			goto out_err;
713 		}
714 	}
715 
716 	if (params->stop_us) {
717 		retval = osnoise_set_stop_us(tool->context, params->stop_us);
718 		if (retval) {
719 			err_msg("Failed to set stop us\n");
720 			goto out_err;
721 		}
722 	}
723 
724 	if (params->stop_total_us) {
725 		retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us);
726 		if (retval) {
727 			err_msg("Failed to set stop total us\n");
728 			goto out_err;
729 		}
730 	}
731 
732 	if (params->timerlat_period_us) {
733 		retval = osnoise_set_timerlat_period_us(tool->context, params->timerlat_period_us);
734 		if (retval) {
735 			err_msg("Failed to set timerlat period\n");
736 			goto out_err;
737 		}
738 	}
739 
740 	if (params->print_stack) {
741 		retval = osnoise_set_print_stack(tool->context, params->print_stack);
742 		if (retval) {
743 			err_msg("Failed to set print stack\n");
744 			goto out_err;
745 		}
746 	}
747 
748 	return 0;
749 
750 out_err:
751 	return -1;
752 }
753 
754 /*
755  * timerlat_init_hist - initialize a timerlat hist tool with parameters
756  */
757 static struct osnoise_tool
timerlat_init_hist(struct timerlat_hist_params * params)758 *timerlat_init_hist(struct timerlat_hist_params *params)
759 {
760 	struct osnoise_tool *tool;
761 	int nr_cpus;
762 
763 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
764 
765 	tool = osnoise_init_tool("timerlat_hist");
766 	if (!tool)
767 		return NULL;
768 
769 	tool->data = timerlat_alloc_histogram(nr_cpus, params->entries, params->bucket_size);
770 	if (!tool->data)
771 		goto out_err;
772 
773 	tool->params = params;
774 
775 	tep_register_event_handler(tool->trace.tep, -1, "ftrace", "timerlat",
776 				   timerlat_hist_handler, tool);
777 
778 	return tool;
779 
780 out_err:
781 	osnoise_destroy_tool(tool);
782 	return NULL;
783 }
784 
785 static int stop_tracing;
stop_hist(int sig)786 static void stop_hist(int sig)
787 {
788 	stop_tracing = 1;
789 }
790 
791 /*
792  * timerlat_hist_set_signals - handles the signal to stop the tool
793  */
794 static void
timerlat_hist_set_signals(struct timerlat_hist_params * params)795 timerlat_hist_set_signals(struct timerlat_hist_params *params)
796 {
797 	signal(SIGINT, stop_hist);
798 	if (params->duration) {
799 		signal(SIGALRM, stop_hist);
800 		alarm(params->duration);
801 	}
802 }
803 
timerlat_hist_main(int argc,char * argv[])804 int timerlat_hist_main(int argc, char *argv[])
805 {
806 	struct timerlat_hist_params *params;
807 	struct osnoise_tool *record = NULL;
808 	struct osnoise_tool *tool = NULL;
809 	struct trace_instance *trace;
810 	int dma_latency_fd = -1;
811 	int return_value = 1;
812 	int retval;
813 
814 	params = timerlat_hist_parse_args(argc, argv);
815 	if (!params)
816 		exit(1);
817 
818 	tool = timerlat_init_hist(params);
819 	if (!tool) {
820 		err_msg("Could not init osnoise hist\n");
821 		goto out_exit;
822 	}
823 
824 	retval = timerlat_hist_apply_config(tool, params);
825 	if (retval) {
826 		err_msg("Could not apply config\n");
827 		goto out_free;
828 	}
829 
830 	trace = &tool->trace;
831 
832 	retval = enable_timerlat(trace);
833 	if (retval) {
834 		err_msg("Failed to enable timerlat tracer\n");
835 		goto out_free;
836 	}
837 
838 	if (params->set_sched) {
839 		retval = set_comm_sched_attr("timerlat/", &params->sched_param);
840 		if (retval) {
841 			err_msg("Failed to set sched parameters\n");
842 			goto out_free;
843 		}
844 	}
845 
846 	if (params->dma_latency >= 0) {
847 		dma_latency_fd = set_cpu_dma_latency(params->dma_latency);
848 		if (dma_latency_fd < 0) {
849 			err_msg("Could not set /dev/cpu_dma_latency.\n");
850 			goto out_free;
851 		}
852 	}
853 
854 	trace_instance_start(trace);
855 
856 	if (params->trace_output) {
857 		record = osnoise_init_trace_tool("timerlat");
858 		if (!record) {
859 			err_msg("Failed to enable the trace instance\n");
860 			goto out_free;
861 		}
862 
863 		if (params->events) {
864 			retval = trace_events_enable(&record->trace, params->events);
865 			if (retval)
866 				goto out_hist;
867 		}
868 
869 		trace_instance_start(&record->trace);
870 	}
871 
872 	tool->start_time = time(NULL);
873 	timerlat_hist_set_signals(params);
874 
875 	while (!stop_tracing) {
876 		sleep(params->sleep_time);
877 
878 		retval = tracefs_iterate_raw_events(trace->tep,
879 						    trace->inst,
880 						    NULL,
881 						    0,
882 						    collect_registered_events,
883 						    trace);
884 		if (retval < 0) {
885 			err_msg("Error iterating on events\n");
886 			goto out_hist;
887 		}
888 
889 		if (trace_is_off(&tool->trace, &record->trace))
890 			break;
891 	}
892 
893 	timerlat_print_stats(params, tool);
894 
895 	return_value = 0;
896 
897 	if (trace_is_off(&tool->trace, &record->trace)) {
898 		printf("rtla timerlat hit stop tracing\n");
899 		if (params->trace_output) {
900 			printf("  Saving trace to %s\n", params->trace_output);
901 			save_trace_to_file(record->trace.inst, params->trace_output);
902 		}
903 	}
904 
905 out_hist:
906 	if (dma_latency_fd >= 0)
907 		close(dma_latency_fd);
908 	trace_events_destroy(&record->trace, params->events);
909 	params->events = NULL;
910 out_free:
911 	timerlat_free_histogram(tool->data);
912 	osnoise_destroy_tool(record);
913 	osnoise_destroy_tool(tool);
914 	free(params);
915 out_exit:
916 	exit(return_value);
917 }
918