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