1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2023 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
16 TIMERLAT_WAITING_THREAD,
22 * Per-cpu data statistics and data.
24 struct timerlat_aa_data {
25 /* Current CPU state */
28 /* timerlat IRQ latency */
29 unsigned long long tlat_irq_seqnum;
30 unsigned long long tlat_irq_latency;
31 unsigned long long tlat_irq_timstamp;
33 /* timerlat Thread latency */
34 unsigned long long tlat_thread_seqnum;
35 unsigned long long tlat_thread_latency;
36 unsigned long long tlat_thread_timstamp;
39 * Information about the thread running when the IRQ
42 * This can be blocking or interference, depending on the
43 * priority of the thread. Assuming timerlat is the highest
44 * prio, it is blocking. If timerlat has a lower prio, it is
46 * note: "unsigned long long" because they are fetch using tep_get_field_val();
48 unsigned long long run_thread_pid;
49 char run_thread_comm[MAX_COMM];
50 unsigned long long thread_blocking_duration;
51 unsigned long long max_exit_idle_latency;
53 /* Information about the timerlat timer irq */
54 unsigned long long timer_irq_start_time;
55 unsigned long long timer_irq_start_delay;
56 unsigned long long timer_irq_duration;
57 unsigned long long timer_exit_from_idle;
60 * Information about the last IRQ before the timerlat irq
63 * If now - timestamp is <= latency, it might have influenced
64 * in the timerlat irq latency. Otherwise, ignore it.
66 unsigned long long prev_irq_duration;
67 unsigned long long prev_irq_timstamp;
72 unsigned long long thread_nmi_sum;
73 unsigned long long thread_irq_sum;
74 unsigned long long thread_softirq_sum;
75 unsigned long long thread_thread_sum;
78 * Interference task information.
80 struct trace_seq *prev_irqs_seq;
81 struct trace_seq *nmi_seq;
82 struct trace_seq *irqs_seq;
83 struct trace_seq *softirqs_seq;
84 struct trace_seq *threads_seq;
85 struct trace_seq *stack_seq;
90 char current_comm[MAX_COMM];
91 unsigned long long current_pid;
94 * Is the system running a kworker?
96 unsigned long long kworker;
97 unsigned long long kworker_func;
101 * The analysis context and system wide view
103 struct timerlat_aa_context {
108 struct timerlat_aa_data *taa_data;
111 * required to translate function names and register
114 struct osnoise_tool *tool;
118 * The data is stored as a local variable, but accessed via a helper function.
120 * It could be stored inside the trace context. But every access would
121 * require container_of() + a series of pointers. Do we need it? Not sure.
123 * For now keep it simple. If needed, store it in the tool, add the *context
124 * as a parameter in timerlat_aa_get_ctx() and do the magic there.
126 static struct timerlat_aa_context *__timerlat_aa_ctx;
128 static struct timerlat_aa_context *timerlat_aa_get_ctx(void)
130 return __timerlat_aa_ctx;
134 * timerlat_aa_get_data - Get the per-cpu data from the timerlat context
136 static struct timerlat_aa_data
137 *timerlat_aa_get_data(struct timerlat_aa_context *taa_ctx, int cpu)
139 return &taa_ctx->taa_data[cpu];
143 * timerlat_aa_irq_latency - Handles timerlat IRQ event
145 static int timerlat_aa_irq_latency(struct timerlat_aa_data *taa_data,
146 struct trace_seq *s, struct tep_record *record,
147 struct tep_event *event)
150 * For interference, we start now looking for things that can delay
153 taa_data->curr_state = TIMERLAT_WAITING_THREAD;
154 taa_data->tlat_irq_timstamp = record->ts;
159 taa_data->thread_nmi_sum = 0;
160 taa_data->thread_irq_sum = 0;
161 taa_data->thread_softirq_sum = 0;
162 taa_data->thread_thread_sum = 0;
163 taa_data->thread_blocking_duration = 0;
164 taa_data->timer_irq_start_time = 0;
165 taa_data->timer_irq_duration = 0;
166 taa_data->timer_exit_from_idle = 0;
169 * Zero interference tasks.
171 trace_seq_reset(taa_data->nmi_seq);
172 trace_seq_reset(taa_data->irqs_seq);
173 trace_seq_reset(taa_data->softirqs_seq);
174 trace_seq_reset(taa_data->threads_seq);
176 /* IRQ latency values */
177 tep_get_field_val(s, event, "timer_latency", record, &taa_data->tlat_irq_latency, 1);
178 tep_get_field_val(s, event, "seqnum", record, &taa_data->tlat_irq_seqnum, 1);
180 /* The thread that can cause blocking */
181 tep_get_common_field_val(s, event, "common_pid", record, &taa_data->run_thread_pid, 1);
184 * Get exit from idle case.
186 * If it is not idle thread:
188 if (taa_data->run_thread_pid)
192 * if the latency is shorter than the known exit from idle:
194 if (taa_data->tlat_irq_latency < taa_data->max_exit_idle_latency)
198 * To be safe, ignore the cases in which an IRQ/NMI could have
199 * interfered with the timerlat IRQ.
201 if (taa_data->tlat_irq_timstamp - taa_data->tlat_irq_latency
202 < taa_data->prev_irq_timstamp + taa_data->prev_irq_duration)
205 taa_data->max_exit_idle_latency = taa_data->tlat_irq_latency;
211 * timerlat_aa_thread_latency - Handles timerlat thread event
213 static int timerlat_aa_thread_latency(struct timerlat_aa_data *taa_data,
214 struct trace_seq *s, struct tep_record *record,
215 struct tep_event *event)
218 * For interference, we start now looking for things that can delay
219 * the IRQ of the next cycle.
221 taa_data->curr_state = TIMERLAT_WAITING_IRQ;
222 taa_data->tlat_thread_timstamp = record->ts;
224 /* Thread latency values */
225 tep_get_field_val(s, event, "timer_latency", record, &taa_data->tlat_thread_latency, 1);
226 tep_get_field_val(s, event, "seqnum", record, &taa_data->tlat_thread_seqnum, 1);
232 * timerlat_aa_handler - Handle timerlat events
234 * This function is called to handle timerlat events recording statistics.
236 * Returns 0 on success, -1 otherwise.
238 static int timerlat_aa_handler(struct trace_seq *s, struct tep_record *record,
239 struct tep_event *event, void *context)
241 struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx();
242 struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu);
243 unsigned long long thread;
248 tep_get_field_val(s, event, "context", record, &thread, 1);
250 return timerlat_aa_irq_latency(taa_data, s, record, event);
252 return timerlat_aa_thread_latency(taa_data, s, record, event);
256 * timerlat_aa_nmi_handler - Handles NMI noise
258 * It is used to collect information about interferences from NMI. It is
259 * hooked to the osnoise:nmi_noise event.
261 static int timerlat_aa_nmi_handler(struct trace_seq *s, struct tep_record *record,
262 struct tep_event *event, void *context)
264 struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx();
265 struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu);
266 unsigned long long duration;
267 unsigned long long start;
269 tep_get_field_val(s, event, "duration", record, &duration, 1);
270 tep_get_field_val(s, event, "start", record, &start, 1);
272 if (taa_data->curr_state == TIMERLAT_WAITING_IRQ) {
273 taa_data->prev_irq_duration = duration;
274 taa_data->prev_irq_timstamp = start;
276 trace_seq_reset(taa_data->prev_irqs_seq);
277 trace_seq_printf(taa_data->prev_irqs_seq, "\t%24s \t\t\t%9.2f us\n",
278 "nmi", ns_to_usf(duration));
282 taa_data->thread_nmi_sum += duration;
283 trace_seq_printf(taa_data->nmi_seq, " %24s \t\t\t%9.2f us\n",
284 "nmi", ns_to_usf(duration));
290 * timerlat_aa_irq_handler - Handles IRQ noise
292 * It is used to collect information about interferences from IRQ. It is
293 * hooked to the osnoise:irq_noise event.
295 * It is a little bit more complex than the other because it measures:
296 * - The IRQs that can delay the timer IRQ before it happened.
297 * - The Timerlat IRQ handler
298 * - The IRQs that happened between the timerlat IRQ and the timerlat thread
299 * (IRQ interference).
301 static int timerlat_aa_irq_handler(struct trace_seq *s, struct tep_record *record,
302 struct tep_event *event, void *context)
304 struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx();
305 struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu);
306 unsigned long long expected_start;
307 unsigned long long duration;
308 unsigned long long vector;
309 unsigned long long start;
313 tep_get_field_val(s, event, "duration", record, &duration, 1);
314 tep_get_field_val(s, event, "start", record, &start, 1);
315 tep_get_field_val(s, event, "vector", record, &vector, 1);
316 desc = tep_get_field_raw(s, event, "desc", record, &val, 1);
319 * Before the timerlat IRQ.
321 if (taa_data->curr_state == TIMERLAT_WAITING_IRQ) {
322 taa_data->prev_irq_duration = duration;
323 taa_data->prev_irq_timstamp = start;
325 trace_seq_reset(taa_data->prev_irqs_seq);
326 trace_seq_printf(taa_data->prev_irqs_seq, "\t%24s:%-3llu \t\t%9.2f us\n",
327 desc, vector, ns_to_usf(duration));
332 * The timerlat IRQ: taa_data->timer_irq_start_time is zeroed at
333 * the timerlat irq handler.
335 if (!taa_data->timer_irq_start_time) {
336 expected_start = taa_data->tlat_irq_timstamp - taa_data->tlat_irq_latency;
338 taa_data->timer_irq_start_time = start;
339 taa_data->timer_irq_duration = duration;
342 * We are dealing with two different clock sources: the
343 * external clock source that timerlat uses as a reference
344 * and the clock used by the tracer. There are also two
345 * moments: the time reading the clock and the timer in
346 * which the event is placed in the buffer (the trace
347 * event timestamp). If the processor is slow or there
348 * is some hardware noise, the difference between the
349 * timestamp and the external clock read can be longer
350 * than the IRQ handler delay, resulting in a negative
351 * time. If so, set IRQ start delay as 0. In the end,
352 * it is less relevant than the noise.
354 if (expected_start < taa_data->timer_irq_start_time)
355 taa_data->timer_irq_start_delay = taa_data->timer_irq_start_time - expected_start;
357 taa_data->timer_irq_start_delay = 0;
360 * not exit from idle.
362 if (taa_data->run_thread_pid)
365 if (expected_start > taa_data->prev_irq_timstamp + taa_data->prev_irq_duration)
366 taa_data->timer_exit_from_idle = taa_data->timer_irq_start_delay;
374 taa_data->thread_irq_sum += duration;
375 trace_seq_printf(taa_data->irqs_seq, " %24s:%-3llu \t %9.2f us\n",
376 desc, vector, ns_to_usf(duration));
381 static char *softirq_name[] = { "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK",
382 "IRQ_POLL", "TASKLET", "SCHED", "HRTIMER", "RCU" };
386 * timerlat_aa_softirq_handler - Handles Softirq noise
388 * It is used to collect information about interferences from Softirq. It is
389 * hooked to the osnoise:softirq_noise event.
391 * It is only printed in the non-rt kernel, as softirqs become thread on RT.
393 static int timerlat_aa_softirq_handler(struct trace_seq *s, struct tep_record *record,
394 struct tep_event *event, void *context)
396 struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx();
397 struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu);
398 unsigned long long duration;
399 unsigned long long vector;
400 unsigned long long start;
402 if (taa_data->curr_state == TIMERLAT_WAITING_IRQ)
405 tep_get_field_val(s, event, "duration", record, &duration, 1);
406 tep_get_field_val(s, event, "start", record, &start, 1);
407 tep_get_field_val(s, event, "vector", record, &vector, 1);
409 taa_data->thread_softirq_sum += duration;
411 trace_seq_printf(taa_data->softirqs_seq, "\t%24s:%-3llu \t %9.2f us\n",
412 softirq_name[vector], vector, ns_to_usf(duration));
417 * timerlat_aa_softirq_handler - Handles thread noise
419 * It is used to collect information about interferences from threads. It is
420 * hooked to the osnoise:thread_noise event.
422 * Note: if you see thread noise, your timerlat thread was not the highest prio one.
424 static int timerlat_aa_thread_handler(struct trace_seq *s, struct tep_record *record,
425 struct tep_event *event, void *context)
427 struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx();
428 struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu);
429 unsigned long long duration;
430 unsigned long long start;
431 unsigned long long pid;
435 if (taa_data->curr_state == TIMERLAT_WAITING_IRQ)
438 tep_get_field_val(s, event, "duration", record, &duration, 1);
439 tep_get_field_val(s, event, "start", record, &start, 1);
441 tep_get_common_field_val(s, event, "common_pid", record, &pid, 1);
442 comm = tep_get_field_raw(s, event, "comm", record, &val, 1);
444 if (pid == taa_data->run_thread_pid && !taa_data->thread_blocking_duration) {
445 taa_data->thread_blocking_duration = duration;
448 strncpy(taa_data->run_thread_comm, comm, MAX_COMM);
450 sprintf(taa_data->run_thread_comm, "<...>");
453 taa_data->thread_thread_sum += duration;
455 trace_seq_printf(taa_data->threads_seq, "\t%24s:%-3llu \t\t%9.2f us\n",
456 comm, pid, ns_to_usf(duration));
463 * timerlat_aa_stack_handler - Handles timerlat IRQ stack trace
465 * Saves and parse the stack trace generated by the timerlat IRQ.
467 static int timerlat_aa_stack_handler(struct trace_seq *s, struct tep_record *record,
468 struct tep_event *event, void *context)
470 struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx();
471 struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu);
472 unsigned long *caller;
473 const char *function;
476 trace_seq_reset(taa_data->stack_seq);
478 trace_seq_printf(taa_data->stack_seq, " Blocking thread stack trace\n");
479 caller = tep_get_field_raw(s, event, "caller", record, &val, 1);
482 function = tep_find_function(taa_ctx->tool->trace.tep, caller[i]);
485 trace_seq_printf(taa_data->stack_seq, "\t\t-> %s\n", function);
492 * timerlat_aa_sched_switch_handler - Tracks the current thread running on the CPU
494 * Handles the sched:sched_switch event to trace the current thread running on the
495 * CPU. It is used to display the threads running on the other CPUs when the trace
498 static int timerlat_aa_sched_switch_handler(struct trace_seq *s, struct tep_record *record,
499 struct tep_event *event, void *context)
501 struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx();
502 struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu);
506 tep_get_field_val(s, event, "next_pid", record, &taa_data->current_pid, 1);
507 comm = tep_get_field_raw(s, event, "next_comm", record, &val, 1);
509 strncpy(taa_data->current_comm, comm, MAX_COMM);
512 * If this was a kworker, clean the last kworkers that ran.
514 taa_data->kworker = 0;
515 taa_data->kworker_func = 0;
521 * timerlat_aa_kworker_start_handler - Tracks a kworker running on the CPU
523 * Handles workqueue:workqueue_execute_start event, keeping track of
524 * the job that a kworker could be doing in the CPU.
526 * We already catch problems of hardware related latencies caused by work queues
527 * running driver code that causes hardware stall. For example, with DRM drivers.
529 static int timerlat_aa_kworker_start_handler(struct trace_seq *s, struct tep_record *record,
530 struct tep_event *event, void *context)
532 struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx();
533 struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu);
535 tep_get_field_val(s, event, "work", record, &taa_data->kworker, 1);
536 tep_get_field_val(s, event, "function", record, &taa_data->kworker_func, 1);
541 * timerlat_thread_analysis - Prints the analysis of a CPU that hit a stop tracing
543 * This is the core of the analysis.
545 static void timerlat_thread_analysis(struct timerlat_aa_data *taa_data, int cpu,
546 int irq_thresh, int thread_thresh)
548 long long exp_irq_ts;
553 * IRQ latency or Thread latency?
555 if (taa_data->tlat_irq_seqnum > taa_data->tlat_thread_seqnum) {
557 total = taa_data->tlat_irq_latency;
560 total = taa_data->tlat_thread_latency;
564 * Expected IRQ arrival time using the trace clock as the base.
566 * TODO: Add a list of previous IRQ, and then run the list backwards.
568 exp_irq_ts = taa_data->timer_irq_start_time - taa_data->timer_irq_start_delay;
569 if (exp_irq_ts < taa_data->prev_irq_timstamp + taa_data->prev_irq_duration) {
570 if (taa_data->prev_irq_timstamp < taa_data->timer_irq_start_time)
571 printf(" Previous IRQ interference: \t\t up to %9.2f us\n",
572 ns_to_usf(taa_data->prev_irq_duration));
576 * The delay that the IRQ suffered before starting.
578 printf(" IRQ handler delay: %16s %9.2f us (%.2f %%)\n",
579 (ns_to_usf(taa_data->timer_exit_from_idle) > 10) ? "(exit from idle)" : "",
580 ns_to_usf(taa_data->timer_irq_start_delay),
581 ns_to_per(total, taa_data->timer_irq_start_delay));
586 printf(" IRQ latency: \t\t\t\t %9.2f us\n",
587 ns_to_usf(taa_data->tlat_irq_latency));
591 * If the trace stopped due to IRQ, the other events will not happen
592 * because... the trace stopped :-).
594 * That is all folks, the stack trace was printed before the stop,
595 * so it will be displayed, it is the key.
597 printf(" Blocking thread:\n");
598 printf(" %24s:%-9llu\n",
599 taa_data->run_thread_comm, taa_data->run_thread_pid);
602 * The duration of the IRQ handler that handled the timerlat IRQ.
604 printf(" Timerlat IRQ duration: \t\t %9.2f us (%.2f %%)\n",
605 ns_to_usf(taa_data->timer_irq_duration),
606 ns_to_per(total, taa_data->timer_irq_duration));
609 * The amount of time that the current thread postponed the scheduler.
611 * Recalling that it is net from NMI/IRQ/Softirq interference, so there
612 * is no need to compute values here.
614 printf(" Blocking thread: \t\t\t %9.2f us (%.2f %%)\n",
615 ns_to_usf(taa_data->thread_blocking_duration),
616 ns_to_per(total, taa_data->thread_blocking_duration));
618 printf(" %24s:%-9llu %9.2f us\n",
619 taa_data->run_thread_comm, taa_data->run_thread_pid,
620 ns_to_usf(taa_data->thread_blocking_duration));
624 * Print the stack trace!
626 trace_seq_do_printf(taa_data->stack_seq);
629 * NMIs can happen during the IRQ, so they are always possible.
631 if (taa_data->thread_nmi_sum)
632 printf(" NMI interference \t\t\t %9.2f us (%.2f %%)\n",
633 ns_to_usf(taa_data->thread_nmi_sum),
634 ns_to_per(total, taa_data->thread_nmi_sum));
637 * If it is an IRQ latency, the other factors can be skipped.
643 * Prints the interference caused by IRQs to the thread latency.
645 if (taa_data->thread_irq_sum) {
646 printf(" IRQ interference \t\t\t %9.2f us (%.2f %%)\n",
647 ns_to_usf(taa_data->thread_irq_sum),
648 ns_to_per(total, taa_data->thread_irq_sum));
650 trace_seq_do_printf(taa_data->irqs_seq);
654 * Prints the interference caused by Softirqs to the thread latency.
656 if (taa_data->thread_softirq_sum) {
657 printf(" Softirq interference \t\t\t %9.2f us (%.2f %%)\n",
658 ns_to_usf(taa_data->thread_softirq_sum),
659 ns_to_per(total, taa_data->thread_softirq_sum));
661 trace_seq_do_printf(taa_data->softirqs_seq);
665 * Prints the interference caused by other threads to the thread latency.
667 * If this happens, your timerlat is not the highest prio. OK, migration
668 * thread can happen. But otherwise, you are not measuring the "scheduling
669 * latency" only, and here is the difference from scheduling latency and
670 * timer handling latency.
672 if (taa_data->thread_thread_sum) {
673 printf(" Thread interference \t\t\t %9.2f us (%.2f %%)\n",
674 ns_to_usf(taa_data->thread_thread_sum),
675 ns_to_per(total, taa_data->thread_thread_sum));
677 trace_seq_do_printf(taa_data->threads_seq);
684 printf("------------------------------------------------------------------------\n");
685 printf(" %s latency: \t\t\t %9.2f us (100%%)\n", irq ? "IRQ" : "Thread",
689 static int timerlat_auto_analysis_collect_trace(struct timerlat_aa_context *taa_ctx)
691 struct trace_instance *trace = &taa_ctx->tool->trace;
694 retval = tracefs_iterate_raw_events(trace->tep,
698 collect_registered_events,
701 err_msg("Error iterating on events\n");
709 * timerlat_auto_analysis - Analyze the collected data
711 void timerlat_auto_analysis(int irq_thresh, int thread_thresh)
713 struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx();
714 unsigned long long max_exit_from_idle = 0;
715 struct timerlat_aa_data *taa_data;
716 int max_exit_from_idle_cpu;
717 struct tep_handle *tep;
720 timerlat_auto_analysis_collect_trace(taa_ctx);
722 /* bring stop tracing to the ns scale */
723 irq_thresh = irq_thresh * 1000;
724 thread_thresh = thread_thresh * 1000;
726 for (cpu = 0; cpu < taa_ctx->nr_cpus; cpu++) {
727 taa_data = timerlat_aa_get_data(taa_ctx, cpu);
729 if (irq_thresh && taa_data->tlat_irq_latency >= irq_thresh) {
730 printf("## CPU %d hit stop tracing, analyzing it ##\n", cpu);
731 timerlat_thread_analysis(taa_data, cpu, irq_thresh, thread_thresh);
732 } else if (thread_thresh && (taa_data->tlat_thread_latency) >= thread_thresh) {
733 printf("## CPU %d hit stop tracing, analyzing it ##\n", cpu);
734 timerlat_thread_analysis(taa_data, cpu, irq_thresh, thread_thresh);
737 if (taa_data->max_exit_idle_latency > max_exit_from_idle) {
738 max_exit_from_idle = taa_data->max_exit_idle_latency;
739 max_exit_from_idle_cpu = cpu;
744 if (max_exit_from_idle) {
746 printf("Max timerlat IRQ latency from idle: %.2f us in cpu %d\n",
747 ns_to_usf(max_exit_from_idle), max_exit_from_idle_cpu);
749 if (!taa_ctx->dump_tasks)
753 printf("Printing CPU tasks:\n");
754 for (cpu = 0; cpu < taa_ctx->nr_cpus; cpu++) {
755 taa_data = timerlat_aa_get_data(taa_ctx, cpu);
756 tep = taa_ctx->tool->trace.tep;
758 printf(" [%.3d] %24s:%llu", cpu, taa_data->current_comm, taa_data->current_pid);
760 if (taa_data->kworker_func)
761 printf(" kworker:%s:%s",
762 tep_find_function(tep, taa_data->kworker) ? : "<...>",
763 tep_find_function(tep, taa_data->kworker_func));
770 * timerlat_aa_destroy_seqs - Destroy seq files used to store parsed data
772 static void timerlat_aa_destroy_seqs(struct timerlat_aa_context *taa_ctx)
774 struct timerlat_aa_data *taa_data;
777 if (!taa_ctx->taa_data)
780 for (i = 0; i < taa_ctx->nr_cpus; i++) {
781 taa_data = timerlat_aa_get_data(taa_ctx, i);
783 if (taa_data->prev_irqs_seq) {
784 trace_seq_destroy(taa_data->prev_irqs_seq);
785 free(taa_data->prev_irqs_seq);
788 if (taa_data->nmi_seq) {
789 trace_seq_destroy(taa_data->nmi_seq);
790 free(taa_data->nmi_seq);
793 if (taa_data->irqs_seq) {
794 trace_seq_destroy(taa_data->irqs_seq);
795 free(taa_data->irqs_seq);
798 if (taa_data->softirqs_seq) {
799 trace_seq_destroy(taa_data->softirqs_seq);
800 free(taa_data->softirqs_seq);
803 if (taa_data->threads_seq) {
804 trace_seq_destroy(taa_data->threads_seq);
805 free(taa_data->threads_seq);
808 if (taa_data->stack_seq) {
809 trace_seq_destroy(taa_data->stack_seq);
810 free(taa_data->stack_seq);
816 * timerlat_aa_init_seqs - Init seq files used to store parsed information
818 * Instead of keeping data structures to store raw data, use seq files to
821 * Allocates and initialize seq files.
823 * Returns 0 on success, -1 otherwise.
825 static int timerlat_aa_init_seqs(struct timerlat_aa_context *taa_ctx)
827 struct timerlat_aa_data *taa_data;
830 for (i = 0; i < taa_ctx->nr_cpus; i++) {
832 taa_data = timerlat_aa_get_data(taa_ctx, i);
834 taa_data->prev_irqs_seq = calloc(1, sizeof(*taa_data->prev_irqs_seq));
835 if (!taa_data->prev_irqs_seq)
838 trace_seq_init(taa_data->prev_irqs_seq);
840 taa_data->nmi_seq = calloc(1, sizeof(*taa_data->nmi_seq));
841 if (!taa_data->nmi_seq)
844 trace_seq_init(taa_data->nmi_seq);
846 taa_data->irqs_seq = calloc(1, sizeof(*taa_data->irqs_seq));
847 if (!taa_data->irqs_seq)
850 trace_seq_init(taa_data->irqs_seq);
852 taa_data->softirqs_seq = calloc(1, sizeof(*taa_data->softirqs_seq));
853 if (!taa_data->softirqs_seq)
856 trace_seq_init(taa_data->softirqs_seq);
858 taa_data->threads_seq = calloc(1, sizeof(*taa_data->threads_seq));
859 if (!taa_data->threads_seq)
862 trace_seq_init(taa_data->threads_seq);
864 taa_data->stack_seq = calloc(1, sizeof(*taa_data->stack_seq));
865 if (!taa_data->stack_seq)
868 trace_seq_init(taa_data->stack_seq);
874 timerlat_aa_destroy_seqs(taa_ctx);
879 * timerlat_aa_unregister_events - Unregister events used in the auto-analysis
881 static void timerlat_aa_unregister_events(struct osnoise_tool *tool, int dump_tasks)
884 tep_unregister_event_handler(tool->trace.tep, -1, "ftrace", "timerlat",
885 timerlat_aa_handler, tool);
887 tracefs_event_disable(tool->trace.inst, "osnoise", NULL);
889 tep_unregister_event_handler(tool->trace.tep, -1, "osnoise", "nmi_noise",
890 timerlat_aa_nmi_handler, tool);
892 tep_unregister_event_handler(tool->trace.tep, -1, "osnoise", "irq_noise",
893 timerlat_aa_irq_handler, tool);
895 tep_unregister_event_handler(tool->trace.tep, -1, "osnoise", "softirq_noise",
896 timerlat_aa_softirq_handler, tool);
898 tep_unregister_event_handler(tool->trace.tep, -1, "osnoise", "thread_noise",
899 timerlat_aa_thread_handler, tool);
901 tep_unregister_event_handler(tool->trace.tep, -1, "ftrace", "kernel_stack",
902 timerlat_aa_stack_handler, tool);
906 tracefs_event_disable(tool->trace.inst, "sched", "sched_switch");
907 tep_unregister_event_handler(tool->trace.tep, -1, "sched", "sched_switch",
908 timerlat_aa_sched_switch_handler, tool);
910 tracefs_event_disable(tool->trace.inst, "workqueue", "workqueue_execute_start");
911 tep_unregister_event_handler(tool->trace.tep, -1, "workqueue", "workqueue_execute_start",
912 timerlat_aa_kworker_start_handler, tool);
916 * timerlat_aa_register_events - Register events used in the auto-analysis
918 * Returns 0 on success, -1 otherwise.
920 static int timerlat_aa_register_events(struct osnoise_tool *tool, int dump_tasks)
924 tep_register_event_handler(tool->trace.tep, -1, "ftrace", "timerlat",
925 timerlat_aa_handler, tool);
929 * register auto-analysis handlers.
931 retval = tracefs_event_enable(tool->trace.inst, "osnoise", NULL);
932 if (retval < 0 && !errno) {
933 err_msg("Could not find osnoise events\n");
937 tep_register_event_handler(tool->trace.tep, -1, "osnoise", "nmi_noise",
938 timerlat_aa_nmi_handler, tool);
940 tep_register_event_handler(tool->trace.tep, -1, "osnoise", "irq_noise",
941 timerlat_aa_irq_handler, tool);
943 tep_register_event_handler(tool->trace.tep, -1, "osnoise", "softirq_noise",
944 timerlat_aa_softirq_handler, tool);
946 tep_register_event_handler(tool->trace.tep, -1, "osnoise", "thread_noise",
947 timerlat_aa_thread_handler, tool);
949 tep_register_event_handler(tool->trace.tep, -1, "ftrace", "kernel_stack",
950 timerlat_aa_stack_handler, tool);
958 retval = tracefs_event_enable(tool->trace.inst, "sched", "sched_switch");
959 if (retval < 0 && !errno) {
960 err_msg("Could not find sched_switch\n");
964 tep_register_event_handler(tool->trace.tep, -1, "sched", "sched_switch",
965 timerlat_aa_sched_switch_handler, tool);
967 retval = tracefs_event_enable(tool->trace.inst, "workqueue", "workqueue_execute_start");
968 if (retval < 0 && !errno) {
969 err_msg("Could not find workqueue_execute_start\n");
973 tep_register_event_handler(tool->trace.tep, -1, "workqueue", "workqueue_execute_start",
974 timerlat_aa_kworker_start_handler, tool);
979 timerlat_aa_unregister_events(tool, dump_tasks);
984 * timerlat_aa_destroy - Destroy timerlat auto-analysis
986 void timerlat_aa_destroy(void)
988 struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx();
993 if (!taa_ctx->taa_data)
996 timerlat_aa_unregister_events(taa_ctx->tool, taa_ctx->dump_tasks);
997 timerlat_aa_destroy_seqs(taa_ctx);
998 free(taa_ctx->taa_data);
1004 * timerlat_aa_init - Initialize timerlat auto-analysis
1006 * Returns 0 on success, -1 otherwise.
1008 int timerlat_aa_init(struct osnoise_tool *tool, int dump_tasks)
1010 int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
1011 struct timerlat_aa_context *taa_ctx;
1014 taa_ctx = calloc(1, sizeof(*taa_ctx));
1018 __timerlat_aa_ctx = taa_ctx;
1020 taa_ctx->nr_cpus = nr_cpus;
1021 taa_ctx->tool = tool;
1022 taa_ctx->dump_tasks = dump_tasks;
1024 taa_ctx->taa_data = calloc(nr_cpus, sizeof(*taa_ctx->taa_data));
1025 if (!taa_ctx->taa_data)
1028 retval = timerlat_aa_init_seqs(taa_ctx);
1032 retval = timerlat_aa_register_events(tool, dump_tasks);
1039 timerlat_aa_destroy();