// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
*/
#define _GNU_SOURCE
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <sched.h>
#include <pthread.h>
#include "timerlat.h"
#include "timerlat_aa.h"
#include "timerlat_bpf.h"
struct timerlat_top_cpu {
unsigned long long irq_count;
unsigned long long thread_count;
unsigned long long user_count;
unsigned long long cur_irq;
unsigned long long min_irq;
unsigned long long sum_irq;
unsigned long long max_irq;
unsigned long long cur_thread;
unsigned long long min_thread;
unsigned long long sum_thread;
unsigned long long max_thread;
unsigned long long cur_user;
unsigned long long min_user;
unsigned long long sum_user;
unsigned long long max_user;
};
struct timerlat_top_data {
struct timerlat_top_cpu *cpu_data;
int nr_cpus;
};
/*
* timerlat_free_top - free runtime data
*/
static void timerlat_free_top(struct timerlat_top_data *data)
{
free(data->cpu_data);
free(data);
}
static void timerlat_free_top_tool(struct osnoise_tool *tool)
{
timerlat_free_top(tool->data);
timerlat_free(tool);
}
/*
* timerlat_alloc_histogram - alloc runtime data
*/
static struct timerlat_top_data *timerlat_alloc_top(int nr_cpus)
{
struct timerlat_top_data *data;
int cpu;
data = calloc(1, sizeof(*data));
if (!data)
return NULL;
data->nr_cpus = nr_cpus;
/* one set of histograms per CPU */
data->cpu_data = calloc(1, sizeof(*data->cpu_data) * nr_cpus);
if (!data->cpu_data)
goto cleanup;
/* set the min to max */
for (cpu = 0; cpu < nr_cpus; cpu++) {
data->cpu_data[cpu].min_irq = ~0;
data->cpu_data[cpu].min_thread = ~0;
data->cpu_data[cpu].min_user = ~0;
}
return data;
cleanup:
timerlat_free_top(data);
return NULL;
}
static void
timerlat_top_reset_sum(struct timerlat_top_cpu *summary)
{
memset(summary, 0, sizeof(*summary));
summary->min_irq = ~0;
summary->min_thread = ~0;
summary->min_user = ~0;
}
static void
timerlat_top_update_sum(struct osnoise_tool *tool, int cpu, struct timerlat_top_cpu *sum)
{
struct timerlat_top_data *data = tool->data;
struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
sum->irq_count += cpu_data->irq_count;
update_min(&sum->min_irq, &cpu_data->min_irq);
update_sum(&sum->sum_irq, &cpu_data->sum_irq);
update_max(&sum->max_irq, &cpu_data->max_irq);
sum->thread_count += cpu_data->thread_count;
update_min(&sum->min_thread, &cpu_data->min_thread);
update_sum(&sum->sum_thread, &cpu_data->sum_thread);
update_max(&sum->max_thread, &cpu_data->max_thread);
sum->user_count += cpu_data->user_count;
update_min(&sum->min_user, &cpu_data->min_user);
update_sum(&sum->sum_user, &cpu_data->sum_user);
update_max(&sum->max_user, &cpu_data->max_user);
}
/*
* timerlat_hist_update - record a new timerlat occurent on cpu, updating data
*/
static void
timerlat_top_update(struct osnoise_tool *tool, int cpu,
unsigned long long thread,
unsigned long long latency)
{
struct timerlat_params *params = to_timerlat_params(tool->params);
struct timerlat_top_data *data = tool->data;
struct timerlat_top_cpu *cpu_data = &data->cpu_data[cpu];
if (params->common.output_divisor)
latency = latency / params->common.output_divisor;
if (!thread) {
cpu_data->irq_count++;
cpu_data->cur_irq = latency;
update_min(&cpu_data->min_irq, &latency);
update_sum(&cpu_data->sum_irq, &