#include "util.h"
#include "build-id.h"
#include "hist.h"
#include "session.h"
#include "sort.h"
#include <math.h>
struct callchain_param callchain_param = {
.mode = CHAIN_GRAPH_REL,
.min_percent = 0.5
};
static void hist_entry__add_cpumode_period(struct hist_entry *self,
unsigned int cpumode, u64 period)
{
switch (cpumode) {
case PERF_RECORD_MISC_KERNEL:
self->period_sys += period;
break;
case PERF_RECORD_MISC_USER:
self->period_us += period;
break;
case PERF_RECORD_MISC_GUEST_KERNEL:
self->period_guest_sys += period;
break;
case PERF_RECORD_MISC_GUEST_USER:
self->period_guest_us += period;
break;
default:
break;
}
}
/*
* histogram, sorted on item, collects periods
*/
static struct hist_entry *hist_entry__new(struct hist_entry *template)
{
size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_node) : 0;
struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
if (self != NULL) {
*self = *template;
self->nr_events = 1;
if (symbol_conf.use_callchain)
callchain_init(self->callchain);
}
return self;
}
static void hists__inc_nr_entries(struct hists *self, struct hist_entry *entry)
{
if (entry->ms.sym && self->max_sym_namelen < entry->ms.sym->namelen)
self->max_sym_namelen = entry->ms.sym->namelen;
++self->nr_entries;
}
struct hist_entry *__hists__add_entry(struct hists *self,
struct addr_location *al,
struct symbol *sym_parent, u64 period)
{
struct rb_node **p = &self->entries.rb_node;
struct rb_node *parent = NULL;
struct hist_entry *he;
struct hist_entry entry = {
.thread = al->thread,
.ms = {
.map = al->map,
.sym = al->sym,
},
.ip = al->addr,
.level = al->level,
.period = period,
.parent = sym_parent,
};
int cmp;
while (*p != NULL) {
parent = *p;
he = rb_entry(parent, struct hist_entry, rb_node);
cmp = hist_entry__cmp(&entry, he);
if (!cmp) {
he->period += period;
++he->nr_events;
goto out;
}
if (cmp < 0)
p = &(*p)->rb_left;
else
p = &(*p)->rb_right;
}
he = hist_entry__new(&entry);
if (!he)
return NULL;
rb_link_node(&he->rb_node, parent, p);
rb_insert_color(&he->rb_node, &self->entries);
hists__inc_nr_entries(self, he);
out:
hist_entry__add_cpumode_period(he, al->cpumode, period);
return he;
}
int64_t
hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
{
struct sort_entry *se;
int64_t cmp = 0;
list_for_each_entry(se, &hist_entry__sort_list, list)