]> asedeno.scripts.mit.edu Git - linux.git/blob - tools/perf/util/block-info.c
Linux 5.6-rc7
[linux.git] / tools / perf / util / block-info.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdlib.h>
3 #include <string.h>
4 #include <linux/zalloc.h>
5 #include "block-info.h"
6 #include "sort.h"
7 #include "annotate.h"
8 #include "symbol.h"
9 #include "dso.h"
10 #include "map.h"
11 #include "srcline.h"
12 #include "evlist.h"
13 #include "hist.h"
14 #include "ui/browsers/hists.h"
15
16 static struct block_header_column {
17         const char *name;
18         int width;
19 } block_columns[PERF_HPP_REPORT__BLOCK_MAX_INDEX] = {
20         [PERF_HPP_REPORT__BLOCK_TOTAL_CYCLES_PCT] = {
21                 .name = "Sampled Cycles%",
22                 .width = 15,
23         },
24         [PERF_HPP_REPORT__BLOCK_LBR_CYCLES] = {
25                 .name = "Sampled Cycles",
26                 .width = 14,
27         },
28         [PERF_HPP_REPORT__BLOCK_CYCLES_PCT] = {
29                 .name = "Avg Cycles%",
30                 .width = 11,
31         },
32         [PERF_HPP_REPORT__BLOCK_AVG_CYCLES] = {
33                 .name = "Avg Cycles",
34                 .width = 10,
35         },
36         [PERF_HPP_REPORT__BLOCK_RANGE] = {
37                 .name = "[Program Block Range]",
38                 .width = 70,
39         },
40         [PERF_HPP_REPORT__BLOCK_DSO] = {
41                 .name = "Shared Object",
42                 .width = 20,
43         }
44 };
45
46 struct block_info *block_info__get(struct block_info *bi)
47 {
48         if (bi)
49                 refcount_inc(&bi->refcnt);
50         return bi;
51 }
52
53 void block_info__put(struct block_info *bi)
54 {
55         if (bi && refcount_dec_and_test(&bi->refcnt))
56                 free(bi);
57 }
58
59 struct block_info *block_info__new(void)
60 {
61         struct block_info *bi = zalloc(sizeof(*bi));
62
63         if (bi)
64                 refcount_set(&bi->refcnt, 1);
65         return bi;
66 }
67
68 int64_t block_info__cmp(struct perf_hpp_fmt *fmt __maybe_unused,
69                         struct hist_entry *left, struct hist_entry *right)
70 {
71         struct block_info *bi_l = left->block_info;
72         struct block_info *bi_r = right->block_info;
73         int cmp;
74
75         if (!bi_l->sym || !bi_r->sym) {
76                 if (!bi_l->sym && !bi_r->sym)
77                         return 0;
78                 else if (!bi_l->sym)
79                         return -1;
80                 else
81                         return 1;
82         }
83
84         if (bi_l->sym == bi_r->sym) {
85                 if (bi_l->start == bi_r->start) {
86                         if (bi_l->end == bi_r->end)
87                                 return 0;
88                         else
89                                 return (int64_t)(bi_r->end - bi_l->end);
90                 } else
91                         return (int64_t)(bi_r->start - bi_l->start);
92         } else {
93                 cmp = strcmp(bi_l->sym->name, bi_r->sym->name);
94                 return cmp;
95         }
96
97         if (bi_l->sym->start != bi_r->sym->start)
98                 return (int64_t)(bi_r->sym->start - bi_l->sym->start);
99
100         return (int64_t)(bi_r->sym->end - bi_l->sym->end);
101 }
102
103 static void init_block_info(struct block_info *bi, struct symbol *sym,
104                             struct cyc_hist *ch, int offset,
105                             u64 total_cycles)
106 {
107         bi->sym = sym;
108         bi->start = ch->start;
109         bi->end = offset;
110         bi->cycles = ch->cycles;
111         bi->cycles_aggr = ch->cycles_aggr;
112         bi->num = ch->num;
113         bi->num_aggr = ch->num_aggr;
114         bi->total_cycles = total_cycles;
115
116         memcpy(bi->cycles_spark, ch->cycles_spark,
117                NUM_SPARKS * sizeof(u64));
118 }
119
120 int block_info__process_sym(struct hist_entry *he, struct block_hist *bh,
121                             u64 *block_cycles_aggr, u64 total_cycles)
122 {
123         struct annotation *notes;
124         struct cyc_hist *ch;
125         static struct addr_location al;
126         u64 cycles = 0;
127
128         if (!he->ms.map || !he->ms.sym)
129                 return 0;
130
131         memset(&al, 0, sizeof(al));
132         al.map = he->ms.map;
133         al.sym = he->ms.sym;
134
135         notes = symbol__annotation(he->ms.sym);
136         if (!notes || !notes->src || !notes->src->cycles_hist)
137                 return 0;
138         ch = notes->src->cycles_hist;
139         for (unsigned int i = 0; i < symbol__size(he->ms.sym); i++) {
140                 if (ch[i].num_aggr) {
141                         struct block_info *bi;
142                         struct hist_entry *he_block;
143
144                         bi = block_info__new();
145                         if (!bi)
146                                 return -1;
147
148                         init_block_info(bi, he->ms.sym, &ch[i], i,
149                                         total_cycles);
150                         cycles += bi->cycles_aggr / bi->num_aggr;
151
152                         he_block = hists__add_entry_block(&bh->block_hists,
153                                                           &al, bi);
154                         if (!he_block) {
155                                 block_info__put(bi);
156                                 return -1;
157                         }
158                 }
159         }
160
161         if (block_cycles_aggr)
162                 *block_cycles_aggr += cycles;
163
164         return 0;
165 }
166
167 static int block_column_header(struct perf_hpp_fmt *fmt,
168                                struct perf_hpp *hpp,
169                                struct hists *hists __maybe_unused,
170                                int line __maybe_unused,
171                                int *span __maybe_unused)
172 {
173         struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
174
175         return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
176                          block_fmt->header);
177 }
178
179 static int block_column_width(struct perf_hpp_fmt *fmt,
180                               struct perf_hpp *hpp __maybe_unused,
181                               struct hists *hists __maybe_unused)
182 {
183         struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
184
185         return block_fmt->width;
186 }
187
188 static int block_total_cycles_pct_entry(struct perf_hpp_fmt *fmt,
189                                         struct perf_hpp *hpp,
190                                         struct hist_entry *he)
191 {
192         struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
193         struct block_info *bi = he->block_info;
194         double ratio = 0.0;
195         char buf[16];
196
197         if (block_fmt->total_cycles)
198                 ratio = (double)bi->cycles / (double)block_fmt->total_cycles;
199
200         sprintf(buf, "%.2f%%", 100.0 * ratio);
201
202         return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, buf);
203 }
204
205 static int64_t block_total_cycles_pct_sort(struct perf_hpp_fmt *fmt,
206                                            struct hist_entry *left,
207                                            struct hist_entry *right)
208 {
209         struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
210         struct block_info *bi_l = left->block_info;
211         struct block_info *bi_r = right->block_info;
212         double l, r;
213
214         if (block_fmt->total_cycles) {
215                 l = ((double)bi_l->cycles /
216                         (double)block_fmt->total_cycles) * 100000.0;
217                 r = ((double)bi_r->cycles /
218                         (double)block_fmt->total_cycles) * 100000.0;
219                 return (int64_t)l - (int64_t)r;
220         }
221
222         return 0;
223 }
224
225 static void cycles_string(u64 cycles, char *buf, int size)
226 {
227         if (cycles >= 1000000)
228                 scnprintf(buf, size, "%.1fM", (double)cycles / 1000000.0);
229         else if (cycles >= 1000)
230                 scnprintf(buf, size, "%.1fK", (double)cycles / 1000.0);
231         else
232                 scnprintf(buf, size, "%1d", cycles);
233 }
234
235 static int block_cycles_lbr_entry(struct perf_hpp_fmt *fmt,
236                                   struct perf_hpp *hpp, struct hist_entry *he)
237 {
238         struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
239         struct block_info *bi = he->block_info;
240         char cycles_buf[16];
241
242         cycles_string(bi->cycles_aggr, cycles_buf, sizeof(cycles_buf));
243
244         return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
245                          cycles_buf);
246 }
247
248 static int block_cycles_pct_entry(struct perf_hpp_fmt *fmt,
249                                   struct perf_hpp *hpp, struct hist_entry *he)
250 {
251         struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
252         struct block_info *bi = he->block_info;
253         double ratio = 0.0;
254         u64 avg;
255         char buf[16];
256
257         if (block_fmt->block_cycles && bi->num_aggr) {
258                 avg = bi->cycles_aggr / bi->num_aggr;
259                 ratio = (double)avg / (double)block_fmt->block_cycles;
260         }
261
262         sprintf(buf, "%.2f%%", 100.0 * ratio);
263
264         return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, buf);
265 }
266
267 static int block_avg_cycles_entry(struct perf_hpp_fmt *fmt,
268                                   struct perf_hpp *hpp,
269                                   struct hist_entry *he)
270 {
271         struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
272         struct block_info *bi = he->block_info;
273         char cycles_buf[16];
274
275         cycles_string(bi->cycles_aggr / bi->num_aggr, cycles_buf,
276                       sizeof(cycles_buf));
277
278         return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
279                          cycles_buf);
280 }
281
282 static int block_range_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
283                              struct hist_entry *he)
284 {
285         struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
286         struct block_info *bi = he->block_info;
287         char buf[128];
288         char *start_line, *end_line;
289
290         symbol_conf.disable_add2line_warn = true;
291
292         start_line = map__srcline(he->ms.map, bi->sym->start + bi->start,
293                                   he->ms.sym);
294
295         end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
296                                 he->ms.sym);
297
298         if ((strncmp(start_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0) &&
299             (strncmp(end_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)) {
300                 scnprintf(buf, sizeof(buf), "[%s -> %s]",
301                           start_line, end_line);
302         } else {
303                 scnprintf(buf, sizeof(buf), "[%7lx -> %7lx]",
304                           bi->start, bi->end);
305         }
306
307         free_srcline(start_line);
308         free_srcline(end_line);
309
310         return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, buf);
311 }
312
313 static int block_dso_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
314                            struct hist_entry *he)
315 {
316         struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
317         struct map *map = he->ms.map;
318
319         if (map && map->dso) {
320                 return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
321                                  map->dso->short_name);
322         }
323
324         return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
325                          "[unknown]");
326 }
327
328 static void init_block_header(struct block_fmt *block_fmt)
329 {
330         struct perf_hpp_fmt *fmt = &block_fmt->fmt;
331
332         BUG_ON(block_fmt->idx >= PERF_HPP_REPORT__BLOCK_MAX_INDEX);
333
334         block_fmt->header = block_columns[block_fmt->idx].name;
335         block_fmt->width = block_columns[block_fmt->idx].width;
336
337         fmt->header = block_column_header;
338         fmt->width = block_column_width;
339 }
340
341 static void hpp_register(struct block_fmt *block_fmt, int idx,
342                          struct perf_hpp_list *hpp_list)
343 {
344         struct perf_hpp_fmt *fmt = &block_fmt->fmt;
345
346         block_fmt->idx = idx;
347         INIT_LIST_HEAD(&fmt->list);
348         INIT_LIST_HEAD(&fmt->sort_list);
349
350         switch (idx) {
351         case PERF_HPP_REPORT__BLOCK_TOTAL_CYCLES_PCT:
352                 fmt->entry = block_total_cycles_pct_entry;
353                 fmt->cmp = block_info__cmp;
354                 fmt->sort = block_total_cycles_pct_sort;
355                 break;
356         case PERF_HPP_REPORT__BLOCK_LBR_CYCLES:
357                 fmt->entry = block_cycles_lbr_entry;
358                 break;
359         case PERF_HPP_REPORT__BLOCK_CYCLES_PCT:
360                 fmt->entry = block_cycles_pct_entry;
361                 break;
362         case PERF_HPP_REPORT__BLOCK_AVG_CYCLES:
363                 fmt->entry = block_avg_cycles_entry;
364                 break;
365         case PERF_HPP_REPORT__BLOCK_RANGE:
366                 fmt->entry = block_range_entry;
367                 break;
368         case PERF_HPP_REPORT__BLOCK_DSO:
369                 fmt->entry = block_dso_entry;
370                 break;
371         default:
372                 return;
373         }
374
375         init_block_header(block_fmt);
376         perf_hpp_list__column_register(hpp_list, fmt);
377 }
378
379 static void register_block_columns(struct perf_hpp_list *hpp_list,
380                                    struct block_fmt *block_fmts)
381 {
382         for (int i = 0; i < PERF_HPP_REPORT__BLOCK_MAX_INDEX; i++)
383                 hpp_register(&block_fmts[i], i, hpp_list);
384 }
385
386 static void init_block_hist(struct block_hist *bh, struct block_fmt *block_fmts)
387 {
388         __hists__init(&bh->block_hists, &bh->block_list);
389         perf_hpp_list__init(&bh->block_list);
390         bh->block_list.nr_header_lines = 1;
391
392         register_block_columns(&bh->block_list, block_fmts);
393
394         perf_hpp_list__register_sort_field(&bh->block_list,
395                 &block_fmts[PERF_HPP_REPORT__BLOCK_TOTAL_CYCLES_PCT].fmt);
396 }
397
398 static void process_block_report(struct hists *hists,
399                                  struct block_report *block_report,
400                                  u64 total_cycles)
401 {
402         struct rb_node *next = rb_first_cached(&hists->entries);
403         struct block_hist *bh = &block_report->hist;
404         struct hist_entry *he;
405
406         init_block_hist(bh, block_report->fmts);
407
408         while (next) {
409                 he = rb_entry(next, struct hist_entry, rb_node);
410                 block_info__process_sym(he, bh, &block_report->cycles,
411                                         total_cycles);
412                 next = rb_next(&he->rb_node);
413         }
414
415         for (int i = 0; i < PERF_HPP_REPORT__BLOCK_MAX_INDEX; i++) {
416                 block_report->fmts[i].total_cycles = total_cycles;
417                 block_report->fmts[i].block_cycles = block_report->cycles;
418         }
419
420         hists__output_resort(&bh->block_hists, NULL);
421 }
422
423 struct block_report *block_info__create_report(struct evlist *evlist,
424                                                u64 total_cycles)
425 {
426         struct block_report *block_reports;
427         int nr_hists = evlist->core.nr_entries, i = 0;
428         struct evsel *pos;
429
430         block_reports = calloc(nr_hists, sizeof(struct block_report));
431         if (!block_reports)
432                 return NULL;
433
434         evlist__for_each_entry(evlist, pos) {
435                 struct hists *hists = evsel__hists(pos);
436
437                 process_block_report(hists, &block_reports[i], total_cycles);
438                 i++;
439         }
440
441         return block_reports;
442 }
443
444 int report__browse_block_hists(struct block_hist *bh, float min_percent,
445                                struct evsel *evsel, struct perf_env *env,
446                                struct annotation_options *annotation_opts)
447 {
448         int ret;
449
450         switch (use_browser) {
451         case 0:
452                 symbol_conf.report_individual_block = true;
453                 hists__fprintf(&bh->block_hists, true, 0, 0, min_percent,
454                                stdout, true);
455                 hists__delete_entries(&bh->block_hists);
456                 return 0;
457         case 1:
458                 symbol_conf.report_individual_block = true;
459                 ret = block_hists_tui_browse(bh, evsel, min_percent,
460                                              env, annotation_opts);
461                 hists__delete_entries(&bh->block_hists);
462                 return ret;
463         default:
464                 return -1;
465         }
466
467         return 0;
468 }
469
470 float block_info__total_cycles_percent(struct hist_entry *he)
471 {
472         struct block_info *bi = he->block_info;
473
474         if (bi->total_cycles)
475                 return bi->cycles * 100.0 / bi->total_cycles;
476
477         return 0.0;
478 }