Skip to content

Commit 8015457

Browse files
committed
perf annotate: Calculate instruction overhead using hashmap
Use annotated_source.samples hashmap instead of addr array in the struct sym_hist. Reviewed-by: Ian Rogers <[email protected]> Reviewed-by: Arnaldo Carvalho de Melo <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Cc: Andi Kleen <[email protected]> Signed-off-by: Namhyung Kim <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent d3e7cad commit 8015457

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

tools/perf/ui/gtk/annotate.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,29 @@ static const char *const col_names[] = {
2828
static int perf_gtk__get_percent(char *buf, size_t size, struct symbol *sym,
2929
struct disasm_line *dl, int evidx)
3030
{
31+
struct annotation *notes;
3132
struct sym_hist *symhist;
33+
struct sym_hist_entry *entry;
3234
double percent = 0.0;
3335
const char *markup;
3436
int ret = 0;
37+
u64 nr_samples = 0;
3538

3639
strcpy(buf, "");
3740

3841
if (dl->al.offset == (s64) -1)
3942
return 0;
4043

41-
symhist = annotation__histogram(symbol__annotation(sym), evidx);
42-
if (!symbol_conf.event_group && !symhist->addr[dl->al.offset].nr_samples)
44+
notes = symbol__annotation(sym);
45+
symhist = annotation__histogram(notes, evidx);
46+
entry = annotated_source__hist_entry(notes->src, evidx, dl->al.offset);
47+
if (entry)
48+
nr_samples = entry->nr_samples;
49+
50+
if (!symbol_conf.event_group && nr_samples == 0)
4351
return 0;
4452

45-
percent = 100.0 * symhist->addr[dl->al.offset].nr_samples / symhist->nr_samples;
53+
percent = 100.0 * nr_samples / symhist->nr_samples;
4654

4755
markup = perf_gtk__get_percent_color(percent);
4856
if (markup)

tools/perf/util/annotate.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,17 +2368,25 @@ static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
23682368
return err;
23692369
}
23702370

2371-
static void calc_percent(struct sym_hist *sym_hist,
2372-
struct hists *hists,
2371+
static void calc_percent(struct annotation *notes,
2372+
struct evsel *evsel,
23732373
struct annotation_data *data,
23742374
s64 offset, s64 end)
23752375
{
2376+
struct hists *hists = evsel__hists(evsel);
2377+
int evidx = evsel->core.idx;
2378+
struct sym_hist *sym_hist = annotation__histogram(notes, evidx);
23762379
unsigned int hits = 0;
23772380
u64 period = 0;
23782381

23792382
while (offset < end) {
2380-
hits += sym_hist->addr[offset].nr_samples;
2381-
period += sym_hist->addr[offset].period;
2383+
struct sym_hist_entry *entry;
2384+
2385+
entry = annotated_source__hist_entry(notes->src, evidx, offset);
2386+
if (entry) {
2387+
hits += entry->nr_samples;
2388+
period += entry->period;
2389+
}
23822390
++offset;
23832391
}
23842392

@@ -2415,16 +2423,13 @@ static void annotation__calc_percent(struct annotation *notes,
24152423
end = next ? next->offset : len;
24162424

24172425
for_each_group_evsel(evsel, leader) {
2418-
struct hists *hists = evsel__hists(evsel);
24192426
struct annotation_data *data;
2420-
struct sym_hist *sym_hist;
24212427

24222428
BUG_ON(i >= al->data_nr);
24232429

2424-
sym_hist = annotation__histogram(notes, evsel->core.idx);
24252430
data = &al->data[i++];
24262431

2427-
calc_percent(sym_hist, hists, data, al->offset, end);
2432+
calc_percent(notes, evsel, data, al->offset, end);
24282433
}
24292434
}
24302435
}
@@ -2619,14 +2624,19 @@ static void print_summary(struct rb_root *root, const char *filename)
26192624

26202625
static void symbol__annotate_hits(struct symbol *sym, struct evsel *evsel)
26212626
{
2627+
int evidx = evsel->core.idx;
26222628
struct annotation *notes = symbol__annotation(sym);
2623-
struct sym_hist *h = annotation__histogram(notes, evsel->core.idx);
2629+
struct sym_hist *h = annotation__histogram(notes, evidx);
26242630
u64 len = symbol__size(sym), offset;
26252631

2626-
for (offset = 0; offset < len; ++offset)
2627-
if (h->addr[offset].nr_samples != 0)
2632+
for (offset = 0; offset < len; ++offset) {
2633+
struct sym_hist_entry *entry;
2634+
2635+
entry = annotated_source__hist_entry(notes->src, evidx, offset);
2636+
if (entry && entry->nr_samples != 0)
26282637
printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
2629-
sym->start + offset, h->addr[offset].nr_samples);
2638+
sym->start + offset, entry->nr_samples);
2639+
}
26302640
printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples);
26312641
}
26322642

@@ -2855,8 +2865,14 @@ void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
28552865

28562866
h->nr_samples = 0;
28572867
for (offset = 0; offset < len; ++offset) {
2858-
h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8;
2859-
h->nr_samples += h->addr[offset].nr_samples;
2868+
struct sym_hist_entry *entry;
2869+
2870+
entry = annotated_source__hist_entry(notes->src, evidx, offset);
2871+
if (entry == NULL)
2872+
continue;
2873+
2874+
entry->nr_samples = entry->nr_samples * 7 / 8;
2875+
h->nr_samples += entry->nr_samples;
28602876
}
28612877
}
28622878

tools/perf/util/annotate.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,17 @@ static inline struct sym_hist *annotation__histogram(struct annotation *notes, i
356356
return annotated_source__histogram(notes->src, idx);
357357
}
358358

359+
static inline struct sym_hist_entry *
360+
annotated_source__hist_entry(struct annotated_source *src, int idx, u64 offset)
361+
{
362+
struct sym_hist_entry *entry;
363+
long key = offset << 16 | idx;
364+
365+
if (!hashmap__find(src->samples, key, &entry))
366+
return NULL;
367+
return entry;
368+
}
369+
359370
static inline struct annotation *symbol__annotation(struct symbol *sym)
360371
{
361372
return (void *)sym - symbol_conf.priv_size;

0 commit comments

Comments
 (0)