* [PATCH v1 1/1] perf annotate-data: Cache the per-instruction data type resolution
@ 2026-12-05 13:01 Arnaldo Carvalho de Melo
2026-12-05 13:18 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-12-05 13:01 UTC (permalink / raw)
To: Namhyung Kim
Cc: Ingo Molnar, Thomas Gleixner, James Clark, Jiri Olsa, Ian Rogers,
Adrian Hunter, Clark Williams, linux-perf-users,
Arnaldo Carvalho de Melo, Arnaldo Carvalho de Melo
Resolving a sample's data type parses the instruction's operands and
then searches DWARF for the CU, the CFA, the scopes and the variable
behind the operand. All of that is keyed on the instruction, not on the
sample: every hist entry from the same address resolves to the same
type, member offset and verdict. A per-CPU data type report on a
3.9M-sample capture, for example, makes 149,000 such resolutions for
28,500 distinct instructions. The disassembly walk in the caller is
unchanged.
Memoize the result on (dso, ip): the ip is already dso-relative, being
sym->start plus the offset in its disasm line, and the types the
resolution returns live in the dso's type tree, so the key holds a dso
reference and every map of that dso shares the entry. Replay the
per-entry accounting from a saved delta of the data-type stats, so
--type-stat matches the uncached build, and cache the no-memory-operand
verdict too.
Verified by 'perf report --sort cpu,type' and 'perf annotate --data-type
--type-stat' are byte-identical to the uncached build on that capture
and on three others, the largest with twenty processes.
Best of two runs on the 3.9M-sample capture above, with the vmlinux it
was recorded on: the per-CPU data type report goes from 26.5s to 9.9s
and --sort type from 7.9s to 6.9s, the smaller gain there being expected
as without the per-CPU split there are far fewer hist entries per
instruction, hence less repeated work to save.
Built and tested with and without REFCNT_CHECKING=1: the data type
profiling tests pass.
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
tools/perf/util/annotate.c | 259 +++++++++++++++++++++++++++++++++++--
tools/perf/util/annotate.h | 1 +
tools/perf/util/session.c | 1 +
3 files changed, 250 insertions(+), 11 deletions(-)
diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
index e270d16ce1778487..f5e07746e76c681a 100644
--- a/tools/perf/util/annotate.c
+++ b/tools/perf/util/annotate.c
@@ -938,6 +938,211 @@ __hist_entry__get_data_type(struct hist_entry *he, const struct arch *arch,
struct debuginfo *dbg, struct disasm_line *dl,
int *type_offset);
+#define FOREACH_ANNOTATED_DATA_STAT_FIELD(f) \
+ f(total) \
+ f(no_sym) \
+ f(no_insn) \
+ f(no_insn_ops) \
+ f(no_mem_ops) \
+ f(no_reg) \
+ f(no_dbginfo) \
+ f(no_cuinfo) \
+ f(no_var) \
+ f(no_typeinfo) \
+ f(invalid_size) \
+ f(bad_offset) \
+ f(insn_track)
+
+/*
+ * The data-type cache replays these stats on a hit: if you add a field,
+ * add it to FOREACH_ANNOTATED_DATA_STAT_FIELD and bump the count here.
+ */
+static_assert(sizeof(struct annotated_data_stat) == 13 * sizeof(int),
+ "update FOREACH_ANNOTATED_DATA_STAT_FIELD");
+
+#define DATA_STAT_SUB(field) delta->field = ann_data_stat.field - before->field;
+#define DATA_STAT_ADD(field) ann_data_stat.field += delta->field;
+
+static void annotated_data_stat__delta(struct annotated_data_stat *delta,
+ const struct annotated_data_stat *before)
+{
+ FOREACH_ANNOTATED_DATA_STAT_FIELD(DATA_STAT_SUB)
+}
+
+static void annotated_data_stat__add(const struct annotated_data_stat *delta)
+{
+ FOREACH_ANNOTATED_DATA_STAT_FIELD(DATA_STAT_ADD)
+}
+
+#undef DATA_STAT_SUB
+#undef DATA_STAT_ADD
+
+/*
+ * The data-type resolution is a function of the instruction, not of the
+ * sample: the operand address comes from the PC-relative displacement or
+ * the per-cpu GS offset, and the whole DWARF walk keys on the code
+ * address. Sample streams hit the same few thousand instructions
+ * millions of times, so memoize the walk per (dso, ip) and replay only
+ * the per-entry accounting. The key holds a dso reference because the
+ * cached type lives in the dso's type tree, which dso__delete() frees.
+ * The di_cache above already assumes single-threaded processing; so does
+ * this.
+ */
+enum dtype_replay {
+ DTYPE_REPLAY_NONE, /* not filled, do not cache */
+ DTYPE_REPLAY_NO_OPS, /* operands unparsable */
+ DTYPE_REPLAY_STACKOP, /* push/pop: the stack-op marker */
+ DTYPE_REPLAY_ADDRGEN, /* lea-like, never a data access */
+ DTYPE_REPLAY_CANARY,
+ DTYPE_REPLAY_MEM, /* resolved member (type may still be NULL) */
+ DTYPE_REPLAY_NOMEM, /* no memory operand: caller may retry fused */
+};
+
+struct dtype_cache_key {
+ struct dso *dso; /* held: it owns the cached type */
+ u64 ip; /* the instruction, in dso space */
+};
+
+struct dtype_cache {
+ struct dtype_cache_key key; /* hashmap key: keep first */
+ enum dtype_replay replay;
+ struct annotated_data_type *type;
+ struct annotated_item_stat *istat;
+ struct annotated_data_stat stat_delta;
+ int type_offset;
+};
+
+static size_t dtype_cache__hash(long _key, void *ctx __maybe_unused)
+{
+ const struct dtype_cache_key *key = (const void *)_key;
+ void *dso = key->dso ? RC_CHK_ACCESS(key->dso) : NULL;
+ u64 h = (u64)(uintptr_t)dso * 0x9E3779B97F4A7C15ULL;
+
+ /* Fibonacci hash: the odd multiplier mixes the low ip bits in. */
+ h ^= key->ip * 0xC2B2AE3D27D4EB4FULL;
+ return h;
+}
+
+static bool dtype_cache__equal(long _key1, long _key2, void *ctx __maybe_unused)
+{
+ const struct dtype_cache_key *k1 = (const void *)_key1;
+ const struct dtype_cache_key *k2 = (const void *)_key2;
+
+ return RC_CHK_EQUAL(k1->dso, k2->dso) && k1->ip == k2->ip;
+}
+
+static struct hashmap dtype_cache = {
+ .hash_fn = dtype_cache__hash,
+ .equal_fn = dtype_cache__equal,
+};
+
+static struct dtype_cache *dtype_cache__find(struct dso *dso, u64 ip)
+{
+ struct dtype_cache_key key = {
+ .dso = dso,
+ .ip = ip,
+ };
+ struct dtype_cache *dc = NULL;
+
+ if (debug_type_profile)
+ return NULL;
+
+ hashmap__find(&dtype_cache, &key, &dc);
+ return dc;
+}
+
+static void dtype_cache__add(struct dso *dso, u64 ip,
+ const struct dtype_cache *fill)
+{
+ struct dtype_cache *dc;
+
+ if (fill->replay == DTYPE_REPLAY_NONE || debug_type_profile)
+ return;
+
+ dc = zalloc(sizeof(*dc));
+ if (dc == NULL)
+ return;
+
+ dc->key.dso = dso__get(dso);
+ if (dc->key.dso == NULL) {
+ free(dc);
+ return;
+ }
+ dc->key.ip = ip;
+ dc->replay = fill->replay;
+ dc->type = fill->type;
+ dc->istat = fill->istat;
+ dc->stat_delta = fill->stat_delta;
+ dc->type_offset = fill->type_offset;
+
+ if (hashmap__add(&dtype_cache, &dc->key, dc) < 0) {
+ dso__put(dc->key.dso);
+ free(dc);
+ }
+}
+
+void dtype_cache__delete(void)
+{
+ struct hashmap_entry *cur, *tmp;
+ struct dtype_cache *dc;
+ size_t bkt;
+
+ hashmap__for_each_entry_safe(&dtype_cache, cur, tmp, bkt) {
+ dc = cur->pvalue;
+ dso__put(dc->key.dso);
+ free(dc);
+ }
+ hashmap__clear(&dtype_cache);
+}
+
+/*
+ * Replay a cached resolution: add the stats the live walk bumped for this
+ * instruction and emit the same verdict.
+ */
+static struct annotated_data_type *
+dtype_cache__replay(struct dtype_cache *dc, struct hist_entry *he, int *type_offset)
+{
+ annotated_data_stat__add(&dc->stat_delta);
+
+ switch (dc->replay) {
+ case DTYPE_REPLAY_NONE: /* not reachable: not cached */
+ case DTYPE_REPLAY_NOMEM: /* caller retries a fused instruction */
+ return NULL;
+ case DTYPE_REPLAY_NO_OPS:
+ dc->istat->bad++;
+ return NO_TYPE;
+ case DTYPE_REPLAY_STACKOP:
+ dc->istat->good++;
+ *type_offset = 0;
+ return &stackop_type;
+ case DTYPE_REPLAY_ADDRGEN:
+ dc->istat->bad++;
+ return NO_TYPE;
+ case DTYPE_REPLAY_CANARY:
+ dc->istat->good++;
+ *type_offset = 0;
+ return &canary_type;
+ case DTYPE_REPLAY_MEM:
+ if (dc->type)
+ dc->istat->good++;
+ else
+ dc->istat->bad++;
+
+ if (symbol_conf.annotate_data_sample) {
+ struct evsel *evsel = hists_to_evsel(he->hists);
+
+ annotated_data_type__update_samples(dc->type, evsel,
+ dc->type_offset,
+ he->stat.nr_events,
+ he->stat.period);
+ }
+ *type_offset = dc->type_offset;
+ return dc->type ?: NO_TYPE;
+ default:
+ return NULL;
+ }
+}
+
static bool needs_type_info(struct annotated_data_type *data_type)
{
if (data_type == NULL || data_type == NO_TYPE)
@@ -2999,32 +3204,48 @@ __hist_entry__get_data_type(struct hist_entry *he, const struct arch *arch,
struct map_symbol *ms = &he->ms;
struct annotated_insn_loc loc;
struct annotated_op_loc *op_loc;
- struct annotated_data_type *mem_type;
+ struct annotated_data_type *mem_type = NULL, *ret;
struct annotated_item_stat *istat;
+ struct dtype_cache fill = { .replay = DTYPE_REPLAY_NONE };
+ struct dtype_cache *dc;
+ struct annotated_data_stat before;
int i;
+ dc = dtype_cache__find(map__dso(ms->map), ms->sym->start + dl->al.offset);
+ if (dc)
+ return dtype_cache__replay(dc, he, type_offset);
+
+ before = ann_data_stat;
+
istat = annotate_data_stat(&ann_insn_stat, dl->ins.name);
if (istat == NULL) {
ann_data_stat.no_insn++;
- return NO_TYPE;
+ goto out_none_type;
}
if (annotate_get_insn_location(arch, dl, &loc) < 0) {
ann_data_stat.no_insn_ops++;
istat->bad++;
- return NO_TYPE;
+ fill.replay = DTYPE_REPLAY_NO_OPS;
+ fill.istat = istat;
+ goto out_none_type;
}
if (is_stack_operation(arch, dl)) {
istat->good++;
*type_offset = 0;
- return &stackop_type;
+ fill.replay = DTYPE_REPLAY_STACKOP;
+ fill.istat = istat;
+ mem_type = &stackop_type;
+ goto out_cached;
}
if (is_address_gen_insn(arch, dl)) {
istat->bad++;
ann_data_stat.no_mem_ops++;
- return NO_TYPE;
+ fill.replay = DTYPE_REPLAY_ADDRGEN;
+ fill.istat = istat;
+ goto out_none_type;
}
for_each_insn_op_loc(&loc, i, op_loc) {
@@ -3059,7 +3280,10 @@ __hist_entry__get_data_type(struct hist_entry *he, const struct arch *arch,
if (mem_type == NULL && is_stack_canary(arch, op_loc)) {
istat->good++;
*type_offset = 0;
- return &canary_type;
+ fill.replay = DTYPE_REPLAY_CANARY;
+ fill.istat = istat;
+ mem_type = &canary_type;
+ goto out_cached;
}
if (mem_type)
@@ -3076,11 +3300,24 @@ __hist_entry__get_data_type(struct hist_entry *he, const struct arch *arch,
he->stat.period);
}
*type_offset = dloc.type_offset;
- return mem_type ?: NO_TYPE;
- }
-
- /* retry with a fused instruction */
- return NULL;
+ fill.replay = DTYPE_REPLAY_MEM;
+ fill.istat = istat;
+ fill.type_offset = dloc.type_offset;
+ goto out_cached;
+ }
+
+ fill.replay = DTYPE_REPLAY_NOMEM;
+ ret = NULL;
+ goto out_add;
+
+out_cached:
+ fill.type = mem_type;
+out_none_type:
+ ret = mem_type ?: NO_TYPE;
+out_add:
+ annotated_data_stat__delta(&fill.stat_delta, &before);
+ dtype_cache__add(map__dso(ms->map), ms->sym->start + dl->al.offset, &fill);
+ return ret;
}
/**
diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h
index 03d0b8a2288592a5..0d214b34eb9eae39 100644
--- a/tools/perf/util/annotate.h
+++ b/tools/perf/util/annotate.h
@@ -675,6 +675,7 @@ int annotate_get_basic_blocks(struct symbol *sym, s64 src, s64 dst,
struct list_head *head);
void debuginfo_cache__delete(void);
+void dtype_cache__delete(void);
int annotation_br_cntr_entry(char **str, int br_cntr_nr, u64 *br_cntr,
int num_aggr, struct evsel *evsel);
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 4d9450ef5df8e9ce..62f75541e75966e9 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -265,6 +265,7 @@ void perf_session__delete(struct perf_session *session)
auxtrace__free(session);
auxtrace_index__free(&session->auxtrace_index);
debuginfo_cache__delete();
+ dtype_cache__delete();
perf_session__destroy_kernel_maps(session);
perf_decomp__release_events(session->decomp_data.decomp);
perf_env__exit(&session->header.env);
--
2.56.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v1 1/1] perf annotate-data: Cache the per-instruction data type resolution
2026-12-05 13:01 [PATCH v1 1/1] perf annotate-data: Cache the per-instruction data type resolution Arnaldo Carvalho de Melo
@ 2026-12-05 13:18 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-05 13:18 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo; +Cc: linux-perf-users
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] perf annotate-data: wrong ms key in
__hist_entry__get_data_type()
--
commit 91c505c1c8646d1d466660ac433e03bbaba797f2
Author: Arnaldo Carvalho de Melo <acme@kernel.org>
perf annotate-data: Cache the per-instruction data type resolution
This commit adds a session-global hashmap (dtype_cache) keyed on (dso, ip)
to memoize per-instruction data type resolution and replay its statistics
in __hist_entry__get_data_type().
> diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
> index 86a4beec6528e..321c0af4265c3 100644
> --- a/tools/perf/util/annotate.c
> +++ b/tools/perf/util/annotate.c
[ ... ]
> @@ -2847,32 +3052,48 @@ __hist_entry__get_data_type(struct hist_entry *he, const struct arch *arch,
> struct map_symbol *ms = &he->ms;
> struct annotated_insn_loc loc;
> struct annotated_op_loc *op_loc;
> - struct annotated_data_type *mem_type;
> + struct annotated_data_type *mem_type = NULL, *ret;
> struct annotated_item_stat *istat;
> + struct dtype_cache fill = { .replay = DTYPE_REPLAY_NONE };
> + struct dtype_cache *dc;
> + struct annotated_data_stat before;
> int i;
>
> + dc = dtype_cache__find(map__dso(ms->map), ms->sym->start + dl->al.offset);
> + if (dc)
> + return dtype_cache__replay(dc, he, type_offset);
[ ... ]
> +out_add:
> + annotated_data_stat__delta(&fill.stat_delta, &before);
> + dtype_cache__add(map__dso(ms->map), ms->sym->start + dl->al.offset, &fill);
> + return ret;
> }
[Severity: Medium]
Can he->ms differ from the symbol that owns dl, or have a NULL sym or map,
when __hist_entry__get_data_type() keys dtype_cache on it?
In __hist_entry__tui_annotate(), browser.he->ms is only updated from ms when
hbt != NULL && he != &annotate_he. When hbt is NULL (in perf report, perf
annotate, or perf c2c) or on nested calls in perf top, browser.he->ms stays
on the caller or root entry while dl comes from ms.
With code-with-type enabled, annotate_browser__callq() passes the callee's
target_ms with the caller's browser->he. disasm_line__snprint_type_info()
then looks up and inserts under the caller's (dso, sym->start + offset),
replaying the caller's verdicts onto the callee and poisoning dtype_cache.
If he->ms.sym is NULL, such as when do_annotate() annotates a synthetic
symbol from symbol__new_unresolved() in perf_evsel__hists_browse(),
ms->sym->start also causes a NULL pointer dereference on entry before any
early returns.
Passing the annotated map_symbol to __hist_entry__get_data_type() (and
checking ms->map and ms->sym) would ensure the cache key matches dl.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205130136.5962-1-acme@kernel.org?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-05 13:18 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 13:01 [PATCH v1 1/1] perf annotate-data: Cache the per-instruction data type resolution Arnaldo Carvalho de Melo
2026-12-05 13:18 ` sashiko-bot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.