1 // SPDX-License-Identifier: GPL-2.0
3 * Compare and figure out the top N hottest streams
4 * Copyright (c) 2020, Intel Corporation.
10 #include <linux/zalloc.h>
17 static void evsel_streams__delete(struct evsel_streams *es, int nr_evsel)
19 for (int i = 0; i < nr_evsel; i++)
20 zfree(&es[i].streams);
25 void evlist_streams__delete(struct evlist_streams *els)
27 evsel_streams__delete(els->ev_streams, els->nr_evsel);
31 static struct evlist_streams *evlist_streams__new(int nr_evsel,
34 struct evlist_streams *els;
35 struct evsel_streams *es;
37 els = zalloc(sizeof(*els));
41 es = calloc(nr_evsel, sizeof(struct evsel_streams));
47 for (int i = 0; i < nr_evsel; i++) {
48 struct evsel_streams *s = &es[i];
50 s->streams = calloc(nr_streams_max, sizeof(struct stream));
54 s->nr_streams_max = nr_streams_max;
58 els->nr_evsel = nr_evsel;
62 evsel_streams__delete(es, nr_evsel);
67 * The cnodes with high hit number are hot callchains.
69 static void evsel_streams__set_hot_cnode(struct evsel_streams *es,
70 struct callchain_node *cnode)
75 if (es->nr_streams < es->nr_streams_max) {
77 es->streams[i].cnode = cnode;
83 * Considering a few number of hot streams, only use simple
84 * way to find the cnode with smallest hit number and replace.
86 hit = (es->streams[0].cnode)->hit;
87 for (i = 1; i < es->nr_streams; i++) {
88 if ((es->streams[i].cnode)->hit < hit) {
89 hit = (es->streams[i].cnode)->hit;
95 es->streams[idx].cnode = cnode;
98 static void update_hot_callchain(struct hist_entry *he,
99 struct evsel_streams *es)
101 struct rb_root *root = &he->sorted_chain;
102 struct rb_node *rb_node = rb_first(root);
103 struct callchain_node *cnode;
106 cnode = rb_entry(rb_node, struct callchain_node, rb_node);
107 evsel_streams__set_hot_cnode(es, cnode);
108 rb_node = rb_next(rb_node);
112 static void init_hot_callchain(struct hists *hists, struct evsel_streams *es)
114 struct rb_node *next = rb_first_cached(&hists->entries);
117 struct hist_entry *he;
119 he = rb_entry(next, struct hist_entry, rb_node);
120 update_hot_callchain(he, es);
121 next = rb_next(&he->rb_node);
124 es->streams_hits = callchain_total_hits(hists);
127 static int evlist__init_callchain_streams(struct evlist *evlist,
128 struct evlist_streams *els)
130 struct evsel_streams *es = els->ev_streams;
134 BUG_ON(els->nr_evsel < evlist->core.nr_entries);
136 evlist__for_each_entry(evlist, pos) {
137 struct hists *hists = evsel__hists(pos);
139 hists__output_resort(hists, NULL);
140 init_hot_callchain(hists, &es[i]);
148 struct evlist_streams *evlist__create_streams(struct evlist *evlist,
151 int nr_evsel = evlist->core.nr_entries, ret = -1;
152 struct evlist_streams *els = evlist_streams__new(nr_evsel,
158 ret = evlist__init_callchain_streams(evlist, els);
160 evlist_streams__delete(els);
167 struct evsel_streams *evsel_streams__entry(struct evlist_streams *els,
168 const struct evsel *evsel)
170 struct evsel_streams *es = els->ev_streams;
172 for (int i = 0; i < els->nr_evsel; i++) {
173 if (es[i].evsel == evsel)
180 static struct stream *stream__callchain_match(struct stream *base_stream,
181 struct evsel_streams *es_pair)
183 for (int i = 0; i < es_pair->nr_streams; i++) {
184 struct stream *pair_stream = &es_pair->streams[i];
186 if (callchain_cnode_matched(base_stream->cnode,
187 pair_stream->cnode)) {
195 static struct stream *stream__match(struct stream *base_stream,
196 struct evsel_streams *es_pair)
198 return stream__callchain_match(base_stream, es_pair);
201 static void stream__link(struct stream *base_stream, struct stream *pair_stream)
203 base_stream->pair_cnode = pair_stream->cnode;
204 pair_stream->pair_cnode = base_stream->cnode;
207 void evsel_streams__match(struct evsel_streams *es_base,
208 struct evsel_streams *es_pair)
210 for (int i = 0; i < es_base->nr_streams; i++) {
211 struct stream *base_stream = &es_base->streams[i];
212 struct stream *pair_stream;
214 pair_stream = stream__match(base_stream, es_pair);
216 stream__link(base_stream, pair_stream);
220 static void print_callchain_pair(struct stream *base_stream, int idx,
221 struct evsel_streams *es_base,
222 struct evsel_streams *es_pair)
224 struct callchain_node *base_cnode = base_stream->cnode;
225 struct callchain_node *pair_cnode = base_stream->pair_cnode;
226 struct callchain_list *base_chain, *pair_chain;
227 char buf1[512], buf2[512], cbuf1[256], cbuf2[256];
231 printf("\nhot chain pair %d:\n", idx);
233 pct = (double)base_cnode->hit / (double)es_base->streams_hits;
234 scnprintf(buf1, sizeof(buf1), "cycles: %ld, hits: %.2f%%",
235 callchain_avg_cycles(base_cnode), pct * 100.0);
237 pct = (double)pair_cnode->hit / (double)es_pair->streams_hits;
238 scnprintf(buf2, sizeof(buf2), "cycles: %ld, hits: %.2f%%",
239 callchain_avg_cycles(pair_cnode), pct * 100.0);
241 printf("%35s\t%35s\n", buf1, buf2);
243 printf("%35s\t%35s\n",
244 "---------------------------",
245 "--------------------------");
247 pair_chain = list_first_entry(&pair_cnode->val,
248 struct callchain_list,
251 list_for_each_entry(base_chain, &base_cnode->val, list) {
252 if (&pair_chain->list == &pair_cnode->val)
255 s1 = callchain_list__sym_name(base_chain, cbuf1, sizeof(cbuf1),
257 s2 = callchain_list__sym_name(pair_chain, cbuf2, sizeof(cbuf2),
260 scnprintf(buf1, sizeof(buf1), "%35s\t%35s", s1, s2);
261 printf("%s\n", buf1);
262 pair_chain = list_next_entry(pair_chain, list);
266 static void print_stream_callchain(struct stream *stream, int idx,
267 struct evsel_streams *es, bool pair)
269 struct callchain_node *cnode = stream->cnode;
270 struct callchain_list *chain;
271 char buf[512], cbuf[256], *s;
274 printf("\nhot chain %d:\n", idx);
276 pct = (double)cnode->hit / (double)es->streams_hits;
277 scnprintf(buf, sizeof(buf), "cycles: %ld, hits: %.2f%%",
278 callchain_avg_cycles(cnode), pct * 100.0);
281 printf("%35s\t%35s\n", "", buf);
282 printf("%35s\t%35s\n",
283 "", "--------------------------");
285 printf("%35s\n", buf);
286 printf("%35s\n", "--------------------------");
289 list_for_each_entry(chain, &cnode->val, list) {
290 s = callchain_list__sym_name(chain, cbuf, sizeof(cbuf), false);
293 scnprintf(buf, sizeof(buf), "%35s\t%35s", "", s);
295 scnprintf(buf, sizeof(buf), "%35s", s);
301 static void callchain_streams_report(struct evsel_streams *es_base,
302 struct evsel_streams *es_pair)
304 struct stream *base_stream;
307 printf("[ Matched hot streams ]\n");
308 for (i = 0; i < es_base->nr_streams; i++) {
309 base_stream = &es_base->streams[i];
310 if (base_stream->pair_cnode) {
311 print_callchain_pair(base_stream, ++idx,
317 printf("\n[ Hot streams in old perf data only ]\n");
318 for (i = 0; i < es_base->nr_streams; i++) {
319 base_stream = &es_base->streams[i];
320 if (!base_stream->pair_cnode) {
321 print_stream_callchain(base_stream, ++idx,
327 printf("\n[ Hot streams in new perf data only ]\n");
328 for (i = 0; i < es_pair->nr_streams; i++) {
329 base_stream = &es_pair->streams[i];
330 if (!base_stream->pair_cnode) {
331 print_stream_callchain(base_stream, ++idx,
337 void evsel_streams__report(struct evsel_streams *es_base,
338 struct evsel_streams *es_pair)
340 return callchain_streams_report(es_base, es_pair);