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
15 #include "osnoise.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 int nr_cpus;
33 };
34
35 /*
36 * osnoise_free_histogram - free runtime data
37 */
38 static void
osnoise_free_histogram(struct osnoise_hist_data * data)39 osnoise_free_histogram(struct osnoise_hist_data *data)
40 {
41 int cpu;
42
43 /* one histogram for IRQ and one for thread, per CPU */
44 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
45 if (data->hist[cpu].samples)
46 free(data->hist[cpu].samples);
47 }
48
49 /* one set of histograms per CPU */
50 if (data->hist)
51 free(data->hist);
52
53 free(data);
54 }
55
osnoise_free_hist_tool(struct osnoise_tool * tool)56 static void osnoise_free_hist_tool(struct osnoise_tool *tool)
57 {
58 osnoise_free_histogram(tool->data);
59 }
60
61 /*
62 * osnoise_alloc_histogram - alloc runtime data
63 */
64 static struct osnoise_hist_data
osnoise_alloc_histogram(int nr_cpus,int entries,int bucket_size)65 *osnoise_alloc_histogram(int nr_cpus, int entries, int bucket_size)
66 {
67 struct osnoise_hist_data *data;
68 int cpu;
69
70 data = calloc(1, sizeof(*data));
71 if (!data)
72 return NULL;
73
74 data->entries = entries;
75 data->bucket_size = bucket_size;
76 data->nr_cpus = nr_cpus;
77
78 data->hist = calloc(1, sizeof(*data->hist) * nr_cpus);
79 if (!data->hist)
80 goto cleanup;
81
82 for (cpu = 0; cpu < nr_cpus; cpu++) {
83 data->hist[cpu].samples = calloc(1, sizeof(*data->hist->samples) * (entries + 1));
84 if (!data->hist[cpu].samples)
85 goto cleanup;
86 }
87
88 /* set the min to max */
89 for (cpu = 0; cpu < nr_cpus; cpu++)
90 data->hist[cpu].min_sample = ~0;
91
92 return data;
93
94 cleanup:
95 osnoise_free_histogram(data);
96 return NULL;
97 }
98
osnoise_hist_update_multiple(struct osnoise_tool * tool,int cpu,unsigned long long duration,int count)99 static void osnoise_hist_update_multiple(struct osnoise_tool *tool, int cpu,
100 unsigned long long duration, int count)
101 {
102 struct osnoise_params *params = to_osnoise_params(tool->params);
103 struct osnoise_hist_data *data = tool->data;
104 unsigned long long total_duration;
105 int entries = data->entries;
106 int bucket;
107 int *hist;
108
109 if (params->common.output_divisor)
110 duration = duration / params->common.output_divisor;
111
112 bucket = duration / data->bucket_size;
113
114 total_duration = duration * count;
115
116 hist = data->hist[cpu].samples;
117 data->hist[cpu].count += count;
118 update_min(&data->hist[cpu].min_sample, &duration);
119 update_sum(&data->hist[cpu].sum_sample, &total_duration);
120 update_max(&data->hist[cpu].max_sample, &duration);
121
122 if (bucket < entries)
123 hist[bucket] += count;
124 else
125 hist[entries] += count;
126 }
127
128 /*
129 * osnoise_destroy_trace_hist - disable events used to collect histogram
130 */
osnoise_destroy_trace_hist(struct osnoise_tool * tool)131 static void osnoise_destroy_trace_hist(struct osnoise_tool *tool)
132 {
133 struct osnoise_hist_data *data = tool->data;
134
135 tracefs_hist_pause(tool->trace.inst, data->trace_hist);
136 tracefs_hist_destroy(tool->trace.inst, data->trace_hist);
137 }
138
139 /*
140 * osnoise_init_trace_hist - enable events used to collect histogram
141 */
osnoise_init_trace_hist(struct osnoise_tool * tool)142 static int osnoise_init_trace_hist(struct osnoise_tool *tool)
143 {
144 struct osnoise_params *params = to_osnoise_params(tool->params);
145 struct osnoise_hist_data *data = tool->data;
146 int bucket_size;
147 char buff[128];
148 int retval = 0;
149
150 /*
151 * Set the size of the bucket.
152 */
153 bucket_size = params->common.output_divisor * params->common.hist.bucket_size;
154 snprintf(buff, sizeof(buff), "duration.buckets=%d", bucket_size);
155
156 data->trace_hist = tracefs_hist_alloc(tool->trace.tep, "osnoise", "sample_threshold",
157 buff, TRACEFS_HIST_KEY_NORMAL);
158 if (!data->trace_hist)
159 return 1;
160
161 retval = tracefs_hist_add_key(data->trace_hist, "cpu", 0);
162 if (retval)
163 goto out_err;
164
165 retval = tracefs_hist_start(tool->trace.inst, data->trace_hist);
166 if (retval)
167 goto out_err;
168
169 return 0;
170
171 out_err:
172 osnoise_destroy_trace_hist(tool);
173 return 1;
174 }
175
176 /*
177 * osnoise_read_trace_hist - parse histogram file and file osnoise histogram
178 */
osnoise_read_trace_hist(struct osnoise_tool * tool)179 static void osnoise_read_trace_hist(struct osnoise_tool *tool)
180 {
181 struct osnoise_hist_data *data = tool->data;
182 long long cpu, counter, duration;
183 char *content, *position;
184
185 tracefs_hist_pause(tool->trace.inst, data->trace_hist);
186
187 content = tracefs_event_file_read(tool->trace.inst, "osnoise",
188 "sample_threshold",
189 "hist", NULL);
190 if (!content)
191 return;
192
193 position = content;
194 while (true) {
195 position = strstr(position, "duration: ~");
196 if (!position)
197 break;
198 position += strlen("duration: ~");
199 duration = get_llong_from_str(position);
200 if (duration == -1)
201 err_msg("error reading duration from histogram\n");
202
203 position = strstr(position, "cpu:");
204 if (!position)
205 break;
206 position += strlen("cpu: ");
207 cpu = get_llong_from_str(position);
208 if (cpu == -1)
209 err_msg("error reading cpu from histogram\n");
210
211 position = strstr(position, "hitcount:");
212 if (!position)
213 break;
214 position += strlen("hitcount: ");
215 counter = get_llong_from_str(position);
216 if (counter == -1)
217 err_msg("error reading counter from histogram\n");
218
219 osnoise_hist_update_multiple(tool, cpu, duration, counter);
220 }
221 free(content);
222 }
223
224 /*
225 * osnoise_hist_header - print the header of the tracer to the output
226 */
osnoise_hist_header(struct osnoise_tool * tool)227 static void osnoise_hist_header(struct osnoise_tool *tool)
228 {
229 struct osnoise_params *params = to_osnoise_params(tool->params);
230 struct osnoise_hist_data *data = tool->data;
231 struct trace_seq *s = tool->trace.seq;
232 char duration[26];
233 int cpu;
234
235 if (params->common.hist.no_header)
236 return;
237
238 get_duration(tool->start_time, duration, sizeof(duration));
239 trace_seq_printf(s, "# RTLA osnoise histogram\n");
240 trace_seq_printf(s, "# Time unit is %s (%s)\n",
241 params->common.output_divisor == 1 ? "nanoseconds" : "microseconds",
242 params->common.output_divisor == 1 ? "ns" : "us");
243
244 trace_seq_printf(s, "# Duration: %s\n", duration);
245
246 if (!params->common.hist.no_index)
247 trace_seq_printf(s, "Index");
248
249 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {
250
251 if (!data->hist[cpu].count)
252 continue;
253
254 trace_seq_printf(s, " CPU-%03d", cpu);
255 }
256 trace_seq_printf(s, "\n");
257
258 trace_seq_do_printf(s);
259 trace_seq_reset(s);
260 }
261
262 /*
263 * osnoise_print_summary - print the summary of the hist data to the output
264 */
265 static void
osnoise_print_summary(struct osnoise_params * params,struct trace_instance * trace,struct osnoise_hist_data * data)266 osnoise_print_summary(struct osnoise_params *params,
267 struct trace_instance *trace,
268 struct osnoise_hist_data *data)
269 {
270 int cpu;
271
272 if (params->common.hist.no_summary)
273 return;
274
275 if (!params->common.hist.no_index)
276 trace_seq_printf(trace->seq, "count:");
277
278 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {
279
280 if (!data->hist[cpu].count)
281 continue;
282
283 trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].count);
284 }
285 trace_seq_printf(trace->seq, "\n");
286
287 if (!params->common.hist.no_index)
288 trace_seq_printf(trace->seq, "min: ");
289
290 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {
291
292 if (!data->hist[cpu].count)
293 continue;
294
295 trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].min_sample);
296
297 }
298 trace_seq_printf(trace->seq, "\n");
299
300 if (!params->common.hist.no_index)
301 trace_seq_printf(trace->seq, "avg: ");
302
303 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {
304
305 if (!data->hist[cpu].count)
306 continue;
307
308 if (data->hist[cpu].count)
309 trace_seq_printf(trace->seq, "%9.2f ",
310 ((double) data->hist[cpu].sum_sample) / data->hist[cpu].count);
311 else
312 trace_seq_printf(trace->seq, " - ");
313 }
314 trace_seq_printf(trace->seq, "\n");
315
316 if (!params->common.hist.no_index)
317 trace_seq_printf(trace->seq, "max: ");
318
319 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {
320
321 if (!data->hist[cpu].count)
322 continue;
323
324 trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].max_sample);
325
326 }
327 trace_seq_printf(trace->seq, "\n");
328 trace_seq_do_printf(trace->seq);
329 trace_seq_reset(trace->seq);
330 }
331
332 /*
333 * osnoise_print_stats - print data for all CPUs
334 */
335 static void
osnoise_print_stats(struct osnoise_tool * tool)336 osnoise_print_stats(struct osnoise_tool *tool)
337 {
338 struct osnoise_params *params = to_osnoise_params(tool->params);
339 struct osnoise_hist_data *data = tool->data;
340 struct trace_instance *trace = &tool->trace;
341 int has_samples = 0;
342 int bucket, cpu;
343 int total;
344
345 osnoise_hist_header(tool);
346
347 for (bucket = 0; bucket < data->entries; bucket++) {
348 total = 0;
349
350 if (!params->common.hist.no_index)
351 trace_seq_printf(trace->seq, "%-6d",
352 bucket * data->bucket_size);
353
354 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {
355
356 if (!data->hist[cpu].count)
357 continue;
358
359 total += data->hist[cpu].samples[bucket];
360 trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].samples[bucket]);
361 }
362
363 if (total == 0 && !params->common.hist.with_zeros) {
364 trace_seq_reset(trace->seq);
365 continue;
366 }
367
368 /* There are samples above the threshold */
369 has_samples = 1;
370 trace_seq_printf(trace->seq, "\n");
371 trace_seq_do_printf(trace->seq);
372 trace_seq_reset(trace->seq);
373 }
374
375 /*
376 * If no samples were recorded, skip calculations, print zeroed statistics
377 * and return.
378 */
379 if (!has_samples) {
380 trace_seq_reset(trace->seq);
381 trace_seq_printf(trace->seq, "over: 0\ncount: 0\nmin: 0\navg: 0\nmax: 0\n");
382 trace_seq_do_printf(trace->seq);
383 trace_seq_reset(trace->seq);
384 return;
385 }
386
387 if (!params->common.hist.no_index)
388 trace_seq_printf(trace->seq, "over: ");
389
390 for_each_monitored_cpu(cpu, data->nr_cpus, ¶ms->common) {
391
392 if (!data->hist[cpu].count)
393 continue;
394
395 trace_seq_printf(trace->seq, "%9d ",
396 data->hist[cpu].samples[data->entries]);
397 }
398 trace_seq_printf(trace->seq, "\n");
399 trace_seq_do_printf(trace->seq);
400 trace_seq_reset(trace->seq);
401
402 osnoise_print_summary(params, trace, data);
403 osnoise_report_missed_events(tool);
404 }
405
406 /*
407 * osnoise_hist_usage - prints osnoise hist usage message
408 */
osnoise_hist_usage(void)409 static void osnoise_hist_usage(void)
410 {
411 static const char * const msg_start[] = {
412 "[-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",
413 " [-T us] [-t [file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\",
414 " [-c cpu-list] [-H cpu-list] [-P priority] [-b N] [-E N] [--no-header] [--no-summary] \\",
415 " [--no-index] [--with-zeros] [-C [cgroup_name]] [--warm-up]",
416 NULL,
417 };
418
419 static const char * const msg_opts[] = {
420 " -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit",
421 " -p/--period us: osnoise period in us",
422 " -r/--runtime us: osnoise runtime in us",
423 " -s/--stop us: stop trace if a single sample is higher than the argument in us",
424 " -S/--stop-total us: stop trace if the total sample is higher than the argument in us",
425 " -T/--threshold us: the minimum delta to be considered a noise",
426 " -c/--cpus cpu-list: list of cpus to run osnoise threads",
427 " -H/--house-keeping cpus: run rtla control threads only on the given cpus",
428 " -C/--cgroup [cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",
429 " -d/--duration time[s|m|h|d]: duration of the session",
430 " -D/--debug: print debug info",
431 " -t/--trace [file]: save the stopped trace to [file|osnoise_trace.txt]",
432 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
433 " --filter <filter>: enable a trace event filter to the previous -e event",
434 " --trigger <trigger>: enable a trace event trigger to the previous -e event",
435 " -b/--bucket-size N: set the histogram bucket size (default 1)",
436 " -E/--entries N: set the number of entries of the histogram (default 256)",
437 " --no-header: do not print header",
438 " --no-summary: do not print summary",
439 " --no-index: do not print index",
440 " --with-zeros: print zero only entries",
441 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period: set scheduling parameters",
442 " o:prio - use SCHED_OTHER with prio",
443 " r:prio - use SCHED_RR with prio",
444 " f:prio - use SCHED_FIFO with prio",
445 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
446 " in nanoseconds",
447 " --warm-up: let the workload run for s seconds before collecting data",
448 " --trace-buffer-size kB: set the per-cpu trace buffer size in kB",
449 " --on-threshold <action>: define action to be executed at stop-total threshold, multiple are allowed",
450 " --on-end <action>: define action to be executed at measurement end, multiple are allowed",
451 NULL,
452 };
453
454 common_usage("osnoise", "hist", "a per-cpu histogram of the OS noise",
455 msg_start, msg_opts);
456 }
457
458 /*
459 * osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters
460 */
461 static struct common_params
osnoise_hist_parse_args(int argc,char * argv[])462 *osnoise_hist_parse_args(int argc, char *argv[])
463 {
464 struct osnoise_params *params;
465 int retval;
466 int c;
467 char *trace_output = NULL;
468
469 params = calloc(1, sizeof(*params));
470 if (!params)
471 exit(1);
472
473 actions_init(¶ms->common.threshold_actions);
474 actions_init(¶ms->common.end_actions);
475
476 /* display data in microseconds */
477 params->common.output_divisor = 1000;
478 params->common.hist.bucket_size = 1;
479 params->common.hist.entries = 256;
480
481 while (1) {
482 static struct option long_options[] = {
483 {"auto", required_argument, 0, 'a'},
484 {"bucket-size", required_argument, 0, 'b'},
485 {"entries", required_argument, 0, 'E'},
486 {"help", no_argument, 0, 'h'},
487 {"period", required_argument, 0, 'p'},
488 {"runtime", required_argument, 0, 'r'},
489 {"stop", required_argument, 0, 's'},
490 {"stop-total", required_argument, 0, 'S'},
491 {"trace", optional_argument, 0, 't'},
492 {"threshold", required_argument, 0, 'T'},
493 {"no-header", no_argument, 0, '0'},
494 {"no-summary", no_argument, 0, '1'},
495 {"no-index", no_argument, 0, '2'},
496 {"with-zeros", no_argument, 0, '3'},
497 {"trigger", required_argument, 0, '4'},
498 {"filter", required_argument, 0, '5'},
499 {"warm-up", required_argument, 0, '6'},
500 {"trace-buffer-size", required_argument, 0, '7'},
501 {"on-threshold", required_argument, 0, '8'},
502 {"on-end", required_argument, 0, '9'},
503 {0, 0, 0, 0}
504 };
505
506 if (common_parse_options(argc, argv, ¶ms->common))
507 continue;
508
509 c = getopt_long(argc, argv, "a:b:E:hp:r:s:S:t::T:01234:5:6:7:",
510 long_options, NULL);
511
512 /* detect the end of the options. */
513 if (c == -1)
514 break;
515
516 switch (c) {
517 case 'a':
518 /* set sample stop to auto_thresh */
519 params->common.stop_us = get_llong_from_str(optarg);
520
521 /* set sample threshold to 1 */
522 params->threshold = 1;
523
524 /* set trace */
525 if (!trace_output)
526 trace_output = "osnoise_trace.txt";
527
528 break;
529 case 'b':
530 params->common.hist.bucket_size = get_llong_from_str(optarg);
531 if (params->common.hist.bucket_size == 0 ||
532 params->common.hist.bucket_size >= 1000000)
533 fatal("Bucket size needs to be > 0 and <= 1000000");
534 break;
535 case 'E':
536 params->common.hist.entries = get_llong_from_str(optarg);
537 if (params->common.hist.entries < 10 ||
538 params->common.hist.entries > 9999999)
539 fatal("Entries must be > 10 and < 9999999");
540 break;
541 case 'h':
542 case '?':
543 osnoise_hist_usage();
544 break;
545 case 'p':
546 params->period = get_llong_from_str(optarg);
547 if (params->period > 10000000)
548 fatal("Period longer than 10 s");
549 break;
550 case 'r':
551 params->runtime = get_llong_from_str(optarg);
552 if (params->runtime < 100)
553 fatal("Runtime shorter than 100 us");
554 break;
555 case 's':
556 params->common.stop_us = get_llong_from_str(optarg);
557 break;
558 case 'S':
559 params->common.stop_total_us = get_llong_from_str(optarg);
560 break;
561 case 'T':
562 params->threshold = get_llong_from_str(optarg);
563 break;
564 case 't':
565 trace_output = parse_optional_arg(argc, argv);
566 if (!trace_output)
567 trace_output = "osnoise_trace.txt";
568 break;
569 case '0': /* no header */
570 params->common.hist.no_header = 1;
571 break;
572 case '1': /* no summary */
573 params->common.hist.no_summary = 1;
574 break;
575 case '2': /* no index */
576 params->common.hist.no_index = 1;
577 break;
578 case '3': /* with zeros */
579 params->common.hist.with_zeros = 1;
580 break;
581 case '4': /* trigger */
582 if (params->common.events) {
583 retval = trace_event_add_trigger(params->common.events, optarg);
584 if (retval)
585 fatal("Error adding trigger %s", optarg);
586 } else {
587 fatal("--trigger requires a previous -e");
588 }
589 break;
590 case '5': /* filter */
591 if (params->common.events) {
592 retval = trace_event_add_filter(params->common.events, optarg);
593 if (retval)
594 fatal("Error adding filter %s", optarg);
595 } else {
596 fatal("--filter requires a previous -e");
597 }
598 break;
599 case '6':
600 params->common.warmup = get_llong_from_str(optarg);
601 break;
602 case '7':
603 params->common.buffer_size = get_llong_from_str(optarg);
604 break;
605 case '8':
606 retval = actions_parse(¶ms->common.threshold_actions, optarg,
607 "osnoise_trace.txt");
608 if (retval)
609 fatal("Invalid action %s", optarg);
610 break;
611 case '9':
612 retval = actions_parse(¶ms->common.end_actions, optarg,
613 "osnoise_trace.txt");
614 if (retval)
615 fatal("Invalid action %s", optarg);
616 break;
617 default:
618 fatal("Invalid option");
619 }
620 }
621
622 if (trace_output)
623 actions_add_trace_output(¶ms->common.threshold_actions, trace_output);
624
625 if (geteuid())
626 fatal("rtla needs root permission");
627
628 if (params->common.hist.no_index && !params->common.hist.with_zeros)
629 fatal("no-index set and with-zeros not set - it does not make sense");
630
631 return ¶ms->common;
632 }
633
634 /*
635 * osnoise_hist_apply_config - apply the hist configs to the initialized tool
636 */
637 static int
osnoise_hist_apply_config(struct osnoise_tool * tool)638 osnoise_hist_apply_config(struct osnoise_tool *tool)
639 {
640 return osnoise_apply_config(tool, to_osnoise_params(tool->params));
641 }
642
643 /*
644 * osnoise_init_hist - initialize a osnoise hist tool with parameters
645 */
646 static struct osnoise_tool
osnoise_init_hist(struct common_params * params)647 *osnoise_init_hist(struct common_params *params)
648 {
649 struct osnoise_tool *tool;
650 int nr_cpus;
651
652 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
653
654 tool = osnoise_init_tool("osnoise_hist");
655 if (!tool)
656 return NULL;
657
658 tool->data = osnoise_alloc_histogram(nr_cpus, params->hist.entries,
659 params->hist.bucket_size);
660 if (!tool->data)
661 goto out_err;
662
663 return tool;
664
665 out_err:
666 osnoise_destroy_tool(tool);
667 return NULL;
668 }
669
osnoise_hist_enable(struct osnoise_tool * tool)670 static int osnoise_hist_enable(struct osnoise_tool *tool)
671 {
672 int retval;
673
674 retval = osnoise_init_trace_hist(tool);
675 if (retval)
676 return retval;
677
678 return osnoise_enable(tool);
679 }
680
osnoise_hist_main_loop(struct osnoise_tool * tool)681 static int osnoise_hist_main_loop(struct osnoise_tool *tool)
682 {
683 int retval;
684
685 retval = hist_main_loop(tool);
686 osnoise_read_trace_hist(tool);
687
688 return retval;
689 }
690
691 struct tool_ops osnoise_hist_ops = {
692 .tracer = "osnoise",
693 .comm_prefix = "osnoise/",
694 .parse_args = osnoise_hist_parse_args,
695 .init_tool = osnoise_init_hist,
696 .apply_config = osnoise_hist_apply_config,
697 .enable = osnoise_hist_enable,
698 .main = osnoise_hist_main_loop,
699 .print_stats = osnoise_print_stats,
700 .free = osnoise_free_hist_tool,
701 };
702