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 "utils.h"
18 #include "osnoise.h"
19 #include "timerlat.h"
20 #include "timerlat_aa.h"
21 #include "timerlat_u.h"
22
23 struct timerlat_hist_params {
24 char *cpus;
25 cpu_set_t monitored_cpus;
26 char *trace_output;
27 char *cgroup_name;
28 unsigned long long runtime;
29 long long stop_us;
30 long long stop_total_us;
31 long long timerlat_period_us;
32 long long print_stack;
33 int sleep_time;
34 int output_divisor;
35 int duration;
36 int set_sched;
37 int dma_latency;
38 int cgroup;
39 int hk_cpus;
40 int no_aa;
41 int dump_tasks;
42 int user_workload;
43 int kernel_workload;
44 int user_hist;
45 cpu_set_t hk_cpu_set;
46 struct sched_attr sched_param;
47 struct trace_events *events;
48 char no_irq;
49 char no_thread;
50 char no_header;
51 char no_summary;
52 char no_index;
53 char with_zeros;
54 int bucket_size;
55 int entries;
56 int warmup;
57 int buffer_size;
58 int deepest_idle_state;
59 };
60
61 struct timerlat_hist_cpu {
62 int *irq;
63 int *thread;
64 int *user;
65
66 unsigned long long irq_count;
67 unsigned long long thread_count;
68 unsigned long long user_count;
69
70 unsigned long long min_irq;
71 unsigned long long sum_irq;
72 unsigned long long max_irq;
73
74 unsigned long long min_thread;
75 unsigned long long sum_thread;
76 unsigned long long max_thread;
77
78 unsigned long long min_user;
79 unsigned long long sum_user;
80 unsigned long long max_user;
81 };
82
83 struct timerlat_hist_data {
84 struct timerlat_hist_cpu *hist;
85 int entries;
86 int bucket_size;
87 int nr_cpus;
88 };
89
90 /*
91 * timerlat_free_histogram - free runtime data
92 */
93 static void
timerlat_free_histogram(struct timerlat_hist_data * data)94 timerlat_free_histogram(struct timerlat_hist_data *data)
95 {
96 int cpu;
97
98 /* one histogram for IRQ and one for thread, per CPU */
99 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
100 if (data->hist[cpu].irq)
101 free(data->hist[cpu].irq);
102
103 if (data->hist[cpu].thread)
104 free(data->hist[cpu].thread);
105
106 if (data->hist[cpu].user)
107 free(data->hist[cpu].user);
108
109 }
110
111 /* one set of histograms per CPU */
112 if (data->hist)
113 free(data->hist);
114
115 free(data);
116 }
117
118 /*
119 * timerlat_alloc_histogram - alloc runtime data
120 */
121 static struct timerlat_hist_data
timerlat_alloc_histogram(int nr_cpus,int entries,int bucket_size)122 *timerlat_alloc_histogram(int nr_cpus, int entries, int bucket_size)
123 {
124 struct timerlat_hist_data *data;
125 int cpu;
126
127 data = calloc(1, sizeof(*data));
128 if (!data)
129 return NULL;
130
131 data->entries = entries;
132 data->bucket_size = bucket_size;
133 data->nr_cpus = nr_cpus;
134
135 /* one set of histograms per CPU */
136 data->hist = calloc(1, sizeof(*data->hist) * nr_cpus);
137 if (!data->hist)
138 goto cleanup;
139
140 /* one histogram for IRQ and one for thread, per cpu */
141 for (cpu = 0; cpu < nr_cpus; cpu++) {
142 data->hist[cpu].irq = calloc(1, sizeof(*data->hist->irq) * (entries + 1));
143 if (!data->hist[cpu].irq)
144 goto cleanup;
145
146 data->hist[cpu].thread = calloc(1, sizeof(*data->hist->thread) * (entries + 1));
147 if (!data->hist[cpu].thread)
148 goto cleanup;
149
150 data->hist[cpu].user = calloc(1, sizeof(*data->hist->user) * (entries + 1));
151 if (!data->hist[cpu].user)
152 goto cleanup;
153 }
154
155 /* set the min to max */
156 for (cpu = 0; cpu < nr_cpus; cpu++) {
157 data->hist[cpu].min_irq = ~0;
158 data->hist[cpu].min_thread = ~0;
159 data->hist[cpu].min_user = ~0;
160 }
161
162 return data;
163
164 cleanup:
165 timerlat_free_histogram(data);
166 return NULL;
167 }
168
169 /*
170 * timerlat_hist_update - record a new timerlat occurent on cpu, updating data
171 */
172 static void
timerlat_hist_update(struct osnoise_tool * tool,int cpu,unsigned long long context,unsigned long long latency)173 timerlat_hist_update(struct osnoise_tool *tool, int cpu,
174 unsigned long long context,
175 unsigned long long latency)
176 {
177 struct timerlat_hist_params *params = tool->params;
178 struct timerlat_hist_data *data = tool->data;
179 int entries = data->entries;
180 int bucket;
181 int *hist;
182
183 if (params->output_divisor)
184 latency = latency / params->output_divisor;
185
186 bucket = latency / data->bucket_size;
187
188 if (!context) {
189 hist = data->hist[cpu].irq;
190 data->hist[cpu].irq_count++;
191 update_min(&data->hist[cpu].min_irq, &latency);
192 update_sum(&data->hist[cpu].sum_irq, &latency);
193 update_max(&data->hist[cpu].max_irq, &latency);
194 } else if (context == 1) {
195 hist = data->hist[cpu].thread;
196 data->hist[cpu].thread_count++;
197 update_min(&data->hist[cpu].min_thread, &latency);
198 update_sum(&data->hist[cpu].sum_thread, &latency);
199 update_max(&data->hist[cpu].max_thread, &latency);
200 } else { /* user */
201 hist = data->hist[cpu].user;
202 data->hist[cpu].user_count++;
203 update_min(&data->hist[cpu].min_user, &latency);
204 update_sum(&data->hist[cpu].sum_user, &latency);
205 update_max(&data->hist[cpu].max_user, &latency);
206 }
207
208 if (bucket < entries)
209 hist[bucket]++;
210 else
211 hist[entries]++;
212 }
213
214 /*
215 * timerlat_hist_handler - this is the handler for timerlat tracer events
216 */
217 static int
timerlat_hist_handler(struct trace_seq * s,struct tep_record * record,struct tep_event * event,void * data)218 timerlat_hist_handler(struct trace_seq *s, struct tep_record *record,
219 struct tep_event *event, void *data)
220 {
221 struct trace_instance *trace = data;
222 unsigned long long context, latency;
223 struct osnoise_tool *tool;
224 int cpu = record->cpu;
225
226 tool = container_of(trace, struct osnoise_tool, trace);
227
228 tep_get_field_val(s, event, "context", record, &context, 1);
229 tep_get_field_val(s, event, "timer_latency", record, &latency, 1);
230
231 timerlat_hist_update(tool, cpu, context, latency);
232
233 return 0;
234 }
235
236 /*
237 * timerlat_hist_header - print the header of the tracer to the output
238 */
timerlat_hist_header(struct osnoise_tool * tool)239 static void timerlat_hist_header(struct osnoise_tool *tool)
240 {
241 struct timerlat_hist_params *params = tool->params;
242 struct timerlat_hist_data *data = tool->data;
243 struct trace_seq *s = tool->trace.seq;
244 char duration[26];
245 int cpu;
246
247 if (params->no_header)
248 return;
249
250 get_duration(tool->start_time, duration, sizeof(duration));
251 trace_seq_printf(s, "# RTLA timerlat histogram\n");
252 trace_seq_printf(s, "# Time unit is %s (%s)\n",
253 params->output_divisor == 1 ? "nanoseconds" : "microseconds",
254 params->output_divisor == 1 ? "ns" : "us");
255
256 trace_seq_printf(s, "# Duration: %s\n", duration);
257
258 if (!params->no_index)
259 trace_seq_printf(s, "Index");
260
261 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
262 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
263 continue;
264
265 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
266 continue;
267
268 if (!params->no_irq)
269 trace_seq_printf(s, " IRQ-%03d", cpu);
270
271 if (!params->no_thread)
272 trace_seq_printf(s, " Thr-%03d", cpu);
273
274 if (params->user_hist)
275 trace_seq_printf(s, " Usr-%03d", cpu);
276 }
277 trace_seq_printf(s, "\n");
278
279
280 trace_seq_do_printf(s);
281 trace_seq_reset(s);
282 }
283
284 /*
285 * format_summary_value - format a line of summary value (min, max or avg)
286 * of hist data
287 */
format_summary_value(struct trace_seq * seq,int count,unsigned long long val,bool avg)288 static void format_summary_value(struct trace_seq *seq,
289 int count,
290 unsigned long long val,
291 bool avg)
292 {
293 if (count)
294 trace_seq_printf(seq, "%9llu ", avg ? val / count : val);
295 else
296 trace_seq_printf(seq, "%9c ", '-');
297 }
298
299 /*
300 * timerlat_print_summary - print the summary of the hist data to the output
301 */
302 static void
timerlat_print_summary(struct timerlat_hist_params * params,struct trace_instance * trace,struct timerlat_hist_data * data)303 timerlat_print_summary(struct timerlat_hist_params *params,
304 struct trace_instance *trace,
305 struct timerlat_hist_data *data)
306 {
307 int cpu;
308
309 if (params->no_summary)
310 return;
311
312 if (!params->no_index)
313 trace_seq_printf(trace->seq, "count:");
314
315 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
316 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
317 continue;
318
319 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
320 continue;
321
322 if (!params->no_irq)
323 trace_seq_printf(trace->seq, "%9llu ",
324 data->hist[cpu].irq_count);
325
326 if (!params->no_thread)
327 trace_seq_printf(trace->seq, "%9llu ",
328 data->hist[cpu].thread_count);
329
330 if (params->user_hist)
331 trace_seq_printf(trace->seq, "%9llu ",
332 data->hist[cpu].user_count);
333 }
334 trace_seq_printf(trace->seq, "\n");
335
336 if (!params->no_index)
337 trace_seq_printf(trace->seq, "min: ");
338
339 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
340 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
341 continue;
342
343 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
344 continue;
345
346 if (!params->no_irq)
347 format_summary_value(trace->seq,
348 data->hist[cpu].irq_count,
349 data->hist[cpu].min_irq,
350 false);
351
352 if (!params->no_thread)
353 format_summary_value(trace->seq,
354 data->hist[cpu].thread_count,
355 data->hist[cpu].min_thread,
356 false);
357
358 if (params->user_hist)
359 format_summary_value(trace->seq,
360 data->hist[cpu].user_count,
361 data->hist[cpu].min_user,
362 false);
363 }
364 trace_seq_printf(trace->seq, "\n");
365
366 if (!params->no_index)
367 trace_seq_printf(trace->seq, "avg: ");
368
369 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
370 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
371 continue;
372
373 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
374 continue;
375
376 if (!params->no_irq)
377 format_summary_value(trace->seq,
378 data->hist[cpu].irq_count,
379 data->hist[cpu].sum_irq,
380 true);
381
382 if (!params->no_thread)
383 format_summary_value(trace->seq,
384 data->hist[cpu].thread_count,
385 data->hist[cpu].sum_thread,
386 true);
387
388 if (params->user_hist)
389 format_summary_value(trace->seq,
390 data->hist[cpu].user_count,
391 data->hist[cpu].sum_user,
392 true);
393 }
394 trace_seq_printf(trace->seq, "\n");
395
396 if (!params->no_index)
397 trace_seq_printf(trace->seq, "max: ");
398
399 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
400 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
401 continue;
402
403 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
404 continue;
405
406 if (!params->no_irq)
407 format_summary_value(trace->seq,
408 data->hist[cpu].irq_count,
409 data->hist[cpu].max_irq,
410 false);
411
412 if (!params->no_thread)
413 format_summary_value(trace->seq,
414 data->hist[cpu].thread_count,
415 data->hist[cpu].max_thread,
416 false);
417
418 if (params->user_hist)
419 format_summary_value(trace->seq,
420 data->hist[cpu].user_count,
421 data->hist[cpu].max_user,
422 false);
423 }
424 trace_seq_printf(trace->seq, "\n");
425 trace_seq_do_printf(trace->seq);
426 trace_seq_reset(trace->seq);
427 }
428
429 static void
timerlat_print_stats_all(struct timerlat_hist_params * params,struct trace_instance * trace,struct timerlat_hist_data * data)430 timerlat_print_stats_all(struct timerlat_hist_params *params,
431 struct trace_instance *trace,
432 struct timerlat_hist_data *data)
433 {
434 struct timerlat_hist_cpu *cpu_data;
435 struct timerlat_hist_cpu sum;
436 int cpu;
437
438 if (params->no_summary)
439 return;
440
441 memset(&sum, 0, sizeof(sum));
442 sum.min_irq = ~0;
443 sum.min_thread = ~0;
444 sum.min_user = ~0;
445
446 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
447 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
448 continue;
449
450 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
451 continue;
452
453 cpu_data = &data->hist[cpu];
454
455 sum.irq_count += cpu_data->irq_count;
456 update_min(&sum.min_irq, &cpu_data->min_irq);
457 update_sum(&sum.sum_irq, &cpu_data->sum_irq);
458 update_max(&sum.max_irq, &cpu_data->max_irq);
459
460 sum.thread_count += cpu_data->thread_count;
461 update_min(&sum.min_thread, &cpu_data->min_thread);
462 update_sum(&sum.sum_thread, &cpu_data->sum_thread);
463 update_max(&sum.max_thread, &cpu_data->max_thread);
464
465 sum.user_count += cpu_data->user_count;
466 update_min(&sum.min_user, &cpu_data->min_user);
467 update_sum(&sum.sum_user, &cpu_data->sum_user);
468 update_max(&sum.max_user, &cpu_data->max_user);
469 }
470
471 if (!params->no_index)
472 trace_seq_printf(trace->seq, "ALL: ");
473
474 if (!params->no_irq)
475 trace_seq_printf(trace->seq, " IRQ");
476
477 if (!params->no_thread)
478 trace_seq_printf(trace->seq, " Thr");
479
480 if (params->user_hist)
481 trace_seq_printf(trace->seq, " Usr");
482
483 trace_seq_printf(trace->seq, "\n");
484
485 if (!params->no_index)
486 trace_seq_printf(trace->seq, "count:");
487
488 if (!params->no_irq)
489 trace_seq_printf(trace->seq, "%9llu ",
490 sum.irq_count);
491
492 if (!params->no_thread)
493 trace_seq_printf(trace->seq, "%9llu ",
494 sum.thread_count);
495
496 if (params->user_hist)
497 trace_seq_printf(trace->seq, "%9llu ",
498 sum.user_count);
499
500 trace_seq_printf(trace->seq, "\n");
501
502 if (!params->no_index)
503 trace_seq_printf(trace->seq, "min: ");
504
505 if (!params->no_irq)
506 format_summary_value(trace->seq,
507 sum.irq_count,
508 sum.min_irq,
509 false);
510
511 if (!params->no_thread)
512 format_summary_value(trace->seq,
513 sum.thread_count,
514 sum.min_thread,
515 false);
516
517 if (params->user_hist)
518 format_summary_value(trace->seq,
519 sum.user_count,
520 sum.min_user,
521 false);
522
523 trace_seq_printf(trace->seq, "\n");
524
525 if (!params->no_index)
526 trace_seq_printf(trace->seq, "avg: ");
527
528 if (!params->no_irq)
529 format_summary_value(trace->seq,
530 sum.irq_count,
531 sum.sum_irq,
532 true);
533
534 if (!params->no_thread)
535 format_summary_value(trace->seq,
536 sum.thread_count,
537 sum.sum_thread,
538 true);
539
540 if (params->user_hist)
541 format_summary_value(trace->seq,
542 sum.user_count,
543 sum.sum_user,
544 true);
545
546 trace_seq_printf(trace->seq, "\n");
547
548 if (!params->no_index)
549 trace_seq_printf(trace->seq, "max: ");
550
551 if (!params->no_irq)
552 format_summary_value(trace->seq,
553 sum.irq_count,
554 sum.max_irq,
555 false);
556
557 if (!params->no_thread)
558 format_summary_value(trace->seq,
559 sum.thread_count,
560 sum.max_thread,
561 false);
562
563 if (params->user_hist)
564 format_summary_value(trace->seq,
565 sum.user_count,
566 sum.max_user,
567 false);
568
569 trace_seq_printf(trace->seq, "\n");
570 trace_seq_do_printf(trace->seq);
571 trace_seq_reset(trace->seq);
572 }
573
574 /*
575 * timerlat_print_stats - print data for each CPUs
576 */
577 static void
timerlat_print_stats(struct timerlat_hist_params * params,struct osnoise_tool * tool)578 timerlat_print_stats(struct timerlat_hist_params *params, struct osnoise_tool *tool)
579 {
580 struct timerlat_hist_data *data = tool->data;
581 struct trace_instance *trace = &tool->trace;
582 int bucket, cpu;
583 int total;
584
585 timerlat_hist_header(tool);
586
587 for (bucket = 0; bucket < data->entries; bucket++) {
588 total = 0;
589
590 if (!params->no_index)
591 trace_seq_printf(trace->seq, "%-6d",
592 bucket * data->bucket_size);
593
594 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
595 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
596 continue;
597
598 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
599 continue;
600
601 if (!params->no_irq) {
602 total += data->hist[cpu].irq[bucket];
603 trace_seq_printf(trace->seq, "%9d ",
604 data->hist[cpu].irq[bucket]);
605 }
606
607 if (!params->no_thread) {
608 total += data->hist[cpu].thread[bucket];
609 trace_seq_printf(trace->seq, "%9d ",
610 data->hist[cpu].thread[bucket]);
611 }
612
613 if (params->user_hist) {
614 total += data->hist[cpu].user[bucket];
615 trace_seq_printf(trace->seq, "%9d ",
616 data->hist[cpu].user[bucket]);
617 }
618
619 }
620
621 if (total == 0 && !params->with_zeros) {
622 trace_seq_reset(trace->seq);
623 continue;
624 }
625
626 trace_seq_printf(trace->seq, "\n");
627 trace_seq_do_printf(trace->seq);
628 trace_seq_reset(trace->seq);
629 }
630
631 if (!params->no_index)
632 trace_seq_printf(trace->seq, "over: ");
633
634 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
635 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
636 continue;
637
638 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
639 continue;
640
641 if (!params->no_irq)
642 trace_seq_printf(trace->seq, "%9d ",
643 data->hist[cpu].irq[data->entries]);
644
645 if (!params->no_thread)
646 trace_seq_printf(trace->seq, "%9d ",
647 data->hist[cpu].thread[data->entries]);
648
649 if (params->user_hist)
650 trace_seq_printf(trace->seq, "%9d ",
651 data->hist[cpu].user[data->entries]);
652 }
653 trace_seq_printf(trace->seq, "\n");
654 trace_seq_do_printf(trace->seq);
655 trace_seq_reset(trace->seq);
656
657 timerlat_print_summary(params, trace, data);
658 timerlat_print_stats_all(params, trace, data);
659 osnoise_report_missed_events(tool);
660 }
661
662 /*
663 * timerlat_hist_usage - prints timerlat top usage message
664 */
timerlat_hist_usage(char * usage)665 static void timerlat_hist_usage(char *usage)
666 {
667 int i;
668
669 char *msg[] = {
670 "",
671 " usage: [rtla] timerlat hist [-h] [-q] [-d s] [-D] [-n] [-a us] [-p us] [-i us] [-T us] [-s us] \\",
672 " [-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\",
673 " [-P priority] [-E N] [-b N] [--no-irq] [--no-thread] [--no-header] [--no-summary] \\",
674 " [--no-index] [--with-zeros] [--dma-latency us] [-C[=cgroup_name]] [--no-aa] [--dump-task] [-u|-k]",
675 " [--warm-up s] [--deepest-idle-state n]",
676 "",
677 " -h/--help: print this menu",
678 " -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",
679 " -p/--period us: timerlat period in us",
680 " -i/--irq us: stop trace if the irq latency is higher than the argument in us",
681 " -T/--thread us: stop trace if the thread latency is higher than the argument in us",
682 " -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",
683 " -c/--cpus cpus: run the tracer only on the given cpus",
684 " -H/--house-keeping cpus: run rtla control threads only on the given cpus",
685 " -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",
686 " -d/--duration time[m|h|d]: duration of the session in seconds",
687 " --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)",
688 " -D/--debug: print debug info",
689 " -t/--trace[file]: save the stopped trace to [file|timerlat_trace.txt]",
690 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
691 " --filter <filter>: enable a trace event filter to the previous -e event",
692 " --trigger <trigger>: enable a trace event trigger to the previous -e event",
693 " -n/--nano: display data in nanoseconds",
694 " --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage",
695 " -b/--bucket-size N: set the histogram bucket size (default 1)",
696 " -E/--entries N: set the number of entries of the histogram (default 256)",
697 " --no-irq: ignore IRQ latencies",
698 " --no-thread: ignore thread latencies",
699 " --no-header: do not print header",
700 " --no-summary: do not print summary",
701 " --no-index: do not print index",
702 " --with-zeros: print zero only entries",
703 " --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency",
704 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
705 " o:prio - use SCHED_OTHER with prio",
706 " r:prio - use SCHED_RR with prio",
707 " f:prio - use SCHED_FIFO with prio",
708 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
709 " in nanoseconds",
710 " -u/--user-threads: use rtla user-space threads instead of kernel-space timerlat threads",
711 " -k/--kernel-threads: use timerlat kernel-space threads instead of rtla user-space threads",
712 " -U/--user-load: enable timerlat for user-defined user-space workload",
713 " --warm-up s: let the workload run for s seconds before collecting data",
714 " --trace-buffer-size kB: set the per-cpu trace buffer size in kB",
715 " --deepest-idle-state n: only go down to idle state n on cpus used by timerlat to reduce exit from idle latency",
716 NULL,
717 };
718
719 if (usage)
720 fprintf(stderr, "%s\n", usage);
721
722 fprintf(stderr, "rtla timerlat hist: a per-cpu histogram of the timer latency (version %s)\n",
723 VERSION);
724
725 for (i = 0; msg[i]; i++)
726 fprintf(stderr, "%s\n", msg[i]);
727
728 if (usage)
729 exit(EXIT_FAILURE);
730
731 exit(EXIT_SUCCESS);
732 }
733
734 /*
735 * timerlat_hist_parse_args - allocs, parse and fill the cmd line parameters
736 */
737 static struct timerlat_hist_params
timerlat_hist_parse_args(int argc,char * argv[])738 *timerlat_hist_parse_args(int argc, char *argv[])
739 {
740 struct timerlat_hist_params *params;
741 struct trace_events *tevent;
742 int auto_thresh;
743 int retval;
744 int c;
745
746 params = calloc(1, sizeof(*params));
747 if (!params)
748 exit(1);
749
750 /* disabled by default */
751 params->dma_latency = -1;
752
753 /* disabled by default */
754 params->deepest_idle_state = -2;
755
756 /* display data in microseconds */
757 params->output_divisor = 1000;
758 params->bucket_size = 1;
759 params->entries = 256;
760
761 while (1) {
762 static struct option long_options[] = {
763 {"auto", required_argument, 0, 'a'},
764 {"cpus", required_argument, 0, 'c'},
765 {"cgroup", optional_argument, 0, 'C'},
766 {"bucket-size", required_argument, 0, 'b'},
767 {"debug", no_argument, 0, 'D'},
768 {"entries", required_argument, 0, 'E'},
769 {"duration", required_argument, 0, 'd'},
770 {"house-keeping", required_argument, 0, 'H'},
771 {"help", no_argument, 0, 'h'},
772 {"irq", required_argument, 0, 'i'},
773 {"nano", no_argument, 0, 'n'},
774 {"period", required_argument, 0, 'p'},
775 {"priority", required_argument, 0, 'P'},
776 {"stack", required_argument, 0, 's'},
777 {"thread", required_argument, 0, 'T'},
778 {"trace", optional_argument, 0, 't'},
779 {"user-threads", no_argument, 0, 'u'},
780 {"kernel-threads", no_argument, 0, 'k'},
781 {"user-load", no_argument, 0, 'U'},
782 {"event", required_argument, 0, 'e'},
783 {"no-irq", no_argument, 0, '0'},
784 {"no-thread", no_argument, 0, '1'},
785 {"no-header", no_argument, 0, '2'},
786 {"no-summary", no_argument, 0, '3'},
787 {"no-index", no_argument, 0, '4'},
788 {"with-zeros", no_argument, 0, '5'},
789 {"trigger", required_argument, 0, '6'},
790 {"filter", required_argument, 0, '7'},
791 {"dma-latency", required_argument, 0, '8'},
792 {"no-aa", no_argument, 0, '9'},
793 {"dump-task", no_argument, 0, '\1'},
794 {"warm-up", required_argument, 0, '\2'},
795 {"trace-buffer-size", required_argument, 0, '\3'},
796 {"deepest-idle-state", required_argument, 0, '\4'},
797 {0, 0, 0, 0}
798 };
799
800 /* getopt_long stores the option index here. */
801 int option_index = 0;
802
803 c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:i:knp:P:s:t::T:uU0123456:7:8:9\1\2:\3:",
804 long_options, &option_index);
805
806 /* detect the end of the options. */
807 if (c == -1)
808 break;
809
810 switch (c) {
811 case 'a':
812 auto_thresh = get_llong_from_str(optarg);
813
814 /* set thread stop to auto_thresh */
815 params->stop_total_us = auto_thresh;
816 params->stop_us = auto_thresh;
817
818 /* get stack trace */
819 params->print_stack = auto_thresh;
820
821 /* set trace */
822 params->trace_output = "timerlat_trace.txt";
823
824 break;
825 case 'c':
826 retval = parse_cpu_set(optarg, ¶ms->monitored_cpus);
827 if (retval)
828 timerlat_hist_usage("\nInvalid -c cpu list\n");
829 params->cpus = optarg;
830 break;
831 case 'C':
832 params->cgroup = 1;
833 if (!optarg) {
834 /* will inherit this cgroup */
835 params->cgroup_name = NULL;
836 } else if (*optarg == '=') {
837 /* skip the = */
838 params->cgroup_name = ++optarg;
839 }
840 break;
841 case 'b':
842 params->bucket_size = get_llong_from_str(optarg);
843 if ((params->bucket_size == 0) || (params->bucket_size >= 1000000))
844 timerlat_hist_usage("Bucket size needs to be > 0 and <= 1000000\n");
845 break;
846 case 'D':
847 config_debug = 1;
848 break;
849 case 'd':
850 params->duration = parse_seconds_duration(optarg);
851 if (!params->duration)
852 timerlat_hist_usage("Invalid -D duration\n");
853 break;
854 case 'e':
855 tevent = trace_event_alloc(optarg);
856 if (!tevent) {
857 err_msg("Error alloc trace event");
858 exit(EXIT_FAILURE);
859 }
860
861 if (params->events)
862 tevent->next = params->events;
863
864 params->events = tevent;
865 break;
866 case 'E':
867 params->entries = get_llong_from_str(optarg);
868 if ((params->entries < 10) || (params->entries > 9999999))
869 timerlat_hist_usage("Entries must be > 10 and < 9999999\n");
870 break;
871 case 'h':
872 case '?':
873 timerlat_hist_usage(NULL);
874 break;
875 case 'H':
876 params->hk_cpus = 1;
877 retval = parse_cpu_set(optarg, ¶ms->hk_cpu_set);
878 if (retval) {
879 err_msg("Error parsing house keeping CPUs\n");
880 exit(EXIT_FAILURE);
881 }
882 break;
883 case 'i':
884 params->stop_us = get_llong_from_str(optarg);
885 break;
886 case 'k':
887 params->kernel_workload = 1;
888 break;
889 case 'n':
890 params->output_divisor = 1;
891 break;
892 case 'p':
893 params->timerlat_period_us = get_llong_from_str(optarg);
894 if (params->timerlat_period_us > 1000000)
895 timerlat_hist_usage("Period longer than 1 s\n");
896 break;
897 case 'P':
898 retval = parse_prio(optarg, ¶ms->sched_param);
899 if (retval == -1)
900 timerlat_hist_usage("Invalid -P priority");
901 params->set_sched = 1;
902 break;
903 case 's':
904 params->print_stack = get_llong_from_str(optarg);
905 break;
906 case 'T':
907 params->stop_total_us = get_llong_from_str(optarg);
908 break;
909 case 't':
910 if (optarg) {
911 if (optarg[0] == '=')
912 params->trace_output = &optarg[1];
913 else
914 params->trace_output = &optarg[0];
915 } else if (optind < argc && argv[optind][0] != '-')
916 params->trace_output = argv[optind];
917 else
918 params->trace_output = "timerlat_trace.txt";
919 break;
920 case 'u':
921 params->user_workload = 1;
922 /* fallback: -u implies in -U */
923 case 'U':
924 params->user_hist = 1;
925 break;
926 case '0': /* no irq */
927 params->no_irq = 1;
928 break;
929 case '1': /* no thread */
930 params->no_thread = 1;
931 break;
932 case '2': /* no header */
933 params->no_header = 1;
934 break;
935 case '3': /* no summary */
936 params->no_summary = 1;
937 break;
938 case '4': /* no index */
939 params->no_index = 1;
940 break;
941 case '5': /* with zeros */
942 params->with_zeros = 1;
943 break;
944 case '6': /* trigger */
945 if (params->events) {
946 retval = trace_event_add_trigger(params->events, optarg);
947 if (retval) {
948 err_msg("Error adding trigger %s\n", optarg);
949 exit(EXIT_FAILURE);
950 }
951 } else {
952 timerlat_hist_usage("--trigger requires a previous -e\n");
953 }
954 break;
955 case '7': /* filter */
956 if (params->events) {
957 retval = trace_event_add_filter(params->events, optarg);
958 if (retval) {
959 err_msg("Error adding filter %s\n", optarg);
960 exit(EXIT_FAILURE);
961 }
962 } else {
963 timerlat_hist_usage("--filter requires a previous -e\n");
964 }
965 break;
966 case '8':
967 params->dma_latency = get_llong_from_str(optarg);
968 if (params->dma_latency < 0 || params->dma_latency > 10000) {
969 err_msg("--dma-latency needs to be >= 0 and < 10000");
970 exit(EXIT_FAILURE);
971 }
972 break;
973 case '9':
974 params->no_aa = 1;
975 break;
976 case '\1':
977 params->dump_tasks = 1;
978 break;
979 case '\2':
980 params->warmup = get_llong_from_str(optarg);
981 break;
982 case '\3':
983 params->buffer_size = get_llong_from_str(optarg);
984 break;
985 case '\4':
986 params->deepest_idle_state = get_llong_from_str(optarg);
987 break;
988 default:
989 timerlat_hist_usage("Invalid option");
990 }
991 }
992
993 if (geteuid()) {
994 err_msg("rtla needs root permission\n");
995 exit(EXIT_FAILURE);
996 }
997
998 if (params->no_irq && params->no_thread)
999 timerlat_hist_usage("no-irq and no-thread set, there is nothing to do here");
1000
1001 if (params->no_index && !params->with_zeros)
1002 timerlat_hist_usage("no-index set with with-zeros is not set - it does not make sense");
1003
1004 /*
1005 * Auto analysis only happens if stop tracing, thus:
1006 */
1007 if (!params->stop_us && !params->stop_total_us)
1008 params->no_aa = 1;
1009
1010 if (params->kernel_workload && params->user_workload)
1011 timerlat_hist_usage("--kernel-threads and --user-threads are mutually exclusive!");
1012
1013 return params;
1014 }
1015
1016 /*
1017 * timerlat_hist_apply_config - apply the hist configs to the initialized tool
1018 */
1019 static int
timerlat_hist_apply_config(struct osnoise_tool * tool,struct timerlat_hist_params * params)1020 timerlat_hist_apply_config(struct osnoise_tool *tool, struct timerlat_hist_params *params)
1021 {
1022 int retval, i;
1023
1024 if (!params->sleep_time)
1025 params->sleep_time = 1;
1026
1027 if (params->cpus) {
1028 retval = osnoise_set_cpus(tool->context, params->cpus);
1029 if (retval) {
1030 err_msg("Failed to apply CPUs config\n");
1031 goto out_err;
1032 }
1033 } else {
1034 for (i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++)
1035 CPU_SET(i, ¶ms->monitored_cpus);
1036 }
1037
1038 if (params->stop_us) {
1039 retval = osnoise_set_stop_us(tool->context, params->stop_us);
1040 if (retval) {
1041 err_msg("Failed to set stop us\n");
1042 goto out_err;
1043 }
1044 }
1045
1046 if (params->stop_total_us) {
1047 retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us);
1048 if (retval) {
1049 err_msg("Failed to set stop total us\n");
1050 goto out_err;
1051 }
1052 }
1053
1054 if (params->timerlat_period_us) {
1055 retval = osnoise_set_timerlat_period_us(tool->context, params->timerlat_period_us);
1056 if (retval) {
1057 err_msg("Failed to set timerlat period\n");
1058 goto out_err;
1059 }
1060 }
1061
1062 if (params->print_stack) {
1063 retval = osnoise_set_print_stack(tool->context, params->print_stack);
1064 if (retval) {
1065 err_msg("Failed to set print stack\n");
1066 goto out_err;
1067 }
1068 }
1069
1070 if (params->hk_cpus) {
1071 retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set),
1072 ¶ms->hk_cpu_set);
1073 if (retval == -1) {
1074 err_msg("Failed to set rtla to the house keeping CPUs\n");
1075 goto out_err;
1076 }
1077 } else if (params->cpus) {
1078 /*
1079 * Even if the user do not set a house-keeping CPU, try to
1080 * move rtla to a CPU set different to the one where the user
1081 * set the workload to run.
1082 *
1083 * No need to check results as this is an automatic attempt.
1084 */
1085 auto_house_keeping(¶ms->monitored_cpus);
1086 }
1087
1088 /*
1089 * If the user did not specify a type of thread, try user-threads first.
1090 * Fall back to kernel threads otherwise.
1091 */
1092 if (!params->kernel_workload && !params->user_hist) {
1093 retval = tracefs_file_exists(NULL, "osnoise/per_cpu/cpu0/timerlat_fd");
1094 if (retval) {
1095 debug_msg("User-space interface detected, setting user-threads\n");
1096 params->user_workload = 1;
1097 params->user_hist = 1;
1098 } else {
1099 debug_msg("User-space interface not detected, setting kernel-threads\n");
1100 params->kernel_workload = 1;
1101 }
1102 }
1103
1104 /*
1105 * Set workload according to type of thread if the kernel supports it.
1106 * On kernels without support, user threads will have already failed
1107 * on missing timerlat_fd, and kernel threads do not need it.
1108 */
1109 retval = osnoise_set_workload(tool->context, params->kernel_workload);
1110 if (retval < -1) {
1111 err_msg("Failed to set OSNOISE_WORKLOAD option\n");
1112 goto out_err;
1113 }
1114
1115 return 0;
1116
1117 out_err:
1118 return -1;
1119 }
1120
1121 /*
1122 * timerlat_init_hist - initialize a timerlat hist tool with parameters
1123 */
1124 static struct osnoise_tool
timerlat_init_hist(struct timerlat_hist_params * params)1125 *timerlat_init_hist(struct timerlat_hist_params *params)
1126 {
1127 struct osnoise_tool *tool;
1128 int nr_cpus;
1129
1130 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
1131
1132 tool = osnoise_init_tool("timerlat_hist");
1133 if (!tool)
1134 return NULL;
1135
1136 tool->data = timerlat_alloc_histogram(nr_cpus, params->entries, params->bucket_size);
1137 if (!tool->data)
1138 goto out_err;
1139
1140 tool->params = params;
1141
1142 tep_register_event_handler(tool->trace.tep, -1, "ftrace", "timerlat",
1143 timerlat_hist_handler, tool);
1144
1145 return tool;
1146
1147 out_err:
1148 osnoise_destroy_tool(tool);
1149 return NULL;
1150 }
1151
1152 static int stop_tracing;
1153 static struct trace_instance *hist_inst = NULL;
stop_hist(int sig)1154 static void stop_hist(int sig)
1155 {
1156 if (stop_tracing) {
1157 /*
1158 * Stop requested twice in a row; abort event processing and
1159 * exit immediately
1160 */
1161 tracefs_iterate_stop(hist_inst->inst);
1162 return;
1163 }
1164 stop_tracing = 1;
1165 if (hist_inst)
1166 trace_instance_stop(hist_inst);
1167 }
1168
1169 /*
1170 * timerlat_hist_set_signals - handles the signal to stop the tool
1171 */
1172 static void
timerlat_hist_set_signals(struct timerlat_hist_params * params)1173 timerlat_hist_set_signals(struct timerlat_hist_params *params)
1174 {
1175 signal(SIGINT, stop_hist);
1176 if (params->duration) {
1177 signal(SIGALRM, stop_hist);
1178 alarm(params->duration);
1179 }
1180 }
1181
timerlat_hist_main(int argc,char * argv[])1182 int timerlat_hist_main(int argc, char *argv[])
1183 {
1184 struct timerlat_hist_params *params;
1185 struct osnoise_tool *record = NULL;
1186 struct timerlat_u_params params_u;
1187 struct osnoise_tool *tool = NULL;
1188 struct osnoise_tool *aa = NULL;
1189 struct trace_instance *trace;
1190 int dma_latency_fd = -1;
1191 int return_value = 1;
1192 pthread_t timerlat_u;
1193 int retval;
1194 int nr_cpus, i;
1195
1196 params = timerlat_hist_parse_args(argc, argv);
1197 if (!params)
1198 exit(1);
1199
1200 tool = timerlat_init_hist(params);
1201 if (!tool) {
1202 err_msg("Could not init osnoise hist\n");
1203 goto out_exit;
1204 }
1205
1206 retval = timerlat_hist_apply_config(tool, params);
1207 if (retval) {
1208 err_msg("Could not apply config\n");
1209 goto out_free;
1210 }
1211
1212 trace = &tool->trace;
1213 /*
1214 * Save trace instance into global variable so that SIGINT can stop
1215 * the timerlat tracer.
1216 * Otherwise, rtla could loop indefinitely when overloaded.
1217 */
1218 hist_inst = trace;
1219
1220 retval = enable_timerlat(trace);
1221 if (retval) {
1222 err_msg("Failed to enable timerlat tracer\n");
1223 goto out_free;
1224 }
1225
1226 if (params->set_sched) {
1227 retval = set_comm_sched_attr("timerlat/", ¶ms->sched_param);
1228 if (retval) {
1229 err_msg("Failed to set sched parameters\n");
1230 goto out_free;
1231 }
1232 }
1233
1234 if (params->cgroup && !params->user_workload) {
1235 retval = set_comm_cgroup("timerlat/", params->cgroup_name);
1236 if (!retval) {
1237 err_msg("Failed to move threads to cgroup\n");
1238 goto out_free;
1239 }
1240 }
1241
1242 if (params->dma_latency >= 0) {
1243 dma_latency_fd = set_cpu_dma_latency(params->dma_latency);
1244 if (dma_latency_fd < 0) {
1245 err_msg("Could not set /dev/cpu_dma_latency.\n");
1246 goto out_free;
1247 }
1248 }
1249
1250 if (params->deepest_idle_state >= -1) {
1251 if (!have_libcpupower_support()) {
1252 err_msg("rtla built without libcpupower, --deepest-idle-state is not supported\n");
1253 goto out_free;
1254 }
1255
1256 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
1257
1258 for (i = 0; i < nr_cpus; i++) {
1259 if (params->cpus && !CPU_ISSET(i, ¶ms->monitored_cpus))
1260 continue;
1261 if (save_cpu_idle_disable_state(i) < 0) {
1262 err_msg("Could not save cpu idle state.\n");
1263 goto out_free;
1264 }
1265 if (set_deepest_cpu_idle_state(i, params->deepest_idle_state) < 0) {
1266 err_msg("Could not set deepest cpu idle state.\n");
1267 goto out_free;
1268 }
1269 }
1270 }
1271
1272 if (params->trace_output) {
1273 record = osnoise_init_trace_tool("timerlat");
1274 if (!record) {
1275 err_msg("Failed to enable the trace instance\n");
1276 goto out_free;
1277 }
1278
1279 if (params->events) {
1280 retval = trace_events_enable(&record->trace, params->events);
1281 if (retval)
1282 goto out_hist;
1283 }
1284
1285 if (params->buffer_size > 0) {
1286 retval = trace_set_buffer_size(&record->trace, params->buffer_size);
1287 if (retval)
1288 goto out_hist;
1289 }
1290 }
1291
1292 if (!params->no_aa) {
1293 aa = osnoise_init_tool("timerlat_aa");
1294 if (!aa)
1295 goto out_hist;
1296
1297 retval = timerlat_aa_init(aa, params->dump_tasks);
1298 if (retval) {
1299 err_msg("Failed to enable the auto analysis instance\n");
1300 goto out_hist;
1301 }
1302
1303 retval = enable_timerlat(&aa->trace);
1304 if (retval) {
1305 err_msg("Failed to enable timerlat tracer\n");
1306 goto out_hist;
1307 }
1308 }
1309
1310 if (params->user_workload) {
1311 /* rtla asked to stop */
1312 params_u.should_run = 1;
1313 /* all threads left */
1314 params_u.stopped_running = 0;
1315
1316 params_u.set = ¶ms->monitored_cpus;
1317 if (params->set_sched)
1318 params_u.sched_param = ¶ms->sched_param;
1319 else
1320 params_u.sched_param = NULL;
1321
1322 params_u.cgroup_name = params->cgroup_name;
1323
1324 retval = pthread_create(&timerlat_u, NULL, timerlat_u_dispatcher, ¶ms_u);
1325 if (retval)
1326 err_msg("Error creating timerlat user-space threads\n");
1327 }
1328
1329 if (params->warmup > 0) {
1330 debug_msg("Warming up for %d seconds\n", params->warmup);
1331 sleep(params->warmup);
1332 if (stop_tracing)
1333 goto out_hist;
1334 }
1335
1336 /*
1337 * Start the tracers here, after having set all instances.
1338 *
1339 * Let the trace instance start first for the case of hitting a stop
1340 * tracing while enabling other instances. The trace instance is the
1341 * one with most valuable information.
1342 */
1343 if (params->trace_output)
1344 trace_instance_start(&record->trace);
1345 if (!params->no_aa)
1346 trace_instance_start(&aa->trace);
1347 trace_instance_start(trace);
1348
1349 tool->start_time = time(NULL);
1350 timerlat_hist_set_signals(params);
1351
1352 while (!stop_tracing) {
1353 sleep(params->sleep_time);
1354
1355 retval = tracefs_iterate_raw_events(trace->tep,
1356 trace->inst,
1357 NULL,
1358 0,
1359 collect_registered_events,
1360 trace);
1361 if (retval < 0) {
1362 err_msg("Error iterating on events\n");
1363 goto out_hist;
1364 }
1365
1366 if (osnoise_trace_is_off(tool, record))
1367 break;
1368
1369 /* is there still any user-threads ? */
1370 if (params->user_workload) {
1371 if (params_u.stopped_running) {
1372 debug_msg("timerlat user-space threads stopped!\n");
1373 break;
1374 }
1375 }
1376 }
1377
1378 if (params->user_workload && !params_u.stopped_running) {
1379 params_u.should_run = 0;
1380 sleep(1);
1381 }
1382
1383 timerlat_print_stats(params, tool);
1384
1385 return_value = 0;
1386
1387 if (osnoise_trace_is_off(tool, record) && !stop_tracing) {
1388 printf("rtla timerlat hit stop tracing\n");
1389
1390 if (!params->no_aa)
1391 timerlat_auto_analysis(params->stop_us, params->stop_total_us);
1392
1393 if (params->trace_output) {
1394 printf(" Saving trace to %s\n", params->trace_output);
1395 save_trace_to_file(record->trace.inst, params->trace_output);
1396 }
1397 }
1398
1399 out_hist:
1400 timerlat_aa_destroy();
1401 if (dma_latency_fd >= 0)
1402 close(dma_latency_fd);
1403 if (params->deepest_idle_state >= -1) {
1404 for (i = 0; i < nr_cpus; i++) {
1405 if (params->cpus && !CPU_ISSET(i, ¶ms->monitored_cpus))
1406 continue;
1407 restore_cpu_idle_disable_state(i);
1408 }
1409 }
1410 trace_events_destroy(&record->trace, params->events);
1411 params->events = NULL;
1412 out_free:
1413 timerlat_free_histogram(tool->data);
1414 osnoise_destroy_tool(aa);
1415 osnoise_destroy_tool(record);
1416 osnoise_destroy_tool(tool);
1417 free(params);
1418 free_cpu_idle_disable_states();
1419 out_exit:
1420 exit(return_value);
1421 }
1422