1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
25 * osnoise top parameters
27 struct osnoise_top_params {
29 cpu_set_t monitored_cpus;
32 unsigned long long runtime;
33 unsigned long long period;
36 long long stop_total_us;
44 struct sched_attr sched_param;
45 struct trace_events *events;
46 enum osnoise_mode mode;
49 struct osnoise_top_cpu {
50 unsigned long long sum_runtime;
51 unsigned long long sum_noise;
52 unsigned long long max_noise;
53 unsigned long long max_sample;
55 unsigned long long hw_count;
56 unsigned long long nmi_count;
57 unsigned long long irq_count;
58 unsigned long long softirq_count;
59 unsigned long long thread_count;
64 struct osnoise_top_data {
65 struct osnoise_top_cpu *cpu_data;
70 * osnoise_free_top - free runtime data
73 osnoise_free_top(struct osnoise_top_data *data)
80 * osnoise_alloc_histogram - alloc runtime data
82 static struct osnoise_top_data *osnoise_alloc_top(int nr_cpus)
84 struct osnoise_top_data *data;
86 data = calloc(1, sizeof(*data));
90 data->nr_cpus = nr_cpus;
92 /* one set of histograms per CPU */
93 data->cpu_data = calloc(1, sizeof(*data->cpu_data) * nr_cpus);
100 osnoise_free_top(data);
105 * osnoise_top_handler - this is the handler for osnoise tracer events
108 osnoise_top_handler(struct trace_seq *s, struct tep_record *record,
109 struct tep_event *event, void *context)
111 struct trace_instance *trace = context;
112 struct osnoise_tool *tool;
113 unsigned long long val;
114 struct osnoise_top_cpu *cpu_data;
115 struct osnoise_top_data *data;
116 int cpu = record->cpu;
118 tool = container_of(trace, struct osnoise_tool, trace);
121 cpu_data = &data->cpu_data[cpu];
123 cpu_data->sum_cycles++;
125 tep_get_field_val(s, event, "runtime", record, &val, 1);
126 update_sum(&cpu_data->sum_runtime, &val);
128 tep_get_field_val(s, event, "noise", record, &val, 1);
129 update_max(&cpu_data->max_noise, &val);
130 update_sum(&cpu_data->sum_noise, &val);
132 tep_get_field_val(s, event, "max_sample", record, &val, 1);
133 update_max(&cpu_data->max_sample, &val);
135 tep_get_field_val(s, event, "hw_count", record, &val, 1);
136 update_sum(&cpu_data->hw_count, &val);
138 tep_get_field_val(s, event, "nmi_count", record, &val, 1);
139 update_sum(&cpu_data->nmi_count, &val);
141 tep_get_field_val(s, event, "irq_count", record, &val, 1);
142 update_sum(&cpu_data->irq_count, &val);
144 tep_get_field_val(s, event, "softirq_count", record, &val, 1);
145 update_sum(&cpu_data->softirq_count, &val);
147 tep_get_field_val(s, event, "thread_count", record, &val, 1);
148 update_sum(&cpu_data->thread_count, &val);
154 * osnoise_top_header - print the header of the tool output
156 static void osnoise_top_header(struct osnoise_tool *top)
158 struct osnoise_top_params *params = top->params;
159 struct trace_seq *s = top->trace.seq;
162 get_duration(top->start_time, duration, sizeof(duration));
164 trace_seq_printf(s, "\033[2;37;40m");
165 trace_seq_printf(s, " ");
167 if (params->mode == MODE_OSNOISE) {
168 trace_seq_printf(s, "Operating System Noise");
169 trace_seq_printf(s, " ");
170 } else if (params->mode == MODE_HWNOISE) {
171 trace_seq_printf(s, "Hardware-related Noise");
174 trace_seq_printf(s, " ");
175 trace_seq_printf(s, "\033[0;0;0m");
176 trace_seq_printf(s, "\n");
178 trace_seq_printf(s, "duration: %9s | time is in us\n", duration);
180 trace_seq_printf(s, "\033[2;30;47m");
181 trace_seq_printf(s, "CPU Period Runtime ");
182 trace_seq_printf(s, " Noise ");
183 trace_seq_printf(s, " %% CPU Aval ");
184 trace_seq_printf(s, " Max Noise Max Single ");
185 trace_seq_printf(s, " HW NMI");
187 if (params->mode == MODE_HWNOISE)
190 trace_seq_printf(s, " IRQ Softirq Thread");
193 trace_seq_printf(s, "\033[0;0;0m");
194 trace_seq_printf(s, "\n");
198 * clear_terminal - clears the output terminal
200 static void clear_terminal(struct trace_seq *seq)
203 trace_seq_printf(seq, "\033c");
207 * osnoise_top_print - prints the output of a given CPU
209 static void osnoise_top_print(struct osnoise_tool *tool, int cpu)
211 struct osnoise_top_params *params = tool->params;
212 struct trace_seq *s = tool->trace.seq;
213 struct osnoise_top_cpu *cpu_data;
214 struct osnoise_top_data *data;
219 cpu_data = &data->cpu_data[cpu];
221 if (!cpu_data->sum_runtime)
224 percentage = ((cpu_data->sum_runtime - cpu_data->sum_noise) * 10000000)
225 / cpu_data->sum_runtime;
226 decimal = percentage % 100000;
227 percentage = percentage / 100000;
229 trace_seq_printf(s, "%3d #%-6d %12llu ", cpu, cpu_data->sum_cycles, cpu_data->sum_runtime);
230 trace_seq_printf(s, "%12llu ", cpu_data->sum_noise);
231 trace_seq_printf(s, " %3d.%05d", percentage, decimal);
232 trace_seq_printf(s, "%12llu %12llu", cpu_data->max_noise, cpu_data->max_sample);
234 trace_seq_printf(s, "%12llu ", cpu_data->hw_count);
235 trace_seq_printf(s, "%12llu ", cpu_data->nmi_count);
237 if (params->mode == MODE_HWNOISE) {
238 trace_seq_printf(s, "\n");
242 trace_seq_printf(s, "%12llu ", cpu_data->irq_count);
243 trace_seq_printf(s, "%12llu ", cpu_data->softirq_count);
244 trace_seq_printf(s, "%12llu\n", cpu_data->thread_count);
248 * osnoise_print_stats - print data for all cpus
251 osnoise_print_stats(struct osnoise_top_params *params, struct osnoise_tool *top)
253 struct trace_instance *trace = &top->trace;
254 static int nr_cpus = -1;
258 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
261 clear_terminal(trace->seq);
263 osnoise_top_header(top);
265 for (i = 0; i < nr_cpus; i++) {
266 if (params->cpus && !CPU_ISSET(i, ¶ms->monitored_cpus))
268 osnoise_top_print(top, i);
271 trace_seq_do_printf(trace->seq);
272 trace_seq_reset(trace->seq);
276 * osnoise_top_usage - prints osnoise top usage message
278 static void osnoise_top_usage(struct osnoise_top_params *params, char *usage)
282 static const char * const msg[] = {
283 " [-h] [-q] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",
284 " [-T us] [-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\",
285 " [-c cpu-list] [-H cpu-list] [-P priority] [-C[=cgroup_name]]",
287 " -h/--help: print this menu",
288 " -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit",
289 " -p/--period us: osnoise period in us",
290 " -r/--runtime us: osnoise runtime in us",
291 " -s/--stop us: stop trace if a single sample is higher than the argument in us",
292 " -S/--stop-total us: stop trace if the total sample is higher than the argument in us",
293 " -T/--threshold us: the minimum delta to be considered a noise",
294 " -c/--cpus cpu-list: list of cpus to run osnoise threads",
295 " -H/--house-keeping cpus: run rtla control threads only on the given cpus",
296 " -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",
297 " -d/--duration time[s|m|h|d]: duration of the session",
298 " -D/--debug: print debug info",
299 " -t/--trace[=file]: save the stopped trace to [file|osnoise_trace.txt]",
300 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
301 " --filter <filter>: enable a trace event filter to the previous -e event",
302 " --trigger <trigger>: enable a trace event trigger to the previous -e event",
303 " -q/--quiet print only a summary at the end",
304 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
305 " o:prio - use SCHED_OTHER with prio",
306 " r:prio - use SCHED_RR with prio",
307 " f:prio - use SCHED_FIFO with prio",
308 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
314 fprintf(stderr, "%s\n", usage);
316 if (params->mode == MODE_OSNOISE) {
318 "rtla osnoise top: a per-cpu summary of the OS noise (version %s)\n",
321 fprintf(stderr, " usage: rtla osnoise [top]");
324 if (params->mode == MODE_HWNOISE) {
326 "rtla hwnoise: a summary of hardware-related noise (version %s)\n",
329 fprintf(stderr, " usage: rtla hwnoise");
332 for (i = 0; msg[i]; i++)
333 fprintf(stderr, "%s\n", msg[i]);
342 * osnoise_top_parse_args - allocs, parse and fill the cmd line parameters
344 struct osnoise_top_params *osnoise_top_parse_args(int argc, char **argv)
346 struct osnoise_top_params *params;
347 struct trace_events *tevent;
351 params = calloc(1, sizeof(*params));
355 if (strcmp(argv[0], "hwnoise") == 0) {
356 params->mode = MODE_HWNOISE;
358 * Reduce CPU usage for 75% to avoid killing the system.
360 params->runtime = 750000;
361 params->period = 1000000;
365 static struct option long_options[] = {
366 {"auto", required_argument, 0, 'a'},
367 {"cpus", required_argument, 0, 'c'},
368 {"cgroup", optional_argument, 0, 'C'},
369 {"debug", no_argument, 0, 'D'},
370 {"duration", required_argument, 0, 'd'},
371 {"event", required_argument, 0, 'e'},
372 {"house-keeping", required_argument, 0, 'H'},
373 {"help", no_argument, 0, 'h'},
374 {"period", required_argument, 0, 'p'},
375 {"priority", required_argument, 0, 'P'},
376 {"quiet", no_argument, 0, 'q'},
377 {"runtime", required_argument, 0, 'r'},
378 {"stop", required_argument, 0, 's'},
379 {"stop-total", required_argument, 0, 'S'},
380 {"threshold", required_argument, 0, 'T'},
381 {"trace", optional_argument, 0, 't'},
382 {"trigger", required_argument, 0, '0'},
383 {"filter", required_argument, 0, '1'},
387 /* getopt_long stores the option index here. */
388 int option_index = 0;
390 c = getopt_long(argc, argv, "a:c:C::d:De:hH:p:P:qr:s:S:t::T:0:1:",
391 long_options, &option_index);
393 /* Detect the end of the options. */
399 /* set sample stop to auto_thresh */
400 params->stop_us = get_llong_from_str(optarg);
402 /* set sample threshold to 1 */
403 params->threshold = 1;
406 params->trace_output = "osnoise_trace.txt";
410 retval = parse_cpu_set(optarg, ¶ms->monitored_cpus);
412 osnoise_top_usage(params, "\nInvalid -c cpu list\n");
413 params->cpus = optarg;
418 /* will inherit this cgroup */
419 params->cgroup_name = NULL;
420 } else if (*optarg == '=') {
422 params->cgroup_name = ++optarg;
429 params->duration = parse_seconds_duration(optarg);
430 if (!params->duration)
431 osnoise_top_usage(params, "Invalid -D duration\n");
434 tevent = trace_event_alloc(optarg);
436 err_msg("Error alloc trace event");
441 tevent->next = params->events;
442 params->events = tevent;
447 osnoise_top_usage(params, NULL);
451 retval = parse_cpu_set(optarg, ¶ms->hk_cpu_set);
453 err_msg("Error parsing house keeping CPUs\n");
458 params->period = get_llong_from_str(optarg);
459 if (params->period > 10000000)
460 osnoise_top_usage(params, "Period longer than 10 s\n");
463 retval = parse_prio(optarg, ¶ms->sched_param);
465 osnoise_top_usage(params, "Invalid -P priority");
466 params->set_sched = 1;
472 params->runtime = get_llong_from_str(optarg);
473 if (params->runtime < 100)
474 osnoise_top_usage(params, "Runtime shorter than 100 us\n");
477 params->stop_us = get_llong_from_str(optarg);
480 params->stop_total_us = get_llong_from_str(optarg);
485 params->trace_output = &optarg[1];
487 params->trace_output = "osnoise_trace.txt";
490 params->threshold = get_llong_from_str(optarg);
492 case '0': /* trigger */
493 if (params->events) {
494 retval = trace_event_add_trigger(params->events, optarg);
496 err_msg("Error adding trigger %s\n", optarg);
500 osnoise_top_usage(params, "--trigger requires a previous -e\n");
503 case '1': /* filter */
504 if (params->events) {
505 retval = trace_event_add_filter(params->events, optarg);
507 err_msg("Error adding filter %s\n", optarg);
511 osnoise_top_usage(params, "--filter requires a previous -e\n");
515 osnoise_top_usage(params, "Invalid option");
520 err_msg("osnoise needs root permission\n");
528 * osnoise_top_apply_config - apply the top configs to the initialized tool
531 osnoise_top_apply_config(struct osnoise_tool *tool, struct osnoise_top_params *params)
535 if (!params->sleep_time)
536 params->sleep_time = 1;
539 retval = osnoise_set_cpus(tool->context, params->cpus);
541 err_msg("Failed to apply CPUs config\n");
546 if (params->runtime || params->period) {
547 retval = osnoise_set_runtime_period(tool->context,
551 err_msg("Failed to set runtime and/or period\n");
556 if (params->stop_us) {
557 retval = osnoise_set_stop_us(tool->context, params->stop_us);
559 err_msg("Failed to set stop us\n");
564 if (params->stop_total_us) {
565 retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us);
567 err_msg("Failed to set stop total us\n");
572 if (params->threshold) {
573 retval = osnoise_set_tracing_thresh(tool->context, params->threshold);
575 err_msg("Failed to set tracing_thresh\n");
580 if (params->mode == MODE_HWNOISE) {
581 retval = osnoise_set_irq_disable(tool->context, 1);
583 err_msg("Failed to set OSNOISE_IRQ_DISABLE option\n");
588 if (params->hk_cpus) {
589 retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set),
590 ¶ms->hk_cpu_set);
592 err_msg("Failed to set rtla to the house keeping CPUs\n");
595 } else if (params->cpus) {
597 * Even if the user do not set a house-keeping CPU, try to
598 * move rtla to a CPU set different to the one where the user
599 * set the workload to run.
601 * No need to check results as this is an automatic attempt.
603 auto_house_keeping(¶ms->monitored_cpus);
613 * osnoise_init_top - initialize a osnoise top tool with parameters
615 struct osnoise_tool *osnoise_init_top(struct osnoise_top_params *params)
617 struct osnoise_tool *tool;
620 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
622 tool = osnoise_init_tool("osnoise_top");
626 tool->data = osnoise_alloc_top(nr_cpus);
630 tool->params = params;
632 tep_register_event_handler(tool->trace.tep, -1, "ftrace", "osnoise",
633 osnoise_top_handler, NULL);
638 osnoise_free_top(tool->data);
639 osnoise_destroy_tool(tool);
643 static int stop_tracing;
644 static void stop_top(int sig)
650 * osnoise_top_set_signals - handles the signal to stop the tool
652 static void osnoise_top_set_signals(struct osnoise_top_params *params)
654 signal(SIGINT, stop_top);
655 if (params->duration) {
656 signal(SIGALRM, stop_top);
657 alarm(params->duration);
661 int osnoise_top_main(int argc, char **argv)
663 struct osnoise_top_params *params;
664 struct osnoise_tool *record = NULL;
665 struct osnoise_tool *tool = NULL;
666 struct trace_instance *trace;
667 int return_value = 1;
670 params = osnoise_top_parse_args(argc, argv);
674 tool = osnoise_init_top(params);
676 err_msg("Could not init osnoise top\n");
680 retval = osnoise_top_apply_config(tool, params);
682 err_msg("Could not apply config\n");
686 trace = &tool->trace;
688 retval = enable_osnoise(trace);
690 err_msg("Failed to enable osnoise tracer\n");
694 if (params->set_sched) {
695 retval = set_comm_sched_attr("osnoise/", ¶ms->sched_param);
697 err_msg("Failed to set sched parameters\n");
702 if (params->cgroup) {
703 retval = set_comm_cgroup("osnoise/", params->cgroup_name);
705 err_msg("Failed to move threads to cgroup\n");
710 if (params->trace_output) {
711 record = osnoise_init_trace_tool("osnoise");
713 err_msg("Failed to enable the trace instance\n");
717 if (params->events) {
718 retval = trace_events_enable(&record->trace, params->events);
725 * Start the tracer here, after having set all instances.
727 * Let the trace instance start first for the case of hitting a stop
728 * tracing while enabling other instances. The trace instance is the
729 * one with most valuable information.
731 if (params->trace_output)
732 trace_instance_start(&record->trace);
733 trace_instance_start(trace);
735 tool->start_time = time(NULL);
736 osnoise_top_set_signals(params);
738 while (!stop_tracing) {
739 sleep(params->sleep_time);
741 retval = tracefs_iterate_raw_events(trace->tep,
745 collect_registered_events,
748 err_msg("Error iterating on events\n");
753 osnoise_print_stats(params, tool);
755 if (trace_is_off(&tool->trace, &record->trace))
760 osnoise_print_stats(params, tool);
764 if (trace_is_off(&tool->trace, &record->trace)) {
765 printf("osnoise hit stop tracing\n");
766 if (params->trace_output) {
767 printf(" Saving trace to %s\n", params->trace_output);
768 save_trace_to_file(record->trace.inst, params->trace_output);
773 trace_events_destroy(&record->trace, params->events);
774 params->events = NULL;
776 osnoise_free_top(tool->data);
777 osnoise_destroy_tool(record);
778 osnoise_destroy_tool(tool);