Debian Codegraph @

tools/tracing/rtla/src/timerlat_top.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <[email protected]>
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 #include <sched.h>
15 #include <pthread.h>
16
17 #include "timerlat.h"
18 #include "timerlat_aa.h"
19 #include "timerlat_bpf.h"
20
21 struct timerlat_top_cpu {
22 unsigned long long irq_count;
23 unsigned long long thread_count;
24 unsigned long long user_count;
25
26 unsigned long long cur_irq;
27 unsigned long long min_irq;
28 unsigned long long sum_irq;
29 unsigned long long max_irq;
30
31 unsigned long long cur_thread;
32 unsigned long long min_thread;
33 unsigned long long sum_thread;
34 unsigned long long max_thread;
35
36 unsigned long long cur_user;
37 unsigned long long min_user;
38 unsigned long long sum_user;
39 unsigned long long max_user;
40 };
41
42 struct timerlat_top_data {
43 struct timerlat_top_cpu *cpu_data;
44 int nr_cpus;
45 };
46
47 /*
48 * timerlat_free_top - free runtime data
49 */
50 static void timerlat_free_top(struct timerlat_top_data *data)
51 {
52 free(data->cpu_data);
53 free(data);
54 }
55
56 static void timerlat_free_top_tool(struct osnoise_tool *tool)
57 {
58 timerlat_free_top(tool->data);
59 timerlat_free(tool);
60 }
61
62 /*
63 * timerlat_alloc_histogram - alloc runtime data
64 */
65 static struct timerlat_top_data *timerlat_alloc_top(int nr_cpus)
66 {
67 struct timerlat_top_data *data;
68 int cpu;
69
70 data = calloc(1, sizeof(*data));
71 if (!data)
72 return NULL;
73
74 data->nr_cpus = nr_cpus;
75
76 /* one set of histograms per CPU */
77 data->cpu_data = calloc(1, sizeof(*data->cpu_data) * nr_cpus);
78 if (!data->cpu_data)
79 goto cleanup;
80
81 /* set the min to max */
82 for (cpu = 0; cpu < nr_cpus; cpu++) {
83 data->cpu_data[cpu].min_irq = ~0;
84 data->cpu_data[cpu].min_thread = ~0;
85 data->cpu_data[cpu].min_user = ~0;
86 }
87
88 return data;
89
90 cleanup:
91 timerlat_free_top(data);
92 return NULL;
93 }
94
95 static void
96 timerlat_top_reset_sum(struct timerlat_top_cpu *summary)
97 {
98 memset(summary, 0, sizeof(*summary));
99 summary->min_irq = ~0;
100 summary->min_thread = ~0;
101 summary->min_user = ~0;
102 }
103
104 static void
105 timerlat_top_update_sum(struct osnoise_tool *tool, int cpu, struct timerlat_top_cpu *sum)
106 {
107 struct timerlat_top_data *data = tool->data;
108 struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
109
110 sum->irq_count += cpu_data->irq_count;
111 update_min(&sum->min_irq, &cpu_data->min_irq);
112 update_sum(&sum->sum_irq, &cpu_data->sum_irq);
113 update_max(&sum->max_irq, &cpu_data->max_irq);
114
115 sum->thread_count += cpu_data->thread_count;
116 update_min(&sum->min_thread, &cpu_data->min_thread);
117 update_sum(&sum->sum_thread, &cpu_data->sum_thread);
118 update_max(&sum->max_thread, &cpu_data->max_thread);
119
120 sum->user_count += cpu_data->user_count;
121 update_min(&sum->min_user, &cpu_data->min_user);
122 update_sum(&sum->sum_user, &cpu_data->sum_user);
123 update_max(&sum->max_user, &cpu_data->max_user);
124 }
125
126 /*
127 * timerlat_hist_update - record a new timerlat occurent on cpu, updating data
128 */
129 static void
130 timerlat_top_update(struct osnoise_tool *tool, int cpu,
131 unsigned long long thread,
132 unsigned long long latency)
133 {
134 struct timerlat_params *params = to_timerlat_params(tool->params);
135 struct timerlat_top_data *data = tool->data;
136 struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
137
138 if (params->common.output_divisor)
139 latency = latency / params->common.output_divisor;
140
141 if (!thread) {
142 cpu_data->irq_count++;
143 cpu_data->cur_irq = latency;
144 update_min(&cpu_data->min_irq, &latency);
145 update_sum(&cpu_data->sum_irq, &latency);
146 update_max(&cpu_data->max_irq, &latency);
147 } else if (thread == 1) {
148 cpu_data->thread_count++;
149 cpu_data->cur_thread = latency;
150 update_min(&cpu_data->min_thread, &latency);
151 update_sum(&cpu_data->sum_thread, &latency);
152 update_max(&cpu_data->max_thread, &latency);
153 } else {
154 cpu_data->user_count++;
155 cpu_data->cur_user = latency;
156 update_min(&cpu_data->min_user, &latency);
157 update_sum(&cpu_data->sum_user, &latency);
158 update_max(&cpu_data->max_user, &latency);
159 }
160 }
161
162 /*
163 * timerlat_top_handler - this is the handler for timerlat tracer events
164 */
165 static int
166 timerlat_top_handler(struct trace_seq *s, struct tep_record *record,
167 struct tep_event *event, void *context)
168 {
169 struct trace_instance *trace = context;
170 unsigned long long latency, thread;
171 struct osnoise_tool *top;
172 int cpu = record->cpu;
173
174 top = container_of(trace, struct osnoise_tool, trace);
175
176 if (!top->params->aa_only) {
177 tep_get_field_val(s, event, "context", record, &thread, 1);
178 tep_get_field_val(s, event, "timer_latency", record, &latency, 1);
179
180 timerlat_top_update(top, cpu, thread, latency);
181 }
182
183 return 0;
184 }
185
186 /*
187 * timerlat_top_bpf_pull_data - copy data from BPF maps into userspace
188 */
189 static int timerlat_top_bpf_pull_data(struct osnoise_tool *tool)
190 {
191 struct timerlat_top_data *data = tool->data;
192 int i, err;
193 long long value_irq[data->nr_cpus],
194 value_thread[data->nr_cpus],
195 value_user[data->nr_cpus];
196
197 /* Pull summary */
198 err = timerlat_bpf_get_summary_value(SUMMARY_CURRENT,
199 value_irq, value_thread, value_user,
200 data->nr_cpus);
201 if (err)
202 return err;
203 for (i = 0; i < data->nr_cpus; i++) {
204 data->cpu_data[i].cur_irq = value_irq[i];
205 data->cpu_data[i].cur_thread = value_thread[i];
206 data->cpu_data[i].cur_user = value_user[i];
207 }
208
209 err = timerlat_bpf_get_summary_value(SUMMARY_COUNT,
210 value_irq, value_thread, value_user,
211 data->nr_cpus);
212 if (err)
213 return err;
214 for (i = 0; i < data->nr_cpus; i++) {
215 data->cpu_data[i].irq_count = value_irq[i];
216 data->cpu_data[i].thread_count = value_thread[i];
217 data->cpu_data[i].user_count = value_user[i];
218 }
219
220 err = timerlat_bpf_get_summary_value(SUMMARY_MIN,
221 value_irq, value_thread, value_user,
222 data->nr_cpus);
223 if (err)
224 return err;
225 for (i = 0; i < data->nr_cpus; i++) {
226 data->cpu_data[i].min_irq = value_irq[i];
227 data->cpu_data[i].min_thread = value_thread[i];
228 data->cpu_data[i].min_user = value_user[i];
229 }
230
231 err = timerlat_bpf_get_summary_value(SUMMARY_MAX,
232 value_irq, value_thread, value_user,
233 data->nr_cpus);
234 if (err)
235 return err;
236 for (i = 0; i < data->nr_cpus; i++) {
237 data->cpu_data[i].max_irq = value_irq[i];
238 data->cpu_data[i].max_thread = value_thread[i];
239 data->cpu_data[i].max_user = value_user[i];
240 }
241
242 err = timerlat_bpf_get_summary_value(SUMMARY_SUM,
243 value_irq, value_thread, value_user,
244 data->nr_cpus);
245 if (err)
246 return err;
247 for (i = 0; i < data->nr_cpus; i++) {
248 data->cpu_data[i].sum_irq = value_irq[i];
249 data->cpu_data[i].sum_thread = value_thread[i];
250 data->cpu_data[i].sum_user = value_user[i];
251 }
252
253 return 0;
254 }
255
256 /*
257 * timerlat_top_header - print the header of the tool output
258 */
259 static void timerlat_top_header(struct timerlat_params *params, struct osnoise_tool *top)
260 {
261 struct trace_seq *s = top->trace.seq;
262 bool pretty = params->common.pretty_output;
263 char duration[26];
264
265 get_duration(top->start_time, duration, sizeof(duration));
266
267 if (pretty)
268 trace_seq_printf(s, "\033[2;37;40m");
269
270 trace_seq_printf(s, " Timer Latency ");
271 if (params->common.user_data)
272 trace_seq_printf(s, " ");
273
274 if (pretty)
275 trace_seq_printf(s, "\033[0;0;0m");
276 trace_seq_printf(s, "\n");
277
278 trace_seq_printf(s, "%-6s | IRQ Timer Latency (%s) | Thread Timer Latency (%s)", duration,
279 params->common.output_divisor == 1 ? "ns" : "us",
280 params->common.output_divisor == 1 ? "ns" : "us");
281
282 if (params->common.user_data) {
283 trace_seq_printf(s, " | Ret user Timer Latency (%s)",
284 params->common.output_divisor == 1 ? "ns" : "us");
285 }
286
287 trace_seq_printf(s, "\n");
288 if (pretty)
289 trace_seq_printf(s, "\033[2;30;47m");
290
291 trace_seq_printf(s, "CPU COUNT | cur min avg max | cur min avg max");
292 if (params->common.user_data)
293 trace_seq_printf(s, " | cur min avg max");
294
295 if (pretty)
296 trace_seq_printf(s, "\033[0;0;0m");
297 trace_seq_printf(s, "\n");
298 }
299
300 static const char *no_value = " -";
301
302 /*
303 * timerlat_top_print - prints the output of a given CPU
304 */
305 static void timerlat_top_print(struct osnoise_tool *top, int cpu)
306 {
307 struct timerlat_params *params = to_timerlat_params(top->params);
308 struct timerlat_top_data *data = top->data;
309 struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
310 struct trace_seq *s = top->trace.seq;
311
312 /*
313 * Skip if no data is available: is this cpu offline?
314 */
315 if (!cpu_data->irq_count && !cpu_data->thread_count)
316 return;
317
318 /*
319 * Unless trace is being lost, IRQ counter is always the max.
320 */
321 trace_seq_printf(s, "%3d #%-9llu |", cpu, cpu_data->irq_count);
322
323 if (!cpu_data->irq_count) {
324 trace_seq_printf(s, "%s %s %s %s |", no_value, no_value, no_value, no_value);
325 } else {
326 trace_seq_printf(s, "%9llu ", cpu_data->cur_irq);
327 trace_seq_printf(s, "%9llu ", cpu_data->min_irq);
328 trace_seq_printf(s, "%9llu ", cpu_data->sum_irq / cpu_data->irq_count);
329 trace_seq_printf(s, "%9llu |", cpu_data->max_irq);
330 }
331
332 if (!cpu_data->thread_count) {
333 trace_seq_printf(s, "%s %s %s %s", no_value, no_value, no_value, no_value);
334 } else {
335 trace_seq_printf(s, "%9llu ", cpu_data->cur_thread);
336 trace_seq_printf(s, "%9llu ", cpu_data->min_thread);
337 trace_seq_printf(s, "%9llu ",
338 cpu_data->sum_thread / cpu_data->thread_count);
339 trace_seq_printf(s, "%9llu", cpu_data->max_thread);
340 }
341
342 if (!params->common.user_data) {
343 trace_seq_printf(s, "\n");
344 return;
345 }
346
347 trace_seq_printf(s, " |");
348
349 if (!cpu_data->user_count) {
350 trace_seq_printf(s, "%s %s %s %s\n", no_value, no_value, no_value, no_value);
351 } else {
352 trace_seq_printf(s, "%9llu ", cpu_data->cur_user);
353 trace_seq_printf(s, "%9llu ", cpu_data->min_user);
354 trace_seq_printf(s, "%9llu ",
355 cpu_data->sum_user / cpu_data->user_count);
356 trace_seq_printf(s, "%9llu\n", cpu_data->max_user);
357 }
358 }
359
360 /*
361 * timerlat_top_print_sum - prints the summary output
362 */
363 static void
364 timerlat_top_print_sum(struct osnoise_tool *top, struct timerlat_top_cpu *summary)
365 {
366 const char *split = "----------------------------------------";
367 struct timerlat_params *params = to_timerlat_params(top->params);
368 unsigned long long count = summary->irq_count;
369 struct trace_seq *s = top->trace.seq;
370 int e = 0;
371
372 /*
373 * Skip if no data is available: is this cpu offline?
374 */
375 if (!summary->irq_count && !summary->thread_count)
376 return;
377
378 while (count > 999999) {
379 e++;
380 count /= 10;
381 }
382
383 trace_seq_printf(s, "%.*s|%.*s|%.*s", 15, split, 40, split, 39, split);
384 if (params->common.user_data)
385 trace_seq_printf(s, "-|%.*s", 39, split);
386 trace_seq_printf(s, "\n");
387
388 trace_seq_printf(s, "ALL #%-6llu e%d |", count, e);
389
390 if (!summary->irq_count) {
391 trace_seq_printf(s, " %s %s %s |", no_value, no_value, no_value);
392 } else {
393 trace_seq_printf(s, " ");
394 trace_seq_printf(s, "%9llu ", summary->min_irq);
395 trace_seq_printf(s, "%9llu ", summary->sum_irq / summary->irq_count);
396 trace_seq_printf(s, "%9llu |", summary->max_irq);
397 }
398
399 if (!summary->thread_count) {
400 trace_seq_printf(s, "%s %s %s %s", no_value, no_value, no_value, no_value);
401 } else {
402 trace_seq_printf(s, " ");
403 trace_seq_printf(s, "%9llu ", summary->min_thread);
404 trace_seq_printf(s, "%9llu ",
405 summary->sum_thread / summary->thread_count);
406 trace_seq_printf(s, "%9llu", summary->max_thread);
407 }
408
409 if (!params->common.user_data) {
410 trace_seq_printf(s, "\n");
411 return;
412 }
413
414 trace_seq_printf(s, " |");
415
416 if (!summary->user_count) {
417 trace_seq_printf(s, " %s %s %s |", no_value, no_value, no_value);
418 } else {
419 trace_seq_printf(s, " ");
420 trace_seq_printf(s, "%9llu ", summary->min_user);
421 trace_seq_printf(s, "%9llu ",
422 summary->sum_user / summary->user_count);
423 trace_seq_printf(s, "%9llu\n", summary->max_user);
424 }
425 }
426
427 /*
428 * clear_terminal - clears the output terminal
429 */
430 static void clear_terminal(struct trace_seq *seq)
431 {
432 if (!config_debug)
433 trace_seq_printf(seq, "\033c");
434 }
435
436 /*
437 * timerlat_print_stats - print data for all cpus
438 */
439 static void
440 timerlat_print_stats(struct osnoise_tool *top)
441 {
442 struct timerlat_params *params = to_timerlat_params(top->params);
443 struct trace_instance *trace = &top->trace;
444 struct timerlat_top_cpu summary;
445 static int nr_cpus = -1;
446 int i;
447
448 if (params->common.aa_only)
449 return;
450
451 if (nr_cpus == -1)
452 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
453
454 if (!params->common.quiet)
455 clear_terminal(trace->seq);
456
457 timerlat_top_reset_sum(&summary);
458
459 timerlat_top_header(params, top);
460
461 for_each_monitored_cpu(i, nr_cpus, &params->common) {
462 timerlat_top_print(top, i);
463 timerlat_top_update_sum(top, i, &summary);
464 }
465
466 timerlat_top_print_sum(top, &summary);
467
468 trace_seq_do_printf(trace->seq);
469 trace_seq_reset(trace->seq);
470 osnoise_report_missed_events(top);
471 }
472
473 /*
474 * timerlat_top_usage - prints timerlat top usage message
475 */
476 static void timerlat_top_usage(void)
477 {
478 static const char *const msg_start[] = {
479 "[-q] [-a us] [-d s] [-D] [-n] [-p us] [-i us] [-T us] [-s us] \\",
480 " [[-t [file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\",
481 " [-P priority] [--dma-latency us] [--aa-only us] [-C [cgroup_name]] [-u|-k] [--warm-up s] [--deepest-idle-state n]",
482 NULL,
483 };
484
485 static const char *const msg_opts[] = {
486 " -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",
487 " --aa-only us: stop if <us> latency is hit, only printing the auto analysis (reduces CPU usage)",
488 " -p/--period us: timerlat period in us",
489 " -i/--irq us: stop trace if the irq latency is higher than the argument in us",
490 " -T/--thread us: stop trace if the thread latency is higher than the argument in us",
491 " -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",
492 " -c/--cpus cpus: run the tracer only on the given cpus",
493 " -H/--house-keeping cpus: run rtla control threads only on the given cpus",
494 " -C/--cgroup [cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",
495 " -d/--duration time[s|m|h|d]: duration of the session",
496 " -D/--debug: print debug info",
497 " --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)",
498 " -t/--trace [file]: save the stopped trace to [file|timerlat_trace.txt]",
499 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
500 " --filter <command>: enable a trace event filter to the previous -e event",
501 " --trigger <command>: enable a trace event trigger to the previous -e event",
502 " -n/--nano: display data in nanoseconds",
503 " --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage",
504 " -q/--quiet print only a summary at the end",
505 " --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency",
506 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
507 " o:prio - use SCHED_OTHER with prio",
508 " r:prio - use SCHED_RR with prio",
509 " f:prio - use SCHED_FIFO with prio",
510 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
511 " in nanoseconds",
512 " -u/--user-threads: use rtla user-space threads instead of kernel-space timerlat threads",
513 " -k/--kernel-threads: use timerlat kernel-space threads instead of rtla user-space threads",
514 " -U/--user-load: enable timerlat for user-defined user-space workload",
515 " --warm-up s: let the workload run for s seconds before collecting data",
516 " --trace-buffer-size kB: set the per-cpu trace buffer size in kB",
517 " --deepest-idle-state n: only go down to idle state n on cpus used by timerlat to reduce exit from idle latency",
518 " --on-threshold <action>: define action to be executed at latency threshold, multiple are allowed",
519 " --on-end: define action to be executed at measurement end, multiple are allowed",
520 " --bpf-action <program>: load and execute BPF program when latency threshold is exceeded",
521 NULL,
522 };
523
524 common_usage("timerlat", "top", "a per-cpu summary of the timer latency",
525 msg_start, msg_opts);
526 }
527
528 /*
529 * timerlat_top_parse_args - allocs, parse and fill the cmd line parameters
530 */
531 static struct common_params
532 *timerlat_top_parse_args(int argc, char **argv)
533 {
534 struct timerlat_params *params;
535 long long auto_thresh;
536 int retval;
537 int c;
538 char *trace_output = NULL;
539
540 params = calloc(1, sizeof(*params));
541 if (!params)
542 exit(1);
543
544 actions_init(&params->common.threshold_actions);
545 actions_init(&params->common.end_actions);
546
547 /* disabled by default */
548 params->dma_latency = -1;
549
550 /* disabled by default */
551 params->deepest_idle_state = -2;
552
553 /* display data in microseconds */
554 params->common.output_divisor = 1000;
555
556 /* default to BPF mode */
557 params->mode = TRACING_MODE_BPF;
558
559 while (1) {
560 static struct option long_options[] = {
561 {"auto", required_argument, 0, 'a'},
562 {"help", no_argument, 0, 'h'},
563 {"irq", required_argument, 0, 'i'},
564 {"nano", no_argument, 0, 'n'},
565 {"period", required_argument, 0, 'p'},
566 {"quiet", no_argument, 0, 'q'},
567 {"stack", required_argument, 0, 's'},
568 {"thread", required_argument, 0, 'T'},
569 {"trace", optional_argument, 0, 't'},
570 {"user-threads", no_argument, 0, 'u'},
571 {"kernel-threads", no_argument, 0, 'k'},
572 {"user-load", no_argument, 0, 'U'},
573 {"trigger", required_argument, 0, '0'},
574 {"filter", required_argument, 0, '1'},
575 {"dma-latency", required_argument, 0, '2'},
576 {"no-aa", no_argument, 0, '3'},
577 {"dump-tasks", no_argument, 0, '4'},
578 {"aa-only", required_argument, 0, '5'},
579 {"warm-up", required_argument, 0, '6'},
580 {"trace-buffer-size", required_argument, 0, '7'},
581 {"deepest-idle-state", required_argument, 0, '8'},
582 {"on-threshold", required_argument, 0, '9'},
583 {"on-end", required_argument, 0, '\1'},
584 {"bpf-action", required_argument, 0, '\2'},
585 {0, 0, 0, 0}
586 };
587
588 if (common_parse_options(argc, argv, &params->common))
589 continue;
590
591 c = getopt_auto(argc, argv, long_options);
592
593 /* detect the end of the options. */
594 if (c == -1)
595 break;
596
597 switch (c) {
598 case 'a':
599 auto_thresh = get_llong_from_str(optarg);
600
601 /* set thread stop to auto_thresh */
602 params->common.stop_total_us = auto_thresh;
603 params->common.stop_us = auto_thresh;
604
605 /* get stack trace */
606 params->print_stack = auto_thresh;
607
608 /* set trace */
609 if (!trace_output)
610 trace_output = "timerlat_trace.txt";
611
612 break;
613 case '5':
614 /* it is here because it is similar to -a */
615 auto_thresh = get_llong_from_str(optarg);
616
617 /* set thread stop to auto_thresh */
618 params->common.stop_total_us = auto_thresh;
619 params->common.stop_us = auto_thresh;
620
621 /* get stack trace */
622 params->print_stack = auto_thresh;
623
624 /* set aa_only to avoid parsing the trace */
625 params->common.aa_only = 1;
626 break;
627 case 'h':
628 case '?':
629 timerlat_top_usage();
630 break;
631 case 'i':
632 params->common.stop_us = get_llong_from_str(optarg);
633 break;
634 case 'k':
635 params->common.kernel_workload = true;
636 break;
637 case 'n':
638 params->common.output_divisor = 1;
639 break;
640 case 'p':
641 params->timerlat_period_us = get_llong_from_str(optarg);
642 if (params->timerlat_period_us > 1000000)
643 fatal("Period longer than 1 s");
644 break;
645 case 'q':
646 params->common.quiet = 1;
647 break;
648 case 's':
649 params->print_stack = get_llong_from_str(optarg);
650 break;
651 case 'T':
652 params->common.stop_total_us = get_llong_from_str(optarg);
653 break;
654 case 't':
655 trace_output = parse_optional_arg(argc, argv);
656 if (!trace_output)
657 trace_output = "timerlat_trace.txt";
658 break;
659 case 'u':
660 params->common.user_workload = true;
661 /* fallback: -u implies -U */
662 case 'U':
663 params->common.user_data = true;
664 break;
665 case '0': /* trigger */
666 if (params->common.events) {
667 retval = trace_event_add_trigger(params->common.events, optarg);
668 if (retval)
669 fatal("Error adding trigger %s", optarg);
670 } else {
671 fatal("--trigger requires a previous -e");
672 }
673 break;
674 case '1': /* filter */
675 if (params->common.events) {
676 retval = trace_event_add_filter(params->common.events, optarg);
677 if (retval)
678 fatal("Error adding filter %s", optarg);
679 } else {
680 fatal("--filter requires a previous -e");
681 }
682 break;
683 case '2': /* dma-latency */
684 params->dma_latency = get_llong_from_str(optarg);
685 if (params->dma_latency < 0 || params->dma_latency > 10000)
686 fatal("--dma-latency needs to be >= 0 and < 10000");
687 break;
688 case '3': /* no-aa */
689 params->no_aa = 1;
690 break;
691 case '4':
692 params->dump_tasks = 1;
693 break;
694 case '6':
695 params->common.warmup = get_llong_from_str(optarg);
696 break;
697 case '7':
698 params->common.buffer_size = get_llong_from_str(optarg);
699 break;
700 case '8':
701 params->deepest_idle_state = get_llong_from_str(optarg);
702 break;
703 case '9':
704 retval = actions_parse(&params->common.threshold_actions, optarg,
705 "timerlat_trace.txt");
706 if (retval)
707 fatal("Invalid action %s", optarg);
708 break;
709 case '\1':
710 retval = actions_parse(&params->common.end_actions, optarg,
711 "timerlat_trace.txt");
712 if (retval)
713 fatal("Invalid action %s", optarg);
714 break;
715 case '\2':
716 params->bpf_action_program = optarg;
717 break;
718 default:
719 fatal("Invalid option");
720 }
721 }
722
723 if (trace_output)
724 actions_add_trace_output(&params->common.threshold_actions, trace_output);
725
726 if (geteuid())
727 fatal("rtla needs root permission");
728
729 /*
730 * Auto analysis only happens if stop tracing, thus:
731 */
732 if (!params->common.stop_us && !params->common.stop_total_us)
733 params->no_aa = 1;
734
735 if (params->no_aa && params->common.aa_only)
736 fatal("--no-aa and --aa-only are mutually exclusive!");
737
738 if (params->common.kernel_workload && params->common.user_workload)
739 fatal("--kernel-threads and --user-threads are mutually exclusive!");
740
741 /*
742 * If auto-analysis or trace output is enabled, switch from BPF mode to
743 * mixed mode
744 */
745 if (params->mode == TRACING_MODE_BPF &&
746 (params->common.threshold_actions.present[ACTION_TRACE_OUTPUT] ||
747 params->common.end_actions.present[ACTION_TRACE_OUTPUT] ||
748 !params->no_aa))
749 params->mode = TRACING_MODE_MIXED;
750
751 return &params->common;
752 }
753
754 /*
755 * timerlat_top_apply_config - apply the top configs to the initialized tool
756 */
757 static int
758 timerlat_top_apply_config(struct osnoise_tool *top)
759 {
760 struct timerlat_params *params = to_timerlat_params(top->params);
761 int retval;
762
763 retval = timerlat_apply_config(top, params);
764 if (retval)
765 goto out_err;
766
767 if (isatty(STDOUT_FILENO) && !params->common.quiet)
768 params->common.pretty_output = 1;
769
770 return 0;
771
772 out_err:
773 return -1;
774 }
775
776 /*
777 * timerlat_init_top - initialize a timerlat top tool with parameters
778 */
779 static struct osnoise_tool
780 *timerlat_init_top(struct common_params *params)
781 {
782 struct osnoise_tool *top;
783 int nr_cpus;
784
785 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
786
787 top = osnoise_init_tool("timerlat_top");
788 if (!top)
789 return NULL;
790
791 top->data = timerlat_alloc_top(nr_cpus);
792 if (!top->data)
793 goto out_err;
794
795 tep_register_event_handler(top->trace.tep, -1, "ftrace", "timerlat",
796 timerlat_top_handler, top);
797
798 return top;
799
800 out_err:
801 osnoise_destroy_tool(top);
802 return NULL;
803 }
804
805 /*
806 * timerlat_top_bpf_main_loop - main loop to process events (BPF variant)
807 */
808 static int
809 timerlat_top_bpf_main_loop(struct osnoise_tool *tool)
810 {
811 struct timerlat_params *params = to_timerlat_params(tool->params);
812 int retval, wait_retval;
813
814 if (params->common.aa_only) {
815 /* Auto-analysis only, just wait for stop tracing */
816 timerlat_bpf_wait(-1);
817 return 0;
818 }
819
820 /* Pull and display data in a loop */
821 while (!stop_tracing) {
822 wait_retval = timerlat_bpf_wait(params->common.quiet ? -1 :
823 params->common.sleep_time);
824
825 retval = timerlat_top_bpf_pull_data(tool);
826 if (retval) {
827 err_msg("Error pulling BPF data\n");
828 return retval;
829 }
830
831 if (!params->common.quiet)
832 timerlat_print_stats(tool);
833
834 if (wait_retval != 0) {
835 /* Stopping requested by tracer */
836 actions_perform(&params->common.threshold_actions);
837
838 if (!params->common.threshold_actions.continue_flag)
839 /* continue flag not set, break */
840 break;
841
842 /* continue action reached, re-enable tracing */
843 if (tool->record)
844 trace_instance_start(&tool->record->trace);
845 if (tool->aa)
846 trace_instance_start(&tool->aa->trace);
847 timerlat_bpf_restart_tracing();
848 }
849
850 /* is there still any user-threads ? */
851 if (params->common.user_workload) {
852 if (params->common.user.stopped_running) {
853 debug_msg("timerlat user space threads stopped!\n");
854 break;
855 }
856 }
857 }
858
859 return 0;
860 }
861
862 static int timerlat_top_main_loop(struct osnoise_tool *tool)
863 {
864 struct timerlat_params *params = to_timerlat_params(tool->params);
865 int retval;
866
867 if (params->mode == TRACING_MODE_TRACEFS) {
868 retval = top_main_loop(tool);
869 } else {
870 retval = timerlat_top_bpf_main_loop(tool);
871 timerlat_bpf_detach();
872 }
873
874 return retval;
875 }
876
877 struct tool_ops timerlat_top_ops = {
878 .tracer = "timerlat",
879 .comm_prefix = "timerlat/",
880 .parse_args = timerlat_top_parse_args,
881 .init_tool = timerlat_init_top,
882 .apply_config = timerlat_top_apply_config,
883 .enable = timerlat_enable,
884 .main = timerlat_top_main_loop,
885 .print_stats = timerlat_print_stats,
886 .analyze = timerlat_analyze,
887 .free = timerlat_free_top_tool,
888 };