1 // SPDX-License-Identifier: GPL-2.0
18 struct osnoise_hist_params {
22 unsigned long long runtime;
23 unsigned long long period;
26 long long stop_total_us;
31 struct sched_attr sched_param;
32 struct trace_events *events;
42 struct osnoise_hist_cpu {
46 unsigned long long min_sample;
47 unsigned long long sum_sample;
48 unsigned long long max_sample;
52 struct osnoise_hist_data {
53 struct tracefs_hist *trace_hist;
54 struct osnoise_hist_cpu *hist;
61 * osnoise_free_histogram - free runtime data
64 osnoise_free_histogram(struct osnoise_hist_data *data)
68 /* one histogram for IRQ and one for thread, per CPU */
69 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
70 if (data->hist[cpu].samples)
71 free(data->hist[cpu].samples);
74 /* one set of histograms per CPU */
82 * osnoise_alloc_histogram - alloc runtime data
84 static struct osnoise_hist_data
85 *osnoise_alloc_histogram(int nr_cpus, int entries, int bucket_size)
87 struct osnoise_hist_data *data;
90 data = calloc(1, sizeof(*data));
94 data->entries = entries;
95 data->bucket_size = bucket_size;
96 data->nr_cpus = nr_cpus;
98 data->hist = calloc(1, sizeof(*data->hist) * nr_cpus);
102 for (cpu = 0; cpu < nr_cpus; cpu++) {
103 data->hist[cpu].samples = calloc(1, sizeof(*data->hist->samples) * (entries + 1));
104 if (!data->hist[cpu].samples)
108 /* set the min to max */
109 for (cpu = 0; cpu < nr_cpus; cpu++)
110 data->hist[cpu].min_sample = ~0;
115 osnoise_free_histogram(data);
119 static void osnoise_hist_update_multiple(struct osnoise_tool *tool, int cpu,
120 unsigned long long duration, int count)
122 struct osnoise_hist_params *params = tool->params;
123 struct osnoise_hist_data *data = tool->data;
124 int entries = data->entries;
128 if (params->output_divisor)
129 duration = duration / params->output_divisor;
131 if (data->bucket_size)
132 bucket = duration / data->bucket_size;
134 hist = data->hist[cpu].samples;
135 data->hist[cpu].count += count;
136 update_min(&data->hist[cpu].min_sample, &duration);
137 update_sum(&data->hist[cpu].sum_sample, &duration);
138 update_max(&data->hist[cpu].max_sample, &duration);
140 if (bucket < entries)
141 hist[bucket] += count;
143 hist[entries] += count;
147 * osnoise_destroy_trace_hist - disable events used to collect histogram
149 static void osnoise_destroy_trace_hist(struct osnoise_tool *tool)
151 struct osnoise_hist_data *data = tool->data;
153 tracefs_hist_pause(tool->trace.inst, data->trace_hist);
154 tracefs_hist_destroy(tool->trace.inst, data->trace_hist);
158 * osnoise_init_trace_hist - enable events used to collect histogram
160 static int osnoise_init_trace_hist(struct osnoise_tool *tool)
162 struct osnoise_hist_params *params = tool->params;
163 struct osnoise_hist_data *data = tool->data;
169 * Set the size of the bucket.
171 bucket_size = params->output_divisor * params->bucket_size;
172 snprintf(buff, sizeof(buff), "duration.buckets=%d", bucket_size);
174 data->trace_hist = tracefs_hist_alloc(tool->trace.tep, "osnoise", "sample_threshold",
175 buff, TRACEFS_HIST_KEY_NORMAL);
176 if (!data->trace_hist)
179 retval = tracefs_hist_add_key(data->trace_hist, "cpu", 0);
183 retval = tracefs_hist_start(tool->trace.inst, data->trace_hist);
190 osnoise_destroy_trace_hist(tool);
195 * osnoise_read_trace_hist - parse histogram file and file osnoise histogram
197 static void osnoise_read_trace_hist(struct osnoise_tool *tool)
199 struct osnoise_hist_data *data = tool->data;
200 long long cpu, counter, duration;
201 char *content, *position;
203 tracefs_hist_pause(tool->trace.inst, data->trace_hist);
205 content = tracefs_event_file_read(tool->trace.inst, "osnoise",
213 position = strstr(position, "duration: ~");
216 position += strlen("duration: ~");
217 duration = get_llong_from_str(position);
219 err_msg("error reading duration from histogram\n");
221 position = strstr(position, "cpu:");
224 position += strlen("cpu: ");
225 cpu = get_llong_from_str(position);
227 err_msg("error reading cpu from histogram\n");
229 position = strstr(position, "hitcount:");
232 position += strlen("hitcount: ");
233 counter = get_llong_from_str(position);
235 err_msg("error reading counter from histogram\n");
237 osnoise_hist_update_multiple(tool, cpu, duration, counter);
243 * osnoise_hist_header - print the header of the tracer to the output
245 static void osnoise_hist_header(struct osnoise_tool *tool)
247 struct osnoise_hist_params *params = tool->params;
248 struct osnoise_hist_data *data = tool->data;
249 struct trace_seq *s = tool->trace.seq;
253 if (params->no_header)
256 get_duration(tool->start_time, duration, sizeof(duration));
257 trace_seq_printf(s, "# RTLA osnoise histogram\n");
258 trace_seq_printf(s, "# Time unit is %s (%s)\n",
259 params->output_divisor == 1 ? "nanoseconds" : "microseconds",
260 params->output_divisor == 1 ? "ns" : "us");
262 trace_seq_printf(s, "# Duration: %s\n", duration);
264 if (!params->no_index)
265 trace_seq_printf(s, "Index");
267 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
268 if (params->cpus && !params->monitored_cpus[cpu])
271 if (!data->hist[cpu].count)
274 trace_seq_printf(s, " CPU-%03d", cpu);
276 trace_seq_printf(s, "\n");
278 trace_seq_do_printf(s);
283 * osnoise_print_summary - print the summary of the hist data to the output
286 osnoise_print_summary(struct osnoise_hist_params *params,
287 struct trace_instance *trace,
288 struct osnoise_hist_data *data)
292 if (params->no_summary)
295 if (!params->no_index)
296 trace_seq_printf(trace->seq, "count:");
298 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
299 if (params->cpus && !params->monitored_cpus[cpu])
302 if (!data->hist[cpu].count)
305 trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].count);
307 trace_seq_printf(trace->seq, "\n");
309 if (!params->no_index)
310 trace_seq_printf(trace->seq, "min: ");
312 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
313 if (params->cpus && !params->monitored_cpus[cpu])
316 if (!data->hist[cpu].count)
319 trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].min_sample);
322 trace_seq_printf(trace->seq, "\n");
324 if (!params->no_index)
325 trace_seq_printf(trace->seq, "avg: ");
327 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
328 if (params->cpus && !params->monitored_cpus[cpu])
331 if (!data->hist[cpu].count)
334 if (data->hist[cpu].count)
335 trace_seq_printf(trace->seq, "%9llu ",
336 data->hist[cpu].sum_sample / data->hist[cpu].count);
338 trace_seq_printf(trace->seq, " - ");
340 trace_seq_printf(trace->seq, "\n");
342 if (!params->no_index)
343 trace_seq_printf(trace->seq, "max: ");
345 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
346 if (params->cpus && !params->monitored_cpus[cpu])
349 if (!data->hist[cpu].count)
352 trace_seq_printf(trace->seq, "%9llu ", data->hist[cpu].max_sample);
355 trace_seq_printf(trace->seq, "\n");
356 trace_seq_do_printf(trace->seq);
357 trace_seq_reset(trace->seq);
361 * osnoise_print_stats - print data for all CPUs
364 osnoise_print_stats(struct osnoise_hist_params *params, struct osnoise_tool *tool)
366 struct osnoise_hist_data *data = tool->data;
367 struct trace_instance *trace = &tool->trace;
371 osnoise_hist_header(tool);
373 for (bucket = 0; bucket < data->entries; bucket++) {
376 if (!params->no_index)
377 trace_seq_printf(trace->seq, "%-6d",
378 bucket * data->bucket_size);
380 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
381 if (params->cpus && !params->monitored_cpus[cpu])
384 if (!data->hist[cpu].count)
387 total += data->hist[cpu].samples[bucket];
388 trace_seq_printf(trace->seq, "%9d ", data->hist[cpu].samples[bucket]);
391 if (total == 0 && !params->with_zeros) {
392 trace_seq_reset(trace->seq);
396 trace_seq_printf(trace->seq, "\n");
397 trace_seq_do_printf(trace->seq);
398 trace_seq_reset(trace->seq);
401 if (!params->no_index)
402 trace_seq_printf(trace->seq, "over: ");
404 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
405 if (params->cpus && !params->monitored_cpus[cpu])
408 if (!data->hist[cpu].count)
411 trace_seq_printf(trace->seq, "%9d ",
412 data->hist[cpu].samples[data->entries]);
414 trace_seq_printf(trace->seq, "\n");
415 trace_seq_do_printf(trace->seq);
416 trace_seq_reset(trace->seq);
418 osnoise_print_summary(params, trace, data);
422 * osnoise_hist_usage - prints osnoise hist usage message
424 static void osnoise_hist_usage(char *usage)
428 static const char * const msg[] = {
430 " usage: rtla osnoise hist [-h] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",
431 " [-T us] [-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\",
432 " [-c cpu-list] [-P priority] [-b N] [-E N] [--no-header] [--no-summary] [--no-index] \\",
435 " -h/--help: print this menu",
436 " -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit",
437 " -p/--period us: osnoise period in us",
438 " -r/--runtime us: osnoise runtime in us",
439 " -s/--stop us: stop trace if a single sample is higher than the argument in us",
440 " -S/--stop-total us: stop trace if the total sample is higher than the argument in us",
441 " -T/--threshold us: the minimum delta to be considered a noise",
442 " -c/--cpus cpu-list: list of cpus to run osnoise threads",
443 " -d/--duration time[s|m|h|d]: duration of the session",
444 " -D/--debug: print debug info",
445 " -t/--trace[=file]: save the stopped trace to [file|osnoise_trace.txt]",
446 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
447 " --filter <filter>: enable a trace event filter to the previous -e event",
448 " --trigger <trigger>: enable a trace event trigger to the previous -e event",
449 " -b/--bucket-size N: set the histogram bucket size (default 1)",
450 " -E/--entries N: set the number of entries of the histogram (default 256)",
451 " --no-header: do not print header",
452 " --no-summary: do not print summary",
453 " --no-index: do not print index",
454 " --with-zeros: print zero only entries",
455 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period: set scheduling parameters",
456 " o:prio - use SCHED_OTHER with prio",
457 " r:prio - use SCHED_RR with prio",
458 " f:prio - use SCHED_FIFO with prio",
459 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
465 fprintf(stderr, "%s\n", usage);
467 fprintf(stderr, "rtla osnoise hist: a per-cpu histogram of the OS noise (version %s)\n",
470 for (i = 0; msg[i]; i++)
471 fprintf(stderr, "%s\n", msg[i]);
476 * osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters
478 static struct osnoise_hist_params
479 *osnoise_hist_parse_args(int argc, char *argv[])
481 struct osnoise_hist_params *params;
482 struct trace_events *tevent;
486 params = calloc(1, sizeof(*params));
490 /* display data in microseconds */
491 params->output_divisor = 1000;
492 params->bucket_size = 1;
493 params->entries = 256;
496 static struct option long_options[] = {
497 {"auto", required_argument, 0, 'a'},
498 {"bucket-size", required_argument, 0, 'b'},
499 {"entries", required_argument, 0, 'E'},
500 {"cpus", required_argument, 0, 'c'},
501 {"debug", no_argument, 0, 'D'},
502 {"duration", required_argument, 0, 'd'},
503 {"help", no_argument, 0, 'h'},
504 {"period", required_argument, 0, 'p'},
505 {"priority", required_argument, 0, 'P'},
506 {"runtime", required_argument, 0, 'r'},
507 {"stop", required_argument, 0, 's'},
508 {"stop-total", required_argument, 0, 'S'},
509 {"trace", optional_argument, 0, 't'},
510 {"event", required_argument, 0, 'e'},
511 {"threshold", required_argument, 0, 'T'},
512 {"no-header", no_argument, 0, '0'},
513 {"no-summary", no_argument, 0, '1'},
514 {"no-index", no_argument, 0, '2'},
515 {"with-zeros", no_argument, 0, '3'},
516 {"trigger", required_argument, 0, '4'},
517 {"filter", required_argument, 0, '5'},
521 /* getopt_long stores the option index here. */
522 int option_index = 0;
524 c = getopt_long(argc, argv, "a:c:b:d:e:E:Dhp:P:r:s:S:t::T:01234:5:",
525 long_options, &option_index);
527 /* detect the end of the options. */
533 /* set sample stop to auto_thresh */
534 params->stop_us = get_llong_from_str(optarg);
536 /* set sample threshold to 1 */
537 params->threshold = 1;
540 params->trace_output = "osnoise_trace.txt";
544 params->bucket_size = get_llong_from_str(optarg);
545 if ((params->bucket_size == 0) || (params->bucket_size >= 1000000))
546 osnoise_hist_usage("Bucket size needs to be > 0 and <= 1000000\n");
549 retval = parse_cpu_list(optarg, ¶ms->monitored_cpus);
551 osnoise_hist_usage("\nInvalid -c cpu list\n");
552 params->cpus = optarg;
558 params->duration = parse_seconds_duration(optarg);
559 if (!params->duration)
560 osnoise_hist_usage("Invalid -D duration\n");
563 tevent = trace_event_alloc(optarg);
565 err_msg("Error alloc trace event");
570 tevent->next = params->events;
572 params->events = tevent;
575 params->entries = get_llong_from_str(optarg);
576 if ((params->entries < 10) || (params->entries > 9999999))
577 osnoise_hist_usage("Entries must be > 10 and < 9999999\n");
581 osnoise_hist_usage(NULL);
584 params->period = get_llong_from_str(optarg);
585 if (params->period > 10000000)
586 osnoise_hist_usage("Period longer than 10 s\n");
589 retval = parse_prio(optarg, ¶ms->sched_param);
591 osnoise_hist_usage("Invalid -P priority");
592 params->set_sched = 1;
595 params->runtime = get_llong_from_str(optarg);
596 if (params->runtime < 100)
597 osnoise_hist_usage("Runtime shorter than 100 us\n");
600 params->stop_us = get_llong_from_str(optarg);
603 params->stop_total_us = get_llong_from_str(optarg);
606 params->threshold = get_llong_from_str(optarg);
611 params->trace_output = &optarg[1];
613 params->trace_output = "osnoise_trace.txt";
615 case '0': /* no header */
616 params->no_header = 1;
618 case '1': /* no summary */
619 params->no_summary = 1;
621 case '2': /* no index */
622 params->no_index = 1;
624 case '3': /* with zeros */
625 params->with_zeros = 1;
627 case '4': /* trigger */
628 if (params->events) {
629 retval = trace_event_add_trigger(params->events, optarg);
631 err_msg("Error adding trigger %s\n", optarg);
635 osnoise_hist_usage("--trigger requires a previous -e\n");
638 case '5': /* filter */
639 if (params->events) {
640 retval = trace_event_add_filter(params->events, optarg);
642 err_msg("Error adding filter %s\n", optarg);
646 osnoise_hist_usage("--filter requires a previous -e\n");
650 osnoise_hist_usage("Invalid option");
655 err_msg("rtla needs root permission\n");
659 if (params->no_index && !params->with_zeros)
660 osnoise_hist_usage("no-index set and with-zeros not set - it does not make sense");
666 * osnoise_hist_apply_config - apply the hist configs to the initialized tool
669 osnoise_hist_apply_config(struct osnoise_tool *tool, struct osnoise_hist_params *params)
673 if (!params->sleep_time)
674 params->sleep_time = 1;
677 retval = osnoise_set_cpus(tool->context, params->cpus);
679 err_msg("Failed to apply CPUs config\n");
684 if (params->runtime || params->period) {
685 retval = osnoise_set_runtime_period(tool->context,
689 err_msg("Failed to set runtime and/or period\n");
694 if (params->stop_us) {
695 retval = osnoise_set_stop_us(tool->context, params->stop_us);
697 err_msg("Failed to set stop us\n");
702 if (params->stop_total_us) {
703 retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us);
705 err_msg("Failed to set stop total us\n");
710 if (params->threshold) {
711 retval = osnoise_set_tracing_thresh(tool->context, params->threshold);
713 err_msg("Failed to set tracing_thresh\n");
725 * osnoise_init_hist - initialize a osnoise hist tool with parameters
727 static struct osnoise_tool
728 *osnoise_init_hist(struct osnoise_hist_params *params)
730 struct osnoise_tool *tool;
733 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
735 tool = osnoise_init_tool("osnoise_hist");
739 tool->data = osnoise_alloc_histogram(nr_cpus, params->entries, params->bucket_size);
743 tool->params = params;
748 osnoise_destroy_tool(tool);
752 static int stop_tracing;
753 static void stop_hist(int sig)
759 * osnoise_hist_set_signals - handles the signal to stop the tool
762 osnoise_hist_set_signals(struct osnoise_hist_params *params)
764 signal(SIGINT, stop_hist);
765 if (params->duration) {
766 signal(SIGALRM, stop_hist);
767 alarm(params->duration);
771 int osnoise_hist_main(int argc, char *argv[])
773 struct osnoise_hist_params *params;
774 struct osnoise_tool *record = NULL;
775 struct osnoise_tool *tool = NULL;
776 struct trace_instance *trace;
777 int return_value = 1;
780 params = osnoise_hist_parse_args(argc, argv);
784 tool = osnoise_init_hist(params);
786 err_msg("Could not init osnoise hist\n");
790 retval = osnoise_hist_apply_config(tool, params);
792 err_msg("Could not apply config\n");
796 trace = &tool->trace;
798 retval = enable_osnoise(trace);
800 err_msg("Failed to enable osnoise tracer\n");
804 retval = osnoise_init_trace_hist(tool);
808 if (params->set_sched) {
809 retval = set_comm_sched_attr("osnoise/", ¶ms->sched_param);
811 err_msg("Failed to set sched parameters\n");
816 trace_instance_start(trace);
818 if (params->trace_output) {
819 record = osnoise_init_trace_tool("osnoise");
821 err_msg("Failed to enable the trace instance\n");
825 if (params->events) {
826 retval = trace_events_enable(&record->trace, params->events);
831 trace_instance_start(&record->trace);
834 tool->start_time = time(NULL);
835 osnoise_hist_set_signals(params);
837 while (!stop_tracing) {
838 sleep(params->sleep_time);
840 retval = tracefs_iterate_raw_events(trace->tep,
844 collect_registered_events,
847 err_msg("Error iterating on events\n");
851 if (trace_is_off(&tool->trace, &record->trace))
855 osnoise_read_trace_hist(tool);
857 osnoise_print_stats(params, tool);
861 if (trace_is_off(&tool->trace, &record->trace)) {
862 printf("rtla osnoise hit stop tracing\n");
863 if (params->trace_output) {
864 printf(" Saving trace to %s\n", params->trace_output);
865 save_trace_to_file(record->trace.inst, params->trace_output);
870 trace_events_destroy(&record->trace, params->events);
871 params->events = NULL;
873 osnoise_free_histogram(tool->data);
875 osnoise_destroy_tool(record);
876 osnoise_destroy_tool(tool);