1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
18 struct osnoise_hist_params {
22 unsigned long long runtime;
23 unsigned long long period;
25 long long stop_total_us;
30 struct sched_attr sched_param;
40 struct osnoise_hist_cpu {
44 unsigned long long min_sample;
45 unsigned long long sum_sample;
46 unsigned long long max_sample;
50 struct osnoise_hist_data {
51 struct tracefs_hist *trace_hist;
52 struct osnoise_hist_cpu *hist;
59 * osnoise_free_histogram - free runtime data
62 osnoise_free_histogram(struct osnoise_hist_data *data)
66 /* one histogram for IRQ and one for thread, per CPU */
67 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
68 if (data->hist[cpu].samples)
69 free(data->hist[cpu].samples);
72 /* one set of histograms per CPU */
80 * osnoise_alloc_histogram - alloc runtime data
82 static struct osnoise_hist_data
83 *osnoise_alloc_histogram(int nr_cpus, int entries, int bucket_size)
85 struct osnoise_hist_data *data;
88 data = calloc(1, sizeof(*data));
92 data->entries = entries;
93 data->bucket_size = bucket_size;
94 data->nr_cpus = nr_cpus;
96 data->hist = calloc(1, sizeof(*data->hist) * nr_cpus);
100 for (cpu = 0; cpu < nr_cpus; cpu++) {
101 data->hist[cpu].samples = calloc(1, sizeof(*data->hist->samples) * (entries + 1));
102 if (!data->hist[cpu].samples)
106 /* set the min to max */
107 for (cpu = 0; cpu < nr_cpus; cpu++)
108 data->hist[cpu].min_sample = ~0;
113 osnoise_free_histogram(data);
117 static void osnoise_hist_update_multiple(struct osnoise_tool *tool, int cpu,
118 unsigned long long duration, int count)
120 struct osnoise_hist_params *params = tool->params;
121 struct osnoise_hist_data *data = tool->data;
122 int entries = data->entries;
126 if (params->output_divisor)
127 duration = duration / params->output_divisor;
129 if (data->bucket_size)
130 bucket = duration / data->bucket_size;
132 hist = data->hist[cpu].samples;
133 data->hist[cpu].count += count;
134 update_min(&data->hist[cpu].min_sample, &duration);
135 update_sum(&data->hist[cpu].sum_sample, &duration);
136 update_max(&data->hist[cpu].max_sample, &duration);
138 if (bucket < entries)
139 hist[bucket] += count;
141 hist[entries] += count;
145 * osnoise_destroy_trace_hist - disable events used to collect histogram
147 static void osnoise_destroy_trace_hist(struct osnoise_tool *tool)
149 struct osnoise_hist_data *data = tool->data;
151 tracefs_hist_pause(tool->trace.inst, data->trace_hist);
152 tracefs_hist_destroy(tool->trace.inst, data->trace_hist);
156 * osnoise_init_trace_hist - enable events used to collect histogram
158 static int osnoise_init_trace_hist(struct osnoise_tool *tool)
160 struct osnoise_hist_params *params = tool->params;
161 struct osnoise_hist_data *data = tool->data;
167 * Set the size of the bucket.
169 bucket_size = params->output_divisor * params->bucket_size;
170 snprintf(buff, sizeof(buff), "duration.buckets=%d", bucket_size);
172 data->trace_hist = tracefs_hist_alloc(tool->trace.tep, "osnoise", "sample_threshold",
173 buff, TRACEFS_HIST_KEY_NORMAL);
174 if (!data->trace_hist)
177 retval = tracefs_hist_add_key(data->trace_hist, "cpu", 0);
181 retval = tracefs_hist_start(tool->trace.inst, data->trace_hist);
188 osnoise_destroy_trace_hist(tool);
193 * osnoise_read_trace_hist - parse histogram file and file osnoise histogram
195 static void osnoise_read_trace_hist(struct osnoise_tool *tool)
197 struct osnoise_hist_data *data = tool->data;
198 long long cpu, counter, duration;
199 char *content, *position;
201 tracefs_hist_pause(tool->trace.inst, data->trace_hist);
203 content = tracefs_event_file_read(tool->trace.inst, "osnoise",
211 position = strstr(position, "duration: ~");
214 position += strlen("duration: ~");
215 duration = get_llong_from_str(position);
217 err_msg("error reading duration from histogram\n");
219 position = strstr(position, "cpu:");
222 position += strlen("cpu: ");
223 cpu = get_llong_from_str(position);
225 err_msg("error reading cpu from histogram\n");
227 position = strstr(position, "hitcount:");
230 position += strlen("hitcount: ");
231 counter = get_llong_from_str(position);
233 err_msg("error reading counter from histogram\n");
235 osnoise_hist_update_multiple(tool, cpu, duration, counter);
241 * osnoise_hist_header - print the header of the tracer to the output
243 static void osnoise_hist_header(struct osnoise_tool *tool)
245 struct osnoise_hist_params *params = tool->params;
246 struct osnoise_hist_data *data = tool->data;
247 struct trace_seq *s = tool->trace.seq;
251 if (params->no_header)
254 get_duration(tool->start_time, duration, sizeof(duration));
255 trace_seq_printf(s, "# RTLA osnoise histogram\n");
256 trace_seq_printf(s, "# Time unit is %s (%s)\n",
257 params->output_divisor == 1 ? "nanoseconds" : "microseconds",
258 params->output_divisor == 1 ? "ns" : "us");
260 trace_seq_printf(s, "# Duration: %s\n", duration);
262 if (!params->no_index)
263 trace_seq_printf(s, "Index");
265 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
266 if (params->cpus && !params->monitored_cpus[cpu])
269 if (!data->hist[cpu].count)
272 trace_seq_printf(s, " CPU-%03d", cpu);
274 trace_seq_printf(s, "\n");
276 trace_seq_do_printf(s);
281 * osnoise_print_summary - print the summary of the hist data to the output
284 osnoise_print_summary(struct osnoise_hist_params *params,
285 struct trace_instance *trace,
286 struct osnoise_hist_data *data)
290 if (params->no_summary)
293 if (!params->no_index)
294 trace_seq_printf(trace->seq, "count:");
296 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
297 if (params->cpus && !params->monitored_cpus[cpu])
300 if (!data->hist[cpu].count)
303 trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].count);
305 trace_seq_printf(trace->seq, "\n");
307 if (!params->no_index)
308 trace_seq_printf(trace->seq, "min: ");
310 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
311 if (params->cpus && !params->monitored_cpus[cpu])
314 if (!data->hist[cpu].count)
317 trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].min_sample);
320 trace_seq_printf(trace->seq, "\n");
322 if (!params->no_index)
323 trace_seq_printf(trace->seq, "avg: ");
325 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
326 if (params->cpus && !params->monitored_cpus[cpu])
329 if (!data->hist[cpu].count)
332 if (data->hist[cpu].count)
333 trace_seq_printf(trace->seq, "%9llu ",
334 data->hist[cpu].sum_sample / data->hist[cpu].count);
336 trace_seq_printf(trace->seq, " - ");
338 trace_seq_printf(trace->seq, "\n");
340 if (!params->no_index)
341 trace_seq_printf(trace->seq, "max: ");
343 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
344 if (params->cpus && !params->monitored_cpus[cpu])
347 if (!data->hist[cpu].count)
350 trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].max_sample);
353 trace_seq_printf(trace->seq, "\n");
354 trace_seq_do_printf(trace->seq);
355 trace_seq_reset(trace->seq);
359 * osnoise_print_stats - print data for all CPUs
362 osnoise_print_stats(struct osnoise_hist_params *params, struct osnoise_tool *tool)
364 struct osnoise_hist_data *data = tool->data;
365 struct trace_instance *trace = &tool->trace;
369 osnoise_hist_header(tool);
371 for (bucket = 0; bucket < data->entries; bucket++) {
374 if (!params->no_index)
375 trace_seq_printf(trace->seq, "%-6d",
376 bucket * data->bucket_size);
378 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
379 if (params->cpus && !params->monitored_cpus[cpu])
382 if (!data->hist[cpu].count)
385 total += data->hist[cpu].samples[bucket];
386 trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].samples[bucket]);
389 if (total == 0 && !params->with_zeros) {
390 trace_seq_reset(trace->seq);
394 trace_seq_printf(trace->seq, "\n");
395 trace_seq_do_printf(trace->seq);
396 trace_seq_reset(trace->seq);
399 if (!params->no_index)
400 trace_seq_printf(trace->seq, "over: ");
402 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
403 if (params->cpus && !params->monitored_cpus[cpu])
406 if (!data->hist[cpu].count)
409 trace_seq_printf(trace->seq, "%9d ",
410 data->hist[cpu].samples[data->entries]);
412 trace_seq_printf(trace->seq, "\n");
413 trace_seq_do_printf(trace->seq);
414 trace_seq_reset(trace->seq);
416 osnoise_print_summary(params, trace, data);
420 * osnoise_hist_usage - prints osnoise hist usage message
422 static void osnoise_hist_usage(char *usage)
426 static const char * const msg[] = {
428 " usage: rtla osnoise hist [-h] [-D] [-d s] [-p us] [-r us] [-s us] [-S us] [-t[=file]] \\",
429 " [-c cpu-list] [-P priority] [-b N] [-e N] [--no-header] [--no-summary] \\",
430 " [--no-index] [--with-zeros]",
432 " -h/--help: print this menu",
433 " -p/--period us: osnoise period in us",
434 " -r/--runtime us: osnoise runtime in us",
435 " -s/--stop us: stop trace if a single sample is higher than the argument in us",
436 " -S/--stop-total us: stop trace if the total sample is higher than the argument in us",
437 " -c/--cpus cpu-list: list of cpus to run osnoise threads",
438 " -d/--duration time[s|m|h|d]: duration of the session",
439 " -D/--debug: print debug info",
440 " -t/--trace[=file]: save the stopped trace to [file|osnoise_trace.txt]",
441 " -b/--bucket-size N: set the histogram bucket size (default 1)",
442 " -e/--entries N: set the number of entries of the histogram (default 256)",
443 " --no-header: do not print header",
444 " --no-summary: do not print summary",
445 " --no-index: do not print index",
446 " --with-zeros: print zero only entries",
447 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period: set scheduling parameters",
448 " o:prio - use SCHED_OTHER with prio",
449 " r:prio - use SCHED_RR with prio",
450 " f:prio - use SCHED_FIFO with prio",
451 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
457 fprintf(stderr, "%s\n", usage);
459 fprintf(stderr, "rtla osnoise hist: a per-cpu histogram of the OS noise (version %s)\n",
462 for (i = 0; msg[i]; i++)
463 fprintf(stderr, "%s\n", msg[i]);
468 * osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters
470 static struct osnoise_hist_params
471 *osnoise_hist_parse_args(int argc, char *argv[])
473 struct osnoise_hist_params *params;
477 params = calloc(1, sizeof(*params));
481 /* display data in microseconds */
482 params->output_divisor = 1000;
483 params->bucket_size = 1;
484 params->entries = 256;
487 static struct option long_options[] = {
488 {"bucket-size", required_argument, 0, 'b'},
489 {"entries", required_argument, 0, 'e'},
490 {"cpus", required_argument, 0, 'c'},
491 {"debug", no_argument, 0, 'D'},
492 {"duration", required_argument, 0, 'd'},
493 {"help", no_argument, 0, 'h'},
494 {"period", required_argument, 0, 'p'},
495 {"priority", required_argument, 0, 'P'},
496 {"runtime", required_argument, 0, 'r'},
497 {"stop", required_argument, 0, 's'},
498 {"stop-total", required_argument, 0, 'S'},
499 {"trace", optional_argument, 0, 't'},
500 {"no-header", no_argument, 0, '0'},
501 {"no-summary", no_argument, 0, '1'},
502 {"no-index", no_argument, 0, '2'},
503 {"with-zeros", no_argument, 0, '3'},
507 /* getopt_long stores the option index here. */
508 int option_index = 0;
510 c = getopt_long(argc, argv, "c:b:d:e:Dhp:P:r:s:S:t::0123",
511 long_options, &option_index);
513 /* detect the end of the options. */
519 params->bucket_size = get_llong_from_str(optarg);
520 if ((params->bucket_size == 0) || (params->bucket_size >= 1000000))
521 osnoise_hist_usage("Bucket size needs to be > 0 and <= 1000000\n");
524 retval = parse_cpu_list(optarg, ¶ms->monitored_cpus);
526 osnoise_hist_usage("\nInvalid -c cpu list\n");
527 params->cpus = optarg;
533 params->duration = parse_seconds_duration(optarg);
534 if (!params->duration)
535 osnoise_hist_usage("Invalid -D duration\n");
538 params->entries = get_llong_from_str(optarg);
539 if ((params->entries < 10) || (params->entries > 9999999))
540 osnoise_hist_usage("Entries must be > 10 and < 9999999\n");
544 osnoise_hist_usage(NULL);
547 params->period = get_llong_from_str(optarg);
548 if (params->period > 10000000)
549 osnoise_hist_usage("Period longer than 10 s\n");
552 retval = parse_prio(optarg, ¶ms->sched_param);
554 osnoise_hist_usage("Invalid -P priority");
555 params->set_sched = 1;
558 params->runtime = get_llong_from_str(optarg);
559 if (params->runtime < 100)
560 osnoise_hist_usage("Runtime shorter than 100 us\n");
563 params->stop_us = get_llong_from_str(optarg);
566 params->stop_total_us = get_llong_from_str(optarg);
571 params->trace_output = &optarg[1];
573 params->trace_output = "osnoise_trace.txt";
575 case '0': /* no header */
576 params->no_header = 1;
578 case '1': /* no summary */
579 params->no_summary = 1;
581 case '2': /* no index */
582 params->no_index = 1;
584 case '3': /* with zeros */
585 params->with_zeros = 1;
588 osnoise_hist_usage("Invalid option");
593 err_msg("rtla needs root permission\n");
597 if (params->no_index && !params->with_zeros)
598 osnoise_hist_usage("no-index set and with-zeros not set - it does not make sense");
604 * osnoise_hist_apply_config - apply the hist configs to the initialized tool
607 osnoise_hist_apply_config(struct osnoise_tool *tool, struct osnoise_hist_params *params)
611 if (!params->sleep_time)
612 params->sleep_time = 1;
615 retval = osnoise_set_cpus(tool->context, params->cpus);
617 err_msg("Failed to apply CPUs config\n");
622 if (params->runtime || params->period) {
623 retval = osnoise_set_runtime_period(tool->context,
627 err_msg("Failed to set runtime and/or period\n");
632 if (params->stop_us) {
633 retval = osnoise_set_stop_us(tool->context, params->stop_us);
635 err_msg("Failed to set stop us\n");
640 if (params->stop_total_us) {
641 retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us);
643 err_msg("Failed to set stop total us\n");
655 * osnoise_init_hist - initialize a osnoise hist tool with parameters
657 static struct osnoise_tool
658 *osnoise_init_hist(struct osnoise_hist_params *params)
660 struct osnoise_tool *tool;
663 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
665 tool = osnoise_init_tool("osnoise_hist");
669 tool->data = osnoise_alloc_histogram(nr_cpus, params->entries, params->bucket_size);
673 tool->params = params;
678 osnoise_destroy_tool(tool);
682 static int stop_tracing;
683 static void stop_hist(int sig)
689 * osnoise_hist_set_signals - handles the signal to stop the tool
692 osnoise_hist_set_signals(struct osnoise_hist_params *params)
694 signal(SIGINT, stop_hist);
695 if (params->duration) {
696 signal(SIGALRM, stop_hist);
697 alarm(params->duration);
701 int osnoise_hist_main(int argc, char *argv[])
703 struct osnoise_hist_params *params;
704 struct trace_instance *trace;
705 struct osnoise_tool *record;
706 struct osnoise_tool *tool;
707 int return_value = 1;
710 params = osnoise_hist_parse_args(argc, argv);
714 tool = osnoise_init_hist(params);
716 err_msg("Could not init osnoise hist\n");
720 retval = osnoise_hist_apply_config(tool, params);
722 err_msg("Could not apply config\n");
726 trace = &tool->trace;
728 retval = enable_osnoise(trace);
730 err_msg("Failed to enable osnoise tracer\n");
734 retval = osnoise_init_trace_hist(tool);
738 if (params->set_sched) {
739 retval = set_comm_sched_attr("osnoise/", ¶ms->sched_param);
741 err_msg("Failed to set sched parameters\n");
746 trace_instance_start(trace);
748 if (params->trace_output) {
749 record = osnoise_init_trace_tool("osnoise");
751 err_msg("Failed to enable the trace instance\n");
754 trace_instance_start(&record->trace);
757 tool->start_time = time(NULL);
758 osnoise_hist_set_signals(params);
760 while (!stop_tracing) {
761 sleep(params->sleep_time);
763 retval = tracefs_iterate_raw_events(trace->tep,
767 collect_registered_events,
770 err_msg("Error iterating on events\n");
774 if (!tracefs_trace_is_on(trace->inst))
778 osnoise_read_trace_hist(tool);
780 osnoise_print_stats(params, tool);
784 if (!tracefs_trace_is_on(trace->inst)) {
785 printf("rtla timelat hit stop tracing\n");
786 if (params->trace_output) {
787 printf(" Saving trace to %s\n", params->trace_output);
788 save_trace_to_file(record->trace.inst, params->trace_output);
793 osnoise_free_histogram(tool->data);
795 osnoise_destroy_tool(tool);
796 if (params->trace_output)
797 osnoise_destroy_tool(record);