From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796475707; b=zVVA5J28jslAuf/bN9KJucgLeARJFQlOaMe6XYEbse0CsfQQwuTqzor4EtL0eOZl1a/f +rISR+/EL2fWarjKIMELm0pjw6BzXDlJSMs/TfE4qRvUYF3SxvJPgyzKiVKXfyUMXLhzR +JrX8hVn+ZkV87ii7VmcKOmnCFVFyvrGlwdbws/OkoHs6vy085OKwUuC9p1DvvXAh1tT5 7sFyp5WOcBz03cDNYw5Lmy5Q1KuYNNro1cm5XJUan4/8aabdADKgP1OPEWutfbO+4Yy2B kCcDaopjAqziOtk/wSr+lw4xiBZogw18332RSn6ZxkrgfVOF/J+0LjtOHOGHkTFEboQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796475707; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Transfer-Encoding; bh=bYVtwdTcBYhExW8efk6/+f5W2ZYRCBEWg9uVmFYNI6k=; b=KqFHBGdDJQApujq1q+47VIOXddr0CUf54T+YxRCGCR59l2w8TGCAsaE6QiSoXxIzP/82 ECIwG/ZMaOS4mBUFKk+p6usQ/QDYSGs8lnl3jUdeDIezuwwW+Aty5yiTixKWE4ZfrGc1U BY7/T+N8DYm+Nqxx8c5rFEBI73W2zLVtwatksp/3EijpEytuGLZYi6v4POz8rFyTX6sF2 GRncezgfremBqHiFUHyQKmUOi0D8eEQ2GN4ENv7Q7AjKRgRVt1XuV0IIeIbTSL6E8QNF2 oiX2b/OfJJtYxmxoETBwQp9zdNAoDtAiUJgiy9ZDqy1pg1xN38AZd8657FqE96oT8jA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BtoxWTE1; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42806606A338 for ; Sat, 5 Dec 2026 13:01:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BtoxWTE1 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E5F2D1F0092B; Sat, 5 Dec 2026 13:01:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796475707; bh=bYVtwdTcBYhExW8efk6/+f5W2ZYRCBEWg9uVmFYNI6k=; h=From:To:Cc:Subject:Date; b=BtoxWTE1NucPAQkIEDRAYYZCsZQms2BN1mfGVwUnE828XryHtfT0yDX/2bfYgBHca 2m1zzfY2K8KWUv/nikRNcdhVBXbqDJ9QgvUMa9uI/ro/jZ9nVGYt3OP+jJzyacVhGT JE+YsQfoQfwiMxnv9uwAWEh4WeZ9BQalR39nLktHXBnHDsAhawTdPBvty75M3cMRIL n3o1TCxqrY736vdeC8e55Yi20FNGAJ6Kz9wJKhuD0iz3q4xn008aNmiYM4We/mplbD d167b6aE4TDt+2MUpyOIn8DiqMDXCi5HbVMvB+s1OnBBwdEynEYUId6dd+oseMiH10 Rflwt3SmH3K0Q== From: Arnaldo Carvalho de Melo To: Namhyung Kim Cc: Ingo Molnar , Thomas Gleixner , James Clark , Jiri Olsa , Ian Rogers , Adrian Hunter , Clark Williams , linux-perf-users@vger.kernel.org, Arnaldo Carvalho de Melo , Arnaldo Carvalho de Melo Subject: [PATCH v1 1/1] perf annotate-data: Cache the per-instruction data type resolution Date: Sat, 5 Dec 2026 10:01:36 -0300 Message-ID: <20261205130136.5962-1-acme@kernel.org> X-Mailer: git-send-email 2.54.0 X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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