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