1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/zalloc.h>
5 #include "block-info.h"
13 #include "ui/browsers/hists.h"
15 static struct block_header_column {
18 } block_columns[PERF_HPP_REPORT__BLOCK_MAX_INDEX] = {
19 [PERF_HPP_REPORT__BLOCK_TOTAL_CYCLES_PCT] = {
20 .name = "Sampled Cycles%",
23 [PERF_HPP_REPORT__BLOCK_LBR_CYCLES] = {
24 .name = "Sampled Cycles",
27 [PERF_HPP_REPORT__BLOCK_CYCLES_PCT] = {
28 .name = "Avg Cycles%",
31 [PERF_HPP_REPORT__BLOCK_AVG_CYCLES] = {
35 [PERF_HPP_REPORT__BLOCK_RANGE] = {
36 .name = "[Program Block Range]",
39 [PERF_HPP_REPORT__BLOCK_DSO] = {
40 .name = "Shared Object",
45 struct block_info *block_info__get(struct block_info *bi)
48 refcount_inc(&bi->refcnt);
52 void block_info__put(struct block_info *bi)
54 if (bi && refcount_dec_and_test(&bi->refcnt))
58 struct block_info *block_info__new(void)
60 struct block_info *bi = zalloc(sizeof(*bi));
63 refcount_set(&bi->refcnt, 1);
67 int64_t block_info__cmp(struct perf_hpp_fmt *fmt __maybe_unused,
68 struct hist_entry *left, struct hist_entry *right)
70 struct block_info *bi_l = left->block_info;
71 struct block_info *bi_r = right->block_info;
74 if (!bi_l->sym || !bi_r->sym) {
75 if (!bi_l->sym && !bi_r->sym)
83 if (bi_l->sym == bi_r->sym) {
84 if (bi_l->start == bi_r->start) {
85 if (bi_l->end == bi_r->end)
88 return (int64_t)(bi_r->end - bi_l->end);
90 return (int64_t)(bi_r->start - bi_l->start);
92 cmp = strcmp(bi_l->sym->name, bi_r->sym->name);
96 if (bi_l->sym->start != bi_r->sym->start)
97 return (int64_t)(bi_r->sym->start - bi_l->sym->start);
99 return (int64_t)(bi_r->sym->end - bi_l->sym->end);
102 static void init_block_info(struct block_info *bi, struct symbol *sym,
103 struct cyc_hist *ch, int offset,
107 bi->start = ch->start;
109 bi->cycles = ch->cycles;
110 bi->cycles_aggr = ch->cycles_aggr;
112 bi->num_aggr = ch->num_aggr;
113 bi->total_cycles = total_cycles;
115 memcpy(bi->cycles_spark, ch->cycles_spark,
116 NUM_SPARKS * sizeof(u64));
119 int block_info__process_sym(struct hist_entry *he, struct block_hist *bh,
120 u64 *block_cycles_aggr, u64 total_cycles)
122 struct annotation *notes;
124 static struct addr_location al;
127 if (!he->ms.map || !he->ms.sym)
130 memset(&al, 0, sizeof(al));
134 notes = symbol__annotation(he->ms.sym);
135 if (!notes || !notes->src || !notes->src->cycles_hist)
137 ch = notes->src->cycles_hist;
138 for (unsigned int i = 0; i < symbol__size(he->ms.sym); i++) {
139 if (ch[i].num_aggr) {
140 struct block_info *bi;
141 struct hist_entry *he_block;
143 bi = block_info__new();
147 init_block_info(bi, he->ms.sym, &ch[i], i,
149 cycles += bi->cycles_aggr / bi->num_aggr;
151 he_block = hists__add_entry_block(&bh->block_hists,
160 if (block_cycles_aggr)
161 *block_cycles_aggr += cycles;
166 static int block_column_header(struct perf_hpp_fmt *fmt,
167 struct perf_hpp *hpp,
168 struct hists *hists __maybe_unused,
169 int line __maybe_unused,
170 int *span __maybe_unused)
172 struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
174 return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
178 static int block_column_width(struct perf_hpp_fmt *fmt,
179 struct perf_hpp *hpp __maybe_unused,
180 struct hists *hists __maybe_unused)
182 struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
184 return block_fmt->width;
187 static int block_total_cycles_pct_entry(struct perf_hpp_fmt *fmt,
188 struct perf_hpp *hpp,
189 struct hist_entry *he)
191 struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
192 struct block_info *bi = he->block_info;
196 if (block_fmt->total_cycles)
197 ratio = (double)bi->cycles / (double)block_fmt->total_cycles;
199 sprintf(buf, "%.2f%%", 100.0 * ratio);
201 return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, buf);
204 static int64_t block_total_cycles_pct_sort(struct perf_hpp_fmt *fmt,
205 struct hist_entry *left,
206 struct hist_entry *right)
208 struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
209 struct block_info *bi_l = left->block_info;
210 struct block_info *bi_r = right->block_info;
213 if (block_fmt->total_cycles) {
214 l = ((double)bi_l->cycles /
215 (double)block_fmt->total_cycles) * 100000.0;
216 r = ((double)bi_r->cycles /
217 (double)block_fmt->total_cycles) * 100000.0;
218 return (int64_t)l - (int64_t)r;
224 static void cycles_string(u64 cycles, char *buf, int size)
226 if (cycles >= 1000000)
227 scnprintf(buf, size, "%.1fM", (double)cycles / 1000000.0);
228 else if (cycles >= 1000)
229 scnprintf(buf, size, "%.1fK", (double)cycles / 1000.0);
231 scnprintf(buf, size, "%1d", cycles);
234 static int block_cycles_lbr_entry(struct perf_hpp_fmt *fmt,
235 struct perf_hpp *hpp, struct hist_entry *he)
237 struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
238 struct block_info *bi = he->block_info;
241 cycles_string(bi->cycles_aggr, cycles_buf, sizeof(cycles_buf));
243 return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
247 static int block_cycles_pct_entry(struct perf_hpp_fmt *fmt,
248 struct perf_hpp *hpp, struct hist_entry *he)
250 struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
251 struct block_info *bi = he->block_info;
256 if (block_fmt->block_cycles && bi->num_aggr) {
257 avg = bi->cycles_aggr / bi->num_aggr;
258 ratio = (double)avg / (double)block_fmt->block_cycles;
261 sprintf(buf, "%.2f%%", 100.0 * ratio);
263 return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, buf);
266 static int block_avg_cycles_entry(struct perf_hpp_fmt *fmt,
267 struct perf_hpp *hpp,
268 struct hist_entry *he)
270 struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
271 struct block_info *bi = he->block_info;
274 cycles_string(bi->cycles_aggr / bi->num_aggr, cycles_buf,
277 return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
281 static int block_range_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
282 struct hist_entry *he)
284 struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
285 struct block_info *bi = he->block_info;
287 char *start_line, *end_line;
289 symbol_conf.disable_add2line_warn = true;
291 start_line = map__srcline(he->ms.map, bi->sym->start + bi->start,
294 end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
297 if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
298 scnprintf(buf, sizeof(buf), "[%s -> %s]",
299 start_line, end_line);
301 scnprintf(buf, sizeof(buf), "[%7lx -> %7lx]",
305 free_srcline(start_line);
306 free_srcline(end_line);
308 return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, buf);
311 static int block_dso_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
312 struct hist_entry *he)
314 struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
315 struct map *map = he->ms.map;
317 if (map && map->dso) {
318 return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
319 map->dso->short_name);
322 return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
326 static void init_block_header(struct block_fmt *block_fmt)
328 struct perf_hpp_fmt *fmt = &block_fmt->fmt;
330 BUG_ON(block_fmt->idx >= PERF_HPP_REPORT__BLOCK_MAX_INDEX);
332 block_fmt->header = block_columns[block_fmt->idx].name;
333 block_fmt->width = block_columns[block_fmt->idx].width;
335 fmt->header = block_column_header;
336 fmt->width = block_column_width;
339 static void hpp_register(struct block_fmt *block_fmt, int idx,
340 struct perf_hpp_list *hpp_list)
342 struct perf_hpp_fmt *fmt = &block_fmt->fmt;
344 block_fmt->idx = idx;
345 INIT_LIST_HEAD(&fmt->list);
346 INIT_LIST_HEAD(&fmt->sort_list);
349 case PERF_HPP_REPORT__BLOCK_TOTAL_CYCLES_PCT:
350 fmt->entry = block_total_cycles_pct_entry;
351 fmt->cmp = block_info__cmp;
352 fmt->sort = block_total_cycles_pct_sort;
354 case PERF_HPP_REPORT__BLOCK_LBR_CYCLES:
355 fmt->entry = block_cycles_lbr_entry;
357 case PERF_HPP_REPORT__BLOCK_CYCLES_PCT:
358 fmt->entry = block_cycles_pct_entry;
360 case PERF_HPP_REPORT__BLOCK_AVG_CYCLES:
361 fmt->entry = block_avg_cycles_entry;
363 case PERF_HPP_REPORT__BLOCK_RANGE:
364 fmt->entry = block_range_entry;
366 case PERF_HPP_REPORT__BLOCK_DSO:
367 fmt->entry = block_dso_entry;
373 init_block_header(block_fmt);
374 perf_hpp_list__column_register(hpp_list, fmt);
377 static void register_block_columns(struct perf_hpp_list *hpp_list,
378 struct block_fmt *block_fmts)
380 for (int i = 0; i < PERF_HPP_REPORT__BLOCK_MAX_INDEX; i++)
381 hpp_register(&block_fmts[i], i, hpp_list);
384 static void init_block_hist(struct block_hist *bh, struct block_fmt *block_fmts)
386 __hists__init(&bh->block_hists, &bh->block_list);
387 perf_hpp_list__init(&bh->block_list);
388 bh->block_list.nr_header_lines = 1;
390 register_block_columns(&bh->block_list, block_fmts);
392 perf_hpp_list__register_sort_field(&bh->block_list,
393 &block_fmts[PERF_HPP_REPORT__BLOCK_TOTAL_CYCLES_PCT].fmt);
396 static void process_block_report(struct hists *hists,
397 struct block_report *block_report,
400 struct rb_node *next = rb_first_cached(&hists->entries);
401 struct block_hist *bh = &block_report->hist;
402 struct hist_entry *he;
404 init_block_hist(bh, block_report->fmts);
407 he = rb_entry(next, struct hist_entry, rb_node);
408 block_info__process_sym(he, bh, &block_report->cycles,
410 next = rb_next(&he->rb_node);
413 for (int i = 0; i < PERF_HPP_REPORT__BLOCK_MAX_INDEX; i++) {
414 block_report->fmts[i].total_cycles = total_cycles;
415 block_report->fmts[i].block_cycles = block_report->cycles;
418 hists__output_resort(&bh->block_hists, NULL);
421 struct block_report *block_info__create_report(struct evlist *evlist,
424 struct block_report *block_reports;
425 int nr_hists = evlist->core.nr_entries, i = 0;
428 block_reports = calloc(nr_hists, sizeof(struct block_report));
432 evlist__for_each_entry(evlist, pos) {
433 struct hists *hists = evsel__hists(pos);
435 process_block_report(hists, &block_reports[i], total_cycles);
439 return block_reports;
442 #ifdef HAVE_SLANG_SUPPORT
443 static int block_hists_browser__title(struct hist_browser *browser, char *bf,
446 struct hists *hists = evsel__hists(browser->block_evsel);
447 const char *evname = perf_evsel__name(browser->block_evsel);
448 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
451 ret = scnprintf(bf, size, "# Samples: %lu", nr_samples);
453 scnprintf(bf + ret, size - ret, " of event '%s'", evname);
458 static int block_hists_tui_browse(struct block_hist *bh, struct evsel *evsel,
461 struct hists *hists = &bh->block_hists;
462 struct hist_browser *browser;
464 static const char help[] =
467 browser = hist_browser__new(hists);
471 browser->block_evsel = evsel;
472 browser->title = block_hists_browser__title;
473 browser->min_pcnt = min_percent;
475 /* reset abort key so that it can get Ctrl-C as a key */
477 SLang_init_tty(0, 0, 0);
480 key = hist_browser__run(browser, "? - help", true);
486 ui_browser__help_window(&browser->b, help);
494 hist_browser__delete(browser);
498 static int block_hists_tui_browse(struct block_hist *bh __maybe_unused,
499 struct evsel *evsel __maybe_unused,
500 float min_percent __maybe_unused)
506 int report__browse_block_hists(struct block_hist *bh, float min_percent,
511 switch (use_browser) {
513 symbol_conf.report_individual_block = true;
514 hists__fprintf(&bh->block_hists, true, 0, 0, min_percent,
516 hists__delete_entries(&bh->block_hists);
519 symbol_conf.report_individual_block = true;
520 ret = block_hists_tui_browse(bh, evsel, min_percent);
521 hists__delete_entries(&bh->block_hists);
530 float block_info__total_cycles_percent(struct hist_entry *he)
532 struct block_info *bi = he->block_info;
534 if (bi->total_cycles)
535 return bi->cycles * 100.0 / bi->total_cycles;