xref: /linux/tools/tracing/rtla/src/osnoise_hist.c (revision 5d9af63e80b5a202e69ce5bcf54af320e46f397a)
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 <stdlib.h>
8 #include <string.h>
9 #include <signal.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <time.h>
13 
14 #include "osnoise.h"
15 #include "cli.h"
16 
17 struct osnoise_hist_cpu {
18 	int			*samples;
19 	int			count;
20 
21 	unsigned long long	min_sample;
22 	unsigned long long	sum_sample;
23 	unsigned long long	max_sample;
24 
25 };
26 
27 struct osnoise_hist_data {
28 	struct tracefs_hist	*trace_hist;
29 	struct osnoise_hist_cpu	*hist;
30 	int			entries;
31 	int			bucket_size;
32 };
33 
34 /*
35  * osnoise_free_histogram - free runtime data
36  */
37 static void
38 osnoise_free_histogram(struct osnoise_hist_data *data)
39 {
40 	int cpu;
41 
42 	/* one histogram for IRQ and one for thread, per CPU */
43 	for (cpu = 0; cpu < nr_cpus; cpu++) {
44 		if (data->hist[cpu].samples)
45 			free(data->hist[cpu].samples);
46 	}
47 
48 	/* one set of histograms per CPU */
49 	if (data->hist)
50 		free(data->hist);
51 
52 	free(data);
53 }
54 
55 static void osnoise_free_hist_tool(struct osnoise_tool *tool)
56 {
57 	osnoise_free_histogram(tool->data);
58 }
59 
60 /*
61  * osnoise_alloc_histogram - alloc runtime data
62  */
63 static struct osnoise_hist_data
64 *osnoise_alloc_histogram(int entries, int bucket_size)
65 {
66 	struct osnoise_hist_data *data;
67 	int cpu;
68 
69 	data = calloc(1, sizeof(*data));
70 	if (!data)
71 		return NULL;
72 
73 	data->entries = entries;
74 	data->bucket_size = bucket_size;
75 
76 	data->hist = calloc(1, sizeof(*data->hist) * nr_cpus);
77 	if (!data->hist)
78 		goto cleanup;
79 
80 	for (cpu = 0; cpu < nr_cpus; cpu++) {
81 		data->hist[cpu].samples = calloc(1, sizeof(*data->hist->samples) * (entries + 1));
82 		if (!data->hist[cpu].samples)
83 			goto cleanup;
84 	}
85 
86 	/* set the min to max */
87 	for (cpu = 0; cpu < nr_cpus; cpu++)
88 		data->hist[cpu].min_sample = ~0;
89 
90 	return data;
91 
92 cleanup:
93 	osnoise_free_histogram(data);
94 	return NULL;
95 }
96 
97 static void osnoise_hist_update_multiple(struct osnoise_tool *tool, int cpu,
98 					 unsigned long long duration, int count)
99 {
100 	struct osnoise_params *params = to_osnoise_params(tool->params);
101 	struct osnoise_hist_data *data = tool->data;
102 	unsigned long long total_duration;
103 	int entries = data->entries;
104 	int bucket;
105 	int *hist;
106 
107 	if (params->common.output_divisor)
108 		duration = duration / params->common.output_divisor;
109 
110 	bucket = duration / data->bucket_size;
111 
112 	total_duration = duration * count;
113 
114 	hist = data->hist[cpu].samples;
115 	data->hist[cpu].count += count;
116 	update_min(&data->hist[cpu].min_sample, &duration);
117 	update_sum(&data->hist[cpu].sum_sample, &total_duration);
118 	update_max(&data->hist[cpu].max_sample, &duration);
119 
120 	if (bucket < entries)
121 		hist[bucket] += count;
122 	else
123 		hist[entries] += count;
124 }
125 
126 /*
127  * osnoise_destroy_trace_hist - disable events used to collect histogram
128  */
129 static void osnoise_destroy_trace_hist(struct osnoise_tool *tool)
130 {
131 	struct osnoise_hist_data *data = tool->data;
132 
133 	tracefs_hist_pause(tool->trace.inst, data->trace_hist);
134 	tracefs_hist_destroy(tool->trace.inst, data->trace_hist);
135 }
136 
137 /*
138  * osnoise_init_trace_hist - enable events used to collect histogram
139  */
140 static int osnoise_init_trace_hist(struct osnoise_tool *tool)
141 {
142 	struct osnoise_params *params = to_osnoise_params(tool->params);
143 	struct osnoise_hist_data *data = tool->data;
144 	int bucket_size;
145 	char buff[128];
146 	int retval = 0;
147 
148 	/*
149 	 * Set the size of the bucket.
150 	 */
151 	bucket_size = params->common.output_divisor * params->common.hist.bucket_size;
152 	snprintf(buff, sizeof(buff), "duration.buckets=%d", bucket_size);
153 
154 	data->trace_hist = tracefs_hist_alloc(tool->trace.tep, "osnoise", "sample_threshold",
155 			buff, TRACEFS_HIST_KEY_NORMAL);
156 	if (!data->trace_hist)
157 		return 1;
158 
159 	retval = tracefs_hist_add_key(data->trace_hist, "cpu", 0);
160 	if (retval)
161 		goto out_err;
162 
163 	retval = tracefs_hist_start(tool->trace.inst, data->trace_hist);
164 	if (retval)
165 		goto out_err;
166 
167 	return 0;
168 
169 out_err:
170 	osnoise_destroy_trace_hist(tool);
171 	return 1;
172 }
173 
174 /*
175  * osnoise_read_trace_hist - parse histogram file and file osnoise histogram
176  */
177 static void osnoise_read_trace_hist(struct osnoise_tool *tool)
178 {
179 	struct osnoise_hist_data *data = tool->data;
180 	long long cpu, counter, duration;
181 	char *content, *position;
182 
183 	tracefs_hist_pause(tool->trace.inst, data->trace_hist);
184 
185 	content = tracefs_event_file_read(tool->trace.inst, "osnoise",
186 					  "sample_threshold",
187 					  "hist", NULL);
188 	if (!content)
189 		return;
190 
191 	position = content;
192 	while (true) {
193 		position = strstr(position, "duration: ~");
194 		if (!position)
195 			break;
196 		position += strlen("duration: ~");
197 		duration = get_llong_from_str(position);
198 		if (duration == -1)
199 			err_msg("error reading duration from histogram\n");
200 
201 		position = strstr(position, "cpu:");
202 		if (!position)
203 			break;
204 		position += strlen("cpu: ");
205 		cpu = get_llong_from_str(position);
206 		if (cpu == -1)
207 			err_msg("error reading cpu from histogram\n");
208 
209 		position = strstr(position, "hitcount:");
210 		if (!position)
211 			break;
212 		position += strlen("hitcount: ");
213 		counter = get_llong_from_str(position);
214 		if (counter == -1)
215 			err_msg("error reading counter from histogram\n");
216 
217 		osnoise_hist_update_multiple(tool, cpu, duration, counter);
218 	}
219 	free(content);
220 }
221 
222 /*
223  * osnoise_hist_header - print the header of the tracer to the output
224  */
225 static void osnoise_hist_header(struct osnoise_tool *tool)
226 {
227 	struct osnoise_params *params = to_osnoise_params(tool->params);
228 	struct osnoise_hist_data *data = tool->data;
229 	struct trace_seq *s = tool->trace.seq;
230 	char duration[26];
231 	int cpu;
232 
233 	if (params->common.hist.no_header)
234 		return;
235 
236 	get_duration(tool->start_time, duration, sizeof(duration));
237 	trace_seq_printf(s, "# RTLA osnoise histogram\n");
238 	trace_seq_printf(s, "# Time unit is %s (%s)\n",
239 			params->common.output_divisor == 1 ? "nanoseconds" : "microseconds",
240 			params->common.output_divisor == 1 ? "ns" : "us");
241 
242 	trace_seq_printf(s, "# Duration: %s\n", duration);
243 
244 	if (!params->common.hist.no_index)
245 		trace_seq_printf(s, "Index");
246 
247 	for_each_monitored_cpu(cpu, &params->common) {
248 
249 		if (!data->hist[cpu].count)
250 			continue;
251 
252 		trace_seq_printf(s, "   CPU-%03d", cpu);
253 	}
254 	trace_seq_printf(s, "\n");
255 
256 	trace_seq_do_printf(s);
257 	trace_seq_reset(s);
258 }
259 
260 /*
261  * osnoise_print_summary - print the summary of the hist data to the output
262  */
263 static void
264 osnoise_print_summary(struct osnoise_params *params,
265 		       struct trace_instance *trace,
266 		       struct osnoise_hist_data *data)
267 {
268 	int cpu;
269 
270 	if (params->common.hist.no_summary)
271 		return;
272 
273 	if (!params->common.hist.no_index)
274 		trace_seq_printf(trace->seq, "count:");
275 
276 	for_each_monitored_cpu(cpu, &params->common) {
277 		if (!data->hist[cpu].count)
278 			continue;
279 
280 		trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].count);
281 	}
282 	trace_seq_printf(trace->seq, "\n");
283 
284 	if (!params->common.hist.no_index)
285 		trace_seq_printf(trace->seq, "min:  ");
286 
287 	for_each_monitored_cpu(cpu, &params->common) {
288 
289 		if (!data->hist[cpu].count)
290 			continue;
291 
292 		trace_seq_printf(trace->seq, "%9llu ",	data->hist[cpu].min_sample);
293 
294 	}
295 	trace_seq_printf(trace->seq, "\n");
296 
297 	if (!params->common.hist.no_index)
298 		trace_seq_printf(trace->seq, "avg:  ");
299 
300 	for_each_monitored_cpu(cpu, &params->common) {
301 
302 		if (!data->hist[cpu].count)
303 			continue;
304 
305 		if (data->hist[cpu].count)
306 			trace_seq_printf(trace->seq, "%9.2f ",
307 				((double) data->hist[cpu].sum_sample) / data->hist[cpu].count);
308 		else
309 			trace_seq_printf(trace->seq, "        - ");
310 	}
311 	trace_seq_printf(trace->seq, "\n");
312 
313 	if (!params->common.hist.no_index)
314 		trace_seq_printf(trace->seq, "max:  ");
315 
316 	for_each_monitored_cpu(cpu, &params->common) {
317 
318 		if (!data->hist[cpu].count)
319 			continue;
320 
321 		trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].max_sample);
322 
323 	}
324 	trace_seq_printf(trace->seq, "\n");
325 	trace_seq_do_printf(trace->seq);
326 	trace_seq_reset(trace->seq);
327 }
328 
329 /*
330  * osnoise_print_stats - print data for all CPUs
331  */
332 static void
333 osnoise_print_stats(struct osnoise_tool *tool)
334 {
335 	struct osnoise_params *params = to_osnoise_params(tool->params);
336 	struct osnoise_hist_data *data = tool->data;
337 	struct trace_instance *trace = &tool->trace;
338 	int has_samples = 0;
339 	int bucket, cpu;
340 	int total;
341 
342 	osnoise_hist_header(tool);
343 
344 	for (bucket = 0; bucket < data->entries; bucket++) {
345 		total = 0;
346 
347 		if (!params->common.hist.no_index)
348 			trace_seq_printf(trace->seq, "%-6d",
349 					 bucket * data->bucket_size);
350 
351 		for_each_monitored_cpu(cpu, &params->common) {
352 
353 			if (!data->hist[cpu].count)
354 				continue;
355 
356 			total += data->hist[cpu].samples[bucket];
357 			trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].samples[bucket]);
358 		}
359 
360 		if (total == 0 && !params->common.hist.with_zeros) {
361 			trace_seq_reset(trace->seq);
362 			continue;
363 		}
364 
365 		/* There are samples above the threshold */
366 		has_samples = 1;
367 		trace_seq_printf(trace->seq, "\n");
368 		trace_seq_do_printf(trace->seq);
369 		trace_seq_reset(trace->seq);
370 	}
371 
372 	/*
373 	 * If no samples were recorded, skip calculations, print zeroed statistics
374 	 * and return.
375 	 */
376 	if (!has_samples) {
377 		trace_seq_reset(trace->seq);
378 		trace_seq_printf(trace->seq, "over: 0\ncount: 0\nmin: 0\navg: 0\nmax: 0\n");
379 		trace_seq_do_printf(trace->seq);
380 		trace_seq_reset(trace->seq);
381 		return;
382 	}
383 
384 	if (!params->common.hist.no_index)
385 		trace_seq_printf(trace->seq, "over: ");
386 
387 	for_each_monitored_cpu(cpu, &params->common) {
388 
389 		if (!data->hist[cpu].count)
390 			continue;
391 
392 		trace_seq_printf(trace->seq, "%9d ",
393 				 data->hist[cpu].samples[data->entries]);
394 	}
395 	trace_seq_printf(trace->seq, "\n");
396 	trace_seq_do_printf(trace->seq);
397 	trace_seq_reset(trace->seq);
398 
399 	osnoise_print_summary(params, trace, data);
400 	osnoise_report_missed_events(tool);
401 }
402 
403 /*
404  * osnoise_hist_apply_config - apply the hist configs to the initialized tool
405  */
406 static int
407 osnoise_hist_apply_config(struct osnoise_tool *tool)
408 {
409 	return osnoise_apply_config(tool, to_osnoise_params(tool->params));
410 }
411 
412 /*
413  * osnoise_init_hist - initialize a osnoise hist tool with parameters
414  */
415 static struct osnoise_tool
416 *osnoise_init_hist(struct common_params *params)
417 {
418 	struct osnoise_tool *tool;
419 
420 	tool = osnoise_init_tool("osnoise_hist");
421 	if (!tool)
422 		return NULL;
423 
424 	tool->data = osnoise_alloc_histogram(params->hist.entries,
425 					     params->hist.bucket_size);
426 	if (!tool->data)
427 		goto out_err;
428 
429 	return tool;
430 
431 out_err:
432 	osnoise_destroy_tool(tool);
433 	return NULL;
434 }
435 
436 static int osnoise_hist_enable(struct osnoise_tool *tool)
437 {
438 	int retval;
439 
440 	retval = osnoise_init_trace_hist(tool);
441 	if (retval)
442 		return retval;
443 
444 	return osnoise_enable(tool);
445 }
446 
447 static int osnoise_hist_main_loop(struct osnoise_tool *tool)
448 {
449 	int retval;
450 
451 	retval = hist_main_loop(tool);
452 	osnoise_read_trace_hist(tool);
453 
454 	return retval;
455 }
456 
457 struct tool_ops osnoise_hist_ops = {
458 	.tracer = "osnoise",
459 	.comm_prefix = "osnoise/",
460 	.parse_args = osnoise_hist_parse_args,
461 	.init_tool = osnoise_init_hist,
462 	.apply_config = osnoise_hist_apply_config,
463 	.enable = osnoise_hist_enable,
464 	.main = osnoise_hist_main_loop,
465 	.print_stats = osnoise_print_stats,
466 	.free = osnoise_free_hist_tool,
467 };
468