// 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 "utils.h"
#include "osnoise.h"
#include "timerlat.h"
#include "timerlat_aa.h"
#include "timerlat_u.h"
struct timerlat_top_params {
char *cpus;
cpu_set_t monitored_cpus;
char *trace_output;
char *cgroup_name;
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 quiet;
int set_sched;
int dma_latency;
int no_aa;
int aa_only;
int dump_tasks;
int cgroup;
int hk_cpus;
int user_top;
int user_workload;
int kernel_workload;
int pretty_output;
int warmup;
int buffer_size;
cpu_set_t hk_cpu_set;
struct sched_attr sched_param;
struct trace_events *events;
};
struct timerlat_top_cpu {
int irq_count;
int thread_count;
int 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);
}
/*
* 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->