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 
335 	if (usage)
336 		exit(EXIT_FAILURE);
337 
338 	exit(EXIT_SUCCESS);
339 }
340 
341 /*
342  * osnoise_top_parse_args - allocs, parse and fill the cmd line parameters
343  */
344 struct osnoise_top_params *osnoise_top_parse_args(int argc, char **argv)
345 {
346 	struct osnoise_top_params *params;
347 	struct trace_events *tevent;
348 	int retval;
349 	int c;
350 
351 	params = calloc(1, sizeof(*params));
352 	if (!params)
353 		exit(1);
354 
355 	if (strcmp(argv[0], "hwnoise") == 0) {
356 		params->mode = MODE_HWNOISE;
357 		/*
358 		 * Reduce CPU usage for 75% to avoid killing the system.
359 		 */
360 		params->runtime = 750000;
361 		params->period = 1000000;
362 	}
363 
364 	while (1) {
365 		static struct option long_options[] = {
366 			{"auto",		required_argument,	0, 'a'},
367 			{"cpus",		required_argument,	0, 'c'},
368 			{"cgroup",		optional_argument,	0, 'C'},
369 			{"debug",		no_argument,		0, 'D'},
370 			{"duration",		required_argument,	0, 'd'},
371 			{"event",		required_argument,	0, 'e'},
372 			{"house-keeping",	required_argument,	0, 'H'},
373 			{"help",		no_argument,		0, 'h'},
374 			{"period",		required_argument,	0, 'p'},
375 			{"priority",		required_argument,	0, 'P'},
376 			{"quiet",		no_argument,		0, 'q'},
377 			{"runtime",		required_argument,	0, 'r'},
378 			{"stop",		required_argument,	0, 's'},
379 			{"stop-total",		required_argument,	0, 'S'},
380 			{"threshold",		required_argument,	0, 'T'},
381 			{"trace",		optional_argument,	0, 't'},
382 			{"trigger",		required_argument,	0, '0'},
383 			{"filter",		required_argument,	0, '1'},
384 			{0, 0, 0, 0}
385 		};
386 
387 		/* getopt_long stores the option index here. */
388 		int option_index = 0;
389 
390 		c = getopt_long(argc, argv, "a:c:C::d:De:hH:p:P:qr:s:S:t::T:0:1:",
391 				 long_options, &option_index);
392 
393 		/* Detect the end of the options. */
394 		if (c == -1)
395 			break;
396 
397 		switch (c) {
398 		case 'a':
399 			/* set sample stop to auto_thresh */
400 			params->stop_us = get_llong_from_str(optarg);
401 
402 			/* set sample threshold to 1 */
403 			params->threshold = 1;
404 
405 			/* set trace */
406 			params->trace_output = "osnoise_trace.txt";
407 
408 			break;
409 		case 'c':
410 			retval = parse_cpu_set(optarg, &params->monitored_cpus);
411 			if (retval)
412 				osnoise_top_usage(params, "\nInvalid -c cpu list\n");
413 			params->cpus = optarg;
414 			break;
415 		case 'C':
416 			params->cgroup = 1;
417 			if (!optarg) {
418 				/* will inherit this cgroup */
419 				params->cgroup_name = NULL;
420 			} else if (*optarg == '=') {
421 				/* skip the = */
422 				params->cgroup_name = ++optarg;
423 			}
424 			break;
425 		case 'D':
426 			config_debug = 1;
427 			break;
428 		case 'd':
429 			params->duration = parse_seconds_duration(optarg);
430 			if (!params->duration)
431 				osnoise_top_usage(params, "Invalid -d duration\n");
432 			break;
433 		case 'e':
434 			tevent = trace_event_alloc(optarg);
435 			if (!tevent) {
436 				err_msg("Error alloc trace event");
437 				exit(EXIT_FAILURE);
438 			}
439 
440 			if (params->events)
441 				tevent->next = params->events;
442 			params->events = tevent;
443 
444 			break;
445 		case 'h':
446 		case '?':
447 			osnoise_top_usage(params, NULL);
448 			break;
449 		case 'H':
450 			params->hk_cpus = 1;
451 			retval = parse_cpu_set(optarg, &params->hk_cpu_set);
452 			if (retval) {
453 				err_msg("Error parsing house keeping CPUs\n");
454 				exit(EXIT_FAILURE);
455 			}
456 			break;
457 		case 'p':
458 			params->period = get_llong_from_str(optarg);
459 			if (params->period > 10000000)
460 				osnoise_top_usage(params, "Period longer than 10 s\n");
461 			break;
462 		case 'P':
463 			retval = parse_prio(optarg, &params->sched_param);
464 			if (retval == -1)
465 				osnoise_top_usage(params, "Invalid -P priority");
466 			params->set_sched = 1;
467 			break;
468 		case 'q':
469 			params->quiet = 1;
470 			break;
471 		case 'r':
472 			params->runtime = get_llong_from_str(optarg);
473 			if (params->runtime < 100)
474 				osnoise_top_usage(params, "Runtime shorter than 100 us\n");
475 			break;
476 		case 's':
477 			params->stop_us = get_llong_from_str(optarg);
478 			break;
479 		case 'S':
480 			params->stop_total_us = get_llong_from_str(optarg);
481 			break;
482 		case 't':
483 			if (optarg)
484 				/* skip = */
485 				params->trace_output = &optarg[1];
486 			else
487 				params->trace_output = "osnoise_trace.txt";
488 			break;
489 		case 'T':
490 			params->threshold = get_llong_from_str(optarg);
491 			break;
492 		case '0': /* trigger */
493 			if (params->events) {
494 				retval = trace_event_add_trigger(params->events, optarg);
495 				if (retval) {
496 					err_msg("Error adding trigger %s\n", optarg);
497 					exit(EXIT_FAILURE);
498 				}
499 			} else {
500 				osnoise_top_usage(params, "--trigger requires a previous -e\n");
501 			}
502 			break;
503 		case '1': /* filter */
504 			if (params->events) {
505 				retval = trace_event_add_filter(params->events, optarg);
506 				if (retval) {
507 					err_msg("Error adding filter %s\n", optarg);
508 					exit(EXIT_FAILURE);
509 				}
510 			} else {
511 				osnoise_top_usage(params, "--filter requires a previous -e\n");
512 			}
513 			break;
514 		default:
515 			osnoise_top_usage(params, "Invalid option");
516 		}
517 	}
518 
519 	if (geteuid()) {
520 		err_msg("osnoise needs root permission\n");
521 		exit(EXIT_FAILURE);
522 	}
523 
524 	return params;
525 }
526 
527 /*
528  * osnoise_top_apply_config - apply the top configs to the initialized tool
529  */
530 static int
531 osnoise_top_apply_config(struct osnoise_tool *tool, struct osnoise_top_params *params)
532 {
533 	int retval;
534 
535 	if (!params->sleep_time)
536 		params->sleep_time = 1;
537 
538 	if (params->cpus) {
539 		retval = osnoise_set_cpus(tool->context, params->cpus);
540 		if (retval) {
541 			err_msg("Failed to apply CPUs config\n");
542 			goto out_err;
543 		}
544 	}
545 
546 	if (params->runtime || params->period) {
547 		retval = osnoise_set_runtime_period(tool->context,
548 						    params->runtime,
549 						    params->period);
550 		if (retval) {
551 			err_msg("Failed to set runtime and/or period\n");
552 			goto out_err;
553 		}
554 	}
555 
556 	if (params->stop_us) {
557 		retval = osnoise_set_stop_us(tool->context, params->stop_us);
558 		if (retval) {
559 			err_msg("Failed to set stop us\n");
560 			goto out_err;
561 		}
562 	}
563 
564 	if (params->stop_total_us) {
565 		retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us);
566 		if (retval) {
567 			err_msg("Failed to set stop total us\n");
568 			goto out_err;
569 		}
570 	}
571 
572 	if (params->threshold) {
573 		retval = osnoise_set_tracing_thresh(tool->context, params->threshold);
574 		if (retval) {
575 			err_msg("Failed to set tracing_thresh\n");
576 			goto out_err;
577 		}
578 	}
579 
580 	if (params->mode == MODE_HWNOISE) {
581 		retval = osnoise_set_irq_disable(tool->context, 1);
582 		if (retval) {
583 			err_msg("Failed to set OSNOISE_IRQ_DISABLE option\n");
584 			goto out_err;
585 		}
586 	}
587 
588 	if (params->hk_cpus) {
589 		retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set),
590 					   &params->hk_cpu_set);
591 		if (retval == -1) {
592 			err_msg("Failed to set rtla to the house keeping CPUs\n");
593 			goto out_err;
594 		}
595 	} else if (params->cpus) {
596 		/*
597 		 * Even if the user do not set a house-keeping CPU, try to
598 		 * move rtla to a CPU set different to the one where the user
599 		 * set the workload to run.
600 		 *
601 		 * No need to check results as this is an automatic attempt.
602 		 */
603 		auto_house_keeping(&params->monitored_cpus);
604 	}
605 
606 	return 0;
607 
608 out_err:
609 	return -1;
610 }
611 
612 /*
613  * osnoise_init_top - initialize a osnoise top tool with parameters
614  */
615 struct osnoise_tool *osnoise_init_top(struct osnoise_top_params *params)
616 {
617 	struct osnoise_tool *tool;
618 	int nr_cpus;
619 
620 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
621 
622 	tool = osnoise_init_tool("osnoise_top");
623 	if (!tool)
624 		return NULL;
625 
626 	tool->data = osnoise_alloc_top(nr_cpus);
627 	if (!tool->data) {
628 		osnoise_destroy_tool(tool);
629 		return NULL;
630 	}
631 
632 	tool->params = params;
633 
634 	tep_register_event_handler(tool->trace.tep, -1, "ftrace", "osnoise",
635 				   osnoise_top_handler, NULL);
636 
637 	return tool;
638 }
639 
640 static int stop_tracing;
641 static void stop_top(int sig)
642 {
643 	stop_tracing = 1;
644 }
645 
646 /*
647  * osnoise_top_set_signals - handles the signal to stop the tool
648  */
649 static void osnoise_top_set_signals(struct osnoise_top_params *params)
650 {
651 	signal(SIGINT, stop_top);
652 	if (params->duration) {
653 		signal(SIGALRM, stop_top);
654 		alarm(params->duration);
655 	}
656 }
657 
658 int osnoise_top_main(int argc, char **argv)
659 {
660 	struct osnoise_top_params *params;
661 	struct osnoise_tool *record = NULL;
662 	struct osnoise_tool *tool = NULL;
663 	struct trace_instance *trace;
664 	int return_value = 1;
665 	int retval;
666 
667 	params = osnoise_top_parse_args(argc, argv);
668 	if (!params)
669 		exit(1);
670 
671 	tool = osnoise_init_top(params);
672 	if (!tool) {
673 		err_msg("Could not init osnoise top\n");
674 		goto out_exit;
675 	}
676 
677 	retval = osnoise_top_apply_config(tool, params);
678 	if (retval) {
679 		err_msg("Could not apply config\n");
680 		goto out_free;
681 	}
682 
683 	trace = &tool->trace;
684 
685 	retval = enable_osnoise(trace);
686 	if (retval) {
687 		err_msg("Failed to enable osnoise tracer\n");
688 		goto out_free;
689 	}
690 
691 	if (params->set_sched) {
692 		retval = set_comm_sched_attr("osnoise/", &params->sched_param);
693 		if (retval) {
694 			err_msg("Failed to set sched parameters\n");
695 			goto out_free;
696 		}
697 	}
698 
699 	if (params->cgroup) {
700 		retval = set_comm_cgroup("osnoise/", params->cgroup_name);
701 		if (!retval) {
702 			err_msg("Failed to move threads to cgroup\n");
703 			goto out_free;
704 		}
705 	}
706 
707 	if (params->trace_output) {
708 		record = osnoise_init_trace_tool("osnoise");
709 		if (!record) {
710 			err_msg("Failed to enable the trace instance\n");
711 			goto out_free;
712 		}
713 
714 		if (params->events) {
715 			retval = trace_events_enable(&record->trace, params->events);
716 			if (retval)
717 				goto out_top;
718 		}
719 	}
720 
721 	/*
722 	 * Start the tracer here, after having set all instances.
723 	 *
724 	 * Let the trace instance start first for the case of hitting a stop
725 	 * tracing while enabling other instances. The trace instance is the
726 	 * one with most valuable information.
727 	 */
728 	if (params->trace_output)
729 		trace_instance_start(&record->trace);
730 	trace_instance_start(trace);
731 
732 	tool->start_time = time(NULL);
733 	osnoise_top_set_signals(params);
734 
735 	while (!stop_tracing) {
736 		sleep(params->sleep_time);
737 
738 		retval = tracefs_iterate_raw_events(trace->tep,
739 						    trace->inst,
740 						    NULL,
741 						    0,
742 						    collect_registered_events,
743 						    trace);
744 		if (retval < 0) {
745 			err_msg("Error iterating on events\n");
746 			goto out_top;
747 		}
748 
749 		if (!params->quiet)
750 			osnoise_print_stats(params, tool);
751 
752 		if (trace_is_off(&tool->trace, &record->trace))
753 			break;
754 
755 	}
756 
757 	osnoise_print_stats(params, tool);
758 
759 	return_value = 0;
760 
761 	if (trace_is_off(&tool->trace, &record->trace)) {
762 		printf("osnoise hit stop tracing\n");
763 		if (params->trace_output) {
764 			printf("  Saving trace to %s\n", params->trace_output);
765 			save_trace_to_file(record->trace.inst, params->trace_output);
766 		}
767 	}
768 
769 out_top:
770 	trace_events_destroy(&record->trace, params->events);
771 	params->events = NULL;
772 out_free:
773 	osnoise_free_top(tool->data);
774 	osnoise_destroy_tool(record);
775 	osnoise_destroy_tool(tool);
776 	free(params);
777 out_exit:
778 	exit(return_value);
779 }
780