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 		goto out_err;
629 
630 	tool->params = params;
631 
632 	tep_register_event_handler(tool->trace.tep, -1, "ftrace", "osnoise",
633 				   osnoise_top_handler, NULL);
634 
635 	return tool;
636 
637 out_err:
638 	osnoise_free_top(tool->data);
639 	osnoise_destroy_tool(tool);
640 	return NULL;
641 }
642 
643 static int stop_tracing;
644 static void stop_top(int sig)
645 {
646 	stop_tracing = 1;
647 }
648 
649 /*
650  * osnoise_top_set_signals - handles the signal to stop the tool
651  */
652 static void osnoise_top_set_signals(struct osnoise_top_params *params)
653 {
654 	signal(SIGINT, stop_top);
655 	if (params->duration) {
656 		signal(SIGALRM, stop_top);
657 		alarm(params->duration);
658 	}
659 }
660 
661 int osnoise_top_main(int argc, char **argv)
662 {
663 	struct osnoise_top_params *params;
664 	struct osnoise_tool *record = NULL;
665 	struct osnoise_tool *tool = NULL;
666 	struct trace_instance *trace;
667 	int return_value = 1;
668 	int retval;
669 
670 	params = osnoise_top_parse_args(argc, argv);
671 	if (!params)
672 		exit(1);
673 
674 	tool = osnoise_init_top(params);
675 	if (!tool) {
676 		err_msg("Could not init osnoise top\n");
677 		goto out_exit;
678 	}
679 
680 	retval = osnoise_top_apply_config(tool, params);
681 	if (retval) {
682 		err_msg("Could not apply config\n");
683 		goto out_free;
684 	}
685 
686 	trace = &tool->trace;
687 
688 	retval = enable_osnoise(trace);
689 	if (retval) {
690 		err_msg("Failed to enable osnoise tracer\n");
691 		goto out_free;
692 	}
693 
694 	if (params->set_sched) {
695 		retval = set_comm_sched_attr("osnoise/", &params->sched_param);
696 		if (retval) {
697 			err_msg("Failed to set sched parameters\n");
698 			goto out_free;
699 		}
700 	}
701 
702 	if (params->cgroup) {
703 		retval = set_comm_cgroup("osnoise/", params->cgroup_name);
704 		if (!retval) {
705 			err_msg("Failed to move threads to cgroup\n");
706 			goto out_free;
707 		}
708 	}
709 
710 	if (params->trace_output) {
711 		record = osnoise_init_trace_tool("osnoise");
712 		if (!record) {
713 			err_msg("Failed to enable the trace instance\n");
714 			goto out_free;
715 		}
716 
717 		if (params->events) {
718 			retval = trace_events_enable(&record->trace, params->events);
719 			if (retval)
720 				goto out_top;
721 		}
722 	}
723 
724 	/*
725 	 * Start the tracer here, after having set all instances.
726 	 *
727 	 * Let the trace instance start first for the case of hitting a stop
728 	 * tracing while enabling other instances. The trace instance is the
729 	 * one with most valuable information.
730 	 */
731 	if (params->trace_output)
732 		trace_instance_start(&record->trace);
733 	trace_instance_start(trace);
734 
735 	tool->start_time = time(NULL);
736 	osnoise_top_set_signals(params);
737 
738 	while (!stop_tracing) {
739 		sleep(params->sleep_time);
740 
741 		retval = tracefs_iterate_raw_events(trace->tep,
742 						    trace->inst,
743 						    NULL,
744 						    0,
745 						    collect_registered_events,
746 						    trace);
747 		if (retval < 0) {
748 			err_msg("Error iterating on events\n");
749 			goto out_top;
750 		}
751 
752 		if (!params->quiet)
753 			osnoise_print_stats(params, tool);
754 
755 		if (trace_is_off(&tool->trace, &record->trace))
756 			break;
757 
758 	}
759 
760 	osnoise_print_stats(params, tool);
761 
762 	return_value = 0;
763 
764 	if (trace_is_off(&tool->trace, &record->trace)) {
765 		printf("osnoise hit stop tracing\n");
766 		if (params->trace_output) {
767 			printf("  Saving trace to %s\n", params->trace_output);
768 			save_trace_to_file(record->trace.inst, params->trace_output);
769 		}
770 	}
771 
772 out_top:
773 	trace_events_destroy(&record->trace, params->events);
774 	params->events = NULL;
775 out_free:
776 	osnoise_free_top(tool->data);
777 	osnoise_destroy_tool(record);
778 	osnoise_destroy_tool(tool);
779 	free(params);
780 out_exit:
781 	exit(return_value);
782 }
783