xref: /linux/tools/tracing/rtla/src/osnoise_top.c (revision 3daee2e4b3568f0ed88b0598df96547fcf21cb9b)
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 #include <sched.h>
15 
16 #include "osnoise.h"
17 #include "utils.h"
18 
19 enum osnoise_mode {
20 	MODE_OSNOISE = 0,
21 	MODE_HWNOISE
22 };
23 
24 /*
25  * osnoise top parameters
26  */
27 struct osnoise_top_params {
28 	char			*cpus;
29 	cpu_set_t		monitored_cpus;
30 	char			*trace_output;
31 	char			*cgroup_name;
32 	unsigned long long	runtime;
33 	unsigned long long	period;
34 	long long		threshold;
35 	long long		stop_us;
36 	long long		stop_total_us;
37 	int			sleep_time;
38 	int			duration;
39 	int			quiet;
40 	int			set_sched;
41 	int			cgroup;
42 	int			hk_cpus;
43 	int			warmup;
44 	int			buffer_size;
45 	cpu_set_t		hk_cpu_set;
46 	struct sched_attr	sched_param;
47 	struct trace_events	*events;
48 	enum osnoise_mode	mode;
49 };
50 
51 struct osnoise_top_cpu {
52 	unsigned long long	sum_runtime;
53 	unsigned long long	sum_noise;
54 	unsigned long long	max_noise;
55 	unsigned long long	max_sample;
56 
57 	unsigned long long	hw_count;
58 	unsigned long long	nmi_count;
59 	unsigned long long	irq_count;
60 	unsigned long long	softirq_count;
61 	unsigned long long	thread_count;
62 
63 	int			sum_cycles;
64 };
65 
66 struct osnoise_top_data {
67 	struct osnoise_top_cpu	*cpu_data;
68 	int			nr_cpus;
69 };
70 
71 /*
72  * osnoise_free_top - free runtime data
73  */
74 static void
75 osnoise_free_top(struct osnoise_top_data *data)
76 {
77 	free(data->cpu_data);
78 	free(data);
79 }
80 
81 /*
82  * osnoise_alloc_histogram - alloc runtime data
83  */
84 static struct osnoise_top_data *osnoise_alloc_top(int nr_cpus)
85 {
86 	struct osnoise_top_data *data;
87 
88 	data = calloc(1, sizeof(*data));
89 	if (!data)
90 		return NULL;
91 
92 	data->nr_cpus = nr_cpus;
93 
94 	/* one set of histograms per CPU */
95 	data->cpu_data = calloc(1, sizeof(*data->cpu_data) * nr_cpus);
96 	if (!data->cpu_data)
97 		goto cleanup;
98 
99 	return data;
100 
101 cleanup:
102 	osnoise_free_top(data);
103 	return NULL;
104 }
105 
106 /*
107  * osnoise_top_handler - this is the handler for osnoise tracer events
108  */
109 static int
110 osnoise_top_handler(struct trace_seq *s, struct tep_record *record,
111 		    struct tep_event *event, void *context)
112 {
113 	struct trace_instance *trace = context;
114 	struct osnoise_tool *tool;
115 	unsigned long long val;
116 	struct osnoise_top_cpu *cpu_data;
117 	struct osnoise_top_data *data;
118 	int cpu = record->cpu;
119 
120 	tool = container_of(trace, struct osnoise_tool, trace);
121 
122 	data = tool->data;
123 	cpu_data = &data->cpu_data[cpu];
124 
125 	cpu_data->sum_cycles++;
126 
127 	tep_get_field_val(s, event, "runtime", record, &val, 1);
128 	update_sum(&cpu_data->sum_runtime, &val);
129 
130 	tep_get_field_val(s, event, "noise", record, &val, 1);
131 	update_max(&cpu_data->max_noise, &val);
132 	update_sum(&cpu_data->sum_noise, &val);
133 
134 	tep_get_field_val(s, event, "max_sample", record, &val, 1);
135 	update_max(&cpu_data->max_sample, &val);
136 
137 	tep_get_field_val(s, event, "hw_count", record, &val, 1);
138 	update_sum(&cpu_data->hw_count, &val);
139 
140 	tep_get_field_val(s, event, "nmi_count", record, &val, 1);
141 	update_sum(&cpu_data->nmi_count, &val);
142 
143 	tep_get_field_val(s, event, "irq_count", record, &val, 1);
144 	update_sum(&cpu_data->irq_count, &val);
145 
146 	tep_get_field_val(s, event, "softirq_count", record, &val, 1);
147 	update_sum(&cpu_data->softirq_count, &val);
148 
149 	tep_get_field_val(s, event, "thread_count", record, &val, 1);
150 	update_sum(&cpu_data->thread_count, &val);
151 
152 	return 0;
153 }
154 
155 /*
156  * osnoise_top_header - print the header of the tool output
157  */
158 static void osnoise_top_header(struct osnoise_tool *top)
159 {
160 	struct osnoise_top_params *params = top->params;
161 	struct trace_seq *s = top->trace.seq;
162 	char duration[26];
163 
164 	get_duration(top->start_time, duration, sizeof(duration));
165 
166 	trace_seq_printf(s, "\033[2;37;40m");
167 	trace_seq_printf(s, "                                          ");
168 
169 	if (params->mode == MODE_OSNOISE) {
170 		trace_seq_printf(s, "Operating System Noise");
171 		trace_seq_printf(s, "                                       ");
172 	} else if (params->mode == MODE_HWNOISE) {
173 		trace_seq_printf(s, "Hardware-related Noise");
174 	}
175 
176 	trace_seq_printf(s, "                                   ");
177 	trace_seq_printf(s, "\033[0;0;0m");
178 	trace_seq_printf(s, "\n");
179 
180 	trace_seq_printf(s, "duration: %9s | time is in us\n", duration);
181 
182 	trace_seq_printf(s, "\033[2;30;47m");
183 	trace_seq_printf(s, "CPU Period       Runtime ");
184 	trace_seq_printf(s, "       Noise ");
185 	trace_seq_printf(s, " %% CPU Aval ");
186 	trace_seq_printf(s, "  Max Noise   Max Single ");
187 	trace_seq_printf(s, "         HW          NMI");
188 
189 	if (params->mode == MODE_HWNOISE)
190 		goto eol;
191 
192 	trace_seq_printf(s, "          IRQ      Softirq       Thread");
193 
194 eol:
195 	trace_seq_printf(s, "\033[0;0;0m");
196 	trace_seq_printf(s, "\n");
197 }
198 
199 /*
200  * clear_terminal - clears the output terminal
201  */
202 static void clear_terminal(struct trace_seq *seq)
203 {
204 	if (!config_debug)
205 		trace_seq_printf(seq, "\033c");
206 }
207 
208 /*
209  * osnoise_top_print - prints the output of a given CPU
210  */
211 static void osnoise_top_print(struct osnoise_tool *tool, int cpu)
212 {
213 	struct osnoise_top_params *params = tool->params;
214 	struct trace_seq *s = tool->trace.seq;
215 	struct osnoise_top_cpu *cpu_data;
216 	struct osnoise_top_data *data;
217 	int percentage;
218 	int decimal;
219 
220 	data = tool->data;
221 	cpu_data = &data->cpu_data[cpu];
222 
223 	if (!cpu_data->sum_runtime)
224 		return;
225 
226 	percentage = ((cpu_data->sum_runtime - cpu_data->sum_noise) * 10000000)
227 			/ cpu_data->sum_runtime;
228 	decimal = percentage % 100000;
229 	percentage = percentage / 100000;
230 
231 	trace_seq_printf(s, "%3d #%-6d %12llu ", cpu, cpu_data->sum_cycles, cpu_data->sum_runtime);
232 	trace_seq_printf(s, "%12llu ", cpu_data->sum_noise);
233 	trace_seq_printf(s, "  %3d.%05d", percentage, decimal);
234 	trace_seq_printf(s, "%12llu %12llu", cpu_data->max_noise, cpu_data->max_sample);
235 
236 	trace_seq_printf(s, "%12llu ", cpu_data->hw_count);
237 	trace_seq_printf(s, "%12llu ", cpu_data->nmi_count);
238 
239 	if (params->mode == MODE_HWNOISE) {
240 		trace_seq_printf(s, "\n");
241 		return;
242 	}
243 
244 	trace_seq_printf(s, "%12llu ", cpu_data->irq_count);
245 	trace_seq_printf(s, "%12llu ", cpu_data->softirq_count);
246 	trace_seq_printf(s, "%12llu\n", cpu_data->thread_count);
247 }
248 
249 /*
250  * osnoise_print_stats - print data for all cpus
251  */
252 static void
253 osnoise_print_stats(struct osnoise_top_params *params, struct osnoise_tool *top)
254 {
255 	struct trace_instance *trace = &top->trace;
256 	static int nr_cpus = -1;
257 	int i;
258 
259 	if (nr_cpus == -1)
260 		nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
261 
262 	if (!params->quiet)
263 		clear_terminal(trace->seq);
264 
265 	osnoise_top_header(top);
266 
267 	for (i = 0; i < nr_cpus; i++) {
268 		if (params->cpus && !CPU_ISSET(i, &params->monitored_cpus))
269 			continue;
270 		osnoise_top_print(top, i);
271 	}
272 
273 	trace_seq_do_printf(trace->seq);
274 	trace_seq_reset(trace->seq);
275 }
276 
277 /*
278  * osnoise_top_usage - prints osnoise top usage message
279  */
280 static void osnoise_top_usage(struct osnoise_top_params *params, char *usage)
281 {
282 	int i;
283 
284 	static const char * const msg[] = {
285 		" [-h] [-q] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",
286 		"	  [-T us] [-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\",
287 		"	  [-c cpu-list] [-H cpu-list] [-P priority] [-C[=cgroup_name]] [--warm-up s]",
288 		"",
289 		"	  -h/--help: print this menu",
290 		"	  -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit",
291 		"	  -p/--period us: osnoise period in us",
292 		"	  -r/--runtime us: osnoise runtime in us",
293 		"	  -s/--stop us: stop trace if a single sample is higher than the argument in us",
294 		"	  -S/--stop-total us: stop trace if the total sample is higher than the argument in us",
295 		"	  -T/--threshold us: the minimum delta to be considered a noise",
296 		"	  -c/--cpus cpu-list: list of cpus to run osnoise threads",
297 		"	  -H/--house-keeping cpus: run rtla control threads only on the given cpus",
298 		"	  -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",
299 		"	  -d/--duration time[s|m|h|d]: duration of the session",
300 		"	  -D/--debug: print debug info",
301 		"	  -t/--trace[file]: save the stopped trace to [file|osnoise_trace.txt]",
302 		"	  -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
303 		"	     --filter <filter>: enable a trace event filter to the previous -e event",
304 		"	     --trigger <trigger>: enable a trace event trigger to the previous -e event",
305 		"	  -q/--quiet print only a summary at the end",
306 		"	  -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
307 		"		o:prio - use SCHED_OTHER with prio",
308 		"		r:prio - use SCHED_RR with prio",
309 		"		f:prio - use SCHED_FIFO with prio",
310 		"		d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
311 		"						       in nanoseconds",
312 		"	     --warm-up s: let the workload run for s seconds before collecting data",
313 		"	     --trace-buffer-size kB: set the per-cpu trace buffer size in kB",
314 		NULL,
315 	};
316 
317 	if (usage)
318 		fprintf(stderr, "%s\n", usage);
319 
320 	if (params->mode == MODE_OSNOISE) {
321 		fprintf(stderr,
322 			"rtla osnoise top: a per-cpu summary of the OS noise (version %s)\n",
323 			VERSION);
324 
325 		fprintf(stderr, "  usage: rtla osnoise [top]");
326 	}
327 
328 	if (params->mode == MODE_HWNOISE) {
329 		fprintf(stderr,
330 			"rtla hwnoise: a summary of hardware-related noise (version %s)\n",
331 			VERSION);
332 
333 		fprintf(stderr, "  usage: rtla hwnoise");
334 	}
335 
336 	for (i = 0; msg[i]; i++)
337 		fprintf(stderr, "%s\n", msg[i]);
338 
339 	if (usage)
340 		exit(EXIT_FAILURE);
341 
342 	exit(EXIT_SUCCESS);
343 }
344 
345 /*
346  * osnoise_top_parse_args - allocs, parse and fill the cmd line parameters
347  */
348 struct osnoise_top_params *osnoise_top_parse_args(int argc, char **argv)
349 {
350 	struct osnoise_top_params *params;
351 	struct trace_events *tevent;
352 	int retval;
353 	int c;
354 
355 	params = calloc(1, sizeof(*params));
356 	if (!params)
357 		exit(1);
358 
359 	if (strcmp(argv[0], "hwnoise") == 0) {
360 		params->mode = MODE_HWNOISE;
361 		/*
362 		 * Reduce CPU usage for 75% to avoid killing the system.
363 		 */
364 		params->runtime = 750000;
365 		params->period = 1000000;
366 	}
367 
368 	while (1) {
369 		static struct option long_options[] = {
370 			{"auto",		required_argument,	0, 'a'},
371 			{"cpus",		required_argument,	0, 'c'},
372 			{"cgroup",		optional_argument,	0, 'C'},
373 			{"debug",		no_argument,		0, 'D'},
374 			{"duration",		required_argument,	0, 'd'},
375 			{"event",		required_argument,	0, 'e'},
376 			{"house-keeping",	required_argument,	0, 'H'},
377 			{"help",		no_argument,		0, 'h'},
378 			{"period",		required_argument,	0, 'p'},
379 			{"priority",		required_argument,	0, 'P'},
380 			{"quiet",		no_argument,		0, 'q'},
381 			{"runtime",		required_argument,	0, 'r'},
382 			{"stop",		required_argument,	0, 's'},
383 			{"stop-total",		required_argument,	0, 'S'},
384 			{"threshold",		required_argument,	0, 'T'},
385 			{"trace",		optional_argument,	0, 't'},
386 			{"trigger",		required_argument,	0, '0'},
387 			{"filter",		required_argument,	0, '1'},
388 			{"warm-up",		required_argument,	0, '2'},
389 			{"trace-buffer-size",	required_argument,	0, '3'},
390 			{0, 0, 0, 0}
391 		};
392 
393 		/* getopt_long stores the option index here. */
394 		int option_index = 0;
395 
396 		c = getopt_long(argc, argv, "a:c:C::d:De:hH:p:P:qr:s:S:t::T:0:1:2:3:",
397 				 long_options, &option_index);
398 
399 		/* Detect the end of the options. */
400 		if (c == -1)
401 			break;
402 
403 		switch (c) {
404 		case 'a':
405 			/* set sample stop to auto_thresh */
406 			params->stop_us = get_llong_from_str(optarg);
407 
408 			/* set sample threshold to 1 */
409 			params->threshold = 1;
410 
411 			/* set trace */
412 			params->trace_output = "osnoise_trace.txt";
413 
414 			break;
415 		case 'c':
416 			retval = parse_cpu_set(optarg, &params->monitored_cpus);
417 			if (retval)
418 				osnoise_top_usage(params, "\nInvalid -c cpu list\n");
419 			params->cpus = optarg;
420 			break;
421 		case 'C':
422 			params->cgroup = 1;
423 			if (!optarg) {
424 				/* will inherit this cgroup */
425 				params->cgroup_name = NULL;
426 			} else if (*optarg == '=') {
427 				/* skip the = */
428 				params->cgroup_name = ++optarg;
429 			}
430 			break;
431 		case 'D':
432 			config_debug = 1;
433 			break;
434 		case 'd':
435 			params->duration = parse_seconds_duration(optarg);
436 			if (!params->duration)
437 				osnoise_top_usage(params, "Invalid -D duration\n");
438 			break;
439 		case 'e':
440 			tevent = trace_event_alloc(optarg);
441 			if (!tevent) {
442 				err_msg("Error alloc trace event");
443 				exit(EXIT_FAILURE);
444 			}
445 
446 			if (params->events)
447 				tevent->next = params->events;
448 			params->events = tevent;
449 
450 			break;
451 		case 'h':
452 		case '?':
453 			osnoise_top_usage(params, NULL);
454 			break;
455 		case 'H':
456 			params->hk_cpus = 1;
457 			retval = parse_cpu_set(optarg, &params->hk_cpu_set);
458 			if (retval) {
459 				err_msg("Error parsing house keeping CPUs\n");
460 				exit(EXIT_FAILURE);
461 			}
462 			break;
463 		case 'p':
464 			params->period = get_llong_from_str(optarg);
465 			if (params->period > 10000000)
466 				osnoise_top_usage(params, "Period longer than 10 s\n");
467 			break;
468 		case 'P':
469 			retval = parse_prio(optarg, &params->sched_param);
470 			if (retval == -1)
471 				osnoise_top_usage(params, "Invalid -P priority");
472 			params->set_sched = 1;
473 			break;
474 		case 'q':
475 			params->quiet = 1;
476 			break;
477 		case 'r':
478 			params->runtime = get_llong_from_str(optarg);
479 			if (params->runtime < 100)
480 				osnoise_top_usage(params, "Runtime shorter than 100 us\n");
481 			break;
482 		case 's':
483 			params->stop_us = get_llong_from_str(optarg);
484 			break;
485 		case 'S':
486 			params->stop_total_us = get_llong_from_str(optarg);
487 			break;
488 		case 't':
489 			if (optarg) {
490 				if (optarg[0] == '=')
491 					params->trace_output = &optarg[1];
492 				else
493 					params->trace_output = &optarg[0];
494 			} else if (optind < argc && argv[optind][0] != '-')
495 				params->trace_output = argv[optind];
496 			else
497 				params->trace_output = "osnoise_trace.txt";
498 			break;
499 		case 'T':
500 			params->threshold = get_llong_from_str(optarg);
501 			break;
502 		case '0': /* trigger */
503 			if (params->events) {
504 				retval = trace_event_add_trigger(params->events, optarg);
505 				if (retval) {
506 					err_msg("Error adding trigger %s\n", optarg);
507 					exit(EXIT_FAILURE);
508 				}
509 			} else {
510 				osnoise_top_usage(params, "--trigger requires a previous -e\n");
511 			}
512 			break;
513 		case '1': /* filter */
514 			if (params->events) {
515 				retval = trace_event_add_filter(params->events, optarg);
516 				if (retval) {
517 					err_msg("Error adding filter %s\n", optarg);
518 					exit(EXIT_FAILURE);
519 				}
520 			} else {
521 				osnoise_top_usage(params, "--filter requires a previous -e\n");
522 			}
523 			break;
524 		case '2':
525 			params->warmup = get_llong_from_str(optarg);
526 			break;
527 		case '3':
528 			params->buffer_size = get_llong_from_str(optarg);
529 			break;
530 		default:
531 			osnoise_top_usage(params, "Invalid option");
532 		}
533 	}
534 
535 	if (geteuid()) {
536 		err_msg("osnoise needs root permission\n");
537 		exit(EXIT_FAILURE);
538 	}
539 
540 	return params;
541 }
542 
543 /*
544  * osnoise_top_apply_config - apply the top configs to the initialized tool
545  */
546 static int
547 osnoise_top_apply_config(struct osnoise_tool *tool, struct osnoise_top_params *params)
548 {
549 	int retval;
550 
551 	if (!params->sleep_time)
552 		params->sleep_time = 1;
553 
554 	if (params->cpus) {
555 		retval = osnoise_set_cpus(tool->context, params->cpus);
556 		if (retval) {
557 			err_msg("Failed to apply CPUs config\n");
558 			goto out_err;
559 		}
560 	}
561 
562 	if (params->runtime || params->period) {
563 		retval = osnoise_set_runtime_period(tool->context,
564 						    params->runtime,
565 						    params->period);
566 		if (retval) {
567 			err_msg("Failed to set runtime and/or period\n");
568 			goto out_err;
569 		}
570 	}
571 
572 	if (params->stop_us) {
573 		retval = osnoise_set_stop_us(tool->context, params->stop_us);
574 		if (retval) {
575 			err_msg("Failed to set stop us\n");
576 			goto out_err;
577 		}
578 	}
579 
580 	if (params->stop_total_us) {
581 		retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us);
582 		if (retval) {
583 			err_msg("Failed to set stop total us\n");
584 			goto out_err;
585 		}
586 	}
587 
588 	if (params->threshold) {
589 		retval = osnoise_set_tracing_thresh(tool->context, params->threshold);
590 		if (retval) {
591 			err_msg("Failed to set tracing_thresh\n");
592 			goto out_err;
593 		}
594 	}
595 
596 	if (params->mode == MODE_HWNOISE) {
597 		retval = osnoise_set_irq_disable(tool->context, 1);
598 		if (retval) {
599 			err_msg("Failed to set OSNOISE_IRQ_DISABLE option\n");
600 			goto out_err;
601 		}
602 	}
603 
604 	if (params->hk_cpus) {
605 		retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set),
606 					   &params->hk_cpu_set);
607 		if (retval == -1) {
608 			err_msg("Failed to set rtla to the house keeping CPUs\n");
609 			goto out_err;
610 		}
611 	} else if (params->cpus) {
612 		/*
613 		 * Even if the user do not set a house-keeping CPU, try to
614 		 * move rtla to a CPU set different to the one where the user
615 		 * set the workload to run.
616 		 *
617 		 * No need to check results as this is an automatic attempt.
618 		 */
619 		auto_house_keeping(&params->monitored_cpus);
620 	}
621 
622 	return 0;
623 
624 out_err:
625 	return -1;
626 }
627 
628 /*
629  * osnoise_init_top - initialize a osnoise top tool with parameters
630  */
631 struct osnoise_tool *osnoise_init_top(struct osnoise_top_params *params)
632 {
633 	struct osnoise_tool *tool;
634 	int nr_cpus;
635 
636 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
637 
638 	tool = osnoise_init_tool("osnoise_top");
639 	if (!tool)
640 		return NULL;
641 
642 	tool->data = osnoise_alloc_top(nr_cpus);
643 	if (!tool->data)
644 		goto out_err;
645 
646 	tool->params = params;
647 
648 	tep_register_event_handler(tool->trace.tep, -1, "ftrace", "osnoise",
649 				   osnoise_top_handler, NULL);
650 
651 	return tool;
652 
653 out_err:
654 	osnoise_free_top(tool->data);
655 	osnoise_destroy_tool(tool);
656 	return NULL;
657 }
658 
659 static int stop_tracing;
660 static void stop_top(int sig)
661 {
662 	stop_tracing = 1;
663 }
664 
665 /*
666  * osnoise_top_set_signals - handles the signal to stop the tool
667  */
668 static void osnoise_top_set_signals(struct osnoise_top_params *params)
669 {
670 	signal(SIGINT, stop_top);
671 	if (params->duration) {
672 		signal(SIGALRM, stop_top);
673 		alarm(params->duration);
674 	}
675 }
676 
677 int osnoise_top_main(int argc, char **argv)
678 {
679 	struct osnoise_top_params *params;
680 	struct osnoise_tool *record = NULL;
681 	struct osnoise_tool *tool = NULL;
682 	struct trace_instance *trace;
683 	int return_value = 1;
684 	int retval;
685 
686 	params = osnoise_top_parse_args(argc, argv);
687 	if (!params)
688 		exit(1);
689 
690 	tool = osnoise_init_top(params);
691 	if (!tool) {
692 		err_msg("Could not init osnoise top\n");
693 		goto out_exit;
694 	}
695 
696 	retval = osnoise_top_apply_config(tool, params);
697 	if (retval) {
698 		err_msg("Could not apply config\n");
699 		goto out_free;
700 	}
701 
702 	trace = &tool->trace;
703 
704 	retval = enable_osnoise(trace);
705 	if (retval) {
706 		err_msg("Failed to enable osnoise tracer\n");
707 		goto out_free;
708 	}
709 
710 	if (params->set_sched) {
711 		retval = set_comm_sched_attr("osnoise/", &params->sched_param);
712 		if (retval) {
713 			err_msg("Failed to set sched parameters\n");
714 			goto out_free;
715 		}
716 	}
717 
718 	if (params->cgroup) {
719 		retval = set_comm_cgroup("osnoise/", params->cgroup_name);
720 		if (!retval) {
721 			err_msg("Failed to move threads to cgroup\n");
722 			goto out_free;
723 		}
724 	}
725 
726 	if (params->trace_output) {
727 		record = osnoise_init_trace_tool("osnoise");
728 		if (!record) {
729 			err_msg("Failed to enable the trace instance\n");
730 			goto out_free;
731 		}
732 
733 		if (params->events) {
734 			retval = trace_events_enable(&record->trace, params->events);
735 			if (retval)
736 				goto out_top;
737 		}
738 
739 		if (params->buffer_size > 0) {
740 			retval = trace_set_buffer_size(&record->trace, params->buffer_size);
741 			if (retval)
742 				goto out_top;
743 		}
744 	}
745 
746 	/*
747 	 * Start the tracer here, after having set all instances.
748 	 *
749 	 * Let the trace instance start first for the case of hitting a stop
750 	 * tracing while enabling other instances. The trace instance is the
751 	 * one with most valuable information.
752 	 */
753 	if (params->trace_output)
754 		trace_instance_start(&record->trace);
755 	trace_instance_start(trace);
756 
757 	if (params->warmup > 0) {
758 		debug_msg("Warming up for %d seconds\n", params->warmup);
759 		sleep(params->warmup);
760 		if (stop_tracing)
761 			goto out_top;
762 
763 		/*
764 		 * Clean up the buffer. The osnoise workload do not run
765 		 * with tracing off to avoid creating a performance penalty
766 		 * when not needed.
767 		 */
768 		retval = tracefs_instance_file_write(trace->inst, "trace", "");
769 		if (retval < 0) {
770 			debug_msg("Error cleaning up the buffer");
771 			goto out_top;
772 		}
773 
774 	}
775 
776 	tool->start_time = time(NULL);
777 	osnoise_top_set_signals(params);
778 
779 	while (!stop_tracing) {
780 		sleep(params->sleep_time);
781 
782 		retval = tracefs_iterate_raw_events(trace->tep,
783 						    trace->inst,
784 						    NULL,
785 						    0,
786 						    collect_registered_events,
787 						    trace);
788 		if (retval < 0) {
789 			err_msg("Error iterating on events\n");
790 			goto out_top;
791 		}
792 
793 		if (!params->quiet)
794 			osnoise_print_stats(params, tool);
795 
796 		if (trace_is_off(&tool->trace, &record->trace))
797 			break;
798 
799 	}
800 
801 	osnoise_print_stats(params, tool);
802 
803 	return_value = 0;
804 
805 	if (trace_is_off(&tool->trace, &record->trace)) {
806 		printf("osnoise hit stop tracing\n");
807 		if (params->trace_output) {
808 			printf("  Saving trace to %s\n", params->trace_output);
809 			save_trace_to_file(record->trace.inst, params->trace_output);
810 		}
811 	}
812 
813 out_top:
814 	trace_events_destroy(&record->trace, params->events);
815 	params->events = NULL;
816 out_free:
817 	osnoise_free_top(tool->data);
818 	osnoise_destroy_tool(record);
819 	osnoise_destroy_tool(tool);
820 	free(params);
821 out_exit:
822 	exit(return_value);
823 }
824