Skip to contentSkip to conversation

perf script: Bounded and lazy symbol loading

PATCH Superseded Alireza Haghdoost 3 participants 18 messages Sep 15 – Sep 19 perf events

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

cover perf script: Bounded and lazy symbol loading

perf script loads the entire ELF symbol table of every DSO that appears in a sample, allocating each symbol into an rb-tree held until process exit. Therefore, a large enough profile turns symbol loading into an OOM kill. This is not scalable for profiling a large cgroup with a lot of large binaries on a production system with limited free memory.

This series adds two independent, opt-in mechanisms and a leading regression fix they build on:

[1/4] Fix a broken "#ifdef ELF_C_READ_MMAP" guard so perf actually
mmaps ELF files instead of malloc'ing section data. This is a standalone regression fix introduced by 22dd1ac91a77.

[2/4] --max-symbol-bytes <size>: a byte budget on struct symbol
allocations (and the lazy index) enforced at the ELF symbol loader, degrading to [unknown] with a warning past the cap. An unbounded profile doesn't just risk OOM-killing itself. It also forces memory pressure on the whole host, pushing the kernel to reclaim from co-located latency-sensitive processes. Capping it lets the user bound that footprint up front and choose the trade-off explicitly.

[3/4] --lazy-load-symbols: build a compact per-DSO sorted index and
resolve only the sampled addresses, reading names via pread() from the file's string table at lookup time. On the production fixture, peak RssAnon drops from 265 MiB to 39 MiB (6.8x) and wall time from 3.1 s to 1.85 s (1.7x). Memory optimizations usually cost time; this one does not because lazy loading skips a lot of calloc and demangle calls. The output is byte-identical to the default loader.

[4/4] Documentation and a shell test.

---
Alireza Haghdoost (4):
perf symbols: Fix broken ELF_C_READ_MMAP fallback guard
perf script: Add --max-symbol-bytes to bound ELF symbol memory perf script: Add --lazy-load-symbols for lazy symbol loading perf script: Document and test --lazy-load-symbols and --max-symbol-bytes

 tools/perf/Documentation/perf-script.txt           |  24 +
 tools/perf/builtin-script.c                        |  33 ++
 tools/perf/tests/shell/script_lazy_load_symbols.sh | 120 +++++
 tools/perf/util/dso.c                              |  10 +
 tools/perf/util/dso.h                              |  29 ++
 tools/perf/util/map.c                              |   9 +-
 tools/perf/util/symbol-elf.c                       | 561 +++++++++++++++++++++
 tools/perf/util/symbol-minimal.c                   |  11 +
 tools/perf/util/symbol.c                           |  87 +++-
 tools/perf/util/symbol.h                           |  18 +-
 tools/perf/util/symbol_conf.h                      |   2 +
 11 files changed, 875 insertions(+), 29 deletions(-)

---
base-commit: aa18964dd64511305de0711fed912054da6f5d18
change-id: 20260915-perf-symbol-memory-send-e7cfca1ac3d9

Best regards,

Signature · 1 line
Alireza Haghdoost <[email protected]>
#1 lore raw

PATCH 1/4 perf symbols: Fix broken ELF_C_READ_MMAP fallback guard

This patch makes perf mmap ELF files. This was the intended behavior, regressed by commit 22dd1ac91a77 ("tools: Remove feature-libelf-mmap feature detection"). ELF_C_READ_MMAP is an Elf_Cmd enumerator, not a preprocessor macro, so the #ifdef is false on every libelf that provides it. objtool uses ELF_C_READ_MMAP without an #ifdef guard and has been fine for five years.

elf_getdata() still allocates when translation is needed (32-on-64 or cross-endian), and libelf falls back to internal reads on non-seekable fds.

Fixes 22dd1ac91a77 ("tools: Remove feature-libelf-mmap feature detection")
Assisted-by Kimi:K3
---
 tools/perf/util/symbol.h | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
tools/perf/util/symbol.h +2 -9
index d0bac824c79c..e5cef16b240d 100644
@@ -57,15 +57,8 @@ static inline bool is_livepatch_symbol(const char *str)
 	return strstarts(str, KLP_SYM_PREFIX);
 }
 
-/*
- * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
- * for newer versions we can use mmap to reduce memory usage:
- */
-#ifdef ELF_C_READ_MMAP
-# define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
-#else
-# define PERF_ELF_C_READ_MMAP ELF_C_READ
-#endif
+/* libelf falls back to internal reads when mmap fails (e.g. non-seekable fd). */
+#define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
 
 #ifdef HAVE_LIBELF_SUPPORT
 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
Signature · 1 line
Git-155)
#2 lore raw

Re: [PATCH 1/4] perf symbols: Fix broken ELF_C_READ_MMAP fallback guard

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] perf: build failure on systems lacking ELF_C_READ_MMAP enum --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] perf: build failure on systems lacking ELF_C_READ_MMAP enum
--

commit d38dd3de1c465f53af2cb4e7df04744c926b7ed6
Author: Alireza Haghdoost <[email protected]>

perf symbols: Fix broken ELF_C_READ_MMAP fallback guard

This patch attempts to make perf mmap ELF files by unconditionally defining PERF_ELF_C_READ_MMAP to ELF_C_READ_MMAP. It removes the previous guard because ELF_C_READ_MMAP is an enum rather than a preprocessor macro.

Quoted 8 lines
tools/perf/util/symbol.h +0 -1
@@ -57,15 +57,8 @@ static inline bool is_livepatch_symbol(const char *str)
 	return strstarts(str, KLP_SYM_PREFIX);
 }
 
-/*

tools/perf/util/symbol.h static inline bool is_livepatch_symbol(const char *str) · line 61

Quoted 10 lines
tools/perf/util/symbol.h +2 -8
- * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
- * for newer versions we can use mmap to reduce memory usage:
- */
-#ifdef ELF_C_READ_MMAP
-# define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
-#else
-# define PERF_ELF_C_READ_MMAP ELF_C_READ
-#endif
+/* libelf falls back to internal reads when mmap fails (e.g. non-seekable fd). */
+#define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP

[Severity: Low]
Will this cause compilation errors on systems where the libelf.h header lacks the ELF_C_READ_MMAP enum?

By unconditionally assigning PERF_ELF_C_READ_MMAP to ELF_C_READ_MMAP in tools/perf/util/symbol.h, this bypasses build feature detection. On systems with libelf implementations that do not provide ELF_C_READ_MMAP, such as elftoolchain on FreeBSD and macOS, this triggers an undeclared identifier compilation error.

The commit message notes that objtool uses an #ifndef guard which works fine, but since ELF_C_READ_MMAP is an enum in libelf, that #ifndef in objtool silently evaluates to true and incorrectly downgrades all mmap reads to standard reads instead of actually using mmap.

Could we restore a proper feature detection mechanism here rather than relying on an unconditional definition?

Quoted 3 lines
tools/perf/util/symbol.h +0 -0
 #ifdef HAVE_LIBELF_SUPPORT
 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH 1/4] perf symbols: Fix broken ELF_C_READ_MMAP fallback guard

Quoted 2 lines

Could we restore a proper feature detection mechanism here rather than relying on an unconditional definition?

Thanks, this is a valid concern. I’ll restore the compile-time feature-libelf-mmap probe removed by 22dd1ac91a77 and use it to select between ELF_C_READ_MMAP and ELF_C_READ in v2.

maintainer
#4 lore raw

Re: [PATCH 1/4] perf symbols: Fix broken ELF_C_READ_MMAP fallback guard

Hello,

Quoted 6 lines · Alireza Haghdoost
Quoted 2 lines

Could we restore a proper feature detection mechanism here rather than relying on an unconditional definition?

Thanks, this is a valid concern. I’ll restore the compile-time feature-libelf-mmap probe removed by 22dd1ac91a77 and use it to select between ELF_C_READ_MMAP and ELF_C_READ in v2.

Please don't.

I think we already require much recent versions of elfutils like 0.157 for libdw which is released more than 10 years ago. So I think it's ok just to assume _MMAP is available.

Thanks,
Namhyung

#5 lore raw

PATCH 2/4 perf script: Add --max-symbol-bytes to bound ELF symbol memory

In reply to Alireza Haghdoost

perf script eagerly materializes every ELF symbol into an rb-tree kept until process exit. Large profiles can therefore consume substantial anonymous memory, causing perf script to be OOM-killed or forcing the kernel to reclaim memory from co-located workloads.

This patch adds --max-symbol-bytes to bound struct symbol allocations. Once the budget is reached, the ELF loader stops loading symbols, warns once, and lets unresolved addresses appear as [unknown]. This allows users to bound the memory footprint upfront and explicitly choose between complete symbolization and avoiding unbounded host memory pressure. perf record already provides a similar --max-size option to bound disk usage.

The counter includes every symbol__new() allocation, but this patch enforces the limit only in the ELF loader, which is the source of the unbounded memory growth addressed here. In this path, reaching the limit can safely produce [unknown] symbols. Other loaders currently treat a failed symbol allocation as an error. Capping those paths would therefore require separate changes whose complexity may outweigh the potential memory savings.

Sizes require a B/K/M/G suffix. Zero, the default, means unlimited.

Assisted-by Kimi:K3
---
 tools/perf/builtin-script.c   | 31 +++++++++++++++++++++++++++++++
 tools/perf/util/symbol-elf.c  |  7 +++++++
 tools/perf/util/symbol.c      | 29 +++++++++++++++++++++++++++--
 tools/perf/util/symbol.h      |  3 +++
 tools/perf/util/symbol_conf.h |  1 +
 5 files changed, 69 insertions(+), 2 deletions(-)
tools/perf/builtin-script.c +31 -0
index ad8ca08ceb5f..50fa6ca6455a 100644
@@ -68,6 +68,7 @@
 #include "util/thread.h"
 #include "util/thread_map.h"
 #include "util/time-utils.h"
+#include "util/units.h"
 #include "util/tool.h"
 #include "util/trace-event.h"
 #include "util/unwind.h"
