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