* [PATCH v8 1/7] perf symbols: Split out name-independent duplicate symbol selection
2026-11-21 16:41 [PATCH v8 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
@ 2026-11-21 16:41 ` Alireza Haghdoost via B4 Relay
2026-11-21 16:41 ` [PATCH v8 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-21 16:41 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 a1ac1cd1d2e7..a5ed8a65986b 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 a6a2ab200f98..fc98fab1c8d0 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -295,6 +295,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 v8 2/7] perf symbols: Point to the symbol name instead of embedding it
2026-11-21 16:41 [PATCH v8 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
2026-11-21 16:41 ` [PATCH v8 1/7] perf symbols: Split out name-independent duplicate symbol selection Alireza Haghdoost via B4 Relay
@ 2026-11-21 16:41 ` Alireza Haghdoost via B4 Relay
2026-11-21 16:41 ` [PATCH v8 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-21 16:41 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 rb-tree 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 a5ed8a65986b..ad4bf7685293 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -354,6 +354,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 fc98fab1c8d0..a449c170d834 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;
};
void symbol__delete(struct symbol *sym);
--
Git-157)
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH v8 3/7] perf symbols: Add lazy symbol names
2026-11-21 16:41 [PATCH v8 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
2026-11-21 16:41 ` [PATCH v8 1/7] perf symbols: Split out name-independent duplicate symbol selection Alireza Haghdoost via B4 Relay
2026-11-21 16:41 ` [PATCH v8 2/7] perf symbols: Point to the symbol name instead of embedding it Alireza Haghdoost via B4 Relay
@ 2026-11-21 16:41 ` Alireza Haghdoost via B4 Relay
2026-11-21 16:41 ` [PATCH v8 4/7] perf symbols: Check the file before reading " Alireza Haghdoost via B4 Relay
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Alireza Haghdoost via B4 Relay @ 2026-11-21 16:41 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 DSO lookups load the name of the symbol they return, and code that
walks all symbols of a DSO gets them from dso__named_symbols(), which
loads every name. 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. 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. 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.c | 2 +-
tools/perf/util/dso.h | 3 +-
tools/perf/util/symbol-elf.c | 97 ++++++++++++++++++++++++++++++++-
tools/perf/util/symbol-minimal.c | 6 +++
tools/perf/util/symbol.c | 114 ++++++++++++++++++++++++++++++++++++---
tools/perf/util/symbol.h | 27 +++++++++-
tools/perf/util/symbol_conf.h | 1 +
8 files changed, 237 insertions(+), 15 deletions(-)
diff --git a/tools/perf/ui/browsers/map.c b/tools/perf/ui/browsers/map.c
index be7390156edd..b7ae2ad4126f 100644
--- a/tools/perf/ui/browsers/map.c
+++ b/tools/perf/ui/browsers/map.c
@@ -111,7 +111,7 @@ int map__browse(struct map *map)
{
struct map_browser mb = {
.b = {
- .entries = dso__symbols(map__dso(map)),
+ .entries = dso__named_symbols(map__dso(map)),
.refresh = ui_browser__rb_tree_refresh,
.seek = ui_browser__rb_tree_seek,
.write = map_browser__write,
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index e46cd614eb21..881603d78b4b 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -2021,7 +2021,7 @@ size_t dso__fprintf(struct dso *dso, FILE *fp)
ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
ret += dso__fprintf_buildid(dso, fp);
ret += fprintf(fp, ")\n");
- for (nd = rb_first_cached(dso__symbols(dso)); nd; nd = rb_next(nd)) {
+ for (nd = rb_first_cached(dso__named_symbols(dso)); nd; nd = rb_next(nd)) {
struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
ret += symbol__fprintf(pos, fp);
}
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index d5f7382d4e13..10eac0b31b7d 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -369,7 +369,7 @@ int dso_id__cmp(const struct dso_id *a, const struct dso_id *b);
* @n: the 'struct rb_node *' to use as a temporary storage
*/
#define dso__for_each_symbol(dso, pos, n) \
- symbols__for_each_entry(dso__symbols(dso), pos, n)
+ symbols__for_each_entry(dso__named_symbols(dso), pos, n)
static inline void *dso__a2l(const struct dso *dso)
{
@@ -831,6 +831,7 @@ char *dso__filename_with_chroot(const struct dso *dso, const char *filename);
bool dso__sorted_by_name(const struct dso *dso);
void dso__set_sorted_by_name(struct dso *dso);
void dso__sort_by_name(struct dso *dso);
+struct rb_root_cached *dso__named_symbols(struct dso *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 2b7f9e30916d..82d6a76176d2 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -17,6 +17,7 @@
#include "machine.h"
#include "vdso.h"
#include "debug.h"
+#include "namespaces.h"
#include "util/copyfile.h"
#include <linux/ctype.h>
#include <linux/kernel.h>
@@ -460,7 +461,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 ||
@@ -741,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;
@@ -1628,6 +1629,98 @@ 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 caller holds the
+ * DSO lock.
+ */
+void dso__load_symbol_names(struct dso *dso, struct symbol **syms, size_t nr)
+{
+ enum dso_binary_type type = dso__symtab_type(dso);
+ /* As in dso__load(), the build-id cache is in perf's mount namespace. */
+ bool own_ns = type == DSO_BINARY_TYPE__BUILD_ID_CACHE ||
+ type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO;
+ struct nscookie nsc;
+ bool decomp;
+ char *name;
+ 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;
+ if (!own_ns)
+ nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
+ name = dso__get_filename(dso, "", &decomp, type);
+ if (name)
+ fd = open(name, O_RDONLY | O_CLOEXEC);
+ if (!own_ns)
+ nsinfo__mountns_exit(&nsc);
+
+ 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 (name)
+ dso__put_filename(dso, name, 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 ad4bf7685293..f4dd630aa10f 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -338,9 +338,9 @@ void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms)
curr->end = roundup(curr->start, 4096) + 4096;
}
-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 namelen)
{
- size_t namelen = strlen(name) + 1;
struct symbol *sym = calloc(1, sizeof(*sym) + namelen);
if (sym == NULL)
@@ -349,7 +349,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",
@@ -360,10 +371,39 @@ 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;
+}
+
+/* @sym takes ownership of @name. */
+void symbol__set_loaded_name(struct symbol *sym, char *name)
+{
+ sym->namelen = strlen(name);
+ sym->name = name;
+ atomic_fetch_and(&sym->flags, ~SYMBOL_FLAG_LAZY_NAME);
+}
+
+static const char unknown_name[] = "[unknown]";
+
+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;
+ /* A loaded lazy name is allocated apart from the symbol. */
+ if (!symbol__needs_lazy_name(sym) && sym->name != (char *)(sym + 1) &&
+ sym->name != unknown_name)
+ free(sym->name);
symbol__annotation_delete(sym);
free(sym);
}
@@ -451,7 +491,7 @@ void symbols__insert(struct rb_root_cached *symbols, struct symbol *sym)
__symbols__insert(symbols, sym);
}
-static struct symbol *symbols__find(struct rb_root_cached *symbols, u64 ip)
+struct symbol *symbols__find(struct rb_root_cached *symbols, u64 ip)
{
struct rb_node *n;
@@ -626,6 +666,18 @@ void dso__delete_symbol(struct dso *dso, struct symbol *sym)
dso__reset_find_symbol_cache(dso);
}
+static struct symbol *dso__resolve_symbol_name(struct dso *dso, struct symbol *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)
{
if (dso__last_find_result_addr(dso) != addr || dso__last_find_result_symbol(dso) == NULL) {
@@ -633,22 +685,22 @@ struct symbol *dso__find_symbol(struct dso *dso, u64 addr)
dso__set_last_find_result_symbol(dso, symbols__find(dso__symbols(dso), addr));
}
- return dso__last_find_result_symbol(dso);
+ return dso__resolve_symbol_name(dso, dso__last_find_result_symbol(dso));
}
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__first_symbol(struct dso *dso)
{
- return symbols__first(dso__symbols(dso));
+ return symbols__first(dso__named_symbols(dso));
}
struct symbol *dso__last_symbol(struct dso *dso)
{
- return symbols__last(dso__symbols(dso));
+ return symbols__last(dso__named_symbols(dso));
}
struct symbol *dso__next_symbol(struct symbol *sym)
@@ -680,12 +732,58 @@ struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_
return s;
}
+/*
+ * Do not delete the symbols whose name cannot be read, since address
+ * lookups search the tree without the DSO lock.
+ */
+static void __dso__load_all_symbol_names(struct dso *dso)
+{
+ struct rb_node *nd = rb_first_cached(dso__symbols(dso));
+ struct symbol *batch[512];
+
+ while (nd) {
+ size_t nr = 0;
+
+ for (; nd && nr < ARRAY_SIZE(batch); nd = rb_next(nd)) {
+ struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
+
+ if (symbol__needs_lazy_name(pos))
+ batch[nr++] = pos;
+ }
+ if (nr)
+ dso__load_symbol_names(dso, batch, nr);
+ }
+}
+
+/*
+ * 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);
+}
+
+struct rb_root_cached *dso__named_symbols(struct dso *dso)
+{
+ if (dso__may_have_lazy_names(dso)) {
+ mutex_lock(dso__lock(dso));
+ /* All names are loaded once the DSO is sorted by name. */
+ if (!dso__sorted_by_name(dso))
+ __dso__load_all_symbol_names(dso);
+ mutex_unlock(dso__lock(dso));
+ }
+ return dso__symbols(dso);
+}
+
void dso__sort_by_name(struct dso *dso)
{
mutex_lock(dso__lock(dso));
if (!dso__sorted_by_name(dso)) {
size_t len = 0;
+ if (dso__may_have_lazy_names(dso))
+ __dso__load_all_symbol_names(dso);
dso__set_symbol_names(dso, symbols__sort_by_name(dso__symbols(dso), &len));
if (dso__symbol_names(dso)) {
dso__set_symbol_names_len(dso, len);
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index a449c170d834..d690cb1d021c 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -78,8 +78,15 @@ 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)
/**
* 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 {
struct rb_node rb_node;
@@ -91,13 +98,24 @@ 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;
+ };
};
void symbol__delete(struct symbol *sym);
void symbols__delete(struct rb_root_cached *symbols);
+/* 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) &
@@ -219,6 +237,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,
@@ -249,11 +270,13 @@ 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);
+void dso__load_symbol_names(struct dso *dso, struct symbol **syms, size_t nr);
char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name);
void __symbols__insert(struct rb_root_cached *symbols, struct symbol *sym);
void symbols__insert(struct rb_root_cached *symbols, struct symbol *sym);
+struct symbol *symbols__find(struct rb_root_cached *symbols, u64 ip);
void symbols__fixup_duplicate(struct rb_root_cached *symbols);
void symbols__fixup_end(struct rb_root_cached *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 v8 4/7] perf symbols: Check the file before reading lazy symbol names
2026-11-21 16:41 [PATCH v8 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
` (2 preceding siblings ...)
2026-11-21 16:41 ` [PATCH v8 3/7] perf symbols: Add lazy symbol names Alireza Haghdoost via B4 Relay
@ 2026-11-21 16:41 ` Alireza Haghdoost via B4 Relay
2026-11-21 16:41 ` [PATCH v8 5/7] perf script: Add --lazy-load-symbols to load symbol names lazily Alireza Haghdoost via B4 Relay
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Alireza Haghdoost via B4 Relay @ 2026-11-21 16:41 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 makes perf check that a file is still the one it loaded
before it reads lazy symbol names from it. A lazy name is read later
from a file that perf opens again by path, at the offset that its
string table had at load time. If the file was replaced in between,
for example by a deploy, perf would read names from the wrong file and
print wrong names without a warning. Eager loading rejects such a
file, since symsrc__init() checks its build ID.
perf checks the file each time it opens it again. dso__set_lazy_file()
records the file at load time. Nothing calls it yet: the next patch
makes the ELF loader call it when it creates lazy symbols.
If the DSO has a build ID, symsrc__init() matched the file with it at
load time, so accept a file with that build ID, such as a copy of the
same build. The build ID alone is not enough, since the offsets are
only valid if the string tables did not move: objcopy
--add-gnu-debuglink keeps the build ID but moves .strtab, and strip
--strip-debug removes sections. So perf also records the index, offset
and size of the .symtab and .dynsym string tables, and compares them.
Reading the build ID each time is slow when most lookups load a name:
the parity test on a 524 MiB binary, which loads 260k names one at a
time, takes 9.3 seconds instead of 3.9. So perf also records the
device, inode, size, mtime and ctime of the file, and reads the build
ID only if they changed. The ctime changes when another file is copied
over this one in place, even if its size and mtime are kept, as with
cp -p. Then perf keeps the identity of the accepted file for later
reads. With this, the test takes 4.1 seconds. A DSO without a build ID
must keep the same identity.
If the file is not the one perf loaded, perf warns and shows the
remaining lazy symbols of that DSO as "[unknown]" rather than print
wrong names, without opening the file again. If the file cannot be
opened or checked, for example because perf ran out of file descriptors
or fstat() or a read of its ELF headers failed, perf warns once and
shows the symbols it was reading the names of as "[unknown]", but tries
again for later names, since the failure can be temporary. A file whose
build ID cannot be read counts as not checked, since perf cannot tell a
failed read from a missing build ID note; perf reads no names from it
either.
Like symsrc__init(), the check trusts the build ID: a file edited after
linking without a new build ID, and with its string tables in place,
gives the edited names.
The unit tests later in this series cover these cases.
No functional change, since no symbol has a lazy name yet.
Signed-off-by: Alireza Haghdoost <haghdoost@uber.com>
---
tools/perf/util/dso.h | 33 +++++++++
tools/perf/util/symbol-elf.c | 156 ++++++++++++++++++++++++++++++++++++++++++-
tools/perf/util/symbol.h | 3 +
3 files changed, 189 insertions(+), 3 deletions(-)
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index 10eac0b31b7d..216a9bc9366b 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -282,6 +282,33 @@ struct dso_bpf_prog {
struct perf_env *env;
};
+/* A string table that lazy symbol names point into. */
+struct dso_lazy_strtab {
+ u64 shndx;
+ u64 off;
+ u64 size;
+};
+
+/* The file that lazy symbol names are read from, as it was at load time. */
+struct dso_lazy_file {
+ u64 dev;
+ u64 ino;
+ u64 size;
+ u64 mtime_sec;
+ u64 mtime_nsec;
+ /* Copying over the file in place, keeping its size and mtime, changes it. */
+ u64 ctime_sec;
+ u64 ctime_nsec;
+ /* The string tables of .symtab and .dynsym. */
+ struct dso_lazy_strtab strtab[2];
+ /* symsrc__init() matched the file with the build ID of the DSO. */
+ bool check_build_id;
+ /* The file changed after the names were loaded. */
+ bool rejected;
+ /* perf warned that the file could not be opened or checked again. */
+ bool retry_warned;
+};
+
struct auxtrace_cache;
DECLARE_RC_STRUCT(dso) {
@@ -321,6 +348,7 @@ DECLARE_RC_STRUCT(dso) {
struct dso_bpf_prog bpf_prog;
/* dso data file */
struct dso_data data;
+ struct dso_lazy_file lazy_file;
struct dso_id id;
unsigned int a2l_fails;
int comp;
@@ -503,6 +531,11 @@ static inline struct dso_data *dso__data(struct dso *dso)
return &RC_CHK_ACCESS(dso)->data;
}
+static inline struct dso_lazy_file *dso__lazy_file(struct dso *dso)
+{
+ return &RC_CHK_ACCESS(dso)->lazy_file;
+}
+
static inline u64 dso__db_id(const struct dso *dso)
{
return RC_CHK_ACCESS(dso)->db_id;
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 82d6a76176d2..468a0695809e 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -6,6 +6,7 @@
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
+#include <sys/stat.h>
#include "compress.h"
#include "dso.h"
@@ -1678,6 +1679,148 @@ static int read_symbol_names(struct dso *dso, int fd, struct symbol **syms, size
return err;
}
+static int lazy_file__stat(struct dso_lazy_file *lf, int fd)
+{
+ struct stat st;
+
+ if (fstat(fd, &st))
+ return -1;
+ lf->dev = st.st_dev;
+ lf->ino = st.st_ino;
+ lf->size = st.st_size;
+ lf->mtime_sec = st.st_mtim.tv_sec;
+ lf->mtime_nsec = st.st_mtim.tv_nsec;
+ lf->ctime_sec = st.st_ctim.tv_sec;
+ lf->ctime_nsec = st.st_ctim.tv_nsec;
+ return 0;
+}
+
+/*
+ * Record that lazy names of @dso point into @strtab of the file of @fd, the
+ * string table of .dynsym if @dynsym, else of .symtab. The caller holds the
+ * DSO lock, as dso__load() does.
+ */
+int dso__set_lazy_file(struct dso *dso, int fd, int dynsym,
+ const struct dso_lazy_strtab *strtab)
+{
+ struct dso_lazy_file *lf = dso__lazy_file(dso);
+
+ if (lazy_file__stat(lf, fd))
+ return -1;
+ lf->strtab[dynsym] = *strtab;
+ lf->check_build_id = dso__has_build_id(dso) && !symbol_conf.ignore_vmlinux_buildid;
+ lf->rejected = false;
+ lf->retry_warned = false;
+ return 0;
+}
+
+enum lazy_file_check {
+ LAZY_FILE_MATCH,
+ LAZY_FILE_MISMATCH,
+ /* The file cannot be read, which can be temporary. */
+ LAZY_FILE_ERROR,
+};
+
+/* Check that the file of @fd has the identity in @lf. */
+static enum lazy_file_check lazy_file__check_stat(const struct dso_lazy_file *lf, int fd)
+{
+ struct dso_lazy_file now;
+
+ if (lazy_file__stat(&now, fd))
+ return LAZY_FILE_ERROR;
+ if (now.dev == lf->dev && now.ino == lf->ino && now.size == lf->size &&
+ now.mtime_sec == lf->mtime_sec && now.mtime_nsec == lf->mtime_nsec &&
+ now.ctime_sec == lf->ctime_sec && now.ctime_nsec == lf->ctime_nsec)
+ return LAZY_FILE_MATCH;
+ return LAZY_FILE_MISMATCH;
+}
+
+/*
+ * Check that the file of @fd has the build ID of @dso and the string tables
+ * where @lf has them. A copy with an added section, such as a
+ * .gnu_debuglink, keeps the build ID but can move the tables.
+ */
+static enum lazy_file_check lazy_file__check_build(const struct dso_lazy_file *lf,
+ struct dso *dso, int fd)
+{
+ enum lazy_file_check ret = LAZY_FILE_ERROR;
+ u8 build_id[BUILD_ID_SIZE];
+ struct build_id bid;
+ GElf_Shdr shdr;
+ size_t shnum;
+ Elf *elf;
+ int size;
+
+ elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
+ if (!elf)
+ return LAZY_FILE_ERROR;
+ /* A failed read cannot be told from a missing build ID note. */
+ size = elf_read_build_id(elf, build_id, sizeof(build_id));
+ if (size <= 0)
+ goto out;
+ build_id__init(&bid, build_id, size);
+ if (!dso__build_id_equal(dso, &bid)) {
+ ret = LAZY_FILE_MISMATCH;
+ goto out;
+ }
+ if (elf_getshdrnum(elf, &shnum))
+ goto out;
+ ret = LAZY_FILE_MATCH;
+ for (size_t i = 0; ret == LAZY_FILE_MATCH && i < ARRAY_SIZE(lf->strtab); i++) {
+ const struct dso_lazy_strtab *st = &lf->strtab[i];
+
+ if (!st->size)
+ continue;
+ /* A stripped copy can have fewer sections. */
+ if (st->shndx >= shnum)
+ ret = LAZY_FILE_MISMATCH;
+ else if (!gelf_getshdr(elf_getscn(elf, st->shndx), &shdr))
+ ret = LAZY_FILE_ERROR;
+ else if (shdr.sh_type != SHT_STRTAB || (shdr.sh_flags & SHF_COMPRESSED) ||
+ shdr.sh_offset != st->off || shdr.sh_size != st->size)
+ ret = LAZY_FILE_MISMATCH;
+ }
+out:
+ elf_end(elf);
+ return ret;
+}
+
+/*
+ * The name offsets are only valid in the file that dso__load() read. Accept
+ * @fd if it is that file, or another file with the build ID of @dso and the
+ * same string tables, such as a copy, and keep its identity for later reads.
+ * If it is not, for example after the binary was replaced, reject it and
+ * every later read for @dso. If it cannot be checked, do not read from it,
+ * but let later reads try again.
+ */
+static bool dso__lazy_file_ok(struct dso *dso, int fd, const char *name)
+{
+ struct dso_lazy_file *lf = dso__lazy_file(dso);
+ /* Reading the build ID at every lookup that loads a name is slow. */
+ enum lazy_file_check status = lazy_file__check_stat(lf, fd);
+
+ if (status == LAZY_FILE_MATCH)
+ return true;
+ if (lf->check_build_id) {
+ status = lazy_file__check_build(lf, dso, fd);
+ if (status == LAZY_FILE_MATCH) {
+ lazy_file__stat(lf, fd);
+ return true;
+ }
+ }
+ if (status == LAZY_FILE_ERROR) {
+ if (!lf->retry_warned)
+ pr_warning("%s cannot be checked after its symbols were loaded, some of its symbol names are not shown\n",
+ name);
+ lf->retry_warned = true;
+ return false;
+ }
+ pr_warning("%s changed after its symbols were loaded, its symbol names are not shown\n",
+ name);
+ lf->rejected = true;
+ return false;
+}
+
/*
* A name that cannot be read becomes "[unknown]". The caller holds the
* DSO lock.
@@ -1699,6 +1842,8 @@ void dso__load_symbol_names(struct dso *dso, struct symbol **syms, size_t nr)
;
if (i == nr)
return;
+ if (dso__lazy_file(dso)->rejected)
+ goto out_unknown;
if (!own_ns)
nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
name = dso__get_filename(dso, "", &decomp, type);
@@ -1708,13 +1853,18 @@ void dso__load_symbol_names(struct dso *dso, struct symbol **syms, size_t nr)
nsinfo__mountns_exit(&nsc);
if (fd >= 0) {
- read_symbol_names(dso, fd, syms, nr);
+ if (dso__lazy_file_ok(dso, fd, name))
+ read_symbol_names(dso, fd, syms, nr);
close(fd);
- } else {
- pr_debug("%s: cannot open the symbol table file\n", dso__long_name(dso));
+ } else if (!dso__lazy_file(dso)->retry_warned) {
+ /* The failure can be temporary, so later reads try again. */
+ pr_warning("%s cannot be opened after its symbols were loaded, some of its symbol names are not shown\n",
+ name ?: dso__long_name(dso));
+ dso__lazy_file(dso)->retry_warned = true;
}
if (name)
dso__put_filename(dso, name, decomp);
+out_unknown:
for (i = 0; i < nr; i++) {
if (symbol__needs_lazy_name(syms[i]))
symbol__set_unknown_name(syms[i]);
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index d690cb1d021c..27def18f0f5c 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -271,6 +271,9 @@ 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);
void dso__load_symbol_names(struct dso *dso, struct symbol **syms, size_t nr);
+struct dso_lazy_strtab;
+int dso__set_lazy_file(struct dso *dso, int fd, int dynsym,
+ const struct dso_lazy_strtab *strtab);
char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name);
--
Git-157)
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH v8 5/7] perf script: Add --lazy-load-symbols to load symbol names lazily
2026-11-21 16:41 [PATCH v8 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
` (3 preceding siblings ...)
2026-11-21 16:41 ` [PATCH v8 4/7] perf symbols: Check the file before reading " Alireza Haghdoost via B4 Relay
@ 2026-11-21 16:41 ` Alireza Haghdoost via B4 Relay
2026-11-21 16:41 ` [PATCH v8 6/7] perf test: Add lazy symbol loading unit tests Alireza Haghdoost via B4 Relay
2026-11-21 16:41 ` [PATCH v8 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-21 16:41 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.
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 offsets are only valid in the file that perf loaded, so the loader
records the file and its string tables with dso__set_lazy_file(), and
perf checks the file, as the previous patch describes, before it reads
names from it.
The kernel, modules, PPC64 .opd and .gnu_debugdata still load names
eagerly.
On a 45,073-sample callchain profile of a large C++ service (about 1M
symbols), lazy loading cuts max RSS from 327 MiB to 174 MiB, with
identical output. It saves little run time, 3.56 seconds against 3.75,
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.
Signed-off-by: Alireza Haghdoost <haghdoost@uber.com>
---
tools/perf/Documentation/perf-script.txt | 11 ++++
tools/perf/builtin-script.c | 2 +
tools/perf/util/symbol-elf.c | 108 +++++++++++++++++++++++++++----
3 files changed, 110 insertions(+), 11 deletions(-)
diff --git a/tools/perf/Documentation/perf-script.txt b/tools/perf/Documentation/perf-script.txt
index f0228e784ced..4d04f5ecae8a 100644
--- a/tools/perf/Documentation/perf-script.txt
+++ b/tools/perf/Documentation/perf-script.txt
@@ -343,6 +343,17 @@ 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 while perf script runs. If a binary cannot be opened
+ then, or no longer matches the one perf loaded, perf warns and shows
+ the symbols whose names it cannot read as unknown. A binary matches
+ if it has the same build ID and string table layout or, without a
+ build ID, if it is the same unchanged file.
+
--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 468a0695809e..76f4aecb8632 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -1871,6 +1871,52 @@ 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 rb_node *nd = rb_first_cached(dso__symbols(dso));
+
+ while (nd) {
+ struct symbol *first = rb_entry(nd, struct symbol, rb_node);
+ struct rb_node *end = rb_next(nd), *a, *b;
+
+ while (end && rb_entry(end, struct symbol, rb_node)->start == first->start)
+ end = rb_next(end);
+
+ for (a = nd; a != end; a = rb_next(a)) {
+ for (b = rb_next(a); b != end; b = rb_next(b)) {
+ struct symbol *pair[2] = {
+ rb_entry(a, struct symbol, rb_node),
+ rb_entry(b, struct symbol, rb_node),
+ };
+
+ if (symbol__choice_needs_names(pair[0], pair[1]) &&
+ read_symbol_names(dso, fd, pair, 2))
+ return -1;
+ }
+ }
+ nd = end;
+ }
+ return 0;
+}
+
+/* PLT synthesis names IRELATIVE slots after IFUNC symbols. Load those names. */
+static int dso__load_ifunc_names(struct dso *dso, int fd)
+{
+ struct symbol *pos;
+ struct rb_node *nd;
+
+ symbols__for_each_entry(dso__symbols(dso), pos, nd) {
+ if ((symbol__type(pos) == STT_GNU_IFUNC || symbol__ifunc_alias(pos)) &&
+ read_symbol_names(dso, fd, &pos, 1))
+ return -1;
+ }
+ return 0;
+}
+
static int
dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
struct symsrc *runtime_ss, int kmodule, int dynsym)
@@ -1888,11 +1934,21 @@ 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) &&
+ !kmodule && syms_ss->type == dso__symtab_type(dso) &&
+ syms_ss->type != DSO_BINARY_TYPE__GNU_DEBUGDATA &&
+ !runtime_ss->opdsec;
+ bool lazy_names = false;
+ GElf_Shdr strshdr;
if (kmap && !kmaps)
return -1;
@@ -1928,6 +1984,17 @@ 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, and later reads must find it in the same place.
+ */
+ if (lazy_allowed && gelf_getshdr(sec, &strshdr) &&
+ strshdr.sh_type == SHT_STRTAB && !(strshdr.sh_flags & SHF_COMPRESSED) &&
+ !dso__set_lazy_file(dso, syms_ss->fd, dynsym,
+ &(struct dso_lazy_strtab){ shdr.sh_link, strshdr.sh_offset,
+ strshdr.sh_size }))
+ lazy_names = true;
+
sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
if (sec_strndx == NULL)
goto out_elf_end;
@@ -2166,16 +2233,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;
@@ -2186,6 +2261,10 @@ 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 (lazy_names)
+ pr_debug("%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);
@@ -2256,9 +2335,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, never in the kernel or modules. */
+ bool lazy = symbol_conf.lazy_load_symbols && !dso__kernel(dso) && !kmodule;
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
--
Git-157)
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH v8 6/7] perf test: Add lazy symbol loading unit tests
2026-11-21 16:41 [PATCH v8 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
` (4 preceding siblings ...)
2026-11-21 16:41 ` [PATCH v8 5/7] perf script: Add --lazy-load-symbols to load symbol names lazily Alireza Haghdoost via B4 Relay
@ 2026-11-21 16:41 ` Alireza Haghdoost via B4 Relay
2026-11-21 16:41 ` [PATCH v8 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-21 16:41 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, and eager and lazy parity on a real DSO (perf itself,
or the one given with --dso).
It also covers the check of the file that names are read from: a file
without a build ID that is replaced after the first name was read, or
overwritten in place keeping its size and mtime, gives no more names. A
file that cannot be opened for one name gives the next one. A copy of
perf keeps every name, unless its string tables moved or are missing,
or it is of another build; then perf rejects it for good. A copy whose
build ID cannot be read gives no name, but a later copy that can be
checked 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 | 963 ++++++++++++++++++++++++++++++++++++++++
tools/perf/tests/tests.h | 1 +
4 files changed, 966 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..1268f86d8e63
--- /dev/null
+++ b/tools/perf/tests/symbol-lazy.c
@@ -0,0 +1,963 @@
+// 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 <sys/stat.h>
+#include <unistd.h>
+#include <linux/kernel.h>
+
+#include "build-id.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"
+#include "util/copyfile.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)
+{
+ struct dso_lazy_strtab strtab = { 0 };
+ 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];
+ }
+ strtab.size = off;
+ if (fflush(fp) || dso__set_lazy_file(f->dso, fileno(fp), 0, &strtab))
+ goto out_close;
+ 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 size_t lazy_nr_symbols(struct dso *dso)
+{
+ struct rb_node *nd;
+ size_t nr = 0;
+
+ for (nd = rb_first_cached(dso__symbols(dso)); nd; nd = rb_next(nd))
+ nr++;
+ return nr;
+}
+
+/*
+ * 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_fixture f;
+ struct symbol *sym;
+ struct rb_node *nd;
+ 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);
+ i = 0;
+ dso__for_each_symbol(f.dso, sym, nd) {
+ if (!(i < 3 ? lazy_symbol_ok(sym, &f, i) : lazy_symbol_unknown(sym, i))) {
+ pr_debug("walk of name %zu failed\n", i);
+ goto out_exit;
+ }
+ i++;
+ }
+ if (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;
+}
+
+/*
+ * After the file is replaced, as a deploy replaces a binary, names must not
+ * be read from the new file. It has no build ID, so this holds even if it has
+ * the same contents. A name loaded before stays.
+ */
+static int test__symbol_lazy_replaced(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 lazy_fixture f, new;
+ struct symbol *sym;
+ int ret = TEST_FAIL;
+ size_t i;
+ int err;
+
+ symbol_conf.lazy_load_symbols = true;
+ if (lazy_fixture__init(&f, len, ARRAY_SIZE(len)))
+ goto out;
+ sym = dso__find_symbol(f.dso, LAZY_SYM_START);
+ if (!lazy_symbol_ok(sym, &f, 0) || lazy_fixture__init(&new, len, ARRAY_SIZE(len)))
+ goto out_exit;
+ err = rename(new.path, f.path);
+ lazy_fixture__exit(&new);
+ if (err)
+ goto out_exit;
+ for (i = 1; i < ARRAY_SIZE(len); i++) {
+ sym = dso__find_symbol_nocache(f.dso, LAZY_SYM_START + i * LAZY_SYM_SIZE);
+ if (sym) {
+ pr_debug("name %zu read from the replaced file: %s\n", i, sym->name);
+ goto out_exit;
+ }
+ }
+ sym = dso__find_symbol_nocache(f.dso, LAZY_SYM_START);
+ if (lazy_symbol_ok(sym, &f, 0))
+ ret = TEST_OK;
+out_exit:
+ lazy_fixture__exit(&f);
+out:
+ symbol_conf.lazy_load_symbols = saved_lazy;
+ return ret;
+}
+
+/*
+ * A file overwritten in place with another of the same size, keeping its
+ * mtime, as cp -p does, must not give names either. Only its ctime changes.
+ */
+static int test__symbol_lazy_overwritten(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 timespec times[2];
+ struct lazy_fixture f;
+ struct stat st, now;
+ struct symbol *sym;
+ int ret = TEST_FAIL;
+ int tries = 0;
+ u64 off = 1;
+ int fd = -1;
+ size_t i;
+
+ 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);
+ fd = open(f.path, O_WRONLY);
+ if (!lazy_symbol_ok(sym, &f, 0) || fd < 0 || fstat(fd, &st))
+ goto out_exit;
+ for (i = 0; i < ARRAY_SIZE(len); i++) {
+ char name[32];
+
+ for (size_t j = 0; j < len[i]; j++)
+ name[j] = f.names[i][j] - 'a' + 'A';
+ if (pwrite(fd, name, len[i], off) != (ssize_t)len[i])
+ goto out_exit;
+ off += len[i] + 1;
+ }
+ times[0] = st.st_atim;
+ times[1] = st.st_mtim;
+ /* The ctime can stay the same within one tick of the clock. */
+ do {
+ usleep(1000);
+ if (futimens(fd, times) || fstat(fd, &now))
+ goto out_exit;
+ } while (now.st_ctim.tv_sec == st.st_ctim.tv_sec &&
+ now.st_ctim.tv_nsec == st.st_ctim.tv_nsec && ++tries < 1000);
+ if (tries == 1000) {
+ ret = TEST_SKIP;
+ goto out_exit;
+ }
+ for (i = 1; i < ARRAY_SIZE(len); i++) {
+ sym = dso__find_symbol_nocache(f.dso, LAZY_SYM_START + i * LAZY_SYM_SIZE);
+ if (sym) {
+ pr_debug("name %zu read from the overwritten file: %s\n", i, sym->name);
+ goto out_exit;
+ }
+ }
+ ret = TEST_OK;
+out_exit:
+ if (fd >= 0)
+ close(fd);
+ 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 symbol *pos;
+ struct rb_node *nd;
+
+ i = 0;
+ dso__for_each_symbol(f->dso, pos, nd) {
+ if (!(i < f->nr ? lazy_symbol_ok(pos, f, i) : lazy_symbol_unknown(pos, i)))
+ race->failed = true;
+ i++;
+ }
+ 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;
+ 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;
+ }
+
+ 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 (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, 100, 100);
+ 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;
+}
+
+static int get_sym_entries(struct dso *dso, struct sym_entry **entries_p, size_t *nr_p)
+{
+ struct sym_entry *entries = NULL;
+ size_t nr = 0, alloc = 0;
+ struct symbol *pos;
+ struct rb_node *nd;
+
+ dso__for_each_symbol(dso, pos, nd) {
+ if (nr == alloc) {
+ struct sym_entry *tmp;
+
+ alloc = alloc ? alloc * 2 : 1024;
+ tmp = realloc(entries, alloc * sizeof(*entries));
+ if (!tmp)
+ goto out_free;
+ entries = tmp;
+ }
+ entries[nr].start = pos->start;
+ entries[nr].end = pos->end;
+ entries[nr].name = strdup(pos->name);
+ if (!entries[nr].name)
+ goto out_free;
+ nr++;
+ }
+ qsort(entries, nr, sizeof(*entries), cmp_sym_entry);
+ *entries_p = entries;
+ *nr_p = nr;
+ return 0;
+out_free:
+ free_sym_entries(entries, nr);
+ return -1;
+}
+
+/*
+ * 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];
+ struct symbol *pos;
+ struct rb_node *nd;
+ 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)
+ goto out_eager;
+
+ ret = TEST_FAIL;
+ symbols__for_each_entry(dso__symbols(map__dso(lazy_l.map)), pos, nd)
+ nr_names += symbol__needs_lazy_name(pos);
+ 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;
+}
+
+enum lazy_copy {
+ LAZY_COPY_SAME,
+ /* The string tables are not where they were, as after objcopy. */
+ LAZY_COPY_MOVED,
+ /* The copy is of another build. */
+ LAZY_COPY_OTHER_BUILD,
+ /* The copy has fewer sections, as after strip --strip-debug. */
+ LAZY_COPY_FEWER_SECTIONS,
+};
+
+/*
+ * Load @path lazily, replace it with a copy, count the lazy names and the
+ * names that become "[unknown]", and tell if perf rejected the copy.
+ */
+static int lazy_copy__load(const char *path, const char *copy, enum lazy_copy how,
+ size_t *nr_names, size_t *nr_unknown, bool *rejected)
+{
+ size_t names = 0, unknown = 0;
+ struct dso_lazy_file *lf;
+ struct sym_loader l;
+ struct build_id bid;
+ struct symbol *pos;
+ struct rb_node *nd;
+ struct dso *dso;
+ int ret;
+
+ ret = sym_loader__init(&l, path, true);
+ if (ret != TEST_OK)
+ return ret;
+ dso = map__dso(l.map);
+ symbols__for_each_entry(dso__symbols(dso), pos, nd)
+ names += symbol__needs_lazy_name(pos);
+ if (!names || !dso__has_build_id(dso) ||
+ dso__symtab_type(dso) != DSO_BINARY_TYPE__SYSTEM_PATH_DSO) {
+ pr_debug("%s: no lazy names read from the file\n", path);
+ ret = TEST_SKIP;
+ goto out;
+ }
+
+ lf = dso__lazy_file(dso);
+ switch (how) {
+ case LAZY_COPY_MOVED:
+ for (size_t i = 0; i < ARRAY_SIZE(lf->strtab); i++)
+ lf->strtab[i].off += !!lf->strtab[i].size;
+ break;
+ case LAZY_COPY_OTHER_BUILD:
+ bid = *dso__bid(dso);
+ bid.data[0] ^= 1;
+ dso__set_build_id(dso, &bid);
+ break;
+ case LAZY_COPY_FEWER_SECTIONS:
+ for (size_t i = 0; i < ARRAY_SIZE(lf->strtab); i++)
+ lf->strtab[i].shndx += 1 << 16;
+ break;
+ case LAZY_COPY_SAME:
+ default:
+ break;
+ }
+
+ ret = TEST_FAIL;
+ if (copyfile(path, copy) || rename(copy, path))
+ goto out;
+ dso__for_each_symbol(dso, pos, nd)
+ unknown += !strcmp(pos->name, "[unknown]");
+ pr_debug("%s: %zu lazy names, %zu unknown\n", path, names, unknown);
+ *nr_names = names;
+ *nr_unknown = unknown;
+ *rejected = lf->rejected;
+ ret = TEST_OK;
+out:
+ sym_loader__exit(&l);
+ return ret;
+}
+
+/*
+ * A copy of the file with the same build ID and string tables must give
+ * every name. A copy whose string tables moved or are missing, or one of
+ * another build, must give none, and perf must not try it again.
+ */
+static int test__symbol_lazy_copy(struct test_suite *test __maybe_unused,
+ int subtest __maybe_unused)
+{
+ static const enum lazy_copy cases[] = {
+ LAZY_COPY_SAME, LAZY_COPY_MOVED, LAZY_COPY_OTHER_BUILD,
+ LAZY_COPY_FEWER_SECTIONS,
+ };
+ bool saved_lazy = symbol_conf.lazy_load_symbols;
+ char dir[] = "/tmp/perf-test-lazy-copy-XXXXXX";
+ char exe[PATH_MAX], path[PATH_MAX], copy[PATH_MAX], saved_buildid_dir[PATH_MAX];
+ size_t nr_names, nr_unknown;
+ int ret = TEST_FAIL;
+ bool rejected;
+
+ perf_exe(exe, sizeof(exe));
+ if (!mkdtemp(dir))
+ return TEST_FAIL;
+ /* Names must be read from the copy, not from a cached perf binary. */
+ strlcpy(saved_buildid_dir, buildid_dir, sizeof(saved_buildid_dir));
+ set_buildid_dir(dir);
+ snprintf(path, sizeof(path), "%s/perf", dir);
+ snprintf(copy, sizeof(copy), "%s/perf-new", dir);
+ if (copyfile(exe, path)) {
+ pr_debug("cannot copy %s\n", exe);
+ goto out_unlink;
+ }
+
+ for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
+ bool same;
+
+ ret = lazy_copy__load(path, copy, cases[i], &nr_names, &nr_unknown,
+ &rejected);
+ if (ret != TEST_OK)
+ break;
+ same = cases[i] == LAZY_COPY_SAME;
+ if ((same && (nr_unknown || rejected)) ||
+ (!same && (nr_unknown != nr_names || !rejected))) {
+ pr_debug("case %zu: %zu of %zu names unknown%s\n", i, nr_unknown,
+ nr_names, rejected ? ", rejected" : "");
+ ret = TEST_FAIL;
+ break;
+ }
+ }
+out_unlink:
+ unlink(copy);
+ unlink(path);
+ rmdir(dir);
+ set_buildid_dir(saved_buildid_dir);
+ symbol_conf.lazy_load_symbols = saved_lazy;
+ return ret;
+}
+
+/* Rename the owner of the build ID note of @dso in @path, so it is not found. */
+static int lazy_break_build_id(const char *path, struct dso *dso)
+{
+ const struct build_id *bid = dso__bid(dso);
+ char note[4 + BUILD_ID_SIZE] = "GNU";
+ int fd = open(path, O_RDWR);
+ struct stat st;
+ char *p, *map;
+ int ret = -1;
+
+ if (fd < 0)
+ return -1;
+ memcpy(note + 4, bid->data, bid->size);
+ if (fstat(fd, &st))
+ goto out_close;
+ map = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (map == MAP_FAILED)
+ goto out_close;
+ p = memmem(map, st.st_size, note, 4 + bid->size);
+ if (p) {
+ p[0] = 'X';
+ ret = 0;
+ }
+ munmap(map, st.st_size);
+out_close:
+ close(fd);
+ return ret;
+}
+
+/*
+ * A copy whose build ID cannot be read, as after a failed read, must give no
+ * names, but must not stop a later copy that can be checked from giving them.
+ */
+static int test__symbol_lazy_check_failed(struct test_suite *test __maybe_unused,
+ int subtest __maybe_unused)
+{
+ bool saved_lazy = symbol_conf.lazy_load_symbols;
+ char dir[] = "/tmp/perf-test-lazy-check-XXXXXX";
+ char exe[PATH_MAX], path[PATH_MAX], copy[PATH_MAX], saved_buildid_dir[PATH_MAX];
+ struct symbol *pos, *syms[2] = { NULL, NULL };
+ struct sym_loader l;
+ struct rb_node *nd;
+ struct dso *dso;
+ int ret = TEST_FAIL;
+ size_t nr = 0;
+
+ perf_exe(exe, sizeof(exe));
+ if (!mkdtemp(dir))
+ return TEST_FAIL;
+ /* Names must be read from the copy, not from a cached perf binary. */
+ strlcpy(saved_buildid_dir, buildid_dir, sizeof(saved_buildid_dir));
+ set_buildid_dir(dir);
+ snprintf(path, sizeof(path), "%s/perf", dir);
+ snprintf(copy, sizeof(copy), "%s/perf-new", dir);
+ if (copyfile(exe, path)) {
+ pr_debug("cannot copy %s\n", exe);
+ goto out_unlink;
+ }
+
+ ret = sym_loader__init(&l, path, true);
+ if (ret != TEST_OK)
+ goto out_unlink;
+ dso = map__dso(l.map);
+ symbols__for_each_entry(dso__symbols(dso), pos, nd) {
+ if (symbol__needs_lazy_name(pos) && pos->end > pos->start)
+ syms[nr++] = pos;
+ if (nr == ARRAY_SIZE(syms))
+ break;
+ }
+ if (nr < ARRAY_SIZE(syms) || !dso__has_build_id(dso) ||
+ dso__symtab_type(dso) != DSO_BINARY_TYPE__SYSTEM_PATH_DSO) {
+ pr_debug("%s: no lazy names read from the file\n", path);
+ ret = TEST_SKIP;
+ goto out;
+ }
+
+ ret = TEST_FAIL;
+ if (copyfile(path, copy) || lazy_break_build_id(copy, dso) || rename(copy, path))
+ goto out;
+ if (dso__find_symbol_nocache(dso, syms[0]->start)) {
+ pr_debug("name read from a file that was not checked\n");
+ goto out;
+ }
+ if (copyfile(exe, copy) || rename(copy, path))
+ goto out;
+ pos = dso__find_symbol_nocache(dso, syms[1]->start);
+ if (pos == syms[1] && !symbol__needs_lazy_name(pos) && strcmp(pos->name, "[unknown]"))
+ ret = TEST_OK;
+ else
+ pr_debug("name not read after the file could be checked\n");
+out:
+ sym_loader__exit(&l);
+out_unlink:
+ unlink(copy);
+ unlink(path);
+ rmdir(dir);
+ set_buildid_dir(saved_buildid_dir);
+ 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 from a replaced file", symbol_lazy_replaced),
+ TEST_CASE("Lazy symbol names from a file overwritten in place", symbol_lazy_overwritten),
+ TEST_CASE("Lazy symbol names after a failed open", symbol_lazy_open_failed),
+ TEST_CASE("Lazy symbol names from a copy of the file", symbol_lazy_copy),
+ TEST_CASE("Lazy symbol names after a failed check", symbol_lazy_check_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 v8 7/7] perf test: Add lazy symbol loading shell tests
2026-11-21 16:41 [PATCH v8 0/7] perf script: Lazy symbol loading Alireza Haghdoost via B4 Relay
` (5 preceding siblings ...)
2026-11-21 16:41 ` [PATCH v8 6/7] perf test: Add lazy symbol loading unit tests Alireza Haghdoost via B4 Relay
@ 2026-11-21 16:41 ` Alireza Haghdoost via B4 Relay
6 siblings, 0 replies; 8+ messages in thread
From: Alireza Haghdoost via B4 Relay @ 2026-11-21 16:41 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.
script_lazy_load_symbols.sh records a small callchain profile and checks
that perf script prints the same output with and without
--lazy-load-symbols. As root, it also checks that names are read from
the build-id cache when the profiled process is in another mount
namespace.
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_parity.sh | 447 +++++++++++++++++++++
tools/perf/tests/shell/lib/lazy_names.sh | 20 +
tools/perf/tests/shell/script_lazy_load_symbols.sh | 190 +++++++++
3 files changed, 657 insertions(+)
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..178988a6202f
--- /dev/null
+++ b/tools/perf/tests/shell/lazy_load_symbols_parity.sh
@@ -0,0 +1,447 @@
+#!/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, which keeps zero size only because the .dynsym
+# copy of the function follows it;
+# - on x86_64, a shared library of exported functions with nested entry
+# points, where a lookup past the end of the inner one must still find
+# the outer one;
+# - 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
+
+shelldir=$(dirname "$0")
+# shellcheck source=lib/lazy_names.sh
+. "${shelldir}"/lib/lazy_names.sh
+
+err=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]"
+ 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
+ objcopy --only-keep-debug "${prog}" "${prog}.debug"
+ strip -s "${prog}"
+ objcopy --add-gnu-debuglink="${prog}.debug" "${prog}"
+
+ # 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.
+ objcopy --strip-symbol=dyn_only --strip-symbol=dyn_and_symtab_alias \
+ "${lib}"
+ 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
+ # The lookup at the last byte of each outer function, past the end of
+ # its inner entry point, depends on the shape of the rb-tree, which
+ # the .dynsym copies take part in.
+ {
+ echo " .text"
+ 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
+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
+}
diff --git a/tools/perf/tests/shell/script_lazy_load_symbols.sh b/tools/perf/tests/shell/script_lazy_load_symbols.sh
new file mode 100755
index 000000000000..48f05b24c921
--- /dev/null
+++ b/tools/perf/tests/shell/script_lazy_load_symbols.sh
@@ -0,0 +1,190 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# perf script lazy symbol loading tests (exclusive)
+#
+# Verifies that --lazy-load-symbols matches the default eager loader for a
+# controlled symbol.
+
+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 -v 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 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 -i "${perfdata}" 2> /dev/null > "${eager_out}" || \
+ ! perf script -v --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 ! check_lazy_names "Lazy-load identical"
+ then
+ return
+ fi
+ if ! cmp -s "${eager_out}" "${lazy_out}"
+ then
+ echo "Lazy-load identical [Failed output differs from eager]"
+ err=1
+ return
+ fi
+ echo "Lazy-load identical [Success]"
+ passed=$((passed + 1))
+}
+
+# A build-id cache file is opened outside the target's mount namespace, so
+# lazy name reads must open it there too.
+test_lazy_load_mount_ns() {
+ echo "Lazy-load build-id cache source of a process in a 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 -i "${nsdata}" \
+ 2> /dev/null > "${eager_out}" ||
+ ! perf --buildid-dir "${buildid_dir}" script -v --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 ! check_lazy_names "Lazy-load mount namespace"
+ then
+ return
+ fi
+ if ! cmp -s "${eager_out}" "${lazy_out}"
+ then
+ echo "Lazy-load mount namespace [Failed output differs from eager]"
+ err=1
+ return
+ fi
+ echo "Lazy-load mount namespace [Success]"
+ passed=$((passed + 1))
+}
+
+test_lazy_load_identical
+test_lazy_load_mount_ns
+
+cleanup
+# Report a skip only if no test case ran.
+if [ "${err}" -eq 0 ] && [ "${passed}" -eq 0 ]
+then
+ err=2
+fi
+exit $err
--
Git-157)
^ permalink raw reply [flat|nested] 8+ messages in thread