xref: /linux/tools/tracing/rtla/src/osnoise_hist.c (revision caf3fc0fdd63b4b163ee4eac1ca822772108c0f8)
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 
15 #include "osnoise.h"
16 
17 struct osnoise_hist_cpu {
18 	int			*samples;
19 	int			count;
20 
21 	unsigned long long	min_sample;
22 	unsigned long long	sum_sample;
23 	unsigned long long	max_sample;
24 
25 };
26 
27 struct osnoise_hist_data {
28 	struct tracefs_hist	*trace_hist;
29 	struct osnoise_hist_cpu	*hist;
30 	int			entries;
31 	int			bucket_size;
32 };
33 
34 /*
35  * osnoise_free_histogram - free runtime data
36  */
37 static void
38 osnoise_free_histogram(struct osnoise_hist_data *data)
39 {
40 	int cpu;
41 
42 	/* one histogram for IRQ and one for thread, per CPU */
43 	for (cpu = 0; cpu < nr_cpus; cpu++) {
44 		if (data->hist[cpu].samples)
45 			free(data->hist[cpu].samples);
46 	}
47 
48 	/* one set of histograms per CPU */
49 	if (data->hist)
50 		free(data->hist);
51 
52 	free(data);
53 }
54 
55 static void osnoise_free_hist_tool(struct osnoise_tool *tool)
56 {
57 	osnoise_free_histogram(tool->data);
58 }
59 
60 /*
61  * osnoise_alloc_histogram - alloc runtime data
62  */
63 static struct osnoise_hist_data
64 *osnoise_alloc_histogram(int entries, int bucket_size)
65 {
66 	struct osnoise_hist_data *data;
67 	int cpu;
68 
69 	data = calloc(1, sizeof(*data));
70 	if (!data)
71 		return NULL;
72 
73 	data->entries = entries;
74 	data->bucket_size = bucket_size;
75 
76 	data->hist = calloc(1, sizeof(*data->hist) * nr_cpus);
77 	if (!data->hist)
78 		goto cleanup;
79 
80 	for (cpu = 0; cpu < nr_cpus; cpu++) {
81 		data->hist[cpu].samples = calloc(1, sizeof(*data->hist->samples) * (entries + 1));
82 		if (!data->hist[cpu].samples)
83 			goto cleanup;
84 	}
85 
86 	/* set the min to max */
87 	for (cpu = 0; cpu < nr_cpus; cpu++)
88 		data->hist[cpu].min_sample = ~0;
89 
90 	return data;
91 
92 cleanup:
93 	osnoise_free_histogram(data);
94 	return NULL;
95 }
96 
97 static void osnoise_hist_update_multiple(struct osnoise_tool *tool, int cpu,
98 					 unsigned long long duration, int count)
99 {
100 	struct osnoise_params *params = to_osnoise_params(tool->params);
101 	struct osnoise_hist_data *data = tool->data;
102 	unsigned long long total_duration;
103 	int entries = data->entries;
104 	int bucket;
105 	int *hist;
106 
107 	if (params->common.output_divisor)
108 		duration = duration / params->common.output_divisor;
109 
110 	bucket = duration / data->bucket_size;
111 
112 	total_duration = duration * count;
113 
114 	hist = data->hist[cpu].samples;
115 	data->hist[cpu].count += count;
116 	update_min(&data->hist[cpu].min_sample, &duration);
117 	update_sum(&data->hist[cpu].sum_sample, &total_duration);
118 	update_max(&data->hist[cpu].max_sample, &duration);
119 
120 	if (bucket < entries)
121 		hist[bucket] += count;
122 	else
123 		hist[entries] += count;
124 }
125 
126 /*
127  * osnoise_destroy_trace_hist - disable events used to collect histogram
128  */
129 static void osnoise_destroy_trace_hist(struct osnoise_tool *tool)
130 {
131 	struct osnoise_hist_data *data = tool->data;
132 
133 	tracefs_hist_pause(tool->trace.inst, data->trace_hist);
134 	tracefs_hist_destroy(tool->trace.inst, data->trace_hist);
135 }
136 
137 /*
138  * osnoise_init_trace_hist - enable events used to collect histogram
139  */
140 static int osnoise_init_trace_hist(struct osnoise_tool *tool)
141 {
142 	struct osnoise_params *params = to_osnoise_params(tool->params);
143 	struct osnoise_hist_data *data = tool->data;
144 	int bucket_size;
145 	char buff[128];
146 	int retval = 0;
147 
148 	/*
149 	 * Set the size of the bucket.
150 	 */
151 	bucket_size = params->common.output_divisor * params->common.hist.bucket_size;
152 	snprintf(buff, sizeof(buff), "duration.buckets=%d", bucket_size);
153 
154 	data->trace_hist = tracefs_hist_alloc(tool->trace.tep, "osnoise", "sample_threshold",
155 			buff, TRACEFS_HIST_KEY_NORMAL);
156 	if (!data->trace_hist)
157 		return 1;
158 
159 	retval = tracefs_hist_add_key(data->trace_hist, "cpu", 0);
160 	if (retval)
161 		goto out_err;
162 
163 	retval = tracefs_hist_start(tool->trace.inst, data->trace_hist);
164 	if (retval)
165 		goto out_err;
166 
167 	return 0;
168 
169 out_err:
170 	osnoise_destroy_trace_hist(tool);
171 	return 1;
172 }
173 
174 /*
175  * osnoise_read_trace_hist - parse histogram file and file osnoise histogram
176  */
177 static void osnoise_read_trace_hist(struct osnoise_tool *tool)
178 {
179 	struct osnoise_hist_data *data = tool->data;
180 	long long cpu, counter, duration;
181 	char *content, *position;
182 
183 	tracefs_hist_pause(tool->trace.inst, data->trace_hist);
184 
185 	content = tracefs_event_file_read(tool->trace.inst, "osnoise",
186 					  "sample_threshold",
187 					  "hist", NULL);
188 	if (!content)
189 		return;
190 
191 	position = content;
192 	while (true) {
193 		position = strstr(position, "duration: ~");
194 		if (!position)
195 			break;
196 		position += strlen("duration: ~");
197 		duration = get_llong_from_str(position);
198 		if (duration == -1)
199 			err_msg("error reading duration from histogram\n");
200 
201 		position = strstr(position, "cpu:");
202 		if (!position)
203 			break;
204 		position += strlen("cpu: ");
205 		cpu = get_llong_from_str(position);
206 		if (cpu == -1)
207 			err_msg("error reading cpu from histogram\n");
208 
209 		position = strstr(position, "hitcount:");
210 		if (!position)
211 			break;
212 		position += strlen("hitcount: ");
213 		counter = get_llong_from_str(position);
214 		if (counter == -1)
215 			err_msg("error reading counter from histogram\n");
216 
217 		osnoise_hist_update_multiple(tool, cpu, duration, counter);
218 	}
219 	free(content);
220 }
221 
222 /*
223  * osnoise_hist_header - print the header of the tracer to the output
224  */
225 static void osnoise_hist_header(struct osnoise_tool *tool)
226 {
227 	struct osnoise_params *params = to_osnoise_params(tool->params);
228 	struct osnoise_hist_data *data = tool->data;
229 	struct trace_seq *s = tool->trace.seq;
230 	char duration[26];
231 	int cpu;
232 
233 	if (params->common.hist.no_header)
234 		return;
235 
236 	get_duration(tool->start_time, duration, sizeof(duration));
237 	trace_seq_printf(s, "# RTLA osnoise histogram\n");
238 	trace_seq_printf(s, "# Time unit is %s (%s)\n",
239 			params->common.output_divisor == 1 ? "nanoseconds" : "microseconds",
240 			params->common.output_divisor == 1 ? "ns" : "us");
241 
242 	trace_seq_printf(s, "# Duration: %s\n", duration);
243 
244 	if (!params->common.hist.no_index)
245 		trace_seq_printf(s, "Index");
246 
247 	for_each_monitored_cpu(cpu, nr_cpus, &params->common) {
248 
249 		if (!data->hist[cpu].count)
250 			continue;
251 
252 		trace_seq_printf(s, "   CPU-%03d", cpu);
253 	}
254 	trace_seq_printf(s, "\n");
255 
256 	trace_seq_do_printf(s);
257 	trace_seq_reset(s);
258 }
259 
260 /*
261  * osnoise_print_summary - print the summary of the hist data to the output
262  */
263 static void
264 osnoise_print_summary(struct osnoise_params *params,
265 		       struct trace_instance *trace,
266 		       struct osnoise_hist_data *data)
267 {
268 	int cpu;
269 
270 	if (params->common.hist.no_summary)
271 		return;
272 
273 	if (!params->common.hist.no_index)
274 		trace_seq_printf(trace->seq, "count:");
275 
276 	for_each_monitored_cpu(cpu, nr_cpus, &params->common) {
277 
278 		if (!data->hist[cpu].count)
279 			continue;
280 
281 		trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].count);
282 	}
283 	trace_seq_printf(trace->seq, "\n");
284 
285 	if (!params->common.hist.no_index)
286 		trace_seq_printf(trace->seq, "min:  ");
287 
288 	for_each_monitored_cpu(cpu, nr_cpus, &params->common) {
289 
290 		if (!data->hist[cpu].count)
291 			continue;
292 
293 		trace_seq_printf(trace->seq, "%9llu ",	data->hist[cpu].min_sample);
294 
295 	}
296 	trace_seq_printf(trace->seq, "\n");
297 
298 	if (!params->common.hist.no_index)
299 		trace_seq_printf(trace->seq, "avg:  ");
300 
301 	for_each_monitored_cpu(cpu, nr_cpus, &params->common) {
302 
303 		if (!data->hist[cpu].count)
304 			continue;
305 
306 		if (data->hist[cpu].count)
307 			trace_seq_printf(trace->seq, "%9.2f ",
308 				((double) data->hist[cpu].sum_sample) / data->hist[cpu].count);
309 		else
310 			trace_seq_printf(trace->seq, "        - ");
311 	}
312 	trace_seq_printf(trace->seq, "\n");
313 
314 	if (!params->common.hist.no_index)
315 		trace_seq_printf(trace->seq, "max:  ");
316 
317 	for_each_monitored_cpu(cpu, nr_cpus, &params->common) {
318 
319 		if (!data->hist[cpu].count)
320 			continue;
321 
322 		trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].max_sample);
323 
324 	}
325 	trace_seq_printf(trace->seq, "\n");
326 	trace_seq_do_printf(trace->seq);
327 	trace_seq_reset(trace->seq);
328 }
329 
330 /*
331  * osnoise_print_stats - print data for all CPUs
332  */
333 static void
334 osnoise_print_stats(struct osnoise_tool *tool)
335 {
336 	struct osnoise_params *params = to_osnoise_params(tool->params);
337 	struct osnoise_hist_data *data = tool->data;
338 	struct trace_instance *trace = &tool->trace;
339 	int has_samples = 0;
340 	int bucket, cpu;
341 	int total;
342 
343 	osnoise_hist_header(tool);
344 
345 	for (bucket = 0; bucket < data->entries; bucket++) {
346 		total = 0;
347 
348 		if (!params->common.hist.no_index)
349 			trace_seq_printf(trace->seq, "%-6d",
350 					 bucket * data->bucket_size);
351 
352 		for_each_monitored_cpu(cpu, nr_cpus, &params->common) {
353 
354 			if (!data->hist[cpu].count)
355 				continue;
356 
357 			total += data->hist[cpu].samples[bucket];
358 			trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].samples[bucket]);
359 		}
360 
361 		if (total == 0 && !params->common.hist.with_zeros) {
362 			trace_seq_reset(trace->seq);
363 			continue;
364 		}
365 
366 		/* There are samples above the threshold */
367 		has_samples = 1;
368 		trace_seq_printf(trace->seq, "\n");
369 		trace_seq_do_printf(trace->seq);
370 		trace_seq_reset(trace->seq);
371 	}
372 
373 	/*
374 	 * If no samples were recorded, skip calculations, print zeroed statistics
375 	 * and return.
376 	 */
377 	if (!has_samples) {
378 		trace_seq_reset(trace->seq);
379 		trace_seq_printf(trace->seq, "over: 0\ncount: 0\nmin: 0\navg: 0\nmax: 0\n");
380 		trace_seq_do_printf(trace->seq);
381 		trace_seq_reset(trace->seq);
382 		return;
383 	}
384 
385 	if (!params->common.hist.no_index)
386 		trace_seq_printf(trace->seq, "over: ");
387 
388 	for_each_monitored_cpu(cpu, nr_cpus, &params->common) {
389 
390 		if (!data->hist[cpu].count)
391 			continue;
392 
393 		trace_seq_printf(trace->seq, "%9d ",
394 				 data->hist[cpu].samples[data->entries]);
395 	}
396 	trace_seq_printf(trace->seq, "\n");
397 	trace_seq_do_printf(trace->seq);
398 	trace_seq_reset(trace->seq);
399 
400 	osnoise_print_summary(params, trace, data);
401 	osnoise_report_missed_events(tool);
402 }
403 
404 /*
405  * osnoise_hist_usage - prints osnoise hist usage message
406  */
407 static void osnoise_hist_usage(void)
408 {
409 	static const char * const msg_start[] = {
410 		"[-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",
411 		"	  [-T us] [-t [file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\",
412 		"	  [-c cpu-list] [-H cpu-list] [-P priority] [-b N] [-E N] [--no-header] [--no-summary] \\",
413 		"	  [--no-index] [--with-zeros] [-C [cgroup_name]] [--warm-up]",
414 		NULL,
415 	};
416 
417 	static const char * const msg_opts[] = {
418 		"	  -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit",
419 		"	  -p/--period us: osnoise period in us",
420 		"	  -r/--runtime us: osnoise runtime in us",
421 		"	  -s/--stop us: stop trace if a single sample is higher than the argument in us",
422 		"	  -S/--stop-total us: stop trace if the total sample is higher than the argument in us",
423 		"	  -T/--threshold us: the minimum delta to be considered a noise",
424 		"	  -c/--cpus cpu-list: list of cpus to run osnoise threads",
425 		"	  -H/--house-keeping cpus: run rtla control threads only on the given cpus",
426 		"	  -C/--cgroup [cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",
427 		"	  -d/--duration time[s|m|h|d]: duration of the session",
428 		"	  -D/--debug: print debug info",
429 		"	  -t/--trace [file]: save the stopped trace to [file|osnoise_trace.txt]",
430 		"	  -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
431 		"	     --filter <filter>: enable a trace event filter to the previous -e event",
432 		"	     --trigger <trigger>: enable a trace event trigger to the previous -e event",
433 		"	  -b/--bucket-size N: set the histogram bucket size (default 1)",
434 		"	  -E/--entries N: set the number of entries of the histogram (default 256)",
435 		"	     --no-header: do not print header",
436 		"	     --no-summary: do not print summary",
437 		"	     --no-index: do not print index",
438 		"	     --with-zeros: print zero only entries",
439 		"	  -P/--priority o:prio|r:prio|f:prio|d:runtime:period: set scheduling parameters",
440 		"		o:prio - use SCHED_OTHER with prio",
441 		"		r:prio - use SCHED_RR with prio",
442 		"		f:prio - use SCHED_FIFO with prio",
443 		"		d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
444 		"						       in nanoseconds",
445 		"	     --warm-up: let the workload run for s seconds before collecting data",
446 		"	     --trace-buffer-size kB: set the per-cpu trace buffer size in kB",
447 		"	     --on-threshold <action>: define action to be executed at stop-total threshold, multiple are allowed",
448 		"	     --on-end <action>: define action to be executed at measurement end, multiple are allowed",
449 		NULL,
450 	};
451 
452 	common_usage("osnoise", "hist", "a per-cpu histogram of the OS noise",
453 		     msg_start, msg_opts);
454 }
455 
456 /*
457  * osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters
458  */
459 static struct common_params
460 *osnoise_hist_parse_args(int argc, char *argv[])
461 {
462 	struct osnoise_params *params;
463 	int retval;
464 	int c;
465 	char *trace_output = NULL;
466 
467 	params = calloc(1, sizeof(*params));
468 	if (!params)
469 		exit(1);
470 
471 	actions_init(&params->common.threshold_actions);
472 	actions_init(&params->common.end_actions);
473 
474 	/* display data in microseconds */
475 	params->common.output_divisor = 1000;
476 	params->common.hist.bucket_size = 1;
477 	params->common.hist.entries = 256;
478 
479 	while (1) {
480 		static struct option long_options[] = {
481 			{"auto",		required_argument,	0, 'a'},
482 			{"bucket-size",		required_argument,	0, 'b'},
483 			{"entries",		required_argument,	0, 'E'},
484 			{"help",		no_argument,		0, 'h'},
485 			{"period",		required_argument,	0, 'p'},
486 			{"runtime",		required_argument,	0, 'r'},
487 			{"stop",		required_argument,	0, 's'},
488 			{"stop-total",		required_argument,	0, 'S'},
489 			{"trace",		optional_argument,	0, 't'},
490 			{"threshold",		required_argument,	0, 'T'},
491 			{"no-header",		no_argument,		0, '0'},
492 			{"no-summary",		no_argument,		0, '1'},
493 			{"no-index",		no_argument,		0, '2'},
494 			{"with-zeros",		no_argument,		0, '3'},
495 			{"trigger",		required_argument,	0, '4'},
496 			{"filter",		required_argument,	0, '5'},
497 			{"warm-up",		required_argument,	0, '6'},
498 			{"trace-buffer-size",	required_argument,	0, '7'},
499 			{"on-threshold",	required_argument,	0, '8'},
500 			{"on-end",		required_argument,	0, '9'},
501 			{0, 0, 0, 0}
502 		};
503 
504 		if (common_parse_options(argc, argv, &params->common))
505 			continue;
506 
507 		c = getopt_auto(argc, argv, long_options);
508 
509 		/* detect the end of the options. */
510 		if (c == -1)
511 			break;
512 
513 		switch (c) {
514 		case 'a':
515 			/* set sample stop to auto_thresh */
516 			params->common.stop_us = get_llong_from_str(optarg);
517 
518 			/* set sample threshold to 1 */
519 			params->threshold = 1;
520 
521 			/* set trace */
522 			if (!trace_output)
523 				trace_output = "osnoise_trace.txt";
524 
525 			break;
526 		case 'b':
527 			params->common.hist.bucket_size = get_llong_from_str(optarg);
528 			if (params->common.hist.bucket_size == 0 ||
529 			    params->common.hist.bucket_size >= 1000000)
530 				fatal("Bucket size needs to be > 0 and <= 1000000");
531 			break;
532 		case 'E':
533 			params->common.hist.entries = get_llong_from_str(optarg);
534 			if (params->common.hist.entries < 10 ||
535 			    params->common.hist.entries > 9999999)
536 				fatal("Entries must be > 10 and < 9999999");
537 			break;
538 		case 'h':
539 		case '?':
540 			osnoise_hist_usage();
541 			break;
542 		case 'p':
543 			params->period = get_llong_from_str(optarg);
544 			if (params->period > 10000000)
545 				fatal("Period longer than 10 s");
546 			break;
547 		case 'r':
548 			params->runtime = get_llong_from_str(optarg);
549 			if (params->runtime < 100)
550 				fatal("Runtime shorter than 100 us");
551 			break;
552 		case 's':
553 			params->common.stop_us = get_llong_from_str(optarg);
554 			break;
555 		case 'S':
556 			params->common.stop_total_us = get_llong_from_str(optarg);
557 			break;
558 		case 'T':
559 			params->threshold = get_llong_from_str(optarg);
560 			break;
561 		case 't':
562 			trace_output = parse_optional_arg(argc, argv);
563 			if (!trace_output)
564 				trace_output = "osnoise_trace.txt";
565 			break;
566 		case '0': /* no header */
567 			params->common.hist.no_header = 1;
568 			break;
569 		case '1': /* no summary */
570 			params->common.hist.no_summary = 1;
571 			break;
572 		case '2': /* no index */
573 			params->common.hist.no_index = 1;
574 			break;
575 		case '3': /* with zeros */
576 			params->common.hist.with_zeros = 1;
577 			break;
578 		case '4': /* trigger */
579 			if (params->common.events) {
580 				retval = trace_event_add_trigger(params->common.events, optarg);
581 				if (retval)
582 					fatal("Error adding trigger %s", optarg);
583 			} else {
584 				fatal("--trigger requires a previous -e");
585 			}
586 			break;
587 		case '5': /* filter */
588 			if (params->common.events) {
589 				retval = trace_event_add_filter(params->common.events, optarg);
590 				if (retval)
591 					fatal("Error adding filter %s", optarg);
592 			} else {
593 				fatal("--filter requires a previous -e");
594 			}
595 			break;
596 		case '6':
597 			params->common.warmup = get_llong_from_str(optarg);
598 			break;
599 		case '7':
600 			params->common.buffer_size = get_llong_from_str(optarg);
601 			break;
602 		case '8':
603 			retval = actions_parse(&params->common.threshold_actions, optarg,
604 					       "osnoise_trace.txt");
605 			if (retval)
606 				fatal("Invalid action %s", optarg);
607 			break;
608 		case '9':
609 			retval = actions_parse(&params->common.end_actions, optarg,
610 					       "osnoise_trace.txt");
611 			if (retval)
612 				fatal("Invalid action %s", optarg);
613 			break;
614 		default:
615 			fatal("Invalid option");
616 		}
617 	}
618 
619 	if (trace_output)
620 		actions_add_trace_output(&params->common.threshold_actions, trace_output);
621 
622 	if (geteuid())
623 		fatal("rtla needs root permission");
624 
625 	if (params->common.hist.no_index && !params->common.hist.with_zeros)
626 		fatal("no-index set and with-zeros not set - it does not make sense");
627 
628 	return &params->common;
629 }
630 
631 /*
632  * osnoise_hist_apply_config - apply the hist configs to the initialized tool
633  */
634 static int
635 osnoise_hist_apply_config(struct osnoise_tool *tool)
636 {
637 	return osnoise_apply_config(tool, to_osnoise_params(tool->params));
638 }
639 
640 /*
641  * osnoise_init_hist - initialize a osnoise hist tool with parameters
642  */
643 static struct osnoise_tool
644 *osnoise_init_hist(struct common_params *params)
645 {
646 	struct osnoise_tool *tool;
647 
648 	tool = osnoise_init_tool("osnoise_hist");
649 	if (!tool)
650 		return NULL;
651 
652 	tool->data = osnoise_alloc_histogram(params->hist.entries,
653 					     params->hist.bucket_size);
654 	if (!tool->data)
655 		goto out_err;
656 
657 	return tool;
658 
659 out_err:
660 	osnoise_destroy_tool(tool);
661 	return NULL;
662 }
663 
664 static int osnoise_hist_enable(struct osnoise_tool *tool)
665 {
666 	int retval;
667 
668 	retval = osnoise_init_trace_hist(tool);
669 	if (retval)
670 		return retval;
671 
672 	return osnoise_enable(tool);
673 }
674 
675 static int osnoise_hist_main_loop(struct osnoise_tool *tool)
676 {
677 	int retval;
678 
679 	retval = hist_main_loop(tool);
680 	osnoise_read_trace_hist(tool);
681 
682 	return retval;
683 }
684 
685 struct tool_ops osnoise_hist_ops = {
686 	.tracer = "osnoise",
687 	.comm_prefix = "osnoise/",
688 	.parse_args = osnoise_hist_parse_args,
689 	.init_tool = osnoise_init_hist,
690 	.apply_config = osnoise_hist_apply_config,
691 	.enable = osnoise_hist_enable,
692 	.main = osnoise_hist_main_loop,
693 	.print_stats = osnoise_print_stats,
694 	.free = osnoise_free_hist_tool,
695 };
696