// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
*/
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include "utils.h"
#include "osnoise.h"
#include "timerlat.h"
struct timerlat_hist_params {
char *cpus;
char *monitored_cpus;
char *trace_output;
unsigned long long runtime;
long long stop_us;
long long stop_total_us;
long long timerlat_period_us;
long long print_stack;
int sleep_time;
int output_divisor;
int duration;
int set_sched;
int dma_latency;
struct sched_attr sched_param;
struct trace_events *events;
char no_irq;
char no_thread;
char no_header;
char no_summary;
char no_index;
char with_zeros;
int bucket_size;
int entries;
};
struct timerlat_hist_cpu {
int *irq;
int *thread;
int irq_count;
int thread_count;
unsigned long long min_irq;
unsigned long long sum_irq;
unsigned long long max_irq;
unsigned long long min_thread;
unsigned long long sum_thread;
unsigned long long max_thread;
};
struct timerlat_hist_data {
struct timerlat_hist_cpu *hist;
int entries;
int bucket_size;
int nr_cpus;
};
/*
* timerlat_free_histogram - free runtime data
*/
static void
timerlat_free_histogram(struct timerlat_hist_data *data)
{
int cpu;
/* one histogram for IRQ and one for thread, per CPU */
for (cpu = 0; cpu < data->nr_cpus; cpu++) {
if (data->hist[cpu].irq)
free(data->hist[cpu].irq);
if (data->hist[cpu].thread)
free(data->hist[cpu].thread);
}
/* one set of histograms per CPU */
if (data->hist)
free(data->hist);
free(data);
}
/*
* timerlat_alloc_histogram - alloc runtime data
*/
static struct timerlat_hist_data
*timerlat_alloc_histogram(int nr_cpus, int entries, int bucket_size)
{
struct timerlat_hist_data *data;
int cpu;
data = calloc(1, sizeof(*data));
if (!data)
return NULL;
data->entries = entries;
data->bucket_size = bucket_size;
data->nr_cpus = nr_cpus;
/* one set of histograms per CPU */
data->hist = calloc(1, sizeof(*data->hist) * nr_cpus);
if (!data->hist)
goto cleanup;
/* one histogram for IRQ and one for thread, per cpu */
for (cpu = 0; cpu < nr_cpus; cpu++) {
data->hist[cpu].irq = calloc(1, sizeof(*data->hist->irq) * (entries + 1));
if (!data->hist[cpu].irq)
goto cleanup;
data->hist[cpu].thread = calloc(1, sizeof(*data->hist->thread) * (entries + 1));
if (!data->hist[cpu].thread)
goto cleanup;
}
/* set the min to max */
for (cpu = 0; cpu < nr_cpus; cpu++) {
data->hist[cpu].min_irq = ~0;
data->hist[cpu].min_thread = ~0;
}
return data;
cleanup:
timerlat_free_histogram(data);
return NULL;
}
/*
* timerlat_hist_update - record a new timerlat occurent on cpu, updating data
*/
static void
timerlat_hist_update(struct osnoise_tool *tool, int cpu,
unsigned long long thread,
unsigned long long latency)
{
struct timerlat_hist_params *params = tool->params;
struct timerlat_hist_data *data = tool->data;
int entries = data->entries;
int bucket;
int *hist;
if (params->output_divisor)
latency = latency / params->output_divisor;
if (data->bucket_size)
bucket = latency / data->bucket_size;
if (!thread) {
hist = data->hist[cpu].irq;
data->hist[cpu].irq_count++;
update_min(&data->hist[cpu].min_irq, &latency);
update_sum(&