// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <sched.h>
#include <pthread.h>
#include "timerlat.h"
#include "timerlat_aa.h"
#include "timerlat_bpf.h"
#include "cli.h"
#include "common.h"
struct timerlat_hist_cpu {
int *irq;
int *thread;
int *user;
unsigned long long irq_count;
unsigned long long thread_count;
unsigned long long user_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;
unsigned long long min_user;
unsigned long long sum_user;
unsigned long long max_user;
};
struct timerlat_hist_data {
struct timerlat_hist_cpu *hist;
int entries;
int bucket_size;
};
/*
* 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 < nr_cpus; cpu++) {
if (data->hist[cpu].irq)
free(data->hist[cpu].irq);
if (data->hist[cpu].thread)
free(data->hist[cpu].thread);
if (data->hist[cpu].user)
free(data->hist[cpu].user);
}
/* one set of histograms per CPU */
if (data->hist)
free(data->hist);
}
static void timerlat_free_histogram_tool(struct osnoise_tool *tool)
{
timerlat_free_histogram(tool->data);
timerlat_free(tool);
}
/*
* timerlat_alloc_histogram - alloc runtime data
*/
static struct timerlat_hist_data
*timerlat_alloc_histogram(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;
/* 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;
data->hist[cpu].user = calloc(1, sizeof(*data->hist->user) * (entries + 1));
if (!data->hist[cpu].user)
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;
data->hist[cpu].min_user = ~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 context,
unsigned long long latency)
{
struct timerlat_params *params = to_timerlat_params(tool->params);
struct timerlat_hist_data *data = tool->data;
int entries = data->entries;
int bucket;
int *hist;
if (params->common.output_divisor)
latency = latency / params->common.output_divisor;
bucket = latency / data->bucket_size;
if (!context) {
hist = data->hist[cpu].irq;
data->hist[cpu].irq_count++;
update_min(&data->hist[cpu].min_irq, &latency);
update_sum(&data->hist[cpu].sum_irq, &latency);
update_max(&data->hist[cpu].max_irq, &latency);
} else if (context == 1) {
hist = data->hist[cpu].thread;
data->hist[cpu].thread_count++;
update_min(&data->hist[cpu].min_thread, &latency);
update_sum(&data->hist[cpu].sum_thread, &latency);
update_max(&data->hist[cpu].max_thread, &latency);
} else { /* user */
hist = data->hist[cpu].user;
data->hist[cpu].user_count++;
update_min(&