xref: /linux/tools/tracing/rtla/src/osnoise_hist.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1829a6c0bSDaniel Bristot de Oliveira // SPDX-License-Identifier: GPL-2.0
2829a6c0bSDaniel Bristot de Oliveira /*
3829a6c0bSDaniel Bristot de Oliveira  * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
4829a6c0bSDaniel Bristot de Oliveira  */
5829a6c0bSDaniel Bristot de Oliveira 
6272ced25SDaniel Bristot de Oliveira #define _GNU_SOURCE
7829a6c0bSDaniel Bristot de Oliveira #include <getopt.h>
8829a6c0bSDaniel Bristot de Oliveira #include <stdlib.h>
9829a6c0bSDaniel Bristot de Oliveira #include <string.h>
10829a6c0bSDaniel Bristot de Oliveira #include <signal.h>
11829a6c0bSDaniel Bristot de Oliveira #include <unistd.h>
12829a6c0bSDaniel Bristot de Oliveira #include <errno.h>
13829a6c0bSDaniel Bristot de Oliveira #include <stdio.h>
14829a6c0bSDaniel Bristot de Oliveira #include <time.h>
15272ced25SDaniel Bristot de Oliveira #include <sched.h>
16829a6c0bSDaniel Bristot de Oliveira 
17829a6c0bSDaniel Bristot de Oliveira #include "utils.h"
18829a6c0bSDaniel Bristot de Oliveira #include "osnoise.h"
19829a6c0bSDaniel Bristot de Oliveira 
20829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_params {
21829a6c0bSDaniel Bristot de Oliveira 	char			*cpus;
22894c29c7SDaniel Bristot de Oliveira 	cpu_set_t		monitored_cpus;
23829a6c0bSDaniel Bristot de Oliveira 	char			*trace_output;
24a957cbc0SDaniel Bristot de Oliveira 	char			*cgroup_name;
25829a6c0bSDaniel Bristot de Oliveira 	unsigned long long	runtime;
26829a6c0bSDaniel Bristot de Oliveira 	unsigned long long	period;
27d635316aSDaniel Bristot de Oliveira 	long long		threshold;
28829a6c0bSDaniel Bristot de Oliveira 	long long		stop_us;
29829a6c0bSDaniel Bristot de Oliveira 	long long		stop_total_us;
30829a6c0bSDaniel Bristot de Oliveira 	int			sleep_time;
31829a6c0bSDaniel Bristot de Oliveira 	int			duration;
32829a6c0bSDaniel Bristot de Oliveira 	int			set_sched;
33829a6c0bSDaniel Bristot de Oliveira 	int			output_divisor;
34a957cbc0SDaniel Bristot de Oliveira 	int			cgroup;
35272ced25SDaniel Bristot de Oliveira 	int			hk_cpus;
36272ced25SDaniel Bristot de Oliveira 	cpu_set_t		hk_cpu_set;
37829a6c0bSDaniel Bristot de Oliveira 	struct sched_attr	sched_param;
3851d64c3aSDaniel Bristot de Oliveira 	struct trace_events	*events;
39829a6c0bSDaniel Bristot de Oliveira 	char			no_header;
40829a6c0bSDaniel Bristot de Oliveira 	char			no_summary;
41829a6c0bSDaniel Bristot de Oliveira 	char			no_index;
42829a6c0bSDaniel Bristot de Oliveira 	char			with_zeros;
43829a6c0bSDaniel Bristot de Oliveira 	int			bucket_size;
44829a6c0bSDaniel Bristot de Oliveira 	int			entries;
45cdbf7196SDaniel Bristot de Oliveira 	int			warmup;
46e9a4062eSDaniel Bristot de Oliveira 	int			buffer_size;
47829a6c0bSDaniel Bristot de Oliveira };
48829a6c0bSDaniel Bristot de Oliveira 
49829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_cpu {
50829a6c0bSDaniel Bristot de Oliveira 	int			*samples;
51829a6c0bSDaniel Bristot de Oliveira 	int			count;
52829a6c0bSDaniel Bristot de Oliveira 
53829a6c0bSDaniel Bristot de Oliveira 	unsigned long long	min_sample;
54829a6c0bSDaniel Bristot de Oliveira 	unsigned long long	sum_sample;
55829a6c0bSDaniel Bristot de Oliveira 	unsigned long long	max_sample;
56829a6c0bSDaniel Bristot de Oliveira 
57829a6c0bSDaniel Bristot de Oliveira };
58829a6c0bSDaniel Bristot de Oliveira 
59829a6c0bSDaniel Bristot de Oliveira struct osnoise_hist_data {
60829a6c0bSDaniel Bristot de Oliveira 	struct tracefs_hist	*trace_hist;
61829a6c0bSDaniel Bristot de Oliveira 	struct osnoise_hist_cpu	*hist;
62829a6c0bSDaniel Bristot de Oliveira 	int			entries;
63829a6c0bSDaniel Bristot de Oliveira 	int			bucket_size;
64829a6c0bSDaniel Bristot de Oliveira 	int			nr_cpus;
65829a6c0bSDaniel Bristot de Oliveira };
66829a6c0bSDaniel Bristot de Oliveira 
67829a6c0bSDaniel Bristot de Oliveira /*
68829a6c0bSDaniel Bristot de Oliveira  * osnoise_free_histogram - free runtime data
69829a6c0bSDaniel Bristot de Oliveira  */
70829a6c0bSDaniel Bristot de Oliveira static void
osnoise_free_histogram(struct osnoise_hist_data * data)71829a6c0bSDaniel Bristot de Oliveira osnoise_free_histogram(struct osnoise_hist_data *data)
72829a6c0bSDaniel Bristot de Oliveira {
73829a6c0bSDaniel Bristot de Oliveira 	int cpu;
74829a6c0bSDaniel Bristot de Oliveira 
75829a6c0bSDaniel Bristot de Oliveira 	/* one histogram for IRQ and one for thread, per CPU */
76829a6c0bSDaniel Bristot de Oliveira 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
77829a6c0bSDaniel Bristot de Oliveira 		if (data->hist[cpu].samples)
78829a6c0bSDaniel Bristot de Oliveira 			free(data->hist[cpu].samples);
79829a6c0bSDaniel Bristot de Oliveira 	}
80829a6c0bSDaniel Bristot de Oliveira 
81829a6c0bSDaniel Bristot de Oliveira 	/* one set of histograms per CPU */
82829a6c0bSDaniel Bristot de Oliveira 	if (data->hist)
83829a6c0bSDaniel Bristot de Oliveira 		free(data->hist);
84829a6c0bSDaniel Bristot de Oliveira 
85829a6c0bSDaniel Bristot de Oliveira 	free(data);
86829a6c0bSDaniel Bristot de Oliveira }
87829a6c0bSDaniel Bristot de Oliveira 
88829a6c0bSDaniel Bristot de Oliveira /*
89829a6c0bSDaniel Bristot de Oliveira  * osnoise_alloc_histogram - alloc runtime data
90829a6c0bSDaniel Bristot de Oliveira  */
91829a6c0bSDaniel Bristot de Oliveira static struct osnoise_hist_data
osnoise_alloc_histogram(int nr_cpus,int entries,int bucket_size)92829a6c0bSDaniel Bristot de Oliveira *osnoise_alloc_histogram(int nr_cpus, int entries, int bucket_size)
93829a6c0bSDaniel Bristot de Oliveira {
94829a6c0bSDaniel Bristot de Oliveira 	struct osnoise_hist_data *data;
95829a6c0bSDaniel Bristot de Oliveira 	int cpu;
96829a6c0bSDaniel Bristot de Oliveira 
97829a6c0bSDaniel Bristot de Oliveira 	data = calloc(1, sizeof(*data));
98829a6c0bSDaniel Bristot de Oliveira 	if (!data)
99829a6c0bSDaniel Bristot de Oliveira 		return NULL;
100829a6c0bSDaniel Bristot de Oliveira 
101829a6c0bSDaniel Bristot de Oliveira 	data->entries = entries;
102829a6c0bSDaniel Bristot de Oliveira 	data->bucket_size = bucket_size;
103829a6c0bSDaniel Bristot de Oliveira 	data->nr_cpus = nr_cpus;
104829a6c0bSDaniel Bristot de Oliveira 
105829a6c0bSDaniel Bristot de Oliveira 	data->hist = calloc(1, sizeof(*data->hist) * nr_cpus);
106829a6c0bSDaniel Bristot de Oliveira 	if (!data->hist)
107829a6c0bSDaniel Bristot de Oliveira 		goto cleanup;
108829a6c0bSDaniel Bristot de Oliveira 
109829a6c0bSDaniel Bristot de Oliveira 	for (cpu = 0; cpu < nr_cpus; cpu++) {
110829a6c0bSDaniel Bristot de Oliveira 		data->hist[cpu].samples = calloc(1, sizeof(*data->hist->samples) * (entries + 1));
111829a6c0bSDaniel Bristot de Oliveira 		if (!data->hist[cpu].samples)
112829a6c0bSDaniel Bristot de Oliveira 			goto cleanup;
113829a6c0bSDaniel Bristot de Oliveira 	}
114829a6c0bSDaniel Bristot de Oliveira 
115829a6c0bSDaniel Bristot de Oliveira 	/* set the min to max */
116829a6c0bSDaniel Bristot de Oliveira 	for (cpu = 0; cpu < nr_cpus; cpu++)
117829a6c0bSDaniel Bristot de Oliveira 		data->hist[cpu].min_sample = ~0;
118829a6c0bSDaniel Bristot de Oliveira 
119829a6c0bSDaniel Bristot de Oliveira 	return data;
120829a6c0bSDaniel Bristot de Oliveira 
121829a6c0bSDaniel Bristot de Oliveira cleanup:
122829a6c0bSDaniel Bristot de Oliveira 	osnoise_free_histogram(data);
123829a6c0bSDaniel Bristot de Oliveira 	return NULL;
124829a6c0bSDaniel Bristot de Oliveira }
125829a6c0bSDaniel Bristot de Oliveira 
osnoise_hist_update_multiple(struct osnoise_tool * tool,int cpu,unsigned long long duration,int count)126829a6c0bSDaniel Bristot de Oliveira static void osnoise_hist_update_multiple(struct osnoise_tool *tool, int cpu,
127829a6c0bSDaniel Bristot de Oliveira 					 unsigned long long duration, int count)
128829a6c0bSDaniel Bristot de Oliveira {
129829a6c0bSDaniel Bristot de Oliveira 	struct osnoise_hist_params *params = tool->params;
130829a6c0bSDaniel Bristot de Oliveira 	struct osnoise_hist_data *data = tool->data;
131fe137a4fSAndreas Ziegler 	unsigned long long total_duration;
132829a6c0bSDaniel Bristot de Oliveira 	int entries = data->entries;
133829a6c0bSDaniel Bristot de Oliveira 	int bucket;
134829a6c0bSDaniel Bristot de Oliveira 	int *hist;
135829a6c0bSDaniel Bristot de Oliveira 
136829a6c0bSDaniel Bristot de Oliveira 	if (params->output_divisor)
137829a6c0bSDaniel Bristot de Oliveira 		duration = duration / params->output_divisor;
138829a6c0bSDaniel Bristot de Oliveira 
139829a6c0bSDaniel Bristot de Oliveira 	bucket = duration / data->bucket_size;
140829a6c0bSDaniel Bristot de Oliveira 
141fe137a4fSAndreas Ziegler 	total_duration = duration * count;
142fe137a4fSAndreas Ziegler 
143829a6c0bSDaniel Bristot de Oliveira 	hist = data->hist[cpu].samples;
144829a6c0bSDaniel Bristot de Oliveira 	data->hist[cpu].count += count;
145829a6c0bSDaniel Bristot de Oliveira 	update_min(&data->hist[cpu].min_sample, &duration);
146fe137a4fSAndreas Ziegler 	update_sum(&data->hist[cpu].sum_sample, &total_duration);
147829a6c0bSDaniel Bristot de Oliveira 	update_max(&data->hist[cpu].max_sample, &duration);
148829a6c0bSDaniel Bristot de Oliveira 
149829a6c0bSDaniel Bristot de Oliveira 	if (bucket < entries)
150829a6c0bSDaniel Bristot de Oliveira 		hist[bucket] += count;
151829a6c0bSDaniel Bristot de Oliveira 	else
152829a6c0bSDaniel Bristot de Oliveira 		hist[entries] += count;
153829a6c0bSDaniel Bristot de Oliveira }
154829a6c0bSDaniel Bristot de Oliveira 
155829a6c0bSDaniel Bristot de Oliveira /*
156829a6c0bSDaniel Bristot de Oliveira  * osnoise_destroy_trace_hist - disable events used to collect histogram
157829a6c0bSDaniel Bristot de Oliveira  */
osnoise_destroy_trace_hist(struct osnoise_tool * tool)158829a6c0bSDaniel Bristot de Oliveira static void osnoise_destroy_trace_hist(struct osnoise_tool *tool)
159829a6c0bSDaniel Bristot de Oliveira {
160829a6c0bSDaniel Bristot de Oliveira 	struct osnoise_hist_data *data = tool->data;
161829a6c0bSDaniel Bristot de Oliveira 
162829a6c0bSDaniel Bristot de Oliveira 	tracefs_hist_pause(tool->trace.inst, data->trace_hist);
163829a6c0bSDaniel Bristot de Oliveira 	tracefs_hist_destroy(tool->trace.inst, data->trace_hist);
164829a6c0bSDaniel Bristot de Oliveira }
165829a6c0bSDaniel Bristot de Oliveira 
166829a6c0bSDaniel Bristot de Oliveira /*
167829a6c0bSDaniel Bristot de Oliveira  * osnoise_init_trace_hist - enable events used to collect histogram
168829a6c0bSDaniel Bristot de Oliveira  */
osnoise_init_trace_hist(struct osnoise_tool * tool)169829a6c0bSDaniel Bristot de Oliveira static int osnoise_init_trace_hist(struct osnoise_tool *tool)
170829a6c0bSDaniel Bristot de Oliveira {
171829a6c0bSDaniel Bristot de Oliveira 	struct osnoise_hist_params *params = tool->params;
172829a6c0bSDaniel Bristot de Oliveira 	struct osnoise_hist_data *data = tool->data;
173829a6c0bSDaniel Bristot de Oliveira 	int bucket_size;
174829a6c0bSDaniel Bristot de Oliveira 	char buff[128];
175829a6c0bSDaniel Bristot de Oliveira 	int retval = 0;
176829a6c0bSDaniel Bristot de Oliveira 
177829a6c0bSDaniel Bristot de Oliveira 	/*
178829a6c0bSDaniel Bristot de Oliveira 	 * Set the size of the bucket.
179829a6c0bSDaniel Bristot de Oliveira 	 */
180829a6c0bSDaniel Bristot de Oliveira 	bucket_size = params->output_divisor * params->bucket_size;
181829a6c0bSDaniel Bristot de Oliveira 	snprintf(buff, sizeof(buff), "duration.buckets=%d", bucket_size);
182829a6c0bSDaniel Bristot de Oliveira 
183829a6c0bSDaniel Bristot de Oliveira 	data->trace_hist = tracefs_hist_alloc(tool->trace.tep, "osnoise", "sample_threshold",
184829a6c0bSDaniel Bristot de Oliveira 			buff, TRACEFS_HIST_KEY_NORMAL);
185829a6c0bSDaniel Bristot de Oliveira 	if (!data->trace_hist)
186829a6c0bSDaniel Bristot de Oliveira 		return 1;
187829a6c0bSDaniel Bristot de Oliveira 
188829a6c0bSDaniel Bristot de Oliveira 	retval = tracefs_hist_add_key(data->trace_hist, "cpu", 0);
189829a6c0bSDaniel Bristot de Oliveira 	if (retval)
190829a6c0bSDaniel Bristot de Oliveira 		goto out_err;
191829a6c0bSDaniel Bristot de Oliveira 
192829a6c0bSDaniel Bristot de Oliveira 	retval = tracefs_hist_start(tool->trace.inst, data->trace_hist);
193829a6c0bSDaniel Bristot de Oliveira 	if (retval)
194829a6c0bSDaniel Bristot de Oliveira 		goto out_err;
195829a6c0bSDaniel Bristot de Oliveira 
196829a6c0bSDaniel Bristot de Oliveira 	return 0;
197829a6c0bSDaniel Bristot de Oliveira 
198829a6c0bSDaniel Bristot de Oliveira out_err:
199829a6c0bSDaniel Bristot de Oliveira 	osnoise_destroy_trace_hist(tool);
200829a6c0bSDaniel Bristot de Oliveira 	return 1;
201829a6c0bSDaniel Bristot de Oliveira }
202829a6c0bSDaniel Bristot de Oliveira 
203829a6c0bSDaniel Bristot de Oliveira /*
204829a6c0bSDaniel Bristot de Oliveira  * osnoise_read_trace_hist - parse histogram file and file osnoise histogram
205829a6c0bSDaniel Bristot de Oliveira  */
osnoise_read_trace_hist(struct osnoise_tool * tool)206829a6c0bSDaniel Bristot de Oliveira static void osnoise_read_trace_hist(struct osnoise_tool *tool)
207829a6c0bSDaniel Bristot de Oliveira {
208829a6c0bSDaniel Bristot de Oliveira 	struct osnoise_hist_data *data = tool->data;
209829a6c0bSDaniel Bristot de Oliveira 	long long cpu, counter, duration;
210829a6c0bSDaniel Bristot de Oliveira 	char *content, *position;
211829a6c0bSDaniel Bristot de Oliveira 
212829a6c0bSDaniel Bristot de Oliveira 	tracefs_hist_pause(tool->trace.inst, data->trace_hist);
213829a6c0bSDaniel Bristot de Oliveira 
214829a6c0bSDaniel Bristot de Oliveira 	content = tracefs_event_file_read(tool->trace.inst, "osnoise",
215829a6c0bSDaniel Bristot de Oliveira 					  "sample_threshold",
216829a6c0bSDaniel Bristot de Oliveira 					  "hist", NULL);
217829a6c0bSDaniel Bristot de Oliveira 	if (!content)
218829a6c0bSDaniel Bristot de Oliveira 		return;
219829a6c0bSDaniel Bristot de Oliveira 
220829a6c0bSDaniel Bristot de Oliveira 	position = content;
221829a6c0bSDaniel Bristot de Oliveira 	while (true) {
222829a6c0bSDaniel Bristot de Oliveira 		position = strstr(position, "duration: ~");
223829a6c0bSDaniel Bristot de Oliveira 		if (!position)
224829a6c0bSDaniel Bristot de Oliveira 			break;
225829a6c0bSDaniel Bristot de Oliveira 		position += strlen("duration: ~");
226829a6c0bSDaniel Bristot de Oliveira 		duration = get_llong_from_str(position);
227829a6c0bSDaniel Bristot de Oliveira 		if (duration == -1)
228829a6c0bSDaniel Bristot de Oliveira 			err_msg("error reading duration from histogram\n");
229829a6c0bSDaniel Bristot de Oliveira 
230829a6c0bSDaniel Bristot de Oliveira 		position = strstr(position, "cpu:");
231829a6c0bSDaniel Bristot de Oliveira 		if (!position)
232829a6c0bSDaniel Bristot de Oliveira 			break;
233829a6c0bSDaniel Bristot de Oliveira 		position += strlen("cpu: ");
234829a6c0bSDaniel Bristot de Oliveira 		cpu = get_llong_from_str(position);
235829a6c0bSDaniel Bristot de Oliveira 		if (cpu == -1)
236829a6c0bSDaniel Bristot de Oliveira 			err_msg("error reading cpu from histogram\n");
237829a6c0bSDaniel Bristot de Oliveira 
238829a6c0bSDaniel Bristot de Oliveira 		position = strstr(position, "hitcount:");
239829a6c0bSDaniel Bristot de Oliveira 		if (!position)
240829a6c0bSDaniel Bristot de Oliveira 			break;
241829a6c0bSDaniel Bristot de Oliveira 		position += strlen("hitcount: ");
242829a6c0bSDaniel Bristot de Oliveira 		counter = get_llong_from_str(position);
243829a6c0bSDaniel Bristot de Oliveira 		if (counter == -1)
244829a6c0bSDaniel Bristot de Oliveira 			err_msg("error reading counter from histogram\n");
245829a6c0bSDaniel Bristot de Oliveira 
246829a6c0bSDaniel Bristot de Oliveira 		osnoise_hist_update_multiple(tool, cpu, duration, counter);
247829a6c0bSDaniel Bristot de Oliveira 	}
248829a6c0bSDaniel Bristot de Oliveira 	free(content);
249829a6c0bSDaniel Bristot de Oliveira }
250829a6c0bSDaniel Bristot de Oliveira 
251829a6c0bSDaniel Bristot de Oliveira /*
252829a6c0bSDaniel Bristot de Oliveira  * osnoise_hist_header - print the header of the tracer to the output
253829a6c0bSDaniel Bristot de Oliveira  */
osnoise_hist_header(struct osnoise_tool * tool)254829a6c0bSDaniel Bristot de Oliveira static void osnoise_hist_header(struct osnoise_tool *tool)
255829a6c0bSDaniel Bristot de Oliveira {
256829a6c0bSDaniel Bristot de Oliveira 	struct osnoise_hist_params *params = tool->params;
257829a6c0bSDaniel Bristot de Oliveira 	struct osnoise_hist_data *data = tool->data;
258829a6c0bSDaniel Bristot de Oliveira 	struct trace_seq *s = tool->trace.seq;
259829a6c0bSDaniel Bristot de Oliveira 	char duration[26];
260829a6c0bSDaniel Bristot de Oliveira 	int cpu;
261829a6c0bSDaniel Bristot de Oliveira 
262829a6c0bSDaniel Bristot de Oliveira 	if (params->no_header)
263829a6c0bSDaniel Bristot de Oliveira 		return;
264829a6c0bSDaniel Bristot de Oliveira 
265829a6c0bSDaniel Bristot de Oliveira 	get_duration(tool->start_time, duration, sizeof(duration));
266829a6c0bSDaniel Bristot de Oliveira 	trace_seq_printf(s, "# RTLA osnoise histogram\n");
267829a6c0bSDaniel Bristot de Oliveira 	trace_seq_printf(s, "# Time unit is %s (%s)\n",
268829a6c0bSDaniel Bristot de Oliveira 			params->output_divisor == 1 ? "nanoseconds" : "microseconds",
269829a6c0bSDaniel Bristot de Oliveira 			params->output_divisor == 1 ? "ns" : "us");
270829a6c0bSDaniel Bristot de Oliveira 
271829a6c0bSDaniel Bristot de Oliveira 	trace_seq_printf(s, "# Duration: %s\n", duration);
272829a6c0bSDaniel Bristot de Oliveira 
273829a6c0bSDaniel Bristot de Oliveira 	if (!params->no_index)
274829a6c0bSDaniel Bristot de Oliveira 		trace_seq_printf(s, "Index");
275829a6c0bSDaniel Bristot de Oliveira 
276829a6c0bSDaniel Bristot de Oliveira 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
277894c29c7SDaniel Bristot de Oliveira 		if (params->cpus && !CPU_ISSET(cpu, &params->monitored_cpus))
278829a6c0bSDaniel Bristot de Oliveira 			continue;
279829a6c0bSDaniel Bristot de Oliveira 
280829a6c0bSDaniel Bristot de Oliveira 		if (!data->hist[cpu].count)
281829a6c0bSDaniel Bristot de Oliveira 			continue;
282829a6c0bSDaniel Bristot de Oliveira 
283829a6c0bSDaniel Bristot de Oliveira 		trace_seq_printf(s, "   CPU-%03d", cpu);
284829a6c0bSDaniel Bristot de Oliveira 	}
285829a6c0bSDaniel Bristot de Oliveira 	trace_seq_printf(s, "\n");
286829a6c0bSDaniel Bristot de Oliveira 
287829a6c0bSDaniel Bristot de Oliveira 	trace_seq_do_printf(s);
288829a6c0bSDaniel Bristot de Oliveira 	trace_seq_reset(s);
289829a6c0bSDaniel Bristot de Oliveira }
290829a6c0bSDaniel Bristot de Oliveira 
291829a6c0bSDaniel Bristot de Oliveira /*
292829a6c0bSDaniel Bristot de Oliveira  * osnoise_print_summary - print the summary of the hist data to the output
293829a6c0bSDaniel Bristot de Oliveira  */
294829a6c0bSDaniel Bristot de Oliveira static void
osnoise_print_summary(struct osnoise_hist_params * params,struct trace_instance * trace,struct osnoise_hist_data * data)295829a6c0bSDaniel Bristot de Oliveira osnoise_print_summary(struct osnoise_hist_params *params,
296829a6c0bSDaniel Bristot de Oliveira 		       struct trace_instance *trace,
297829a6c0bSDaniel Bristot de Oliveira 		       struct osnoise_hist_data *data)
298829a6c0bSDaniel Bristot de Oliveira {
299829a6c0bSDaniel Bristot de Oliveira 	int cpu;
300829a6c0bSDaniel Bristot de Oliveira 
301829a6c0bSDaniel Bristot de Oliveira 	if (params->no_summary)
302829a6c0bSDaniel Bristot de Oliveira 		return;
303829a6c0bSDaniel Bristot de Oliveira 
304829a6c0bSDaniel Bristot de Oliveira 	if (!params->no_index)
305829a6c0bSDaniel Bristot de Oliveira 		trace_seq_printf(trace->seq, "count:");
306829a6c0bSDaniel Bristot de Oliveira 
307829a6c0bSDaniel Bristot de Oliveira 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
308894c29c7SDaniel Bristot de Oliveira 		if (params->cpus && !CPU_ISSET(cpu, &params->monitored_cpus))
309829a6c0bSDaniel Bristot de Oliveira 			continue;
310829a6c0bSDaniel Bristot de Oliveira 
311829a6c0bSDaniel Bristot de Oliveira 		if (!data->hist[cpu].count)
312829a6c0bSDaniel Bristot de Oliveira 			continue;
313829a6c0bSDaniel Bristot de Oliveira 
314829a6c0bSDaniel Bristot de Oliveira 		trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].count);
315829a6c0bSDaniel Bristot de Oliveira 	}
316829a6c0bSDaniel Bristot de Oliveira 	trace_seq_printf(trace->seq, "\n");
317829a6c0bSDaniel Bristot de Oliveira 
318829a6c0bSDaniel Bristot de Oliveira 	if (!params->no_index)
319829a6c0bSDaniel Bristot de Oliveira 		trace_seq_printf(trace->seq, "min:  ");
320829a6c0bSDaniel Bristot de Oliveira 
321829a6c0bSDaniel Bristot de Oliveira 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
322894c29c7SDaniel Bristot de Oliveira 		if (params->cpus && !CPU_ISSET(cpu, &params->monitored_cpus))
323829a6c0bSDaniel Bristot de Oliveira 			continue;
324829a6c0bSDaniel Bristot de Oliveira 
325829a6c0bSDaniel Bristot de Oliveira 		if (!data->hist[cpu].count)
326829a6c0bSDaniel Bristot de Oliveira 			continue;
327829a6c0bSDaniel Bristot de Oliveira 
328829a6c0bSDaniel Bristot de Oliveira 		trace_seq_printf(trace->seq, "%9llu ",	data->hist[cpu].min_sample);
329829a6c0bSDaniel Bristot de Oliveira 
330829a6c0bSDaniel Bristot de Oliveira 	}
331829a6c0bSDaniel Bristot de Oliveira 	trace_seq_printf(trace->seq, "\n");
332829a6c0bSDaniel Bristot de Oliveira 
333829a6c0bSDaniel Bristot de Oliveira 	if (!params->no_index)
334829a6c0bSDaniel Bristot de Oliveira 		trace_seq_printf(trace->seq, "avg:  ");
335829a6c0bSDaniel Bristot de Oliveira 
336829a6c0bSDaniel Bristot de Oliveira 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
337894c29c7SDaniel Bristot de Oliveira 		if (params->cpus && !CPU_ISSET(cpu, &params->monitored_cpus))
338829a6c0bSDaniel Bristot de Oliveira 			continue;
339829a6c0bSDaniel Bristot de Oliveira 
340829a6c0bSDaniel Bristot de Oliveira 		if (!data->hist[cpu].count)
341829a6c0bSDaniel Bristot de Oliveira 			continue;
342829a6c0bSDaniel Bristot de Oliveira 
343829a6c0bSDaniel Bristot de Oliveira 		if (data->hist[cpu].count)
3441fab1469SAndreas Ziegler 			trace_seq_printf(trace->seq, "%9.2f ",
3451fab1469SAndreas Ziegler 				((double) data->hist[cpu].sum_sample) / data->hist[cpu].count);
346829a6c0bSDaniel Bristot de Oliveira 		else
347829a6c0bSDaniel Bristot de Oliveira 			trace_seq_printf(trace->seq, "        - ");
348829a6c0bSDaniel Bristot de Oliveira 	}
349829a6c0bSDaniel Bristot de Oliveira 	trace_seq_printf(trace->seq, "\n");
350829a6c0bSDaniel Bristot de Oliveira 
351829a6c0bSDaniel Bristot de Oliveira 	if (!params->no_index)
352829a6c0bSDaniel Bristot de Oliveira 		trace_seq_printf(trace->seq, "max:  ");
353829a6c0bSDaniel Bristot de Oliveira 
354829a6c0bSDaniel Bristot de Oliveira 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
355894c29c7SDaniel Bristot de Oliveira 		if (params->cpus && !CPU_ISSET(cpu, &params->monitored_cpus))
356829a6c0bSDaniel Bristot de Oliveira 			continue;
357829a6c0bSDaniel Bristot de Oliveira 
358829a6c0bSDaniel Bristot de Oliveira 		if (!data->hist[cpu].count)
359829a6c0bSDaniel Bristot de Oliveira 			continue;
360829a6c0bSDaniel Bristot de Oliveira 
361829a6c0bSDaniel Bristot de Oliveira 		trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].max_sample);
362829a6c0bSDaniel Bristot de Oliveira 
363829a6c0bSDaniel Bristot de Oliveira 	}
364829a6c0bSDaniel Bristot de Oliveira 	trace_seq_printf(trace->seq, "\n");
365829a6c0bSDaniel Bristot de Oliveira 	trace_seq_do_printf(trace->seq);
366829a6c0bSDaniel Bristot de Oliveira 	trace_seq_reset(trace->seq);
367829a6c0bSDaniel Bristot de Oliveira }
368829a6c0bSDaniel Bristot de Oliveira 
369829a6c0bSDaniel Bristot de Oliveira /*
370829a6c0bSDaniel Bristot de Oliveira  * osnoise_print_stats - print data for all CPUs
371829a6c0bSDaniel Bristot de Oliveira  */
372829a6c0bSDaniel Bristot de Oliveira static void
osnoise_print_stats(struct osnoise_hist_params * params,struct osnoise_tool * tool)373829a6c0bSDaniel Bristot de Oliveira osnoise_print_stats(struct osnoise_hist_params *params, struct osnoise_tool *tool)
374829a6c0bSDaniel Bristot de Oliveira {
375829a6c0bSDaniel Bristot de Oliveira 	struct osnoise_hist_data *data = tool->data;
376829a6c0bSDaniel Bristot de Oliveira 	struct trace_instance *trace = &tool->trace;
377*587f05a8SLuis Claudio R. Goncalves 	int has_samples = 0;
378829a6c0bSDaniel Bristot de Oliveira 	int bucket, cpu;
379829a6c0bSDaniel Bristot de Oliveira 	int total;
380829a6c0bSDaniel Bristot de Oliveira 
381829a6c0bSDaniel Bristot de Oliveira 	osnoise_hist_header(tool);
382829a6c0bSDaniel Bristot de Oliveira 
383829a6c0bSDaniel Bristot de Oliveira 	for (bucket = 0; bucket < data->entries; bucket++) {
384829a6c0bSDaniel Bristot de Oliveira 		total = 0;
385829a6c0bSDaniel Bristot de Oliveira 
386829a6c0bSDaniel Bristot de Oliveira 		if (!params->no_index)
387829a6c0bSDaniel Bristot de Oliveira 			trace_seq_printf(trace->seq, "%-6d",
388829a6c0bSDaniel Bristot de Oliveira 					 bucket * data->bucket_size);
389829a6c0bSDaniel Bristot de Oliveira 
390829a6c0bSDaniel Bristot de Oliveira 		for (cpu = 0; cpu < data->nr_cpus; cpu++) {
391894c29c7SDaniel Bristot de Oliveira 			if (params->cpus && !CPU_ISSET(cpu, &params->monitored_cpus))
392829a6c0bSDaniel Bristot de Oliveira 				continue;
393829a6c0bSDaniel Bristot de Oliveira 
394829a6c0bSDaniel Bristot de Oliveira 			if (!data->hist[cpu].count)
395829a6c0bSDaniel Bristot de Oliveira 				continue;
396829a6c0bSDaniel Bristot de Oliveira 
397829a6c0bSDaniel Bristot de Oliveira 			total += data->hist[cpu].samples[bucket];
398829a6c0bSDaniel Bristot de Oliveira 			trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].samples[bucket]);
399829a6c0bSDaniel Bristot de Oliveira 		}
400829a6c0bSDaniel Bristot de Oliveira 
401829a6c0bSDaniel Bristot de Oliveira 		if (total == 0 && !params->with_zeros) {
402829a6c0bSDaniel Bristot de Oliveira 			trace_seq_reset(trace->seq);
403829a6c0bSDaniel Bristot de Oliveira 			continue;
404829a6c0bSDaniel Bristot de Oliveira 		}
405829a6c0bSDaniel Bristot de Oliveira 
406*587f05a8SLuis Claudio R. Goncalves 		/* There are samples above the threshold */
407*587f05a8SLuis Claudio R. Goncalves 		has_samples = 1;
408829a6c0bSDaniel Bristot de Oliveira 		trace_seq_printf(trace->seq, "\n");
409829a6c0bSDaniel Bristot de Oliveira 		trace_seq_do_printf(trace->seq);
410829a6c0bSDaniel Bristot de Oliveira 		trace_seq_reset(trace->seq);
411829a6c0bSDaniel Bristot de Oliveira 	}
412829a6c0bSDaniel Bristot de Oliveira 
413*587f05a8SLuis Claudio R. Goncalves 	/*
414*587f05a8SLuis Claudio R. Goncalves 	 * If no samples were recorded, skip calculations, print zeroed statistics
415*587f05a8SLuis Claudio R. Goncalves 	 * and return.
416*587f05a8SLuis Claudio R. Goncalves 	 */
417*587f05a8SLuis Claudio R. Goncalves 	if (!has_samples) {
418*587f05a8SLuis Claudio R. Goncalves 		trace_seq_reset(trace->seq);
419*587f05a8SLuis Claudio R. Goncalves 		trace_seq_printf(trace->seq, "over: 0\ncount: 0\nmin: 0\navg: 0\nmax: 0\n");
420*587f05a8SLuis Claudio R. Goncalves 		trace_seq_do_printf(trace->seq);
421*587f05a8SLuis Claudio R. Goncalves 		trace_seq_reset(trace->seq);
422*587f05a8SLuis Claudio R. Goncalves 		return;
423*587f05a8SLuis Claudio R. Goncalves 	}
424*587f05a8SLuis Claudio R. Goncalves 
425829a6c0bSDaniel Bristot de Oliveira 	if (!params->no_index)
426829a6c0bSDaniel Bristot de Oliveira 		trace_seq_printf(trace->seq, "over: ");
427829a6c0bSDaniel Bristot de Oliveira 
428829a6c0bSDaniel Bristot de Oliveira 	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
429894c29c7SDaniel Bristot de Oliveira 		if (params->cpus && !CPU_ISSET(cpu, &params->monitored_cpus))
430829a6c0bSDaniel Bristot de Oliveira 			continue;
431829a6c0bSDaniel Bristot de Oliveira 
432829a6c0bSDaniel Bristot de Oliveira 		if (!data->hist[cpu].count)
433829a6c0bSDaniel Bristot de Oliveira 			continue;
434829a6c0bSDaniel Bristot de Oliveira 
435829a6c0bSDaniel Bristot de Oliveira 		trace_seq_printf(trace->seq, "%9d ",
436829a6c0bSDaniel Bristot de Oliveira 				 data->hist[cpu].samples[data->entries]);
437829a6c0bSDaniel Bristot de Oliveira 	}
438829a6c0bSDaniel Bristot de Oliveira 	trace_seq_printf(trace->seq, "\n");
439829a6c0bSDaniel Bristot de Oliveira 	trace_seq_do_printf(trace->seq);
440829a6c0bSDaniel Bristot de Oliveira 	trace_seq_reset(trace->seq);
441829a6c0bSDaniel Bristot de Oliveira 
442829a6c0bSDaniel Bristot de Oliveira 	osnoise_print_summary(params, trace, data);
443829a6c0bSDaniel Bristot de Oliveira }
444829a6c0bSDaniel Bristot de Oliveira 
445829a6c0bSDaniel Bristot de Oliveira /*
446829a6c0bSDaniel Bristot de Oliveira  * osnoise_hist_usage - prints osnoise hist usage message
447829a6c0bSDaniel Bristot de Oliveira  */
osnoise_hist_usage(char * usage)448829a6c0bSDaniel Bristot de Oliveira static void osnoise_hist_usage(char *usage)
449829a6c0bSDaniel Bristot de Oliveira {
450829a6c0bSDaniel Bristot de Oliveira 	int i;
451829a6c0bSDaniel Bristot de Oliveira 
452829a6c0bSDaniel Bristot de Oliveira 	static const char * const msg[] = {
453829a6c0bSDaniel Bristot de Oliveira 		"",
4542b622eddSDaniel Bristot de Oliveira 		"  usage: rtla osnoise hist [-h] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",
455842fc5b8SJohn Kacur 		"	  [-T us] [-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\",
456272ced25SDaniel Bristot de Oliveira 		"	  [-c cpu-list] [-H cpu-list] [-P priority] [-b N] [-E N] [--no-header] [--no-summary] \\",
457cdbf7196SDaniel Bristot de Oliveira 		"	  [--no-index] [--with-zeros] [-C[=cgroup_name]] [--warm-up]",
458829a6c0bSDaniel Bristot de Oliveira 		"",
459829a6c0bSDaniel Bristot de Oliveira 		"	  -h/--help: print this menu",
4602b622eddSDaniel Bristot de Oliveira 		"	  -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit",
461829a6c0bSDaniel Bristot de Oliveira 		"	  -p/--period us: osnoise period in us",
462829a6c0bSDaniel Bristot de Oliveira 		"	  -r/--runtime us: osnoise runtime in us",
463829a6c0bSDaniel Bristot de Oliveira 		"	  -s/--stop us: stop trace if a single sample is higher than the argument in us",
464829a6c0bSDaniel Bristot de Oliveira 		"	  -S/--stop-total us: stop trace if the total sample is higher than the argument in us",
465d635316aSDaniel Bristot de Oliveira 		"	  -T/--threshold us: the minimum delta to be considered a noise",
466829a6c0bSDaniel Bristot de Oliveira 		"	  -c/--cpus cpu-list: list of cpus to run osnoise threads",
467272ced25SDaniel Bristot de Oliveira 		"	  -H/--house-keeping cpus: run rtla control threads only on the given cpus",
468a957cbc0SDaniel Bristot de Oliveira 		"	  -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",
469829a6c0bSDaniel Bristot de Oliveira 		"	  -d/--duration time[s|m|h|d]: duration of the session",
470829a6c0bSDaniel Bristot de Oliveira 		"	  -D/--debug: print debug info",
471842fc5b8SJohn Kacur 		"	  -t/--trace[file]: save the stopped trace to [file|osnoise_trace.txt]",
47251d64c3aSDaniel Bristot de Oliveira 		"	  -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
47344f3a37dSDaniel Bristot de Oliveira 		"	     --filter <filter>: enable a trace event filter to the previous -e event",
4741a754893SDaniel Bristot de Oliveira 		"	     --trigger <trigger>: enable a trace event trigger to the previous -e event",
475829a6c0bSDaniel Bristot de Oliveira 		"	  -b/--bucket-size N: set the histogram bucket size (default 1)",
476dd48f316SDaniel Bristot de Oliveira 		"	  -E/--entries N: set the number of entries of the histogram (default 256)",
477829a6c0bSDaniel Bristot de Oliveira 		"	     --no-header: do not print header",
478829a6c0bSDaniel Bristot de Oliveira 		"	     --no-summary: do not print summary",
479829a6c0bSDaniel Bristot de Oliveira 		"	     --no-index: do not print index",
480829a6c0bSDaniel Bristot de Oliveira 		"	     --with-zeros: print zero only entries",
481829a6c0bSDaniel Bristot de Oliveira 		"	  -P/--priority o:prio|r:prio|f:prio|d:runtime:period: set scheduling parameters",
482829a6c0bSDaniel Bristot de Oliveira 		"		o:prio - use SCHED_OTHER with prio",
483829a6c0bSDaniel Bristot de Oliveira 		"		r:prio - use SCHED_RR with prio",
484829a6c0bSDaniel Bristot de Oliveira 		"		f:prio - use SCHED_FIFO with prio",
485829a6c0bSDaniel Bristot de Oliveira 		"		d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
486829a6c0bSDaniel Bristot de Oliveira 		"						       in nanoseconds",
487cdbf7196SDaniel Bristot de Oliveira 		"	     --warm-up: let the workload run for s seconds before collecting data",
488e9a4062eSDaniel Bristot de Oliveira 		"	     --trace-buffer-size kB: set the per-cpu trace buffer size in kB",
489829a6c0bSDaniel Bristot de Oliveira 		NULL,
490829a6c0bSDaniel Bristot de Oliveira 	};
491829a6c0bSDaniel Bristot de Oliveira 
492829a6c0bSDaniel Bristot de Oliveira 	if (usage)
493829a6c0bSDaniel Bristot de Oliveira 		fprintf(stderr, "%s\n", usage);
494829a6c0bSDaniel Bristot de Oliveira 
495829a6c0bSDaniel Bristot de Oliveira 	fprintf(stderr, "rtla osnoise hist: a per-cpu histogram of the OS noise (version %s)\n",
496829a6c0bSDaniel Bristot de Oliveira 			VERSION);
497829a6c0bSDaniel Bristot de Oliveira 
498829a6c0bSDaniel Bristot de Oliveira 	for (i = 0; msg[i]; i++)
499829a6c0bSDaniel Bristot de Oliveira 		fprintf(stderr, "%s\n", msg[i]);
500b5f31936SJohn Kacur 
501b5f31936SJohn Kacur 	if (usage)
502b5f31936SJohn Kacur 		exit(EXIT_FAILURE);
503b5f31936SJohn Kacur 
504b5f31936SJohn Kacur 	exit(EXIT_SUCCESS);
505829a6c0bSDaniel Bristot de Oliveira }
506829a6c0bSDaniel Bristot de Oliveira 
507829a6c0bSDaniel Bristot de Oliveira /*
508829a6c0bSDaniel Bristot de Oliveira  * osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters
509829a6c0bSDaniel Bristot de Oliveira  */
510829a6c0bSDaniel Bristot de Oliveira static struct osnoise_hist_params
osnoise_hist_parse_args(int argc,char * argv[])511829a6c0bSDaniel Bristot de Oliveira *osnoise_hist_parse_args(int argc, char *argv[])
512829a6c0bSDaniel Bristot de Oliveira {
513829a6c0bSDaniel Bristot de Oliveira 	struct osnoise_hist_params *params;
51451d64c3aSDaniel Bristot de Oliveira 	struct trace_events *tevent;
515829a6c0bSDaniel Bristot de Oliveira 	int retval;
516829a6c0bSDaniel Bristot de Oliveira 	int c;
517829a6c0bSDaniel Bristot de Oliveira 
518829a6c0bSDaniel Bristot de Oliveira 	params = calloc(1, sizeof(*params));
519829a6c0bSDaniel Bristot de Oliveira 	if (!params)
520829a6c0bSDaniel Bristot de Oliveira 		exit(1);
521829a6c0bSDaniel Bristot de Oliveira 
522829a6c0bSDaniel Bristot de Oliveira 	/* display data in microseconds */
523829a6c0bSDaniel Bristot de Oliveira 	params->output_divisor = 1000;
524829a6c0bSDaniel Bristot de Oliveira 	params->bucket_size = 1;
525829a6c0bSDaniel Bristot de Oliveira 	params->entries = 256;
526829a6c0bSDaniel Bristot de Oliveira 
527829a6c0bSDaniel Bristot de Oliveira 	while (1) {
528829a6c0bSDaniel Bristot de Oliveira 		static struct option long_options[] = {
5292b622eddSDaniel Bristot de Oliveira 			{"auto",		required_argument,	0, 'a'},
530829a6c0bSDaniel Bristot de Oliveira 			{"bucket-size",		required_argument,	0, 'b'},
531dd48f316SDaniel Bristot de Oliveira 			{"entries",		required_argument,	0, 'E'},
532829a6c0bSDaniel Bristot de Oliveira 			{"cpus",		required_argument,	0, 'c'},
533a957cbc0SDaniel Bristot de Oliveira 			{"cgroup",		optional_argument,	0, 'C'},
534829a6c0bSDaniel Bristot de Oliveira 			{"debug",		no_argument,		0, 'D'},
535829a6c0bSDaniel Bristot de Oliveira 			{"duration",		required_argument,	0, 'd'},
536272ced25SDaniel Bristot de Oliveira 			{"house-keeping",	required_argument,		0, 'H'},
537829a6c0bSDaniel Bristot de Oliveira 			{"help",		no_argument,		0, 'h'},
538829a6c0bSDaniel Bristot de Oliveira 			{"period",		required_argument,	0, 'p'},
539829a6c0bSDaniel Bristot de Oliveira 			{"priority",		required_argument,	0, 'P'},
540829a6c0bSDaniel Bristot de Oliveira 			{"runtime",		required_argument,	0, 'r'},
541829a6c0bSDaniel Bristot de Oliveira 			{"stop",		required_argument,	0, 's'},
542829a6c0bSDaniel Bristot de Oliveira 			{"stop-total",		required_argument,	0, 'S'},
543829a6c0bSDaniel Bristot de Oliveira 			{"trace",		optional_argument,	0, 't'},
54451d64c3aSDaniel Bristot de Oliveira 			{"event",		required_argument,	0, 'e'},
545d635316aSDaniel Bristot de Oliveira 			{"threshold",		required_argument,	0, 'T'},
546829a6c0bSDaniel Bristot de Oliveira 			{"no-header",		no_argument,		0, '0'},
547829a6c0bSDaniel Bristot de Oliveira 			{"no-summary",		no_argument,		0, '1'},
548829a6c0bSDaniel Bristot de Oliveira 			{"no-index",		no_argument,		0, '2'},
549829a6c0bSDaniel Bristot de Oliveira 			{"with-zeros",		no_argument,		0, '3'},
5501a754893SDaniel Bristot de Oliveira 			{"trigger",		required_argument,	0, '4'},
55144f3a37dSDaniel Bristot de Oliveira 			{"filter",		required_argument,	0, '5'},
552cdbf7196SDaniel Bristot de Oliveira 			{"warm-up",		required_argument,	0, '6'},
553e9a4062eSDaniel Bristot de Oliveira 			{"trace-buffer-size",	required_argument,	0, '7'},
554829a6c0bSDaniel Bristot de Oliveira 			{0, 0, 0, 0}
555829a6c0bSDaniel Bristot de Oliveira 		};
556829a6c0bSDaniel Bristot de Oliveira 
557829a6c0bSDaniel Bristot de Oliveira 		/* getopt_long stores the option index here. */
558829a6c0bSDaniel Bristot de Oliveira 		int option_index = 0;
559829a6c0bSDaniel Bristot de Oliveira 
560e9a4062eSDaniel Bristot de Oliveira 		c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:p:P:r:s:S:t::T:01234:5:6:7:",
561829a6c0bSDaniel Bristot de Oliveira 				 long_options, &option_index);
562829a6c0bSDaniel Bristot de Oliveira 
563829a6c0bSDaniel Bristot de Oliveira 		/* detect the end of the options. */
564829a6c0bSDaniel Bristot de Oliveira 		if (c == -1)
565829a6c0bSDaniel Bristot de Oliveira 			break;
566829a6c0bSDaniel Bristot de Oliveira 
567829a6c0bSDaniel Bristot de Oliveira 		switch (c) {
5682b622eddSDaniel Bristot de Oliveira 		case 'a':
5692b622eddSDaniel Bristot de Oliveira 			/* set sample stop to auto_thresh */
5702b622eddSDaniel Bristot de Oliveira 			params->stop_us = get_llong_from_str(optarg);
5712b622eddSDaniel Bristot de Oliveira 
5722b622eddSDaniel Bristot de Oliveira 			/* set sample threshold to 1 */
5732b622eddSDaniel Bristot de Oliveira 			params->threshold = 1;
5742b622eddSDaniel Bristot de Oliveira 
5752b622eddSDaniel Bristot de Oliveira 			/* set trace */
5762b622eddSDaniel Bristot de Oliveira 			params->trace_output = "osnoise_trace.txt";
5772b622eddSDaniel Bristot de Oliveira 
5782b622eddSDaniel Bristot de Oliveira 			break;
579829a6c0bSDaniel Bristot de Oliveira 		case 'b':
580829a6c0bSDaniel Bristot de Oliveira 			params->bucket_size = get_llong_from_str(optarg);
581829a6c0bSDaniel Bristot de Oliveira 			if ((params->bucket_size == 0) || (params->bucket_size >= 1000000))
582829a6c0bSDaniel Bristot de Oliveira 				osnoise_hist_usage("Bucket size needs to be > 0 and <= 1000000\n");
583829a6c0bSDaniel Bristot de Oliveira 			break;
584829a6c0bSDaniel Bristot de Oliveira 		case 'c':
585894c29c7SDaniel Bristot de Oliveira 			retval = parse_cpu_set(optarg, &params->monitored_cpus);
586829a6c0bSDaniel Bristot de Oliveira 			if (retval)
587829a6c0bSDaniel Bristot de Oliveira 				osnoise_hist_usage("\nInvalid -c cpu list\n");
588829a6c0bSDaniel Bristot de Oliveira 			params->cpus = optarg;
589829a6c0bSDaniel Bristot de Oliveira 			break;
590a957cbc0SDaniel Bristot de Oliveira 		case 'C':
591a957cbc0SDaniel Bristot de Oliveira 			params->cgroup = 1;
592a957cbc0SDaniel Bristot de Oliveira 			if (!optarg) {
593a957cbc0SDaniel Bristot de Oliveira 				/* will inherit this cgroup */
594a957cbc0SDaniel Bristot de Oliveira 				params->cgroup_name = NULL;
595a957cbc0SDaniel Bristot de Oliveira 			} else if (*optarg == '=') {
596a957cbc0SDaniel Bristot de Oliveira 				/* skip the = */
597a957cbc0SDaniel Bristot de Oliveira 				params->cgroup_name = ++optarg;
598a957cbc0SDaniel Bristot de Oliveira 			}
599a957cbc0SDaniel Bristot de Oliveira 			break;
600829a6c0bSDaniel Bristot de Oliveira 		case 'D':
601829a6c0bSDaniel Bristot de Oliveira 			config_debug = 1;
602829a6c0bSDaniel Bristot de Oliveira 			break;
603829a6c0bSDaniel Bristot de Oliveira 		case 'd':
604829a6c0bSDaniel Bristot de Oliveira 			params->duration = parse_seconds_duration(optarg);
605829a6c0bSDaniel Bristot de Oliveira 			if (!params->duration)
606829a6c0bSDaniel Bristot de Oliveira 				osnoise_hist_usage("Invalid -D duration\n");
607829a6c0bSDaniel Bristot de Oliveira 			break;
60851d64c3aSDaniel Bristot de Oliveira 		case 'e':
60951d64c3aSDaniel Bristot de Oliveira 			tevent = trace_event_alloc(optarg);
61051d64c3aSDaniel Bristot de Oliveira 			if (!tevent) {
61151d64c3aSDaniel Bristot de Oliveira 				err_msg("Error alloc trace event");
61251d64c3aSDaniel Bristot de Oliveira 				exit(EXIT_FAILURE);
61351d64c3aSDaniel Bristot de Oliveira 			}
61451d64c3aSDaniel Bristot de Oliveira 
61551d64c3aSDaniel Bristot de Oliveira 			if (params->events)
61651d64c3aSDaniel Bristot de Oliveira 				tevent->next = params->events;
61751d64c3aSDaniel Bristot de Oliveira 
61851d64c3aSDaniel Bristot de Oliveira 			params->events = tevent;
61951d64c3aSDaniel Bristot de Oliveira 			break;
620dd48f316SDaniel Bristot de Oliveira 		case 'E':
621829a6c0bSDaniel Bristot de Oliveira 			params->entries = get_llong_from_str(optarg);
622829a6c0bSDaniel Bristot de Oliveira 			if ((params->entries < 10) || (params->entries > 9999999))
623829a6c0bSDaniel Bristot de Oliveira 				osnoise_hist_usage("Entries must be > 10 and < 9999999\n");
624829a6c0bSDaniel Bristot de Oliveira 			break;
625829a6c0bSDaniel Bristot de Oliveira 		case 'h':
626829a6c0bSDaniel Bristot de Oliveira 		case '?':
627829a6c0bSDaniel Bristot de Oliveira 			osnoise_hist_usage(NULL);
628829a6c0bSDaniel Bristot de Oliveira 			break;
629272ced25SDaniel Bristot de Oliveira 		case 'H':
630272ced25SDaniel Bristot de Oliveira 			params->hk_cpus = 1;
631272ced25SDaniel Bristot de Oliveira 			retval = parse_cpu_set(optarg, &params->hk_cpu_set);
632272ced25SDaniel Bristot de Oliveira 			if (retval) {
633272ced25SDaniel Bristot de Oliveira 				err_msg("Error parsing house keeping CPUs\n");
634272ced25SDaniel Bristot de Oliveira 				exit(EXIT_FAILURE);
635272ced25SDaniel Bristot de Oliveira 			}
636272ced25SDaniel Bristot de Oliveira 			break;
637829a6c0bSDaniel Bristot de Oliveira 		case 'p':
638829a6c0bSDaniel Bristot de Oliveira 			params->period = get_llong_from_str(optarg);
639829a6c0bSDaniel Bristot de Oliveira 			if (params->period > 10000000)
640829a6c0bSDaniel Bristot de Oliveira 				osnoise_hist_usage("Period longer than 10 s\n");
641829a6c0bSDaniel Bristot de Oliveira 			break;
642829a6c0bSDaniel Bristot de Oliveira 		case 'P':
643829a6c0bSDaniel Bristot de Oliveira 			retval = parse_prio(optarg, &params->sched_param);
644829a6c0bSDaniel Bristot de Oliveira 			if (retval == -1)
645829a6c0bSDaniel Bristot de Oliveira 				osnoise_hist_usage("Invalid -P priority");
646829a6c0bSDaniel Bristot de Oliveira 			params->set_sched = 1;
647829a6c0bSDaniel Bristot de Oliveira 			break;
648829a6c0bSDaniel Bristot de Oliveira 		case 'r':
649829a6c0bSDaniel Bristot de Oliveira 			params->runtime = get_llong_from_str(optarg);
650829a6c0bSDaniel Bristot de Oliveira 			if (params->runtime < 100)
651829a6c0bSDaniel Bristot de Oliveira 				osnoise_hist_usage("Runtime shorter than 100 us\n");
652829a6c0bSDaniel Bristot de Oliveira 			break;
653829a6c0bSDaniel Bristot de Oliveira 		case 's':
654829a6c0bSDaniel Bristot de Oliveira 			params->stop_us = get_llong_from_str(optarg);
655829a6c0bSDaniel Bristot de Oliveira 			break;
656829a6c0bSDaniel Bristot de Oliveira 		case 'S':
657829a6c0bSDaniel Bristot de Oliveira 			params->stop_total_us = get_llong_from_str(optarg);
658829a6c0bSDaniel Bristot de Oliveira 			break;
659d635316aSDaniel Bristot de Oliveira 		case 'T':
660d635316aSDaniel Bristot de Oliveira 			params->threshold = get_llong_from_str(optarg);
661d635316aSDaniel Bristot de Oliveira 			break;
662829a6c0bSDaniel Bristot de Oliveira 		case 't':
663842fc5b8SJohn Kacur 			if (optarg) {
664842fc5b8SJohn Kacur 				if (optarg[0] == '=')
665829a6c0bSDaniel Bristot de Oliveira 					params->trace_output = &optarg[1];
666829a6c0bSDaniel Bristot de Oliveira 				else
667842fc5b8SJohn Kacur 					params->trace_output = &optarg[0];
668842fc5b8SJohn Kacur 			} else if (optind < argc && argv[optind][0] != '0')
669842fc5b8SJohn Kacur 				params->trace_output = argv[optind];
670842fc5b8SJohn Kacur 			else
671829a6c0bSDaniel Bristot de Oliveira 				params->trace_output = "osnoise_trace.txt";
672829a6c0bSDaniel Bristot de Oliveira 			break;
673829a6c0bSDaniel Bristot de Oliveira 		case '0': /* no header */
674829a6c0bSDaniel Bristot de Oliveira 			params->no_header = 1;
675829a6c0bSDaniel Bristot de Oliveira 			break;
676829a6c0bSDaniel Bristot de Oliveira 		case '1': /* no summary */
677829a6c0bSDaniel Bristot de Oliveira 			params->no_summary = 1;
678829a6c0bSDaniel Bristot de Oliveira 			break;
679829a6c0bSDaniel Bristot de Oliveira 		case '2': /* no index */
680829a6c0bSDaniel Bristot de Oliveira 			params->no_index = 1;
681829a6c0bSDaniel Bristot de Oliveira 			break;
682829a6c0bSDaniel Bristot de Oliveira 		case '3': /* with zeros */
683829a6c0bSDaniel Bristot de Oliveira 			params->with_zeros = 1;
684829a6c0bSDaniel Bristot de Oliveira 			break;
6851a754893SDaniel Bristot de Oliveira 		case '4': /* trigger */
6861a754893SDaniel Bristot de Oliveira 			if (params->events) {
6871a754893SDaniel Bristot de Oliveira 				retval = trace_event_add_trigger(params->events, optarg);
6881a754893SDaniel Bristot de Oliveira 				if (retval) {
6891a754893SDaniel Bristot de Oliveira 					err_msg("Error adding trigger %s\n", optarg);
6901a754893SDaniel Bristot de Oliveira 					exit(EXIT_FAILURE);
6911a754893SDaniel Bristot de Oliveira 				}
6921a754893SDaniel Bristot de Oliveira 			} else {
6931a754893SDaniel Bristot de Oliveira 				osnoise_hist_usage("--trigger requires a previous -e\n");
6941a754893SDaniel Bristot de Oliveira 			}
6951a754893SDaniel Bristot de Oliveira 			break;
69644f3a37dSDaniel Bristot de Oliveira 		case '5': /* filter */
69744f3a37dSDaniel Bristot de Oliveira 			if (params->events) {
69844f3a37dSDaniel Bristot de Oliveira 				retval = trace_event_add_filter(params->events, optarg);
69944f3a37dSDaniel Bristot de Oliveira 				if (retval) {
70044f3a37dSDaniel Bristot de Oliveira 					err_msg("Error adding filter %s\n", optarg);
70144f3a37dSDaniel Bristot de Oliveira 					exit(EXIT_FAILURE);
70244f3a37dSDaniel Bristot de Oliveira 				}
70344f3a37dSDaniel Bristot de Oliveira 			} else {
70444f3a37dSDaniel Bristot de Oliveira 				osnoise_hist_usage("--filter requires a previous -e\n");
70544f3a37dSDaniel Bristot de Oliveira 			}
70644f3a37dSDaniel Bristot de Oliveira 			break;
707cdbf7196SDaniel Bristot de Oliveira 		case '6':
708cdbf7196SDaniel Bristot de Oliveira 			params->warmup = get_llong_from_str(optarg);
709cdbf7196SDaniel Bristot de Oliveira 			break;
710e9a4062eSDaniel Bristot de Oliveira 		case '7':
711e9a4062eSDaniel Bristot de Oliveira 			params->buffer_size = get_llong_from_str(optarg);
712e9a4062eSDaniel Bristot de Oliveira 			break;
713829a6c0bSDaniel Bristot de Oliveira 		default:
714829a6c0bSDaniel Bristot de Oliveira 			osnoise_hist_usage("Invalid option");
715829a6c0bSDaniel Bristot de Oliveira 		}
716829a6c0bSDaniel Bristot de Oliveira 	}
717829a6c0bSDaniel Bristot de Oliveira 
718829a6c0bSDaniel Bristot de Oliveira 	if (geteuid()) {
719829a6c0bSDaniel Bristot de Oliveira 		err_msg("rtla needs root permission\n");
720829a6c0bSDaniel Bristot de Oliveira 		exit(EXIT_FAILURE);
721829a6c0bSDaniel Bristot de Oliveira 	}
722829a6c0bSDaniel Bristot de Oliveira 
723829a6c0bSDaniel Bristot de Oliveira 	if (params->no_index && !params->with_zeros)
724829a6c0bSDaniel Bristot de Oliveira 		osnoise_hist_usage("no-index set and with-zeros not set - it does not make sense");
725829a6c0bSDaniel Bristot de Oliveira 
726829a6c0bSDaniel Bristot de Oliveira 	return params;
727829a6c0bSDaniel Bristot de Oliveira }
728829a6c0bSDaniel Bristot de Oliveira 
729829a6c0bSDaniel Bristot de Oliveira /*
730829a6c0bSDaniel Bristot de Oliveira  * osnoise_hist_apply_config - apply the hist configs to the initialized tool
731829a6c0bSDaniel Bristot de Oliveira  */
732829a6c0bSDaniel Bristot de Oliveira static int
osnoise_hist_apply_config(struct osnoise_tool * tool,struct osnoise_hist_params * params)733829a6c0bSDaniel Bristot de Oliveira osnoise_hist_apply_config(struct osnoise_tool *tool, struct osnoise_hist_params *params)
734829a6c0bSDaniel Bristot de Oliveira {
735829a6c0bSDaniel Bristot de Oliveira 	int retval;
736829a6c0bSDaniel Bristot de Oliveira 
737829a6c0bSDaniel Bristot de Oliveira 	if (!params->sleep_time)
738829a6c0bSDaniel Bristot de Oliveira 		params->sleep_time = 1;
739829a6c0bSDaniel Bristot de Oliveira 
740829a6c0bSDaniel Bristot de Oliveira 	if (params->cpus) {
741829a6c0bSDaniel Bristot de Oliveira 		retval = osnoise_set_cpus(tool->context, params->cpus);
742829a6c0bSDaniel Bristot de Oliveira 		if (retval) {
743829a6c0bSDaniel Bristot de Oliveira 			err_msg("Failed to apply CPUs config\n");
744829a6c0bSDaniel Bristot de Oliveira 			goto out_err;
745829a6c0bSDaniel Bristot de Oliveira 		}
746829a6c0bSDaniel Bristot de Oliveira 	}
747829a6c0bSDaniel Bristot de Oliveira 
748829a6c0bSDaniel Bristot de Oliveira 	if (params->runtime || params->period) {
749829a6c0bSDaniel Bristot de Oliveira 		retval = osnoise_set_runtime_period(tool->context,
750829a6c0bSDaniel Bristot de Oliveira 						    params->runtime,
751829a6c0bSDaniel Bristot de Oliveira 						    params->period);
752829a6c0bSDaniel Bristot de Oliveira 		if (retval) {
753829a6c0bSDaniel Bristot de Oliveira 			err_msg("Failed to set runtime and/or period\n");
754829a6c0bSDaniel Bristot de Oliveira 			goto out_err;
755829a6c0bSDaniel Bristot de Oliveira 		}
756829a6c0bSDaniel Bristot de Oliveira 	}
757829a6c0bSDaniel Bristot de Oliveira 
758829a6c0bSDaniel Bristot de Oliveira 	if (params->stop_us) {
759829a6c0bSDaniel Bristot de Oliveira 		retval = osnoise_set_stop_us(tool->context, params->stop_us);
760829a6c0bSDaniel Bristot de Oliveira 		if (retval) {
761829a6c0bSDaniel Bristot de Oliveira 			err_msg("Failed to set stop us\n");
762829a6c0bSDaniel Bristot de Oliveira 			goto out_err;
763829a6c0bSDaniel Bristot de Oliveira 		}
764829a6c0bSDaniel Bristot de Oliveira 	}
765829a6c0bSDaniel Bristot de Oliveira 
766829a6c0bSDaniel Bristot de Oliveira 	if (params->stop_total_us) {
767829a6c0bSDaniel Bristot de Oliveira 		retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us);
768829a6c0bSDaniel Bristot de Oliveira 		if (retval) {
769829a6c0bSDaniel Bristot de Oliveira 			err_msg("Failed to set stop total us\n");
770829a6c0bSDaniel Bristot de Oliveira 			goto out_err;
771829a6c0bSDaniel Bristot de Oliveira 		}
772829a6c0bSDaniel Bristot de Oliveira 	}
773829a6c0bSDaniel Bristot de Oliveira 
774d635316aSDaniel Bristot de Oliveira 	if (params->threshold) {
775d635316aSDaniel Bristot de Oliveira 		retval = osnoise_set_tracing_thresh(tool->context, params->threshold);
776d635316aSDaniel Bristot de Oliveira 		if (retval) {
777d635316aSDaniel Bristot de Oliveira 			err_msg("Failed to set tracing_thresh\n");
778d635316aSDaniel Bristot de Oliveira 			goto out_err;
779d635316aSDaniel Bristot de Oliveira 		}
780d635316aSDaniel Bristot de Oliveira 	}
781d635316aSDaniel Bristot de Oliveira 
782272ced25SDaniel Bristot de Oliveira 	if (params->hk_cpus) {
783272ced25SDaniel Bristot de Oliveira 		retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set),
784272ced25SDaniel Bristot de Oliveira 					   &params->hk_cpu_set);
785272ced25SDaniel Bristot de Oliveira 		if (retval == -1) {
786272ced25SDaniel Bristot de Oliveira 			err_msg("Failed to set rtla to the house keeping CPUs\n");
787272ced25SDaniel Bristot de Oliveira 			goto out_err;
788272ced25SDaniel Bristot de Oliveira 		}
789c58a3f8cSDaniel Bristot de Oliveira 	} else if (params->cpus) {
790c58a3f8cSDaniel Bristot de Oliveira 		/*
791c58a3f8cSDaniel Bristot de Oliveira 		 * Even if the user do not set a house-keeping CPU, try to
792c58a3f8cSDaniel Bristot de Oliveira 		 * move rtla to a CPU set different to the one where the user
793c58a3f8cSDaniel Bristot de Oliveira 		 * set the workload to run.
794c58a3f8cSDaniel Bristot de Oliveira 		 *
795c58a3f8cSDaniel Bristot de Oliveira 		 * No need to check results as this is an automatic attempt.
796c58a3f8cSDaniel Bristot de Oliveira 		 */
797c58a3f8cSDaniel Bristot de Oliveira 		auto_house_keeping(&params->monitored_cpus);
798272ced25SDaniel Bristot de Oliveira 	}
799272ced25SDaniel Bristot de Oliveira 
800829a6c0bSDaniel Bristot de Oliveira 	return 0;
801829a6c0bSDaniel Bristot de Oliveira 
802829a6c0bSDaniel Bristot de Oliveira out_err:
803829a6c0bSDaniel Bristot de Oliveira 	return -1;
804829a6c0bSDaniel Bristot de Oliveira }
805829a6c0bSDaniel Bristot de Oliveira 
806829a6c0bSDaniel Bristot de Oliveira /*
807829a6c0bSDaniel Bristot de Oliveira  * osnoise_init_hist - initialize a osnoise hist tool with parameters
808829a6c0bSDaniel Bristot de Oliveira  */
809829a6c0bSDaniel Bristot de Oliveira static struct osnoise_tool
osnoise_init_hist(struct osnoise_hist_params * params)810829a6c0bSDaniel Bristot de Oliveira *osnoise_init_hist(struct osnoise_hist_params *params)
811829a6c0bSDaniel Bristot de Oliveira {
812829a6c0bSDaniel Bristot de Oliveira 	struct osnoise_tool *tool;
813829a6c0bSDaniel Bristot de Oliveira 	int nr_cpus;
814829a6c0bSDaniel Bristot de Oliveira 
815829a6c0bSDaniel Bristot de Oliveira 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
816829a6c0bSDaniel Bristot de Oliveira 
817829a6c0bSDaniel Bristot de Oliveira 	tool = osnoise_init_tool("osnoise_hist");
818829a6c0bSDaniel Bristot de Oliveira 	if (!tool)
819829a6c0bSDaniel Bristot de Oliveira 		return NULL;
820829a6c0bSDaniel Bristot de Oliveira 
821829a6c0bSDaniel Bristot de Oliveira 	tool->data = osnoise_alloc_histogram(nr_cpus, params->entries, params->bucket_size);
822829a6c0bSDaniel Bristot de Oliveira 	if (!tool->data)
823829a6c0bSDaniel Bristot de Oliveira 		goto out_err;
824829a6c0bSDaniel Bristot de Oliveira 
825829a6c0bSDaniel Bristot de Oliveira 	tool->params = params;
826829a6c0bSDaniel Bristot de Oliveira 
827829a6c0bSDaniel Bristot de Oliveira 	return tool;
828829a6c0bSDaniel Bristot de Oliveira 
829829a6c0bSDaniel Bristot de Oliveira out_err:
830829a6c0bSDaniel Bristot de Oliveira 	osnoise_destroy_tool(tool);
831829a6c0bSDaniel Bristot de Oliveira 	return NULL;
832829a6c0bSDaniel Bristot de Oliveira }
833829a6c0bSDaniel Bristot de Oliveira 
834829a6c0bSDaniel Bristot de Oliveira static int stop_tracing;
stop_hist(int sig)835829a6c0bSDaniel Bristot de Oliveira static void stop_hist(int sig)
836829a6c0bSDaniel Bristot de Oliveira {
837829a6c0bSDaniel Bristot de Oliveira 	stop_tracing = 1;
838829a6c0bSDaniel Bristot de Oliveira }
839829a6c0bSDaniel Bristot de Oliveira 
840829a6c0bSDaniel Bristot de Oliveira /*
841829a6c0bSDaniel Bristot de Oliveira  * osnoise_hist_set_signals - handles the signal to stop the tool
842829a6c0bSDaniel Bristot de Oliveira  */
843829a6c0bSDaniel Bristot de Oliveira static void
osnoise_hist_set_signals(struct osnoise_hist_params * params)844829a6c0bSDaniel Bristot de Oliveira osnoise_hist_set_signals(struct osnoise_hist_params *params)
845829a6c0bSDaniel Bristot de Oliveira {
846829a6c0bSDaniel Bristot de Oliveira 	signal(SIGINT, stop_hist);
847829a6c0bSDaniel Bristot de Oliveira 	if (params->duration) {
848829a6c0bSDaniel Bristot de Oliveira 		signal(SIGALRM, stop_hist);
849829a6c0bSDaniel Bristot de Oliveira 		alarm(params->duration);
850829a6c0bSDaniel Bristot de Oliveira 	}
851829a6c0bSDaniel Bristot de Oliveira }
852829a6c0bSDaniel Bristot de Oliveira 
osnoise_hist_main(int argc,char * argv[])853829a6c0bSDaniel Bristot de Oliveira int osnoise_hist_main(int argc, char *argv[])
854829a6c0bSDaniel Bristot de Oliveira {
855829a6c0bSDaniel Bristot de Oliveira 	struct osnoise_hist_params *params;
8564bbf59a9SDaniel Bristot de Oliveira 	struct osnoise_tool *record = NULL;
8574bbf59a9SDaniel Bristot de Oliveira 	struct osnoise_tool *tool = NULL;
858829a6c0bSDaniel Bristot de Oliveira 	struct trace_instance *trace;
859829a6c0bSDaniel Bristot de Oliveira 	int return_value = 1;
860829a6c0bSDaniel Bristot de Oliveira 	int retval;
861829a6c0bSDaniel Bristot de Oliveira 
862829a6c0bSDaniel Bristot de Oliveira 	params = osnoise_hist_parse_args(argc, argv);
863829a6c0bSDaniel Bristot de Oliveira 	if (!params)
864829a6c0bSDaniel Bristot de Oliveira 		exit(1);
865829a6c0bSDaniel Bristot de Oliveira 
866829a6c0bSDaniel Bristot de Oliveira 	tool = osnoise_init_hist(params);
867829a6c0bSDaniel Bristot de Oliveira 	if (!tool) {
868829a6c0bSDaniel Bristot de Oliveira 		err_msg("Could not init osnoise hist\n");
869829a6c0bSDaniel Bristot de Oliveira 		goto out_exit;
870829a6c0bSDaniel Bristot de Oliveira 	}
871829a6c0bSDaniel Bristot de Oliveira 
872829a6c0bSDaniel Bristot de Oliveira 	retval = osnoise_hist_apply_config(tool, params);
873829a6c0bSDaniel Bristot de Oliveira 	if (retval) {
874829a6c0bSDaniel Bristot de Oliveira 		err_msg("Could not apply config\n");
875829a6c0bSDaniel Bristot de Oliveira 		goto out_destroy;
876829a6c0bSDaniel Bristot de Oliveira 	}
877829a6c0bSDaniel Bristot de Oliveira 
878829a6c0bSDaniel Bristot de Oliveira 	trace = &tool->trace;
879829a6c0bSDaniel Bristot de Oliveira 
880829a6c0bSDaniel Bristot de Oliveira 	retval = enable_osnoise(trace);
881829a6c0bSDaniel Bristot de Oliveira 	if (retval) {
882829a6c0bSDaniel Bristot de Oliveira 		err_msg("Failed to enable osnoise tracer\n");
883829a6c0bSDaniel Bristot de Oliveira 		goto out_destroy;
884829a6c0bSDaniel Bristot de Oliveira 	}
885829a6c0bSDaniel Bristot de Oliveira 
886829a6c0bSDaniel Bristot de Oliveira 	retval = osnoise_init_trace_hist(tool);
887829a6c0bSDaniel Bristot de Oliveira 	if (retval)
888829a6c0bSDaniel Bristot de Oliveira 		goto out_destroy;
889829a6c0bSDaniel Bristot de Oliveira 
890829a6c0bSDaniel Bristot de Oliveira 	if (params->set_sched) {
891829a6c0bSDaniel Bristot de Oliveira 		retval = set_comm_sched_attr("osnoise/", &params->sched_param);
892829a6c0bSDaniel Bristot de Oliveira 		if (retval) {
893829a6c0bSDaniel Bristot de Oliveira 			err_msg("Failed to set sched parameters\n");
8942a6b52edSWan Jiabing 			goto out_free;
895829a6c0bSDaniel Bristot de Oliveira 		}
896829a6c0bSDaniel Bristot de Oliveira 	}
897829a6c0bSDaniel Bristot de Oliveira 
898a957cbc0SDaniel Bristot de Oliveira 	if (params->cgroup) {
899a957cbc0SDaniel Bristot de Oliveira 		retval = set_comm_cgroup("timerlat/", params->cgroup_name);
900a957cbc0SDaniel Bristot de Oliveira 		if (!retval) {
901a957cbc0SDaniel Bristot de Oliveira 			err_msg("Failed to move threads to cgroup\n");
902a957cbc0SDaniel Bristot de Oliveira 			goto out_free;
903a957cbc0SDaniel Bristot de Oliveira 		}
904a957cbc0SDaniel Bristot de Oliveira 	}
905a957cbc0SDaniel Bristot de Oliveira 
906829a6c0bSDaniel Bristot de Oliveira 	if (params->trace_output) {
907829a6c0bSDaniel Bristot de Oliveira 		record = osnoise_init_trace_tool("osnoise");
908829a6c0bSDaniel Bristot de Oliveira 		if (!record) {
909829a6c0bSDaniel Bristot de Oliveira 			err_msg("Failed to enable the trace instance\n");
9102a6b52edSWan Jiabing 			goto out_free;
911829a6c0bSDaniel Bristot de Oliveira 		}
91251d64c3aSDaniel Bristot de Oliveira 
91351d64c3aSDaniel Bristot de Oliveira 		if (params->events) {
91451d64c3aSDaniel Bristot de Oliveira 			retval = trace_events_enable(&record->trace, params->events);
91551d64c3aSDaniel Bristot de Oliveira 			if (retval)
91651d64c3aSDaniel Bristot de Oliveira 				goto out_hist;
91751d64c3aSDaniel Bristot de Oliveira 		}
91851d64c3aSDaniel Bristot de Oliveira 
919e9a4062eSDaniel Bristot de Oliveira 		if (params->buffer_size > 0) {
920e9a4062eSDaniel Bristot de Oliveira 			retval = trace_set_buffer_size(&record->trace, params->buffer_size);
921e9a4062eSDaniel Bristot de Oliveira 			if (retval)
922e9a4062eSDaniel Bristot de Oliveira 				goto out_hist;
923e9a4062eSDaniel Bristot de Oliveira 		}
924829a6c0bSDaniel Bristot de Oliveira 	}
925829a6c0bSDaniel Bristot de Oliveira 
92657cf76ecSDaniel Bristot de Oliveira 	/*
92757cf76ecSDaniel Bristot de Oliveira 	 * Start the tracer here, after having set all instances.
92857cf76ecSDaniel Bristot de Oliveira 	 *
92957cf76ecSDaniel Bristot de Oliveira 	 * Let the trace instance start first for the case of hitting a stop
93057cf76ecSDaniel Bristot de Oliveira 	 * tracing while enabling other instances. The trace instance is the
93157cf76ecSDaniel Bristot de Oliveira 	 * one with most valuable information.
93257cf76ecSDaniel Bristot de Oliveira 	 */
93357cf76ecSDaniel Bristot de Oliveira 	if (params->trace_output)
93457cf76ecSDaniel Bristot de Oliveira 		trace_instance_start(&record->trace);
93557cf76ecSDaniel Bristot de Oliveira 	trace_instance_start(trace);
93657cf76ecSDaniel Bristot de Oliveira 
937cdbf7196SDaniel Bristot de Oliveira 	if (params->warmup > 0) {
938cdbf7196SDaniel Bristot de Oliveira 		debug_msg("Warming up for %d seconds\n", params->warmup);
939cdbf7196SDaniel Bristot de Oliveira 		sleep(params->warmup);
940cdbf7196SDaniel Bristot de Oliveira 		if (stop_tracing)
941cdbf7196SDaniel Bristot de Oliveira 			goto out_hist;
942cdbf7196SDaniel Bristot de Oliveira 
943cdbf7196SDaniel Bristot de Oliveira 		/*
944cdbf7196SDaniel Bristot de Oliveira 		 * Clean up the buffer. The osnoise workload do not run
945cdbf7196SDaniel Bristot de Oliveira 		 * with tracing off to avoid creating a performance penalty
946cdbf7196SDaniel Bristot de Oliveira 		 * when not needed.
947cdbf7196SDaniel Bristot de Oliveira 		 */
948cdbf7196SDaniel Bristot de Oliveira 		retval = tracefs_instance_file_write(trace->inst, "trace", "");
949cdbf7196SDaniel Bristot de Oliveira 		if (retval < 0) {
950cdbf7196SDaniel Bristot de Oliveira 			debug_msg("Error cleaning up the buffer");
951cdbf7196SDaniel Bristot de Oliveira 			goto out_hist;
952cdbf7196SDaniel Bristot de Oliveira 		}
953cdbf7196SDaniel Bristot de Oliveira 
954cdbf7196SDaniel Bristot de Oliveira 	}
955cdbf7196SDaniel Bristot de Oliveira 
956829a6c0bSDaniel Bristot de Oliveira 	tool->start_time = time(NULL);
957829a6c0bSDaniel Bristot de Oliveira 	osnoise_hist_set_signals(params);
958829a6c0bSDaniel Bristot de Oliveira 
959829a6c0bSDaniel Bristot de Oliveira 	while (!stop_tracing) {
960829a6c0bSDaniel Bristot de Oliveira 		sleep(params->sleep_time);
961829a6c0bSDaniel Bristot de Oliveira 
962829a6c0bSDaniel Bristot de Oliveira 		retval = tracefs_iterate_raw_events(trace->tep,
963829a6c0bSDaniel Bristot de Oliveira 						    trace->inst,
964829a6c0bSDaniel Bristot de Oliveira 						    NULL,
965829a6c0bSDaniel Bristot de Oliveira 						    0,
966829a6c0bSDaniel Bristot de Oliveira 						    collect_registered_events,
967829a6c0bSDaniel Bristot de Oliveira 						    trace);
968829a6c0bSDaniel Bristot de Oliveira 		if (retval < 0) {
969829a6c0bSDaniel Bristot de Oliveira 			err_msg("Error iterating on events\n");
970829a6c0bSDaniel Bristot de Oliveira 			goto out_hist;
971829a6c0bSDaniel Bristot de Oliveira 		}
972829a6c0bSDaniel Bristot de Oliveira 
97328d2160cSDaniel Bristot de Oliveira 		if (trace_is_off(&tool->trace, &record->trace))
974829a6c0bSDaniel Bristot de Oliveira 			break;
97575016ca3SDaniel Bristot de Oliveira 	}
976829a6c0bSDaniel Bristot de Oliveira 
977829a6c0bSDaniel Bristot de Oliveira 	osnoise_read_trace_hist(tool);
978829a6c0bSDaniel Bristot de Oliveira 
979829a6c0bSDaniel Bristot de Oliveira 	osnoise_print_stats(params, tool);
980829a6c0bSDaniel Bristot de Oliveira 
981829a6c0bSDaniel Bristot de Oliveira 	return_value = 0;
982829a6c0bSDaniel Bristot de Oliveira 
98328d2160cSDaniel Bristot de Oliveira 	if (trace_is_off(&tool->trace, &record->trace)) {
9847d38c351SDaniel Bristot de Oliveira 		printf("rtla osnoise hit stop tracing\n");
985829a6c0bSDaniel Bristot de Oliveira 		if (params->trace_output) {
986829a6c0bSDaniel Bristot de Oliveira 			printf("  Saving trace to %s\n", params->trace_output);
987829a6c0bSDaniel Bristot de Oliveira 			save_trace_to_file(record->trace.inst, params->trace_output);
988829a6c0bSDaniel Bristot de Oliveira 		}
989829a6c0bSDaniel Bristot de Oliveira 	}
990829a6c0bSDaniel Bristot de Oliveira 
991829a6c0bSDaniel Bristot de Oliveira out_hist:
99251d64c3aSDaniel Bristot de Oliveira 	trace_events_destroy(&record->trace, params->events);
99351d64c3aSDaniel Bristot de Oliveira 	params->events = NULL;
9942a6b52edSWan Jiabing out_free:
995829a6c0bSDaniel Bristot de Oliveira 	osnoise_free_histogram(tool->data);
996829a6c0bSDaniel Bristot de Oliveira out_destroy:
997829a6c0bSDaniel Bristot de Oliveira 	osnoise_destroy_tool(record);
9984bbf59a9SDaniel Bristot de Oliveira 	osnoise_destroy_tool(tool);
999829a6c0bSDaniel Bristot de Oliveira 	free(params);
1000829a6c0bSDaniel Bristot de Oliveira out_exit:
1001829a6c0bSDaniel Bristot de Oliveira 	exit(return_value);
1002829a6c0bSDaniel Bristot de Oliveira }
1003