162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2023 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org> 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci#include <stdlib.h> 762306a36Sopenharmony_ci#include <errno.h> 862306a36Sopenharmony_ci#include "utils.h" 962306a36Sopenharmony_ci#include "osnoise.h" 1062306a36Sopenharmony_ci#include "timerlat.h" 1162306a36Sopenharmony_ci#include <unistd.h> 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_cienum timelat_state { 1462306a36Sopenharmony_ci TIMERLAT_INIT = 0, 1562306a36Sopenharmony_ci TIMERLAT_WAITING_IRQ, 1662306a36Sopenharmony_ci TIMERLAT_WAITING_THREAD, 1762306a36Sopenharmony_ci}; 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#define MAX_COMM 24 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci/* 2262306a36Sopenharmony_ci * Per-cpu data statistics and data. 2362306a36Sopenharmony_ci */ 2462306a36Sopenharmony_cistruct timerlat_aa_data { 2562306a36Sopenharmony_ci /* Current CPU state */ 2662306a36Sopenharmony_ci int curr_state; 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci /* timerlat IRQ latency */ 2962306a36Sopenharmony_ci unsigned long long tlat_irq_seqnum; 3062306a36Sopenharmony_ci unsigned long long tlat_irq_latency; 3162306a36Sopenharmony_ci unsigned long long tlat_irq_timstamp; 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci /* timerlat Thread latency */ 3462306a36Sopenharmony_ci unsigned long long tlat_thread_seqnum; 3562306a36Sopenharmony_ci unsigned long long tlat_thread_latency; 3662306a36Sopenharmony_ci unsigned long long tlat_thread_timstamp; 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci /* 3962306a36Sopenharmony_ci * Information about the thread running when the IRQ 4062306a36Sopenharmony_ci * arrived. 4162306a36Sopenharmony_ci * 4262306a36Sopenharmony_ci * This can be blocking or interference, depending on the 4362306a36Sopenharmony_ci * priority of the thread. Assuming timerlat is the highest 4462306a36Sopenharmony_ci * prio, it is blocking. If timerlat has a lower prio, it is 4562306a36Sopenharmony_ci * interference. 4662306a36Sopenharmony_ci * note: "unsigned long long" because they are fetch using tep_get_field_val(); 4762306a36Sopenharmony_ci */ 4862306a36Sopenharmony_ci unsigned long long run_thread_pid; 4962306a36Sopenharmony_ci char run_thread_comm[MAX_COMM]; 5062306a36Sopenharmony_ci unsigned long long thread_blocking_duration; 5162306a36Sopenharmony_ci unsigned long long max_exit_idle_latency; 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci /* Information about the timerlat timer irq */ 5462306a36Sopenharmony_ci unsigned long long timer_irq_start_time; 5562306a36Sopenharmony_ci unsigned long long timer_irq_start_delay; 5662306a36Sopenharmony_ci unsigned long long timer_irq_duration; 5762306a36Sopenharmony_ci unsigned long long timer_exit_from_idle; 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci /* 6062306a36Sopenharmony_ci * Information about the last IRQ before the timerlat irq 6162306a36Sopenharmony_ci * arrived. 6262306a36Sopenharmony_ci * 6362306a36Sopenharmony_ci * If now - timestamp is <= latency, it might have influenced 6462306a36Sopenharmony_ci * in the timerlat irq latency. Otherwise, ignore it. 6562306a36Sopenharmony_ci */ 6662306a36Sopenharmony_ci unsigned long long prev_irq_duration; 6762306a36Sopenharmony_ci unsigned long long prev_irq_timstamp; 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci /* 7062306a36Sopenharmony_ci * Interference sum. 7162306a36Sopenharmony_ci */ 7262306a36Sopenharmony_ci unsigned long long thread_nmi_sum; 7362306a36Sopenharmony_ci unsigned long long thread_irq_sum; 7462306a36Sopenharmony_ci unsigned long long thread_softirq_sum; 7562306a36Sopenharmony_ci unsigned long long thread_thread_sum; 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci /* 7862306a36Sopenharmony_ci * Interference task information. 7962306a36Sopenharmony_ci */ 8062306a36Sopenharmony_ci struct trace_seq *prev_irqs_seq; 8162306a36Sopenharmony_ci struct trace_seq *nmi_seq; 8262306a36Sopenharmony_ci struct trace_seq *irqs_seq; 8362306a36Sopenharmony_ci struct trace_seq *softirqs_seq; 8462306a36Sopenharmony_ci struct trace_seq *threads_seq; 8562306a36Sopenharmony_ci struct trace_seq *stack_seq; 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci /* 8862306a36Sopenharmony_ci * Current thread. 8962306a36Sopenharmony_ci */ 9062306a36Sopenharmony_ci char current_comm[MAX_COMM]; 9162306a36Sopenharmony_ci unsigned long long current_pid; 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci /* 9462306a36Sopenharmony_ci * Is the system running a kworker? 9562306a36Sopenharmony_ci */ 9662306a36Sopenharmony_ci unsigned long long kworker; 9762306a36Sopenharmony_ci unsigned long long kworker_func; 9862306a36Sopenharmony_ci}; 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci/* 10162306a36Sopenharmony_ci * The analysis context and system wide view 10262306a36Sopenharmony_ci */ 10362306a36Sopenharmony_cistruct timerlat_aa_context { 10462306a36Sopenharmony_ci int nr_cpus; 10562306a36Sopenharmony_ci int dump_tasks; 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci /* per CPU data */ 10862306a36Sopenharmony_ci struct timerlat_aa_data *taa_data; 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci /* 11162306a36Sopenharmony_ci * required to translate function names and register 11262306a36Sopenharmony_ci * events. 11362306a36Sopenharmony_ci */ 11462306a36Sopenharmony_ci struct osnoise_tool *tool; 11562306a36Sopenharmony_ci}; 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci/* 11862306a36Sopenharmony_ci * The data is stored as a local variable, but accessed via a helper function. 11962306a36Sopenharmony_ci * 12062306a36Sopenharmony_ci * It could be stored inside the trace context. But every access would 12162306a36Sopenharmony_ci * require container_of() + a series of pointers. Do we need it? Not sure. 12262306a36Sopenharmony_ci * 12362306a36Sopenharmony_ci * For now keep it simple. If needed, store it in the tool, add the *context 12462306a36Sopenharmony_ci * as a parameter in timerlat_aa_get_ctx() and do the magic there. 12562306a36Sopenharmony_ci */ 12662306a36Sopenharmony_cistatic struct timerlat_aa_context *__timerlat_aa_ctx; 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_cistatic struct timerlat_aa_context *timerlat_aa_get_ctx(void) 12962306a36Sopenharmony_ci{ 13062306a36Sopenharmony_ci return __timerlat_aa_ctx; 13162306a36Sopenharmony_ci} 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci/* 13462306a36Sopenharmony_ci * timerlat_aa_get_data - Get the per-cpu data from the timerlat context 13562306a36Sopenharmony_ci */ 13662306a36Sopenharmony_cistatic struct timerlat_aa_data 13762306a36Sopenharmony_ci*timerlat_aa_get_data(struct timerlat_aa_context *taa_ctx, int cpu) 13862306a36Sopenharmony_ci{ 13962306a36Sopenharmony_ci return &taa_ctx->taa_data[cpu]; 14062306a36Sopenharmony_ci} 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci/* 14362306a36Sopenharmony_ci * timerlat_aa_irq_latency - Handles timerlat IRQ event 14462306a36Sopenharmony_ci */ 14562306a36Sopenharmony_cistatic int timerlat_aa_irq_latency(struct timerlat_aa_data *taa_data, 14662306a36Sopenharmony_ci struct trace_seq *s, struct tep_record *record, 14762306a36Sopenharmony_ci struct tep_event *event) 14862306a36Sopenharmony_ci{ 14962306a36Sopenharmony_ci /* 15062306a36Sopenharmony_ci * For interference, we start now looking for things that can delay 15162306a36Sopenharmony_ci * the thread. 15262306a36Sopenharmony_ci */ 15362306a36Sopenharmony_ci taa_data->curr_state = TIMERLAT_WAITING_THREAD; 15462306a36Sopenharmony_ci taa_data->tlat_irq_timstamp = record->ts; 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci /* 15762306a36Sopenharmony_ci * Zero values. 15862306a36Sopenharmony_ci */ 15962306a36Sopenharmony_ci taa_data->thread_nmi_sum = 0; 16062306a36Sopenharmony_ci taa_data->thread_irq_sum = 0; 16162306a36Sopenharmony_ci taa_data->thread_softirq_sum = 0; 16262306a36Sopenharmony_ci taa_data->thread_thread_sum = 0; 16362306a36Sopenharmony_ci taa_data->thread_blocking_duration = 0; 16462306a36Sopenharmony_ci taa_data->timer_irq_start_time = 0; 16562306a36Sopenharmony_ci taa_data->timer_irq_duration = 0; 16662306a36Sopenharmony_ci taa_data->timer_exit_from_idle = 0; 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci /* 16962306a36Sopenharmony_ci * Zero interference tasks. 17062306a36Sopenharmony_ci */ 17162306a36Sopenharmony_ci trace_seq_reset(taa_data->nmi_seq); 17262306a36Sopenharmony_ci trace_seq_reset(taa_data->irqs_seq); 17362306a36Sopenharmony_ci trace_seq_reset(taa_data->softirqs_seq); 17462306a36Sopenharmony_ci trace_seq_reset(taa_data->threads_seq); 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci /* IRQ latency values */ 17762306a36Sopenharmony_ci tep_get_field_val(s, event, "timer_latency", record, &taa_data->tlat_irq_latency, 1); 17862306a36Sopenharmony_ci tep_get_field_val(s, event, "seqnum", record, &taa_data->tlat_irq_seqnum, 1); 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci /* The thread that can cause blocking */ 18162306a36Sopenharmony_ci tep_get_common_field_val(s, event, "common_pid", record, &taa_data->run_thread_pid, 1); 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci /* 18462306a36Sopenharmony_ci * Get exit from idle case. 18562306a36Sopenharmony_ci * 18662306a36Sopenharmony_ci * If it is not idle thread: 18762306a36Sopenharmony_ci */ 18862306a36Sopenharmony_ci if (taa_data->run_thread_pid) 18962306a36Sopenharmony_ci return 0; 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ci /* 19262306a36Sopenharmony_ci * if the latency is shorter than the known exit from idle: 19362306a36Sopenharmony_ci */ 19462306a36Sopenharmony_ci if (taa_data->tlat_irq_latency < taa_data->max_exit_idle_latency) 19562306a36Sopenharmony_ci return 0; 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci /* 19862306a36Sopenharmony_ci * To be safe, ignore the cases in which an IRQ/NMI could have 19962306a36Sopenharmony_ci * interfered with the timerlat IRQ. 20062306a36Sopenharmony_ci */ 20162306a36Sopenharmony_ci if (taa_data->tlat_irq_timstamp - taa_data->tlat_irq_latency 20262306a36Sopenharmony_ci < taa_data->prev_irq_timstamp + taa_data->prev_irq_duration) 20362306a36Sopenharmony_ci return 0; 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci taa_data->max_exit_idle_latency = taa_data->tlat_irq_latency; 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci return 0; 20862306a36Sopenharmony_ci} 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ci/* 21162306a36Sopenharmony_ci * timerlat_aa_thread_latency - Handles timerlat thread event 21262306a36Sopenharmony_ci */ 21362306a36Sopenharmony_cistatic int timerlat_aa_thread_latency(struct timerlat_aa_data *taa_data, 21462306a36Sopenharmony_ci struct trace_seq *s, struct tep_record *record, 21562306a36Sopenharmony_ci struct tep_event *event) 21662306a36Sopenharmony_ci{ 21762306a36Sopenharmony_ci /* 21862306a36Sopenharmony_ci * For interference, we start now looking for things that can delay 21962306a36Sopenharmony_ci * the IRQ of the next cycle. 22062306a36Sopenharmony_ci */ 22162306a36Sopenharmony_ci taa_data->curr_state = TIMERLAT_WAITING_IRQ; 22262306a36Sopenharmony_ci taa_data->tlat_thread_timstamp = record->ts; 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci /* Thread latency values */ 22562306a36Sopenharmony_ci tep_get_field_val(s, event, "timer_latency", record, &taa_data->tlat_thread_latency, 1); 22662306a36Sopenharmony_ci tep_get_field_val(s, event, "seqnum", record, &taa_data->tlat_thread_seqnum, 1); 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ci return 0; 22962306a36Sopenharmony_ci} 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci/* 23262306a36Sopenharmony_ci * timerlat_aa_handler - Handle timerlat events 23362306a36Sopenharmony_ci * 23462306a36Sopenharmony_ci * This function is called to handle timerlat events recording statistics. 23562306a36Sopenharmony_ci * 23662306a36Sopenharmony_ci * Returns 0 on success, -1 otherwise. 23762306a36Sopenharmony_ci */ 23862306a36Sopenharmony_cistatic int timerlat_aa_handler(struct trace_seq *s, struct tep_record *record, 23962306a36Sopenharmony_ci struct tep_event *event, void *context) 24062306a36Sopenharmony_ci{ 24162306a36Sopenharmony_ci struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx(); 24262306a36Sopenharmony_ci struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu); 24362306a36Sopenharmony_ci unsigned long long thread; 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci if (!taa_data) 24662306a36Sopenharmony_ci return -1; 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci tep_get_field_val(s, event, "context", record, &thread, 1); 24962306a36Sopenharmony_ci if (!thread) 25062306a36Sopenharmony_ci return timerlat_aa_irq_latency(taa_data, s, record, event); 25162306a36Sopenharmony_ci else 25262306a36Sopenharmony_ci return timerlat_aa_thread_latency(taa_data, s, record, event); 25362306a36Sopenharmony_ci} 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ci/* 25662306a36Sopenharmony_ci * timerlat_aa_nmi_handler - Handles NMI noise 25762306a36Sopenharmony_ci * 25862306a36Sopenharmony_ci * It is used to collect information about interferences from NMI. It is 25962306a36Sopenharmony_ci * hooked to the osnoise:nmi_noise event. 26062306a36Sopenharmony_ci */ 26162306a36Sopenharmony_cistatic int timerlat_aa_nmi_handler(struct trace_seq *s, struct tep_record *record, 26262306a36Sopenharmony_ci struct tep_event *event, void *context) 26362306a36Sopenharmony_ci{ 26462306a36Sopenharmony_ci struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx(); 26562306a36Sopenharmony_ci struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu); 26662306a36Sopenharmony_ci unsigned long long duration; 26762306a36Sopenharmony_ci unsigned long long start; 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ci tep_get_field_val(s, event, "duration", record, &duration, 1); 27062306a36Sopenharmony_ci tep_get_field_val(s, event, "start", record, &start, 1); 27162306a36Sopenharmony_ci 27262306a36Sopenharmony_ci if (taa_data->curr_state == TIMERLAT_WAITING_IRQ) { 27362306a36Sopenharmony_ci taa_data->prev_irq_duration = duration; 27462306a36Sopenharmony_ci taa_data->prev_irq_timstamp = start; 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_ci trace_seq_reset(taa_data->prev_irqs_seq); 27762306a36Sopenharmony_ci trace_seq_printf(taa_data->prev_irqs_seq, "\t%24s \t\t\t%9.2f us\n", 27862306a36Sopenharmony_ci "nmi", ns_to_usf(duration)); 27962306a36Sopenharmony_ci return 0; 28062306a36Sopenharmony_ci } 28162306a36Sopenharmony_ci 28262306a36Sopenharmony_ci taa_data->thread_nmi_sum += duration; 28362306a36Sopenharmony_ci trace_seq_printf(taa_data->nmi_seq, " %24s \t\t\t%9.2f us\n", 28462306a36Sopenharmony_ci "nmi", ns_to_usf(duration)); 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_ci return 0; 28762306a36Sopenharmony_ci} 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_ci/* 29062306a36Sopenharmony_ci * timerlat_aa_irq_handler - Handles IRQ noise 29162306a36Sopenharmony_ci * 29262306a36Sopenharmony_ci * It is used to collect information about interferences from IRQ. It is 29362306a36Sopenharmony_ci * hooked to the osnoise:irq_noise event. 29462306a36Sopenharmony_ci * 29562306a36Sopenharmony_ci * It is a little bit more complex than the other because it measures: 29662306a36Sopenharmony_ci * - The IRQs that can delay the timer IRQ before it happened. 29762306a36Sopenharmony_ci * - The Timerlat IRQ handler 29862306a36Sopenharmony_ci * - The IRQs that happened between the timerlat IRQ and the timerlat thread 29962306a36Sopenharmony_ci * (IRQ interference). 30062306a36Sopenharmony_ci */ 30162306a36Sopenharmony_cistatic int timerlat_aa_irq_handler(struct trace_seq *s, struct tep_record *record, 30262306a36Sopenharmony_ci struct tep_event *event, void *context) 30362306a36Sopenharmony_ci{ 30462306a36Sopenharmony_ci struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx(); 30562306a36Sopenharmony_ci struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu); 30662306a36Sopenharmony_ci unsigned long long expected_start; 30762306a36Sopenharmony_ci unsigned long long duration; 30862306a36Sopenharmony_ci unsigned long long vector; 30962306a36Sopenharmony_ci unsigned long long start; 31062306a36Sopenharmony_ci char *desc; 31162306a36Sopenharmony_ci int val; 31262306a36Sopenharmony_ci 31362306a36Sopenharmony_ci tep_get_field_val(s, event, "duration", record, &duration, 1); 31462306a36Sopenharmony_ci tep_get_field_val(s, event, "start", record, &start, 1); 31562306a36Sopenharmony_ci tep_get_field_val(s, event, "vector", record, &vector, 1); 31662306a36Sopenharmony_ci desc = tep_get_field_raw(s, event, "desc", record, &val, 1); 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_ci /* 31962306a36Sopenharmony_ci * Before the timerlat IRQ. 32062306a36Sopenharmony_ci */ 32162306a36Sopenharmony_ci if (taa_data->curr_state == TIMERLAT_WAITING_IRQ) { 32262306a36Sopenharmony_ci taa_data->prev_irq_duration = duration; 32362306a36Sopenharmony_ci taa_data->prev_irq_timstamp = start; 32462306a36Sopenharmony_ci 32562306a36Sopenharmony_ci trace_seq_reset(taa_data->prev_irqs_seq); 32662306a36Sopenharmony_ci trace_seq_printf(taa_data->prev_irqs_seq, "\t%24s:%-3llu \t\t%9.2f us\n", 32762306a36Sopenharmony_ci desc, vector, ns_to_usf(duration)); 32862306a36Sopenharmony_ci return 0; 32962306a36Sopenharmony_ci } 33062306a36Sopenharmony_ci 33162306a36Sopenharmony_ci /* 33262306a36Sopenharmony_ci * The timerlat IRQ: taa_data->timer_irq_start_time is zeroed at 33362306a36Sopenharmony_ci * the timerlat irq handler. 33462306a36Sopenharmony_ci */ 33562306a36Sopenharmony_ci if (!taa_data->timer_irq_start_time) { 33662306a36Sopenharmony_ci expected_start = taa_data->tlat_irq_timstamp - taa_data->tlat_irq_latency; 33762306a36Sopenharmony_ci 33862306a36Sopenharmony_ci taa_data->timer_irq_start_time = start; 33962306a36Sopenharmony_ci taa_data->timer_irq_duration = duration; 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ci /* 34262306a36Sopenharmony_ci * We are dealing with two different clock sources: the 34362306a36Sopenharmony_ci * external clock source that timerlat uses as a reference 34462306a36Sopenharmony_ci * and the clock used by the tracer. There are also two 34562306a36Sopenharmony_ci * moments: the time reading the clock and the timer in 34662306a36Sopenharmony_ci * which the event is placed in the buffer (the trace 34762306a36Sopenharmony_ci * event timestamp). If the processor is slow or there 34862306a36Sopenharmony_ci * is some hardware noise, the difference between the 34962306a36Sopenharmony_ci * timestamp and the external clock read can be longer 35062306a36Sopenharmony_ci * than the IRQ handler delay, resulting in a negative 35162306a36Sopenharmony_ci * time. If so, set IRQ start delay as 0. In the end, 35262306a36Sopenharmony_ci * it is less relevant than the noise. 35362306a36Sopenharmony_ci */ 35462306a36Sopenharmony_ci if (expected_start < taa_data->timer_irq_start_time) 35562306a36Sopenharmony_ci taa_data->timer_irq_start_delay = taa_data->timer_irq_start_time - expected_start; 35662306a36Sopenharmony_ci else 35762306a36Sopenharmony_ci taa_data->timer_irq_start_delay = 0; 35862306a36Sopenharmony_ci 35962306a36Sopenharmony_ci /* 36062306a36Sopenharmony_ci * not exit from idle. 36162306a36Sopenharmony_ci */ 36262306a36Sopenharmony_ci if (taa_data->run_thread_pid) 36362306a36Sopenharmony_ci return 0; 36462306a36Sopenharmony_ci 36562306a36Sopenharmony_ci if (expected_start > taa_data->prev_irq_timstamp + taa_data->prev_irq_duration) 36662306a36Sopenharmony_ci taa_data->timer_exit_from_idle = taa_data->timer_irq_start_delay; 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ci return 0; 36962306a36Sopenharmony_ci } 37062306a36Sopenharmony_ci 37162306a36Sopenharmony_ci /* 37262306a36Sopenharmony_ci * IRQ interference. 37362306a36Sopenharmony_ci */ 37462306a36Sopenharmony_ci taa_data->thread_irq_sum += duration; 37562306a36Sopenharmony_ci trace_seq_printf(taa_data->irqs_seq, " %24s:%-3llu \t %9.2f us\n", 37662306a36Sopenharmony_ci desc, vector, ns_to_usf(duration)); 37762306a36Sopenharmony_ci 37862306a36Sopenharmony_ci return 0; 37962306a36Sopenharmony_ci} 38062306a36Sopenharmony_ci 38162306a36Sopenharmony_cistatic char *softirq_name[] = { "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", 38262306a36Sopenharmony_ci "IRQ_POLL", "TASKLET", "SCHED", "HRTIMER", "RCU" }; 38362306a36Sopenharmony_ci 38462306a36Sopenharmony_ci 38562306a36Sopenharmony_ci/* 38662306a36Sopenharmony_ci * timerlat_aa_softirq_handler - Handles Softirq noise 38762306a36Sopenharmony_ci * 38862306a36Sopenharmony_ci * It is used to collect information about interferences from Softirq. It is 38962306a36Sopenharmony_ci * hooked to the osnoise:softirq_noise event. 39062306a36Sopenharmony_ci * 39162306a36Sopenharmony_ci * It is only printed in the non-rt kernel, as softirqs become thread on RT. 39262306a36Sopenharmony_ci */ 39362306a36Sopenharmony_cistatic int timerlat_aa_softirq_handler(struct trace_seq *s, struct tep_record *record, 39462306a36Sopenharmony_ci struct tep_event *event, void *context) 39562306a36Sopenharmony_ci{ 39662306a36Sopenharmony_ci struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx(); 39762306a36Sopenharmony_ci struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu); 39862306a36Sopenharmony_ci unsigned long long duration; 39962306a36Sopenharmony_ci unsigned long long vector; 40062306a36Sopenharmony_ci unsigned long long start; 40162306a36Sopenharmony_ci 40262306a36Sopenharmony_ci if (taa_data->curr_state == TIMERLAT_WAITING_IRQ) 40362306a36Sopenharmony_ci return 0; 40462306a36Sopenharmony_ci 40562306a36Sopenharmony_ci tep_get_field_val(s, event, "duration", record, &duration, 1); 40662306a36Sopenharmony_ci tep_get_field_val(s, event, "start", record, &start, 1); 40762306a36Sopenharmony_ci tep_get_field_val(s, event, "vector", record, &vector, 1); 40862306a36Sopenharmony_ci 40962306a36Sopenharmony_ci taa_data->thread_softirq_sum += duration; 41062306a36Sopenharmony_ci 41162306a36Sopenharmony_ci trace_seq_printf(taa_data->softirqs_seq, "\t%24s:%-3llu \t %9.2f us\n", 41262306a36Sopenharmony_ci softirq_name[vector], vector, ns_to_usf(duration)); 41362306a36Sopenharmony_ci return 0; 41462306a36Sopenharmony_ci} 41562306a36Sopenharmony_ci 41662306a36Sopenharmony_ci/* 41762306a36Sopenharmony_ci * timerlat_aa_softirq_handler - Handles thread noise 41862306a36Sopenharmony_ci * 41962306a36Sopenharmony_ci * It is used to collect information about interferences from threads. It is 42062306a36Sopenharmony_ci * hooked to the osnoise:thread_noise event. 42162306a36Sopenharmony_ci * 42262306a36Sopenharmony_ci * Note: if you see thread noise, your timerlat thread was not the highest prio one. 42362306a36Sopenharmony_ci */ 42462306a36Sopenharmony_cistatic int timerlat_aa_thread_handler(struct trace_seq *s, struct tep_record *record, 42562306a36Sopenharmony_ci struct tep_event *event, void *context) 42662306a36Sopenharmony_ci{ 42762306a36Sopenharmony_ci struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx(); 42862306a36Sopenharmony_ci struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu); 42962306a36Sopenharmony_ci unsigned long long duration; 43062306a36Sopenharmony_ci unsigned long long start; 43162306a36Sopenharmony_ci unsigned long long pid; 43262306a36Sopenharmony_ci const char *comm; 43362306a36Sopenharmony_ci int val; 43462306a36Sopenharmony_ci 43562306a36Sopenharmony_ci if (taa_data->curr_state == TIMERLAT_WAITING_IRQ) 43662306a36Sopenharmony_ci return 0; 43762306a36Sopenharmony_ci 43862306a36Sopenharmony_ci tep_get_field_val(s, event, "duration", record, &duration, 1); 43962306a36Sopenharmony_ci tep_get_field_val(s, event, "start", record, &start, 1); 44062306a36Sopenharmony_ci 44162306a36Sopenharmony_ci tep_get_common_field_val(s, event, "common_pid", record, &pid, 1); 44262306a36Sopenharmony_ci comm = tep_get_field_raw(s, event, "comm", record, &val, 1); 44362306a36Sopenharmony_ci 44462306a36Sopenharmony_ci if (pid == taa_data->run_thread_pid && !taa_data->thread_blocking_duration) { 44562306a36Sopenharmony_ci taa_data->thread_blocking_duration = duration; 44662306a36Sopenharmony_ci 44762306a36Sopenharmony_ci if (comm) 44862306a36Sopenharmony_ci strncpy(taa_data->run_thread_comm, comm, MAX_COMM); 44962306a36Sopenharmony_ci else 45062306a36Sopenharmony_ci sprintf(taa_data->run_thread_comm, "<...>"); 45162306a36Sopenharmony_ci 45262306a36Sopenharmony_ci } else { 45362306a36Sopenharmony_ci taa_data->thread_thread_sum += duration; 45462306a36Sopenharmony_ci 45562306a36Sopenharmony_ci trace_seq_printf(taa_data->threads_seq, "\t%24s:%-3llu \t\t%9.2f us\n", 45662306a36Sopenharmony_ci comm, pid, ns_to_usf(duration)); 45762306a36Sopenharmony_ci } 45862306a36Sopenharmony_ci 45962306a36Sopenharmony_ci return 0; 46062306a36Sopenharmony_ci} 46162306a36Sopenharmony_ci 46262306a36Sopenharmony_ci/* 46362306a36Sopenharmony_ci * timerlat_aa_stack_handler - Handles timerlat IRQ stack trace 46462306a36Sopenharmony_ci * 46562306a36Sopenharmony_ci * Saves and parse the stack trace generated by the timerlat IRQ. 46662306a36Sopenharmony_ci */ 46762306a36Sopenharmony_cistatic int timerlat_aa_stack_handler(struct trace_seq *s, struct tep_record *record, 46862306a36Sopenharmony_ci struct tep_event *event, void *context) 46962306a36Sopenharmony_ci{ 47062306a36Sopenharmony_ci struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx(); 47162306a36Sopenharmony_ci struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu); 47262306a36Sopenharmony_ci unsigned long *caller; 47362306a36Sopenharmony_ci const char *function; 47462306a36Sopenharmony_ci int val, i; 47562306a36Sopenharmony_ci 47662306a36Sopenharmony_ci trace_seq_reset(taa_data->stack_seq); 47762306a36Sopenharmony_ci 47862306a36Sopenharmony_ci trace_seq_printf(taa_data->stack_seq, " Blocking thread stack trace\n"); 47962306a36Sopenharmony_ci caller = tep_get_field_raw(s, event, "caller", record, &val, 1); 48062306a36Sopenharmony_ci if (caller) { 48162306a36Sopenharmony_ci for (i = 0; ; i++) { 48262306a36Sopenharmony_ci function = tep_find_function(taa_ctx->tool->trace.tep, caller[i]); 48362306a36Sopenharmony_ci if (!function) 48462306a36Sopenharmony_ci break; 48562306a36Sopenharmony_ci trace_seq_printf(taa_data->stack_seq, "\t\t-> %s\n", function); 48662306a36Sopenharmony_ci } 48762306a36Sopenharmony_ci } 48862306a36Sopenharmony_ci return 0; 48962306a36Sopenharmony_ci} 49062306a36Sopenharmony_ci 49162306a36Sopenharmony_ci/* 49262306a36Sopenharmony_ci * timerlat_aa_sched_switch_handler - Tracks the current thread running on the CPU 49362306a36Sopenharmony_ci * 49462306a36Sopenharmony_ci * Handles the sched:sched_switch event to trace the current thread running on the 49562306a36Sopenharmony_ci * CPU. It is used to display the threads running on the other CPUs when the trace 49662306a36Sopenharmony_ci * stops. 49762306a36Sopenharmony_ci */ 49862306a36Sopenharmony_cistatic int timerlat_aa_sched_switch_handler(struct trace_seq *s, struct tep_record *record, 49962306a36Sopenharmony_ci struct tep_event *event, void *context) 50062306a36Sopenharmony_ci{ 50162306a36Sopenharmony_ci struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx(); 50262306a36Sopenharmony_ci struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu); 50362306a36Sopenharmony_ci const char *comm; 50462306a36Sopenharmony_ci int val; 50562306a36Sopenharmony_ci 50662306a36Sopenharmony_ci tep_get_field_val(s, event, "next_pid", record, &taa_data->current_pid, 1); 50762306a36Sopenharmony_ci comm = tep_get_field_raw(s, event, "next_comm", record, &val, 1); 50862306a36Sopenharmony_ci 50962306a36Sopenharmony_ci strncpy(taa_data->current_comm, comm, MAX_COMM); 51062306a36Sopenharmony_ci 51162306a36Sopenharmony_ci /* 51262306a36Sopenharmony_ci * If this was a kworker, clean the last kworkers that ran. 51362306a36Sopenharmony_ci */ 51462306a36Sopenharmony_ci taa_data->kworker = 0; 51562306a36Sopenharmony_ci taa_data->kworker_func = 0; 51662306a36Sopenharmony_ci 51762306a36Sopenharmony_ci return 0; 51862306a36Sopenharmony_ci} 51962306a36Sopenharmony_ci 52062306a36Sopenharmony_ci/* 52162306a36Sopenharmony_ci * timerlat_aa_kworker_start_handler - Tracks a kworker running on the CPU 52262306a36Sopenharmony_ci * 52362306a36Sopenharmony_ci * Handles workqueue:workqueue_execute_start event, keeping track of 52462306a36Sopenharmony_ci * the job that a kworker could be doing in the CPU. 52562306a36Sopenharmony_ci * 52662306a36Sopenharmony_ci * We already catch problems of hardware related latencies caused by work queues 52762306a36Sopenharmony_ci * running driver code that causes hardware stall. For example, with DRM drivers. 52862306a36Sopenharmony_ci */ 52962306a36Sopenharmony_cistatic int timerlat_aa_kworker_start_handler(struct trace_seq *s, struct tep_record *record, 53062306a36Sopenharmony_ci struct tep_event *event, void *context) 53162306a36Sopenharmony_ci{ 53262306a36Sopenharmony_ci struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx(); 53362306a36Sopenharmony_ci struct timerlat_aa_data *taa_data = timerlat_aa_get_data(taa_ctx, record->cpu); 53462306a36Sopenharmony_ci 53562306a36Sopenharmony_ci tep_get_field_val(s, event, "work", record, &taa_data->kworker, 1); 53662306a36Sopenharmony_ci tep_get_field_val(s, event, "function", record, &taa_data->kworker_func, 1); 53762306a36Sopenharmony_ci return 0; 53862306a36Sopenharmony_ci} 53962306a36Sopenharmony_ci 54062306a36Sopenharmony_ci/* 54162306a36Sopenharmony_ci * timerlat_thread_analysis - Prints the analysis of a CPU that hit a stop tracing 54262306a36Sopenharmony_ci * 54362306a36Sopenharmony_ci * This is the core of the analysis. 54462306a36Sopenharmony_ci */ 54562306a36Sopenharmony_cistatic void timerlat_thread_analysis(struct timerlat_aa_data *taa_data, int cpu, 54662306a36Sopenharmony_ci int irq_thresh, int thread_thresh) 54762306a36Sopenharmony_ci{ 54862306a36Sopenharmony_ci long long exp_irq_ts; 54962306a36Sopenharmony_ci int total; 55062306a36Sopenharmony_ci int irq; 55162306a36Sopenharmony_ci 55262306a36Sopenharmony_ci /* 55362306a36Sopenharmony_ci * IRQ latency or Thread latency? 55462306a36Sopenharmony_ci */ 55562306a36Sopenharmony_ci if (taa_data->tlat_irq_seqnum > taa_data->tlat_thread_seqnum) { 55662306a36Sopenharmony_ci irq = 1; 55762306a36Sopenharmony_ci total = taa_data->tlat_irq_latency; 55862306a36Sopenharmony_ci } else { 55962306a36Sopenharmony_ci irq = 0; 56062306a36Sopenharmony_ci total = taa_data->tlat_thread_latency; 56162306a36Sopenharmony_ci } 56262306a36Sopenharmony_ci 56362306a36Sopenharmony_ci /* 56462306a36Sopenharmony_ci * Expected IRQ arrival time using the trace clock as the base. 56562306a36Sopenharmony_ci * 56662306a36Sopenharmony_ci * TODO: Add a list of previous IRQ, and then run the list backwards. 56762306a36Sopenharmony_ci */ 56862306a36Sopenharmony_ci exp_irq_ts = taa_data->timer_irq_start_time - taa_data->timer_irq_start_delay; 56962306a36Sopenharmony_ci if (exp_irq_ts < taa_data->prev_irq_timstamp + taa_data->prev_irq_duration) { 57062306a36Sopenharmony_ci if (taa_data->prev_irq_timstamp < taa_data->timer_irq_start_time) 57162306a36Sopenharmony_ci printf(" Previous IRQ interference: \t\t up to %9.2f us\n", 57262306a36Sopenharmony_ci ns_to_usf(taa_data->prev_irq_duration)); 57362306a36Sopenharmony_ci } 57462306a36Sopenharmony_ci 57562306a36Sopenharmony_ci /* 57662306a36Sopenharmony_ci * The delay that the IRQ suffered before starting. 57762306a36Sopenharmony_ci */ 57862306a36Sopenharmony_ci printf(" IRQ handler delay: %16s %9.2f us (%.2f %%)\n", 57962306a36Sopenharmony_ci (ns_to_usf(taa_data->timer_exit_from_idle) > 10) ? "(exit from idle)" : "", 58062306a36Sopenharmony_ci ns_to_usf(taa_data->timer_irq_start_delay), 58162306a36Sopenharmony_ci ns_to_per(total, taa_data->timer_irq_start_delay)); 58262306a36Sopenharmony_ci 58362306a36Sopenharmony_ci /* 58462306a36Sopenharmony_ci * Timerlat IRQ. 58562306a36Sopenharmony_ci */ 58662306a36Sopenharmony_ci printf(" IRQ latency: \t\t\t\t %9.2f us\n", 58762306a36Sopenharmony_ci ns_to_usf(taa_data->tlat_irq_latency)); 58862306a36Sopenharmony_ci 58962306a36Sopenharmony_ci if (irq) { 59062306a36Sopenharmony_ci /* 59162306a36Sopenharmony_ci * If the trace stopped due to IRQ, the other events will not happen 59262306a36Sopenharmony_ci * because... the trace stopped :-). 59362306a36Sopenharmony_ci * 59462306a36Sopenharmony_ci * That is all folks, the stack trace was printed before the stop, 59562306a36Sopenharmony_ci * so it will be displayed, it is the key. 59662306a36Sopenharmony_ci */ 59762306a36Sopenharmony_ci printf(" Blocking thread:\n"); 59862306a36Sopenharmony_ci printf(" %24s:%-9llu\n", 59962306a36Sopenharmony_ci taa_data->run_thread_comm, taa_data->run_thread_pid); 60062306a36Sopenharmony_ci } else { 60162306a36Sopenharmony_ci /* 60262306a36Sopenharmony_ci * The duration of the IRQ handler that handled the timerlat IRQ. 60362306a36Sopenharmony_ci */ 60462306a36Sopenharmony_ci printf(" Timerlat IRQ duration: \t\t %9.2f us (%.2f %%)\n", 60562306a36Sopenharmony_ci ns_to_usf(taa_data->timer_irq_duration), 60662306a36Sopenharmony_ci ns_to_per(total, taa_data->timer_irq_duration)); 60762306a36Sopenharmony_ci 60862306a36Sopenharmony_ci /* 60962306a36Sopenharmony_ci * The amount of time that the current thread postponed the scheduler. 61062306a36Sopenharmony_ci * 61162306a36Sopenharmony_ci * Recalling that it is net from NMI/IRQ/Softirq interference, so there 61262306a36Sopenharmony_ci * is no need to compute values here. 61362306a36Sopenharmony_ci */ 61462306a36Sopenharmony_ci printf(" Blocking thread: \t\t\t %9.2f us (%.2f %%)\n", 61562306a36Sopenharmony_ci ns_to_usf(taa_data->thread_blocking_duration), 61662306a36Sopenharmony_ci ns_to_per(total, taa_data->thread_blocking_duration)); 61762306a36Sopenharmony_ci 61862306a36Sopenharmony_ci printf(" %24s:%-9llu %9.2f us\n", 61962306a36Sopenharmony_ci taa_data->run_thread_comm, taa_data->run_thread_pid, 62062306a36Sopenharmony_ci ns_to_usf(taa_data->thread_blocking_duration)); 62162306a36Sopenharmony_ci } 62262306a36Sopenharmony_ci 62362306a36Sopenharmony_ci /* 62462306a36Sopenharmony_ci * Print the stack trace! 62562306a36Sopenharmony_ci */ 62662306a36Sopenharmony_ci trace_seq_do_printf(taa_data->stack_seq); 62762306a36Sopenharmony_ci 62862306a36Sopenharmony_ci /* 62962306a36Sopenharmony_ci * NMIs can happen during the IRQ, so they are always possible. 63062306a36Sopenharmony_ci */ 63162306a36Sopenharmony_ci if (taa_data->thread_nmi_sum) 63262306a36Sopenharmony_ci printf(" NMI interference \t\t\t %9.2f us (%.2f %%)\n", 63362306a36Sopenharmony_ci ns_to_usf(taa_data->thread_nmi_sum), 63462306a36Sopenharmony_ci ns_to_per(total, taa_data->thread_nmi_sum)); 63562306a36Sopenharmony_ci 63662306a36Sopenharmony_ci /* 63762306a36Sopenharmony_ci * If it is an IRQ latency, the other factors can be skipped. 63862306a36Sopenharmony_ci */ 63962306a36Sopenharmony_ci if (irq) 64062306a36Sopenharmony_ci goto print_total; 64162306a36Sopenharmony_ci 64262306a36Sopenharmony_ci /* 64362306a36Sopenharmony_ci * Prints the interference caused by IRQs to the thread latency. 64462306a36Sopenharmony_ci */ 64562306a36Sopenharmony_ci if (taa_data->thread_irq_sum) { 64662306a36Sopenharmony_ci printf(" IRQ interference \t\t\t %9.2f us (%.2f %%)\n", 64762306a36Sopenharmony_ci ns_to_usf(taa_data->thread_irq_sum), 64862306a36Sopenharmony_ci ns_to_per(total, taa_data->thread_irq_sum)); 64962306a36Sopenharmony_ci 65062306a36Sopenharmony_ci trace_seq_do_printf(taa_data->irqs_seq); 65162306a36Sopenharmony_ci } 65262306a36Sopenharmony_ci 65362306a36Sopenharmony_ci /* 65462306a36Sopenharmony_ci * Prints the interference caused by Softirqs to the thread latency. 65562306a36Sopenharmony_ci */ 65662306a36Sopenharmony_ci if (taa_data->thread_softirq_sum) { 65762306a36Sopenharmony_ci printf(" Softirq interference \t\t\t %9.2f us (%.2f %%)\n", 65862306a36Sopenharmony_ci ns_to_usf(taa_data->thread_softirq_sum), 65962306a36Sopenharmony_ci ns_to_per(total, taa_data->thread_softirq_sum)); 66062306a36Sopenharmony_ci 66162306a36Sopenharmony_ci trace_seq_do_printf(taa_data->softirqs_seq); 66262306a36Sopenharmony_ci } 66362306a36Sopenharmony_ci 66462306a36Sopenharmony_ci /* 66562306a36Sopenharmony_ci * Prints the interference caused by other threads to the thread latency. 66662306a36Sopenharmony_ci * 66762306a36Sopenharmony_ci * If this happens, your timerlat is not the highest prio. OK, migration 66862306a36Sopenharmony_ci * thread can happen. But otherwise, you are not measuring the "scheduling 66962306a36Sopenharmony_ci * latency" only, and here is the difference from scheduling latency and 67062306a36Sopenharmony_ci * timer handling latency. 67162306a36Sopenharmony_ci */ 67262306a36Sopenharmony_ci if (taa_data->thread_thread_sum) { 67362306a36Sopenharmony_ci printf(" Thread interference \t\t\t %9.2f us (%.2f %%)\n", 67462306a36Sopenharmony_ci ns_to_usf(taa_data->thread_thread_sum), 67562306a36Sopenharmony_ci ns_to_per(total, taa_data->thread_thread_sum)); 67662306a36Sopenharmony_ci 67762306a36Sopenharmony_ci trace_seq_do_printf(taa_data->threads_seq); 67862306a36Sopenharmony_ci } 67962306a36Sopenharmony_ci 68062306a36Sopenharmony_ci /* 68162306a36Sopenharmony_ci * Done. 68262306a36Sopenharmony_ci */ 68362306a36Sopenharmony_ciprint_total: 68462306a36Sopenharmony_ci printf("------------------------------------------------------------------------\n"); 68562306a36Sopenharmony_ci printf(" %s latency: \t\t\t %9.2f us (100%%)\n", irq ? "IRQ" : "Thread", 68662306a36Sopenharmony_ci ns_to_usf(total)); 68762306a36Sopenharmony_ci} 68862306a36Sopenharmony_ci 68962306a36Sopenharmony_cistatic int timerlat_auto_analysis_collect_trace(struct timerlat_aa_context *taa_ctx) 69062306a36Sopenharmony_ci{ 69162306a36Sopenharmony_ci struct trace_instance *trace = &taa_ctx->tool->trace; 69262306a36Sopenharmony_ci int retval; 69362306a36Sopenharmony_ci 69462306a36Sopenharmony_ci retval = tracefs_iterate_raw_events(trace->tep, 69562306a36Sopenharmony_ci trace->inst, 69662306a36Sopenharmony_ci NULL, 69762306a36Sopenharmony_ci 0, 69862306a36Sopenharmony_ci collect_registered_events, 69962306a36Sopenharmony_ci trace); 70062306a36Sopenharmony_ci if (retval < 0) { 70162306a36Sopenharmony_ci err_msg("Error iterating on events\n"); 70262306a36Sopenharmony_ci return 0; 70362306a36Sopenharmony_ci } 70462306a36Sopenharmony_ci 70562306a36Sopenharmony_ci return 1; 70662306a36Sopenharmony_ci} 70762306a36Sopenharmony_ci 70862306a36Sopenharmony_ci/** 70962306a36Sopenharmony_ci * timerlat_auto_analysis - Analyze the collected data 71062306a36Sopenharmony_ci */ 71162306a36Sopenharmony_civoid timerlat_auto_analysis(int irq_thresh, int thread_thresh) 71262306a36Sopenharmony_ci{ 71362306a36Sopenharmony_ci struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx(); 71462306a36Sopenharmony_ci unsigned long long max_exit_from_idle = 0; 71562306a36Sopenharmony_ci struct timerlat_aa_data *taa_data; 71662306a36Sopenharmony_ci int max_exit_from_idle_cpu; 71762306a36Sopenharmony_ci struct tep_handle *tep; 71862306a36Sopenharmony_ci int cpu; 71962306a36Sopenharmony_ci 72062306a36Sopenharmony_ci timerlat_auto_analysis_collect_trace(taa_ctx); 72162306a36Sopenharmony_ci 72262306a36Sopenharmony_ci /* bring stop tracing to the ns scale */ 72362306a36Sopenharmony_ci irq_thresh = irq_thresh * 1000; 72462306a36Sopenharmony_ci thread_thresh = thread_thresh * 1000; 72562306a36Sopenharmony_ci 72662306a36Sopenharmony_ci for (cpu = 0; cpu < taa_ctx->nr_cpus; cpu++) { 72762306a36Sopenharmony_ci taa_data = timerlat_aa_get_data(taa_ctx, cpu); 72862306a36Sopenharmony_ci 72962306a36Sopenharmony_ci if (irq_thresh && taa_data->tlat_irq_latency >= irq_thresh) { 73062306a36Sopenharmony_ci printf("## CPU %d hit stop tracing, analyzing it ##\n", cpu); 73162306a36Sopenharmony_ci timerlat_thread_analysis(taa_data, cpu, irq_thresh, thread_thresh); 73262306a36Sopenharmony_ci } else if (thread_thresh && (taa_data->tlat_thread_latency) >= thread_thresh) { 73362306a36Sopenharmony_ci printf("## CPU %d hit stop tracing, analyzing it ##\n", cpu); 73462306a36Sopenharmony_ci timerlat_thread_analysis(taa_data, cpu, irq_thresh, thread_thresh); 73562306a36Sopenharmony_ci } 73662306a36Sopenharmony_ci 73762306a36Sopenharmony_ci if (taa_data->max_exit_idle_latency > max_exit_from_idle) { 73862306a36Sopenharmony_ci max_exit_from_idle = taa_data->max_exit_idle_latency; 73962306a36Sopenharmony_ci max_exit_from_idle_cpu = cpu; 74062306a36Sopenharmony_ci } 74162306a36Sopenharmony_ci 74262306a36Sopenharmony_ci } 74362306a36Sopenharmony_ci 74462306a36Sopenharmony_ci if (max_exit_from_idle) { 74562306a36Sopenharmony_ci printf("\n"); 74662306a36Sopenharmony_ci printf("Max timerlat IRQ latency from idle: %.2f us in cpu %d\n", 74762306a36Sopenharmony_ci ns_to_usf(max_exit_from_idle), max_exit_from_idle_cpu); 74862306a36Sopenharmony_ci } 74962306a36Sopenharmony_ci if (!taa_ctx->dump_tasks) 75062306a36Sopenharmony_ci return; 75162306a36Sopenharmony_ci 75262306a36Sopenharmony_ci printf("\n"); 75362306a36Sopenharmony_ci printf("Printing CPU tasks:\n"); 75462306a36Sopenharmony_ci for (cpu = 0; cpu < taa_ctx->nr_cpus; cpu++) { 75562306a36Sopenharmony_ci taa_data = timerlat_aa_get_data(taa_ctx, cpu); 75662306a36Sopenharmony_ci tep = taa_ctx->tool->trace.tep; 75762306a36Sopenharmony_ci 75862306a36Sopenharmony_ci printf(" [%.3d] %24s:%llu", cpu, taa_data->current_comm, taa_data->current_pid); 75962306a36Sopenharmony_ci 76062306a36Sopenharmony_ci if (taa_data->kworker_func) 76162306a36Sopenharmony_ci printf(" kworker:%s:%s", 76262306a36Sopenharmony_ci tep_find_function(tep, taa_data->kworker) ? : "<...>", 76362306a36Sopenharmony_ci tep_find_function(tep, taa_data->kworker_func)); 76462306a36Sopenharmony_ci printf("\n"); 76562306a36Sopenharmony_ci } 76662306a36Sopenharmony_ci 76762306a36Sopenharmony_ci} 76862306a36Sopenharmony_ci 76962306a36Sopenharmony_ci/* 77062306a36Sopenharmony_ci * timerlat_aa_destroy_seqs - Destroy seq files used to store parsed data 77162306a36Sopenharmony_ci */ 77262306a36Sopenharmony_cistatic void timerlat_aa_destroy_seqs(struct timerlat_aa_context *taa_ctx) 77362306a36Sopenharmony_ci{ 77462306a36Sopenharmony_ci struct timerlat_aa_data *taa_data; 77562306a36Sopenharmony_ci int i; 77662306a36Sopenharmony_ci 77762306a36Sopenharmony_ci if (!taa_ctx->taa_data) 77862306a36Sopenharmony_ci return; 77962306a36Sopenharmony_ci 78062306a36Sopenharmony_ci for (i = 0; i < taa_ctx->nr_cpus; i++) { 78162306a36Sopenharmony_ci taa_data = timerlat_aa_get_data(taa_ctx, i); 78262306a36Sopenharmony_ci 78362306a36Sopenharmony_ci if (taa_data->prev_irqs_seq) { 78462306a36Sopenharmony_ci trace_seq_destroy(taa_data->prev_irqs_seq); 78562306a36Sopenharmony_ci free(taa_data->prev_irqs_seq); 78662306a36Sopenharmony_ci } 78762306a36Sopenharmony_ci 78862306a36Sopenharmony_ci if (taa_data->nmi_seq) { 78962306a36Sopenharmony_ci trace_seq_destroy(taa_data->nmi_seq); 79062306a36Sopenharmony_ci free(taa_data->nmi_seq); 79162306a36Sopenharmony_ci } 79262306a36Sopenharmony_ci 79362306a36Sopenharmony_ci if (taa_data->irqs_seq) { 79462306a36Sopenharmony_ci trace_seq_destroy(taa_data->irqs_seq); 79562306a36Sopenharmony_ci free(taa_data->irqs_seq); 79662306a36Sopenharmony_ci } 79762306a36Sopenharmony_ci 79862306a36Sopenharmony_ci if (taa_data->softirqs_seq) { 79962306a36Sopenharmony_ci trace_seq_destroy(taa_data->softirqs_seq); 80062306a36Sopenharmony_ci free(taa_data->softirqs_seq); 80162306a36Sopenharmony_ci } 80262306a36Sopenharmony_ci 80362306a36Sopenharmony_ci if (taa_data->threads_seq) { 80462306a36Sopenharmony_ci trace_seq_destroy(taa_data->threads_seq); 80562306a36Sopenharmony_ci free(taa_data->threads_seq); 80662306a36Sopenharmony_ci } 80762306a36Sopenharmony_ci 80862306a36Sopenharmony_ci if (taa_data->stack_seq) { 80962306a36Sopenharmony_ci trace_seq_destroy(taa_data->stack_seq); 81062306a36Sopenharmony_ci free(taa_data->stack_seq); 81162306a36Sopenharmony_ci } 81262306a36Sopenharmony_ci } 81362306a36Sopenharmony_ci} 81462306a36Sopenharmony_ci 81562306a36Sopenharmony_ci/* 81662306a36Sopenharmony_ci * timerlat_aa_init_seqs - Init seq files used to store parsed information 81762306a36Sopenharmony_ci * 81862306a36Sopenharmony_ci * Instead of keeping data structures to store raw data, use seq files to 81962306a36Sopenharmony_ci * store parsed data. 82062306a36Sopenharmony_ci * 82162306a36Sopenharmony_ci * Allocates and initialize seq files. 82262306a36Sopenharmony_ci * 82362306a36Sopenharmony_ci * Returns 0 on success, -1 otherwise. 82462306a36Sopenharmony_ci */ 82562306a36Sopenharmony_cistatic int timerlat_aa_init_seqs(struct timerlat_aa_context *taa_ctx) 82662306a36Sopenharmony_ci{ 82762306a36Sopenharmony_ci struct timerlat_aa_data *taa_data; 82862306a36Sopenharmony_ci int i; 82962306a36Sopenharmony_ci 83062306a36Sopenharmony_ci for (i = 0; i < taa_ctx->nr_cpus; i++) { 83162306a36Sopenharmony_ci 83262306a36Sopenharmony_ci taa_data = timerlat_aa_get_data(taa_ctx, i); 83362306a36Sopenharmony_ci 83462306a36Sopenharmony_ci taa_data->prev_irqs_seq = calloc(1, sizeof(*taa_data->prev_irqs_seq)); 83562306a36Sopenharmony_ci if (!taa_data->prev_irqs_seq) 83662306a36Sopenharmony_ci goto out_err; 83762306a36Sopenharmony_ci 83862306a36Sopenharmony_ci trace_seq_init(taa_data->prev_irqs_seq); 83962306a36Sopenharmony_ci 84062306a36Sopenharmony_ci taa_data->nmi_seq = calloc(1, sizeof(*taa_data->nmi_seq)); 84162306a36Sopenharmony_ci if (!taa_data->nmi_seq) 84262306a36Sopenharmony_ci goto out_err; 84362306a36Sopenharmony_ci 84462306a36Sopenharmony_ci trace_seq_init(taa_data->nmi_seq); 84562306a36Sopenharmony_ci 84662306a36Sopenharmony_ci taa_data->irqs_seq = calloc(1, sizeof(*taa_data->irqs_seq)); 84762306a36Sopenharmony_ci if (!taa_data->irqs_seq) 84862306a36Sopenharmony_ci goto out_err; 84962306a36Sopenharmony_ci 85062306a36Sopenharmony_ci trace_seq_init(taa_data->irqs_seq); 85162306a36Sopenharmony_ci 85262306a36Sopenharmony_ci taa_data->softirqs_seq = calloc(1, sizeof(*taa_data->softirqs_seq)); 85362306a36Sopenharmony_ci if (!taa_data->softirqs_seq) 85462306a36Sopenharmony_ci goto out_err; 85562306a36Sopenharmony_ci 85662306a36Sopenharmony_ci trace_seq_init(taa_data->softirqs_seq); 85762306a36Sopenharmony_ci 85862306a36Sopenharmony_ci taa_data->threads_seq = calloc(1, sizeof(*taa_data->threads_seq)); 85962306a36Sopenharmony_ci if (!taa_data->threads_seq) 86062306a36Sopenharmony_ci goto out_err; 86162306a36Sopenharmony_ci 86262306a36Sopenharmony_ci trace_seq_init(taa_data->threads_seq); 86362306a36Sopenharmony_ci 86462306a36Sopenharmony_ci taa_data->stack_seq = calloc(1, sizeof(*taa_data->stack_seq)); 86562306a36Sopenharmony_ci if (!taa_data->stack_seq) 86662306a36Sopenharmony_ci goto out_err; 86762306a36Sopenharmony_ci 86862306a36Sopenharmony_ci trace_seq_init(taa_data->stack_seq); 86962306a36Sopenharmony_ci } 87062306a36Sopenharmony_ci 87162306a36Sopenharmony_ci return 0; 87262306a36Sopenharmony_ci 87362306a36Sopenharmony_ciout_err: 87462306a36Sopenharmony_ci timerlat_aa_destroy_seqs(taa_ctx); 87562306a36Sopenharmony_ci return -1; 87662306a36Sopenharmony_ci} 87762306a36Sopenharmony_ci 87862306a36Sopenharmony_ci/* 87962306a36Sopenharmony_ci * timerlat_aa_unregister_events - Unregister events used in the auto-analysis 88062306a36Sopenharmony_ci */ 88162306a36Sopenharmony_cistatic void timerlat_aa_unregister_events(struct osnoise_tool *tool, int dump_tasks) 88262306a36Sopenharmony_ci{ 88362306a36Sopenharmony_ci 88462306a36Sopenharmony_ci tep_unregister_event_handler(tool->trace.tep, -1, "ftrace", "timerlat", 88562306a36Sopenharmony_ci timerlat_aa_handler, tool); 88662306a36Sopenharmony_ci 88762306a36Sopenharmony_ci tracefs_event_disable(tool->trace.inst, "osnoise", NULL); 88862306a36Sopenharmony_ci 88962306a36Sopenharmony_ci tep_unregister_event_handler(tool->trace.tep, -1, "osnoise", "nmi_noise", 89062306a36Sopenharmony_ci timerlat_aa_nmi_handler, tool); 89162306a36Sopenharmony_ci 89262306a36Sopenharmony_ci tep_unregister_event_handler(tool->trace.tep, -1, "osnoise", "irq_noise", 89362306a36Sopenharmony_ci timerlat_aa_irq_handler, tool); 89462306a36Sopenharmony_ci 89562306a36Sopenharmony_ci tep_unregister_event_handler(tool->trace.tep, -1, "osnoise", "softirq_noise", 89662306a36Sopenharmony_ci timerlat_aa_softirq_handler, tool); 89762306a36Sopenharmony_ci 89862306a36Sopenharmony_ci tep_unregister_event_handler(tool->trace.tep, -1, "osnoise", "thread_noise", 89962306a36Sopenharmony_ci timerlat_aa_thread_handler, tool); 90062306a36Sopenharmony_ci 90162306a36Sopenharmony_ci tep_unregister_event_handler(tool->trace.tep, -1, "ftrace", "kernel_stack", 90262306a36Sopenharmony_ci timerlat_aa_stack_handler, tool); 90362306a36Sopenharmony_ci if (!dump_tasks) 90462306a36Sopenharmony_ci return; 90562306a36Sopenharmony_ci 90662306a36Sopenharmony_ci tracefs_event_disable(tool->trace.inst, "sched", "sched_switch"); 90762306a36Sopenharmony_ci tep_unregister_event_handler(tool->trace.tep, -1, "sched", "sched_switch", 90862306a36Sopenharmony_ci timerlat_aa_sched_switch_handler, tool); 90962306a36Sopenharmony_ci 91062306a36Sopenharmony_ci tracefs_event_disable(tool->trace.inst, "workqueue", "workqueue_execute_start"); 91162306a36Sopenharmony_ci tep_unregister_event_handler(tool->trace.tep, -1, "workqueue", "workqueue_execute_start", 91262306a36Sopenharmony_ci timerlat_aa_kworker_start_handler, tool); 91362306a36Sopenharmony_ci} 91462306a36Sopenharmony_ci 91562306a36Sopenharmony_ci/* 91662306a36Sopenharmony_ci * timerlat_aa_register_events - Register events used in the auto-analysis 91762306a36Sopenharmony_ci * 91862306a36Sopenharmony_ci * Returns 0 on success, -1 otherwise. 91962306a36Sopenharmony_ci */ 92062306a36Sopenharmony_cistatic int timerlat_aa_register_events(struct osnoise_tool *tool, int dump_tasks) 92162306a36Sopenharmony_ci{ 92262306a36Sopenharmony_ci int retval; 92362306a36Sopenharmony_ci 92462306a36Sopenharmony_ci tep_register_event_handler(tool->trace.tep, -1, "ftrace", "timerlat", 92562306a36Sopenharmony_ci timerlat_aa_handler, tool); 92662306a36Sopenharmony_ci 92762306a36Sopenharmony_ci 92862306a36Sopenharmony_ci /* 92962306a36Sopenharmony_ci * register auto-analysis handlers. 93062306a36Sopenharmony_ci */ 93162306a36Sopenharmony_ci retval = tracefs_event_enable(tool->trace.inst, "osnoise", NULL); 93262306a36Sopenharmony_ci if (retval < 0 && !errno) { 93362306a36Sopenharmony_ci err_msg("Could not find osnoise events\n"); 93462306a36Sopenharmony_ci goto out_err; 93562306a36Sopenharmony_ci } 93662306a36Sopenharmony_ci 93762306a36Sopenharmony_ci tep_register_event_handler(tool->trace.tep, -1, "osnoise", "nmi_noise", 93862306a36Sopenharmony_ci timerlat_aa_nmi_handler, tool); 93962306a36Sopenharmony_ci 94062306a36Sopenharmony_ci tep_register_event_handler(tool->trace.tep, -1, "osnoise", "irq_noise", 94162306a36Sopenharmony_ci timerlat_aa_irq_handler, tool); 94262306a36Sopenharmony_ci 94362306a36Sopenharmony_ci tep_register_event_handler(tool->trace.tep, -1, "osnoise", "softirq_noise", 94462306a36Sopenharmony_ci timerlat_aa_softirq_handler, tool); 94562306a36Sopenharmony_ci 94662306a36Sopenharmony_ci tep_register_event_handler(tool->trace.tep, -1, "osnoise", "thread_noise", 94762306a36Sopenharmony_ci timerlat_aa_thread_handler, tool); 94862306a36Sopenharmony_ci 94962306a36Sopenharmony_ci tep_register_event_handler(tool->trace.tep, -1, "ftrace", "kernel_stack", 95062306a36Sopenharmony_ci timerlat_aa_stack_handler, tool); 95162306a36Sopenharmony_ci 95262306a36Sopenharmony_ci if (!dump_tasks) 95362306a36Sopenharmony_ci return 0; 95462306a36Sopenharmony_ci 95562306a36Sopenharmony_ci /* 95662306a36Sopenharmony_ci * Dump task events. 95762306a36Sopenharmony_ci */ 95862306a36Sopenharmony_ci retval = tracefs_event_enable(tool->trace.inst, "sched", "sched_switch"); 95962306a36Sopenharmony_ci if (retval < 0 && !errno) { 96062306a36Sopenharmony_ci err_msg("Could not find sched_switch\n"); 96162306a36Sopenharmony_ci goto out_err; 96262306a36Sopenharmony_ci } 96362306a36Sopenharmony_ci 96462306a36Sopenharmony_ci tep_register_event_handler(tool->trace.tep, -1, "sched", "sched_switch", 96562306a36Sopenharmony_ci timerlat_aa_sched_switch_handler, tool); 96662306a36Sopenharmony_ci 96762306a36Sopenharmony_ci retval = tracefs_event_enable(tool->trace.inst, "workqueue", "workqueue_execute_start"); 96862306a36Sopenharmony_ci if (retval < 0 && !errno) { 96962306a36Sopenharmony_ci err_msg("Could not find workqueue_execute_start\n"); 97062306a36Sopenharmony_ci goto out_err; 97162306a36Sopenharmony_ci } 97262306a36Sopenharmony_ci 97362306a36Sopenharmony_ci tep_register_event_handler(tool->trace.tep, -1, "workqueue", "workqueue_execute_start", 97462306a36Sopenharmony_ci timerlat_aa_kworker_start_handler, tool); 97562306a36Sopenharmony_ci 97662306a36Sopenharmony_ci return 0; 97762306a36Sopenharmony_ci 97862306a36Sopenharmony_ciout_err: 97962306a36Sopenharmony_ci timerlat_aa_unregister_events(tool, dump_tasks); 98062306a36Sopenharmony_ci return -1; 98162306a36Sopenharmony_ci} 98262306a36Sopenharmony_ci 98362306a36Sopenharmony_ci/** 98462306a36Sopenharmony_ci * timerlat_aa_destroy - Destroy timerlat auto-analysis 98562306a36Sopenharmony_ci */ 98662306a36Sopenharmony_civoid timerlat_aa_destroy(void) 98762306a36Sopenharmony_ci{ 98862306a36Sopenharmony_ci struct timerlat_aa_context *taa_ctx = timerlat_aa_get_ctx(); 98962306a36Sopenharmony_ci 99062306a36Sopenharmony_ci if (!taa_ctx) 99162306a36Sopenharmony_ci return; 99262306a36Sopenharmony_ci 99362306a36Sopenharmony_ci if (!taa_ctx->taa_data) 99462306a36Sopenharmony_ci goto out_ctx; 99562306a36Sopenharmony_ci 99662306a36Sopenharmony_ci timerlat_aa_unregister_events(taa_ctx->tool, taa_ctx->dump_tasks); 99762306a36Sopenharmony_ci timerlat_aa_destroy_seqs(taa_ctx); 99862306a36Sopenharmony_ci free(taa_ctx->taa_data); 99962306a36Sopenharmony_ciout_ctx: 100062306a36Sopenharmony_ci free(taa_ctx); 100162306a36Sopenharmony_ci} 100262306a36Sopenharmony_ci 100362306a36Sopenharmony_ci/** 100462306a36Sopenharmony_ci * timerlat_aa_init - Initialize timerlat auto-analysis 100562306a36Sopenharmony_ci * 100662306a36Sopenharmony_ci * Returns 0 on success, -1 otherwise. 100762306a36Sopenharmony_ci */ 100862306a36Sopenharmony_ciint timerlat_aa_init(struct osnoise_tool *tool, int dump_tasks) 100962306a36Sopenharmony_ci{ 101062306a36Sopenharmony_ci int nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 101162306a36Sopenharmony_ci struct timerlat_aa_context *taa_ctx; 101262306a36Sopenharmony_ci int retval; 101362306a36Sopenharmony_ci 101462306a36Sopenharmony_ci taa_ctx = calloc(1, sizeof(*taa_ctx)); 101562306a36Sopenharmony_ci if (!taa_ctx) 101662306a36Sopenharmony_ci return -1; 101762306a36Sopenharmony_ci 101862306a36Sopenharmony_ci __timerlat_aa_ctx = taa_ctx; 101962306a36Sopenharmony_ci 102062306a36Sopenharmony_ci taa_ctx->nr_cpus = nr_cpus; 102162306a36Sopenharmony_ci taa_ctx->tool = tool; 102262306a36Sopenharmony_ci taa_ctx->dump_tasks = dump_tasks; 102362306a36Sopenharmony_ci 102462306a36Sopenharmony_ci taa_ctx->taa_data = calloc(nr_cpus, sizeof(*taa_ctx->taa_data)); 102562306a36Sopenharmony_ci if (!taa_ctx->taa_data) 102662306a36Sopenharmony_ci goto out_err; 102762306a36Sopenharmony_ci 102862306a36Sopenharmony_ci retval = timerlat_aa_init_seqs(taa_ctx); 102962306a36Sopenharmony_ci if (retval) 103062306a36Sopenharmony_ci goto out_err; 103162306a36Sopenharmony_ci 103262306a36Sopenharmony_ci retval = timerlat_aa_register_events(tool, dump_tasks); 103362306a36Sopenharmony_ci if (retval) 103462306a36Sopenharmony_ci goto out_err; 103562306a36Sopenharmony_ci 103662306a36Sopenharmony_ci return 0; 103762306a36Sopenharmony_ci 103862306a36Sopenharmony_ciout_err: 103962306a36Sopenharmony_ci timerlat_aa_destroy(); 104062306a36Sopenharmony_ci return -1; 104162306a36Sopenharmony_ci} 1042