@@ -4035,6 +4036,33 @@ static int parse_callret_trace(const struct option *opt __maybe_unused,
 	return 0;
 }
 
+static int parse_max_symbol_bytes(const struct option *opt,
+				  const char *str, int unset)
+{
+	unsigned long *max_bytes = (unsigned long *)opt->value;
+	static struct parse_tag size_tags[] = {
+		{ .tag  = 'B', .mult = 1       },
+		{ .tag  = 'K', .mult = 1 << 10 },
+		{ .tag  = 'M', .mult = 1 << 20 },
+		{ .tag  = 'G', .mult = 1 << 30 },
+		{ .tag  = 0 },
+	};
+	unsigned long bytes;
+
+	if (unset) {
+		*max_bytes = 0;
+		return 0;
+	}
+
+	bytes = parse_tag_value(str, size_tags);
+	if (bytes != (unsigned long)-1) {
+		*max_bytes = bytes;
+		return 0;
+	}
+
+	return -1;
+}
+
 int cmd_script(int argc, const char **argv)
 {
 	bool show_full_info = false;
@@ -4135,6 +4163,9 @@ int cmd_script(int argc, const char **argv)
 		     "Set the maximum stack depth when parsing the callchain, "
 		     "anything beyond the specified depth will be ignored. "
 		     "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
+	OPT_CALLBACK(0, "max-symbol-bytes", &symbol_conf.max_symbol_bytes,
+		     "size", "Limit bytes for ELF struct symbol (e.g. 128M; 0=unlimited)",
+		     parse_max_symbol_bytes),
 	OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
 	OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to previous event"),
 	OPT_BOOLEAN('I', "show-info", &show_full_info,
tools/perf/util/symbol-elf.c +7 -0
index e955c3feddcd..914e42d21f70 100644
@@ -1634,6 +1634,13 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		int is_label = elf_sym__is_label(&sym);
 		const char *section_name;
 		bool used_opd = false;
+		if (symbol_conf.max_symbol_bytes &&
+		    symbol__bytes_used() >= symbol_conf.max_symbol_bytes) {
+			pr_warning_once("perf: symbol memory budget exceeded (%lu bytes), "
+					"remaining symbols will be [unknown]\n",
+					symbol_conf.max_symbol_bytes);
+			break;
+		}
 
 		if (!is_label && !elf_sym__filter(&sym))
 			continue;
tools/perf/util/symbol.c +27 -2
index 3587ad243159..62a4f91c2f5d 100644
@@ -310,14 +310,35 @@ void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms)
 		curr->end = roundup(curr->start, 4096) + 4096;
 }
 
+static size_t symbol_bytes_used;
+
+size_t symbol__bytes_used(void)
+{
+	return symbol_bytes_used;
+}
+
+void symbol__account_bytes(size_t bytes)
+{
+	symbol_bytes_used += bytes;
+}
+
+void symbol__unaccount_bytes(size_t bytes)
+{
+	symbol_bytes_used -= bytes;
+}
+
 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
 {
 	size_t namelen = strlen(name) + 1;
-	struct symbol *sym = calloc(1, (symbol_conf.priv_size +
-					sizeof(*sym) + namelen));
+	size_t alloc_size = symbol_conf.priv_size + sizeof(struct symbol) + namelen;
+	struct symbol *sym;
+
+	sym = calloc(1, alloc_size);
 	if (sym == NULL)
 		return NULL;
 
+	symbol__account_bytes(alloc_size);
+
 	if (symbol_conf.priv_size) {
 		if (symbol_conf.init_annotation) {
 			struct annotation *notes = (void *)sym;
@@ -341,6 +362,9 @@ struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *
 
 void symbol__delete(struct symbol *sym)
 {
+	size_t alloc_size = symbol_conf.priv_size + sizeof(struct symbol) +
+			    sym->namelen + 1;
+
 	if (symbol_conf.priv_size) {
 		if (symbol_conf.init_annotation) {
 			struct annotation *notes = symbol__annotation(sym);
@@ -348,6 +372,7 @@ void symbol__delete(struct symbol *sym)
 			annotation__exit(notes);
 		}
 	}
+	symbol__unaccount_bytes(alloc_size);
 	free(((void *)sym) - symbol_conf.priv_size);
 }
 
tools/perf/util/symbol.h +3 -0
index e5cef16b240d..0d5d3792aac1 100644
@@ -228,6 +228,9 @@ void symbol__elf_init(void);
 int symbol__annotation_init(void);
 
 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name);
+size_t symbol__bytes_used(void);
+void symbol__account_bytes(size_t bytes);
+void symbol__unaccount_bytes(size_t bytes);
 size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
 				      const struct addr_location *al,
 				      bool unknown_as_addr,
tools/perf/util/symbol_conf.h +1 -0
index 71f60081a85b..6a16c5badd5e 100644
@@ -120,6 +120,7 @@ struct symbol_conf {
 	int		pad_output_len_dso;
 	int		group_sort_idx;
 	int		addr_range;
+	unsigned long	max_symbol_bytes;
 	DECLARE_BITMAP(parallelism_filter, MAX_NR_CPUS + 1);
 };
 
Signature · 1 line
Git-155)
#6 lore raw

Re: [PATCH 2/4] perf script: Add --max-symbol-bytes to bound ELF symbol memory

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] perf: data race and underflow on global symbol_bytes_used counter - [High] perf: early abort of…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] perf: data race and underflow on global symbol_bytes_used counter
- [High] perf: early abort of ELF symbol loading stretches symbols over address gaps
- [Medium] perf: u16 truncation in sym->namelen causes permanent memory budget leak --

commit c9eea40a0a34f49638af63c080650086fd45e844
Author: Alireza Haghdoost <[email protected]>

perf script: Add --max-symbol-bytes to bound ELF symbol memory

This patch adds a --max-symbol-bytes option to bound the memory allocated for struct symbol when loading ELF symbols. Once the budget is reached, further loading is aborted to prevent OOM kills.

Quoted 5 lines
tools/perf/util/symbol-elf.c +0 -0
index e955c3feddcd6..914e42d21f70f 100644

tools/perf/util/symbol-elf.c dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss, · line 1642

Quoted 9 lines
tools/perf/util/symbol-elf.c +6 -0
@@ -1634,6 +1634,13 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		int is_label = elf_sym__is_label(&sym);
 		const char *section_name;
 		bool used_opd = false;
+		if (symbol_conf.max_symbol_bytes &&
+		    symbol__bytes_used() >= symbol_conf.max_symbol_bytes) {
+			pr_warning_once("perf: symbol memory budget exceeded (%lu bytes), "
+					"remaining symbols will be [unknown]\n",
+					symbol_conf.max_symbol_bytes);
+			break;

[Severity: High]
Since ELF symbol tables are generally not sorted by address (they are grouped by binding), breaking out of the loop early creates random gaps in the address space. Following this, symbols__fixup_end() iterates the partially loaded rb-tree and stretches any zero-length symbols to the start address of the very next loaded symbol (prev->end = curr->start).

Will this cause execution samples originating in the missing un-symbolized gaps to be incorrectly attributed to completely unrelated, stretched zero-length symbols instead of falling back to [unknown]?

[ ... ]

Quoted 4 lines
tools/perf/util/symbol.c +0 -0
index 3587ad2431590..62a4f91c2f5db 100644

[ ... ]

Quoted 6 lines
tools/perf/util/symbol.c +6 -0
+static size_t symbol_bytes_used;
+
+size_t symbol__bytes_used(void)
+{
+	return symbol_bytes_used;
+}

tools/perf/util/symbol.c void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms) · line 328

Quoted 10 lines
tools/perf/util/symbol.c +10 -0
+
+void symbol__account_bytes(size_t bytes)
+{
+	symbol_bytes_used += bytes;
+}
+
+void symbol__unaccount_bytes(size_t bytes)
+{
+	symbol_bytes_used -= bytes;
+}

[Severity: High]
The global symbol_bytes_used is updated without atomic operations. In multi-threaded tools like perf top where different DSOs are loaded concurrently by different threads, will concurrent updates cause lost increments?

If subsequent decrements succeed, this could underflow symbol_bytes_used to SIZE_MAX, instantly exhausting the memory budget and breaking all further symbol resolution.

