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_hist;
43 cpu_set_t hk_cpu_set;
44 struct sched_attr sched_param;
45 struct trace_events *events;
46 char no_irq;
47 char no_thread;
48 char no_header;
49 char no_summary;
50 char no_index;
51 char with_zeros;
52 int bucket_size;
53 int entries;
54 };
55
56 struct timerlat_hist_cpu {
57 int *irq;
58 int *thread;
59 int *user;
60
61 int irq_count;
62 int thread_count;
63 int user_count;
64
65 unsigned long long min_irq;
66 unsigned long long sum_irq;
67 unsigned long long max_irq;
68
69 unsigned long long min_thread;
70 unsigned long long sum_thread;
71 unsigned long long max_thread;
72
73 unsigned long long min_user;
74 unsigned long long sum_user;
75 unsigned long long max_user;
76 };
77
78 struct timerlat_hist_data {
79 struct timerlat_hist_cpu *hist;
80 int entries;
81 int bucket_size;
82 int nr_cpus;
83 };
84
85 /*
86 * timerlat_free_histogram - free runtime data
87 */
88 static void
timerlat_free_histogram(struct timerlat_hist_data * data)89 timerlat_free_histogram(struct timerlat_hist_data *data)
90 {
91 int cpu;
92
93 /* one histogram for IRQ and one for thread, per CPU */
94 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
95 if (data->hist[cpu].irq)
96 free(data->hist[cpu].irq);
97
98 if (data->hist[cpu].thread)
99 free(data->hist[cpu].thread);
100
101 if (data->hist[cpu].user)
102 free(data->hist[cpu].user);
103
104 }
105
106 /* one set of histograms per CPU */
107 if (data->hist)
108 free(data->hist);
109
110 free(data);
111 }
112
113 /*
114 * timerlat_alloc_histogram - alloc runtime data
115 */
116 static struct timerlat_hist_data
timerlat_alloc_histogram(int nr_cpus,int entries,int bucket_size)117 *timerlat_alloc_histogram(int nr_cpus, int entries, int bucket_size)
118 {
119 struct timerlat_hist_data *data;
120 int cpu;
121
122 data = calloc(1, sizeof(*data));
123 if (!data)
124 return NULL;
125
126 data->entries = entries;
127 data->bucket_size = bucket_size;
128 data->nr_cpus = nr_cpus;
129
130 /* one set of histograms per CPU */
131 data->hist = calloc(1, sizeof(*data->hist) * nr_cpus);
132 if (!data->hist)
133 goto cleanup;
134
135 /* one histogram for IRQ and one for thread, per cpu */
136 for (cpu = 0; cpu < nr_cpus; cpu++) {
137 data->hist[cpu].irq = calloc(1, sizeof(*data->hist->irq) * (entries + 1));
138 if (!data->hist[cpu].irq)
139 goto cleanup;
140
141 data->hist[cpu].thread = calloc(1, sizeof(*data->hist->thread) * (entries + 1));
142 if (!data->hist[cpu].thread)
143 goto cleanup;
144
145 data->hist[cpu].user = calloc(1, sizeof(*data->hist->user) * (entries + 1));
146 if (!data->hist[cpu].user)
147 goto cleanup;
148 }
149
150 /* set the min to max */
151 for (cpu = 0; cpu < nr_cpus; cpu++) {
152 data->hist[cpu].min_irq = ~0;
153 data->hist[cpu].min_thread = ~0;
154 data->hist[cpu].min_user = ~0;
155 }
156
157 return data;
158
159 cleanup:
160 timerlat_free_histogram(data);
161 return NULL;
162 }
163
164 /*
165 * timerlat_hist_update - record a new timerlat occurent on cpu, updating data
166 */
167 static void
timerlat_hist_update(struct osnoise_tool * tool,int cpu,unsigned long long context,unsigned long long latency)168 timerlat_hist_update(struct osnoise_tool *tool, int cpu,
169 unsigned long long context,
170 unsigned long long latency)
171 {
172 struct timerlat_hist_params *params = tool->params;
173 struct timerlat_hist_data *data = tool->data;
174 int entries = data->entries;
175 int bucket;
176 int *hist;
177
178 if (params->output_divisor)
179 latency = latency / params->output_divisor;
180
181 bucket = latency / data->bucket_size;
182
183 if (!context) {
184 hist = data->hist[cpu].irq;
185 data->hist[cpu].irq_count++;
186 update_min(&data->hist[cpu].min_irq, &latency);
187 update_sum(&data->hist[cpu].sum_irq, &latency);
188 update_max(&data->hist[cpu].max_irq, &latency);
189 } else if (context == 1) {
190 hist = data->hist[cpu].thread;
191 data->hist[cpu].thread_count++;
192 update_min(&data->hist[cpu].min_thread, &latency);
193 update_sum(&data->hist[cpu].sum_thread, &latency);
194 update_max(&data->hist[cpu].max_thread, &latency);
195 } else { /* user */
196 hist = data->hist[cpu].user;
197 data->hist[cpu].user_count++;
198 update_min(&data->hist[cpu].min_user, &latency);
199 update_sum(&data->hist[cpu].sum_user, &latency);
200 update_max(&data->hist[cpu].max_user, &latency);
201 }
202
203 if (bucket < entries)
204 hist[bucket]++;
205 else
206 hist[entries]++;
207 }
208
209 /*
210 * timerlat_hist_handler - this is the handler for timerlat tracer events
211 */
212 static int
timerlat_hist_handler(struct trace_seq * s,struct tep_record * record,struct tep_event * event,void * data)213 timerlat_hist_handler(struct trace_seq *s, struct tep_record *record,
214 struct tep_event *event, void *data)
215 {
216 struct trace_instance *trace = data;
217 unsigned long long context, latency;
218 struct osnoise_tool *tool;
219 int cpu = record->cpu;
220
221 tool = container_of(trace, struct osnoise_tool, trace);
222
223 tep_get_field_val(s, event, "context", record, &context, 1);
224 tep_get_field_val(s, event, "timer_latency", record, &latency, 1);
225
226 timerlat_hist_update(tool, cpu, context, latency);
227
228 return 0;
229 }
230
231 /*
232 * timerlat_hist_header - print the header of the tracer to the output
233 */
timerlat_hist_header(struct osnoise_tool * tool)234 static void timerlat_hist_header(struct osnoise_tool *tool)
235 {
236 struct timerlat_hist_params *params = tool->params;
237 struct timerlat_hist_data *data = tool->data;
238 struct trace_seq *s = tool->trace.seq;
239 char duration[26];
240 int cpu;
241
242 if (params->no_header)
243 return;
244
245 get_duration(tool->start_time, duration, sizeof(duration));
246 trace_seq_printf(s, "# RTLA timerlat histogram\n");
247 trace_seq_printf(s, "# Time unit is %s (%s)\n",
248 params->output_divisor == 1 ? "nanoseconds" : "microseconds",
249 params->output_divisor == 1 ? "ns" : "us");
250
251 trace_seq_printf(s, "# Duration: %s\n", duration);
252
253 if (!params->no_index)
254 trace_seq_printf(s, "Index");
255
256 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
257 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
258 continue;
259
260 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
261 continue;
262
263 if (!params->no_irq)
264 trace_seq_printf(s, " IRQ-%03d", cpu);
265
266 if (!params->no_thread)
267 trace_seq_printf(s, " Thr-%03d", cpu);
268
269 if (params->user_hist)
270 trace_seq_printf(s, " Usr-%03d", cpu);
271 }
272 trace_seq_printf(s, "\n");
273
274
275 trace_seq_do_printf(s);
276 trace_seq_reset(s);
277 }
278
279 /*
280 * timerlat_print_summary - print the summary of the hist data to the output
281 */
282 static void
timerlat_print_summary(struct timerlat_hist_params * params,struct trace_instance * trace,struct timerlat_hist_data * data)283 timerlat_print_summary(struct timerlat_hist_params *params,
284 struct trace_instance *trace,
285 struct timerlat_hist_data *data)
286 {
287 int cpu;
288
289 if (params->no_summary)
290 return;
291
292 if (!params->no_index)
293 trace_seq_printf(trace->seq, "count:");
294
295 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
296 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
297 continue;
298
299 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
300 continue;
301
302 if (!params->no_irq)
303 trace_seq_printf(trace->seq, "%9d ",
304 data->hist[cpu].irq_count);
305
306 if (!params->no_thread)
307 trace_seq_printf(trace->seq, "%9d ",
308 data->hist[cpu].thread_count);
309
310 if (params->user_hist)
311 trace_seq_printf(trace->seq, "%9d ",
312 data->hist[cpu].user_count);
313 }
314 trace_seq_printf(trace->seq, "\n");
315
316 if (!params->no_index)
317 trace_seq_printf(trace->seq, "min: ");
318
319 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
320 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
321 continue;
322
323 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
324 continue;
325
326 if (!params->no_irq)
327 trace_seq_printf(trace->seq, "%9llu ",
328 data->hist[cpu].min_irq);
329
330 if (!params->no_thread)
331 trace_seq_printf(trace->seq, "%9llu ",
332 data->hist[cpu].min_thread);
333
334 if (params->user_hist)
335 trace_seq_printf(trace->seq, "%9llu ",
336 data->hist[cpu].min_user);
337 }
338 trace_seq_printf(trace->seq, "\n");
339
340 if (!params->no_index)
341 trace_seq_printf(trace->seq, "avg: ");
342
343 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
344 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
345 continue;
346
347 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
348 continue;
349
350 if (!params->no_irq) {
351 if (data->hist[cpu].irq_count)
352 trace_seq_printf(trace->seq, "%9llu ",
353 data->hist[cpu].sum_irq / data->hist[cpu].irq_count);
354 else
355 trace_seq_printf(trace->seq, " - ");
356 }
357
358 if (!params->no_thread) {
359 if (data->hist[cpu].thread_count)
360 trace_seq_printf(trace->seq, "%9llu ",
361 data->hist[cpu].sum_thread / data->hist[cpu].thread_count);
362 else
363 trace_seq_printf(trace->seq, " - ");
364 }
365
366 if (params->user_hist) {
367 if (data->hist[cpu].user_count)
368 trace_seq_printf(trace->seq, "%9llu ",
369 data->hist[cpu].sum_user / data->hist[cpu].user_count);
370 else
371 trace_seq_printf(trace->seq, " - ");
372 }
373 }
374 trace_seq_printf(trace->seq, "\n");
375
376 if (!params->no_index)
377 trace_seq_printf(trace->seq, "max: ");
378
379 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
380 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
381 continue;
382
383 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
384 continue;
385
386 if (!params->no_irq)
387 trace_seq_printf(trace->seq, "%9llu ",
388 data->hist[cpu].max_irq);
389
390 if (!params->no_thread)
391 trace_seq_printf(trace->seq, "%9llu ",
392 data->hist[cpu].max_thread);
393
394 if (params->user_hist)
395 trace_seq_printf(trace->seq, "%9llu ",
396 data->hist[cpu].max_user);
397 }
398 trace_seq_printf(trace->seq, "\n");
399 trace_seq_do_printf(trace->seq);
400 trace_seq_reset(trace->seq);
401 }
402
403 /*
404 * timerlat_print_stats - print data for all CPUs
405 */
406 static void
timerlat_print_stats(struct timerlat_hist_params * params,struct osnoise_tool * tool)407 timerlat_print_stats(struct timerlat_hist_params *params, struct osnoise_tool *tool)
408 {
409 struct timerlat_hist_data *data = tool->data;
410 struct trace_instance *trace = &tool->trace;
411 int bucket, cpu;
412 int total;
413
414 timerlat_hist_header(tool);
415
416 for (bucket = 0; bucket < data->entries; bucket++) {
417 total = 0;
418
419 if (!params->no_index)
420 trace_seq_printf(trace->seq, "%-6d",
421 bucket * data->bucket_size);
422
423 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
424 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
425 continue;
426
427 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
428 continue;
429
430 if (!params->no_irq) {
431 total += data->hist[cpu].irq[bucket];
432 trace_seq_printf(trace->seq, "%9d ",
433 data->hist[cpu].irq[bucket]);
434 }
435
436 if (!params->no_thread) {
437 total += data->hist[cpu].thread[bucket];
438 trace_seq_printf(trace->seq, "%9d ",
439 data->hist[cpu].thread[bucket]);
440 }
441
442 if (params->user_hist) {
443 total += data->hist[cpu].user[bucket];
444 trace_seq_printf(trace->seq, "%9d ",
445 data->hist[cpu].user[bucket]);
446 }
447
448 }
449
450 if (total == 0 && !params->with_zeros) {
451 trace_seq_reset(trace->seq);
452 continue;
453 }
454
455 trace_seq_printf(trace->seq, "\n");
456 trace_seq_do_printf(trace->seq);
457 trace_seq_reset(trace->seq);
458 }
459
460 if (!params->no_index)
461 trace_seq_printf(trace->seq, "over: ");
462
463 for (cpu = 0; cpu < data->nr_cpus; cpu++) {
464 if (params->cpus && !CPU_ISSET(cpu, ¶ms->monitored_cpus))
465 continue;
466
467 if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
468 continue;
469
470 if (!params->no_irq)
471 trace_seq_printf(trace->seq, "%9d ",
472 data->hist[cpu].irq[data->entries]);
473
474 if (!params->no_thread)
475 trace_seq_printf(trace->seq, "%9d ",
476 data->hist[cpu].thread[data->entries]);
477
478 if (params->user_hist)
479 trace_seq_printf(trace->seq, "%9d ",
480 data->hist[cpu].user[data->entries]);
481 }
482 trace_seq_printf(trace->seq, "\n");
483 trace_seq_do_printf(trace->seq);
484 trace_seq_reset(trace->seq);
485
486 timerlat_print_summary(params, trace, data);
487 }
488
489 /*
490 * timerlat_hist_usage - prints timerlat top usage message
491 */
timerlat_hist_usage(char * usage)492 static void timerlat_hist_usage(char *usage)
493 {
494 int i;
495
496 char *msg[] = {
497 "",
498 " usage: [rtla] timerlat hist [-h] [-q] [-d s] [-D] [-n] [-a us] [-p us] [-i us] [-T us] [-s us] \\",
499 " [-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\",
500 " [-P priority] [-E N] [-b N] [--no-irq] [--no-thread] [--no-header] [--no-summary] \\",
501 " [--no-index] [--with-zeros] [--dma-latency us] [-C[=cgroup_name]] [--no-aa] [--dump-task] [-u]",
502 "",
503 " -h/--help: print this menu",
504 " -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",
505 " -p/--period us: timerlat period in us",
506 " -i/--irq us: stop trace if the irq latency is higher than the argument in us",
507 " -T/--thread us: stop trace if the thread latency is higher than the argument in us",
508 " -s/--stack us: save the stack trace at the IRQ if a thread latency is higher than the argument in us",
509 " -c/--cpus cpus: run the tracer only on the given cpus",
510 " -H/--house-keeping cpus: run rtla control threads only on the given cpus",
511 " -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited",
512 " -d/--duration time[m|h|d]: duration of the session in seconds",
513 " --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)",
514 " -D/--debug: print debug info",
515 " -t/--trace[=file]: save the stopped trace to [file|timerlat_trace.txt]",
516 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
517 " --filter <filter>: enable a trace event filter to the previous -e event",
518 " --trigger <trigger>: enable a trace event trigger to the previous -e event",
519 " -n/--nano: display data in nanoseconds",
520 " --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage",
521 " -b/--bucket-size N: set the histogram bucket size (default 1)",
522 " -E/--entries N: set the number of entries of the histogram (default 256)",
523 " --no-irq: ignore IRQ latencies",
524 " --no-thread: ignore thread latencies",
525 " --no-header: do not print header",
526 " --no-summary: do not print summary",
527 " --no-index: do not print index",
528 " --with-zeros: print zero only entries",
529 " --dma-latency us: set /dev/cpu_dma_latency latency <us> to reduce exit from idle latency",
530 " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling parameters",
531 " o:prio - use SCHED_OTHER with prio",
532 " r:prio - use SCHED_RR with prio",
533 " f:prio - use SCHED_FIFO with prio",
534 " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime and period",
535 " in nanoseconds",
536 " -u/--user-threads: use rtla user-space threads instead of in-kernel timerlat threads",
537 NULL,
538 };
539
540 if (usage)
541 fprintf(stderr, "%s\n", usage);
542
543 fprintf(stderr, "rtla timerlat hist: a per-cpu histogram of the timer latency (version %s)\n",
544 VERSION);
545
546 for (i = 0; msg[i]; i++)
547 fprintf(stderr, "%s\n", msg[i]);
548
549 if (usage)
550 exit(EXIT_FAILURE);
551
552 exit(EXIT_SUCCESS);
553 }
554
555 /*
556 * timerlat_hist_parse_args - allocs, parse and fill the cmd line parameters
557 */
558 static struct timerlat_hist_params
timerlat_hist_parse_args(int argc,char * argv[])559 *timerlat_hist_parse_args(int argc, char *argv[])
560 {
561 struct timerlat_hist_params *params;
562 struct trace_events *tevent;
563 int auto_thresh;
564 int retval;
565 int c;
566
567 params = calloc(1, sizeof(*params));
568 if (!params)
569 exit(1);
570
571 /* disabled by default */
572 params->dma_latency = -1;
573
574 /* display data in microseconds */
575 params->output_divisor = 1000;
576 params->bucket_size = 1;
577 params->entries = 256;
578
579 while (1) {
580 static struct option long_options[] = {
581 {"auto", required_argument, 0, 'a'},
582 {"cpus", required_argument, 0, 'c'},
583 {"cgroup", optional_argument, 0, 'C'},
584 {"bucket-size", required_argument, 0, 'b'},
585 {"debug", no_argument, 0, 'D'},
586 {"entries", required_argument, 0, 'E'},
587 {"duration", required_argument, 0, 'd'},
588 {"house-keeping", required_argument, 0, 'H'},
589 {"help", no_argument, 0, 'h'},
590 {"irq", required_argument, 0, 'i'},
591 {"nano", no_argument, 0, 'n'},
592 {"period", required_argument, 0, 'p'},
593 {"priority", required_argument, 0, 'P'},
594 {"stack", required_argument, 0, 's'},
595 {"thread", required_argument, 0, 'T'},
596 {"trace", optional_argument, 0, 't'},
597 {"user-threads", no_argument, 0, 'u'},
598 {"event", required_argument, 0, 'e'},
599 {"no-irq", no_argument, 0, '0'},
600 {"no-thread", no_argument, 0, '1'},
601 {"no-header", no_argument, 0, '2'},
602 {"no-summary", no_argument, 0, '3'},
603 {"no-index", no_argument, 0, '4'},
604 {"with-zeros", no_argument, 0, '5'},
605 {"trigger", required_argument, 0, '6'},
606 {"filter", required_argument, 0, '7'},
607 {"dma-latency", required_argument, 0, '8'},
608 {"no-aa", no_argument, 0, '9'},
609 {"dump-task", no_argument, 0, '\1'},
610 {0, 0, 0, 0}
611 };
612
613 /* getopt_long stores the option index here. */
614 int option_index = 0;
615
616 c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:i:np:P:s:t::T:u0123456:7:8:9\1",
617 long_options, &option_index);
618
619 /* detect the end of the options. */
620 if (c == -1)
621 break;
622
623 switch (c) {
624 case 'a':
625 auto_thresh = get_llong_from_str(optarg);
626
627 /* set thread stop to auto_thresh */
628 params->stop_total_us = auto_thresh;
629 params->stop_us = auto_thresh;
630
631 /* get stack trace */
632 params->print_stack = auto_thresh;
633
634 /* set trace */
635 params->trace_output = "timerlat_trace.txt";
636
637 break;
638 case 'c':
639 retval = parse_cpu_set(optarg, ¶ms->monitored_cpus);
640 if (retval)
641 timerlat_hist_usage("\nInvalid -c cpu list\n");
642 params->cpus = optarg;
643 break;
644 case 'C':
645 params->cgroup = 1;
646 if (!optarg) {
647 /* will inherit this cgroup */
648 params->cgroup_name = NULL;
649 } else if (*optarg == '=') {
650 /* skip the = */
651 params->cgroup_name = ++optarg;
652 }
653 break;
654 case 'b':
655 params->bucket_size = get_llong_from_str(optarg);
656 if ((params->bucket_size == 0) || (params->bucket_size >= 1000000))
657 timerlat_hist_usage("Bucket size needs to be > 0 and <= 1000000\n");
658 break;
659 case 'D':
660 config_debug = 1;
661 break;
662 case 'd':
663 params->duration = parse_seconds_duration(optarg);
664 if (!params->duration)
665 timerlat_hist_usage("Invalid -D duration\n");
666 break;
667 case 'e':
668 tevent = trace_event_alloc(optarg);
669 if (!tevent) {
670 err_msg("Error alloc trace event");
671 exit(EXIT_FAILURE);
672 }
673
674 if (params->events)
675 tevent->next = params->events;
676
677 params->events = tevent;
678 break;
679 case 'E':
680 params->entries = get_llong_from_str(optarg);
681 if ((params->entries < 10) || (params->entries > 9999999))
682 timerlat_hist_usage("Entries must be > 10 and < 9999999\n");
683 break;
684 case 'h':
685 case '?':
686 timerlat_hist_usage(NULL);
687 break;
688 case 'H':
689 params->hk_cpus = 1;
690 retval = parse_cpu_set(optarg, ¶ms->hk_cpu_set);
691 if (retval) {
692 err_msg("Error parsing house keeping CPUs\n");
693 exit(EXIT_FAILURE);
694 }
695 break;
696 case 'i':
697 params->stop_us = get_llong_from_str(optarg);
698 break;
699 case 'n':
700 params->output_divisor = 1;
701 break;
702 case 'p':
703 params->timerlat_period_us = get_llong_from_str(optarg);
704 if (params->timerlat_period_us > 1000000)
705 timerlat_hist_usage("Period longer than 1 s\n");
706 break;
707 case 'P':
708 retval = parse_prio(optarg, ¶ms->sched_param);
709 if (retval == -1)
710 timerlat_hist_usage("Invalid -P priority");
711 params->set_sched = 1;
712 break;
713 case 's':
714 params->print_stack = get_llong_from_str(optarg);
715 break;
716 case 'T':
717 params->stop_total_us = get_llong_from_str(optarg);
718 break;
719 case 't':
720 if (optarg)
721 /* skip = */
722 params->trace_output = &optarg[1];
723 else
724 params->trace_output = "timerlat_trace.txt";
725 break;
726 case 'u':
727 params->user_hist = 1;
728 break;
729 case '0': /* no irq */
730 params->no_irq = 1;
731 break;
732 case '1': /* no thread */
733 params->no_thread = 1;
734 break;
735 case '2': /* no header */
736 params->no_header = 1;
737 break;
738 case '3': /* no summary */
739 params->no_summary = 1;
740 break;
741 case '4': /* no index */
742 params->no_index = 1;
743 break;
744 case '5': /* with zeros */
745 params->with_zeros = 1;
746 break;
747 case '6': /* trigger */
748 if (params->events) {
749 retval = trace_event_add_trigger(params->events, optarg);
750 if (retval) {
751 err_msg("Error adding trigger %s\n", optarg);
752 exit(EXIT_FAILURE);
753 }
754 } else {
755 timerlat_hist_usage("--trigger requires a previous -e\n");
756 }
757 break;
758 case '7': /* filter */
759 if (params->events) {
760 retval = trace_event_add_filter(params->events, optarg);
761 if (retval) {
762 err_msg("Error adding filter %s\n", optarg);
763 exit(EXIT_FAILURE);
764 }
765 } else {
766 timerlat_hist_usage("--filter requires a previous -e\n");
767 }
768 break;
769 case '8':
770 params->dma_latency = get_llong_from_str(optarg);
771 if (params->dma_latency < 0 || params->dma_latency > 10000) {
772 err_msg("--dma-latency needs to be >= 0 and < 10000");
773 exit(EXIT_FAILURE);
774 }
775 break;
776 case '9':
777 params->no_aa = 1;
778 break;
779 case '\1':
780 params->dump_tasks = 1;
781 break;
782 default:
783 timerlat_hist_usage("Invalid option");
784 }
785 }
786
787 if (geteuid()) {
788 err_msg("rtla needs root permission\n");
789 exit(EXIT_FAILURE);
790 }
791
792 if (params->no_irq && params->no_thread)
793 timerlat_hist_usage("no-irq and no-thread set, there is nothing to do here");
794
795 if (params->no_index && !params->with_zeros)
796 timerlat_hist_usage("no-index set with with-zeros is not set - it does not make sense");
797
798 /*
799 * Auto analysis only happens if stop tracing, thus:
800 */
801 if (!params->stop_us && !params->stop_total_us)
802 params->no_aa = 1;
803
804 return params;
805 }
806
807 /*
808 * timerlat_hist_apply_config - apply the hist configs to the initialized tool
809 */
810 static int
timerlat_hist_apply_config(struct osnoise_tool * tool,struct timerlat_hist_params * params)811 timerlat_hist_apply_config(struct osnoise_tool *tool, struct timerlat_hist_params *params)
812 {
813 int retval, i;
814
815 if (!params->sleep_time)
816 params->sleep_time = 1;
817
818 if (params->cpus) {
819 retval = osnoise_set_cpus(tool->context, params->cpus);
820 if (retval) {
821 err_msg("Failed to apply CPUs config\n");
822 goto out_err;
823 }
824 } else {
825 for (i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++)
826 CPU_SET(i, ¶ms->monitored_cpus);
827 }
828
829 if (params->stop_us) {
830 retval = osnoise_set_stop_us(tool->context, params->stop_us);
831 if (retval) {
832 err_msg("Failed to set stop us\n");
833 goto out_err;
834 }
835 }
836
837 if (params->stop_total_us) {
838 retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us);
839 if (retval) {
840 err_msg("Failed to set stop total us\n");
841 goto out_err;
842 }
843 }
844
845 if (params->timerlat_period_us) {
846 retval = osnoise_set_timerlat_period_us(tool->context, params->timerlat_period_us);
847 if (retval) {
848 err_msg("Failed to set timerlat period\n");
849 goto out_err;
850 }
851 }
852
853 if (params->print_stack) {
854 retval = osnoise_set_print_stack(tool->context, params->print_stack);
855 if (retval) {
856 err_msg("Failed to set print stack\n");
857 goto out_err;
858 }
859 }
860
861 if (params->hk_cpus) {
862 retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set),
863 ¶ms->hk_cpu_set);
864 if (retval == -1) {
865 err_msg("Failed to set rtla to the house keeping CPUs\n");
866 goto out_err;
867 }
868 } else if (params->cpus) {
869 /*
870 * Even if the user do not set a house-keeping CPU, try to
871 * move rtla to a CPU set different to the one where the user
872 * set the workload to run.
873 *
874 * No need to check results as this is an automatic attempt.
875 */
876 auto_house_keeping(¶ms->monitored_cpus);
877 }
878
879 if (params->user_hist) {
880 retval = osnoise_set_workload(tool->context, 0);
881 if (retval) {
882 err_msg("Failed to set OSNOISE_WORKLOAD option\n");
883 goto out_err;
884 }
885 }
886
887 return 0;
888
889 out_err:
890 return -1;
891 }
892
893 /*
894 * timerlat_init_hist - initialize a timerlat hist tool with parameters
895 */
896 static struct osnoise_tool
timerlat_init_hist(struct timerlat_hist_params * params)897 *timerlat_init_hist(struct timerlat_hist_params *params)
898 {
899 struct osnoise_tool *tool;
900 int nr_cpus;
901
902 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
903
904 tool = osnoise_init_tool("timerlat_hist");
905 if (!tool)
906 return NULL;
907
908 tool->data = timerlat_alloc_histogram(nr_cpus, params->entries, params->bucket_size);
909 if (!tool->data)
910 goto out_err;
911
912 tool->params = params;
913
914 tep_register_event_handler(tool->trace.tep, -1, "ftrace", "timerlat",
915 timerlat_hist_handler, tool);
916
917 return tool;
918
919 out_err:
920 osnoise_destroy_tool(tool);
921 return NULL;
922 }
923
924 static int stop_tracing;
stop_hist(int sig)925 static void stop_hist(int sig)
926 {
927 stop_tracing = 1;
928 }
929
930 /*
931 * timerlat_hist_set_signals - handles the signal to stop the tool
932 */
933 static void
timerlat_hist_set_signals(struct timerlat_hist_params * params)934 timerlat_hist_set_signals(struct timerlat_hist_params *params)
935 {
936 signal(SIGINT, stop_hist);
937 if (params->duration) {
938 signal(SIGALRM, stop_hist);
939 alarm(params->duration);
940 }
941 }
942
timerlat_hist_main(int argc,char * argv[])943 int timerlat_hist_main(int argc, char *argv[])
944 {
945 struct timerlat_hist_params *params;
946 struct osnoise_tool *record = NULL;
947 struct timerlat_u_params params_u;
948 struct osnoise_tool *tool = NULL;
949 struct osnoise_tool *aa = NULL;
950 struct trace_instance *trace;
951 int dma_latency_fd = -1;
952 int return_value = 1;
953 pthread_t timerlat_u;
954 int retval;
955
956 params = timerlat_hist_parse_args(argc, argv);
957 if (!params)
958 exit(1);
959
960 tool = timerlat_init_hist(params);
961 if (!tool) {
962 err_msg("Could not init osnoise hist\n");
963 goto out_exit;
964 }
965
966 retval = timerlat_hist_apply_config(tool, params);
967 if (retval) {
968 err_msg("Could not apply config\n");
969 goto out_free;
970 }
971
972 trace = &tool->trace;
973
974 retval = enable_timerlat(trace);
975 if (retval) {
976 err_msg("Failed to enable timerlat tracer\n");
977 goto out_free;
978 }
979
980 if (params->set_sched) {
981 retval = set_comm_sched_attr("timerlat/", ¶ms->sched_param);
982 if (retval) {
983 err_msg("Failed to set sched parameters\n");
984 goto out_free;
985 }
986 }
987
988 if (params->cgroup && !params->user_hist) {
989 retval = set_comm_cgroup("timerlat/", params->cgroup_name);
990 if (!retval) {
991 err_msg("Failed to move threads to cgroup\n");
992 goto out_free;
993 }
994 }
995
996 if (params->dma_latency >= 0) {
997 dma_latency_fd = set_cpu_dma_latency(params->dma_latency);
998 if (dma_latency_fd < 0) {
999 err_msg("Could not set /dev/cpu_dma_latency.\n");
1000 goto out_free;
1001 }
1002 }
1003
1004 if (params->trace_output) {
1005 record = osnoise_init_trace_tool("timerlat");
1006 if (!record) {
1007 err_msg("Failed to enable the trace instance\n");
1008 goto out_free;
1009 }
1010
1011 if (params->events) {
1012 retval = trace_events_enable(&record->trace, params->events);
1013 if (retval)
1014 goto out_hist;
1015 }
1016 }
1017
1018 if (!params->no_aa) {
1019 aa = osnoise_init_tool("timerlat_aa");
1020 if (!aa)
1021 goto out_hist;
1022
1023 retval = timerlat_aa_init(aa, params->dump_tasks);
1024 if (retval) {
1025 err_msg("Failed to enable the auto analysis instance\n");
1026 goto out_hist;
1027 }
1028
1029 retval = enable_timerlat(&aa->trace);
1030 if (retval) {
1031 err_msg("Failed to enable timerlat tracer\n");
1032 goto out_hist;
1033 }
1034 }
1035
1036 /*
1037 * Start the tracers here, after having set all instances.
1038 *
1039 * Let the trace instance start first for the case of hitting a stop
1040 * tracing while enabling other instances. The trace instance is the
1041 * one with most valuable information.
1042 */
1043 if (params->trace_output)
1044 trace_instance_start(&record->trace);
1045 if (!params->no_aa)
1046 trace_instance_start(&aa->trace);
1047 trace_instance_start(trace);
1048
1049 tool->start_time = time(NULL);
1050 timerlat_hist_set_signals(params);
1051
1052 if (params->user_hist) {
1053 /* rtla asked to stop */
1054 params_u.should_run = 1;
1055 /* all threads left */
1056 params_u.stopped_running = 0;
1057
1058 params_u.set = ¶ms->monitored_cpus;
1059 if (params->set_sched)
1060 params_u.sched_param = ¶ms->sched_param;
1061 else
1062 params_u.sched_param = NULL;
1063
1064 params_u.cgroup_name = params->cgroup_name;
1065
1066 retval = pthread_create(&timerlat_u, NULL, timerlat_u_dispatcher, ¶ms_u);
1067 if (retval)
1068 err_msg("Error creating timerlat user-space threads\n");
1069 }
1070
1071 while (!stop_tracing) {
1072 sleep(params->sleep_time);
1073
1074 retval = tracefs_iterate_raw_events(trace->tep,
1075 trace->inst,
1076 NULL,
1077 0,
1078 collect_registered_events,
1079 trace);
1080 if (retval < 0) {
1081 err_msg("Error iterating on events\n");
1082 goto out_hist;
1083 }
1084
1085 if (trace_is_off(&tool->trace, &record->trace))
1086 break;
1087
1088 /* is there still any user-threads ? */
1089 if (params->user_hist) {
1090 if (params_u.stopped_running) {
1091 debug_msg("timerlat user-space threads stopped!\n");
1092 break;
1093 }
1094 }
1095 }
1096 if (params->user_hist && !params_u.stopped_running) {
1097 params_u.should_run = 0;
1098 sleep(1);
1099 }
1100
1101 timerlat_print_stats(params, tool);
1102
1103 return_value = 0;
1104
1105 if (trace_is_off(&tool->trace, &record->trace)) {
1106 printf("rtla timerlat hit stop tracing\n");
1107
1108 if (!params->no_aa)
1109 timerlat_auto_analysis(params->stop_us, params->stop_total_us);
1110
1111 if (params->trace_output) {
1112 printf(" Saving trace to %s\n", params->trace_output);
1113 save_trace_to_file(record->trace.inst, params->trace_output);
1114 }
1115 }
1116
1117 out_hist:
1118 timerlat_aa_destroy();
1119 if (dma_latency_fd >= 0)
1120 close(dma_latency_fd);
1121 trace_events_destroy(&record->trace, params->events);
1122 params->events = NULL;
1123 out_free:
1124 timerlat_free_histogram(tool->data);
1125 osnoise_destroy_tool(aa);
1126 osnoise_destroy_tool(record);
1127 osnoise_destroy_tool(tool);
1128 free(params);
1129 out_exit:
1130 exit(return_value);
1131 }
1132