xref: /linux/tools/tracing/rtla/src/osnoise_hist.c (revision 40648d246fa4307ef11d185933cb0d79fc9ff46c)
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
osnoise_free_histogram(struct osnoise_hist_data * data)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
osnoise_alloc_histogram(int nr_cpus,int entries,int bucket_size)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 
osnoise_hist_update_multiple(struct osnoise_tool * tool,int cpu,unsigned long long duration,int count)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  */
osnoise_destroy_trace_hist(struct osnoise_tool * tool)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  */
osnoise_init_trace_hist(struct osnoise_tool * tool)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  */
osnoise_read_trace_hist(struct osnoise_tool * tool)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  */
osnoise_hist_header(struct osnoise_tool * tool)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
osnoise_print_summary(struct osnoise_hist_params * params,struct trace_instance * trace,struct osnoise_hist_data * data)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
osnoise_print_stats(struct osnoise_hist_params * params,struct osnoise_tool * tool)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 has_samples = 0;
378 	int bucket, cpu;
379 	int total;
380 
381 	osnoise_hist_header(tool);
382 
383 	for (bucket = 0; bucket < data->entries; bucket++) {
384 		total = 0;
385 
386 		if (!params->no_index)
387 			trace_seq_printf(trace->seq, "%-6d",
388 					 bucket * data->bucket_size);
389 
390 		for (cpu = 0; cpu < data->nr_cpus; cpu++) {
391 			if (params->cpus && !CPU_ISSET(cpu, &params->monitored_cpus))
392 				continue;
393 
394 			if (!data->hist[cpu].count)
395 				continue;
396 
397 			total += data->hist[cpu].samples[bucket];
398 			trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].samples[bucket]);
399 		}
400 
401 		if (total == 0 && !params->with_zeros) {
402 			trace_seq_reset(trace->seq);
403 			continue;
404 		}
405 
406 		/* There are samples above the threshold */
407 		has_samples = 1;
408 		trace_seq_printf(trace->seq, "\n");
409 		trace_seq_do_printf(trace->seq);
410 		trace_seq_reset(trace->seq);
411 	}
412 
413 	/*
414 	 * If no samples were recorded, skip calculations, print zeroed statistics
415 	 * and return.
416 	 */
417 	if (!has_samples) {
418 		trace_seq_reset(trace->seq);
419 		trace_seq_printf(trace->seq, "over: 0\ncount: 0\nmin: 0\navg: 0\nmax: 0\n");
420 		trace_seq_do_printf(trace->seq);
421 		trace_seq_reset(trace->seq);
422 		return;
423 	}
424 
425 	if (!params->no_index)
426 		trace_seq_printf(trace->seq, "over: ");
427 
428 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
429 		if (params->cpus && !CPU_ISSET(cpu, &params->monitored_cpus))
430 			continue;
431 
432 		if (!data->hist[cpu].count)
433 			continue;
434 
435 		trace_seq_printf(trace->seq, "%9d ",
436 				 data->hist[cpu].samples[data->entries]);
437 	}
438 	trace_seq_printf(trace->seq, "\n");
439 	trace_seq_do_printf(trace->seq);
440 	trace_seq_reset(trace->seq);
441 
442 	osnoise_print_summary(params, trace, data);
443 	osnoise_report_missed_events(tool);
444 }
445 
446 /*
447  * osnoise_hist_usage - prints osnoise hist usage message
448  */
osnoise_hist_usage(char * usage)449 static void osnoise_hist_usage(char *usage)
450 {
451 	int i;
452 
453 	static const char * const msg[] = {
454 		"",
455 		"  usage: rtla osnoise hist [-h] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",
456 		"	  [-T us] [-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\",
457 		"	  [-c cpu-list] [-H cpu-list] [-P priority] [-b N] [-E N] [--no-header] [--no-summary] \\",
458 		"	  [--no-index] [--with-zeros] [-C[=cgroup_name]] [--warm-up]",
459 		"",
460 		"	  -h/--help: print this menu",
461 		"	  -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit",
462 		"	  -p/--period us: osnoise period in us",
463 		"	  -r/--runtime us: osnoise runtime in us",
464 		"	  -s/--stop us: stop trace if a single sample is higher than the argument in us",
465 		"	  -S/--stop-total us: stop trace if the total sample is higher than the argument in us",
466 		"	  -T/--threshold us: the minimum delta to be considered a noise",
467 		"	  -c/--cpus cpu-list: list of cpus to run osnoise threads",
468 		"	  -H/--house-keeping cpus: run rtla control threads only on the given cpus",
469 		"	  -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",
470 		"	  -d/--duration time[s|m|h|d]: duration of the session",
471 		"	  -D/--debug: print debug info",
472 		"	  -t/--trace[file]: save the stopped trace to [file|osnoise_trace.txt]",
473 		"	  -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
474 		"	     --filter <filter>: enable a trace event filter to the previous -e event",
475 		"	     --trigger <trigger>: enable a trace event trigger to the previous -e event",
476 		"	  -b/--bucket-size N: set the histogram bucket size (default 1)",
477 		"	  -E/--entries N: set the number of entries of the histogram (default 256)",
478 		"	     --no-header: do not print header",
479 		"	     --no-summary: do not print summary",
480 		"	     --no-index: do not print index",
481 		"	     --with-zeros: print zero only entries",
482 		"	  -P/--priority o:prio|r:prio|f:prio|d:runtime:period: set scheduling parameters",
483 		"		o:prio - use SCHED_OTHER with prio",
484 		"		r:prio - use SCHED_RR with prio",
485 		"		f:prio - use SCHED_FIFO with prio",
486 		"		d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
487 		"						       in nanoseconds",
488 		"	     --warm-up: let the workload run for s seconds before collecting data",
489 		"	     --trace-buffer-size kB: set the per-cpu trace buffer size in kB",
490 		NULL,
491 	};
492 
493 	if (usage)
494 		fprintf(stderr, "%s\n", usage);
495 
496 	fprintf(stderr, "rtla osnoise hist: a per-cpu histogram of the OS noise (version %s)\n",
497 			VERSION);
498 
499 	for (i = 0; msg[i]; i++)
500 		fprintf(stderr, "%s\n", msg[i]);
501 
502 	if (usage)
503 		exit(EXIT_FAILURE);
504 
505 	exit(EXIT_SUCCESS);
506 }
507 
508 /*
509  * osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters
510  */
511 static struct osnoise_hist_params
osnoise_hist_parse_args(int argc,char * argv[])512 *osnoise_hist_parse_args(int argc, char *argv[])
513 {
514 	struct osnoise_hist_params *params;
515 	struct trace_events *tevent;
516 	int retval;
517 	int c;
518 
519 	params = calloc(1, sizeof(*params));
520 	if (!params)
521 		exit(1);
522 
523 	/* display data in microseconds */
524 	params->output_divisor = 1000;
525 	params->bucket_size = 1;
526 	params->entries = 256;
527 
528 	while (1) {
529 		static struct option long_options[] = {
530 			{"auto",		required_argument,	0, 'a'},
531 			{"bucket-size",		required_argument,	0, 'b'},
532 			{"entries",		required_argument,	0, 'E'},
533 			{"cpus",		required_argument,	0, 'c'},
534 			{"cgroup",		optional_argument,	0, 'C'},
535 			{"debug",		no_argument,		0, 'D'},
536 			{"duration",		required_argument,	0, 'd'},
537 			{"house-keeping",	required_argument,		0, 'H'},
538 			{"help",		no_argument,		0, 'h'},
539 			{"period",		required_argument,	0, 'p'},
540 			{"priority",		required_argument,	0, 'P'},
541 			{"runtime",		required_argument,	0, 'r'},
542 			{"stop",		required_argument,	0, 's'},
543 			{"stop-total",		required_argument,	0, 'S'},
544 			{"trace",		optional_argument,	0, 't'},
545 			{"event",		required_argument,	0, 'e'},
546 			{"threshold",		required_argument,	0, 'T'},
547 			{"no-header",		no_argument,		0, '0'},
548 			{"no-summary",		no_argument,		0, '1'},
549 			{"no-index",		no_argument,		0, '2'},
550 			{"with-zeros",		no_argument,		0, '3'},
551 			{"trigger",		required_argument,	0, '4'},
552 			{"filter",		required_argument,	0, '5'},
553 			{"warm-up",		required_argument,	0, '6'},
554 			{"trace-buffer-size",	required_argument,	0, '7'},
555 			{0, 0, 0, 0}
556 		};
557 
558 		/* getopt_long stores the option index here. */
559 		int option_index = 0;
560 
561 		c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:p:P:r:s:S:t::T:01234:5:6:7:",
562 				 long_options, &option_index);
563 
564 		/* detect the end of the options. */
565 		if (c == -1)
566 			break;
567 
568 		switch (c) {
569 		case 'a':
570 			/* set sample stop to auto_thresh */
571 			params->stop_us = get_llong_from_str(optarg);
572 
573 			/* set sample threshold to 1 */
574 			params->threshold = 1;
575 
576 			/* set trace */
577 			params->trace_output = "osnoise_trace.txt";
578 
579 			break;
580 		case 'b':
581 			params->bucket_size = get_llong_from_str(optarg);
582 			if ((params->bucket_size == 0) || (params->bucket_size >= 1000000))
583 				osnoise_hist_usage("Bucket size needs to be > 0 and <= 1000000\n");
584 			break;
585 		case 'c':
586 			retval = parse_cpu_set(optarg, &params->monitored_cpus);
587 			if (retval)
588 				osnoise_hist_usage("\nInvalid -c cpu list\n");
589 			params->cpus = optarg;
590 			break;
591 		case 'C':
592 			params->cgroup = 1;
593 			if (!optarg) {
594 				/* will inherit this cgroup */
595 				params->cgroup_name = NULL;
596 			} else if (*optarg == '=') {
597 				/* skip the = */
598 				params->cgroup_name = ++optarg;
599 			}
600 			break;
601 		case 'D':
602 			config_debug = 1;
603 			break;
604 		case 'd':
605 			params->duration = parse_seconds_duration(optarg);
606 			if (!params->duration)
607 				osnoise_hist_usage("Invalid -D duration\n");
608 			break;
609 		case 'e':
610 			tevent = trace_event_alloc(optarg);
611 			if (!tevent) {
612 				err_msg("Error alloc trace event");
613 				exit(EXIT_FAILURE);
614 			}
615 
616 			if (params->events)
617 				tevent->next = params->events;
618 
619 			params->events = tevent;
620 			break;
621 		case 'E':
622 			params->entries = get_llong_from_str(optarg);
623 			if ((params->entries < 10) || (params->entries > 9999999))
624 				osnoise_hist_usage("Entries must be > 10 and < 9999999\n");
625 			break;
626 		case 'h':
627 		case '?':
628 			osnoise_hist_usage(NULL);
629 			break;
630 		case 'H':
631 			params->hk_cpus = 1;
632 			retval = parse_cpu_set(optarg, &params->hk_cpu_set);
633 			if (retval) {
634 				err_msg("Error parsing house keeping CPUs\n");
635 				exit(EXIT_FAILURE);
636 			}
637 			break;
638 		case 'p':
639 			params->period = get_llong_from_str(optarg);
640 			if (params->period > 10000000)
641 				osnoise_hist_usage("Period longer than 10 s\n");
642 			break;
643 		case 'P':
644 			retval = parse_prio(optarg, &params->sched_param);
645 			if (retval == -1)
646 				osnoise_hist_usage("Invalid -P priority");
647 			params->set_sched = 1;
648 			break;
649 		case 'r':
650 			params->runtime = get_llong_from_str(optarg);
651 			if (params->runtime < 100)
652 				osnoise_hist_usage("Runtime shorter than 100 us\n");
653 			break;
654 		case 's':
655 			params->stop_us = get_llong_from_str(optarg);
656 			break;
657 		case 'S':
658 			params->stop_total_us = get_llong_from_str(optarg);
659 			break;
660 		case 'T':
661 			params->threshold = get_llong_from_str(optarg);
662 			break;
663 		case 't':
664 			if (optarg) {
665 				if (optarg[0] == '=')
666 					params->trace_output = &optarg[1];
667 				else
668 					params->trace_output = &optarg[0];
669 			} else if (optind < argc && argv[optind][0] != '0')
670 				params->trace_output = argv[optind];
671 			else
672 				params->trace_output = "osnoise_trace.txt";
673 			break;
674 		case '0': /* no header */
675 			params->no_header = 1;
676 			break;
677 		case '1': /* no summary */
678 			params->no_summary = 1;
679 			break;
680 		case '2': /* no index */
681 			params->no_index = 1;
682 			break;
683 		case '3': /* with zeros */
684 			params->with_zeros = 1;
685 			break;
686 		case '4': /* trigger */
687 			if (params->events) {
688 				retval = trace_event_add_trigger(params->events, optarg);
689 				if (retval) {
690 					err_msg("Error adding trigger %s\n", optarg);
691 					exit(EXIT_FAILURE);
692 				}
693 			} else {
694 				osnoise_hist_usage("--trigger requires a previous -e\n");
695 			}
696 			break;
697 		case '5': /* filter */
698 			if (params->events) {
699 				retval = trace_event_add_filter(params->events, optarg);
700 				if (retval) {
701 					err_msg("Error adding filter %s\n", optarg);
702 					exit(EXIT_FAILURE);
703 				}
704 			} else {
705 				osnoise_hist_usage("--filter requires a previous -e\n");
706 			}
707 			break;
708 		case '6':
709 			params->warmup = get_llong_from_str(optarg);
710 			break;
711 		case '7':
712 			params->buffer_size = get_llong_from_str(optarg);
713 			break;
714 		default:
715 			osnoise_hist_usage("Invalid option");
716 		}
717 	}
718 
719 	if (geteuid()) {
720 		err_msg("rtla needs root permission\n");
721 		exit(EXIT_FAILURE);
722 	}
723 
724 	if (params->no_index && !params->with_zeros)
725 		osnoise_hist_usage("no-index set and with-zeros not set - it does not make sense");
726 
727 	return params;
728 }
729 
730 /*
731  * osnoise_hist_apply_config - apply the hist configs to the initialized tool
732  */
733 static int
osnoise_hist_apply_config(struct osnoise_tool * tool,struct osnoise_hist_params * params)734 osnoise_hist_apply_config(struct osnoise_tool *tool, struct osnoise_hist_params *params)
735 {
736 	int retval;
737 
738 	if (!params->sleep_time)
739 		params->sleep_time = 1;
740 
741 	if (params->cpus) {
742 		retval = osnoise_set_cpus(tool->context, params->cpus);
743 		if (retval) {
744 			err_msg("Failed to apply CPUs config\n");
745 			goto out_err;
746 		}
747 	}
748 
749 	if (params->runtime || params->period) {
750 		retval = osnoise_set_runtime_period(tool->context,
751 						    params->runtime,
752 						    params->period);
753 		if (retval) {
754 			err_msg("Failed to set runtime and/or period\n");
755 			goto out_err;
756 		}
757 	}
758 
759 	if (params->stop_us) {
760 		retval = osnoise_set_stop_us(tool->context, params->stop_us);
761 		if (retval) {
762 			err_msg("Failed to set stop us\n");
763 			goto out_err;
764 		}
765 	}
766 
767 	if (params->stop_total_us) {
768 		retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us);
769 		if (retval) {
770 			err_msg("Failed to set stop total us\n");
771 			goto out_err;
772 		}
773 	}
774 
775 	if (params->threshold) {
776 		retval = osnoise_set_tracing_thresh(tool->context, params->threshold);
777 		if (retval) {
778 			err_msg("Failed to set tracing_thresh\n");
779 			goto out_err;
780 		}
781 	}
782 
783 	if (params->hk_cpus) {
784 		retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set),
785 					   &params->hk_cpu_set);
786 		if (retval == -1) {
787 			err_msg("Failed to set rtla to the house keeping CPUs\n");
788 			goto out_err;
789 		}
790 	} else if (params->cpus) {
791 		/*
792 		 * Even if the user do not set a house-keeping CPU, try to
793 		 * move rtla to a CPU set different to the one where the user
794 		 * set the workload to run.
795 		 *
796 		 * No need to check results as this is an automatic attempt.
797 		 */
798 		auto_house_keeping(&params->monitored_cpus);
799 	}
800 
801 	return 0;
802 
803 out_err:
804 	return -1;
805 }
806 
807 /*
808  * osnoise_init_hist - initialize a osnoise hist tool with parameters
809  */
810 static struct osnoise_tool
osnoise_init_hist(struct osnoise_hist_params * params)811 *osnoise_init_hist(struct osnoise_hist_params *params)
812 {
813 	struct osnoise_tool *tool;
814 	int nr_cpus;
815 
816 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
817 
818 	tool = osnoise_init_tool("osnoise_hist");
819 	if (!tool)
820 		return NULL;
821 
822 	tool->data = osnoise_alloc_histogram(nr_cpus, params->entries, params->bucket_size);
823 	if (!tool->data)
824 		goto out_err;
825 
826 	tool->params = params;
827 
828 	return tool;
829 
830 out_err:
831 	osnoise_destroy_tool(tool);
832 	return NULL;
833 }
834 
835 static int stop_tracing;
stop_hist(int sig)836 static void stop_hist(int sig)
837 {
838 	stop_tracing = 1;
839 }
840 
841 /*
842  * osnoise_hist_set_signals - handles the signal to stop the tool
843  */
844 static void
osnoise_hist_set_signals(struct osnoise_hist_params * params)845 osnoise_hist_set_signals(struct osnoise_hist_params *params)
846 {
847 	signal(SIGINT, stop_hist);
848 	if (params->duration) {
849 		signal(SIGALRM, stop_hist);
850 		alarm(params->duration);
851 	}
852 }
853 
osnoise_hist_main(int argc,char * argv[])854 int osnoise_hist_main(int argc, char *argv[])
855 {
856 	struct osnoise_hist_params *params;
857 	struct osnoise_tool *record = NULL;
858 	struct osnoise_tool *tool = NULL;
859 	struct trace_instance *trace;
860 	int return_value = 1;
861 	int retval;
862 
863 	params = osnoise_hist_parse_args(argc, argv);
864 	if (!params)
865 		exit(1);
866 
867 	tool = osnoise_init_hist(params);
868 	if (!tool) {
869 		err_msg("Could not init osnoise hist\n");
870 		goto out_exit;
871 	}
872 
873 	retval = osnoise_hist_apply_config(tool, params);
874 	if (retval) {
875 		err_msg("Could not apply config\n");
876 		goto out_destroy;
877 	}
878 
879 	trace = &tool->trace;
880 
881 	retval = enable_osnoise(trace);
882 	if (retval) {
883 		err_msg("Failed to enable osnoise tracer\n");
884 		goto out_destroy;
885 	}
886 
887 	retval = osnoise_init_trace_hist(tool);
888 	if (retval)
889 		goto out_destroy;
890 
891 	if (params->set_sched) {
892 		retval = set_comm_sched_attr("osnoise/", &params->sched_param);
893 		if (retval) {
894 			err_msg("Failed to set sched parameters\n");
895 			goto out_free;
896 		}
897 	}
898 
899 	if (params->cgroup) {
900 		retval = set_comm_cgroup("timerlat/", params->cgroup_name);
901 		if (!retval) {
902 			err_msg("Failed to move threads to cgroup\n");
903 			goto out_free;
904 		}
905 	}
906 
907 	if (params->trace_output) {
908 		record = osnoise_init_trace_tool("osnoise");
909 		if (!record) {
910 			err_msg("Failed to enable the trace instance\n");
911 			goto out_free;
912 		}
913 
914 		if (params->events) {
915 			retval = trace_events_enable(&record->trace, params->events);
916 			if (retval)
917 				goto out_hist;
918 		}
919 
920 		if (params->buffer_size > 0) {
921 			retval = trace_set_buffer_size(&record->trace, params->buffer_size);
922 			if (retval)
923 				goto out_hist;
924 		}
925 	}
926 
927 	/*
928 	 * Start the tracer here, after having set all instances.
929 	 *
930 	 * Let the trace instance start first for the case of hitting a stop
931 	 * tracing while enabling other instances. The trace instance is the
932 	 * one with most valuable information.
933 	 */
934 	if (params->trace_output)
935 		trace_instance_start(&record->trace);
936 	trace_instance_start(trace);
937 
938 	if (params->warmup > 0) {
939 		debug_msg("Warming up for %d seconds\n", params->warmup);
940 		sleep(params->warmup);
941 		if (stop_tracing)
942 			goto out_hist;
943 
944 		/*
945 		 * Clean up the buffer. The osnoise workload do not run
946 		 * with tracing off to avoid creating a performance penalty
947 		 * when not needed.
948 		 */
949 		retval = tracefs_instance_file_write(trace->inst, "trace", "");
950 		if (retval < 0) {
951 			debug_msg("Error cleaning up the buffer");
952 			goto out_hist;
953 		}
954 
955 	}
956 
957 	tool->start_time = time(NULL);
958 	osnoise_hist_set_signals(params);
959 
960 	while (!stop_tracing) {
961 		sleep(params->sleep_time);
962 
963 		retval = tracefs_iterate_raw_events(trace->tep,
964 						    trace->inst,
965 						    NULL,
966 						    0,
967 						    collect_registered_events,
968 						    trace);
969 		if (retval < 0) {
970 			err_msg("Error iterating on events\n");
971 			goto out_hist;
972 		}
973 
974 		if (osnoise_trace_is_off(tool, record))
975 			break;
976 	}
977 
978 	osnoise_read_trace_hist(tool);
979 
980 	osnoise_print_stats(params, tool);
981 
982 	return_value = 0;
983 
984 	if (osnoise_trace_is_off(tool, record)) {
985 		printf("rtla osnoise hit stop tracing\n");
986 		if (params->trace_output) {
987 			printf("  Saving trace to %s\n", params->trace_output);
988 			save_trace_to_file(record->trace.inst, params->trace_output);
989 		}
990 	}
991 
992 out_hist:
993 	trace_events_destroy(&record->trace, params->events);
994 	params->events = NULL;
995 out_free:
996 	osnoise_free_histogram(tool->data);
997 out_destroy:
998 	osnoise_destroy_tool(record);
999 	osnoise_destroy_tool(tool);
1000 	free(params);
1001 out_exit:
1002 	exit(return_value);
1003 }
1004