0001
0002
0003
0004
0005
0006
0007
0008 #include <inttypes.h>
0009 #include <stdlib.h>
0010 #include <linux/zalloc.h>
0011 #include "debug.h"
0012 #include "hist.h"
0013 #include "sort.h"
0014 #include "stream.h"
0015 #include "evlist.h"
0016
0017 static void evsel_streams__delete(struct evsel_streams *es, int nr_evsel)
0018 {
0019 for (int i = 0; i < nr_evsel; i++)
0020 zfree(&es[i].streams);
0021
0022 free(es);
0023 }
0024
0025 void evlist_streams__delete(struct evlist_streams *els)
0026 {
0027 evsel_streams__delete(els->ev_streams, els->nr_evsel);
0028 free(els);
0029 }
0030
0031 static struct evlist_streams *evlist_streams__new(int nr_evsel,
0032 int nr_streams_max)
0033 {
0034 struct evlist_streams *els;
0035 struct evsel_streams *es;
0036
0037 els = zalloc(sizeof(*els));
0038 if (!els)
0039 return NULL;
0040
0041 es = calloc(nr_evsel, sizeof(struct evsel_streams));
0042 if (!es) {
0043 free(els);
0044 return NULL;
0045 }
0046
0047 for (int i = 0; i < nr_evsel; i++) {
0048 struct evsel_streams *s = &es[i];
0049
0050 s->streams = calloc(nr_streams_max, sizeof(struct stream));
0051 if (!s->streams)
0052 goto err;
0053
0054 s->nr_streams_max = nr_streams_max;
0055 s->evsel_idx = -1;
0056 }
0057
0058 els->ev_streams = es;
0059 els->nr_evsel = nr_evsel;
0060 return els;
0061
0062 err:
0063 evsel_streams__delete(es, nr_evsel);
0064 return NULL;
0065 }
0066
0067
0068
0069
0070 static void evsel_streams__set_hot_cnode(struct evsel_streams *es,
0071 struct callchain_node *cnode)
0072 {
0073 int i, idx = 0;
0074 u64 hit;
0075
0076 if (es->nr_streams < es->nr_streams_max) {
0077 i = es->nr_streams;
0078 es->streams[i].cnode = cnode;
0079 es->nr_streams++;
0080 return;
0081 }
0082
0083
0084
0085
0086
0087 hit = (es->streams[0].cnode)->hit;
0088 for (i = 1; i < es->nr_streams; i++) {
0089 if ((es->streams[i].cnode)->hit < hit) {
0090 hit = (es->streams[i].cnode)->hit;
0091 idx = i;
0092 }
0093 }
0094
0095 if (cnode->hit > hit)
0096 es->streams[idx].cnode = cnode;
0097 }
0098
0099 static void update_hot_callchain(struct hist_entry *he,
0100 struct evsel_streams *es)
0101 {
0102 struct rb_root *root = &he->sorted_chain;
0103 struct rb_node *rb_node = rb_first(root);
0104 struct callchain_node *cnode;
0105
0106 while (rb_node) {
0107 cnode = rb_entry(rb_node, struct callchain_node, rb_node);
0108 evsel_streams__set_hot_cnode(es, cnode);
0109 rb_node = rb_next(rb_node);
0110 }
0111 }
0112
0113 static void init_hot_callchain(struct hists *hists, struct evsel_streams *es)
0114 {
0115 struct rb_node *next = rb_first_cached(&hists->entries);
0116
0117 while (next) {
0118 struct hist_entry *he;
0119
0120 he = rb_entry(next, struct hist_entry, rb_node);
0121 update_hot_callchain(he, es);
0122 next = rb_next(&he->rb_node);
0123 }
0124
0125 es->streams_hits = callchain_total_hits(hists);
0126 }
0127
0128 static int evlist__init_callchain_streams(struct evlist *evlist,
0129 struct evlist_streams *els)
0130 {
0131 struct evsel_streams *es = els->ev_streams;
0132 struct evsel *pos;
0133 int i = 0;
0134
0135 BUG_ON(els->nr_evsel < evlist->core.nr_entries);
0136
0137 evlist__for_each_entry(evlist, pos) {
0138 struct hists *hists = evsel__hists(pos);
0139
0140 hists__output_resort(hists, NULL);
0141 init_hot_callchain(hists, &es[i]);
0142 es[i].evsel_idx = pos->core.idx;
0143 i++;
0144 }
0145
0146 return 0;
0147 }
0148
0149 struct evlist_streams *evlist__create_streams(struct evlist *evlist,
0150 int nr_streams_max)
0151 {
0152 int nr_evsel = evlist->core.nr_entries, ret = -1;
0153 struct evlist_streams *els = evlist_streams__new(nr_evsel,
0154 nr_streams_max);
0155
0156 if (!els)
0157 return NULL;
0158
0159 ret = evlist__init_callchain_streams(evlist, els);
0160 if (ret) {
0161 evlist_streams__delete(els);
0162 return NULL;
0163 }
0164
0165 return els;
0166 }
0167
0168 struct evsel_streams *evsel_streams__entry(struct evlist_streams *els,
0169 int evsel_idx)
0170 {
0171 struct evsel_streams *es = els->ev_streams;
0172
0173 for (int i = 0; i < els->nr_evsel; i++) {
0174 if (es[i].evsel_idx == evsel_idx)
0175 return &es[i];
0176 }
0177
0178 return NULL;
0179 }
0180
0181 static struct stream *stream__callchain_match(struct stream *base_stream,
0182 struct evsel_streams *es_pair)
0183 {
0184 for (int i = 0; i < es_pair->nr_streams; i++) {
0185 struct stream *pair_stream = &es_pair->streams[i];
0186
0187 if (callchain_cnode_matched(base_stream->cnode,
0188 pair_stream->cnode)) {
0189 return pair_stream;
0190 }
0191 }
0192
0193 return NULL;
0194 }
0195
0196 static struct stream *stream__match(struct stream *base_stream,
0197 struct evsel_streams *es_pair)
0198 {
0199 return stream__callchain_match(base_stream, es_pair);
0200 }
0201
0202 static void stream__link(struct stream *base_stream, struct stream *pair_stream)
0203 {
0204 base_stream->pair_cnode = pair_stream->cnode;
0205 pair_stream->pair_cnode = base_stream->cnode;
0206 }
0207
0208 void evsel_streams__match(struct evsel_streams *es_base,
0209 struct evsel_streams *es_pair)
0210 {
0211 for (int i = 0; i < es_base->nr_streams; i++) {
0212 struct stream *base_stream = &es_base->streams[i];
0213 struct stream *pair_stream;
0214
0215 pair_stream = stream__match(base_stream, es_pair);
0216 if (pair_stream)
0217 stream__link(base_stream, pair_stream);
0218 }
0219 }
0220
0221 static void print_callchain_pair(struct stream *base_stream, int idx,
0222 struct evsel_streams *es_base,
0223 struct evsel_streams *es_pair)
0224 {
0225 struct callchain_node *base_cnode = base_stream->cnode;
0226 struct callchain_node *pair_cnode = base_stream->pair_cnode;
0227 struct callchain_list *base_chain, *pair_chain;
0228 char buf1[512], buf2[512], cbuf1[256], cbuf2[256];
0229 char *s1, *s2;
0230 double pct;
0231
0232 printf("\nhot chain pair %d:\n", idx);
0233
0234 pct = (double)base_cnode->hit / (double)es_base->streams_hits;
0235 scnprintf(buf1, sizeof(buf1), "cycles: %ld, hits: %.2f%%",
0236 callchain_avg_cycles(base_cnode), pct * 100.0);
0237
0238 pct = (double)pair_cnode->hit / (double)es_pair->streams_hits;
0239 scnprintf(buf2, sizeof(buf2), "cycles: %ld, hits: %.2f%%",
0240 callchain_avg_cycles(pair_cnode), pct * 100.0);
0241
0242 printf("%35s\t%35s\n", buf1, buf2);
0243
0244 printf("%35s\t%35s\n",
0245 "---------------------------",
0246 "--------------------------");
0247
0248 pair_chain = list_first_entry(&pair_cnode->val,
0249 struct callchain_list,
0250 list);
0251
0252 list_for_each_entry(base_chain, &base_cnode->val, list) {
0253 if (&pair_chain->list == &pair_cnode->val)
0254 return;
0255
0256 s1 = callchain_list__sym_name(base_chain, cbuf1, sizeof(cbuf1),
0257 false);
0258 s2 = callchain_list__sym_name(pair_chain, cbuf2, sizeof(cbuf2),
0259 false);
0260
0261 scnprintf(buf1, sizeof(buf1), "%35s\t%35s", s1, s2);
0262 printf("%s\n", buf1);
0263 pair_chain = list_next_entry(pair_chain, list);
0264 }
0265 }
0266
0267 static void print_stream_callchain(struct stream *stream, int idx,
0268 struct evsel_streams *es, bool pair)
0269 {
0270 struct callchain_node *cnode = stream->cnode;
0271 struct callchain_list *chain;
0272 char buf[512], cbuf[256], *s;
0273 double pct;
0274
0275 printf("\nhot chain %d:\n", idx);
0276
0277 pct = (double)cnode->hit / (double)es->streams_hits;
0278 scnprintf(buf, sizeof(buf), "cycles: %ld, hits: %.2f%%",
0279 callchain_avg_cycles(cnode), pct * 100.0);
0280
0281 if (pair) {
0282 printf("%35s\t%35s\n", "", buf);
0283 printf("%35s\t%35s\n",
0284 "", "--------------------------");
0285 } else {
0286 printf("%35s\n", buf);
0287 printf("%35s\n", "--------------------------");
0288 }
0289
0290 list_for_each_entry(chain, &cnode->val, list) {
0291 s = callchain_list__sym_name(chain, cbuf, sizeof(cbuf), false);
0292
0293 if (pair)
0294 scnprintf(buf, sizeof(buf), "%35s\t%35s", "", s);
0295 else
0296 scnprintf(buf, sizeof(buf), "%35s", s);
0297
0298 printf("%s\n", buf);
0299 }
0300 }
0301
0302 static void callchain_streams_report(struct evsel_streams *es_base,
0303 struct evsel_streams *es_pair)
0304 {
0305 struct stream *base_stream;
0306 int i, idx = 0;
0307
0308 printf("[ Matched hot streams ]\n");
0309 for (i = 0; i < es_base->nr_streams; i++) {
0310 base_stream = &es_base->streams[i];
0311 if (base_stream->pair_cnode) {
0312 print_callchain_pair(base_stream, ++idx,
0313 es_base, es_pair);
0314 }
0315 }
0316
0317 idx = 0;
0318 printf("\n[ Hot streams in old perf data only ]\n");
0319 for (i = 0; i < es_base->nr_streams; i++) {
0320 base_stream = &es_base->streams[i];
0321 if (!base_stream->pair_cnode) {
0322 print_stream_callchain(base_stream, ++idx,
0323 es_base, false);
0324 }
0325 }
0326
0327 idx = 0;
0328 printf("\n[ Hot streams in new perf data only ]\n");
0329 for (i = 0; i < es_pair->nr_streams; i++) {
0330 base_stream = &es_pair->streams[i];
0331 if (!base_stream->pair_cnode) {
0332 print_stream_callchain(base_stream, ++idx,
0333 es_pair, true);
0334 }
0335 }
0336 }
0337
0338 void evsel_streams__report(struct evsel_streams *es_base,
0339 struct evsel_streams *es_pair)
0340 {
0341 return callchain_streams_report(es_base, es_pair);
0342 }