perf script: Lazy symbol loading
v9 cover perf script: Lazy symbol loading
perf script loads the entire ELF symbol table of every DSO that appears in a sample, and keeps each symbol, with its demangled name, until process exit. Most of those symbols are never sampled. On a large profile this turns symbol loading into the main memory cost of perf script, and in a memory-constrained cgroup into an OOM kill.
With this series, perf script, perf report and perf top --lazy-load-symbols read a symbol name only when a sample needs it. On a perf script run over a 45,073-sample callchain profile of a large C++ service (about 1M symbols), the output is identical in all three cases (medians of eleven runs):
time max RSS perf-tools-next 2.24 s 305 MiB v9 2.26 s 315 MiB v9 --lazy-load-symbols 1.97 s 164 MiB
Eager loading remains the default, and costs 8 more bytes per symbol for the name pointer. Lazy loading covers userspace ELF .symtab and .dynsym. The kernel, modules, PPC64 .opd and .gnu_debugdata still load names eagerly. Names are read from the file that perf loaded the symbols from, so the output is the same as with eager loading as long as the binaries do not change while perf runs.
The patches:
[1/7] Split the size, type and binding checks out of
choose_best_symbol(), so that the loader can tell which aliases need their names. No functional change. Unchanged from v8, reviewed by Ian.
[2/7] Make the symbol name a pointer instead of a flexible array, so
that a name can be set after the symbol is in the symbol array. Reviewed by Ian; only the commit message changed.
[3/7] Add lazy symbol names to the symbol code: the DSO address
lookups read the name of the symbol they return, and walks and lookups by name read all names of the DSO first. Nothing creates such symbols yet.
[4/7] Add --lazy-load-symbols to perf script, which makes the ELF
loader create symbols with lazy names.
[5/7] Add --lazy-load-symbols to perf report and perf top.
[6/7] Unit tests.
[7/7] Shell tests, including parity with eager loading for perf
script and perf report.
This series is rebased onto the first six patches of Ian's v8 [1], now in perf-tools-next. Ian's v9 [2] applies on top of it with one trivial conflict in struct dso, and the symbol and lazy loading tests pass on the combination. I'll rebase if his patches land first.
[1] https://lore.kernel.org/linux-perf-users/[email protected]/
[2] https://lore.kernel.org/linux-perf-users/[email protected]/
Changes in v9:
- Rebase onto current perf-tools-next, which has Ian's symbol array and the fix that keeps cached debuginfo in perf's mount namespace. 3/7 now tracks per DSO whether all its names are loaded, since a DSO sorted by name can still have lazy names.
- Drop v8 4/7, the check of the symbol table file, and its tests (Namhyung). The man pages say that the output equals eager loading as long as the binaries do not change while perf runs.
- 4/7 (was 5/7): drop the kmodule check, use pr_debug2(), and add parentheses around multi-line expressions (Namhyung).
- 4/7: create lazy names only for a symbol table file that perf can reopen by the same name in its own mount namespace, since perf top's sample thread cannot switch namespaces.
- New 5/7: --lazy-load-symbols for perf report and perf top (Namhyung).
- 6/7: drop the file check tests.
- 7/7: rename script_lazy_load_symbols.sh to lazy_load_symbols.sh, and add perf report parity and a case for a process in another mount namespace.
Previous versions:
v8: https://lore.kernel.org/all/[email protected]/
v7: https://lore.kernel.org/all/[email protected]/
v6: https://lore.kernel.org/all/[email protected]/
v5: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
---
Alireza Haghdoost (7):
perf symbols: Split out name-independent duplicate symbol selection perf symbols: Point to the symbol name instead of embedding it perf symbols: Add lazy symbol names
perf script: Add --lazy-load-symbols to load symbol names lazily perf report/top: Add --lazy-load-symbols
perf test: Add lazy symbol loading unit tests
perf test: Add lazy symbol loading shell tests
tools/perf/Documentation/perf-report.txt | 9 + tools/perf/Documentation/perf-script.txt | 9 + tools/perf/Documentation/perf-top.txt | 10 + tools/perf/builtin-report.c | 2 + tools/perf/builtin-script.c | 2 + tools/perf/builtin-top.c | 2 + tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/shell/lazy_load_symbols.sh | 243 ++++++++ tools/perf/tests/shell/lazy_load_symbols_parity.sh | 462 +++++++++++++++ tools/perf/tests/shell/lib/lazy_names.sh | 20 + tools/perf/tests/symbol-lazy.c | 647 +++++++++++++++++++++ tools/perf/tests/tests.h | 1 + tools/perf/ui/browsers/map.c | 2 +- tools/perf/ui/stdio/hist.c | 1 + tools/perf/util/dso.h | 28 +- tools/perf/util/symbol-elf.c | 218 ++++++- tools/perf/util/symbol-minimal.c | 6 + tools/perf/util/symbol.c | 138 ++++- tools/perf/util/symbol.h | 29 +- tools/perf/util/symbol_conf.h | 1 + tools/perf/util/symsrc.h | 2 + 22 files changed, 1804 insertions(+), 30 deletions(-)
---
base-commit: 964e8b1c9c795982886a6778cb0cd73c901ca4ca
change-id: 20260915-perf-symbol-memory-send-e7cfca1ac3d9
Best regards,
Signature · 1 line
Alireza Haghdoost <[email protected]>
PATCH v9 1/7 perf symbols: Split out name-independent duplicate symbol selection
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.
--- tools/perf/util/symbol.c | 26 ++++++++++++++++++++++++-- tools/perf/util/symbol.h | 1 + 2 files changed, 25 insertions(+), 2 deletions(-)
tools/perf/util/symbol.c +24 -2
@@ -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);
tools/perf/util/symbol.h +1 -0
@@ -327,6 +327,7 @@ int arch__compare_symbol_names(const char *namea, const char *nameb);int arch__compare_symbol_names_n (const char *namea,const char *nameb,unsigned int n);int arch__choose_best_symbol (struct symbol *syma,struct symbol *symb); +bool symbol__choice_needs_names (const struct symbol *syma,const struct symbol *symb);enum symbol_tag_include {SYMBOL_TAG_INCLUDE__NONE =0 ,
Signature · 1 line
Git-157)
PATCH v9 2/7 perf symbols: Point to the symbol name instead of embedding it
In reply to Alireza HaghdoostThis patch changes the name field of struct symbol from a flexible array (char name[]) to a pointer (char *name). It does not change behavior, but each symbol allocation grows by 8 bytes on 64-bit hosts.
The next patch creates symbols whose names are read only when needed. perf does not know the length of such a name when it creates the symbol, so it cannot allocate the name after the symbol. It cannot reallocate the symbol later either, because the symbol array and the callers hold pointers to it. With a pointer, the name is allocated separately and set when it is read.
--- 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(-)
tools/perf/ui/stdio/hist.c +1 -0
@@ -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; }
tools/perf/util/symbol.c +1 -0
@@ -634,6 +634,7 @@ struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *pr_debug4 ("%s: %s %#" PRIx64"-%#" PRIx64"\n" , __func__, name, start, sym->end); + sym->name = (char *)(sym +1 );memcpy (sym->name, name, namelen);return sym;
tools/perf/util/symbol.h +1 -1
@@ -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; };/**
Signature · 1 line
Git-157)
PATCH v9 3/7 perf symbols: Add lazy symbol names
In reply to Alireza HaghdoostThis patch lets a symbol be created without its name, so that perf reads the name lazily only when it needs it and does not waste RSS memory holding symbol names that are never sampled. symbol__new_lazy() stores the file offset of the name instead, in a union with the name pointer, and SYMBOL_FLAG_LAZY_NAME tells which one is set. This is a preparatory change for a later patch in this series, which adds --lazy-load-symbols to the ELF loader.
The address lookups load the name of the symbol they return. The walks over all symbols of a DSO and the lookups by name get the symbols from dso__named_symbols(), which loads every name the first time. The loader will mark a DSO that has lazy names as pending. dso__named_symbols() loads them and marks the DSO eager, also when the DSO is not loaded yet, so that a load racing with a walk, as in perf top, adds no symbols without names.
Names are read under the DSO lock with pread() from the symbol table file, which is open only while perf reads them, and demangled as at load time. The file name is built again and opened in perf's mount namespace, so the loader must create lazy names only for a file that it opened there by that name. If another thread loaded them first, perf does not open the file. A name that cannot be read becomes "[unknown]", and address lookups do not return that symbol. SYMBOL_FLAG_NAME_ALLOCATED marks a loaded name, which is freed with the symbol. The ELF loader looks symbols up with symbols__find(), so that it does not read names while it loads the symbols.
No functional change, since no symbol has a lazy name yet.
--- tools/perf/ui/browsers/map.c | 2 +- tools/perf/util/dso.h | 28 +++++++++- tools/perf/util/symbol-elf.c | 89 +++++++++++++++++++++++++++++++- tools/perf/util/symbol-minimal.c | 6 +++ tools/perf/util/symbol.c | 108 +++++++++++++++++++++++++++++++++++---- tools/perf/util/symbol.h | 28 ++++++++-- tools/perf/util/symbol_conf.h | 1 + 7 files changed, 245 insertions(+), 17 deletions(-)
tools/perf/ui/browsers/map.c +1 -1
@@ -108,7 +108,7 @@ static int map_browser__run(struct map_browser *browser)int map__browse (struct map *map) { -struct symbols *symbols =dso__symbols (map__dso (map)); +struct symbols *symbols =dso__named_symbols (map__dso (map));struct map_browser mb = { .b = { .refresh = ui_browser__argv_refresh,
tools/perf/util/dso.h +26 -2
@@ -154,6 +154,15 @@ enum dso_swap_type { DSO_SWAP__YES, }; +enum dso_lazy_names { +/* No symbol has a lazy name. */ +DSO_LAZY_NAMES__NONE , +/* Some symbol names are not loaded yet. */ +DSO_LAZY_NAMES__PENDING , +/* All names were needed once, so later loads read names eagerly. */ +DSO_LAZY_NAMES__EAGER , +}; +enum dso_data_status {DSO_DATA_STATUS_ERROR =-1 ,DSO_DATA_STATUS_UNKNOWN =0 ,
@@ -344,6 +353,8 @@ DECLARE_RC_STRUCT(dso) { u8 bintype_searched:1; u8 debuginfo_searched:1;bool loaded; +/* Under the DSO lock. */ +enum dso_lazy_names lazy_names;u8 rel;char name[]; };
@@ -360,10 +371,10 @@ int dso_id__cmp(const struct dso_id *a, const struct dso_id *b);* @data: opaque data pointer passed to @cb */ #define dso__for_each_symbol (dso, cb, data) \ - symbols__visit(dso__symbols(dso), cb, data) + symbols__visit(dso__named_symbols(dso), cb, data)#define dso__for_each_symbol_by_name (dso, cb, data) \ - symbols__visit_by_name(dso__symbols(dso), cb, data) + symbols__visit_by_name(dso__named_symbols(dso), cb, data)static inline void *dso__a2l (const struct dso *dso) {
@@ -602,6 +613,16 @@ static inline void dso__set_loaded(struct dso *dso)RC_CHK_ACCESS (dso)->loaded = true; } +static inline enum dso_lazy_names dso__lazy_names (const struct dso *dso) +{ +return RC_CHK_ACCESS (dso)->lazy_names; +} + +static inline void dso__set_lazy_names (struct dso *dso,enum dso_lazy_names lazy_names) +{ +RC_CHK_ACCESS (dso)->lazy_names = lazy_names; +} +static inline struct mutex *dso__lock (struct dso *dso) __returns_ctx_lock(&RC_CHK_ACCESS(dso)->lock) {
@@ -792,6 +813,9 @@ char *dso__filename_with_chroot(const struct dso *dso, const char *filename);bool dso__sorted_by_name (const struct dso *dso);void dso__sort_by_name (struct dso *dso); +struct symbols *dso__named_symbols (struct dso *dso); +void dso__load_symbol_names(struct dso *dso, struct symbol **syms, size_t nr) + __must_hold(dso__lock(dso));int dso__swap_init (struct dso *dso,unsigned char eidata);
tools/perf/util/symbol-elf.c +87 -2
107 lines not on this page · raw message
tools/perf/util/symbol-minimal.c +6 -0
@@ -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,
tools/perf/util/symbol.c +99 -9
164 lines not on this page · raw message
tools/perf/util/symbol.h +25 -3
@@ -79,8 +79,16 @@ enum symbol_idle_kind {#define SYMBOL_FLAG_INLINED (1 << 11)#define SYMBOL_FLAG_ANNOTATE2 (1 << 12)#define SYMBOL_FLAG_IFUNC_ALIAS (1 << 13) +#define SYMBOL_FLAG_LAZY_NAME (1 << 14) +#define SYMBOL_FLAG_NAME_ALLOCATED (1 << 15)/** * A symtab entry. +* +* The DSO lookups such as dso__find_symbol() load a lazy name before +* returning the symbol, and the address lookups return NULL if it cannot +* be read. Code that walks dso__symbols() directly must use +* dso__named_symbols() or not read the name of a symbol with +* SYMBOL_FLAG_LAZY_NAME. */ struct symbol {/** Range of symbol [start, end). */
@@ -91,8 +99,12 @@ struct symbol { _Atomicuint16_t flags;/** Architecture specific. Unused except on PPC where it holds st_other. */ u8 arch_sym; -/** The name of length namelen associated with the symbol. */ -char *name; +union { +/** The name of length namelen associated with the symbol. */ +char *name; +/** File offset of the name while SYMBOL_FLAG_LAZY_NAME is set. */ +u64 name_off; + }; };/**
@@ -132,6 +144,13 @@ void symbols__exit(struct symbols *symbols);void symbols__delete (struct symbols *symbols); void symbols__sort_read_lock(struct symbols *symbols) __acquires_shared(&symbols->lock); +/* Pairs with the release in symbol__set_loaded_name(). */ +static inline bool symbol__needs_lazy_name (const struct symbol *sym) +{ +return (atomic_load_explicit (&sym->flags, memory_order_acquire) & +SYMBOL_FLAG_LAZY_NAME ) !=0 ; +} +static inline u8 symbol__type (const struct symbol *sym) { return (atomic_load_explicit(&sym->flags, memory_order_relaxed) &
@@ -252,6 +271,9 @@ void symbol__elf_init(void);int symbol__annotation_init (void );struct symbol *symbol__new (u64 start,u64 len,u8 binding,u8 type,const char *name); +struct symbol *symbol__new_lazy (u64 start,u64 len,u8 binding,u8 type,u64 name_off); +void symbol__set_loaded_name (struct symbol *sym,char *name); +void symbol__set_unknown_name (struct symbol *sym); size_t __symbol__fprintf_symname_offs(const struct symbol *sym, const struct addr_location *al, bool unknown_as_addr,
@@ -282,10 +304,10 @@ int dso__load_bfd_symbols(struct dso *dso, const char *debugfile);int dso__load_sym (struct dso *dso,struct map *map,struct symsrc *syms_ss,struct symsrc *runtime_ss,int kmodule);int dso__synthesize_plt_symbols (struct dso *dso,struct symsrc *ss); -char *dso__demangle_sym (struct dso *dso,int kmodule,const char *elf_name);int symbols__insert (struct symbols *symbols,struct symbol *sym); +struct symbol *symbols__find (struct symbols *symbols,u64 ip);void symbols__fixup_duplicate (struct symbols *symbols);void symbols__fixup_end (struct symbols *symbols,bool is_kallsyms);
tools/perf/util/symbol_conf.h +1 -0
@@ -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,
Signature · 1 line
Git-157)
PATCH v9 4/7 perf script: Add --lazy-load-symbols to load symbol names lazily
In reply to Alireza HaghdoostThis patch adds a --lazy-load-symbols option to perf script. With it, the ELF loader creates userspace .symtab and .dynsym symbols with lazy names, and perf reads a name only when a lookup returns that symbol. Eager loading stays the default.
perf script loads the whole ELF symbol table of every DSO that appears in a sample and keeps every symbol, with its demangled name, until exit. Most of those symbols are never sampled, and their names take most of the memory. On a large profile this is the main memory cost of perf script, and in a memory-limited cgroup it gets perf OOM-killed.
On a 45,073-sample callchain profile of a large C++ service (about 1M symbols), lazy loading cuts max RSS from 315 MiB to 164 MiB, with identical output. It saves little run time, 1.97 seconds against 2.26, because one of the binaries exports most of its symbols, and duplicate selection reads the names of each .dynsym copy and of the .symtab symbol that it copies.
The loader still creates every symbol, so end fixup, duplicate selection, PLT synthesis and IFUNC marking do not change. It uses lazy names only if the string table is stored as is in the symbol table file, since names are read from it with pread().
A few names are still read at load time: aliases that duplicate selection can tell apart only by name, such as the .dynsym copies of .symtab symbols, and IFUNC symbols, whose names PLT synthesis uses.
The names are read from the symbol table file while perf script runs, at the offsets it had at load time. So the output is the same as with eager loading as long as the file does not change in the meantime, which is rare.
The kernel, modules, PPC64 .opd and .gnu_debugdata still load names eagerly. So does a binary of a process in another mount namespace unless it is read from the build-id cache, since names are read in perf's own namespace: not every perf top thread can switch namespaces. So does a file that dso__load() found through its chroot fallback, since perf may not find that file again by the name it builds.
--- tools/perf/Documentation/perf-script.txt | 9 +++ tools/perf/builtin-script.c | 2 + tools/perf/util/symbol-elf.c | 129 ++++++++++++++++++++++++++++--- tools/perf/util/symbol.c | 3 + tools/perf/util/symsrc.h | 2 + 5 files changed, 134 insertions(+), 11 deletions(-)
tools/perf/Documentation/perf-script.txt +9 -0
@@ -343,6 +343,15 @@ include::itrace.txt[] Default: 127 +--lazy-load-symbols:: + Read and demangle userspace ELF symbol names only when perf needs + them. By default, perf reads every symbol name from each binary + referenced by a sample. This option reduces memory use when samples + reference only a small fraction of a large binary's symbols. The + names are read from the binaries while perf script runs, so the + binaries must not change in the meantime. If a binary cannot be + opened when a name is needed, perf shows that symbol as unknown. + --ns:: Use 9 decimal places when displaying time (i.e. show the nanoseconds)
tools/perf/builtin-script.c +2 -0
@@ -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,
tools/perf/util/symbol-elf.c +118 -11
188 lines not on this page · raw message
tools/perf/util/symbol.c +3 -0
@@ -2394,6 +2394,7 @@ int dso__load(struct dso *dso, struct map *map)bool next_slot = false;bool is_reg;bool nsexit; +bool chroot = false;int bfdrc =-1 ;int sirc =-1 ;
@@ -2419,6 +2420,7 @@ int dso__load(struct dso *dso, struct map *map) is_reg =is_regular_file (new_name);strlcpy (name, new_name,PATH_MAX );free (new_name); + chroot = true; } }
@@ -2440,6 +2442,7 @@ int dso__load(struct dso *dso, struct map *map)if (!is_reg || sirc <0 )continue ; + ss->chroot = chroot;if (!syms_ss &&symsrc__has_symtab (ss)) { syms_ss = ss;
tools/perf/util/symsrc.h +2 -0
@@ -16,6 +16,8 @@ struct symsrc {char *name;int fd;enum dso_binary_type type; +/* Found through the chroot fallback of dso__load(). */ +bool chroot;#ifdef HAVE_LIBELF_SUPPORT Elf *elf;
Signature · 1 line
Git-157)
PATCH v9 5/7 perf report/top: Add --lazy-load-symbols
In reply to Alireza HaghdoostThis patch adds the --lazy-load-symbols option of perf script to perf report and perf top. Like perf script, they load the whole symbol table of every DSO that appears in a sample and keep every name until exit.
The symbol code already handles both tools. The address lookups that build the histogram entries and call chains load the names of the symbols they return, and the TUI map browser gets all names from dso__named_symbols().
perf top reads the names while it runs, which can be long, so a binary that is updated in the meantime can show wrong names. The documentation of perf top says so.
--- tools/perf/Documentation/perf-report.txt | 9 +++++++++ tools/perf/Documentation/perf-top.txt | 10 ++++++++++ tools/perf/builtin-report.c | 2 ++ tools/perf/builtin-top.c | 2 ++ 4 files changed, 23 insertions(+)
tools/perf/Documentation/perf-report.txt +9 -0
@@ -347,6 +347,15 @@ OPTIONS Default: 127 +--lazy-load-symbols:: + Read and demangle userspace ELF symbol names only when perf needs + them. By default, perf reads every symbol name from each binary + referenced by a sample. This option reduces memory use when samples + reference only a small fraction of a large binary's symbols. The + names are read from the binaries while perf report runs, so the + binaries must not change in the meantime. If a binary cannot be + opened when a name is needed, perf shows that symbol as unknown. + -G:: --inverted:: alias for inverted caller based call graph.
tools/perf/Documentation/perf-top.txt +10 -0
@@ -218,6 +218,16 @@ Default is to monitor all CPUS. Default: /proc/sys/kernel/perf_event_max_stack when present, 127 otherwise. +--lazy-load-symbols:: + Read and demangle userspace ELF symbol names only when perf needs + them. By default, perf reads every symbol name from each binary + referenced by a sample. This option reduces memory use when samples + reference only a small fraction of a large binary's symbols. The + names are read from the binaries while perf top runs, so a binary + that is updated in the meantime can show wrong names. If a binary + cannot be opened when a name is needed, perf shows that symbol as + unknown. + --ignore-callees=<regex>:: Ignore callees of the function(s) matching the given regex. This has the effect of collecting the callers of each such
tools/perf/builtin-report.c +2 -0
@@ -1414,6 +1414,8 @@ int cmd_report(int argc, const char **argv)"Set the maximum stack depth when parsing the callchain, " "anything beyond the specified depth will be ignored. " "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)), +OPT_BOOLEAN (0 ,"lazy-load-symbols" , &symbol_conf.lazy_load_symbols, +"Read symbol names only when they are needed" ),OPT_BOOLEAN ('G' ,"inverted" , &report.inverted_callchain,"alias for inverted call graph" ), OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
tools/perf/builtin-top.c +2 -0
@@ -1589,6 +1589,8 @@ int cmd_top(int argc, const char **argv)OPT_INTEGER (0 ,"max-stack" , &top.max_stack,"Set the maximum stack depth when parsing the callchain. " "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH )), +OPT_BOOLEAN (0 ,"lazy-load-symbols" , &symbol_conf.lazy_load_symbols, +"Read symbol names only when they are needed" ),OPT_CALLBACK (0 ,"ignore-callees" ,NULL ,"regex" ,"ignore callees of these functions in call graphs" , report_parse_ignore_callees_opt),
Signature · 1 line
Git-157)
PATCH v9 6/7 perf test: Add lazy symbol loading unit tests
In reply to Alireza HaghdoostAdd a "Lazy symbol loading" unit test suite. It covers duplicate selection without names, lazy names that take one or more reads or cannot be read at all, lookups from several threads while another one walks the symbols, eager and lazy parity on a real DSO (perf itself, or the one given with --dso), and a file that cannot be opened for one name but gives the next one.
--- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/symbol-lazy.c | 647 ++++++++++++++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 4 files changed, 650 insertions(+)
tools/perf/tests/Build +1 -0
@@ -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
tools/perf/tests/builtin-test.c +1 -0
@@ -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,
tools/perf/tests/symbol-lazy.c new file +647 -0
647 lines not on this page · raw message
tools/perf/tests/tests.h +1 -0
@@ -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);
Signature · 1 line
Git-157)
PATCH v9 7/7 perf test: Add lazy symbol loading shell tests
In reply to Alireza Haghdoostlazy_load_symbols_parity.sh runs the lazy and eager parity unit test on seven small binaries built for the loader's corner cases: split debuginfo whose function section is NOBITS in the debug file, a shared library with .dynsym-only symbols and aliases, a stripped library with .gnu_debugdata, and on x86_64 an IFUNC behind an IRELATIVE PLT slot, the same with the IFUNC and its aliases exported, an exported function followed in .symtab by a zero-size label at its address, and exported functions with nested entry points.
lazy_load_symbols.sh records a small callchain profile and checks that perf script and perf report, with and without call graphs, print the same output with and without --lazy-load-symbols. As root, it also compares perf script output for a process in another mount namespace, with the binary added to the build-id cache.
Both tests skip a case, rather than fail it, when the toolchain produced a file whose names perf loads eagerly anyway (PPC64 ELFv1 .opd, .gnu_debugdata or a compressed .strtab).
--- tools/perf/tests/shell/lazy_load_symbols.sh | 243 +++++++++++ tools/perf/tests/shell/lazy_load_symbols_parity.sh | 462 +++++++++++++++++++++ tools/perf/tests/shell/lib/lazy_names.sh | 20 + 3 files changed, 725 insertions(+)
tools/perf/tests/shell/lazy_load_symbols.sh new file +243 -0
@@ -0,0 +1,243 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Lazy symbol loading tests (exclusive) +# +# Verifies that perf script and perf report print the same output with +# --lazy-load-symbols as with the default eager loader. + +set -e + +shelldir=$(dirname "$0") +# shellcheck source=lib/perf_has_symbol.sh +. "${shelldir}"/lib/perf_has_symbol.sh +# shellcheck source=lib/lazy_names.sh +. "${shelldir}"/lib/lazy_names.sh + +testsym="test_loop" +# Printed with -vv for each symbol table loaded with lazy names. +lazy_names=': [1-9][0-9]* \.(symtab|dynsym) symbols with lazy names$' + +skip_test_missing_symbol ${testsym} + +if ! perf check feature -q libelf +then + echo "Lazy symbol loading [Skipped no libelf support]" + exit 2 +fi + +perf_path=$(readlink -f "$(command -v perf)") +err=0 +passed=0 +temp_dir=$(mktemp -d /tmp/__perf_test.lazy_load.XXXXX) +perfdata="${temp_dir}/perf.data" +eager_out="${temp_dir}/eager.out" +lazy_out="${temp_dir}/lazy.out" +lazy_err="${temp_dir}/lazy.err" +ns_pid= + +cleanup() { + if [ -n "${ns_pid}" ]; then + kill "${ns_pid}" 2> /dev/null || true + wait "${ns_pid}" 2> /dev/null || true + fi + rm -rf "${temp_dir}" + trap - EXIT TERM INT +} + +trap_cleanup() { + echo "Unexpected signal in ${FUNCNAME[1]}" + cleanup + exit 1 +} +trap trap_cleanup EXIT TERM INT + +# check_lazy_names CASE +# Return whether perf was loaded with lazy names. If not, report CASE as +# skipped when perf's symbol table cannot have them, and as failed otherwise. +check_lazy_names() { + if grep -F "${perf_path}: " "${lazy_err}" | grep -Eq "${lazy_names}" + then + return 0 + fi + if ! command -v readelf > /dev/null + then + echo "$1 [Skipped no readelf to check perf's symbol table]" + elif lazy_names_disabled "${perf_path}" + then + echo "$1 [Skipped perf loads its symbol names eagerly]" + else + echo "$1 [Failed perf has no lazy names]" + err=1 + fi + return 1 +} + +test_lazy_load_identical() { + echo "Lazy-load output matches eager loader" + + if ! perf record -o "${perfdata}" -g -- perf test -w thloop 2> /dev/null + then + echo "Lazy-load identical [Skipped record not supported]" + return 0 + fi + + if ! perf script -vv -i "${perfdata}" 2> /dev/null > "${eager_out}" || \ + ! perf script -vv --lazy-load-symbols -i "${perfdata}" \ + 2> "${lazy_err}" > "${lazy_out}" + then + echo "Lazy-load identical [Failed perf script error]" + err=1 + return + fi + # The comparison is only meaningful if something actually resolved; + # two all-[unknown] outputs would also match. Without samples in perf, + # perf is not loaded and has no lazy names either. + if ! grep -q "${testsym}" "${eager_out}" + then + echo "Lazy-load identical [Skipped no ${testsym} resolved]" + return 0 + fi + if ! cmp -s "${eager_out}" "${lazy_out}" + then + echo "Lazy-load identical [Failed output differs from eager]" + err=1 + return + fi + if ! check_lazy_names "Lazy-load identical" + then + return + fi + echo "Lazy-load identical [Success]" + passed=$((passed + 1)) +} + +test_lazy_load_report() { + local eager_report="${temp_dir}/eager.report" + local lazy_report="${temp_dir}/lazy.report" + local mode + + echo "Lazy-load perf report output matches eager loader" + + if ! grep -qs "${testsym}" "${eager_out}" + then + echo "Lazy-load report [Skipped no ${testsym} resolved]" + return 0 + fi + for mode in "--no-children -g none" "--children" + do + # shellcheck disable=SC2086 + if ! perf report -vv --stdio ${mode} -i "${perfdata}" \ + 2> /dev/null > "${eager_report}" || + ! perf report -vv --stdio ${mode} --lazy-load-symbols \ + -i "${perfdata}" 2> "${lazy_err}" > "${lazy_report}" + then + echo "Lazy-load report [Failed perf report ${mode} error]" + err=1 + return + fi + # -q would turn off the debug output, so drop the header, + # which has a random tip. + if ! cmp -s <(grep -v '^#' "${eager_report}") \ + <(grep -v '^#' "${lazy_report}") + then + echo "Lazy-load report [Failed ${mode} output differs from eager]" + err=1 + return + fi + if ! check_lazy_names "Lazy-load report" + then + return + fi + done + echo "Lazy-load report [Success]" + passed=$((passed + 1)) +} + +# perf script output for a process in another mount namespace, with the +# binary in the build-id cache, must not change with lazy names. +test_lazy_load_mount_ns() { + echo "Lazy-load parity for a process in another mount namespace" + + if [ "$(id -u)" != 0 ] || ! command -v unshare > /dev/null + then + echo "Lazy-load mount namespace [Skipped needs root and unshare]" + return 0 + fi + + local buildid_dir="${temp_dir}/buildid" + local nsdata="${temp_dir}/ns.data" + + # The workload sees an empty build-id cache; perf sees the real one. + mkdir -p "${buildid_dir}" + unshare -m --propagation private sh -c \ + "mount -t tmpfs none '${buildid_dir}' && exec perf test -w thloop 30" & + ns_pid=$! + for _ in $(seq 50) + do + if [ "$(readlink "/proc/${ns_pid}/exe" 2> /dev/null)" = "${perf_path}" ] + then + break + fi + sleep 0.1 + done + if [ "$(readlink "/proc/${ns_pid}/exe" 2> /dev/null)" != "${perf_path}" ] + then + echo "Lazy-load mount namespace [Skipped cannot start workload]" + return 0 + fi + + if ! perf record -N -o "${nsdata}" -p "${ns_pid}" -- sleep 1 2> /dev/null || + ! perf --buildid-dir "${buildid_dir}" buildid-cache -a "${perf_path}" \ + 2> /dev/null || + [ -z "$(ls -A "${buildid_dir}/.build-id" 2> /dev/null)" ] + then + echo "Lazy-load mount namespace [Skipped record or build-id cache failed]" + return 0 + fi + if ! perf --buildid-dir "${buildid_dir}" script -vv -i "${nsdata}" \ + 2> /dev/null > "${eager_out}" || + ! perf --buildid-dir "${buildid_dir}" script -vv --lazy-load-symbols \ + -i "${nsdata}" 2> "${lazy_err}" > "${lazy_out}" + then + echo "Lazy-load mount namespace [Failed perf script error]" + err=1 + return + fi + + if ! grep -q "${testsym}" "${eager_out}" + then + echo "Lazy-load mount namespace [Skipped no ${testsym} resolved]" + return 0 + fi + if ! cmp -s "${eager_out}" "${lazy_out}" + then + echo "Lazy-load mount namespace [Failed output differs from eager]" + err=1 + return + fi + # For a process in another mount namespace, only a symbol table from + # the build-id cache (origin B or D in perf report -v) has lazy names. + if ! perf --buildid-dir "${buildid_dir}" report -v --stdio --sort sym \ + -i "${nsdata}" 2> /dev/null | grep -Eq " [BD] \[\.\] ${testsym}( |$)" + then + echo "Lazy-load mount namespace [Skipped perf symbols not from the build-id cache]" + return 0 + fi + if ! check_lazy_names "Lazy-load mount namespace" + then + return + fi + echo "Lazy-load mount namespace [Success]" + passed=$((passed + 1)) +} + +test_lazy_load_identical +test_lazy_load_report +test_lazy_load_mount_ns + +cleanup +if [ "${err}" -eq 0 ] && [ "${passed}" -eq 0 ] +then + err=2 +fi +exit $err
tools/perf/tests/shell/lazy_load_symbols_parity.sh new file +462 -0
462 lines not on this page · raw message
tools/perf/tests/shell/lib/lazy_names.sh new file +20 -0
@@ -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 +}
Signature · 1 line
Git-157)
[email protected]
AI reviewSashiko has reviewed this 7-patch series and found no issues.
Sashiko has reviewed this 7-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]