// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2023 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
*/
#include <stdlib.h>
#include <errno.h>
#include "timerlat.h"
#include <unistd.h>
enum timelat_state {
TIMERLAT_INIT = 0,
TIMERLAT_WAITING_IRQ,
TIMERLAT_WAITING_THREAD,
};
/* Used to fill spaces in the output */
static const char *spaces = " ";
#define MAX_COMM 24
/*
* Per-cpu data statistics and data.
*/
struct timerlat_aa_data {
/* Current CPU state */
int curr_state;
/* timerlat IRQ latency */
unsigned long long tlat_irq_seqnum;
unsigned long long tlat_irq_latency;
unsigned long long tlat_irq_timstamp;
/* timerlat Thread latency */
unsigned long long tlat_thread_seqnum;
unsigned long long tlat_thread_latency;
unsigned long long tlat_thread_timstamp;
/*
* Information about the thread running when the IRQ
* arrived.
*
* This can be blocking or interference, depending on the
* priority of the thread. Assuming timerlat is the highest
* prio, it is blocking. If timerlat has a lower prio, it is
* interference.
* note: "unsigned long long" because they are fetch using tep_get_field_val();
*/
unsigned long long run_thread_pid;
char run_thread_comm[MAX_COMM];
unsigned long long thread_blocking_duration;
unsigned long long max_exit_idle_latency;
/* Information about the timerlat timer irq */
unsigned long long timer_irq_start_time;
unsigned long long timer_irq_start_delay;
unsigned long long timer_irq_duration;
unsigned long long timer_exit_from_idle;
/*
* Information about the last IRQ before the timerlat irq
* arrived.
*
* If now - timestamp is <= latency, it might have influenced
* in the timerlat irq latency. Otherwise, ignore it.
*/
unsigned long long prev_irq_duration;
unsigned long long prev_irq_timstamp;
/*
* Interference sum.
*/
unsigned long long thread_nmi_sum;
unsigned long long thread_irq_sum;
unsigned long long thread_softirq_sum;
unsigned long long thread_thread_sum;
/*
* Interference task information.
*/
struct trace_seq *prev_irqs_seq;
struct trace_seq *nmi_seq;
struct trace_seq *irqs_seq;
struct trace_seq *softirqs_seq;
struct trace_seq *threads_seq;
struct trace_seq *stack_seq;
/*
* Current thread.
*/
char current_comm[MAX_COMM];
unsigned long long current_pid;
/*
* Is the system running a kworker?
*/
unsigned long long kworker;
unsigned long long kworker_func;
};
/*
* The analysis context and system wide view
*/
struct timerlat_aa_context {
int nr_cpus;
int dump_tasks;
/* per CPU data */
struct timerlat_aa_data *taa_data;
/*
* required to translate function names and register
* events.
*/
struct osnoise_tool *tool;
};
/*
* The data is stored as a local variable, but accessed via a helper function.
*
* It could be stored inside the trace context. But every access would
* require container_of() + a series of pointers. Do we need it? Not sure.
*
* For now keep it simple. If needed, store it in the tool, add the *context
* as a parameter in timerlat_aa_get_ctx() and do the magic there.
*/
static struct timerlat_aa_context *__timerlat_aa_ctx;
static struct timerlat_aa_context *timerlat_aa_get_ctx(void)
{
return __timerlat_aa_ctx;
}
/*
* timerlat_aa_get_data - Get the per-cpu data from the timerlat context
*/
static struct timerlat_aa_data
*timerlat_aa_get_data(struct timerlat_aa_context *taa_ctx, int cpu)
{
return &taa_ctx->taa_data[cpu];
}
/*
* timerlat_aa_irq_latency - Handles timerlat IRQ event
*/
static int timerlat_aa_irq_latency(struct timerlat_aa_data *taa_data,
struct trace_seq *s, struct tep_record *record,
struct tep_event *event)
{
/*
* For interference, we start now looking for things that can delay
* the thread.
*/
taa_data->curr_state = TIMERLAT_WAITING_THREAD;
taa_data->tlat_irq_timstamp = record->ts;
/*
* Zero values.
*/
taa_data->thread_nmi_sum = 0;
taa_data->thread_irq_sum = 0;
taa_data->thread_softirq_sum = 0;
taa_data->thread_thread_sum = 0;
taa_data->thread_blocking_duration = 0;
taa_data->timer_irq_start_time = 0;
taa_data->timer_irq_duration = 0;
taa_data->timer_exit_from_idle = 0;
/*
* Zero interference tasks.
*/
trace_seq_reset(taa_data->nmi_seq);
trace_seq_reset(taa_data->irqs_seq);
trace_seq_reset(taa_data->softirqs_seq);
trace_seq_reset(taa_data->threads_seq);
/* IRQ latency values */
tep_get_field_val(s, event, "timer_la