• 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 "osnoise.h"
15 #include "utils.h"
16 
17 /*
18  * osnoise top parameters
19  */
20 struct osnoise_top_params {
21 	char			*cpus;
22 	char			*monitored_cpus;
23 	char			*trace_output;
24 	unsigned long long	runtime;
25 	unsigned long long	period;
26 	long long		threshold;
27 	long long		stop_us;
28 	long long		stop_total_us;
29 	int			sleep_time;
30 	int			duration;
31 	int			quiet;
32 	int			set_sched;
33 	struct sched_attr	sched_param;
34 	struct trace_events	*events;
35 };
36 
37 struct osnoise_top_cpu {
38 	unsigned long long	sum_runtime;
39 	unsigned long long	sum_noise;
40 	unsigned long long	max_noise;
41 	unsigned long long	max_sample;
42 
43 	unsigned long long	hw_count;
44 	unsigned long long	nmi_count;
45 	unsigned long long	irq_count;
46 	unsigned long long	softirq_count;
47 	unsigned long long	thread_count;
48 
49 	int			sum_cycles;
50 };
51 
52 struct osnoise_top_data {
53 	struct osnoise_top_cpu	*cpu_data;
54 	int			nr_cpus;
55 };
56 
57 /*
58  * osnoise_free_top - free runtime data
59  */
60 static void
osnoise_free_top(struct osnoise_top_data * data)61 osnoise_free_top(struct osnoise_top_data *data)
62 {
63 	free(data->cpu_data);
64 	free(data);
65 }
66 
67 /*
68  * osnoise_alloc_histogram - alloc runtime data
69  */
osnoise_alloc_top(int nr_cpus)70 static struct osnoise_top_data *osnoise_alloc_top(int nr_cpus)
71 {
72 	struct osnoise_top_data *data;
73 
74 	data = calloc(1, sizeof(*data));
75 	if (!data)
76 		return NULL;
77 
78 	data->nr_cpus = nr_cpus;
79 
80 	/* one set of histograms per CPU */
81 	data->cpu_data = calloc(1, sizeof(*data->cpu_data) * nr_cpus);
82 	if (!data->cpu_data)
83 		goto cleanup;
84 
85 	return data;
86 
87 cleanup:
88 	osnoise_free_top(data);
89 	return NULL;
90 }
91 
92 /*
93  * osnoise_top_handler - this is the handler for osnoise tracer events
94  */
95 static int
osnoise_top_handler(struct trace_seq * s,struct tep_record * record,struct tep_event * event,void * context)96 osnoise_top_handler(struct trace_seq *s, struct tep_record *record,
97 		    struct tep_event *event, void *context)
98 {
99 	struct trace_instance *trace = context;
100 	struct osnoise_tool *tool;
101 	unsigned long long val;
102 	struct osnoise_top_cpu *cpu_data;
103 	struct osnoise_top_data *data;
104 	int cpu = record->cpu;
105 
106 	tool = container_of(trace, struct osnoise_tool, trace);
107 
108 	data = tool->data;
109 	cpu_data = &data->cpu_data[cpu];
110 
111 	cpu_data->sum_cycles++;
112 
113 	tep_get_field_val(s, event, "runtime", record, &val, 1);
114 	update_sum(&cpu_data->sum_runtime, &val);
115 
116 	tep_get_field_val(s, event, "noise", record, &val, 1);
117 	update_max(&cpu_data->max_noise, &val);
118 	update_sum(&cpu_data->sum_noise, &val);
119 
120 	tep_get_field_val(s, event, "max_sample", record, &val, 1);
121 	update_max(&cpu_data->max_sample, &val);
122 
123 	tep_get_field_val(s, event, "hw_count", record, &val, 1);
124 	update_sum(&cpu_data->hw_count, &val);
125 
126 	tep_get_field_val(s, event, "nmi_count", record, &val, 1);
127 	update_sum(&cpu_data->nmi_count, &val);
128 
129 	tep_get_field_val(s, event, "irq_count", record, &val, 1);
130 	update_sum(&cpu_data->irq_count, &val);
131 
132 	tep_get_field_val(s, event, "softirq_count", record, &val, 1);
133 	update_sum(&cpu_data->softirq_count, &val);
134 
135 	tep_get_field_val(s, event, "thread_count", record, &val, 1);
136 	update_sum(&cpu_data->thread_count, &val);
137 
138 	return 0;
139 }
140 
141 /*
142  * osnoise_top_header - print the header of the tool output
143  */
osnoise_top_header(struct osnoise_tool * top)144 static void osnoise_top_header(struct osnoise_tool *top)
145 {
146 	struct trace_seq *s = top->trace.seq;
147 	char duration[26];
148 
149 	get_duration(top->start_time, duration, sizeof(duration));
150 
151 	trace_seq_printf(s, "\033[2;37;40m");
152 	trace_seq_printf(s, "                                          Operating System Noise");
153 	trace_seq_printf(s, "                                     ");
154 	trace_seq_printf(s, "                                     ");
155 	trace_seq_printf(s, "\033[0;0;0m");
156 	trace_seq_printf(s, "\n");
157 
158 	trace_seq_printf(s, "duration: %9s | time is in us\n", duration);
159 
160 	trace_seq_printf(s, "\033[2;30;47m");
161 	trace_seq_printf(s, "CPU Period       Runtime ");
162 	trace_seq_printf(s, "       Noise ");
163 	trace_seq_printf(s, " %% CPU Aval ");
164 	trace_seq_printf(s, "  Max Noise   Max Single ");
165 	trace_seq_printf(s, "         HW          NMI          IRQ      Softirq       Thread");
166 	trace_seq_printf(s, "\033[0;0;0m");
167 	trace_seq_printf(s, "\n");
168 }
169 
170 /*
171  * clear_terminal - clears the output terminal
172  */
clear_terminal(struct trace_seq * seq)173 static void clear_terminal(struct trace_seq *seq)
174 {
175 	if (!config_debug)
176 		trace_seq_printf(seq, "\033c");
177 }
178 
179 /*
180  * osnoise_top_print - prints the output of a given CPU
181  */
osnoise_top_print(struct osnoise_tool * tool,int cpu)182 static void osnoise_top_print(struct osnoise_tool *tool, int cpu)
183 {
184 	struct trace_seq *s = tool->trace.seq;
185 	struct osnoise_top_cpu *cpu_data;
186 	struct osnoise_top_data *data;
187 	int percentage;
188 	int decimal;
189 
190 	data = tool->data;
191 	cpu_data = &data->cpu_data[cpu];
192 
193 	if (!cpu_data->sum_runtime)
194 		return;
195 
196 	percentage = ((cpu_data->sum_runtime - cpu_data->sum_noise) * 10000000)
197 			/ cpu_data->sum_runtime;
198 	decimal = percentage % 100000;
199 	percentage = percentage / 100000;
200 
201 	trace_seq_printf(s, "%3d #%-6d %12llu ", cpu, cpu_data->sum_cycles, cpu_data->sum_runtime);
202 	trace_seq_printf(s, "%12llu ", cpu_data->sum_noise);
203 	trace_seq_printf(s, "  %3d.%05d", percentage, decimal);
204 	trace_seq_printf(s, "%12llu %12llu", cpu_data->max_noise, cpu_data->max_sample);
205 
206 	trace_seq_printf(s, "%12llu ", cpu_data->hw_count);
207 	trace_seq_printf(s, "%12llu ", cpu_data->nmi_count);
208 	trace_seq_printf(s, "%12llu ", cpu_data->irq_count);
209 	trace_seq_printf(s, "%12llu ", cpu_data->softirq_count);
210 	trace_seq_printf(s, "%12llu\n", cpu_data->thread_count);
211 }
212 
213 /*
214  * osnoise_print_stats - print data for all cpus
215  */
216 static void
osnoise_print_stats(struct osnoise_top_params * params,struct osnoise_tool * top)217 osnoise_print_stats(struct osnoise_top_params *params, struct osnoise_tool *top)
218 {
219 	struct trace_instance *trace = &top->trace;
220 	static int nr_cpus = -1;
221 	int i;
222 
223 	if (nr_cpus == -1)
224 		nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
225 
226 	if (!params->quiet)
227 		clear_terminal(trace->seq);
228 
229 	osnoise_top_header(top);
230 
231 	for (i = 0; i < nr_cpus; i++) {
232 		if (params->cpus && !params->monitored_cpus[i])
233 			continue;
234 		osnoise_top_print(top, i);
235 	}
236 
237 	trace_seq_do_printf(trace->seq);
238 	trace_seq_reset(trace->seq);
239 }
240 
241 /*
242  * osnoise_top_usage - prints osnoise top usage message
243  */
osnoise_top_usage(char * usage)244 void osnoise_top_usage(char *usage)
245 {
246 	int i;
247 
248 	static const char * const msg[] = {
249 		"  usage: rtla osnoise [top] [-h] [-q] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",
250 		"	  [-T us] [-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\",
251 		"	  [-c cpu-list] [-P priority]",
252 		"",
253 		"	  -h/--help: print this menu",
254 		"	  -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit",
255 		"	  -p/--period us: osnoise period in us",
256 		"	  -r/--runtime us: osnoise runtime in us",
257 		"	  -s/--stop us: stop trace if a single sample is higher than the argument in us",
258 		"	  -S/--stop-total us: stop trace if the total sample is higher than the argument in us",
259 		"	  -T/--threshold us: the minimum delta to be considered a noise",
260 		"	  -c/--cpus cpu-list: list of cpus to run osnoise threads",
261 		"	  -d/--duration time[s|m|h|d]: duration of the session",
262 		"	  -D/--debug: print debug info",
263 		"	  -t/--trace[=file]: save the stopped trace to [file|osnoise_trace.txt]",
264 		"	  -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
265 		"	     --filter <filter>: enable a trace event filter to the previous -e event",
266 		"	     --trigger <trigger>: enable a trace event trigger to the previous -e event",
267 		"	  -q/--quiet print only a summary at the end",
268 		"	  -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
269 		"		o:prio - use SCHED_OTHER with prio",
270 		"		r:prio - use SCHED_RR with prio",
271 		"		f:prio - use SCHED_FIFO with prio",
272 		"		d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
273 		"						       in nanoseconds",
274 		NULL,
275 	};
276 
277 	if (usage)
278 		fprintf(stderr, "%s\n", usage);
279 
280 	fprintf(stderr, "rtla osnoise top: a per-cpu summary of the OS noise (version %s)\n",
281 			VERSION);
282 
283 	for (i = 0; msg[i]; i++)
284 		fprintf(stderr, "%s\n", msg[i]);
285 
286 	if (usage)
287 		exit(EXIT_FAILURE);
288 
289 	exit(EXIT_SUCCESS);
290 }
291 
292 /*
293  * osnoise_top_parse_args - allocs, parse and fill the cmd line parameters
294  */
osnoise_top_parse_args(int argc,char ** argv)295 struct osnoise_top_params *osnoise_top_parse_args(int argc, char **argv)
296 {
297 	struct osnoise_top_params *params;
298 	struct trace_events *tevent;
299 	int retval;
300 	int c;
301 
302 	params = calloc(1, sizeof(*params));
303 	if (!params)
304 		exit(1);
305 
306 	while (1) {
307 		static struct option long_options[] = {
308 			{"auto",		required_argument,	0, 'a'},
309 			{"cpus",		required_argument,	0, 'c'},
310 			{"debug",		no_argument,		0, 'D'},
311 			{"duration",		required_argument,	0, 'd'},
312 			{"event",		required_argument,	0, 'e'},
313 			{"help",		no_argument,		0, 'h'},
314 			{"period",		required_argument,	0, 'p'},
315 			{"priority",		required_argument,	0, 'P'},
316 			{"quiet",		no_argument,		0, 'q'},
317 			{"runtime",		required_argument,	0, 'r'},
318 			{"stop",		required_argument,	0, 's'},
319 			{"stop-total",		required_argument,	0, 'S'},
320 			{"threshold",		required_argument,	0, 'T'},
321 			{"trace",		optional_argument,	0, 't'},
322 			{"trigger",		required_argument,	0, '0'},
323 			{"filter",		required_argument,	0, '1'},
324 			{0, 0, 0, 0}
325 		};
326 
327 		/* getopt_long stores the option index here. */
328 		int option_index = 0;
329 
330 		c = getopt_long(argc, argv, "a:c:d:De:hp:P:qr:s:S:t::T:0:1:",
331 				 long_options, &option_index);
332 
333 		/* Detect the end of the options. */
334 		if (c == -1)
335 			break;
336 
337 		switch (c) {
338 		case 'a':
339 			/* set sample stop to auto_thresh */
340 			params->stop_us = get_llong_from_str(optarg);
341 
342 			/* set sample threshold to 1 */
343 			params->threshold = 1;
344 
345 			/* set trace */
346 			params->trace_output = "osnoise_trace.txt";
347 
348 			break;
349 		case 'c':
350 			retval = parse_cpu_list(optarg, &params->monitored_cpus);
351 			if (retval)
352 				osnoise_top_usage("\nInvalid -c cpu list\n");
353 			params->cpus = optarg;
354 			break;
355 		case 'D':
356 			config_debug = 1;
357 			break;
358 		case 'd':
359 			params->duration = parse_seconds_duration(optarg);
360 			if (!params->duration)
361 				osnoise_top_usage("Invalid -D duration\n");
362 			break;
363 		case 'e':
364 			tevent = trace_event_alloc(optarg);
365 			if (!tevent) {
366 				err_msg("Error alloc trace event");
367 				exit(EXIT_FAILURE);
368 			}
369 
370 			if (params->events)
371 				tevent->next = params->events;
372 			params->events = tevent;
373 
374 			break;
375 		case 'h':
376 		case '?':
377 			osnoise_top_usage(NULL);
378 			break;
379 		case 'p':
380 			params->period = get_llong_from_str(optarg);
381 			if (params->period > 10000000)
382 				osnoise_top_usage("Period longer than 10 s\n");
383 			break;
384 		case 'P':
385 			retval = parse_prio(optarg, &params->sched_param);
386 			if (retval == -1)
387 				osnoise_top_usage("Invalid -P priority");
388 			params->set_sched = 1;
389 			break;
390 		case 'q':
391 			params->quiet = 1;
392 			break;
393 		case 'r':
394 			params->runtime = get_llong_from_str(optarg);
395 			if (params->runtime < 100)
396 				osnoise_top_usage("Runtime shorter than 100 us\n");
397 			break;
398 		case 's':
399 			params->stop_us = get_llong_from_str(optarg);
400 			break;
401 		case 'S':
402 			params->stop_total_us = get_llong_from_str(optarg);
403 			break;
404 		case 't':
405 			if (optarg)
406 				/* skip = */
407 				params->trace_output = &optarg[1];
408 			else
409 				params->trace_output = "osnoise_trace.txt";
410 			break;
411 		case 'T':
412 			params->threshold = get_llong_from_str(optarg);
413 			break;
414 		case '0': /* trigger */
415 			if (params->events) {
416 				retval = trace_event_add_trigger(params->events, optarg);
417 				if (retval) {
418 					err_msg("Error adding trigger %s\n", optarg);
419 					exit(EXIT_FAILURE);
420 				}
421 			} else {
422 				osnoise_top_usage("--trigger requires a previous -e\n");
423 			}
424 			break;
425 		case '1': /* filter */
426 			if (params->events) {
427 				retval = trace_event_add_filter(params->events, optarg);
428 				if (retval) {
429 					err_msg("Error adding filter %s\n", optarg);
430 					exit(EXIT_FAILURE);
431 				}
432 			} else {
433 				osnoise_top_usage("--filter requires a previous -e\n");
434 			}
435 			break;
436 		default:
437 			osnoise_top_usage("Invalid option");
438 		}
439 	}
440 
441 	if (geteuid()) {
442 		err_msg("osnoise needs root permission\n");
443 		exit(EXIT_FAILURE);
444 	}
445 
446 	return params;
447 }
448 
449 /*
450  * osnoise_top_apply_config - apply the top configs to the initialized tool
451  */
452 static int
osnoise_top_apply_config(struct osnoise_tool * tool,struct osnoise_top_params * params)453 osnoise_top_apply_config(struct osnoise_tool *tool, struct osnoise_top_params *params)
454 {
455 	int retval;
456 
457 	if (!params->sleep_time)
458 		params->sleep_time = 1;
459 
460 	if (params->cpus) {
461 		retval = osnoise_set_cpus(tool->context, params->cpus);
462 		if (retval) {
463 			err_msg("Failed to apply CPUs config\n");
464 			goto out_err;
465 		}
466 	}
467 
468 	if (params->runtime || params->period) {
469 		retval = osnoise_set_runtime_period(tool->context,
470 						    params->runtime,
471 						    params->period);
472 		if (retval) {
473 			err_msg("Failed to set runtime and/or period\n");
474 			goto out_err;
475 		}
476 	}
477 
478 	if (params->stop_us) {
479 		retval = osnoise_set_stop_us(tool->context, params->stop_us);
480 		if (retval) {
481 			err_msg("Failed to set stop us\n");
482 			goto out_err;
483 		}
484 	}
485 
486 	if (params->stop_total_us) {
487 		retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us);
488 		if (retval) {
489 			err_msg("Failed to set stop total us\n");
490 			goto out_err;
491 		}
492 	}
493 
494 	if (params->threshold) {
495 		retval = osnoise_set_tracing_thresh(tool->context, params->threshold);
496 		if (retval) {
497 			err_msg("Failed to set tracing_thresh\n");
498 			goto out_err;
499 		}
500 	}
501 
502 	return 0;
503 
504 out_err:
505 	return -1;
506 }
507 
508 /*
509  * osnoise_init_top - initialize a osnoise top tool with parameters
510  */
osnoise_init_top(struct osnoise_top_params * params)511 struct osnoise_tool *osnoise_init_top(struct osnoise_top_params *params)
512 {
513 	struct osnoise_tool *tool;
514 	int nr_cpus;
515 
516 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
517 
518 	tool = osnoise_init_tool("osnoise_top");
519 	if (!tool)
520 		return NULL;
521 
522 	tool->data = osnoise_alloc_top(nr_cpus);
523 	if (!tool->data)
524 		goto out_err;
525 
526 	tool->params = params;
527 
528 	tep_register_event_handler(tool->trace.tep, -1, "ftrace", "osnoise",
529 				   osnoise_top_handler, NULL);
530 
531 	return tool;
532 
533 out_err:
534 	osnoise_free_top(tool->data);
535 	osnoise_destroy_tool(tool);
536 	return NULL;
537 }
538 
539 static int stop_tracing;
stop_top(int sig)540 static void stop_top(int sig)
541 {
542 	stop_tracing = 1;
543 }
544 
545 /*
546  * osnoise_top_set_signals - handles the signal to stop the tool
547  */
osnoise_top_set_signals(struct osnoise_top_params * params)548 static void osnoise_top_set_signals(struct osnoise_top_params *params)
549 {
550 	signal(SIGINT, stop_top);
551 	if (params->duration) {
552 		signal(SIGALRM, stop_top);
553 		alarm(params->duration);
554 	}
555 }
556 
osnoise_top_main(int argc,char ** argv)557 int osnoise_top_main(int argc, char **argv)
558 {
559 	struct osnoise_top_params *params;
560 	struct osnoise_tool *record = NULL;
561 	struct osnoise_tool *tool = NULL;
562 	struct trace_instance *trace;
563 	int return_value = 1;
564 	int retval;
565 
566 	params = osnoise_top_parse_args(argc, argv);
567 	if (!params)
568 		exit(1);
569 
570 	tool = osnoise_init_top(params);
571 	if (!tool) {
572 		err_msg("Could not init osnoise top\n");
573 		goto out_exit;
574 	}
575 
576 	retval = osnoise_top_apply_config(tool, params);
577 	if (retval) {
578 		err_msg("Could not apply config\n");
579 		goto out_free;
580 	}
581 
582 	trace = &tool->trace;
583 
584 	retval = enable_osnoise(trace);
585 	if (retval) {
586 		err_msg("Failed to enable osnoise tracer\n");
587 		goto out_free;
588 	}
589 
590 	if (params->set_sched) {
591 		retval = set_comm_sched_attr("osnoise/", &params->sched_param);
592 		if (retval) {
593 			err_msg("Failed to set sched parameters\n");
594 			goto out_free;
595 		}
596 	}
597 
598 	trace_instance_start(trace);
599 
600 	if (params->trace_output) {
601 		record = osnoise_init_trace_tool("osnoise");
602 		if (!record) {
603 			err_msg("Failed to enable the trace instance\n");
604 			goto out_free;
605 		}
606 
607 		if (params->events) {
608 			retval = trace_events_enable(&record->trace, params->events);
609 			if (retval)
610 				goto out_top;
611 		}
612 
613 		trace_instance_start(&record->trace);
614 	}
615 
616 	tool->start_time = time(NULL);
617 	osnoise_top_set_signals(params);
618 
619 	while (!stop_tracing) {
620 		sleep(params->sleep_time);
621 
622 		retval = tracefs_iterate_raw_events(trace->tep,
623 						    trace->inst,
624 						    NULL,
625 						    0,
626 						    collect_registered_events,
627 						    trace);
628 		if (retval < 0) {
629 			err_msg("Error iterating on events\n");
630 			goto out_top;
631 		}
632 
633 		if (!params->quiet)
634 			osnoise_print_stats(params, tool);
635 
636 		if (trace_is_off(&tool->trace, &record->trace))
637 			break;
638 
639 	}
640 
641 	osnoise_print_stats(params, tool);
642 
643 	return_value = 0;
644 
645 	if (trace_is_off(&tool->trace, &record->trace)) {
646 		printf("osnoise hit stop tracing\n");
647 		if (params->trace_output) {
648 			printf("  Saving trace to %s\n", params->trace_output);
649 			save_trace_to_file(record->trace.inst, params->trace_output);
650 		}
651 	}
652 
653 out_top:
654 	trace_events_destroy(&record->trace, params->events);
655 	params->events = NULL;
656 out_free:
657 	osnoise_free_top(tool->data);
658 	osnoise_destroy_tool(record);
659 	osnoise_destroy_tool(tool);
660 	free(params);
661 out_exit:
662 	exit(return_value);
663 }
664