Skip to content

Commit 035f0c2

Browse files
captain5050namhyung
authored andcommitted
perf annotate: Prefer passing evsel to evsel->core.idx
An evsel idx may not be stable due to sorting, evlist removal, etc. Try to reduce it being part of APIs by explicitly passing the evsel in annotate code. Internally the code just reads evsel->core.idx so behavior is unchanged. Signed-off-by: Ian Rogers <irogers@google.com> Cc: Chen Ni <nichen@iscas.ac.cn> Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com> Link: https://lore.kernel.org/r/20250117181848.690474-1-irogers@google.com Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent ac22d75 commit 035f0c2

File tree

5 files changed

+38
-36
lines changed

5 files changed

+38
-36
lines changed

tools/perf/builtin-top.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ static void perf_top__show_details(struct perf_top *top)
267267

268268
if (top->evlist->enabled) {
269269
if (top->zero)
270-
symbol__annotate_zero_histogram(symbol, top->sym_evsel->core.idx);
270+
symbol__annotate_zero_histogram(symbol, top->sym_evsel);
271271
else
272-
symbol__annotate_decay_histogram(symbol, top->sym_evsel->core.idx);
272+
symbol__annotate_decay_histogram(symbol, top->sym_evsel);
273273
}
274274
if (more != 0)
275275
printf("%d lines not displayed, maybe increase display entries [e]\n", more);

