1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
// SPDX-License-Identifier: GPL-2.0
/*
* Compare and figure out the top N hottest streams
* Copyright (c) 2020, Intel Corporation.
* Author: Jin Yao
*/
#include <inttypes.h>
#include <stdlib.h>
#include <linux/zalloc.h>
#include "debug.h"
#include "hist.h"
#include "sort.h"
#include "stream.h"
#include "evlist.h"
static void evsel_streams__delete(struct evsel_streams *es, int nr_evsel)
{
for (int i = 0; i < nr_evsel; i++)
zfree(&es[i].streams);
free(es);
}
void evlist_streams__delete(struct evlist_streams *els)
{
evsel_streams__delete(els->ev_streams, els->nr_evsel);
free(els);
}
static struct evlist_streams *evlist_streams__new(int nr_evsel,
int nr_streams_max)
{
struct evlist_streams *els;
struct evsel_streams *es;
els = zalloc(sizeof(*els));
if (!els)
return NULL;
es = calloc(nr_evsel, sizeof(struct evsel_streams));
if (!es) {
free(els);
return NULL;
}
for (int i = 0; i < nr_evsel; i++) {
struct evsel_streams *s = &es[i];
s->streams = calloc(nr_streams_max, sizeof(struct stream));
if (!s->streams)
goto err;
s->nr_streams_max = nr_streams_max;
s->evsel_idx = -1;
}
els->ev_streams = es;
els->nr_evsel = nr_evsel;
return els;
err:
evsel_streams__delete(es, nr_evsel);
return NULL;
}
/*
* The cnodes with high hit number are hot callchains.
*/
static void evsel_streams__set_hot_cnode(struct evsel_streams *es,
struct callchain_node *cnode)
{
int i, idx = 0;
u64 hit;
if (es->nr_streams < es->nr_streams_max) {
i = es->nr_streams;
es->streams[i].cnode = cnode;
es->nr_streams++;
return;
}
/*
* Considering a few number of hot streams, only use simple
* way to find the cnode with smallest hit number and replace.
*/
hit = (es->streams[0].cnode)->hit;
for (i = 1; i < es->nr_streams; i++) {
if ((es->streams[i].cnode)->hit < hit) {
hit = (es->streams[i].cnode)->hit;
idx = i;
}
}
if (cnode->hit > hit)
es->streams[idx].cnode = cnode;
}
static void update_hot_callchain(struct hist_entry *he,
struct evsel_streams *es)
{
struct rb_root *root = &he->sorted_chain;
struct rb_node *rb_node = rb_first(root);
struct callchain_node *cnode;
while (rb_node) {
cnode = rb_entry(rb_node, struct callchain_node, rb_node);
evsel_streams__set_hot_cnode(es, cnode);
rb_node = rb_next(rb_node);
}
}
static void init_hot_callchain(struct hists *hists, struct evsel_streams *es)
{
struct rb_node *next = rb_first_cached(&hists->entries);
while (next) {
struct hist_entry *he;
he = rb_entry(next, struct hist_entry, rb_node);
update_hot_callchain(he, es);
next = rb_next(&he->rb_node);
}
}
static int evlist__init_callchain_streams(struct evlist *evlist,
struct evlist_streams *els)
{
struct evsel_streams *es = els->ev_streams;
struct evsel *pos;
int i = 0;
BUG_ON(els->nr_evsel < evlist->core.nr_entries);
evlist__for_each_entry(evlist, pos) {
struct hists *hists = evsel__hists(pos);
hists__output_resort(hists, NULL);
init_hot_callchain(hists, &es[i]);
es[i].evsel_idx = pos->idx;
i++;
}
return 0;
}
struct evlist_streams *evlist__create_streams(struct evlist *evlist,
int nr_streams_max)
{
int nr_evsel = evlist->core.nr_entries, ret = -1;
struct evlist_streams *els = evlist_streams__new(nr_evsel,
nr_streams_max);
if (!els)
return NULL;
ret = evlist__init_callchain_streams(evlist, els);
if (ret) {
evlist_streams__delete(els);
return NULL;
}
return els;
}
struct evsel_streams *evsel_streams__entry(struct evlist_streams *els,
int evsel_idx)
{
struct evsel_streams *es = els->ev_streams;
for (int i = 0; i < els->nr_evsel; i++) {
if (es[i].evsel_idx == evsel_idx)
return &es[i];
}
return NULL;
}
|