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_top_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			quiet;
31 	int			set_sched;
32 	struct sched_attr	sched_param;
33 };
34 
35 struct timerlat_top_cpu {
36 	int			irq_count;
37 	int			thread_count;
38 
39 	unsigned long long	cur_irq;
40 	unsigned long long	min_irq;
41 	unsigned long long	sum_irq;
42 	unsigned long long	max_irq;
43 
44 	unsigned long long	cur_thread;
45 	unsigned long long	min_thread;
46 	unsigned long long	sum_thread;
47 	unsigned long long	max_thread;
48 };
49 
50 struct timerlat_top_data {
51 	struct timerlat_top_cpu	*cpu_data;
52 	int			nr_cpus;
53 };
54 
55 /*
56  * timerlat_free_top - free runtime data
57  */
58 static void
59 timerlat_free_top(struct timerlat_top_data *data)
60 {
61 	free(data->cpu_data);
62 	free(data);
63 }
64 
65 /*
66  * timerlat_alloc_histogram - alloc runtime data
67  */
68 static struct timerlat_top_data *timerlat_alloc_top(int nr_cpus)
69 {
70 	struct timerlat_top_data *data;
71 	int cpu;
72 
73 	data = calloc(1, sizeof(*data));
74 	if (!data)
75 		return NULL;
76 
77 	data->nr_cpus = nr_cpus;
78 
79 	/* one set of histograms per CPU */
80 	data->cpu_data = calloc(1, sizeof(*data->cpu_data) * nr_cpus);
81 	if (!data->cpu_data)
82 		goto cleanup;
83 
84 	/* set the min to max */
85 	for (cpu = 0; cpu < nr_cpus; cpu++) {
86 		data->cpu_data[cpu].min_irq = ~0;
87 		data->cpu_data[cpu].min_thread = ~0;
88 	}
89 
90 	return data;
91 
92 cleanup:
93 	timerlat_free_top(data);
94 	return NULL;
95 }
96 
97 /*
98  * timerlat_hist_update - record a new timerlat occurent on cpu, updating data
99  */
100 static void
101 timerlat_top_update(struct osnoise_tool *tool, int cpu,
102 		    unsigned long long thread,
103 		    unsigned long long latency)
104 {
105 	struct timerlat_top_data *data = tool->data;
106 	struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
107 
108 	if (!thread) {
109 		cpu_data->irq_count++;
110 		cpu_data->cur_irq = latency;
111 		update_min(&cpu_data->min_irq, &latency);
112 		update_sum(&cpu_data->sum_irq, &latency);
113 		update_max(&cpu_data->max_irq, &latency);
114 	} else {
115 		cpu_data->thread_count++;
116 		cpu_data->cur_thread = latency;
117 		update_min(&cpu_data->min_thread, &latency);
118 		update_sum(&cpu_data->sum_thread, &latency);
119 		update_max(&cpu_data->max_thread, &latency);
120 	}
121 }
122 
123 /*
124  * timerlat_top_handler - this is the handler for timerlat tracer events
125  */
126 static int
127 timerlat_top_handler(struct trace_seq *s, struct tep_record *record,
128 		     struct tep_event *event, void *context)
129 {
130 	struct trace_instance *trace = context;
131 	unsigned long long latency, thread;
132 	struct osnoise_tool *top;
133 	int cpu = record->cpu;
134 
135 	top = container_of(trace, struct osnoise_tool, trace);
136 
137 	tep_get_field_val(s, event, "context", record, &thread, 1);
138 	tep_get_field_val(s, event, "timer_latency", record, &latency, 1);
139 
140 	timerlat_top_update(top, cpu, thread, latency);
141 
142 	return 0;
143 }
144 
145 /*
146  * timerlat_top_header - print the header of the tool output
147  */
148 static void timerlat_top_header(struct osnoise_tool *top)
149 {
150 	struct timerlat_top_params *params = top->params;
151 	struct trace_seq *s = top->trace.seq;
152 	char duration[26];
153 
154 	get_duration(top->start_time, duration, sizeof(duration));
155 
156 	trace_seq_printf(s, "\033[2;37;40m");
157 	trace_seq_printf(s, "                                     Timer Latency                                              ");
158 	trace_seq_printf(s, "\033[0;0;0m");
159 	trace_seq_printf(s, "\n");
160 
161 	trace_seq_printf(s, "%-6s   |          IRQ Timer Latency (%s)        |         Thread Timer Latency (%s)\n", duration,
162 			params->output_divisor == 1 ? "ns" : "us",
163 			params->output_divisor == 1 ? "ns" : "us");
164 
165 	trace_seq_printf(s, "\033[2;30;47m");
166 	trace_seq_printf(s, "CPU COUNT      |      cur       min       avg       max |      cur       min       avg       max");
167 	trace_seq_printf(s, "\033[0;0;0m");
168 	trace_seq_printf(s, "\n");
169 }
170 
171 /*
172  * timerlat_top_print - prints the output of a given CPU
173  */
174 static void timerlat_top_print(struct osnoise_tool *top, int cpu)
175 {
176 
177 	struct timerlat_top_params *params = top->params;
178 	struct timerlat_top_data *data = top->data;
179 	struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
180 	int divisor = params->output_divisor;
181 	struct trace_seq *s = top->trace.seq;
182 
183 	if (divisor == 0)
184 		return;
185 
186 	/*
187 	 * Skip if no data is available: is this cpu offline?
188 	 */
189 	if (!cpu_data->irq_count && !cpu_data->thread_count)
190 		return;
191 
192 	/*
193 	 * Unless trace is being lost, IRQ counter is always the max.
194 	 */
195 	trace_seq_printf(s, "%3d #%-9d |", cpu, cpu_data->irq_count);
196 
197 	if (!cpu_data->irq_count) {
198 		trace_seq_printf(s, "        - ");
199 		trace_seq_printf(s, "        - ");
200 		trace_seq_printf(s, "        - ");
201 		trace_seq_printf(s, "        - |");
202 	} else {
203 		trace_seq_printf(s, "%9llu ", cpu_data->cur_irq / params->output_divisor);
204 		trace_seq_printf(s, "%9llu ", cpu_data->min_irq / params->output_divisor);
205 		trace_seq_printf(s, "%9llu ", (cpu_data->sum_irq / cpu_data->irq_count) / divisor);
206 		trace_seq_printf(s, "%9llu |", cpu_data->max_irq / divisor);
207 	}
208 
209 	if (!cpu_data->thread_count) {
210 		trace_seq_printf(s, "        - ");
211 		trace_seq_printf(s, "        - ");
212 		trace_seq_printf(s, "        - ");
213 		trace_seq_printf(s, "        -\n");
214 	} else {
215 		trace_seq_printf(s, "%9llu ", cpu_data->cur_thread / divisor);
216 		trace_seq_printf(s, "%9llu ", cpu_data->min_thread / divisor);
217 		trace_seq_printf(s, "%9llu ",
218 				(cpu_data->sum_thread / cpu_data->thread_count) / divisor);
219 		trace_seq_printf(s, "%9llu\n", cpu_data->max_thread / divisor);
220 	}
221 }
222 
223 /*
224  * clear_terminal - clears the output terminal
225  */
226 static void clear_terminal(struct trace_seq *seq)
227 {
228 	if (!config_debug)
229 		trace_seq_printf(seq, "\033c");
230 }
231 
232 /*
233  * timerlat_print_stats - print data for all cpus
234  */
235 static void
236 timerlat_print_stats(struct timerlat_top_params *params, struct osnoise_tool *top)
237 {
238 	struct trace_instance *trace = &top->trace;
239 	static int nr_cpus = -1;
240 	int i;
241 
242 	if (nr_cpus == -1)
243 		nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
244 
245 	if (!params->quiet)
246 		clear_terminal(trace->seq);
247 
248 	timerlat_top_header(top);
249 
250 	for (i = 0; i < nr_cpus; i++) {
251 		if (params->cpus && !params->monitored_cpus[i])
252 			continue;
253 		timerlat_top_print(top, i);
254 	}
255 
256 	trace_seq_do_printf(trace->seq);
257 	trace_seq_reset(trace->seq);
258 }
259 
260 /*
261  * timerlat_top_usage - prints timerlat top usage message
262  */
263 static void timerlat_top_usage(char *usage)
264 {
265 	int i;
266 
267 	static const char *const msg[] = {
268 		"",
269 		"  usage: rtla timerlat [top] [-h] [-q] [-d s] [-D] [-n] [-p us] [-i us] [-T us] [-s us] [-t[=file]] \\",
270 		"	  [-c cpu-list] [-P priority]",
271 		"",
272 		"	  -h/--help: print this menu",
273 		"	  -p/--period us: timerlat period in us",
274 		"	  -i/--irq us: stop trace if the irq latency is higher than the argument in us",
275 		"	  -T/--thread us: stop trace if the thread latency is higher than the argument in us",
276 		"	  -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",
277 		"	  -c/--cpus cpus: run the tracer only on the given cpus",
278 		"	  -d/--duration time[m|h|d]: duration of the session in seconds",
279 		"	  -D/--debug: print debug info",
280 		"	  -t/--trace[=file]: save the stopped trace to [file|timerlat_trace.txt]",
281 		"	  -n/--nano: display data in nanoseconds",
282 		"	  -q/--quiet print only a summary at the end",
283 		"	  -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
284 		"		o:prio - use SCHED_OTHER with prio",
285 		"		r:prio - use SCHED_RR with prio",
286 		"		f:prio - use SCHED_FIFO with prio",
287 		"		d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
288 		"						       in nanoseconds",
289 		NULL,
290 	};
291 
292 	if (usage)
293 		fprintf(stderr, "%s\n", usage);
294 
295 	fprintf(stderr, "rtla timerlat top: a per-cpu summary of the timer latency (version %s)\n",
296 			VERSION);
297 
298 	for (i = 0; msg[i]; i++)
299 		fprintf(stderr, "%s\n", msg[i]);
300 	exit(1);
301 }
302 
303 /*
304  * timerlat_top_parse_args - allocs, parse and fill the cmd line parameters
305  */
306 static struct timerlat_top_params
307 *timerlat_top_parse_args(int argc, char **argv)
308 {
309 	struct timerlat_top_params *params;
310 	int retval;
311 	int c;
312 
313 	params = calloc(1, sizeof(*params));
314 	if (!params)
315 		exit(1);
316 
317 	/* display data in microseconds */
318 	params->output_divisor = 1000;
319 
320 	while (1) {
321 		static struct option long_options[] = {
322 			{"cpus",		required_argument,	0, 'c'},
323 			{"debug",		no_argument,		0, 'D'},
324 			{"duration",		required_argument,	0, 'd'},
325 			{"help",		no_argument,		0, 'h'},
326 			{"irq",			required_argument,	0, 'i'},
327 			{"nano",		no_argument,		0, 'n'},
328 			{"period",		required_argument,	0, 'p'},
329 			{"priority",		required_argument,	0, 'P'},
330 			{"quiet",		no_argument,		0, 'q'},
331 			{"stack",		required_argument,	0, 's'},
332 			{"thread",		required_argument,	0, 'T'},
333 			{"trace",		optional_argument,	0, 't'},
334 			{0, 0, 0, 0}
335 		};
336 
337 		/* getopt_long stores the option index here. */
338 		int option_index = 0;
339 
340 		c = getopt_long(argc, argv, "c:d:Dhi:np:P:qs:t::T:",
341 				 long_options, &option_index);
342 
343 		/* detect the end of the options. */
344 		if (c == -1)
345 			break;
346 
347 		switch (c) {
348 		case 'c':
349 			retval = parse_cpu_list(optarg, &params->monitored_cpus);
350 			if (retval)
351 				timerlat_top_usage("\nInvalid -c cpu list\n");
352 			params->cpus = optarg;
353 			break;
354 		case 'D':
355 			config_debug = 1;
356 			break;
357 		case 'd':
358 			params->duration = parse_seconds_duration(optarg);
359 			if (!params->duration)
360 				timerlat_top_usage("Invalid -D duration\n");
361 			break;
362 		case 'h':
363 		case '?':
364 			timerlat_top_usage(NULL);
365 			break;
366 		case 'i':
367 			params->stop_us = get_llong_from_str(optarg);
368 			break;
369 		case 'n':
370 			params->output_divisor = 1;
371 			break;
372 		case 'p':
373 			params->timerlat_period_us = get_llong_from_str(optarg);
374 			if (params->timerlat_period_us > 1000000)
375 				timerlat_top_usage("Period longer than 1 s\n");
376 			break;
377 		case 'P':
378 			retval = parse_prio(optarg, &params->sched_param);
379 			if (retval == -1)
380 				timerlat_top_usage("Invalid -P priority");
381 			params->set_sched = 1;
382 			break;
383 		case 'q':
384 			params->quiet = 1;
385 			break;
386 		case 's':
387 			params->print_stack = get_llong_from_str(optarg);
388 			break;
389 		case 'T':
390 			params->stop_total_us = get_llong_from_str(optarg);
391 			break;
392 		case 't':
393 			if (optarg)
394 				/* skip = */
395 				params->trace_output = &optarg[1];
396 			else
397 				params->trace_output = "timerlat_trace.txt";
398 			break;
399 		default:
400 			timerlat_top_usage("Invalid option");
401 		}
402 	}
403 
404 	if (geteuid()) {
405 		err_msg("rtla needs root permission\n");
406 		exit(EXIT_FAILURE);
407 	}
408 
409 	return params;
410 }
411 
412 /*
413  * timerlat_top_apply_config - apply the top configs to the initialized tool
414  */
415 static int
416 timerlat_top_apply_config(struct osnoise_tool *top, struct timerlat_top_params *params)
417 {
418 	int retval;
419 
420 	if (!params->sleep_time)
421 		params->sleep_time = 1;
422 
423 	if (params->cpus) {
424 		retval = osnoise_set_cpus(top->context, params->cpus);
425 		if (retval) {
426 			err_msg("Failed to apply CPUs config\n");
427 			goto out_err;
428 		}
429 	}
430 
431 	if (params->stop_us) {
432 		retval = osnoise_set_stop_us(top->context, params->stop_us);
433 		if (retval) {
434 			err_msg("Failed to set stop us\n");
435 			goto out_err;
436 		}
437 	}
438 
439 	if (params->stop_total_us) {
440 		retval = osnoise_set_stop_total_us(top->context, params->stop_total_us);
441 		if (retval) {
442 			err_msg("Failed to set stop total us\n");
443 			goto out_err;
444 		}
445 	}
446 
447 
448 	if (params->timerlat_period_us) {
449 		retval = osnoise_set_timerlat_period_us(top->context, params->timerlat_period_us);
450 		if (retval) {
451 			err_msg("Failed to set timerlat period\n");
452 			goto out_err;
453 		}
454 	}
455 
456 
457 	if (params->print_stack) {
458 		retval = osnoise_set_print_stack(top->context, params->print_stack);
459 		if (retval) {
460 			err_msg("Failed to set print stack\n");
461 			goto out_err;
462 		}
463 	}
464 
465 	return 0;
466 
467 out_err:
468 	return -1;
469 }
470 
471 /*
472  * timerlat_init_top - initialize a timerlat top tool with parameters
473  */
474 static struct osnoise_tool
475 *timerlat_init_top(struct timerlat_top_params *params)
476 {
477 	struct osnoise_tool *top;
478 	int nr_cpus;
479 
480 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
481 
482 	top = osnoise_init_tool("timerlat_top");
483 	if (!top)
484 		return NULL;
485 
486 	top->data = timerlat_alloc_top(nr_cpus);
487 	if (!top->data)
488 		goto out_err;
489 
490 	top->params = params;
491 
492 	tep_register_event_handler(top->trace.tep, -1, "ftrace", "timerlat",
493 				   timerlat_top_handler, top);
494 
495 	return top;
496 
497 out_err:
498 	osnoise_destroy_tool(top);
499 	return NULL;
500 }
501 
502 static int stop_tracing;
503 static void stop_top(int sig)
504 {
505 	stop_tracing = 1;
506 }
507 
508 /*
509  * timerlat_top_set_signals - handles the signal to stop the tool
510  */
511 static void
512 timerlat_top_set_signals(struct timerlat_top_params *params)
513 {
514 	signal(SIGINT, stop_top);
515 	if (params->duration) {
516 		signal(SIGALRM, stop_top);
517 		alarm(params->duration);
518 	}
519 }
520 
521 int timerlat_top_main(int argc, char *argv[])
522 {
523 	struct timerlat_top_params *params;
524 	struct trace_instance *trace;
525 	struct osnoise_tool *record;
526 	struct osnoise_tool *top;
527 	int return_value = 1;
528 	int retval;
529 
530 	params = timerlat_top_parse_args(argc, argv);
531 	if (!params)
532 		exit(1);
533 
534 	top = timerlat_init_top(params);
535 	if (!top) {
536 		err_msg("Could not init osnoise top\n");
537 		goto out_exit;
538 	}
539 
540 	retval = timerlat_top_apply_config(top, params);
541 	if (retval) {
542 		err_msg("Could not apply config\n");
543 		goto out_top;
544 	}
545 
546 	trace = &top->trace;
547 
548 	retval = enable_timerlat(trace);
549 	if (retval) {
550 		err_msg("Failed to enable timerlat tracer\n");
551 		goto out_top;
552 	}
553 
554 	if (params->set_sched) {
555 		retval = set_comm_sched_attr("timerlat/", &params->sched_param);
556 		if (retval) {
557 			err_msg("Failed to set sched parameters\n");
558 			goto out_top;
559 		}
560 	}
561 
562 	trace_instance_start(trace);
563 
564 	if (params->trace_output) {
565 		record = osnoise_init_trace_tool("timerlat");
566 		if (!record) {
567 			err_msg("Failed to enable the trace instance\n");
568 			goto out_top;
569 		}
570 		trace_instance_start(&record->trace);
571 	}
572 
573 	top->start_time = time(NULL);
574 	timerlat_top_set_signals(params);
575 
576 	while (!stop_tracing) {
577 		sleep(params->sleep_time);
578 
579 		retval = tracefs_iterate_raw_events(trace->tep,
580 						    trace->inst,
581 						    NULL,
582 						    0,
583 						    collect_registered_events,
584 						    trace);
585 		if (retval < 0) {
586 			err_msg("Error iterating on events\n");
587 			goto out_top;
588 		}
589 
590 		if (!params->quiet)
591 			timerlat_print_stats(params, top);
592 
593 		if (!tracefs_trace_is_on(trace->inst))
594 			break;
595 
596 	};
597 
598 	timerlat_print_stats(params, top);
599 
600 	return_value = 0;
601 
602 	if (!tracefs_trace_is_on(trace->inst)) {
603 		printf("rtla timelat hit stop tracing\n");
604 		if (params->trace_output) {
605 			printf("  Saving trace to %s\n", params->trace_output);
606 			save_trace_to_file(record->trace.inst, params->trace_output);
607 		}
608 	}
609 
610 out_top:
611 	timerlat_free_top(top->data);
612 	osnoise_destroy_tool(top);
613 	if (params->trace_output)
614 		osnoise_destroy_tool(record);
615 	free(params);
616 out_exit:
617 	exit(return_value);
618 }
619