tools/perf/ui/browsers/annotate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ static int annotate_browser__run(struct annotate_browser *browser,
754754
hbt->timer(hbt->arg);
755755

756756
if (delay_secs != 0) {
757-
symbol__annotate_decay_histogram(sym, evsel->core.idx);
757+
symbol__annotate_decay_histogram(sym, evsel);
758758
hists__scnprintf_title(hists, title, sizeof(title));
759759
annotate_browser__show(&browser->b, title, help);
760760
}

tools/perf/ui/gtk/annotate.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "util/sort.h"
44
#include "util/debug.h"
55
#include "util/annotate.h"
6+
#include "util/evlist.h"
67
#include "util/evsel.h"
78
#include "util/map.h"
89
#include "util/dso.h"
@@ -26,7 +27,7 @@ static const char *const col_names[] = {
2627
};
2728

2829
static int perf_gtk__get_percent(char *buf, size_t size, struct symbol *sym,
29-
struct disasm_line *dl, int evidx)
30+
struct disasm_line *dl, const struct evsel *evsel)
3031
{
3132
struct annotation *notes;
3233
struct sym_hist *symhist;
@@ -42,8 +43,8 @@ static int perf_gtk__get_percent(char *buf, size_t size, struct symbol *sym,
4243
return 0;
4344

4445
notes = symbol__annotation(sym);
45-
symhist = annotation__histogram(notes, evidx);
46-
entry = annotated_source__hist_entry(notes->src, evidx, dl->al.offset);
46+
symhist = annotation__histogram(notes, evsel);
47+
entry = annotated_source__hist_entry(notes->src, evsel, dl->al.offset);
4748
if (entry)
4849
nr_samples = entry->nr_samples;
4950

@@ -139,16 +140,17 @@ static int perf_gtk__annotate_symbol(GtkWidget *window, struct map_symbol *ms,
139140
gtk_list_store_append(store, &iter);
140141

141142
if (evsel__is_group_event(evsel)) {
142-
for (i = 0; i < evsel->core.nr_members; i++) {
143+
struct evsel *cur_evsel;
144+
145+
for_each_group_evsel(cur_evsel, evsel__leader(evsel)) {
143146
ret += perf_gtk__get_percent(s + ret,
144147
sizeof(s) - ret,
145148
sym, pos,
146-
evsel->core.idx + i);
149+
cur_evsel);
147150
ret += scnprintf(s + ret, sizeof(s) - ret, " ");
148151
}
149152
} else {
150-
ret = perf_gtk__get_percent(s, sizeof(s), sym, pos,
151-
evsel->core.idx);
153+
ret = perf_gtk__get_percent(s, sizeof(s), sym, pos, evsel);
152154
}
153155

154156
if (ret)

tools/perf/util/annotate.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ static int __symbol__account_cycles(struct cyc_hist *ch,
209209
}
210210

211211
static int __symbol__inc_addr_samples(struct map_symbol *ms,
212-
struct annotated_source *src, int evidx, u64 addr,
212+
struct annotated_source *src, struct evsel *evsel, u64 addr,
213213
struct perf_sample *sample)
214214
{
215215
struct symbol *sym = ms->sym;
@@ -228,14 +228,14 @@ static int __symbol__inc_addr_samples(struct map_symbol *ms,
228228
}
229229

230230
offset = addr - sym->start;
231-
h = annotated_source__histogram(src, evidx);
231+
h = annotated_source__histogram(src, evsel);
232232
if (h == NULL) {
233233
pr_debug("%s(%d): ENOMEM! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 ", func: %d\n",
234234
__func__, __LINE__, sym->name, sym->start, addr, sym->end, sym->type == STT_FUNC);
235235
return -ENOMEM;
236236
}
237237

238-
hash_key = offset << 16 | evidx;
238+
hash_key = offset << 16 | evsel->core.idx;
239239
if (!hashmap__find(src->samples, hash_key, &entry)) {
240240
entry = zalloc(sizeof(*entry));
241241
if (entry == NULL)
@@ -252,7 +252,7 @@ static int __symbol__inc_addr_samples(struct map_symbol *ms,
252252

253253
pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
254254
", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n",
255-
sym->start, sym->name, addr, addr - sym->start, evidx,
255+
sym->start, sym->name, addr, addr - sym->start, evsel->core.idx,
256256
entry->nr_samples, entry->period);
257257
return 0;
258258
}
@@ -323,7 +323,7 @@ static int symbol__inc_addr_samples(struct map_symbol *ms,
323323
if (sym == NULL)
324324
return 0;
325325
src = symbol__hists(sym, evsel->evlist->core.nr_entries);
326-
return src ? __symbol__inc_addr_samples(ms, src, evsel->core.idx, addr, sample) : 0;
326+
return src ? __symbol__inc_addr_samples(ms, src, evsel, addr, sample) : 0;
327327
}
328328

329329
static int symbol__account_br_cntr(struct annotated_branch *branch,
@@ -861,15 +861,14 @@ static void calc_percent(struct annotation *notes,
861861
s64 offset, s64 end)
862862
{
863863
struct hists *hists = evsel__hists(evsel);
864-
int evidx = evsel->core.idx;
865-
struct sym_hist *sym_hist = annotation__histogram(notes, evidx);
864+
struct sym_hist *sym_hist = annotation__histogram(notes, evsel);
866865
unsigned int hits = 0;
867866
u64 period = 0;
868867

869868
while (offset < end) {
870869
struct sym_hist_entry *entry;
871870

872-
entry = annotated_source__hist_entry(notes->src, evidx, offset);
871+
entry = annotated_source__hist_entry(notes->src, evsel, offset);
873872
if (entry) {
874873
hits += entry->nr_samples;
875874
period += entry->period;
@@ -1140,15 +1139,14 @@ static void print_summary(struct rb_root *root, const char *filename)
11401139

11411140
static void symbol__annotate_hits(struct symbol *sym, struct evsel *evsel)
11421141
{
1143-
int evidx = evsel->core.idx;
11441142
struct annotation *notes = symbol__annotation(sym);
1145-
struct sym_hist *h = annotation__histogram(notes, evidx);
1143+
struct sym_hist *h = annotation__histogram(notes, evsel);
11461144
u64 len = symbol__size(sym), offset;
11471145

11481146
for (offset = 0; offset < len; ++offset) {
11491147
struct sym_hist_entry *entry;
11501148

1151-
entry = annotated_source__hist_entry(notes->src, evidx, offset);
1149+
entry = annotated_source__hist_entry(notes->src, evsel, offset);
11521150
if (entry && entry->nr_samples != 0)
11531151
printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
11541152
sym->start + offset, entry->nr_samples);
@@ -1178,7 +1176,7 @@ int symbol__annotate_printf(struct map_symbol *ms, struct evsel *evsel)
11781176
const char *d_filename;
11791177
const char *evsel_name = evsel__name(evsel);
11801178
struct annotation *notes = symbol__annotation(sym);
1181-
struct sym_hist *h = annotation__histogram(notes, evsel->core.idx);
1179+
struct sym_hist *h = annotation__histogram(notes, evsel);
11821180
struct annotation_line *pos, *queue = NULL;
11831181
struct annotation_options *opts = &annotate_opts;
11841182
u64 start = map__rip_2objdump(map, sym->start);
@@ -1364,18 +1362,18 @@ int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel)
13641362
return err;
13651363
}
13661364

1367-
void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
1365+
void symbol__annotate_zero_histogram(struct symbol *sym, struct evsel *evsel)
13681366
{
13691367
struct annotation *notes = symbol__annotation(sym);
1370-
struct sym_hist *h = annotation__histogram(notes, evidx);
1368+
struct sym_hist *h = annotation__histogram(notes, evsel);
13711369

13721370
memset(h, 0, sizeof(*notes->src->histograms) * notes->src->nr_histograms);
13731371
}
13741372

1375-
void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
1373+
void symbol__annotate_decay_histogram(struct symbol *sym, struct evsel *evsel)
13761374
{
13771375
struct annotation *notes = symbol__annotation(sym);
1378-
struct sym_hist *h = annotation__histogram(notes, evidx);
1376+
struct sym_hist *h = annotation__histogram(notes, evsel);
13791377
struct annotation_line *al;
13801378

13811379
h->nr_samples = 0;
@@ -1385,7 +1383,7 @@ void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
13851383
if (al->offset == -1)
13861384
continue;
13871385

1388-
entry = annotated_source__hist_entry(notes->src, evidx, al->offset);
1386+
entry = annotated_source__hist_entry(notes->src, evsel, al->offset);
13891387
if (entry == NULL)
13901388
continue;
13911389

tools/perf/util/annotate.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "hashmap.h"
1616
#include "disasm.h"
1717
#include "branch.h"
18+
#include "evsel.h"
1819

1920
struct hist_browser_timer;
2021
struct hist_entry;
@@ -23,7 +24,6 @@ struct map_symbol;
2324
struct addr_map_symbol;
2425
struct option;
2526
struct perf_sample;
26-
struct evsel;
2727
struct symbol;
2828
struct annotated_data_type;
2929

@@ -373,21 +373,23 @@ static inline u8 annotation__br_cntr_width(void)
373373
void annotation__update_column_widths(struct annotation *notes);
374374
void annotation__toggle_full_addr(struct annotation *notes, struct map_symbol *ms);
375375

376-
static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src, int idx)
376+
static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src,
377+
const struct evsel *evsel)
377378
{
378-
return &src->histograms[idx];
379+
return &src->histograms[evsel->core.idx];
379380
}
380381

381-
static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
382+
static inline struct sym_hist *annotation__histogram(struct annotation *notes,
383+
const struct evsel *evsel)
382384
{
383-
return annotated_source__histogram(notes->src, idx);
385+
return annotated_source__histogram(notes->src, evsel);
384386
}
385387

386388
static inline struct sym_hist_entry *
387-
annotated_source__hist_entry(struct annotated_source *src, int idx, u64 offset)
389+
annotated_source__hist_entry(struct annotated_source *src, const struct evsel *evsel, u64 offset)
388390
{
389391
struct sym_hist_entry *entry;
390-
long key = offset << 16 | idx;
392+
long key = offset << 16 | evsel->core.idx;
391393

392394
if (!hashmap__find(src->samples, key, &entry))
393395
return NULL;
@@ -449,8 +451,8 @@ enum symbol_disassemble_errno {
449451
int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen);
450452

451453
int symbol__annotate_printf(struct map_symbol *ms, struct evsel *evsel);
452-
void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
453-
void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
454+
void symbol__annotate_zero_histogram(struct symbol *sym, struct evsel *evsel);
455+
void symbol__annotate_decay_histogram(struct symbol *sym, struct evsel *evsel);
454456
void annotated_source__purge(struct annotated_source *as);
455457

456458
int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel);

0 commit comments

Comments
 (0)