Quoted 15 lines
tools/perf/util/symbol.c +7 -2
+
 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
 {
 	size_t namelen = strlen(name) + 1;
-	struct symbol *sym = calloc(1, (symbol_conf.priv_size +
-					sizeof(*sym) + namelen));
+	size_t alloc_size = symbol_conf.priv_size + sizeof(struct symbol) + namelen;
+	struct symbol *sym;
+
+	sym = calloc(1, alloc_size);
 	if (sym == NULL)
 		return NULL;
 
+	symbol__account_bytes(alloc_size);
+

[ ... ]

tools/perf/util/symbol.c struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char * · line 366

Quoted 4 lines

void symbol__delete(struct symbol *sym)
{

tools/perf/util/symbol.c +2 -0
+	size_t alloc_size = symbol_conf.priv_size + sizeof(struct symbol) +
+			    sym->namelen + 1;

[Severity: Medium]
If a symbol name exceeds 65,535 bytes (which is possible with deeply mangled C++ or Rust templates), the u16 sym->namelen field silently wraps when it is assigned during allocation.

When symbol__delete() recalculates the budget to subtract using this truncated u16 value, will it result in a much smaller subtraction than the original addition, acting as a permanent memory budget leak that artificially exhausts the limit?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw

Re: [PATCH 2/4] perf script: Add --max-symbol-bytes to bound ELF symbol memory

Thanks, all three findings are valid.

I’ll make the global accounting counter atomic, ensure that a budget-truncated symbol tree does not fill ranges containing omitted symbols, and use the full stored name length instead of the truncated u16 namelen when unaccounting allocations.

I’ll include these fixes in v2.

maintainer
#8 lore raw

Re: [PATCH 2/4] perf script: Add --max-symbol-bytes to bound ELF symbol memory

In reply to Alireza Haghdoost
Quoted 6 lines · On Tue, Sep 15, 2026 at 11:42:44AM -0700, Alireza Haghdoost via B4 Relay wrote:

From: Alireza Haghdoost <[email protected]>

perf script eagerly materializes every ELF symbol into an rb-tree kept until process exit. Large profiles can therefore consume substantial anonymous memory, causing perf script to be OOM-killed or forcing the kernel to reclaim memory from co-located workloads.

It's not only for perf script. Other commands like perf report and perf annotate would have the same issue.

Quoted 8 lines

This patch adds --max-symbol-bytes to bound struct symbol allocations. Once the budget is reached, the ELF loader stops loading symbols, warns once, and lets unresolved addresses appear as [unknown]. This allows users to bound the memory footprint upfront and explicitly choose between complete symbolization and avoiding unbounded host memory pressure. perf record already provides a similar --max-size option to bound disk usage.

Please add documentation when you add a new command line option.

Quoted 8 lines

The counter includes every symbol__new() allocation, but this patch enforces the limit only in the ELF loader, which is the source of the unbounded memory growth addressed here. In this path, reaching the limit can safely produce [unknown] symbols. Other loaders currently treat a failed symbol allocation as an error. Capping those paths would therefore require separate changes whose complexity may outweigh the potential memory savings.

What's the other loaders you meant?

I don't see it produces [unknown] symbols. Do you mean it just stops loading symbols when it hits the limit?

Quoted 74 lines

Sizes require a B/K/M/G suffix. Zero, the default, means unlimited.

Assisted-by Kimi:K3
---
 tools/perf/builtin-script.c   | 31 +++++++++++++++++++++++++++++++
 tools/perf/util/symbol-elf.c  |  7 +++++++
 tools/perf/util/symbol.c      | 29 +++++++++++++++++++++++++++--
 tools/perf/util/symbol.h      |  3 +++
 tools/perf/util/symbol_conf.h |  1 +
 5 files changed, 69 insertions(+), 2 deletions(-)
tools/perf/builtin-script.c +31 -0
index ad8ca08ceb5f..50fa6ca6455a 100644
@@ -68,6 +68,7 @@
 #include "util/thread.h"
 #include "util/thread_map.h"
 #include "util/time-utils.h"
+#include "util/units.h"
 #include "util/tool.h"
 #include "util/trace-event.h"
 #include "util/unwind.h"
@@ -4035,6 +4036,33 @@ static int parse_callret_trace(const struct option *opt __maybe_unused,
 	return 0;
 }
 
+static int parse_max_symbol_bytes(const struct option *opt,
+				  const char *str, int unset)
+{
+	unsigned long *max_bytes = (unsigned long *)opt->value;
+	static struct parse_tag size_tags[] = {
+		{ .tag  = 'B', .mult = 1       },
+		{ .tag  = 'K', .mult = 1 << 10 },
+		{ .tag  = 'M', .mult = 1 << 20 },
+		{ .tag  = 'G', .mult = 1 << 30 },
+		{ .tag  = 0 },
+	};
+	unsigned long bytes;
+
+	if (unset) {
+		*max_bytes = 0;
+		return 0;
+	}
+
+	bytes = parse_tag_value(str, size_tags);
+	if (bytes != (unsigned long)-1) {
+		*max_bytes = bytes;
+		return 0;
+	}
+
+	return -1;
+}
+
 int cmd_script(int argc, const char **argv)
 {
 	bool show_full_info = false;
@@ -4135,6 +4163,9 @@ int cmd_script(int argc, const char **argv)
 		     "Set the maximum stack depth when parsing the callchain, "
 		     "anything beyond the specified depth will be ignored. "
 		     "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
+	OPT_CALLBACK(0, "max-symbol-bytes", &symbol_conf.max_symbol_bytes,
+		     "size", "Limit bytes for ELF struct symbol (e.g. 128M; 0=unlimited)",
+		     parse_max_symbol_bytes),
 	OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
 	OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to previous event"),
 	OPT_BOOLEAN('I', "show-info", &show_full_info,
tools/perf/util/symbol-elf.c +0 -0
index e955c3feddcd..914e42d21f70 100644

tools/perf/util/symbol-elf.c dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss, · line 1636

Quoted 3 lines
tools/perf/util/symbol-elf.c +0 -0
@@ -1634,6 +1634,13 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		int is_label = elf_sym__is_label(&sym);
 		const char *section_name;
 		bool used_opd = false;

Please keep a blank line after declaration.

Quoted 31 lines
tools/perf/util/symbol-elf.c +7 -0
+		if (symbol_conf.max_symbol_bytes &&
+		    symbol__bytes_used() >= symbol_conf.max_symbol_bytes) {
+			pr_warning_once("perf: symbol memory budget exceeded (%lu bytes), "
+					"remaining symbols will be [unknown]\n",
+					symbol_conf.max_symbol_bytes);
+			break;
+		}
 
 		if (!is_label && !elf_sym__filter(&sym))
 			continue;
tools/perf/util/symbol.c +13 -0
index 3587ad243159..62a4f91c2f5d 100644
@@ -310,14 +310,35 @@ void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms)
 		curr->end = roundup(curr->start, 4096) + 4096;
 }
 
+static size_t symbol_bytes_used;
+
+size_t symbol__bytes_used(void)
+{
+	return symbol_bytes_used;
+}
+
+void symbol__account_bytes(size_t bytes)
+{
+	symbol_bytes_used += bytes;
+}
+
+void symbol__unaccount_bytes(size_t bytes)

tools/perf/util/symbol.c void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms) · line 333

Quoted 10 lines
tools/perf/util/symbol.c +5 -2
+{
+	symbol_bytes_used -= bytes;
+}
+
 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
 {
 	size_t namelen = strlen(name) + 1;
-	struct symbol *sym = calloc(1, (symbol_conf.priv_size +
-					sizeof(*sym) + namelen));
+	size_t alloc_size = symbol_conf.priv_size + sizeof(struct symbol) + namelen;

The convention is 'sizeof(*sym)' rather than 'sizeof(struct symbol)'. So that it can easily handle type changes in the future.

Quoted 12 lines
tools/perf/util/symbol.c +5 -0
+	struct symbol *sym;
+
+	sym = calloc(1, alloc_size);
 	if (sym == NULL)
 		return NULL;
 
+	symbol__account_bytes(alloc_size);
+
 	if (symbol_conf.priv_size) {
 		if (symbol_conf.init_annotation) {
 			struct annotation *notes = (void *)sym;

tools/perf/util/symbol.c struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char * · line 366

Quoted 5 lines
tools/perf/util/symbol.c +2 -0
@@ -341,6 +362,9 @@ struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *
 
 void symbol__delete(struct symbol *sym)
 {
+	size_t alloc_size = symbol_conf.priv_size + sizeof(struct symbol) +
+			    sym->namelen + 1;

Ditto.

Thanks,
Namhyung

Quoted 43 lines
tools/perf/util/symbol.c +2 -0
+
 	if (symbol_conf.priv_size) {
 		if (symbol_conf.init_annotation) {
 			struct annotation *notes = symbol__annotation(sym);
@@ -348,6 +372,7 @@ void symbol__delete(struct symbol *sym)
 			annotation__exit(notes);
 		}
 	}
+	symbol__unaccount_bytes(alloc_size);
 	free(((void *)sym) - symbol_conf.priv_size);
 }
 
tools/perf/util/symbol.h +3 -0
index e5cef16b240d..0d5d3792aac1 100644
@@ -228,6 +228,9 @@ void symbol__elf_init(void);
 int symbol__annotation_init(void);
 
 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name);
+size_t symbol__bytes_used(void);
+void symbol__account_bytes(size_t bytes);
+void symbol__unaccount_bytes(size_t bytes);
 size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
 				      const struct addr_location *al,
 				      bool unknown_as_addr,
tools/perf/util/symbol_conf.h +1 -0
index 71f60081a85b..6a16c5badd5e 100644
@@ -120,6 +120,7 @@ struct symbol_conf {
 	int		pad_output_len_dso;
 	int		group_sort_idx;
 	int		addr_range;
+	unsigned long	max_symbol_bytes;
 	DECLARE_BITMAP(parallelism_filter, MAX_NR_CPUS + 1);
 };
 
Signature · 2 lines
-- 
Git-155)
#9 lore raw

Re: [PATCH 2/4] perf script: Add --max-symbol-bytes to bound ELF symbol memory

Quoted 8 lines
Quoted 7 lines

The counter includes every symbol__new() allocation, but this patch enforces the limit only in the ELF loader, which is the source of the unbounded memory growth addressed here. In this path, reaching the limit can safely produce [unknown] symbols. Other loaders currently treat a failed symbol allocation as an error. Capping those paths would therefore require separate changes whose complexity may outweigh the potential memory savings.

Quoted 5 lines

What's the other loaders you meant?

I don't see it produces [unknown] symbols. Do you mean it just stops loading symbols when it hits the limit?

The other loaders are kallsyms, JIT/perf-map, and libbfd. Their allocations are counted by symbol__new(), but the current patch enforces the limit only in the libelf loader. I will name those paths explicitly in v2 of this patch series.

The loader stops adding ELF symbols once the budget is reached and warns once. Later unresolved IPs are printed as [unknown] by symbol_fprintf.c:40 when sym is NULL.

#10 lore raw

PATCH 3/4 perf script: Add --lazy-load-symbols for lazy symbol loading

In reply to Alireza Haghdoost

perf script eagerly materializes eligible symbols from every DSO encountered in samples. On a production fixture, it loaded about 765k symbols to resolve about 45k distinct (DSO, symbol) frames, exceeding the memory available in a memory-constrained cgroup.

This patch adds --lazy-load-symbols for userspace ELF DSOs. It builds a compact sorted index, resolves sampled addresses by binary search, reads symbol names with pread(), and caches resolved symbols in the existing rb-tree. Lazy lookup retains one CLOEXEC file descriptor per indexed DSO. If the descriptor cannot be retained, perf discards the index and eagerly loads that DSO instead.

On the same fixture, peak RssAnon drops from 265 MiB to 39 MiB and wall time from 3.1 seconds to 1.85 seconds. Memory optimizations usually cost time; this one does not because lazy loading skips many unnecessary calloc() calls and demangling operations. Output was byte-identical on the tested x86-64 workloads and an aarch64 capture.

Lazy loading is most effective when samples reference only a small fraction of the available symbols, such as profiles spanning many large DSOs. It still builds an index proportional to the total symbol count. Eager loading remains available for dense symbol coverage or cases requiring its broader ELF and architecture support.

This does not claim full parity with the eager loader. Lazy loading supports the common userspace ELF symtab/dynsym case, with these known differences:

- .gnu_debugdata merging and PPC64 .opd are not handled.
- SHT_NOBITS re-reading, IFUNC PLT naming, and exact-tie alias ordering are unreachable or output-equivalent on x86-64.

Assisted-by Kimi:K3
---
 tools/perf/builtin-script.c      |   4 +-
 tools/perf/util/dso.c            |  10 +
 tools/perf/util/dso.h            |  29 ++
 tools/perf/util/map.c            |   9 +-
 tools/perf/util/symbol-elf.c     | 560 ++++++++++++++++++++++++++++++++++++++-
 tools/perf/util/symbol-minimal.c |  11 +
 tools/perf/util/symbol.c         |  58 ++--
 tools/perf/util/symbol.h         |   4 +
 tools/perf/util/symbol_conf.h    |   1 +
 9 files changed, 664 insertions(+), 22 deletions(-)
tools/perf/builtin-script.c +3 -1
index 50fa6ca6455a..81e56378d3d2 100644
@@ -4037,7 +4037,7 @@ static int parse_callret_trace(const struct option *opt __maybe_unused,
 }
 
 static int parse_max_symbol_bytes(const struct option *opt,
-				  const char *str, int unset)
+				const char *str, int unset)
 {
 	unsigned long *max_bytes = (unsigned long *)opt->value;
 	static struct parse_tag size_tags[] = {
@@ -4166,6 +4166,8 @@ int cmd_script(int argc, const char **argv)
 	OPT_CALLBACK(0, "max-symbol-bytes", &symbol_conf.max_symbol_bytes,
 		     "size", "Limit bytes for ELF struct symbol (e.g. 128M; 0=unlimited)",
 		     parse_max_symbol_bytes),
+	OPT_BOOLEAN(0, "lazy-load-symbols", &symbol_conf.lazy_load_symbols,
+		    "Resolve symbols lazily instead of loading full symtabs"),
 	OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
 	OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to previous event"),
 	OPT_BOOLEAN('I', "show-info", &show_full_info,
tools/perf/util/dso.c +10 -0
index 42bfe30a3b51..196cd27e79e2 100644
@@ -1755,6 +1755,16 @@ void dso__delete(struct dso *dso)
 
 	dso__data_close(dso);
 	auxtrace_cache__free(RC_CHK_ACCESS(dso)->auxtrace_cache);
+	if (RC_CHK_ACCESS(dso)->ondemand) {
+		struct dso_ondemand *od = RC_CHK_ACCESS(dso)->ondemand;
+
+		free(od->sorted);
+		symbol__unaccount_bytes(od->nr_alloc * sizeof(struct sym_idx));
+		if (od->fd >= 0)
+			close(od->fd);
+		free(od);
+		RC_CHK_ACCESS(dso)->ondemand = NULL;
+	}
 	dso_cache__free(dso);
 	dso__free_a2l(dso);
 	dso__free_libdw(dso);
tools/perf/util/dso.h +29 -0
index 55c4aaa53c38..50c4d64e5081 100644
@@ -282,6 +282,23 @@ struct dso_bpf_prog {
 	struct perf_env	*env;
 };
 
+struct sym_idx {
+	u64	start;	/* adjusted st_value (same space as sym->start) */
+	u64	end;	/* start + st_size, or next start if st_size==0 */
+	u32	name_off; /* symbol's st_name: offset into the strtab */
+	u8	binding;
+	u8	type;
+};
+
+struct dso_ondemand {
+	int		 fd;		/* kept open for pread */
+	u64		 strtab_offset;	/* file offset of strtab section */
+	u64		 strtab_size;
+	struct sym_idx	*sorted;
+	u32		 nr_sorted;	/* deduped count */
+	u32		 nr_alloc;	/* allocated count, for accounting */
+};
+
 struct auxtrace_cache;
 
 DECLARE_RC_STRUCT(dso) {
@@ -308,6 +325,7 @@ DECLARE_RC_STRUCT(dso) {
 	char		 *symsrc_filename;
 	struct nsinfo	*nsinfo;
 	struct auxtrace_cache *auxtrace_cache;
+	struct dso_ondemand *ondemand;
 	union { /* Tool specific area */
 		void	 *priv;
 		u64	 db_id;
@@ -448,6 +466,16 @@ static inline void dso__set_auxtrace_cache(struct dso *dso, struct auxtrace_cach
 	RC_CHK_ACCESS(dso)->auxtrace_cache = cache;
 }
 
+static inline struct dso_ondemand *dso__ondemand(struct dso *dso)
+{
+	return RC_CHK_ACCESS(dso)->ondemand;
+}
+
+static inline void dso__set_ondemand(struct dso *dso, struct dso_ondemand *od)
+{
+	RC_CHK_ACCESS(dso)->ondemand = od;
+}
+
 static inline struct dso_bpf_prog *dso__bpf_prog(struct dso *dso)
 {
 	return &RC_CHK_ACCESS(dso)->bpf_prog;
@@ -823,6 +851,7 @@ int dso__read_binary_type_filename(const struct dso *dso, enum dso_binary_type t
 				   const char *root_dir, char *filename, size_t size);
 bool is_kernel_module(const char *pathname, int cpumode);
 bool dso__needs_decompress(struct dso *dso);
+struct symbol *dso__find_symbol_ondemand(struct dso *dso, u64 addr);
 int dso__decompress_kmodule_fd(struct dso *dso, const char *name);
 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
 				 char *pathname, size_t len);
tools/perf/util/map.c +8 -1
index 41cdddc987ee..68ffa01c9f9e 100644
@@ -382,10 +382,17 @@ int map__load(struct map *map)
 
 struct symbol *map__find_symbol(struct map *map, u64 addr)
 {
+	struct dso *dso;
+	struct symbol *sym;
+
 	if (map__load(map) < 0)
 		return NULL;
 
-	return dso__find_symbol(map__dso(map), addr);
+	dso = map__dso(map);
+	sym = dso__find_symbol(dso, addr);
+	if (!sym && dso__ondemand(dso))
+		sym = dso__find_symbol_ondemand(dso, addr);
+	return sym;
 }
 
 struct symbol *map__find_symbol_by_name_idx(struct map *map, const char *name, size_t *idx)
tools/perf/util/symbol-elf.c +557 -3

596 lines not on this page · raw message

tools/perf/util/symbol-minimal.c +11 -0
index 0a71d1463952..1a7088389d62 100644
@@ -373,6 +373,17 @@ void symbol__elf_init(void)
 {
 }
 
+/*
+ * Without libelf there is no on-demand index to resolve against; the
+ * DSO can never be in on-demand mode, so this is only reached when a
+ * caller ignores the NULL from dso__ondemand().
+ */
+struct symbol *dso__find_symbol_ondemand(struct dso *dso __maybe_unused,
+					 u64 addr __maybe_unused)
+{
+	return NULL;
+}
+
 bool filename__has_section(const char *filename __maybe_unused, const char *sec __maybe_unused)
 {
 	return false;
tools/perf/util/symbol.c +41 -17

112 lines not on this page · raw message

tools/perf/util/symbol.h +4 -0
index 0d5d3792aac1..db1b662988e3 100644
@@ -231,6 +231,10 @@ struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *
 size_t symbol__bytes_used(void);
 void symbol__account_bytes(size_t bytes);
 void symbol__unaccount_bytes(size_t bytes);
+int choose_best_symbol_raw(u64 a_size, u8 a_type, u8 a_binding,
+			   const char *a_name,
+			   u64 b_size, u8 b_type, u8 b_binding,
+			   const char *b_name);
 size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
 				      const struct addr_location *al,
 				      bool unknown_as_addr,
tools/perf/util/symbol_conf.h +1 -0
index 6a16c5badd5e..0f8d044eba20 100644
@@ -74,6 +74,7 @@ struct symbol_conf {
 			no_buildid_mmap2,
 			guest_code,
 			lazy_load_kernel_maps,
+			lazy_load_symbols,
 			keep_exited_threads,
 			annotate_data_member,
 			annotate_data_sample,
Signature · 1 line
Git-155)
#11 lore raw

Re: [PATCH 3/4] perf script: Add --lazy-load-symbols for lazy symbol loading

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf: memory exhaustion in ondemand_read_name() via truncated ELF --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf: memory exhaustion in ondemand_read_name() via truncated ELF --

commit 9189c705674e1061e4de963daab5ec07bd55d920
Author: Alireza Haghdoost <[email protected]>

perf script: Add --lazy-load-symbols for lazy symbol loading

This commit introduces lazy symbol loading for userspace ELF DSOs in perf script. It builds a sorted in-memory index of symbols and resolves them via binary search, reading symbol names on-demand from the string table using pread() to reduce memory footprint.

tools/perf/util/symbol-elf.c +0 -0
index 914e42d21f70f..e4d77e46e8830 100644

[ ... ]

Quoted 5 lines
(quoted diff) +5 -0
+static const char *ondemand_read_name(int fd, u64 strtab_offset,
+				      u64 strtab_size, u64 name_off,
+				      char *buf, size_t buflen,
+				      char **to_free)
+{

[ ... ]

Quoted 30 lines
(quoted diff) +30 -0
+	/*
+	 * Slow path: the name is longer than buflen.  Grow a heap buffer
+	 * geometrically, doubling until the terminator appears, so a long
+	 * name costs O(name length), not O(remaining strtab size).
+	 */
+	cap = 4096;
+	for (;;) {
+		char *tmp;
+		size_t want = cap;
+
+		if (want > remain)
+			want = remain;
+		if (want == 0)
+			break;
+
+		tmp = *to_free ? realloc(*to_free, want + 1) : malloc(want + 1);
+		if (!tmp) {
+			free(*to_free);
+			*to_free = NULL;
+			return NULL;
+		}
+		*to_free = tmp;
+
+		n = pread(fd, *to_free, want, file_off);
+		if (n <= 0) {
+			free(*to_free);
+			*to_free = NULL;
+			return NULL;
+		}
+		(*to_free)[n] = '\0';

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+		if (memchr(*to_free, '\0', n))
+			return *to_free;
+
+		/*
+		 * Read the whole remaining strtab (or hit EOF) with no
+		 * terminator: corrupt file, bail instead of re-reading.
+		 */
+		if (want >= remain || (u64)n >= remain)
+			break;

[Severity: Medium]
Does this loop properly handle a short read (EOF) on a truncated ELF file?

If pread() hits EOF before filling the requested want bytes, n will be smaller than want but greater than 0. The loop condition currently checks if want >= remain or (u64)n >= remain, but it appears to miss an explicit short-read/EOF check like (size_t)n < want.

Because of this, if remain is very large (based on an unvalidated sh_size in the ELF header), the loop could continuously geometrically reallocate up to remain bytes while reading the exact same short bytes from the constant file_off. Could this cause memory exhaustion / an OOM allocation failure?

Quoted 11 lines
(quoted diff) +11 -0
+
+		/* Avoid size_t overflow on absurdly large strtabs. */
+		if (cap > SIZE_MAX / 2)
+			break;
+		cap *= 2;
+	}
+
+	free(*to_free);
+	*to_free = NULL;
+	return NULL;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#12 lore raw

Re: [PATCH 3/4] perf script: Add --lazy-load-symbols for lazy symbol loading

Quoted 3 lines

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf: memory exhaustion in ondemand_read_name() via truncated ELF --

Thanks, this is valid. The constant file offset is intentional because each iteration rereads a larger prefix, but a positive short read must be treated as EOF. I’ll detect n < want after checking the returned bytes for a terminator, abort the lookup as a truncated string table, and add this fix to v2.

maintainer
#13 lore raw

Re: [PATCH 3/4] perf script: Add --lazy-load-symbols for lazy symbol loading

In reply to Alireza Haghdoost
Quoted 14 lines · On Tue, Sep 15, 2026 at 11:42:45AM -0700, Alireza Haghdoost via B4 Relay wrote:

From: Alireza Haghdoost <[email protected]>

perf script eagerly materializes eligible symbols from every DSO encountered in samples. On a production fixture, it loaded about 765k symbols to resolve about 45k distinct (DSO, symbol) frames, exceeding the memory available in a memory-constrained cgroup.

This patch adds --lazy-load-symbols for userspace ELF DSOs. It builds a compact sorted index, resolves sampled addresses by binary search, reads symbol names with pread(), and caches resolved symbols in the existing rb-tree. Lazy lookup retains one CLOEXEC file descriptor per indexed DSO. If the descriptor cannot be retained, perf discards the index and eagerly loads that DSO instead.

Quoted 5 lines

On the same fixture, peak RssAnon drops from 265 MiB to 39 MiB and wall time from 3.1 seconds to 1.85 seconds. Memory optimizations usually cost time; this one does not because lazy loading skips many unnecessary calloc() calls and demangling operations. Output was byte-identical on the tested x86-64 workloads and an aarch64 capture.

Interesting!

Quoted 7 lines

Lazy loading is most effective when samples reference only a small fraction of the available symbols, such as profiles spanning many large DSOs. It still builds an index proportional to the total symbol count. Eager loading remains available for dense symbol coverage or cases requiring its broader ELF and architecture support.

Quoted 7 lines

This does not claim full parity with the eager loader. Lazy loading supports the common userspace ELF symtab/dynsym case, with these known differences:

- .gnu_debugdata merging and PPC64 .opd are not handled.
- SHT_NOBITS re-reading, IFUNC PLT naming, and exact-tie alias ordering are unreachable or output-equivalent on x86-64.

I'm curious how it could handle map__find_symbol_by_name().

Quoted 29 lines
Assisted-by Kimi:K3
---
 tools/perf/builtin-script.c      |   4 +-
 tools/perf/util/dso.c            |  10 +
 tools/perf/util/dso.h            |  29 ++
 tools/perf/util/map.c            |   9 +-
 tools/perf/util/symbol-elf.c     | 560 ++++++++++++++++++++++++++++++++++++++-
 tools/perf/util/symbol-minimal.c |  11 +
 tools/perf/util/symbol.c         |  58 ++--
 tools/perf/util/symbol.h         |   4 +
 tools/perf/util/symbol_conf.h    |   1 +
 9 files changed, 664 insertions(+), 22 deletions(-)
tools/perf/builtin-script.c +1 -1
index 50fa6ca6455a..81e56378d3d2 100644
@@ -4037,7 +4037,7 @@ static int parse_callret_trace(const struct option *opt __maybe_unused,
 }
 
 static int parse_max_symbol_bytes(const struct option *opt,
-				  const char *str, int unset)
+				const char *str, int unset)
 {
 	unsigned long *max_bytes = (unsigned long *)opt->value;
 	static struct parse_tag size_tags[] = {

tools/perf/builtin-script.c int cmd_script(int argc, const char **argv) · line 4170

Quoted 5 lines
tools/perf/builtin-script.c +2 -0
@@ -4166,6 +4166,8 @@ int cmd_script(int argc, const char **argv)
 	OPT_CALLBACK(0, "max-symbol-bytes", &symbol_conf.max_symbol_bytes,
 		     "size", "Limit bytes for ELF struct symbol (e.g. 128M; 0=unlimited)",
 		     parse_max_symbol_bytes),
+	OPT_BOOLEAN(0, "lazy-load-symbols", &symbol_conf.lazy_load_symbols,
+		    "Resolve symbols lazily instead of loading full symtabs"),

Please add documentation as well.

Quoted 3 lines
tools/perf/builtin-script.c +0 -0
 	OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
 	OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to previous event"),
 	OPT_BOOLEAN('I', "show-info", &show_full_info,

[SNIP]

Quoted 13 lines
tools/perf/util/symbol-elf.c +1 -0
index 914e42d21f70..e4d77e46e883 100644
@@ -2,6 +2,7 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <errno.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>

tools/perf/util/symbol-elf.c static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf, · line 603

Quoted 3 lines
tools/perf/util/symbol-elf.c +0 -0
@@ -600,6 +601,32 @@ static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf,
  * And always look at the original dso, not at debuginfo packages, that
  * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  */

It seems the above comment belongs to the original function.

Quoted 7 lines
tools/perf/util/symbol-elf.c +7 -0
+static void dso__clip_ondemand_symbols_at(struct dso *dso, u64 addr)
+{
+	struct dso_ondemand *od = dso__ondemand(dso);
+	struct sym_idx *sym;
+	u32 lo = 0, hi, mid;
+
+	if (!od)

tools/perf/util/symbol-elf.c static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf, · line 620

Quoted 10 lines
tools/perf/util/symbol-elf.c +10 -0
+		return;
+
+	hi = od->nr_sorted;
+	while (lo < hi) {
+		mid = (lo + hi) / 2;
+		if (od->sorted[mid].start < addr)
+			lo = mid + 1;
+		else
+			hi = mid;
+	}

Why not use bsearch()?

Quoted 13 lines
tools/perf/util/symbol-elf.c +9 -0
+
+	if (!lo)
+		return;
+
+	sym = &od->sorted[lo - 1];
+	if (sym->end > addr)
+		sym->end = addr;
+}
+
 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
 {
 	uint32_t idx;

tools/perf/util/symbol-elf.c int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) · line 653

Quoted 4 lines
tools/perf/util/symbol-elf.c +1 -0
@@ -623,6 +650,8 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
 	if (!elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL))
 		return 0;
 
+	dso__clip_ondemand_symbols_at(dso, shdr_plt.sh_offset);

Why is this needed?

Quoted 4 lines
tools/perf/util/symbol-elf.c +1 -0
+
 	/*
 	 * A symbol from a previous section (e.g. .init) can have been expanded
 	 * by symbols__fixup_end() to overlap .plt. Truncate it before adding

[SNIP]

Quoted 130 lines
tools/perf/util/symbol-elf.c +130 -0
+static int dso__build_ondemand_index(struct dso *dso, struct symsrc *syms_ss,
+				     struct symsrc *runtime_ss,
+				     int dynsym)
+{
+	struct dso_ondemand *od;
+	Elf *elf = syms_ss->elf;
+	GElf_Ehdr ehdr = syms_ss->ehdr;
+	GElf_Shdr shdr;
+	GElf_Shdr strshdr;
+	Elf_Scn *strscn, *sec_strndx;
+	Elf_Data *syms;
+	GElf_Sym sym;
+	Elf_Data *secstrs = NULL;
+	size_t i;
+	u32 count = 0, j;
+	u64 nr_entries, strtab_offset;
+
+	if (dynsym)
+		shdr = syms_ss->dynshdr;
+	else
+		shdr = syms_ss->symshdr;
+
+	syms = elf_getdata(dynsym ? syms_ss->dynsym : syms_ss->symtab, NULL);
+	if (!syms)
+		return -1;
+
+	if (!shdr.sh_entsize)
+		return 0;
+
+	nr_entries = shdr.sh_size / shdr.sh_entsize;
+	if (nr_entries > UINT32_MAX)
+		return -EOVERFLOW;
+
+	/*
+	 * File offset of the string table linked from the symbol table.
+	 * Symbol names are pread() from the file at lookup time, so we
+	 * only need the offset and size here, not the strings themselves.
+	 */
+	strscn = elf_getscn(elf, shdr.sh_link);
+	if (!strscn || !gelf_getshdr(strscn, &strshdr))
+		return -1;
+	strtab_offset = strshdr.sh_offset;
+
+	/*
+	 * Section name string table, used to match the eager path's
+	 * elf_sec__filter() (text/data section check for STT_NOTYPE labels).
+	 */
+	sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
+	if (sec_strndx)
+		secstrs = elf_getdata(sec_strndx, NULL);
+
+	/* Count symbols that pass the filter (same filter as fill below) */
+	for (i = 0; i < nr_entries; i++) {
+		if (!gelf_getsym(syms, i, &sym))
+			continue;
+		if (ondemand_sym_ok(elf, secstrs, &sym, shdr.sh_link,
+				    ehdr.e_machine))
+			count++;
+	}
+
+	if (!count)
+		return 0;
+	if (count > SIZE_MAX / sizeof(*od->sorted))
+		return -EOVERFLOW;
+
+	/*
+	 * Account the index against the symbol memory budget: at 24
+	 * bytes/symbol it is the dominant on-demand cost and must count
+	 * toward --max-symbol-bytes just like struct symbol allocations do.
+	 */
+	if (symbol_conf.max_symbol_bytes &&
+	    symbol__bytes_used() + count * sizeof(struct sym_idx) >
+	    symbol_conf.max_symbol_bytes) {
+		symbol_budget_warning();
+		return 0; /* fall back to the eager loader's per-symbol budget */
+	}
+
+	od = zalloc(sizeof(*od));
+	if (!od)
+		return -1;
+
+	od->sorted = malloc(count * sizeof(*od->sorted));
+	if (!od->sorted) {
+		free(od);
+		return -1;
+	}
+	od->nr_alloc = count;	/* allocated; the deduped count may shrink */
+
+	/* Fill the index with adjusted addresses */
+	j = 0;
+	for (i = 0; i < nr_entries; i++) {
+		u64 adjusted;
+		GElf_Phdr phdr;
+
+		if (!gelf_getsym(syms, i, &sym))
+			continue;
+		if (!ondemand_sym_ok(elf, secstrs, &sym, shdr.sh_link,
+				     ehdr.e_machine))
+			continue;
+
+		adjusted = sym.st_value;
+
+		/* ARM Thumb bit fix (same as eager path, FUNC only) */
+		if ((ehdr.e_machine == EM_ARM) &&
+		    (GELF_ST_TYPE(sym.st_info) == STT_FUNC) &&
+		    (adjusted & 1))
+			--adjusted;
+
+		/*
+		 * Program header adjustment, identical to the eager loop:
+		 * read the PT_LOAD containing the symbol from the runtime
+		 * ELF (the debug-info file may have zeroed p_offset), and
+		 * fall back to the section-header bias when no program
+		 * header matches -- exactly what the eager path does when
+		 * elf_read_program_header fails.
+		 */
+		if (elf_read_program_header(runtime_ss->elf, adjusted,
+					    &phdr) == 0) {
+			adjusted -= phdr.p_vaddr - phdr.p_offset;
+		} else {
+			Elf_Scn *sym_sec = elf_getscn(elf, sym.st_shndx);
+			GElf_Shdr sym_shdr;
+
+			if (sym_sec && gelf_getshdr(sym_sec, &sym_shdr))
+				adjusted -= sym_shdr.sh_addr - sym_shdr.sh_offset;
+		}
+
+		od->sorted[j].start = adjusted;
+		od->sorted[j].end = sym.st_size; /* st_size for now, converted later */
+		od->sorted[j].name_off = sym.st_name; /* strtab-relative */

tools/perf/util/symbol-elf.c static int dso__process_kernel_symbol(struct dso *dso, struct map *map, · line 1781

Quoted 10 lines
tools/perf/util/symbol-elf.c +10 -0
+		od->sorted[j].binding = GELF_ST_BIND(sym.st_info);
+		od->sorted[j].type = GELF_ST_TYPE(sym.st_info);
+		j++;
+	}
+
+	/* Sort by adjusted start address */
+	qsort(od->sorted, count, sizeof(*od->sorted), cmp_sym_idx);
+
+	/* Alias dedup: keep only the best symbol for each start address */
+	if (!symbol_conf.allow_aliases) {

Probably better to factor out the dedup logic into a function.

Quoted 111 lines
tools/perf/util/symbol-elf.c +111 -0
+		u32 out = 0;
+
+		for (i = 0; i < count; i++) {
+			u32 best = i;
+			const char *na = NULL, *nb;
+			char *da = NULL, *db;
+
+			/* name_off is the strtab index (st_name) */
+			na = elf_strptr(elf, shdr.sh_link,
+					od->sorted[best].name_off);
+			if (na) {
+				da = dso__demangle_sym(dso, 0, na);
+				if (da)
+					na = da;
+			}
+
+			/* Find the best among all entries with this start */
+			for (j = i + 1; j < count &&
+			     od->sorted[j].start == od->sorted[i].start; j++) {
+				nb = elf_strptr(elf, shdr.sh_link,
+						od->sorted[j].name_off);
+				if (!na || !nb)
+					continue;
+
+				/* Demangle for comparison, matching eager path */
+				db = dso__demangle_sym(dso, 0, nb);
+				if (db)
+					nb = db;
+
+				/* od->sorted[].end holds st_size at this point */
+				if (choose_best_symbol_raw(
+					    od->sorted[best].end,
+					    od->sorted[best].type,
+					    od->sorted[best].binding, na,
+					    od->sorted[j].end,
+					    od->sorted[j].type,
+					    od->sorted[j].binding, nb) == SYMBOL_B) {
+					best = j;
+					free(da);
+					da = db;
+					na = nb;
+				} else {
+					free(db);
+				}
+			}
+
+			free(da);
+			od->sorted[out++] = od->sorted[best];
+			i = j - 1; /* skip past all aliases of this start */
+		}
+
+		if (out < count) {
+			struct sym_idx *shrunk;
+
+			shrunk = realloc(od->sorted, out * sizeof(*od->sorted));
+			if (shrunk) {
+				od->sorted = shrunk;
+				od->nr_alloc = out;
+			}
+		}
+		count = out;
+	}
+
+	/* Convert st_size to end addresses */
+	for (i = 0; i < count; i++) {
+		u64 size = od->sorted[i].end; /* was st_size */
+
+		if (size > 0)
+			od->sorted[i].end = od->sorted[i].start + size;
+		else if (i + 1 < count)
+			od->sorted[i].end = od->sorted[i + 1].start;
+		else
+			/* Match symbols__fixup_end's last-symbol formula. */
+			od->sorted[i].end = roundup(od->sorted[i].start, 4096) + 4096;
+	}
+
+	/*
+	 * Keep a private fd open for pread() of symbol names.  Dup with
+	 * O_CLOEXEC so children don't inherit it, and so that
+	 * symsrc__destroy() can close the original regardless of whether
+	 * it is a real file or a temporary debugdata extraction.
+	 *
+	 * If the dup fails (e.g. fd exhaustion), decline by returning 0
+	 * without setting the index: the caller falls back to the eager
+	 * loader so the DSO still gets symbols rather than going symbol-less.
+	 */
+	od->fd = fcntl(syms_ss->fd, F_DUPFD_CLOEXEC, 0);
+	if (od->fd < 0) {
+		free(od->sorted);
+		free(od);
+		return 0;
+	}
+	od->strtab_offset = strtab_offset;
+	od->strtab_size = strshdr.sh_size;
+	od->nr_sorted = count;
+
+	symbol__account_bytes(od->nr_alloc * sizeof(*od->sorted));
+
+	dso__set_ondemand(dso, od);
+
+	pr_debug("%s: on-demand index: %u symbols\n",
+		 dso__long_name(dso), count);
+
+	return 1;
+}
+
+/*
+ * Read a NUL-terminated symbol name from the file's string table at
+ * file offset @off.  The fast path uses a stack buffer; if the NUL is
+ * not found within it (names can exceed 1 KiB for template-heavy C++
+ * mangled names), grow a heap buffer geometrically from 4 KiB, doubling

tools/perf/util/symbol-elf.c static int dso__process_kernel_symbol(struct dso *dso, struct map *map, · line 1902

Quoted 10 lines
tools/perf/util/symbol-elf.c +10 -0
+ * until the terminator is found or the strtab is exhausted.  This keeps
+ * a single long name cheap even when it sits near the start of a large
+ * (tens of MB) strtab, while bounding a corrupt/missing terminator by
+ * the remaining strtab size.
+ *
+ * Returns a pointer to the name (either @buf or a heap allocation) and
+ * sets *@to_free to the buffer that must be free()d (NULL for @buf).
+ * Returns NULL on read error or if no NUL terminator exists within the
+ * strtab bounds.
+ */

We have dso-cache APIs to read file data (dso__data_read_offset) and it manages file descriptors so you don't need to worry about FD exhaustion.

Quoted 96 lines
tools/perf/util/symbol-elf.c +96 -0
+static const char *ondemand_read_name(int fd, u64 strtab_offset,
+				      u64 strtab_size, u64 name_off,
+				      char *buf, size_t buflen,
+				      char **to_free)
+{
+	ssize_t n;
+	u64 remain;
+	u64 file_off;
+	size_t cap;
+
+	*to_free = NULL;
+
+	/* name_off is strtab-relative (the symbol's st_name). */
+	if (name_off >= strtab_size)
+		return NULL;
+	file_off = strtab_offset + name_off;
+	remain = strtab_size - name_off;
+
+	/*
+	 * Fast path: stack buffer, expect the name to fit.  Cap at the
+	 * remaining strtab so a missing terminator can't read past the
+	 * section into adjacent file data.
+	 */
+	n = pread(fd, buf, min((u64)(buflen - 1), remain), file_off);
+	if (n <= 0)
+		return NULL;
+	buf[n] = '\0';
+	if (memchr(buf, '\0', n))
+		return buf;
+
+	/*
+	 * Slow path: the name is longer than buflen.  Grow a heap buffer
+	 * geometrically, doubling until the terminator appears, so a long
+	 * name costs O(name length), not O(remaining strtab size).
+	 */
+	cap = 4096;
+	for (;;) {
+		char *tmp;
+		size_t want = cap;
+
+		if (want > remain)
+			want = remain;
+		if (want == 0)
+			break;
+
+		tmp = *to_free ? realloc(*to_free, want + 1) : malloc(want + 1);
+		if (!tmp) {
+			free(*to_free);
+			*to_free = NULL;
+			return NULL;
+		}
+		*to_free = tmp;
+
+		n = pread(fd, *to_free, want, file_off);
+		if (n <= 0) {
+			free(*to_free);
+			*to_free = NULL;
+			return NULL;
+		}
+		(*to_free)[n] = '\0';
+
+		if (memchr(*to_free, '\0', n))
+			return *to_free;
+
+		/*
+		 * Read the whole remaining strtab (or hit EOF) with no
+		 * terminator: corrupt file, bail instead of re-reading.
+		 */
+		if (want >= remain || (u64)n >= remain)
+			break;
+
+		/* Avoid size_t overflow on absurdly large strtabs. */
+		if (cap > SIZE_MAX / 2)
+			break;
+		cap *= 2;
+	}
+
+	free(*to_free);
+	*to_free = NULL;
+	return NULL;
+}
+
+struct symbol *dso__find_symbol_ondemand(struct dso *dso, u64 addr)
+{
+	struct dso_ondemand *od = dso__ondemand(dso);
+	u32 lo, hi, mid;
+	const char *name;
+	char namebuf[1024];
+	char *name_heap = NULL;
+	char *demangled;
+	struct symbol *s = NULL;
+
+	if (!od || !od->sorted || od->fd < 0)
+		return NULL;
+
+	lo = 0;

tools/perf/util/symbol-elf.c static int dso__process_kernel_symbol(struct dso *dso, struct map *map, · line 2008

Quoted 10 lines
tools/perf/util/symbol-elf.c +10 -0
+	hi = od->nr_sorted;
+	while (lo < hi) {
+		mid = (lo + hi) / 2;
+		if (addr < od->sorted[mid].start)
+			hi = mid;
+		else if (addr >= od->sorted[mid].end)
+			lo = mid + 1;
+		else
+			goto found;
+	}

bsearch()?

Thanks,
Namhyung

Quoted 35 lines
tools/perf/util/symbol-elf.c +35 -0
+
+	/* Not found */
+	return NULL;
+
+found:
+	/*
+	 * Check the budget before doing any name I/O or demangling, so an
+	 * over-budget DSO stops paying pread+demangle on every later miss.
+	 */
+	if (symbol_conf.max_symbol_bytes &&
+	    symbol__bytes_used() >= symbol_conf.max_symbol_bytes) {
+		symbol_budget_warning();
+		return NULL;
+	}
+
+	name = ondemand_read_name(od->fd, od->strtab_offset, od->strtab_size,
+				  od->sorted[mid].name_off,
+				  namebuf, sizeof(namebuf), &name_heap);
+	if (!name)
+		return NULL;
+
+	demangled = dso__demangle_sym(dso, 0, name);
+	if (demangled)
+		name = demangled;
+
+	s = symbol__new(od->sorted[mid].start,
+			od->sorted[mid].end - od->sorted[mid].start,
+			od->sorted[mid].binding,
+			od->sorted[mid].type, name);
+	free(demangled);
+	free(name_heap);
+	if (s)
+		__symbols__insert(dso__symbols(dso), s);
+	return s;
+}
#14 lore raw

Re: [PATCH 3/4] perf script: Add --lazy-load-symbols for lazy symbol loading

Quoted 11 lines
Quoted 11 lines
tools/perf/util/symbol-elf.c +11 -0
+static void dso__clip_ondemand_symbols_at(struct dso *dso, u64 addr)
+{
+     struct dso_ondemand *od = dso__ondemand(dso);
+     struct sym_idx *sym;
+     u32 lo = 0, hi, mid;
+
+     if (!od)
+             return;
+
+     hi = od->nr_sorted;
+     while (lo < hi) {
Quoted 9 lines

tools/perf/util/symbol-elf.c static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf, · line 620

Quoted 6 lines
tools/perf/util/symbol-elf.c +6 -0
+             mid = (lo + hi) / 2;
+             if (od->sorted[mid].start < addr)
+                     lo = mid + 1;
+             else
+                     hi = mid;
+     }

Why not use bsearch()?

This search is a lower-bound (bisect_left) operation. I could not find an ISO C or glibc API that provides that operation.
In patch series v2, I will refactor this search into a small sym_idx__lower_bound() helper. The clipping helper (symbol-elf.c:626) and the exact-start IRELATIVE lookup (symbol-elf.c:2176) will use it, while ordinary non-overlapping interval lookups will use bsearch().

Quoted 11 lines

tools/perf/util/symbol-elf.c int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) · line 653

Quoted 8 lines

int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)

 {
      uint32_t idx;
tools/perf/util/symbol-elf.c +1 -0
@@ -623,6 +650,8 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
      if (!elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL))
              return 0;
 
+     dso__clip_ondemand_symbols_at(dso, shdr_plt.sh_offset);

Why is this needed?

Lazy address lookup uses the index, not the rb-tree. Without clipping the index at .plt, a preceding fill-forward range can claim PLT IPs before dso__synthesize_plt_symbols() inserts the PLT symbols. This is the same truncation the eager path already does on the rb-tree (symbol-elf.c:680). I will add that explanation at the call site in v2.

Quoted 6 lines

tools/perf/util/symbol-elf.c static int dso__process_kernel_symbol(struct dso *dso, struct map *map, · line 1781

Quoted 3 lines
tools/perf/util/symbol-elf.c +3 -0
+
+     /* Alias dedup: keep only the best symbol for each start address */
+     if (!symbol_conf.allow_aliases) {

Probably better to factor out the dedup logic into a function.

I will factor out the duplicate-selection policy into symbol__choose_best() and use it in both the eager and lazy paths. The surrounding dedup loops remain separate because the eager path uses rb-tree struct symbol objects while the lazy path uses compact index entries before materialization.

Quoted 116 lines
Quoted 116 lines
tools/perf/util/symbol-elf.c +116 -0
+             u32 out = 0;
+
+             for (i = 0; i < count; i++) {
+                     u32 best = i;
+                     const char *na = NULL, *nb;
+                     char *da = NULL, *db;
+
+                     /* name_off is the strtab index (st_name) */
+                     na = elf_strptr(elf, shdr.sh_link,
+                                     od->sorted[best].name_off);
+                     if (na) {
+                             da = dso__demangle_sym(dso, 0, na);
+                             if (da)
+                                     na = da;
+                     }
+
+                     /* Find the best among all entries with this start */
+                     for (j = i + 1; j < count &&
+                          od->sorted[j].start == od->sorted[i].start; j++) {
+                             nb = elf_strptr(elf, shdr.sh_link,
+                                             od->sorted[j].name_off);
+                             if (!na || !nb)
+                                     continue;
+
+                             /* Demangle for comparison, matching eager path */
+                             db = dso__demangle_sym(dso, 0, nb);
+                             if (db)
+                                     nb = db;
+
+                             /* od->sorted[].end holds st_size at this point */
+                             if (choose_best_symbol_raw(
+                                         od->sorted[best].end,
+                                         od->sorted[best].type,
+                                         od->sorted[best].binding, na,
+                                         od->sorted[j].end,
+                                         od->sorted[j].type,
+                                         od->sorted[j].binding, nb) == SYMBOL_B) {
+                                     best = j;
+                                     free(da);
+                                     da = db;
+                                     na = nb;
+                             } else {
+                                     free(db);
+                             }
+                     }
+
+                     free(da);
+                     od->sorted[out++] = od->sorted[best];
+                     i = j - 1; /* skip past all aliases of this start */
+             }
+
+             if (out < count) {
+                     struct sym_idx *shrunk;
+
+                     shrunk = realloc(od->sorted, out * sizeof(*od->sorted));
+                     if (shrunk) {
+                             od->sorted = shrunk;
+                             od->nr_alloc = out;
+                     }
+             }
+             count = out;
+     }
+
+     /* Convert st_size to end addresses */
+     for (i = 0; i < count; i++) {
+             u64 size = od->sorted[i].end; /* was st_size */
+
+             if (size > 0)
+                     od->sorted[i].end = od->sorted[i].start + size;
+             else if (i + 1 < count)
+                     od->sorted[i].end = od->sorted[i + 1].start;
+             else
+                     /* Match symbols__fixup_end's last-symbol formula. */
+                     od->sorted[i].end = roundup(od->sorted[i].start, 4096) + 4096;
+     }
+
+     /*
+      * Keep a private fd open for pread() of symbol names.  Dup with
+      * O_CLOEXEC so children don't inherit it, and so that
+      * symsrc__destroy() can close the original regardless of whether
+      * it is a real file or a temporary debugdata extraction.
+      *
+      * If the dup fails (e.g. fd exhaustion), decline by returning 0
+      * without setting the index: the caller falls back to the eager
+      * loader so the DSO still gets symbols rather than going symbol-less.
+      */
+     od->fd = fcntl(syms_ss->fd, F_DUPFD_CLOEXEC, 0);
+     if (od->fd < 0) {
+             free(od->sorted);
+             free(od);
+             return 0;
+     }
+     od->strtab_offset = strtab_offset;
+     od->strtab_size = strshdr.sh_size;
+     od->nr_sorted = count;
+
+     symbol__account_bytes(od->nr_alloc * sizeof(*od->sorted));
+
+     dso__set_ondemand(dso, od);
+
+     pr_debug("%s: on-demand index: %u symbols\n",
+              dso__long_name(dso), count);
+
+     return 1;
+}
+
+/*
+ * Read a NUL-terminated symbol name from the file's string table at
+ * file offset @off.  The fast path uses a stack buffer; if the NUL is
+ * not found within it (names can exceed 1 KiB for template-heavy C++
+ * mangled names), grow a heap buffer geometrically from 4 KiB, doubling
+ * until the terminator is found or the strtab is exhausted.  This keeps
+ * a single long name cheap even when it sits near the start of a large
+ * (tens of MB) strtab, while bounding a corrupt/missing terminator by
+ * the remaining strtab size.
+ *
Quoted 9 lines
Quoted 5 lines

+ * Returns a pointer to the name (either @buf or a heap allocation) and
+ * sets *@to_free to the buffer that must be free()d (NULL for @buf).
+ * Returns NULL on read error or if no NUL terminator exists within the
+ * strtab bounds.
+ */

We have dso-cache APIs to read file data (dso__data_read_offset) and it manages file descriptors so you don't need to worry about FD exhaustion.

Thanks, I will use dso__data_read_offset() in v2 and remove the per-index retained fd. I will configure the data-cache reads with the same symbol-file path and namespace used to build the index, so split-debuginfo string-table offsets are not read from the runtime image.

Quoted 18 lines
Quoted 18 lines
tools/perf/util/symbol-elf.c +18 -0
+struct symbol *dso__find_symbol_ondemand(struct dso *dso, u64 addr)
+{
+     struct dso_ondemand *od = dso__ondemand(dso);
+     u32 lo, hi, mid;
+     const char *name;
+     char namebuf[1024];
+     char *name_heap = NULL;
+     char *demangled;
+     struct symbol *s = NULL;
+
+     if (!od || !od->sorted || od->fd < 0)
+             return NULL;
+
+     lo = 0;
+     hi = od->nr_sorted;
+     while (lo < hi) {
+             mid = (lo + hi) / 2;
+             if (addr < od->sorted[mid].start)
Quoted 9 lines

tools/perf/util/symbol-elf.c static int dso__process_kernel_symbol(struct dso *dso, struct map *map, · line 2008

Quoted 6 lines
tools/perf/util/symbol-elf.c +6 -0
+                     hi = mid;
+             else if (addr >= od->sorted[mid].end)
+                     lo = mid + 1;
+             else
+                     goto found;
+     }

bsearch()?

Yes. In v2, the default deduplicated index has non-overlapping ranges, so this lookup will use bsearch() with an interval comparator. With --allow-aliases, two symbols can cover the same address. bsearch() then cannot reliably choose which alias to return, so that uncommon case keeps the explicit search.

#15 lore raw

PATCH 4/4 perf script: Document and test --lazy-load-symbols and --max-symbol-bytes

In reply to Alireza Haghdoost

Document both new options in perf-script.txt: their interaction, the memory tradeoff, that lazy loading applies only to userspace ELF DSOs (kernel DSOs and modules always load eagerly), and that output may differ from the default loader for some targets.

Add a shell test that records a small profile with callchains and asserts:

- --lazy-load-symbols produces output byte-identical to the eager loader, and that at least one symbol actually resolved (so the comparison can't pass vacuously on all-[unknown] output);
- --max-symbol-bytes=1K, with and without lazy loading, forces [unknown] resolution with a single warning and exit code 0 (no crash).

Assisted-by Kimi:K3
---
 tools/perf/Documentation/perf-script.txt           |  24 +++++
 tools/perf/tests/shell/script_lazy_load_symbols.sh | 120 +++++++++++++++++++++
 2 files changed, 144 insertions(+)
tools/perf/Documentation/perf-script.txt +24 -0
index 200ea25891d8..b5a90ff22342 100644
@@ -412,6 +412,30 @@ include::itrace.txt[]
 
         Default: 127
 
+--lazy-load-symbols::
+	Resolve symbols lazily instead of eagerly loading the full
+	symbol table of every DSO that appears in a sample. A compact
+	sorted index is built per DSO and only the addresses that appear
+	in samples are materialized into symbols, with names read from the
+	file's string table at lookup time. This sharply reduces memory
+	(and usually time) for profiles of large binaries where only a
+	small fraction of the symbol table is referenced. This applies only
+	to userspace ELF DSOs; kernel DSOs and modules always load eagerly.
+	Output may differ from the default loader for some targets
+	(e.g. PPC64 .opd, .gnu_debugdata, or split debuginfo). Default: off.
+
+--max-symbol-bytes::
+	Limit the bytes held in struct symbol allocations (and, with
+	--lazy-load-symbols, the lazy index) for DSOs on the ELF symbol
+	loader path -- userspace DSOs plus vmlinux-as-ELF and kernel
+	modules. This is not a cap on all symbol memory or RSS: symbols
+	from kallsyms, JIT maps, PLT synthesis, and libbfd are counted
+	but not capped. Accepts a size with a B/K/M/G suffix (e.g. 128M).
+	When the budget is exceeded, further symbols resolve to [unknown]
+	and a warning is printed. This is a safety net independent of
+	--lazy-load-symbols and can be used with or without it. Default: 0
+	(unlimited).
+
 --ns::
 	Use 9 decimal places when displaying time (i.e. show the nanoseconds)
 
tools/perf/tests/shell/script_lazy_load_symbols.sh new file +120 -0

120 lines not on this page · raw message

Signature · 1 line
Git-155)
#16 lore raw

Re: [PATCH 4/4] perf script: Document and test --lazy-load-symbols and --max-symbol-bytes

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] perf test: missing err=2 assignments cause skips to be reported as success - [Medium] perf test…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] perf test: missing err=2 assignments cause skips to be reported as success
- [Medium] perf test: strict byte comparison fails on platforms with differing debug output --

commit 152590a92c1dd38a6f7f70f13ced82231e7560bb
Author: Alireza Haghdoost <[email protected]>

perf script: Document and test --lazy-load-symbols and --max-symbol-bytes

Document both new options in perf-script.txt detailing their interaction, the memory tradeoffs, and output behavior for certain targets. Add a shell test that records a small profile and asserts that lazy loading produces identical output to the eager loader, and that the max symbol budget is enforced.

Quoted 19 lines
tools/perf/Documentation/perf-script.txt +11 -0
index 200ea25891d88..b5a90ff22342f 100644
@@ -412,6 +412,30 @@ include::itrace.txt[]
 
         Default: 127
 
+--lazy-load-symbols::
+	Resolve symbols lazily instead of eagerly loading the full
+	symbol table of every DSO that appears in a sample. A compact
+	sorted index is built per DSO and only the addresses that appear
+	in samples are materialized into symbols, with names read from the
+	file's string table at lookup time. This sharply reduces memory
+	(and usually time) for profiles of large binaries where only a
+	small fraction of the symbol table is referenced. This applies only
+	to userspace ELF DSOs; kernel DSOs and modules always load eagerly.
+	Output may differ from the default loader for some targets
+	(e.g. PPC64 .opd, .gnu_debugdata, or split debuginfo). Default: off.

[ ... ]

Quoted 6 lines
tools/perf/tests/shell/script_lazy_load_symbols.sh new file +0 -0
new file mode 100755
index 0000000000000..799c61e2f3e87
@@ -0,0 +1,120 @@

[ ... ]

tools/perf/tests/shell/script_lazy_load_symbols.sh line 44

Quoted 8 lines
tools/perf/tests/shell/script_lazy_load_symbols.sh new file +8 -0
+test_lazy_load_identical() {
+	echo "Lazy-load output matches eager loader"
+
+	# Record a small profile with callchains so symbol resolution runs.
+	if ! perf record -o "${perfdata}" -g -- perf test -w thloop 2> /dev/null
+	then
+		echo "Lazy-load identical [Skipped record not supported]"
+		return

[Severity: Medium]
Will this script falsely report success when it skips tests? It looks like the function returns here without setting err=2, which would cause the test script to exit with 0 (pass) instead of 2 (skip) when perf record fails.

Quoted 13 lines
tools/perf/tests/shell/script_lazy_load_symbols.sh new file +13 -0
+	fi
+
+	if ! perf script -i "${perfdata}" 2> /dev/null > "${eager_out}" || \
+	   ! perf script --lazy-load-symbols -i "${perfdata}" 2> /dev/null > "${lazy_out}"
+	then
+		echo "Lazy-load identical [Failed perf script error]"
+		err=1
+		return
+	fi
+
+	# The comparison is only meaningful if something actually resolved;
+	# two all-[unknown] outputs would also match.
+	if ! grep -q "${testsym}" "${eager_out}"

tools/perf/tests/shell/script_lazy_load_symbols.sh line 67

Quoted 10 lines
tools/perf/tests/shell/script_lazy_load_symbols.sh new file +10 -0
+	then
+		echo "Lazy-load identical [Skipped no ${testsym} resolved]"
+		return
+	fi
+
+	if ! cmp -s "${eager_out}" "${lazy_out}"
+	then
+		echo "Lazy-load identical [Failed output differs]"
+		err=1
+		return

[Severity: Medium]
Since the documentation mentions that output may differ from the default loader for some targets (e.g., PPC64, .gnu_debugdata, or split debuginfo), will this strict byte-identical comparison fail spuriously on those platforms?

tools/perf/tests/shell/script_lazy_load_symbols.sh line 79

Quoted 12 lines
tools/perf/tests/shell/script_lazy_load_symbols.sh new file +12 -0
+	fi
+	echo "Lazy-load identical [Success]"
+}
+
+test_max_symbol_bytes() {
+	echo "--max-symbol-bytes budget enforcement"
+
+	# Depends on ${perfdata} from test_lazy_load_identical.
+	if [ ! -s "${perfdata}" ]
+	then
+		echo "--max-symbol-bytes budget [Skipped record not supported]"
+		return

[Severity: Medium]
Similar to the skip path in test_lazy_load_identical(), should err=2 be set here so the script correctly returns the skip exit code rather than success?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#17 lore raw

Re: [PATCH 4/4] perf script: Document and test --lazy-load-symbols and --max-symbol-bytes

Quoted 4 lines

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] perf test: missing err=2 assignments cause skips to be reported as success
- [Medium] perf test: strict byte comparison fails on platforms with differing debug output --

Thanks, both findings are valid. I’ll make the skip paths return status 2 without overriding a prior failure, and narrow the eager-versus-lazy comparison to the controlled test symbol rather than comparing all output from every DSO. I’ll include these fixes in v2.