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