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