* [PATCH v9 0/7] perf script: Lazy symbol loading
@ 2026-11-24 19:28 Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 1/7] perf symbols: Split out name-independent duplicate symbol selection Alireza Haghdoost via B4 Relay
` (6 more replies)
0 siblings, 7 replies; 8+ messages in thread
From: Alireza Haghdoost via B4 Relay @ 2026-11-24 19:28 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: linux-perf-users, linux-kernel, Alireza Haghdoost
perf script loads the entire ELF symbol table of every DSO that appears
in a sample, and keeps each symbol, with its demangled name, until
process exit. Most of those symbols are never sampled. On a large
profile this turns symbol loading into the main memory cost of perf
script, and in a memory-constrained cgroup into an OOM kill.
With this series, perf script, perf report and perf top
--lazy-load-symbols read a symbol name only when a sample needs it. On a
perf script run over a 45,073-sample callchain profile of a large C++
service (about 1M symbols), the output is identical in all three cases
(medians of eleven runs):
time max RSS
perf-tools-next 2.24 s 305 MiB
v9 2.26 s 315 MiB
v9 --lazy-load-symbols 1.97 s 164 MiB
Eager loading remains the default, and costs 8 more bytes per symbol for
the name pointer. Lazy loading covers userspace ELF .symtab and .dynsym.
The kernel, modules, PPC64 .opd and .gnu_debugdata still load names
eagerly. Names are read from the file that perf loaded the symbols
from, so the output is the same as with eager loading as long as the
binaries do not change while perf runs.
The patches:
[1/7] Split the size, type and binding checks out of
choose_best_symbol(), so that the loader can tell which aliases
need their names. No functional change. Unchanged from v8,
reviewed by Ian.
[2/7] Make the symbol name a pointer instead of a flexible array, so
that a name can be set after the symbol is in the symbol array.
Reviewed by Ian; only the commit message changed.
[3/7] Add lazy symbol names to the symbol code: the DSO address
lookups read the name of the symbol they return, and walks and
lookups by name read all names of the DSO first. Nothing
creates such symbols yet.
[4/7] Add --lazy-load-symbols to perf script, which makes the ELF
loader create symbols with lazy names.
[5/7] Add --lazy-load-symbols to perf report and perf top.
[6/7] Unit tests.
[7/7] Shell tests, including parity with eager loading for perf
script and perf report.
This series is rebased onto the first six patches of Ian's v8 [1], now
in perf-tools-next. Ian's v9 [2] applies on top of it with one trivial
conflict in struct dso, and the symbol and lazy loading tests pass on
the combination. I'll rebase if his patches land first.
[1] https://lore.kernel.org/linux-perf-users/20261122050405.80335-1-irogers@google.com/
[2] https://lore.kernel.org/linux-perf-users/20261124032902.3859564-1-irogers@google.com/
Changes in v9:
- Rebase onto current perf-tools-next, which has Ian's symbol array and
the fix that keeps cached debuginfo in perf's mount namespace. 3/7
now tracks per DSO whether all its names are loaded, since a DSO
sorted by name can still have lazy names.
- Drop v8 4/7, the check of the symbol table file, and its tests
(Namhyung). The man pages say that the output equals eager loading as
long as the binaries do not change while perf runs.
- 4/7 (was 5/7): drop the kmodule check, use pr_debug2(), and add
parentheses around multi-line expressions (Namhyung).
- 4/7: create lazy names only for a symbol table file that perf can
reopen by the same name in its own mount namespace, since perf top's
sample thread cannot switch namespaces.
- New 5/7: --lazy-load-symbols for perf report and perf top (Namhyung).
- 6/7: drop the file check tests.
- 7/7: rename script_lazy_load_symbols.sh to lazy_load_symbols.sh, and
add perf report parity and a case for a process in another mount
namespace.
Previous versions:
v8: https://lore.kernel.org/all/20261121-perf-symbol-memory-send-v8-0-370561cbb5a7@uber.com/
v7: https://lore.kernel.org/all/20261113-perf-symbol-memory-send-v7-0-d992eced9f9c@uber.com/
v6: https://lore.kernel.org/all/20261028-perf-symbol-memory-send-v6-0-7e90415f0101@uber.com/
v5: https://lore.kernel.org/all/20261005-perf-symbol-memory-send-v5-0-165dceb2b049@uber.com/
v4: https://lore.kernel.org/all/20261002-perf-symbol-memory-send-v4-0-0a592bb2539e@uber.com/
v3: https://lore.kernel.org/all/20260925-perf-symbol-memory-send-v3-0-3e4e234c363b@uber.com/
v2: https://lore.kernel.org/all/20260919-perf-symbol-memory-send-v2-0-495b8f00ad7c@uber.com/
v1: https://lore.kernel.org/all/20260915-perf-symbol-memory-send-v1-0-1d3360e21f07@uber.com/
---
Alireza Haghdoost (7):
perf symbols: Split out name-independent duplicate symbol selection
perf symbols: Point to the symbol name instead of embedding it
perf symbols: Add lazy symbol names
perf script: Add --lazy-load-symbols to load symbol names lazily
perf report/top: Add --lazy-load-symbols
perf test: Add lazy symbol loading unit tests
perf test: Add lazy symbol loading shell tests
tools/perf/Documentation/perf-report.txt | 9 +
tools/perf/Documentation/perf-script.txt | 9 +
tools/perf/Documentation/perf-top.txt | 10 +
tools/perf/builtin-report.c | 2 +
tools/perf/builtin-script.c | 2 +
tools/perf/builtin-top.c | 2 +
tools/perf/tests/Build | 1 +
tools/perf/tests/builtin-test.c | 1 +
tools/perf/tests/shell/lazy_load_symbols.sh | 243 ++++++++
tools/perf/tests/shell/lazy_load_symbols_parity.sh | 462 +++++++++++++++
tools/perf/tests/shell/lib/lazy_names.sh | 20 +
tools/perf/tests/symbol-lazy.c | 647 +++++++++++++++++++++
tools/perf/tests/tests.h | 1 +
tools/perf/ui/browsers/map.c | 2 +-
tools/perf/ui/stdio/hist.c | 1 +
tools/perf/util/dso.h | 28 +-
tools/perf/util/symbol-elf.c | 218 ++++++-
tools/perf/util/symbol-minimal.c | 6 +
tools/perf/util/symbol.c | 138 ++++-
tools/perf/util/symbol.h | 29 +-
tools/perf/util/symbol_conf.h | 1 +
tools/perf/util/symsrc.h | 2 +
22 files changed, 1804 insertions(+), 30 deletions(-)
---
base-commit: 964e8b1c9c795982886a6778cb0cd73c901ca4ca
change-id: 20260915-perf-symbol-memory-send-e7cfca1ac3d9
Best regards,
--
Alireza Haghdoost <haghdoost@uber.com>
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v9 1/7] perf symbols: Split out name-independent duplicate symbol selection
2026-11-24 19:28 [PATCH v9 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
@ 2026-11-24 19:28 ` Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 2/7] perf symbols: Point to the symbol name instead of embedding it Alireza Haghdoost via B4 Relay
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Alireza Haghdoost via B4 Relay @ 2026-11-24 19:28 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: linux-perf-users, linux-kernel, Alireza Haghdoost
From: Alireza Haghdoost <haghdoost@uber.com>
This patch is a refactoring and does not change behavior. It moves the
size, type and binding checks from choose_best_symbol() to
choose_best_symbol_by_attrs(), and adds symbol__choice_needs_names() to
tell whether choosing between two symbols needs their names.
symbols__fixup_duplicate() keeps one of the symbols that share a start
address. It compares size, type and binding first. If those are equal,
it compares leading underscores, name lengths and the result of
arch__choose_best_symbol().
An unresolved lazy symbol stores a file offset instead of a name
pointer, so the lazy loader cannot call choose_best_symbol() before it
has read both names. The new helper lets it leave the names unread when
the attributes already pick one of the symbols.
Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: Alireza Haghdoost <haghdoost@uber.com>
---
tools/perf/util/symbol.c | 26 ++++++++++++++++++++++++--
tools/perf/util/symbol.h | 1 +
2 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 35b08c05a2fd..d9866916fe9a 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -157,11 +157,15 @@ int __weak arch__choose_best_symbol(struct symbol *syma,
return SYMBOL_A;
}
-static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
+/*
+ * Choose between two symbols at the same address by size, type and binding.
+ * Returns SYMBOL_A or SYMBOL_B, or -1 if only their names can decide.
+ */
+static int choose_best_symbol_by_attrs(const struct symbol *syma,
+ const struct symbol *symb)
{
s64 a;
s64 b;
- size_t na, nb;
/* Prefer a symbol with non zero length */
a = syma->end - syma->start;
@@ -194,6 +198,24 @@ static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
if (b && !a)
return SYMBOL_B;
+ return -1;
+}
+
+bool symbol__choice_needs_names(const struct symbol *syma, const struct symbol *symb)
+{
+ return choose_best_symbol_by_attrs(syma, symb) < 0;
+}
+
+static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
+{
+ int ret = choose_best_symbol_by_attrs(syma, symb);
+ s64 a;
+ s64 b;
+ size_t na, nb;
+
+ if (ret >= 0)
+ return ret;
+
/* Prefer a symbol with less underscores */
a = prefix_underscores_count(syma->name);
b = prefix_underscores_count(symb->name);
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 8b8ab7e1e148..41417bc85bb9 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -327,6 +327,7 @@ int arch__compare_symbol_names(const char *namea, const char *nameb);
int arch__compare_symbol_names_n(const char *namea, const char *nameb,
unsigned int n);
int arch__choose_best_symbol(struct symbol *syma, struct symbol *symb);
+bool symbol__choice_needs_names(const struct symbol *syma, const struct symbol *symb);
enum symbol_tag_include {
SYMBOL_TAG_INCLUDE__NONE = 0,
--
Git-157)
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v9 2/7] perf symbols: Point to the symbol name instead of embedding it
2026-11-24 19:28 [PATCH v9 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 1/7] perf symbols: Split out name-independent duplicate symbol selection Alireza Haghdoost via B4 Relay
@ 2026-11-24 19:28 ` Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 3/7] perf symbols: Add lazy symbol names Alireza Haghdoost via B4 Relay
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Alireza Haghdoost via B4 Relay @ 2026-11-24 19:28 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: linux-perf-users, linux-kernel, Alireza Haghdoost
From: Alireza Haghdoost <haghdoost@uber.com>
This patch changes the name field of struct symbol from a flexible
array (char name[]) to a pointer (char *name). It does not change
behavior, but each symbol allocation grows by 8 bytes on 64-bit hosts.
The next patch creates symbols whose names are read only when needed.
perf does not know the length of such a name when it creates the symbol,
so it cannot allocate the name after the symbol. It cannot reallocate
the symbol later either, because the symbol array and the callers hold
pointers to it. With a pointer, the name is allocated separately and set
when it is read.
Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: Alireza Haghdoost <haghdoost@uber.com>
---
tools/perf/ui/stdio/hist.c | 1 +
tools/perf/util/symbol.c | 1 +
tools/perf/util/symbol.h | 2 +-
3 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/tools/perf/ui/stdio/hist.c b/tools/perf/ui/stdio/hist.c
index 3240c39bd025..45841eac6f52 100644
--- a/tools/perf/ui/stdio/hist.c
+++ b/tools/perf/ui/stdio/hist.c
@@ -106,6 +106,7 @@ static void init_rem_hits(void)
return;
}
+ rem_sq_bracket->name = (char *)(rem_sq_bracket + 1);
strcpy(rem_sq_bracket->name, "[...]");
rem_hits.ms.sym = rem_sq_bracket;
}
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index d9866916fe9a..25efc05c2c7a 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -634,6 +634,7 @@ struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *
pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
__func__, name, start, sym->end);
+ sym->name = (char *)(sym + 1);
memcpy(sym->name, name, namelen);
return sym;
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 41417bc85bb9..8abf6ec05bd6 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -92,7 +92,7 @@ struct symbol {
/** Architecture specific. Unused except on PPC where it holds st_other. */
u8 arch_sym;
/** The name of length namelen associated with the symbol. */
- char name[];
+ char *name;
};
/**
--
Git-157)
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v9 3/7] perf symbols: Add lazy symbol names
2026-11-24 19:28 [PATCH v9 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 1/7] perf symbols: Split out name-independent duplicate symbol selection Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 2/7] perf symbols: Point to the symbol name instead of embedding it Alireza Haghdoost via B4 Relay
@ 2026-11-24 19:28 ` Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 4/7] perf script: Add --lazy-load-symbols to load symbol names lazily Alireza Haghdoost via B4 Relay
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Alireza Haghdoost via B4 Relay @ 2026-11-24 19:28 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: linux-perf-users, linux-kernel, Alireza Haghdoost
From: Alireza Haghdoost <haghdoost@uber.com>
This patch lets a symbol be created without its name, so that perf reads
the name lazily only when it needs it and does not waste RSS memory
holding symbol names that are never sampled. symbol__new_lazy() stores
the file offset of the name instead, in a union with the name pointer,
and SYMBOL_FLAG_LAZY_NAME tells which one is set. This is a preparatory
change for a later patch in this series, which adds --lazy-load-symbols
to the ELF loader.
The address lookups load the name of the symbol they return. The walks
over all symbols of a DSO and the lookups by name get the symbols from
dso__named_symbols(), which loads every name the first time. The loader
will mark a DSO that has lazy names as pending. dso__named_symbols()
loads them and marks the DSO eager, also when the DSO is not loaded yet,
so that a load racing with a walk, as in perf top, adds no symbols
without names.
Names are read under the DSO lock with pread() from the symbol table
file, which is open only while perf reads them, and demangled as at
load time. The file name is built again and opened in perf's mount
namespace, so the loader must create lazy names only for a file that it
opened there by that name. If another thread loaded them first, perf
does not open the file. A name that cannot be read becomes "[unknown]",
and address lookups do not return that symbol.
SYMBOL_FLAG_NAME_ALLOCATED marks a loaded name, which is freed with the
symbol. The ELF loader looks symbols up with symbols__find(), so that it
does not read names while it loads the symbols.
No functional change, since no symbol has a lazy name yet.
Signed-off-by: Alireza Haghdoost <haghdoost@uber.com>
---
tools/perf/ui/browsers/map.c | 2 +-
tools/perf/util/dso.h | 28 +++++++++-
tools/perf/util/symbol-elf.c | 89 +++++++++++++++++++++++++++++++-
tools/perf/util/symbol-minimal.c | 6 +++
tools/perf/util/symbol.c | 108 +++++++++++++++++++++++++++++++++++----
tools/perf/util/symbol.h | 28 ++++++++--
tools/perf/util/symbol_conf.h | 1 +
7 files changed, 245 insertions(+), 17 deletions(-)
diff --git a/tools/perf/ui/browsers/map.c b/tools/perf/ui/browsers/map.c
index 51a71b20947f..7e63954d65a3 100644
--- a/tools/perf/ui/browsers/map.c
+++ b/tools/perf/ui/browsers/map.c
@@ -108,7 +108,7 @@ static int map_browser__run(struct map_browser *browser)
int map__browse(struct map *map)
{
- struct symbols *symbols = dso__symbols(map__dso(map));
+ struct symbols *symbols = dso__named_symbols(map__dso(map));
struct map_browser mb = {
.b = {
.refresh = ui_browser__argv_refresh,
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index 63c1b7d6f6f8..ebd8230b9895 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -154,6 +154,15 @@ enum dso_swap_type {
DSO_SWAP__YES,
};
+enum dso_lazy_names {
+ /* No symbol has a lazy name. */
+ DSO_LAZY_NAMES__NONE,
+ /* Some symbol names are not loaded yet. */
+ DSO_LAZY_NAMES__PENDING,
+ /* All names were needed once, so later loads read names eagerly. */
+ DSO_LAZY_NAMES__EAGER,
+};
+
enum dso_data_status {
DSO_DATA_STATUS_ERROR = -1,
DSO_DATA_STATUS_UNKNOWN = 0,
@@ -344,6 +353,8 @@ DECLARE_RC_STRUCT(dso) {
u8 bintype_searched:1;
u8 debuginfo_searched:1;
bool loaded;
+ /* Under the DSO lock. */
+ enum dso_lazy_names lazy_names;
u8 rel;
char name[];
};
@@ -360,10 +371,10 @@ int dso_id__cmp(const struct dso_id *a, const struct dso_id *b);
* @data: opaque data pointer passed to @cb
*/
#define dso__for_each_symbol(dso, cb, data) \
- symbols__visit(dso__symbols(dso), cb, data)
+ symbols__visit(dso__named_symbols(dso), cb, data)
#define dso__for_each_symbol_by_name(dso, cb, data) \
- symbols__visit_by_name(dso__symbols(dso), cb, data)
+ symbols__visit_by_name(dso__named_symbols(dso), cb, data)
static inline void *dso__a2l(const struct dso *dso)
{
@@ -602,6 +613,16 @@ static inline void dso__set_loaded(struct dso *dso)
RC_CHK_ACCESS(dso)->loaded = true;
}
+static inline enum dso_lazy_names dso__lazy_names(const struct dso *dso)
+{
+ return RC_CHK_ACCESS(dso)->lazy_names;
+}
+
+static inline void dso__set_lazy_names(struct dso *dso, enum dso_lazy_names lazy_names)
+{
+ RC_CHK_ACCESS(dso)->lazy_names = lazy_names;
+}
+
static inline struct mutex *dso__lock(struct dso *dso)
__returns_ctx_lock(&RC_CHK_ACCESS(dso)->lock)
{
@@ -792,6 +813,9 @@ char *dso__filename_with_chroot(const struct dso *dso, const char *filename);
bool dso__sorted_by_name(const struct dso *dso);
void dso__sort_by_name(struct dso *dso);
+struct symbols *dso__named_symbols(struct dso *dso);
+void dso__load_symbol_names(struct dso *dso, struct symbol **syms, size_t nr)
+ __must_hold(dso__lock(dso));
int dso__swap_init(struct dso *dso, unsigned char eidata);
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index bda0b92ada24..4b9deea65b09 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -460,7 +460,7 @@ static bool get_ifunc_name(Elf *elf, struct dso *dso, GElf_Ehdr *ehdr,
addr -= phdr.p_vaddr - phdr.p_offset;
- sym = dso__find_symbol_nocache(dso, addr);
+ sym = symbols__find(dso__symbols(dso), addr);
/* Expecting the address to be an IFUNC or IFUNC alias */
if (!sym || sym->start != addr ||
@@ -742,7 +742,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
* by symbols__fixup_end() to overlap .plt. Truncate it before adding
* a symbol for .plt header.
*/
- f = dso__find_symbol_nocache(dso, shdr_plt.sh_offset);
+ f = symbols__find(dso__symbols(dso), shdr_plt.sh_offset);
if (f && f->start < shdr_plt.sh_offset && f->end > shdr_plt.sh_offset)
f->end = shdr_plt.sh_offset;
@@ -1631,6 +1631,91 @@ static int dso__process_kernel_symbol(struct dso *dso, struct map *map,
return 0;
}
+static char *read_name_at(int fd, u64 off)
+{
+ char small[256], *buf = NULL, *tmp;
+ size_t size = sizeof(small);
+ ssize_t n = pread(fd, small, size, off);
+
+ if (n > 0 && memchr(small, '\0', n))
+ return strdup(small);
+
+ while (n == (ssize_t)size) {
+ size *= 2;
+ tmp = realloc(buf, size);
+ if (!tmp)
+ break;
+ buf = tmp;
+ n = pread(fd, buf, size, off);
+ if (n > 0 && memchr(buf, '\0', n))
+ return buf;
+ }
+ free(buf);
+ return NULL;
+}
+
+static int read_symbol_names(struct dso *dso, int fd, struct symbol **syms, size_t nr)
+{
+ int err = 0;
+
+ for (size_t i = 0; i < nr; i++) {
+ char *name, *demangled;
+
+ if (!symbol__needs_lazy_name(syms[i]))
+ continue;
+ name = read_name_at(fd, syms[i]->name_off);
+ if (!name) {
+ pr_debug("%s: cannot read the symbol name at %#" PRIx64 "\n",
+ dso__long_name(dso), syms[i]->name_off);
+ err = -1;
+ continue;
+ }
+ demangled = dso__demangle_sym(dso, /*kmodule=*/0, name);
+ if (demangled) {
+ free(name);
+ name = demangled;
+ }
+ symbol__set_loaded_name(syms[i], name);
+ }
+ return err;
+}
+
+/*
+ * A name that cannot be read becomes "[unknown]". The file name is built
+ * again and opened in perf's mount namespace, so lazy names must only be
+ * created for a symbol table file that perf opened there by that name.
+ */
+void dso__load_symbol_names(struct dso *dso, struct symbol **syms, size_t nr)
+{
+ char *filename;
+ bool decomp;
+ int fd = -1;
+ size_t i;
+
+ /* Another thread can load the names before the caller takes the lock. */
+ for (i = 0; i < nr && !symbol__needs_lazy_name(syms[i]); i++)
+ ;
+ if (i == nr)
+ return;
+
+ filename = dso__get_filename(dso, "", &decomp, dso__symtab_type(dso));
+ if (filename)
+ fd = open(filename, O_RDONLY | O_CLOEXEC);
+ if (fd >= 0) {
+ read_symbol_names(dso, fd, syms, nr);
+ close(fd);
+ } else {
+ pr_debug("%s: cannot open the symbol table file\n", dso__long_name(dso));
+ }
+ if (filename)
+ dso__put_filename(dso, filename, decomp);
+
+ for (i = 0; i < nr; i++) {
+ if (symbol__needs_lazy_name(syms[i]))
+ symbol__set_unknown_name(syms[i]);
+ }
+}
+
static int
dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
struct symsrc *runtime_ss, int kmodule, int dynsym)
diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-minimal.c
index 0a71d1463952..fa87c2d9de2a 100644
--- a/tools/perf/util/symbol-minimal.c
+++ b/tools/perf/util/symbol-minimal.c
@@ -330,6 +330,12 @@ enum dso_type dso__type_fd(int fd)
return DSO__TYPE_32BIT;
}
+void dso__load_symbol_names(struct dso *dso __maybe_unused,
+ struct symbol **syms __maybe_unused,
+ size_t nr __maybe_unused)
+{
+}
+
int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
struct symsrc *ss,
struct symsrc *runtime_ss __maybe_unused,
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 25efc05c2c7a..e1310f950d3d 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -618,10 +618,10 @@ void symbols__fixup_end(struct symbols *symbols, bool is_kallsyms)
up_write(&symbols->lock);
}
-struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
+static struct symbol *symbol__alloc(u64 start, u64 len, u8 binding, u8 type,
+ u16 flags, size_t name_size)
{
- size_t namelen = strlen(name) + 1;
- struct symbol *sym = calloc(1, sizeof(*sym) + namelen);
+ struct symbol *sym = calloc(1, sizeof(*sym) + name_size);
if (sym == NULL)
return NULL;
@@ -629,7 +629,18 @@ struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *
sym->start = start;
sym->end = len ? start + len : start;
atomic_init(&sym->flags, (type << SYMBOL_FLAG_TYPE_SHIFT) |
- (binding << SYMBOL_FLAG_BINDING_SHIFT));
+ (binding << SYMBOL_FLAG_BINDING_SHIFT) | flags);
+ return sym;
+}
+
+struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
+{
+ size_t namelen = strlen(name) + 1;
+ struct symbol *sym = symbol__alloc(start, len, binding, type, 0, namelen);
+
+ if (sym == NULL)
+ return NULL;
+
sym->namelen = namelen - 1;
pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
@@ -640,10 +651,42 @@ struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *
return sym;
}
+struct symbol *symbol__new_lazy(u64 start, u64 len, u8 binding, u8 type, u64 name_off)
+{
+ struct symbol *sym = symbol__alloc(start, len, binding, type,
+ SYMBOL_FLAG_LAZY_NAME, 0);
+
+ if (sym)
+ sym->name_off = name_off;
+ return sym;
+}
+
+static const char unknown_name[] = "[unknown]";
+
+/* @sym takes ownership of @name. */
+void symbol__set_loaded_name(struct symbol *sym, char *name)
+{
+ sym->namelen = strlen(name);
+ sym->name = name;
+ if (name != unknown_name)
+ atomic_fetch_or_explicit(&sym->flags, SYMBOL_FLAG_NAME_ALLOCATED,
+ memory_order_relaxed);
+ atomic_fetch_and_explicit(&sym->flags, ~SYMBOL_FLAG_LAZY_NAME,
+ memory_order_release);
+}
+
+void symbol__set_unknown_name(struct symbol *sym)
+{
+ symbol__set_loaded_name(sym, (char *)unknown_name);
+}
+
void symbol__delete(struct symbol *sym)
{
if (!sym)
return;
+ if (atomic_load_explicit(&sym->flags, memory_order_relaxed) &
+ SYMBOL_FLAG_NAME_ALLOCATED)
+ free(sym->name);
symbol__annotation_delete(sym);
free(sym);
}
@@ -773,7 +816,7 @@ static struct symbol *symbols__find_range(struct symbol **arr, unsigned int cnt,
* Binary search @symbols->symbols for a symbol covering @ip. Lazily sorts
* @symbols under @symbols->lock if unsorted.
*/
-static struct symbol *symbols__find(struct symbols *symbols, u64 ip)
+struct symbol *symbols__find(struct symbols *symbols, u64 ip)
{
struct symbol *res;
@@ -950,14 +993,28 @@ void dso__delete_symbol(struct dso *dso, struct symbol *sym)
up_write(&symbols->lock);
}
+static struct symbol *dso__resolve_symbol_name(struct dso *dso, struct symbol *sym)
+{
+ if (!symbol_conf.lazy_load_symbols)
+ return sym;
+ if (sym && symbol__needs_lazy_name(sym)) {
+ mutex_lock(dso__lock(dso));
+ dso__load_symbol_names(dso, &sym, 1);
+ mutex_unlock(dso__lock(dso));
+ }
+ if (sym && sym->name == unknown_name)
+ return NULL;
+ return sym;
+}
+
struct symbol *dso__find_symbol(struct dso *dso, u64 addr)
{
- return symbols__find(dso__symbols(dso), addr);
+ return dso__resolve_symbol_name(dso, symbols__find(dso__symbols(dso), addr));
}
struct symbol *dso__find_symbol_nocache(struct dso *dso, u64 addr)
{
- return symbols__find(dso__symbols(dso), addr);
+ return dso__resolve_symbol_name(dso, symbols__find(dso__symbols(dso), addr));
}
struct symbol *dso__next_symbol_by_name(struct dso *dso, size_t *idx)
@@ -991,7 +1048,7 @@ struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_
*idx = SIZE_MAX;
if (!dso)
return NULL;
- symbols = dso__symbols(dso);
+ symbols = dso__named_symbols(dso);
if (!symbols__sort_by_name_read_lock(symbols)) {
s = symbols__find_by_name(symbols->symbols_by_name,
symbols->num_by_name,
@@ -1005,10 +1062,43 @@ struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_
return s;
}
-void dso__sort_by_name(struct dso *dso)
+/*
+ * Lazy names exist only in userspace DSOs. Checking that first avoids taking
+ * the DSO lock, which the callers that walk kernel symbols may hold.
+ */
+static bool dso__may_have_lazy_names(struct dso *dso)
+{
+ return symbol_conf.lazy_load_symbols && !dso__kernel(dso);
+}
+
+/*
+ * Symbols whose name cannot be read are named "[unknown]" rather than
+ * deleted, since address lookups can return them without the DSO lock.
+ *
+ * The DSO is marked eager even if it is not loaded yet, so that a load
+ * racing with the caller's walk does not add symbols without names.
+ */
+struct symbols *dso__named_symbols(struct dso *dso)
{
struct symbols *symbols = dso__symbols(dso);
+ if (dso__may_have_lazy_names(dso)) {
+ mutex_lock(dso__lock(dso));
+ if (dso__lazy_names(dso) == DSO_LAZY_NAMES__PENDING) {
+ down_read(&symbols->lock);
+ dso__load_symbol_names(dso, symbols->symbols, symbols->cnt);
+ up_read(&symbols->lock);
+ }
+ dso__set_lazy_names(dso, DSO_LAZY_NAMES__EAGER);
+ mutex_unlock(dso__lock(dso));
+ }
+ return symbols;
+}
+
+void dso__sort_by_name(struct dso *dso)
+{
+ struct symbols *symbols = dso__named_symbols(dso);
+
down_read(&symbols->lock);
if (!symbols->sorted_by_name) {
up_read(&symbols->lock);
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 8abf6ec05bd6..64fdf402d219 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -79,8 +79,16 @@ enum symbol_idle_kind {
#define SYMBOL_FLAG_INLINED (1 << 11)
#define SYMBOL_FLAG_ANNOTATE2 (1 << 12)
#define SYMBOL_FLAG_IFUNC_ALIAS (1 << 13)
+#define SYMBOL_FLAG_LAZY_NAME (1 << 14)
+#define SYMBOL_FLAG_NAME_ALLOCATED (1 << 15)
/**
* A symtab entry.
+ *
+ * The DSO lookups such as dso__find_symbol() load a lazy name before
+ * returning the symbol, and the address lookups return NULL if it cannot
+ * be read. Code that walks dso__symbols() directly must use
+ * dso__named_symbols() or not read the name of a symbol with
+ * SYMBOL_FLAG_LAZY_NAME.
*/
struct symbol {
/** Range of symbol [start, end). */
@@ -91,8 +99,12 @@ struct symbol {
_Atomic uint16_t flags;
/** Architecture specific. Unused except on PPC where it holds st_other. */
u8 arch_sym;
- /** The name of length namelen associated with the symbol. */
- char *name;
+ union {
+ /** The name of length namelen associated with the symbol. */
+ char *name;
+ /** File offset of the name while SYMBOL_FLAG_LAZY_NAME is set. */
+ u64 name_off;
+ };
};
/**
@@ -132,6 +144,13 @@ void symbols__exit(struct symbols *symbols);
void symbols__delete(struct symbols *symbols);
void symbols__sort_read_lock(struct symbols *symbols) __acquires_shared(&symbols->lock);
+/* Pairs with the release in symbol__set_loaded_name(). */
+static inline bool symbol__needs_lazy_name(const struct symbol *sym)
+{
+ return (atomic_load_explicit(&sym->flags, memory_order_acquire) &
+ SYMBOL_FLAG_LAZY_NAME) != 0;
+}
+
static inline u8 symbol__type(const struct symbol *sym)
{
return (atomic_load_explicit(&sym->flags, memory_order_relaxed) &
@@ -252,6 +271,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);
+struct symbol *symbol__new_lazy(u64 start, u64 len, u8 binding, u8 type, u64 name_off);
+void symbol__set_loaded_name(struct symbol *sym, char *name);
+void symbol__set_unknown_name(struct symbol *sym);
size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
const struct addr_location *al,
bool unknown_as_addr,
@@ -282,10 +304,10 @@ int dso__load_bfd_symbols(struct dso *dso, const char *debugfile);
int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
struct symsrc *runtime_ss, int kmodule);
int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);
-
char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name);
int symbols__insert(struct symbols *symbols, struct symbol *sym);
+struct symbol *symbols__find(struct symbols *symbols, u64 ip);
void symbols__fixup_duplicate(struct symbols *symbols);
void symbols__fixup_end(struct symbols *symbols, bool is_kallsyms);
diff --git a/tools/perf/util/symbol_conf.h b/tools/perf/util/symbol_conf.h
index aaa7148c3ac1..f47482072625 100644
--- a/tools/perf/util/symbol_conf.h
+++ b/tools/perf/util/symbol_conf.h
@@ -76,6 +76,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,
--
Git-157)
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v9 4/7] perf script: Add --lazy-load-symbols to load symbol names lazily
2026-11-24 19:28 [PATCH v9 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
` (2 preceding siblings ...)
2026-11-24 19:28 ` [PATCH v9 3/7] perf symbols: Add lazy symbol names Alireza Haghdoost via B4 Relay
@ 2026-11-24 19:28 ` Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 5/7] perf report/top: Add --lazy-load-symbols Alireza Haghdoost via B4 Relay
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Alireza Haghdoost via B4 Relay @ 2026-11-24 19:28 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: linux-perf-users, linux-kernel, Alireza Haghdoost
From: Alireza Haghdoost <haghdoost@uber.com>
This patch adds a --lazy-load-symbols option to perf script. With it,
the ELF loader creates userspace .symtab and .dynsym symbols with lazy
names, and perf reads a name only when a lookup returns that symbol.
Eager loading stays the default.
perf script loads the whole ELF symbol table of every DSO that appears
in a sample and keeps every symbol, with its demangled name, until exit.
Most of those symbols are never sampled, and their names take most of
the memory. On a large profile this is the main memory cost of perf
script, and in a memory-limited cgroup it gets perf OOM-killed.
On a 45,073-sample callchain profile of a large C++ service (about 1M
symbols), lazy loading cuts max RSS from 315 MiB to 164 MiB, with
identical output. It saves little run time, 1.97 seconds against 2.26,
because one of the binaries exports most of its symbols, and duplicate
selection reads the names of each .dynsym copy and of the .symtab
symbol that it copies.
The loader still creates every symbol, so end fixup, duplicate
selection, PLT synthesis and IFUNC marking do not change. It uses lazy
names only if the string table is stored as is in the symbol table file,
since names are read from it with pread().
A few names are still read at load time: aliases that duplicate
selection can tell apart only by name, such as the .dynsym copies of
.symtab symbols, and IFUNC symbols, whose names PLT synthesis uses.
The names are read from the symbol table file while perf script runs,
at the offsets it had at load time. So the output is the same as with
eager loading as long as the file does not change in the meantime,
which is rare.
The kernel, modules, PPC64 .opd and .gnu_debugdata still load names
eagerly. So does a binary of a process in another mount namespace
unless it is read from the build-id cache, since names are read in
perf's own namespace: not every perf top thread can switch namespaces.
So does a file that dso__load() found through its chroot fallback,
since perf may not find that file again by the name it builds.
Signed-off-by: Alireza Haghdoost <haghdoost@uber.com>
---
tools/perf/Documentation/perf-script.txt | 9 +++
tools/perf/builtin-script.c | 2 +
tools/perf/util/symbol-elf.c | 129 ++++++++++++++++++++++++++++---
tools/perf/util/symbol.c | 3 +
tools/perf/util/symsrc.h | 2 +
5 files changed, 134 insertions(+), 11 deletions(-)
diff --git a/tools/perf/Documentation/perf-script.txt b/tools/perf/Documentation/perf-script.txt
index f0228e784ced..844167017521 100644
--- a/tools/perf/Documentation/perf-script.txt
+++ b/tools/perf/Documentation/perf-script.txt
@@ -343,6 +343,15 @@ include::itrace.txt[]
Default: 127
+--lazy-load-symbols::
+ Read and demangle userspace ELF symbol names only when perf needs
+ them. By default, perf reads every symbol name from each binary
+ referenced by a sample. This option reduces memory use when samples
+ reference only a small fraction of a large binary's symbols. The
+ names are read from the binaries while perf script runs, so the
+ binaries must not change in the meantime. If a binary cannot be
+ opened when a name is needed, perf shows that symbol as unknown.
+
--ns::
Use 9 decimal places when displaying time (i.e. show the nanoseconds)
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index b6691ebb1b4e..88aaf9cfd132 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -4287,6 +4287,8 @@ 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_BOOLEAN(0, "lazy-load-symbols", &symbol_conf.lazy_load_symbols,
+ "Read symbol names only when they are needed"),
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,
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 4b9deea65b09..e623fb04cfba 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -12,6 +12,7 @@
#include "libbfd.h"
#include "map.h"
#include "maps.h"
+#include "namespaces.h"
#include "symbol.h"
#include "symsrc.h"
#include "machine.h"
@@ -1396,6 +1397,7 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
ss->fd = fd;
ss->ehdr = ehdr;
ss->type = type;
+ ss->chroot = false;
return 0;
@@ -1716,6 +1718,70 @@ void dso__load_symbol_names(struct dso *dso, struct symbol **syms, size_t nr)
}
}
+/*
+ * symbols__fixup_duplicate() compares the names of symbols at the same
+ * address only when their size, type and binding tie. Load those names.
+ */
+static int dso__load_alias_names(struct dso *dso, int fd)
+{
+ struct symbols *symbols = dso__symbols(dso);
+ unsigned int i, end, a, b;
+ int err = 0;
+
+ symbols__sort_read_lock(symbols);
+ for (i = 0; i < symbols->cnt && !err; i = end) {
+ for (end = i + 1; end < symbols->cnt &&
+ symbols->symbols[end]->start == symbols->symbols[i]->start; end++)
+ ;
+ for (a = i; a < end && !err; a++) {
+ for (b = a + 1; b < end && !err; b++) {
+ struct symbol *pair[2] = {
+ symbols->symbols[a], symbols->symbols[b]
+ };
+
+ if (symbol__choice_needs_names(pair[0], pair[1]))
+ err = read_symbol_names(dso, fd, pair, 2);
+ }
+ }
+ }
+ up_read(&symbols->lock);
+ return err;
+}
+
+/* PLT synthesis names IRELATIVE slots after IFUNC symbols. Load those names. */
+static int dso__load_ifunc_names(struct dso *dso, int fd)
+{
+ struct symbols *symbols = dso__symbols(dso);
+ int err = 0;
+
+ symbols__sort_read_lock(symbols);
+ for (unsigned int i = 0; i < symbols->cnt && !err; i++) {
+ struct symbol *pos = symbols->symbols[i];
+
+ if (symbol__type(pos) == STT_GNU_IFUNC || symbol__ifunc_alias(pos))
+ err = read_symbol_names(dso, fd, &pos, 1);
+ }
+ up_read(&symbols->lock);
+ return err;
+}
+
+/*
+ * dso__load_symbol_names() builds the file name again and opens it in perf's
+ * mount namespace. At load time, perf opened the same file there if it is in
+ * the build-id cache or if the DSO needs no namespace switch, unless perf
+ * found it through the chroot fallback.
+ */
+static bool dso__can_reopen_symtab(struct dso *dso, struct symsrc *ss)
+{
+ struct nsinfo *nsi = dso__nsinfo(dso);
+
+ if (ss->chroot)
+ return false;
+ return (ss->type == DSO_BINARY_TYPE__BUILD_ID_CACHE ||
+ ss->type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO ||
+ !nsi || !nsinfo__need_setns(nsi));
+}
+
static int
dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
struct symsrc *runtime_ss, int kmodule, int dynsym)
@@ -1733,11 +1799,23 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
GElf_Sym sym;
Elf_Scn *sec, *sec_strndx;
Elf *elf;
- int nr = 0;
+ int nr = 0, nr_lazy = 0;
bool remap_kernel = false, adjust_kernel_syms = false;
u64 max_text_sh_offset = 0;
struct elf_phdr_cache phdr_cache = {};
struct elf_sec_cache_entry sec_cache[ELF_SEC_CACHE_SIZE];
+ /*
+ * Names are read later from the file of dso__symtab_type(). The
+ * .gnu_debugdata symbol table is a temporary file.
+ */
+ bool lazy_allowed = (symbol_conf.lazy_load_symbols && !dso__kernel(dso) &&
+ dso__lazy_names(dso) != DSO_LAZY_NAMES__EAGER &&
+ syms_ss->type == dso__symtab_type(dso) &&
+ syms_ss->type != DSO_BINARY_TYPE__GNU_DEBUGDATA &&
+ dso__can_reopen_symtab(dso, syms_ss) &&
+ !runtime_ss->opdsec);
+ bool lazy_names = false;
+ GElf_Shdr strshdr;
if (kmap && !kmaps)
return -1;
@@ -1773,6 +1851,11 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
if (symstrs == NULL)
goto out_elf_end;
+ /* Names are read with pread(), so the string table must be stored as is. */
+ if (lazy_allowed && gelf_getshdr(sec, &strshdr) &&
+ strshdr.sh_type == SHT_STRTAB && !(strshdr.sh_flags & SHF_COMPRESSED))
+ lazy_names = true;
+
sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
if (sec_strndx == NULL)
goto out_elf_end;
@@ -2011,16 +2094,24 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
}
}
- if (!elf_name)
- elf_name = elf_sym__name(&sym, symstrs);
- demangled = dso__demangle_sym(dso, kmodule, elf_name);
- if (demangled != NULL)
- elf_name = demangled;
-
- f = symbol__new(sym.st_value, sym.st_size,
- GELF_ST_BIND(sym.st_info),
- GELF_ST_TYPE(sym.st_info), elf_name);
- free(demangled);
+ if (lazy_names && sym.st_name < strshdr.sh_size) {
+ f = symbol__new_lazy(sym.st_value, sym.st_size,
+ GELF_ST_BIND(sym.st_info),
+ GELF_ST_TYPE(sym.st_info),
+ strshdr.sh_offset + sym.st_name);
+ nr_lazy++;
+ } else {
+ if (!elf_name)
+ elf_name = elf_sym__name(&sym, symstrs);
+ demangled = dso__demangle_sym(dso, kmodule, elf_name);
+ if (demangled != NULL)
+ elf_name = demangled;
+
+ f = symbol__new(sym.st_value, sym.st_size,
+ GELF_ST_BIND(sym.st_info),
+ GELF_ST_TYPE(sym.st_info), elf_name);
+ free(demangled);
+ }
if (!f)
goto out_elf_end;
@@ -2032,10 +2123,19 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
}
dso__put(curr_dso);
elf_phdr_cache__exit(&phdr_cache);
+
+ if (nr_lazy) {
+ dso__set_lazy_names(dso, DSO_LAZY_NAMES__PENDING);
+ pr_debug2("%s: %d %s symbols with lazy names\n", dso__long_name(dso),
+ nr_lazy, dynsym ? ".dynsym" : ".symtab");
+ }
return nr;
out_elf_end:
dso__put(curr_dso);
elf_phdr_cache__exit(&phdr_cache);
+ /* The symbols inserted before the error stay in the DSO. */
+ if (nr_lazy)
+ dso__set_lazy_names(dso, DSO_LAZY_NAMES__PENDING);
return -1;
}
@@ -2102,9 +2202,16 @@ int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
if (nr > 0) {
struct kmap *kmap = dso__kernel(dso) ? map__kmap(map) : NULL;
struct maps *kmaps = kmap ? map__kmaps(map) : NULL;
+ /* Lazy names are only in syms_ss. */
+ bool lazy = (dso__lazy_names(dso) == DSO_LAZY_NAMES__PENDING);
symbols__fixup_end(dso__symbols(dso), false);
+ if (lazy && !symbol_conf.allow_aliases &&
+ dso__load_alias_names(dso, syms_ss->fd))
+ return -1;
symbols__fixup_duplicate(dso__symbols(dso));
+ if (lazy && dso__load_ifunc_names(dso, syms_ss->fd))
+ return -1;
if (kmap) {
/*
* We need to fixup this here too because we create new
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index e1310f950d3d..d6a841dadcad 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -2394,6 +2394,7 @@ int dso__load(struct dso *dso, struct map *map)
bool next_slot = false;
bool is_reg;
bool nsexit;
+ bool chroot = false;
int bfdrc = -1;
int sirc = -1;
@@ -2419,6 +2420,7 @@ int dso__load(struct dso *dso, struct map *map)
is_reg = is_regular_file(new_name);
strlcpy(name, new_name, PATH_MAX);
free(new_name);
+ chroot = true;
}
}
@@ -2440,6 +2442,7 @@ int dso__load(struct dso *dso, struct map *map)
if (!is_reg || sirc < 0)
continue;
+ ss->chroot = chroot;
if (!syms_ss && symsrc__has_symtab(ss)) {
syms_ss = ss;
diff --git a/tools/perf/util/symsrc.h b/tools/perf/util/symsrc.h
index edf82028c9e6..94f51dfaef64 100644
--- a/tools/perf/util/symsrc.h
+++ b/tools/perf/util/symsrc.h
@@ -16,6 +16,8 @@ struct symsrc {
char *name;
int fd;
enum dso_binary_type type;
+ /* Found through the chroot fallback of dso__load(). */
+ bool chroot;
#ifdef HAVE_LIBELF_SUPPORT
Elf *elf;
--
Git-157)
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v9 5/7] perf report/top: Add --lazy-load-symbols
2026-11-24 19:28 [PATCH v9 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
` (3 preceding siblings ...)
2026-11-24 19:28 ` [PATCH v9 4/7] perf script: Add --lazy-load-symbols to load symbol names lazily Alireza Haghdoost via B4 Relay
@ 2026-11-24 19:28 ` Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 6/7] perf test: Add lazy symbol loading unit tests Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 7/7] perf test: Add lazy symbol loading shell tests Alireza Haghdoost via B4 Relay
6 siblings, 0 replies; 8+ messages in thread
From: Alireza Haghdoost via B4 Relay @ 2026-11-24 19:28 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: linux-perf-users, linux-kernel, Alireza Haghdoost
From: Alireza Haghdoost <haghdoost@uber.com>
This patch adds the --lazy-load-symbols option of perf script to perf
report and perf top. Like perf script, they load the whole symbol table
of every DSO that appears in a sample and keep every name until exit.
The symbol code already handles both tools. The address lookups that
build the histogram entries and call chains load the names of the
symbols they return, and the TUI map browser gets all names from
dso__named_symbols().
perf top reads the names while it runs, which can be long, so a binary
that is updated in the meantime can show wrong names. The documentation
of perf top says so.
Suggested-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Alireza Haghdoost <haghdoost@uber.com>
---
tools/perf/Documentation/perf-report.txt | 9 +++++++++
tools/perf/Documentation/perf-top.txt | 10 ++++++++++
tools/perf/builtin-report.c | 2 ++
tools/perf/builtin-top.c | 2 ++
4 files changed, 23 insertions(+)
diff --git a/tools/perf/Documentation/perf-report.txt b/tools/perf/Documentation/perf-report.txt
index e145124d6c9a..b8c34bb4cd74 100644
--- a/tools/perf/Documentation/perf-report.txt
+++ b/tools/perf/Documentation/perf-report.txt
@@ -347,6 +347,15 @@ OPTIONS
Default: 127
+--lazy-load-symbols::
+ Read and demangle userspace ELF symbol names only when perf needs
+ them. By default, perf reads every symbol name from each binary
+ referenced by a sample. This option reduces memory use when samples
+ reference only a small fraction of a large binary's symbols. The
+ names are read from the binaries while perf report runs, so the
+ binaries must not change in the meantime. If a binary cannot be
+ opened when a name is needed, perf shows that symbol as unknown.
+
-G::
--inverted::
alias for inverted caller based call graph.
diff --git a/tools/perf/Documentation/perf-top.txt b/tools/perf/Documentation/perf-top.txt
index c5e96da6ed82..9ec039c8e0b9 100644
--- a/tools/perf/Documentation/perf-top.txt
+++ b/tools/perf/Documentation/perf-top.txt
@@ -218,6 +218,16 @@ Default is to monitor all CPUS.
Default: /proc/sys/kernel/perf_event_max_stack when present, 127 otherwise.
+--lazy-load-symbols::
+ Read and demangle userspace ELF symbol names only when perf needs
+ them. By default, perf reads every symbol name from each binary
+ referenced by a sample. This option reduces memory use when samples
+ reference only a small fraction of a large binary's symbols. The
+ names are read from the binaries while perf top runs, so a binary
+ that is updated in the meantime can show wrong names. If a binary
+ cannot be opened when a name is needed, perf shows that symbol as
+ unknown.
+
--ignore-callees=<regex>::
Ignore callees of the function(s) matching the given regex.
This has the effect of collecting the callers of each such
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 6672a4d16529..8ca54463a84b 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -1414,6 +1414,8 @@ int cmd_report(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_BOOLEAN(0, "lazy-load-symbols", &symbol_conf.lazy_load_symbols,
+ "Read symbol names only when they are needed"),
OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
"alias for inverted call graph"),
OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index a79230b81300..02363e5c6460 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -1589,6 +1589,8 @@ int cmd_top(int argc, const char **argv)
OPT_INTEGER(0, "max-stack", &top.max_stack,
"Set the maximum stack depth when parsing the callchain. "
"Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
+ OPT_BOOLEAN(0, "lazy-load-symbols", &symbol_conf.lazy_load_symbols,
+ "Read symbol names only when they are needed"),
OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
"ignore callees of these functions in call graphs",
report_parse_ignore_callees_opt),
--
Git-157)
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v9 6/7] perf test: Add lazy symbol loading unit tests
2026-11-24 19:28 [PATCH v9 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
` (4 preceding siblings ...)
2026-11-24 19:28 ` [PATCH v9 5/7] perf report/top: Add --lazy-load-symbols Alireza Haghdoost via B4 Relay
@ 2026-11-24 19:28 ` Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 7/7] perf test: Add lazy symbol loading shell tests Alireza Haghdoost via B4 Relay
6 siblings, 0 replies; 8+ messages in thread
From: Alireza Haghdoost via B4 Relay @ 2026-11-24 19:28 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: linux-perf-users, linux-kernel, Alireza Haghdoost
From: Alireza Haghdoost <haghdoost@uber.com>
Add a "Lazy symbol loading" unit test suite. It covers duplicate
selection without names, lazy names that take one or more reads or
cannot be read at all, lookups from several threads while another one
walks the symbols, eager and lazy parity on a real DSO (perf itself,
or the one given with --dso), and a file that cannot be opened for one
name but gives the next one.
Signed-off-by: Alireza Haghdoost <haghdoost@uber.com>
---
tools/perf/tests/Build | 1 +
tools/perf/tests/builtin-test.c | 1 +
tools/perf/tests/symbol-lazy.c | 647 ++++++++++++++++++++++++++++++++++++++++
tools/perf/tests/tests.h | 1 +
4 files changed, 650 insertions(+)
diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 5eebb2ec5dde..1377dd465586 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -69,6 +69,7 @@ perf-test-y += sigtrap.o
perf-test-y += event_groups.o
perf-test-y += hybrid-merge.o
perf-test-y += symbols.o
+perf-test-y += symbol-lazy.o
perf-test-y += util.o
perf-test-y += hwmon_pmu.o
perf-test-y += tool_pmu.o
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 01c78ef20b97..c447432dc41f 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -189,6 +189,7 @@ static struct test_suite *generic_tests[] = {
&suite__event_groups,
&suite__hybrid_merge,
&suite__symbols,
+ &suite__symbol_lazy,
&suite__util,
&suite__subcmd_help,
&suite__kallsyms_split,
diff --git a/tools/perf/tests/symbol-lazy.c b/tools/perf/tests/symbol-lazy.c
new file mode 100644
index 000000000000..8bba54bf345b
--- /dev/null
+++ b/tools/perf/tests/symbol-lazy.c
@@ -0,0 +1,647 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <elf.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/resource.h>
+#include <unistd.h>
+#include <linux/kernel.h>
+
+#include "debug.h"
+#include "dso.h"
+#include "env.h"
+#include "machine.h"
+#include "map.h"
+#include "symbol.h"
+#include "symbol_conf.h"
+#include "tests.h"
+#include "thread.h"
+#include "util.h"
+
+static int test__symbol_lazy_choice(struct test_suite *test __maybe_unused,
+ int subtest __maybe_unused)
+{
+ struct choice_case {
+ u64 len;
+ u8 type, binding;
+ bool needs_names;
+ } cases[] = {
+ { 0x10, STT_FUNC, STB_GLOBAL, true },
+ { 0, STT_FUNC, STB_GLOBAL, false },
+ { 0x10, STT_NOTYPE, STB_GLOBAL, false },
+ { 0x10, STT_GNU_IFUNC, STB_GLOBAL, true },
+ { 0x10, STT_FUNC, STB_WEAK, false },
+ { 0x10, STT_FUNC, STB_LOCAL, false },
+ };
+ struct symbol *a, *b;
+ int ret = TEST_OK;
+ size_t i;
+
+ a = symbol__new(0x1000, 0x10, STB_GLOBAL, STT_FUNC, "a");
+ if (!a)
+ return TEST_FAIL;
+ for (i = 0; i < ARRAY_SIZE(cases) && ret == TEST_OK; i++) {
+ b = symbol__new(0x1000, cases[i].len, cases[i].binding,
+ cases[i].type, "b");
+ if (!b ||
+ symbol__choice_needs_names(a, b) != cases[i].needs_names ||
+ symbol__choice_needs_names(b, a) != cases[i].needs_names) {
+ pr_debug("case %zu\n", i);
+ ret = TEST_FAIL;
+ }
+ if (b)
+ symbol__delete(b);
+ }
+ symbol__delete(a);
+ return ret;
+}
+
+#ifdef HAVE_LIBELF_SUPPORT
+#define LAZY_SYM_START 0x1000
+#define LAZY_SYM_SIZE 0x100
+
+struct lazy_fixture {
+ char path[PATH_MAX];
+ struct dso *dso;
+ char **names;
+ size_t nr;
+};
+
+static void lazy_fixture__exit(struct lazy_fixture *f)
+{
+ size_t i;
+
+ dso__put(f->dso);
+ for (i = 0; f->names && i < f->nr; i++)
+ free(f->names[i]);
+ free(f->names);
+ unlink(f->path);
+}
+
+/*
+ * Write @nr names to a file and add a lazy symbol for each to a new DSO,
+ * whose symbol table file is that file. Name i is @len[i] characters long,
+ * or has no terminating NUL before the end of the file if @len[i] is 0.
+ */
+static int lazy_fixture__init(struct lazy_fixture *f, const size_t *len, size_t nr)
+{
+ u64 off = 1;
+ size_t i;
+ FILE *fp;
+ int fd;
+
+ memset(f, 0, sizeof(*f));
+ strlcpy(f->path, "/tmp/perf-test-lazy-names-XXXXXX", sizeof(f->path));
+ fd = mkstemp(f->path);
+ if (fd < 0)
+ return -1;
+ fp = fdopen(fd, "w");
+ if (!fp) {
+ close(fd);
+ unlink(f->path);
+ return -1;
+ }
+ fputc('\0', fp);
+
+ f->names = calloc(nr, sizeof(*f->names));
+ f->dso = dso__new(f->path);
+ if (!f->names || !f->dso)
+ goto out_close;
+ f->nr = nr;
+ dso__set_symtab_type(f->dso, DSO_BINARY_TYPE__SYSTEM_PATH_DSO);
+
+ for (i = 0; i < nr; i++) {
+ size_t n = len[i] ?: 32;
+ struct symbol *sym;
+
+ f->names[i] = malloc(n + 1);
+ if (!f->names[i])
+ goto out_close;
+ for (size_t j = 0; j < n; j++)
+ f->names[i][j] = 'a' + (i + j) % 26;
+ f->names[i][n] = '\0';
+ if (fwrite(f->names[i], 1, n + !!len[i], fp) != n + !!len[i])
+ goto out_close;
+
+ sym = symbol__new_lazy(LAZY_SYM_START + i * LAZY_SYM_SIZE,
+ LAZY_SYM_SIZE, STB_GLOBAL, STT_FUNC, off);
+ if (!sym)
+ goto out_close;
+ symbols__insert(dso__symbols(f->dso), sym);
+ off += n + !!len[i];
+ }
+ dso__set_lazy_names(f->dso, DSO_LAZY_NAMES__PENDING);
+ dso__set_loaded(f->dso);
+ if (fclose(fp)) {
+ lazy_fixture__exit(f);
+ return -1;
+ }
+ return 0;
+
+out_close:
+ fclose(fp);
+ lazy_fixture__exit(f);
+ return -1;
+}
+
+/* Add a lazy symbol at index @i whose name is past the end of the file. */
+static int lazy_fixture__add_unreadable(struct lazy_fixture *f, size_t i)
+{
+ struct symbol *sym = symbol__new_lazy(LAZY_SYM_START + i * LAZY_SYM_SIZE,
+ LAZY_SYM_SIZE, STB_GLOBAL, STT_FUNC,
+ 1 << 30);
+
+ if (!sym)
+ return -1;
+ symbols__insert(dso__symbols(f->dso), sym);
+ return 0;
+}
+
+static bool lazy_symbol_ok(const struct symbol *sym, const struct lazy_fixture *f,
+ size_t i)
+{
+ return (sym && sym->start == LAZY_SYM_START + i * LAZY_SYM_SIZE &&
+ !symbol__needs_lazy_name(sym) && !strcmp(sym->name, f->names[i]) &&
+ sym->namelen == strlen(f->names[i]));
+}
+
+static bool lazy_symbol_unknown(const struct symbol *sym, size_t i)
+{
+ return (sym && sym->start == LAZY_SYM_START + i * LAZY_SYM_SIZE &&
+ !symbol__needs_lazy_name(sym) && !strcmp(sym->name, "[unknown]"));
+}
+
+static int lazy_count_cb(struct symbol *sym __maybe_unused, void *data)
+{
+ (*(size_t *)data)++;
+ return 0;
+}
+
+static int lazy_count_names_cb(struct symbol *sym, void *data)
+{
+ *(size_t *)data += symbol__needs_lazy_name(sym);
+ return 0;
+}
+
+/* Count the symbols without loading their names. */
+static size_t lazy_nr_symbols(struct dso *dso)
+{
+ size_t nr = 0;
+
+ symbols__visit(dso__symbols(dso), lazy_count_cb, &nr);
+ return nr;
+}
+
+struct lazy_walk {
+ const struct lazy_fixture *f;
+ /* The first nr_ok names can be read, the others cannot. */
+ size_t nr_ok;
+ size_t i;
+ bool failed;
+};
+
+static int lazy_walk_cb(struct symbol *sym, void *data)
+{
+ struct lazy_walk *walk = data;
+ size_t i = walk->i++;
+
+ if (!(i < walk->nr_ok ? lazy_symbol_ok(sym, walk->f, i) : lazy_symbol_unknown(sym, i))) {
+ pr_debug("walk of name %zu failed\n", i);
+ walk->failed = true;
+ }
+ return 0;
+}
+
+/*
+ * Names of 10, 300 and 1000 characters, which take one, two and three
+ * reads, a name with no NUL before the end of the file, and an offset past
+ * the end of the file.
+ */
+static int test__symbol_lazy_names(struct test_suite *test __maybe_unused,
+ int subtest __maybe_unused)
+{
+ static const size_t len[] = { 10, 300, 1000, 0 };
+ bool saved_lazy = symbol_conf.lazy_load_symbols;
+ struct lazy_walk walk = { .nr_ok = 3, };
+ struct lazy_fixture f;
+ struct symbol *sym;
+ int ret = TEST_FAIL;
+ size_t i;
+
+ symbol_conf.lazy_load_symbols = true;
+ if (lazy_fixture__init(&f, len, ARRAY_SIZE(len)))
+ goto out;
+ if (lazy_fixture__add_unreadable(&f, 4))
+ goto out_exit;
+
+ for (i = 0; i < 4; i++) {
+ sym = dso__find_symbol(f.dso, LAZY_SYM_START + i * LAZY_SYM_SIZE + 1);
+ if (!(i < 3 ? lazy_symbol_ok(sym, &f, i) : !sym)) {
+ pr_debug("lookup of name %zu failed\n", i);
+ goto out_exit;
+ }
+ }
+
+ /*
+ * Sorting by name loads every name, and keeps the symbols whose name
+ * cannot be read, named "[unknown]". Lookups still do not return them.
+ */
+ dso__sort_by_name(f.dso);
+ walk.f = &f;
+ dso__for_each_symbol(f.dso, lazy_walk_cb, &walk);
+ if (walk.failed || walk.i != 5)
+ goto out_exit;
+ for (i = 3; i < 5; i++) {
+ if (dso__find_symbol_nocache(f.dso, LAZY_SYM_START + i * LAZY_SYM_SIZE)) {
+ pr_debug("unreadable name %zu was found\n", i);
+ goto out_exit;
+ }
+ }
+ sym = dso__find_symbol_by_name(f.dso, f.names[1], NULL);
+ if (!lazy_symbol_ok(sym, &f, 1))
+ goto out_exit;
+ ret = TEST_OK;
+out_exit:
+ lazy_fixture__exit(&f);
+out:
+ symbol_conf.lazy_load_symbols = saved_lazy;
+ return ret;
+}
+
+/*
+ * A file that cannot be opened for one name, as when perf runs out of file
+ * descriptors, must still give the names read after it can be opened again.
+ */
+static int test__symbol_lazy_open_failed(struct test_suite *test __maybe_unused,
+ int subtest __maybe_unused)
+{
+ static const size_t len[] = { 10, 20, 30 };
+ bool saved_lazy = symbol_conf.lazy_load_symbols;
+ struct rlimit saved_limit, limit;
+ struct lazy_fixture f;
+ struct symbol *sym;
+ int ret = TEST_FAIL;
+ int fd;
+
+ symbol_conf.lazy_load_symbols = true;
+ if (lazy_fixture__init(&f, len, ARRAY_SIZE(len)))
+ goto out;
+ sym = dso__find_symbol_nocache(f.dso, LAZY_SYM_START);
+ if (!lazy_symbol_ok(sym, &f, 0) || getrlimit(RLIMIT_NOFILE, &saved_limit))
+ goto out_exit;
+ /* Lower the limit to the lowest free file descriptor, so open() fails. */
+ fd = open("/dev/null", O_RDONLY);
+ if (fd < 0)
+ goto out_exit;
+ close(fd);
+ limit = saved_limit;
+ limit.rlim_cur = fd;
+ if (setrlimit(RLIMIT_NOFILE, &limit))
+ goto out_exit;
+ sym = dso__find_symbol_nocache(f.dso, LAZY_SYM_START + LAZY_SYM_SIZE);
+ if (setrlimit(RLIMIT_NOFILE, &saved_limit))
+ goto out_exit;
+ if (sym) {
+ pr_debug("name 1 read while no file could be opened: %s\n", sym->name);
+ goto out_exit;
+ }
+ sym = dso__find_symbol_nocache(f.dso, LAZY_SYM_START + 2 * LAZY_SYM_SIZE);
+ if (lazy_symbol_ok(sym, &f, 2))
+ ret = TEST_OK;
+ else
+ pr_debug("name 2 not read after the file could be opened again\n");
+out_exit:
+ lazy_fixture__exit(&f);
+out:
+ symbol_conf.lazy_load_symbols = saved_lazy;
+ return ret;
+}
+
+#define NR_RACE_SYMS 256
+#define NR_RACE_UNREADABLE 16
+#define NR_RACE_ALL (NR_RACE_SYMS + NR_RACE_UNREADABLE)
+#define NR_RACE_THREADS 4
+
+struct lazy_race {
+ struct lazy_fixture *f;
+ int id;
+ bool failed;
+};
+
+/*
+ * Look the symbols up in a different order in each thread. The symbols
+ * after the readable ones have names that cannot be read.
+ */
+static void *lazy_race_lookups(void *data)
+{
+ struct lazy_race *race = data;
+ struct lazy_fixture *f = race->f;
+ size_t i;
+
+ if (race->id == NR_RACE_THREADS - 1) {
+ struct lazy_walk walk = { .f = f, .nr_ok = f->nr, };
+
+ dso__for_each_symbol(f->dso, lazy_walk_cb, &walk);
+ race->failed = walk.failed;
+ return NULL;
+ }
+ for (i = 0; i < NR_RACE_ALL; i++) {
+ size_t k = (i * (2 * race->id + 1) + race->id * 17) % NR_RACE_ALL;
+ struct symbol *sym;
+
+ sym = dso__find_symbol_nocache(f->dso, LAZY_SYM_START + k * LAZY_SYM_SIZE);
+ if (!(k < f->nr ? lazy_symbol_ok(sym, f, k) : !sym))
+ race->failed = true;
+ }
+ return NULL;
+}
+
+static int test__symbol_lazy_race(struct test_suite *test __maybe_unused,
+ int subtest __maybe_unused)
+{
+ bool saved_lazy = symbol_conf.lazy_load_symbols;
+ bool saved_singlethreaded = perf_singlethreaded;
+ struct lazy_race race[NR_RACE_THREADS];
+ pthread_t threads[NR_RACE_THREADS];
+ size_t len[NR_RACE_SYMS];
+ struct lazy_fixture f;
+ int ret = TEST_FAIL;
+ int i, nr_threads = 0;
+
+ for (i = 0; i < NR_RACE_SYMS; i++)
+ len[i] = 8 + i % 300;
+ symbol_conf.lazy_load_symbols = true;
+ if (lazy_fixture__init(&f, len, NR_RACE_SYMS))
+ goto out;
+ for (i = NR_RACE_SYMS; i < NR_RACE_ALL; i++) {
+ if (lazy_fixture__add_unreadable(&f, i))
+ goto out_exit;
+ }
+
+ /* Without this, the rwsems of the symbol array are not taken. */
+ perf_set_multithreaded();
+ for (i = 0; i < NR_RACE_THREADS; i++) {
+ race[i] = (struct lazy_race){ .f = &f, .id = i };
+ if (pthread_create(&threads[i], NULL, lazy_race_lookups, &race[i]))
+ break;
+ nr_threads++;
+ }
+ for (i = 0; i < nr_threads; i++)
+ pthread_join(threads[i], NULL);
+ if (saved_singlethreaded)
+ perf_set_singlethreaded();
+ if (nr_threads != NR_RACE_THREADS)
+ goto out_exit;
+ for (i = 0; i < NR_RACE_THREADS; i++) {
+ if (race[i].failed) {
+ pr_debug("thread %d failed\n", i);
+ goto out_exit;
+ }
+ }
+ if (lazy_nr_symbols(f.dso) != NR_RACE_ALL)
+ goto out_exit;
+ ret = TEST_OK;
+out_exit:
+ lazy_fixture__exit(&f);
+out:
+ symbol_conf.lazy_load_symbols = saved_lazy;
+ return ret;
+}
+
+struct sym_entry {
+ u64 start;
+ u64 end;
+ char *name;
+};
+
+static int cmp_sym_entry(const void *a, const void *b)
+{
+ const struct sym_entry *sa = a, *sb = b;
+
+ if (sa->start != sb->start)
+ return sa->start < sb->start ? -1 : 1;
+ if (sa->end != sb->end)
+ return sa->end < sb->end ? -1 : 1;
+ return strcmp(sa->name, sb->name);
+}
+
+static int check_lazy_lookups(struct map *map, struct map *eager_map,
+ const struct sym_entry *eager, size_t nr_eager)
+{
+ size_t i;
+ int k;
+
+ for (i = 0; i < nr_eager; i++) {
+ if (eager[i].end <= eager[i].start)
+ continue;
+ for (k = 0; k < 2; k++) {
+ u64 addr = k ? eager[i].end - 1 : eager[i].start;
+ struct symbol *want = map__find_symbol(eager_map, addr);
+ struct symbol *sym = map__find_symbol(map, addr);
+
+ if (!want != !sym ||
+ (sym && (sym->start != want->start || sym->end != want->end ||
+ strcmp(sym->name, want->name)))) {
+ pr_debug("lookup %#" PRIx64 ": eager %s, lazy %s\n", addr,
+ want ? want->name : "[none]",
+ sym ? sym->name : "[none]");
+ return TEST_FAIL;
+ }
+ }
+ }
+ return TEST_OK;
+}
+
+static void free_sym_entries(struct sym_entry *entries, size_t nr)
+{
+ size_t i;
+
+ for (i = 0; i < nr; i++)
+ free(entries[i].name);
+ free(entries);
+}
+
+struct sym_loader {
+ struct perf_env env;
+ struct machine *machine;
+ struct thread *thread;
+ struct map *map;
+};
+
+static void sym_loader__exit(struct sym_loader *l)
+{
+ map__put(l->map);
+ thread__put(l->thread);
+ machine__delete(l->machine);
+ perf_env__exit(&l->env);
+}
+
+static int sym_loader__init(struct sym_loader *l, const char *filename, bool lazy)
+{
+ struct dso *dso = NULL;
+ bool kernel;
+
+ memset(l, 0, sizeof(*l));
+ perf_env__init(&l->env);
+ symbol_conf.lazy_load_symbols = lazy;
+ l->machine = machine__new_host(&l->env);
+ if (l->machine)
+ l->thread = machine__findnew_thread(l->machine, getpid(), getpid());
+ if (l->thread)
+ dso = machine__findnew_dso(l->machine, filename);
+ /* map__new() cannot map a loaded kernel module, which has no lazy names. */
+ kernel = dso && dso__kernel(dso) != DSO_SPACE__USER;
+ dso__put(dso);
+ if (kernel) {
+ pr_debug("%s: kernel DSO\n", filename);
+ sym_loader__exit(l);
+ return TEST_SKIP;
+ }
+ if (l->thread)
+ l->map = map__new(l->machine, 0x100000, 0xffffffff, 0, &dso_id_empty,
+ PROT_EXEC, /*flags=*/0, (char *)filename, l->thread);
+ if (!l->map) {
+ sym_loader__exit(l);
+ return TEST_FAIL;
+ }
+ if (dso__load(map__dso(l->map), l->map) <= 0) {
+ pr_debug("%s: no symbols loaded\n", filename);
+ sym_loader__exit(l);
+ return TEST_SKIP;
+ }
+ return TEST_OK;
+}
+
+struct sym_entries {
+ struct sym_entry *entries;
+ size_t nr, alloc;
+};
+
+static int add_sym_entry_cb(struct symbol *pos, void *data)
+{
+ struct sym_entries *e = data;
+
+ if (e->nr == e->alloc) {
+ size_t alloc = e->alloc ? e->alloc * 2 : 1024;
+ struct sym_entry *tmp = realloc(e->entries, alloc * sizeof(*tmp));
+
+ if (!tmp)
+ return -1;
+ e->entries = tmp;
+ e->alloc = alloc;
+ }
+ e->entries[e->nr].start = pos->start;
+ e->entries[e->nr].end = pos->end;
+ e->entries[e->nr].name = strdup(pos->name);
+ if (!e->entries[e->nr].name)
+ return -1;
+ e->nr++;
+ return 0;
+}
+
+static int get_sym_entries(struct dso *dso, struct sym_entry **entries_p, size_t *nr_p)
+{
+ struct sym_entries e = { NULL, 0, 0 };
+
+ if (dso__for_each_symbol(dso, add_sym_entry_cb, &e)) {
+ free_sym_entries(e.entries, e.nr);
+ return -1;
+ }
+ qsort(e.entries, e.nr, sizeof(*e.entries), cmp_sym_entry);
+ *entries_p = e.entries;
+ *nr_p = e.nr;
+ return 0;
+}
+
+/*
+ * Compare the symbols of a DSO (perf itself, or --dso) loaded eagerly and
+ * lazily. Lazy address lookups must agree with eager ones, and once every
+ * name is loaded, the ranges and names must match.
+ */
+static int test__symbol_lazy_parity(struct test_suite *test __maybe_unused,
+ int subtest __maybe_unused)
+{
+ bool saved_lazy = symbol_conf.lazy_load_symbols;
+ struct sym_entry *eager = NULL, *lazy = NULL;
+ size_t nr_eager = 0, nr_lazy = 0, nr_names = 0, i;
+ struct sym_loader eager_l, lazy_l;
+ char filename[PATH_MAX];
+ int ret;
+
+ if (dso_to_test)
+ strlcpy(filename, dso_to_test, sizeof(filename));
+ else
+ perf_exe(filename, sizeof(filename));
+
+ ret = sym_loader__init(&eager_l, filename, false);
+ if (ret != TEST_OK)
+ goto out;
+ ret = sym_loader__init(&lazy_l, filename, true);
+ if (ret != TEST_OK) {
+ pr_debug("%s: loaded eagerly but not lazily\n", filename);
+ ret = TEST_FAIL;
+ goto out_eager;
+ }
+
+ ret = TEST_FAIL;
+ symbols__visit(dso__symbols(map__dso(lazy_l.map)), lazy_count_names_cb, &nr_names);
+ if (nr_names)
+ pr_debug("%s: %zu symbols with lazy names\n", filename, nr_names);
+ else
+ pr_debug("%s: no lazy names\n", filename);
+
+ /* Look up before and after dso__for_each_symbol() loads every name. */
+ if (get_sym_entries(map__dso(eager_l.map), &eager, &nr_eager) ||
+ check_lazy_lookups(lazy_l.map, eager_l.map, eager, nr_eager) != TEST_OK ||
+ get_sym_entries(map__dso(lazy_l.map), &lazy, &nr_lazy) ||
+ check_lazy_lookups(lazy_l.map, eager_l.map, eager, nr_eager) != TEST_OK)
+ goto out_lazy;
+
+ pr_debug("%s: %zu eager and %zu lazy symbols\n", filename, nr_eager, nr_lazy);
+ for (i = 0; i < nr_eager && i < nr_lazy; i++) {
+ if (cmp_sym_entry(&eager[i], &lazy[i])) {
+ pr_debug("mismatch: eager %#" PRIx64 "-%#" PRIx64 " %s, lazy %#"
+ PRIx64 "-%#" PRIx64 " %s\n",
+ eager[i].start, eager[i].end, eager[i].name,
+ lazy[i].start, lazy[i].end, lazy[i].name);
+ goto out_lazy;
+ }
+ }
+ if (nr_eager == nr_lazy)
+ ret = TEST_OK;
+out_lazy:
+ sym_loader__exit(&lazy_l);
+out_eager:
+ sym_loader__exit(&eager_l);
+out:
+ if (eager)
+ free_sym_entries(eager, nr_eager);
+ if (lazy)
+ free_sym_entries(lazy, nr_lazy);
+ symbol_conf.lazy_load_symbols = saved_lazy;
+ return ret;
+}
+
+#endif
+
+static struct test_case tests__symbol_lazy[] = {
+ TEST_CASE("Duplicate selection without names", symbol_lazy_choice),
+#ifdef HAVE_LIBELF_SUPPORT
+ TEST_CASE("Lazy symbol names", symbol_lazy_names),
+ TEST_CASE("Lazy symbol names loaded concurrently", symbol_lazy_race),
+ TEST_CASE("Lazy and eager symbol parity", symbol_lazy_parity),
+ TEST_CASE("Lazy symbol names after a failed open", symbol_lazy_open_failed),
+#endif
+ { .name = NULL, }
+};
+
+struct test_suite suite__symbol_lazy = {
+ .desc = "Lazy symbol loading",
+ .test_cases = tests__symbol_lazy,
+};
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index af822885890d..d9e0b5ad92a2 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -199,6 +199,7 @@ DECLARE_SUITE(sigtrap);
DECLARE_SUITE(event_groups);
DECLARE_SUITE(hybrid_merge);
DECLARE_SUITE(symbols);
+DECLARE_SUITE(symbol_lazy);
DECLARE_SUITE(util);
DECLARE_SUITE(uncore_event_sorting);
DECLARE_SUITE(subcmd_help);
--
Git-157)
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v9 7/7] perf test: Add lazy symbol loading shell tests
2026-11-24 19:28 [PATCH v9 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
` (5 preceding siblings ...)
2026-11-24 19:28 ` [PATCH v9 6/7] perf test: Add lazy symbol loading unit tests Alireza Haghdoost via B4 Relay
@ 2026-11-24 19:28 ` Alireza Haghdoost via B4 Relay
6 siblings, 0 replies; 8+ messages in thread
From: Alireza Haghdoost via B4 Relay @ 2026-11-24 19:28 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: linux-perf-users, linux-kernel, Alireza Haghdoost
From: Alireza Haghdoost <haghdoost@uber.com>
lazy_load_symbols_parity.sh runs the lazy and eager parity unit test on
seven small binaries built for the loader's corner cases: split
debuginfo whose function section is NOBITS in the debug file, a shared
library with .dynsym-only symbols and aliases, a stripped library with
.gnu_debugdata, and on x86_64 an IFUNC behind an IRELATIVE PLT slot, the
same with the IFUNC and its aliases exported, an exported function
followed in .symtab by a zero-size label at its address, and exported
functions with nested entry points.
lazy_load_symbols.sh records a small callchain profile and checks that
perf script and perf report, with and without call graphs, print the
same output with and without --lazy-load-symbols. As root, it also
compares perf script output for a process in another mount namespace,
with the binary added to the build-id cache.
Both tests skip a case, rather than fail it, when the toolchain produced
a file whose names perf loads eagerly anyway (PPC64 ELFv1 .opd,
.gnu_debugdata or a compressed .strtab).
Signed-off-by: Alireza Haghdoost <haghdoost@uber.com>
---
tools/perf/tests/shell/lazy_load_symbols.sh | 243 +++++++++++
tools/perf/tests/shell/lazy_load_symbols_parity.sh | 462 +++++++++++++++++++++
tools/perf/tests/shell/lib/lazy_names.sh | 20 +
3 files changed, 725 insertions(+)
diff --git a/tools/perf/tests/shell/lazy_load_symbols.sh b/tools/perf/tests/shell/lazy_load_symbols.sh
new file mode 100755
index 000000000000..19f0b3c0e9f1
--- /dev/null
+++ b/tools/perf/tests/shell/lazy_load_symbols.sh
@@ -0,0 +1,243 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Lazy symbol loading tests (exclusive)
+#
+# Verifies that perf script and perf report print the same output with
+# --lazy-load-symbols as with the default eager loader.
+
+set -e
+
+shelldir=$(dirname "$0")
+# shellcheck source=lib/perf_has_symbol.sh
+. "${shelldir}"/lib/perf_has_symbol.sh
+# shellcheck source=lib/lazy_names.sh
+. "${shelldir}"/lib/lazy_names.sh
+
+testsym="test_loop"
+# Printed with -vv for each symbol table loaded with lazy names.
+lazy_names=': [1-9][0-9]* \.(symtab|dynsym) symbols with lazy names$'
+
+skip_test_missing_symbol ${testsym}
+
+if ! perf check feature -q libelf
+then
+ echo "Lazy symbol loading [Skipped no libelf support]"
+ exit 2
+fi
+
+perf_path=$(readlink -f "$(command -v perf)")
+err=0
+passed=0
+temp_dir=$(mktemp -d /tmp/__perf_test.lazy_load.XXXXX)
+perfdata="${temp_dir}/perf.data"
+eager_out="${temp_dir}/eager.out"
+lazy_out="${temp_dir}/lazy.out"
+lazy_err="${temp_dir}/lazy.err"
+ns_pid=
+
+cleanup() {
+ if [ -n "${ns_pid}" ]; then
+ kill "${ns_pid}" 2> /dev/null || true
+ wait "${ns_pid}" 2> /dev/null || true
+ fi
+ rm -rf "${temp_dir}"
+ trap - EXIT TERM INT
+}
+
+trap_cleanup() {
+ echo "Unexpected signal in ${FUNCNAME[1]}"
+ cleanup
+ exit 1
+}
+trap trap_cleanup EXIT TERM INT
+
+# check_lazy_names CASE
+# Return whether perf was loaded with lazy names. If not, report CASE as
+# skipped when perf's symbol table cannot have them, and as failed otherwise.
+check_lazy_names() {
+ if grep -F "${perf_path}: " "${lazy_err}" | grep -Eq "${lazy_names}"
+ then
+ return 0
+ fi
+ if ! command -v readelf > /dev/null
+ then
+ echo "$1 [Skipped no readelf to check perf's symbol table]"
+ elif lazy_names_disabled "${perf_path}"
+ then
+ echo "$1 [Skipped perf loads its symbol names eagerly]"
+ else
+ echo "$1 [Failed perf has no lazy names]"
+ err=1
+ fi
+ return 1
+}
+
+test_lazy_load_identical() {
+ echo "Lazy-load output matches eager loader"
+
+ if ! perf record -o "${perfdata}" -g -- perf test -w thloop 2> /dev/null
+ then
+ echo "Lazy-load identical [Skipped record not supported]"
+ return 0
+ fi
+
+ if ! perf script -vv -i "${perfdata}" 2> /dev/null > "${eager_out}" || \
+ ! perf script -vv --lazy-load-symbols -i "${perfdata}" \
+ 2> "${lazy_err}" > "${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. Without samples in perf,
+ # perf is not loaded and has no lazy names either.
+ if ! grep -q "${testsym}" "${eager_out}"
+ then
+ echo "Lazy-load identical [Skipped no ${testsym} resolved]"
+ return 0
+ fi
+ if ! cmp -s "${eager_out}" "${lazy_out}"
+ then
+ echo "Lazy-load identical [Failed output differs from eager]"
+ err=1
+ return
+ fi
+ if ! check_lazy_names "Lazy-load identical"
+ then
+ return
+ fi
+ echo "Lazy-load identical [Success]"
+ passed=$((passed + 1))
+}
+
+test_lazy_load_report() {
+ local eager_report="${temp_dir}/eager.report"
+ local lazy_report="${temp_dir}/lazy.report"
+ local mode
+
+ echo "Lazy-load perf report output matches eager loader"
+
+ if ! grep -qs "${testsym}" "${eager_out}"
+ then
+ echo "Lazy-load report [Skipped no ${testsym} resolved]"
+ return 0
+ fi
+ for mode in "--no-children -g none" "--children"
+ do
+ # shellcheck disable=SC2086
+ if ! perf report -vv --stdio ${mode} -i "${perfdata}" \
+ 2> /dev/null > "${eager_report}" ||
+ ! perf report -vv --stdio ${mode} --lazy-load-symbols \
+ -i "${perfdata}" 2> "${lazy_err}" > "${lazy_report}"
+ then
+ echo "Lazy-load report [Failed perf report ${mode} error]"
+ err=1
+ return
+ fi
+ # -q would turn off the debug output, so drop the header,
+ # which has a random tip.
+ if ! cmp -s <(grep -v '^#' "${eager_report}") \
+ <(grep -v '^#' "${lazy_report}")
+ then
+ echo "Lazy-load report [Failed ${mode} output differs from eager]"
+ err=1
+ return
+ fi
+ if ! check_lazy_names "Lazy-load report"
+ then
+ return
+ fi
+ done
+ echo "Lazy-load report [Success]"
+ passed=$((passed + 1))
+}
+
+# perf script output for a process in another mount namespace, with the
+# binary in the build-id cache, must not change with lazy names.
+test_lazy_load_mount_ns() {
+ echo "Lazy-load parity for a process in another mount namespace"
+
+ if [ "$(id -u)" != 0 ] || ! command -v unshare > /dev/null
+ then
+ echo "Lazy-load mount namespace [Skipped needs root and unshare]"
+ return 0
+ fi
+
+ local buildid_dir="${temp_dir}/buildid"
+ local nsdata="${temp_dir}/ns.data"
+
+ # The workload sees an empty build-id cache; perf sees the real one.
+ mkdir -p "${buildid_dir}"
+ unshare -m --propagation private sh -c \
+ "mount -t tmpfs none '${buildid_dir}' && exec perf test -w thloop 30" &
+ ns_pid=$!
+ for _ in $(seq 50)
+ do
+ if [ "$(readlink "/proc/${ns_pid}/exe" 2> /dev/null)" = "${perf_path}" ]
+ then
+ break
+ fi
+ sleep 0.1
+ done
+ if [ "$(readlink "/proc/${ns_pid}/exe" 2> /dev/null)" != "${perf_path}" ]
+ then
+ echo "Lazy-load mount namespace [Skipped cannot start workload]"
+ return 0
+ fi
+
+ if ! perf record -N -o "${nsdata}" -p "${ns_pid}" -- sleep 1 2> /dev/null ||
+ ! perf --buildid-dir "${buildid_dir}" buildid-cache -a "${perf_path}" \
+ 2> /dev/null ||
+ [ -z "$(ls -A "${buildid_dir}/.build-id" 2> /dev/null)" ]
+ then
+ echo "Lazy-load mount namespace [Skipped record or build-id cache failed]"
+ return 0
+ fi
+ if ! perf --buildid-dir "${buildid_dir}" script -vv -i "${nsdata}" \
+ 2> /dev/null > "${eager_out}" ||
+ ! perf --buildid-dir "${buildid_dir}" script -vv --lazy-load-symbols \
+ -i "${nsdata}" 2> "${lazy_err}" > "${lazy_out}"
+ then
+ echo "Lazy-load mount namespace [Failed perf script error]"
+ err=1
+ return
+ fi
+
+ if ! grep -q "${testsym}" "${eager_out}"
+ then
+ echo "Lazy-load mount namespace [Skipped no ${testsym} resolved]"
+ return 0
+ fi
+ if ! cmp -s "${eager_out}" "${lazy_out}"
+ then
+ echo "Lazy-load mount namespace [Failed output differs from eager]"
+ err=1
+ return
+ fi
+ # For a process in another mount namespace, only a symbol table from
+ # the build-id cache (origin B or D in perf report -v) has lazy names.
+ if ! perf --buildid-dir "${buildid_dir}" report -v --stdio --sort sym \
+ -i "${nsdata}" 2> /dev/null | grep -Eq " [BD] \[\.\] ${testsym}( |$)"
+ then
+ echo "Lazy-load mount namespace [Skipped perf symbols not from the build-id cache]"
+ return 0
+ fi
+ if ! check_lazy_names "Lazy-load mount namespace"
+ then
+ return
+ fi
+ echo "Lazy-load mount namespace [Success]"
+ passed=$((passed + 1))
+}
+
+test_lazy_load_identical
+test_lazy_load_report
+test_lazy_load_mount_ns
+
+cleanup
+if [ "${err}" -eq 0 ] && [ "${passed}" -eq 0 ]
+then
+ err=2
+fi
+exit $err
diff --git a/tools/perf/tests/shell/lazy_load_symbols_parity.sh b/tools/perf/tests/shell/lazy_load_symbols_parity.sh
new file mode 100755
index 000000000000..a55a73560db2
--- /dev/null
+++ b/tools/perf/tests/shell/lazy_load_symbols_parity.sh
@@ -0,0 +1,462 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Lazy symbol loading parity with eager loading
+
+# Build small ELF files whose symbols are hard to get right without their
+# names, and check that lazy and eager loading agree on their address lookups,
+# ranges and names:
+# - split debuginfo, where a function's section is NOBITS (with a different
+# sh_offset) in the debug file and has no PT_LOAD in the runtime ELF;
+# - a shared library with symbols that are only in .dynsym, one of them a
+# longer-named alias of a .symtab symbol;
+# - a stripped shared library whose local symbols are in .gnu_debugdata,
+# one of them an alias of a .dynsym symbol, which must load eagerly;
+# - on x86_64, an IRELATIVE PLT slot whose IFUNC loses duplicate selection
+# to an alias that loses to another one in turn;
+# - on x86_64, an exported function followed in .symtab by a zero-size
+# label at its address;
+# - on x86_64, a shared library of exported functions with nested entry
+# points, where lazy and eager lookups past the end of each inner one
+# must agree;
+# - on x86_64, an IRELATIVE PLT slot whose IFUNC and its aliases are
+# exported, so that the .dynsym copy of the IFUNC passes on its mark.
+
+set -e
+export LC_ALL=C
+
+shelldir=$(dirname "$0")
+# shellcheck source=lib/lazy_names.sh
+. "${shelldir}"/lib/lazy_names.sh
+
+err=0
+passed=0
+tmpdir=$(mktemp -d /tmp/__perf_test.lazy_parity.XXXXX)
+
+cleanup() {
+ rm -rf "${tmpdir}"
+ trap - EXIT TERM INT
+}
+
+trap_cleanup() {
+ echo "Unexpected signal in ${FUNCNAME[1]}"
+ cleanup
+ exit 1
+}
+trap trap_cleanup EXIT TERM INT
+
+skip() {
+ echo "Lazy-load parity [Skipped: $1]"
+ cleanup
+ exit 2
+}
+
+if ! perf check feature -q libelf; then
+ skip "no libelf support"
+fi
+
+for tool in cc objcopy strip readelf nm dd; do
+ if ! command -v "${tool}" > /dev/null; then
+ skip "${tool} not found"
+ fi
+done
+
+# check_parity NAME FILE [eager]
+# Lazy loading must leave names of FILE unread, or none if "eager" is given.
+check_parity() {
+ local out="${tmpdir}/parity.out"
+ local lazy="[0-9][0-9]* symbols with lazy names"
+
+ if [ "$3" = eager ]; then
+ lazy="no lazy names"
+ fi
+ perf test --dso "$2" -vv "Lazy and eager symbol parity" > "${out}" 2>&1 || true
+ if grep -q ': Ok$' "${out}" && grep -q "^$2: ${lazy}$" "${out}"; then
+ echo "Lazy-load parity $1 [Success]"
+ passed=$((passed + 1))
+ return
+ fi
+ if [ "$3" != eager ] && grep -q ': Ok$' "${out}" &&
+ grep -q "^$2: no lazy names$" "${out}" && lazy_names_disabled "$2" "$2.debug"; then
+ echo "Lazy-load parity $1 [Skipped: names load eagerly on this toolchain]"
+ return
+ fi
+ grep -E 'symbols$|names$|mismatch|lookup|no symbols' "${out}" || true
+ echo "Lazy-load parity $1 [Failed]"
+ err=1
+}
+
+test_split_debug() {
+ local prog="${tmpdir}/split"
+
+ cat > "${prog}.c" << EOF
+__attribute__((section("splittext"), noinline, used))
+int split_func(int x)
+{
+ return x * 3 + 1;
+}
+
+int main(int argc, char **argv)
+{
+ (void)argv;
+ return split_func(argc);
+}
+EOF
+ if ! cc -O1 -g -o "${prog}" "${prog}.c" \
+ -Wl,--section-start=splittext=0x800000 2> /dev/null; then
+ echo "Lazy-load parity split debuginfo [Skipped: cannot build]"
+ return
+ fi
+ if ! objcopy --only-keep-debug "${prog}" "${prog}.debug" ||
+ ! strip -s "${prog}" ||
+ ! objcopy --add-gnu-debuglink="${prog}.debug" "${prog}"; then
+ echo "Lazy-load parity split debuginfo [Skipped: cannot split]"
+ return
+ fi
+
+ # The debug file must keep splittext as NOBITS at a stale offset.
+ local debug_sec run_sec debug_off run_off
+ debug_sec=$(readelf -SW "${prog}.debug" 2> /dev/null | grep ' splittext ' || true)
+ run_sec=$(readelf -SW "${prog}" | grep ' splittext ' || true)
+ if ! echo "${debug_sec}" | grep -q NOBITS; then
+ echo "Lazy-load parity split debuginfo [Skipped: splittext is not NOBITS]"
+ return
+ fi
+ debug_off=$(echo "${debug_sec}" | sed 's/.*splittext *//' | awk '{print $3}')
+ run_off=$(echo "${run_sec}" | sed 's/.*splittext *//' | awk '{print $3}')
+ if [ -z "${run_off}" ] || [ "${debug_off}" = "${run_off}" ]; then
+ echo "Lazy-load parity split debuginfo [Skipped: offsets do not differ]"
+ return
+ fi
+
+ # Drop the PT_LOAD covering splittext so program header lookup fails.
+ local phoff phentsize idx
+ phoff=$(readelf -hW "${prog}" | awk '/Start of program headers/ {print $5}')
+ phentsize=$(readelf -hW "${prog}" | awk '/Size of program headers/ {print $5}')
+ idx=$(readelf -lW "${prog}" 2> /dev/null | awk '
+ /^Program Headers:/ { in_ph = 1; next }
+ in_ph && /^ Type/ { next }
+ in_ph && /^ *$/ { exit }
+ in_ph && /^ [A-Z]/ {
+ if ($1 == "LOAD" && $3 ~ /^0x0*800000$/) { print n; exit }
+ n++
+ }')
+ if [ -z "${phoff}" ] || [ -z "${phentsize}" ] || [ -z "${idx}" ]; then
+ echo "Lazy-load parity split debuginfo [Skipped: no PT_LOAD for splittext]"
+ return
+ fi
+ dd if=/dev/zero of="${prog}" bs=1 seek=$((phoff + idx * phentsize)) \
+ count=4 conv=notrunc 2> /dev/null
+ if readelf -lW "${prog}" 2> /dev/null | grep -q 'LOAD .*0x0*800000 '; then
+ echo "Lazy-load parity split debuginfo [Failed to drop PT_LOAD]"
+ err=1
+ return
+ fi
+ check_parity "split debuginfo" "${prog}"
+}
+
+test_dynsym_only() {
+ local lib="${tmpdir}/libdyn.so"
+
+ cat > "${tmpdir}/libdyn.c" << EOF
+int dyn_only(int x)
+{
+ return x + 1;
+}
+
+int dyn_and_symtab(int x)
+{
+ return x * 2;
+}
+
+/* Same range as dyn_and_symtab; duplicate selection prefers the longer name. */
+int dyn_and_symtab_alias(int x) __attribute__((alias("dyn_and_symtab")));
+EOF
+ if ! cc -O1 -shared -fPIC -o "${lib}" "${tmpdir}/libdyn.c" 2> /dev/null; then
+ echo "Lazy-load parity .dynsym-only symbol [Skipped: cannot build]"
+ return
+ fi
+ # --strip-symbol leaves .dynsym untouched.
+ if ! objcopy --strip-symbol=dyn_only --strip-symbol=dyn_and_symtab_alias \
+ "${lib}"; then
+ echo "Lazy-load parity .dynsym-only symbol [Skipped: cannot strip]"
+ return
+ fi
+ for sym in dyn_only dyn_and_symtab_alias; do
+ if readelf -sW "${lib}" | \
+ awk '/Symbol table .\.symtab/ { s = 1 } s' | \
+ grep -qw "${sym}" || \
+ ! readelf --dyn-syms -W "${lib}" | grep -qw "${sym}"; then
+ echo "Lazy-load parity .dynsym-only symbol [Skipped: cannot strip]"
+ return
+ fi
+ done
+ check_parity ".dynsym-only symbol" "${lib}"
+}
+
+test_gnu_debugdata() {
+ local lib="${tmpdir}/libgdd.so"
+ local dir="${tmpdir}/gdd"
+
+ if ! perf check feature -q lzma || ! command -v xz > /dev/null; then
+ echo "Lazy-load parity .gnu_debugdata [Skipped: no lzma support or xz]"
+ return
+ fi
+ mkdir "${dir}"
+ cat > "${dir}/libgdd.c" << EOF
+int exported(int x)
+{
+ return x * 3 + 1;
+}
+
+/* Same range as exported; duplicate selection prefers the global symbol. */
+static int local_alias(int x) __attribute__((alias("exported"), used));
+
+__attribute__((noinline)) static int local_helper(int x)
+{
+ return x + 7;
+}
+
+int call_local(int x)
+{
+ return local_helper(x) * 2;
+}
+EOF
+ if ! cc -O1 -shared -fPIC -o "${lib}" "${dir}/libgdd.c" 2> /dev/null; then
+ echo "Lazy-load parity .gnu_debugdata [Skipped: cannot build]"
+ return
+ fi
+ # Build MiniDebugInfo the way distributions do: the function symbols
+ # that are not in .dynsym, xz-compressed into .gnu_debugdata.
+ nm -D --format=posix --defined-only "${lib}" | awk '{ print $1 }' | \
+ sort > "${dir}/dynsyms"
+ nm --format=posix --defined-only "${lib}" | \
+ awk '$2 == "T" || $2 == "t" { print $1 }' | sort > "${dir}/funcsyms"
+ comm -13 "${dir}/dynsyms" "${dir}/funcsyms" > "${dir}/keep"
+ if ! grep -qx local_alias "${dir}/keep" ||
+ ! objcopy --only-keep-debug "${lib}" "${dir}/debug" ||
+ ! objcopy -S --remove-section .comment \
+ --keep-symbols="${dir}/keep" "${dir}/debug" "${dir}/mini" ||
+ ! xz "${dir}/mini" || ! strip --strip-all "${lib}" ||
+ ! objcopy --add-section .gnu_debugdata="${dir}/mini.xz" "${lib}" ||
+ readelf -SW "${lib}" | grep -qw '\.symtab'; then
+ echo "Lazy-load parity .gnu_debugdata [Skipped: cannot build MiniDebugInfo]"
+ return
+ fi
+ check_parity ".gnu_debugdata" "${lib}" eager
+}
+
+test_ifunc_alias() {
+ local prog="${tmpdir}/ifunc"
+ local order
+
+ if [ "$(uname -m)" != x86_64 ]; then
+ echo "Lazy-load parity IFUNC alias [Skipped: IRELATIVE naming is x86_64 only]"
+ return
+ fi
+ # Duplicate selection prefers fewer leading underscores, so at the
+ # resolver's address __lazy_ifunc loses to _lazy_ifunc_x, which loses
+ # to lazy_ifunc_y. Whether the PLT slot is named after lazy_ifunc_y
+ # depends on how that passes on the IFUNC mark.
+ cat > "${prog}.c" << EOF
+static int impl(void)
+{
+ return 42;
+}
+
+__attribute__((used, noinline)) static void *___lazy_ifunc_resolver(void)
+{
+ return (void *)impl;
+}
+
+asm(".type __lazy_ifunc, @gnu_indirect_function\n"
+ ".set __lazy_ifunc, ___lazy_ifunc_resolver\n"
+ ".size __lazy_ifunc, 1\n"
+ ".type _lazy_ifunc_x, @function\n"
+ ".set _lazy_ifunc_x, ___lazy_ifunc_resolver\n"
+ ".size _lazy_ifunc_x, 1\n"
+ ".type lazy_ifunc_y, @function\n"
+ ".set lazy_ifunc_y, ___lazy_ifunc_resolver\n"
+ ".size lazy_ifunc_y, 1\n");
+
+int __lazy_ifunc(void);
+
+int main(void)
+{
+ return __lazy_ifunc();
+}
+EOF
+ if ! cc -O1 -fno-pie -no-pie -o "${prog}" "${prog}.c" 2> /dev/null ||
+ ! readelf -rW "${prog}" | grep -q R_X86_64_IRELATIVE; then
+ echo "Lazy-load parity IFUNC alias [Skipped: cannot build]"
+ return
+ fi
+ # The outcome depends on the order of the aliases in .symtab.
+ order=$(readelf -sW "${prog}" | \
+ awk '$8 ~ /^_*lazy_ifunc(_x|_y)?$/ { printf "%s ", $8 }')
+ if [ "${order}" != "__lazy_ifunc _lazy_ifunc_x lazy_ifunc_y " ]; then
+ echo "Lazy-load parity IFUNC alias [Skipped: unexpected symbol order]"
+ return
+ fi
+ check_parity "IFUNC alias" "${prog}"
+}
+
+test_dynsym_copy_zero_size() {
+ local prog="${tmpdir}/zero"
+ local order
+
+ if [ "$(uname -m)" != x86_64 ]; then
+ echo "Lazy-load parity zero-size label [Skipped: x86_64 only]"
+ return
+ fi
+ # If end fixup gave the label a size, its longer name would win
+ # duplicate selection over the function.
+ cat > "${prog}.S" << EOF
+ .text
+ .globl lazy_sized
+ .type lazy_sized, @function
+lazy_sized:
+ .globl lazy_sized_label
+ .type lazy_sized_label, @function
+lazy_sized_label:
+ nop
+ ret
+ .size lazy_sized, .-lazy_sized
+ .p2align 4
+ .globl main
+ .type main, @function
+main:
+ call lazy_sized
+ xor %eax, %eax
+ ret
+ .size main, .-main
+ .section .note.GNU-stack,"",@progbits
+EOF
+ if ! cc -o "${prog}" "${prog}.S" \
+ -Wl,--export-dynamic-symbol=lazy_sized 2> /dev/null ||
+ ! readelf --dyn-syms -W "${prog}" | grep -qw lazy_sized ||
+ readelf --dyn-syms -W "${prog}" | grep -qw lazy_sized_label; then
+ echo "Lazy-load parity zero-size label [Skipped: cannot build]"
+ return
+ fi
+ # The label must follow the function in .symtab.
+ order=$(readelf -sW "${prog}" | \
+ awk '/\.symtab/ { s = 1 } s && $8 ~ /^lazy_sized(_label)?$/ { printf "%s ", $8 }')
+ if [ "${order}" != "lazy_sized lazy_sized_label " ]; then
+ echo "Lazy-load parity zero-size label [Skipped: unexpected symbol order]"
+ return
+ fi
+ check_parity "zero-size label" "${prog}"
+}
+
+test_nested_entry() {
+ local lib="${tmpdir}/libnested.so"
+ local i
+
+ if [ "$(uname -m)" != x86_64 ]; then
+ echo "Lazy-load parity nested entry [Skipped: x86_64 only]"
+ return
+ fi
+ # Lookups past the end of each inner entry point must agree. Every
+ # exported function has its name read for duplicate selection, so the
+ # local function keeps a name unread.
+ {
+ echo " .text"
+ echo " .type lazy_local, @function"
+ echo "lazy_local:"
+ echo " ret"
+ echo " .size lazy_local, .-lazy_local"
+ for i in 0 1 2; do
+ echo " .globl lazy_pad_${i}"
+ echo " .type lazy_pad_${i}, @function"
+ echo "lazy_pad_${i}:"
+ echo " ret"
+ echo " .size lazy_pad_${i}, .-lazy_pad_${i}"
+ done
+ for i in 0 1 2 3 4 5 6; do
+ echo " .globl lazy_outer_${i}, lazy_inner_${i}"
+ echo " .type lazy_outer_${i}, @function"
+ echo " .type lazy_inner_${i}, @function"
+ echo "lazy_outer_${i}:"
+ echo " nop"
+ echo "lazy_inner_${i}:"
+ echo " nop"
+ echo " .size lazy_inner_${i}, .-lazy_inner_${i}"
+ echo " ret"
+ echo " .size lazy_outer_${i}, .-lazy_outer_${i}"
+ done
+ echo ' .section .note.GNU-stack,"",@progbits'
+ } > "${tmpdir}/nested.S"
+ if ! cc -shared -nostdlib -o "${lib}" "${tmpdir}/nested.S" 2> /dev/null ||
+ ! readelf --dyn-syms -W "${lib}" | grep -qw lazy_outer_0; then
+ echo "Lazy-load parity nested entry [Skipped: cannot build]"
+ return
+ fi
+ check_parity "nested entry" "${lib}"
+}
+
+test_exported_ifunc_alias() {
+ local lib="${tmpdir}/libifunc.so"
+
+ if [ "$(uname -m)" != x86_64 ]; then
+ echo "Lazy-load parity exported IFUNC alias [Skipped: x86_64 only]"
+ return
+ fi
+ # At the resolver's address, lazy_exp_ifunc_y wins duplicate selection
+ # over __lazy_exp_ifunc and _lazy_exp_ifunc_x, and an alias that lost
+ # after it took the IFUNC mark does not pass the mark on. The PLT slot
+ # of the local IFUNC __lazy_exp_call is named after lazy_exp_ifunc_y
+ # only if the .dynsym copy of __lazy_exp_ifunc marks it.
+ cat > "${tmpdir}/libifunc.c" << EOF
+static int impl(void)
+{
+ return 42;
+}
+
+__attribute__((used, noinline)) static void *lazy_exp_resolver(void)
+{
+ return (void *)impl;
+}
+
+asm(".globl __lazy_exp_ifunc, _lazy_exp_ifunc_x, lazy_exp_ifunc_y\n"
+ ".type __lazy_exp_ifunc, @gnu_indirect_function\n"
+ ".set __lazy_exp_ifunc, lazy_exp_resolver\n"
+ ".size __lazy_exp_ifunc, 1\n"
+ ".type _lazy_exp_ifunc_x, @function\n"
+ ".set _lazy_exp_ifunc_x, lazy_exp_resolver\n"
+ ".size _lazy_exp_ifunc_x, 1\n"
+ ".type lazy_exp_ifunc_y, @function\n"
+ ".set lazy_exp_ifunc_y, lazy_exp_resolver\n"
+ ".size lazy_exp_ifunc_y, 1\n");
+asm(".local __lazy_exp_call\n"
+ ".type __lazy_exp_call, @gnu_indirect_function\n"
+ ".set __lazy_exp_call, lazy_exp_resolver\n");
+
+int __lazy_exp_call(void);
+
+int lazy_exp_main(void)
+{
+ return __lazy_exp_call();
+}
+EOF
+ if ! cc -O1 -shared -fPIC -o "${lib}" "${tmpdir}/libifunc.c" 2> /dev/null ||
+ ! readelf -rW "${lib}" | grep -q R_X86_64_IRELATIVE ||
+ ! readelf --dyn-syms -W "${lib}" | grep -q 'IFUNC .* __lazy_exp_ifunc$'; then
+ echo "Lazy-load parity exported IFUNC alias [Skipped: cannot build]"
+ return
+ fi
+ check_parity "exported IFUNC alias" "${lib}"
+}
+
+test_split_debug
+test_dynsym_only
+test_gnu_debugdata
+test_ifunc_alias
+test_dynsym_copy_zero_size
+test_nested_entry
+test_exported_ifunc_alias
+
+cleanup
+if [ "${err}" -eq 0 ] && [ "${passed}" -eq 0 ]; then
+ err=2
+fi
+exit ${err}
diff --git a/tools/perf/tests/shell/lib/lazy_names.sh b/tools/perf/tests/shell/lib/lazy_names.sh
new file mode 100644
index 000000000000..0358a039b4d2
--- /dev/null
+++ b/tools/perf/tests/shell/lib/lazy_names.sh
@@ -0,0 +1,20 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+# lazy_names_disabled FILE...
+# Return whether one of FILE has a PPC64 ELFv1 .opd, a .gnu_debugdata symbol
+# table or a compressed .strtab, for which perf loads symbol names eagerly.
+lazy_names_disabled() {
+ local f
+
+ command -v readelf > /dev/null || return 1
+ for f in "$@"; do
+ [ -e "${f}" ] || continue
+ if readelf -SW "${f}" 2> /dev/null |
+ grep -Eq ' \.(opd|gnu_debugdata) | \.strtab +STRTAB( +[0-9a-f]+){4} +[A-Z]*C'
+ then
+ return 0
+ fi
+ done
+ return 1
+}
--
Git-157)
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-24 19:29 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 19:28 [PATCH v9 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 1/7] perf symbols: Split out name-independent duplicate symbol selection Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 2/7] perf symbols: Point to the symbol name instead of embedding it Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 3/7] perf symbols: Add lazy symbol names Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 4/7] perf script: Add --lazy-load-symbols to load symbol names lazily Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 5/7] perf report/top: Add --lazy-load-symbols Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 6/7] perf test: Add lazy symbol loading unit tests Alireza Haghdoost via B4 Relay
2026-11-24 19:28 ` [PATCH v9 7/7] perf test: Add lazy symbol loading shell tests Alireza Haghdoost via B4 Relay
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®