perf unwind: Don't load symbols when the caller doesn't need them
v3 cover perf unwind: Don't load symbols when the caller doesn't need them
When perf only needs to know which DSOs a callchain passes through (as build-id marking does at the end of 'perf record' and in 'perf inject -b') it resolves the callchain without symbols. The DWARF unwinders ignore that and look up the symbol of every frame anyway, which loads and demangles the whole symbol table of each DSO they visit.
On arm64 every frame pointer sample also gets a short DWARF unwind to find the leaf function's caller; since v6.17 (commit 29be60c93d2d, "perf build-id: Mark DSO in sample callchains") the end of 'perf record -a --call-graph fp' loads the symbols of every user binary it sampled. On a 128-CPU arm64 host,
perf record -a -e cycles:u --call-graph fp -- sleep 2
never finishes writing perf.data, with over 1GB of heap growth in 5 seconds.
Skipping the symbol lookup isn't enough because loading the symbols also finds two things the unwinders depend on: the DSO's build-id, which libdw needs to fall back to the build-id cache, and any separate debuginfo file, through dso__symsrc_filename(), which is how both unwinders find a .debug_frame there.
1: add dso__load_symsrc(), which does the same search for debuginfo
and the build-id as symbol loading, but doesn't read any symbols
2: pass the symbols flag down to the unwinders and honour it, using
dso__load_symsrc() when symbols aren't wanted, which also makes the arm64 leaf frame caller unwind skip symbols
Testing compared v7.3-rc7 with this series, on x86 and natively on a CentOS Stream 10 arm64 host:
- The 'perf test' dwarf unwind and 'perf inject' DWARF callchain conversion tests pass on both, also in a REFCNT_CHECKING build on x86. The libdw multi-threaded unwind and addr2line inline unwinding tests pass on x86; on the arm64 host the former is skipped and the latter fails both with and without this series.
- 'perf inject -b' on an x86 'perf record --call-graph dwarf' profile of a Python workload goes from 0.19s and 69MB max RSS to 0.10s and 33MB, with the same build-ids injected.
- A test library built with -fno-asynchronous-unwind-tables, so that its CFI is only in .debug_frame, then split with 'objcopy --only-keep-debug', sits between two other DSOs in the sampled stacks. On both architectures, with the debuginfo found either through .gnu_debuglink or only in perf's build-id cache, 'perf script' output for a --call-graph dwarf profile, and on arm64 for an fp profile, is unchanged with both libdw and libunwind, and 'perf inject -b' unwinds the same frames and marks the same DSOs. v1, which only passed the flag down, lost every frame past the library in all but 5 of 4045 samples in 'perf script'.
libunwind's .debug_frame support couldn't be exercised: EL9's x86 libunwind is built without it, and on arm64 perf's libunwind support has been compiled out since v7.2, as libunwind-arm64.c checks HAVE_LIBUNWIND_ARM64_SUPPORT while Makefile.config defines HAVE_LIBUNWIND_AARCH64_SUPPORT. With that macro defined by hand, and a cast for the build error it then hits, arm64 libunwind results are also the same with and without this series. That breakage is independent of this series.
Not tested: the build-id cache fallback for DSOs in another mount namespace when the MMAP2 events don't carry build-ids.
Changes since v2:
- Instead of a separate debuginfo lookup, reuse dso__load()'s search and stop before reading symbols (dso__load_symsrc()) so candidate files are checked against the build-id, --symfs, mount namespaces and chroots are honoured, and the search is done once per DSO
- Drop moving dso__read_build_id() out of 'perf inject'
- Extend the dwarf unwind test to unwind without symbols, check that no symbols get loaded, and compare with unwinding with symbols
- Link:
https://lore.kernel.org/all/[email protected]/
Changes since v1:
- Complete re-work on approach based on feedback
- Link: https://lore.kernel.org/linux-perf-users/[email protected]/
Matthew Wood (2):
perf symbol: Add dso__load_symsrc()
perf unwind: Don't look up symbols when the caller doesn't want them
tools/perf/tests/dwarf-unwind.c | 71 ++++++++++++++++--- .../util/arm64-frame-pointer-unwind-support.c | 7 +- tools/perf/util/dso.h | 11 +++ tools/perf/util/machine.c | 7 +- tools/perf/util/symbol.c | 40 +++++++++-- tools/perf/util/symbol.h | 1 + tools/perf/util/unwind-libdw.c | 17 ++++- tools/perf/util/unwind-libdw.h | 1 + tools/perf/util/unwind-libunwind.c | 22 ++++-- tools/perf/util/unwind.c | 7 +- tools/perf/util/unwind.h | 11 ++- 11 files changed, 164 insertions(+), 31 deletions(-)
base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
Signature · 1 line
2.53.0-Meta
PATCH v3 1/2 perf symbol: Add dso__load_symsrc()
dso__load() goes through the candidate files for a DSO, such as the DSO itself, separate debuginfo found through .gnu_debuglink, the build-id cache and /usr/lib/debug, checks their build-ids, records the one with a symbol table in dso__symsrc_filename(), and then reads and demangles that symbol table.
The DWARF unwinders depend on the first part. dso__symsrc_filename() is how they find a separate debuginfo file, for instance to use its .debug_frame, and libdw's build-id cache fallback needs the build-id that dso__load() reads when the DSO doesn't have one yet. They get both today as a side effect of loading the symbols of every frame they visit.
Add dso__load_symsrc(), which does the same search, inside the DSO's mount namespace and honouring --symfs but stops before reading any symbols. A new symsrc_loaded flag records that it ran, so the search is done once per DSO, and the DSO's symbols stay unloaded, so dso__load() still loads them when they are wanted. Kernel and perf map DSOs are skipped, as the unwinders only handle user space ELF images.
No functional change on its own; the next patch uses it.
--- tools/perf/util/dso.h | 11 +++++++++++ tools/perf/util/symbol.c | 40 ++++++++++++++++++++++++++++++++++++---- tools/perf/util/symbol.h | 1 + 3 files changed, 48 insertions(+), 4 deletions(-)
tools/perf/util/dso.h +11 -0
@@ -340,6 +340,7 @@ DECLARE_RC_STRUCT(dso) { u8 is_64_bit:1;bool sorted_by_name;bool loaded; +bool symsrc_loaded;u8 rel;char name[]; };
@@ -603,6 +604,16 @@ static inline void dso__set_loaded(struct dso *dso)RC_CHK_ACCESS (dso)->loaded = true; } +static inline bool dso__symsrc_loaded (const struct dso *dso) +{ +return RC_CHK_ACCESS (dso)->symsrc_loaded; +} + +static inline void dso__set_symsrc_loaded (struct dso *dso) +{ +RC_CHK_ACCESS (dso)->symsrc_loaded = true; +} +static inline struct mutex *dso__lock (struct dso *dso) {return &RC_CHK_ACCESS (dso)->lock;
tools/perf/util/symbol.c +36 -4
@@ -1798,7 +1798,7 @@ static int dso__find_perf_map(char *filebuf, size_t bufsz,return rc; } -int dso__load (struct dso *dso,struct map *map) +static int __dso__load (struct dso *dso,struct map *map,bool symbols) {char *name;int ret =-1 ;
@@ -1828,11 +1828,17 @@ int dso__load(struct dso *dso, struct map *map)nsinfo__mountns_enter (dso__nsinfo (dso), &nsc);/* check again under the dso->lock */ -if (dso__loaded (dso)) { +if (dso__loaded (dso) || (!symbols &&dso__symsrc_loaded (dso))) { ret =1 ; goto out; } +/* Unwinders only need ELF images, not kernel or perf map symbols. */ +if (!symbols && (dso__kernel (dso) || perfmap)) { + ret =0 ; + goto out; + } + kmod =dso__is_kmod (dso);if (dso__kernel (dso) && !kmod) {
@@ -1915,7 +1921,7 @@ int dso__load(struct dso *dso, struct map *map) }#ifdef HAVE_LIBBFD_SUPPORT -if (is_reg) +if (is_reg && symbols) bfdrc =dso__load_bfd_symbols (dso, name);#endif if (is_reg && bfdrc <0 )
@@ -1978,6 +1984,11 @@ int dso__load(struct dso *dso, struct map *map)if (!runtime_ss && syms_ss) runtime_ss = syms_ss; +if (!symbols) { + ret =0 ; + goto out_destroy; + } +if (syms_ss) ret =dso__load_sym (dso, map, syms_ss, runtime_ss, kmod);else
@@ -1991,6 +2002,7 @@ int dso__load(struct dso *dso, struct map *map) ret += nr_plt; } +out_destroy:for (; ss_pos >0 ; ss_pos--)symsrc__destroy (&ss_[ss_pos -1 ]); out_free:
@@ -1998,13 +2010,33 @@ int dso__load(struct dso *dso, struct map *map)if (ret <0 &&strstr (dso__name (dso)," (deleted)" ) !=NULL ) ret =0 ; out: -dso__set_loaded (dso); +if (symbols) +dso__set_loaded (dso); +dso__set_symsrc_loaded (dso);mutex_unlock (dso__lock (dso));nsinfo__mountns_exit (&nsc);return ret; } +int dso__load (struct dso *dso,struct map *map) +{ +return __dso__load (dso, map,/*symbols=*/ true); +} + +/* +* Discover the same debug images as symbol loading, since they may contain +* unwind information. Do not read symbol tables, demangle names, synthesize +* PLT symbols, or mark the DSO's symbols as loaded. +*/ +int dso__load_symsrc (struct dso *dso) +{ +if (dso__loaded (dso) ||dso__symsrc_loaded (dso)) +return 1 ; + +return __dso__load (dso,NULL ,/*symbols=*/ false); +} +/* * Always takes ownership of vmlinux when vmlinux_allocated == true, even if * it returns an error.
tools/perf/util/symbol.h +1 -0
@@ -196,6 +196,7 @@ struct ref_reloc_sym { };int dso__load (struct dso *dso,struct map *map); +int dso__load_symsrc (struct dso *dso);int dso__load_vmlinux (struct dso *dso,struct map *map,const char *vmlinux,bool vmlinux_allocated);int dso__load_vmlinux_path (struct dso *dso,struct map *map);
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH v3 1/2] perf symbol: Add dso__load_symsrc()
Sashiko has reviewed this patch and found no issues.
Quoted 19 lines
dso__load()goes through the candidate files for a DSO, such as the DSO itself, separate debuginfo found through .gnu_debuglink, the build-id cache and/usr/lib/debug, checks their build-ids, records the one with a symbol table indso__symsrc_filename(), and then reads and demangles that symbol table.The DWARF unwinders depend on the first part.
dso__symsrc_filename()is how they find a separate debuginfo file, for instance to use its .debug_frame, and libdw's build-id cache fallback needs the build-id thatdso__load()reads when the DSO doesn't have one yet. They get both today as a side effect of loading the symbols of every frame they visit.Add
dso__load_symsrc(), which does the same search, inside the DSO's mount namespace and honouring --symfs but stops before reading any symbols. A newsymsrc_loadedflag records that it ran, so the search is done once per DSO, and the DSO's symbols stay unloaded, sodso__load()still loads them when they are wanted. Kernel and perf map DSOs are skipped, as the unwinders only handle user space ELF images.
Quoted 3 lines
No functional change on its own; the next patch uses it.
Signed-off-by Matthew Wood <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v3 2/2 perf unwind: Don't look up symbols when the caller doesn't want them
In reply to Matthew WoodCommit 02b2705017d2 ("perf callchain: Allow symbols to be optional when resolving a callchain") let callers that only need maps, such as build-id marking in 'perf record' and 'perf inject', resolve callchains without loading symbols. The DWARF unwinders were left out:thread__resolve_callchain_unwind() just notes that "Not resolving symbols with an unwinder isn't currently supported", and both libdw and libunwind look up the symbol of every frame they visit. The first lookup in a DSO loads its whole symbol table and demangles every name in it.
On arm64 this happens even for frame pointer callchains, because get_leaf_frame_caller_aarch64() runs a two frame DWARF unwind for every sample to recover the leaf function's caller. Since commit 29be60c93d2d ("perf build-id: Mark DSO in sample callchains"), process_buildids() does that for every sample at the end of 'perf record', so with -a it loads and demangles the symbol tables of every user binary it sampled. A C++ binary with a pathological mangled name sent __cxa_demangle() into exponential time and memory, and
perf record -a -e cycles:u --call-graph fp -- sleep 2
never finished writing perf.data after the workload exited.
Pass a symbols flag down to the unwinders. When it is false, look up only the map of each frame and leave entry->ms.sym NULL, which unwind_entry() already handles just as add_callchain_ip() does for symbols=false. get_leaf_frame_caller_aarch64() only uses the addresses, and its caller looks up the leaf frame caller's symbol through add_callchain_ip() when it wants it, so it never asks for symbols.
The unwinders still need what loading the symbols used to find: the DSO's build-id and any separate debuginfo, through dso__symsrc_filename(). Without symbols, they get those from dso__load_symsrc(). libdw does that before reporting each module, and both its report_module() and entry() follow the flag, so with symbols the module's symbols are still loaded before libdw first asks __find_debuginfo() for its debuginfo, which it does while unwinding the frame to find out whether it is an activation. libunwind does it in entry(), where it used to load the symbols. The frames returned are unchanged.
The dwarf unwind test now unwinds without symbols first, checking that no symbols get loaded, and then checks that unwinding with symbols returns the same addresses and maps.
With libdw, 'perf inject -b' on an x86 'perf record --call-graph dwarf' profile of a Python workload goes from 0.19s and 69MB max RSS to 0.10s and 33MB, with the same build-ids injected.
--- tools/perf/tests/dwarf-unwind.c | 71 ++++++++++++++++--- .../util/arm64-frame-pointer-unwind-support.c | 7 +- tools/perf/util/machine.c | 7 +- tools/perf/util/unwind-libdw.c | 17 ++++- tools/perf/util/unwind-libdw.h | 1 + tools/perf/util/unwind-libunwind.c | 22 ++++-- tools/perf/util/unwind.c | 7 +- tools/perf/util/unwind.h | 11 ++- 8 files changed, 116 insertions(+), 27 deletions(-)
tools/perf/tests/dwarf-unwind.c +63 -8
@@ -15,6 +15,7 @@#include "map.h" #include "symbol.h" #include "thread.h" +#include "dso.h" #include "callchain.h" /* For bsearch. We try to unwind functions in shared object. */
@@ -53,9 +54,38 @@ int test__dwarf_unwind(struct test_suite *test, int subtest);#define MAX_STACK 8 +struct unwind_test_data { +unsigned long cnt; +unsigned long nr; +u64 ips[MAX_STACK ]; +struct map *maps[MAX_STACK ]; +bool check_unloaded; +}; + +static int unwind_entry_maps (struct unwind_entry *entry,void *arg) +{ +struct unwind_test_data *data = arg; +unsigned long idx = data->cnt; + +if (idx >=MAX_STACK || entry->ms.sym || !entry->ms.map) { +pr_debug ("failed: invalid entry without symbols\n" ); +return -1 ; + } +if (data->check_unloaded &&dso__loaded (map__dso (entry->ms.map))) { +pr_debug ("failed: unwinding loaded symbols\n" ); +return -1 ; + } + + data->ips[idx] = entry->ip; + data->maps[idx] =map__get (entry->ms.map); + data->cnt++; +return 0 ; +} +static int unwind_entry (struct unwind_entry *entry,void *arg) { -unsigned long *cnt = (unsigned long *) arg; +struct unwind_test_data *data = arg; +unsigned long *cnt = &data->cnt;char *symbol = entry->ms.sym ? entry->ms.sym->name :NULL ;static const char *funcs[MAX_STACK ] = {"test__arch_unwind_sample" ,
@@ -85,16 +115,26 @@ static int unwind_entry(struct unwind_entry *entry, void *arg)return -1 ; } - (*cnt)++; +if (*cnt >= data->nr || entry->ip != data->ips[*cnt] || + !RC_CHK_EQUAL (entry->ms.map, data->maps[*cnt])) { +pr_debug ("failed: unwinding with symbols changed the callchain\n" ); +return -1 ; + } +pr_debug ("got: %s 0x%" PRIx64", expecting %s\n" , symbol, entry->ip, funcs[idx]); -return strcmp ((const char *) symbol, funcs[idx]); +if (strcmp (symbol, funcs[idx])) +return -1 ; + (*cnt)++; +return 0 ; } NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__thread(struct thread *thread) {struct perf_sample sample; -unsigned long cnt =0 ; +struct unwind_test_data data = { + .check_unloaded = callchain_param.order ==ORDER_CALLER , + };int err =-1 ;perf_sample__init (&sample,/*all=*/ true);
@@ -103,17 +143,32 @@ NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__thread(struct thread *thr goto out; } - err =unwind__get_entries (unwind_entry, &cnt, thread, - &sample,MAX_STACK , false); +/* Resolve maps first so symbol loading cannot hide a regression. */ + err =unwind__get_entries (unwind_entry_maps, &data, thread, + &sample,MAX_STACK ,/*symbols=*/ false, +/*best_effort=*/ false); +if (err || data.cnt !=MAX_STACK ) { +pr_debug ("unwind without symbols failed\n" ); + err =-1 ; + goto out; + } + + data.nr = data.cnt; + data.cnt =0 ; + err =unwind__get_entries (unwind_entry, &data, thread, + &sample,MAX_STACK ,/*symbols=*/ true, +/*best_effort=*/ false);if (err)pr_debug ("unwind failed\n" ); -else if (cnt !=MAX_STACK ) { +else if (data.cnt !=MAX_STACK ) {pr_debug ("got wrong number of stack entries %lu != %d\n" , - cnt,MAX_STACK ); + data.cnt,MAX_STACK ); err =-1 ; } out: +for (unsigned long i =0 ; i <MAX_STACK ; i++) +map__put (data.maps[i]);zfree (&sample.user_stack.data);zfree (&sample.user_regs->regs);perf_sample__exit (&sample);
tools/perf/util/arm64-frame-pointer-unwind-support.c +6 -1
@@ -67,7 +67,12 @@ u64 get_leaf_frame_caller_aarch64(struct perf_sample *sample, struct thread *thr regs->cache_regs[PERF_REG_ARM64_SP ] =0 ; } - ret =unwind__get_entries (add_entry, &entries, thread, sample,2 , true); +/* +* Only the addresses are used. The caller looks up the leaf frame +* caller's symbol itself if it wants it, so don't load symbols here. +*/ + ret =unwind__get_entries (add_entry, &entries, thread, sample,2 , +/*symbols=*/ false,/*best_effort=*/ true);memcpy (regs, &old_regs,sizeof (*regs));if (ret || entries.length !=2 )
tools/perf/util/machine.c +2 -5
@@ -3061,11 +3061,8 @@ static int thread__resolve_callchain_unwind(struct thread *thread, !sample->user_stack.size)return 0 ; -if (!symbols) -pr_debug ("Not resolving symbols with an unwinder isn't currently supported\n" ); - -return unwind__get_entries (unwind_entry, cursor, - thread, sample, max_stack, false); +return unwind__get_entries (unwind_entry, cursor, thread, sample, + max_stack, symbols,/*best_effort=*/ false); } int __thread__resolve_callchain(struct thread *thread,
tools/perf/util/unwind-libdw.c +14 -3
@@ -73,11 +73,17 @@ static int __report_module(struct addr_location *al, u64 ip,Dwfl_Module *mod;struct dso *dso =NULL ;Dwarf_Addr base; +/* -* Some callers will use al->sym, so we can't just use the -* cheaper thread__find_map() here. +* Callers that want symbols will use al->sym. Loading the symbols +* also finds the DSO's build-id and any separate debuginfo, which +* are needed below and by __find_debuginfo(), so look for those +* without loading the symbols when they aren't wanted. */ -thread__find_symbol (ui->thread,PERF_RECORD_MISC_USER , ip, al); +if (ui->symbols) +thread__find_symbol (ui->thread,PERF_RECORD_MISC_USER , ip, al); +else +thread__find_map (ui->thread,PERF_RECORD_MISC_USER , ip, al);if (al->map) dso =map__dso (al->map);
@@ -85,6 +91,9 @@ static int __report_module(struct addr_location *al, u64 ip,if (!dso)return 0 ; +if (!ui->symbols) +dso__load_symsrc (dso); +/* * The generated JIT DSO files only map the code segment without * ELF headers. Since JIT codes used to be packed in a memory
@@ -361,6 +370,7 @@ int libdw__get_entries(unwind_entry_cb_t cb, void *arg,struct thread *thread, struct perf_sample *data, int max_stack, + bool symbols, bool best_effort) { struct maps *maps = thread__maps(thread);
@@ -389,6 +399,7 @@ int libdw__get_entries(unwind_entry_cb_t cb, void *arg, .max_stack = max_stack, .e_machine = e_machine, .e_flags = e_flags, + .symbols = symbols, .best_effort = best_effort };
tools/perf/util/unwind-libdw.h +1 -0
@@ -22,6 +22,7 @@ struct unwind_info {int idx;uint32_t e_flags;uint16_t e_machine; +bool symbols;bool best_effort;struct unwind_entry entries[]; };
tools/perf/util/unwind-libunwind.c +17 -5
@@ -589,14 +589,26 @@ void unwind__finish_access(struct maps *maps)libunwind_arch__finish_access (maps); } -static int entry (uint64_t ip,struct thread *thread,unwind_entry_cb_t cb,void *arg) +static int entry (uint64_t ip,struct thread *thread,bool symbols, +unwind_entry_cb_t cb,void *arg) {struct unwind_entry e;struct addr_location al;int ret;addr_location__init (&al); - e.ms.sym =thread__find_symbol (thread,PERF_RECORD_MISC_USER , ip, &al); +if (symbols) { +thread__find_symbol (thread,PERF_RECORD_MISC_USER , ip, &al); + }else { +/* +* Still find the DSO's separate debuginfo, which loading +* the symbols would have, for read_unwind_spec_debug_frame(). +*/ +thread__find_map (thread,PERF_RECORD_MISC_USER , ip, &al); +if (al.map) +dso__load_symsrc (map__dso (al.map)); + } + e.ms.sym = al.sym; e.ip = ip; e.ms.map = al.map; e.ms.thread =thread__get (al.thread);
@@ -614,7 +626,7 @@ static int entry(uint64_t ip, struct thread *thread, unwind_entry_cb_t cb, voidint libunwind__get_entries (unwind_entry_cb_t cb,void *arg,struct thread *thread,struct perf_sample *sample,int max_stack, -bool best_effort) +bool symbols,bool best_effort) {struct unwind_info *ui;uint64_t first_ip;
@@ -640,7 +652,7 @@ int libunwind__get_entries(unwind_entry_cb_t cb, void *arg,if (max_stack ==1 ) {/* Special case for a single entry. */ - ret =entry (first_ip, thread, cb, arg); + ret =entry (first_ip, thread, symbols, cb, arg);return ret ? (ret == -ENOMEM ? -ENOMEM :0 ) :1 ; }
@@ -662,7 +674,7 @@ int libunwind__get_entries(unwind_entry_cb_t cb, void *arg,int j = callchain_param.order ==ORDER_CALLEE ? i : ui->cur_ip - i -1 ;if (ui->ips[j]) { - ret =entry (ui->ips[j], thread, cb, arg); + ret =entry (ui->ips[j], thread, symbols, cb, arg);if (ret)break ; entries++;
tools/perf/util/unwind.c +5 -2
@@ -10,6 +10,7 @@ int unwind__get_entries(unwind_entry_cb_t cb __maybe_unused, void *arg __maybe_ustruct thread *thread__maybe_unused , struct perf_sample *data __maybe_unused, int max_stack __maybe_unused, + bool symbols __maybe_unused, bool best_effort __maybe_unused) { int ret = 0;
@@ -29,10 +30,12 @@ int unwind__get_entries(unwind_entry_cb_t cb __maybe_unused, void *arg __maybe_ufor (size_t i =0 ; i <ARRAY_SIZE (symbol_conf.unwind_style); i++) {switch (symbol_conf.unwind_style[i]) {case UNWIND_STYLE_LIBDW : - ret =libdw__get_entries (cb, arg, thread, data, max_stack, best_effort); + ret =libdw__get_entries (cb, arg, thread, data, max_stack, + symbols, best_effort);break ;case UNWIND_STYLE_LIBUNWIND : - ret =libunwind__get_entries (cb, arg, thread, data, max_stack, best_effort); + ret =libunwind__get_entries (cb, arg, thread, data, max_stack, + symbols, best_effort);break ;case UNWIND_STYLE_UNKNOWN :default :
tools/perf/util/unwind.h +8 -3
@@ -23,6 +23,9 @@ int unwind__configure(const char *var, const char *value, void *cb);int unwind__option (const struct option *opt,const char *arg,int unset);/* +* When symbols is false, only the map of each frame is looked up and +* entry->ms.sym is left NULL, so that no symbol tables are loaded. +* * When best_effort is set, don't report errors and fail silently. This could * be expanded in the future to be more permissive about things other than * error messages.
@@ -30,19 +33,20 @@ int unwind__option(const struct option *opt, const char *arg, int unset);int unwind__get_entries (unwind_entry_cb_t cb,void *arg,struct thread *thread,struct perf_sample *data,int max_stack, -bool best_effort); +bool symbols,bool best_effort);#ifdef HAVE_LIBDW_SUPPORT int libdw__get_entries (unwind_entry_cb_t cb,void *arg,struct thread *thread,struct perf_sample *data,int max_stack, -bool best_effort); +bool symbols,bool best_effort);#else #include "debug.h" static inline int libdw__get_entries (unwind_entry_cb_t cb__maybe_unused ,void *arg__maybe_unused ,struct thread *thread__maybe_unused ,struct perf_sample *data__maybe_unused ,int max_stack__maybe_unused , +bool symbols__maybe_unused ,bool best_effort__maybe_unused ) {pr_warning_once ("Error: libdw dwarf unwinding not built into perf\n" );
@@ -55,7 +59,7 @@ static inline int libdw__get_entries(unwind_entry_cb_t cb __maybe_unused, void *int libunwind__get_entries (unwind_entry_cb_t cb,void *arg,struct thread *thread,struct perf_sample *data,int max_stack, -bool best_effort); +bool symbols,bool best_effort);int unwind__prepare_access (struct maps *maps,uint16_t e_machine);void unwind__flush_access (struct maps *maps);void unwind__finish_access (struct maps *maps);
@@ -66,6 +70,7 @@ static inline int libunwind__get_entries(unwind_entry_cb_t cb __maybe_unused, struct thread *thread __maybe_unused, struct perf_sample *data __maybe_unused, int max_stack __maybe_unused, + bool symbols __maybe_unused, bool best_effort __maybe_unused) { pr_warning_once("Error: libunwind dwarf unwinding not built into perf\n");
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH v3 2/2] perf unwind: Don't look up symbols when the caller doesn't want them
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf unwind: dso__load_symsrc() in entry() runs after libunwind stepping --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf unwind: dso__load_symsrc() in entry() runs after libunwind stepping --
commit a0b5e533925de51fda6cc2ade28ec95e77a4ee79
Author: Matthew Wood <[email protected]>
perf unwind: Don't look up symbols when the caller doesn't want them
This commit passes a symbols flag down to the DWARF unwinders (libdw and libunwind) so callers that only need maps can unwind callchains without loading and demangling DSO symbol tables. When symbols is false, the unwinders call dso__load_symsrc() to locate build-ids and separate debuginfo without loading symbols.
Quoted 52 lines
tools/perf/util/unwind-libunwind.c +16
-4@@ -589,14 +589,26 @@ void unwind__finish_access(struct maps *maps)libunwind_arch__finish_access (maps); } -static int entry (uint64_t ip,struct thread *thread,unwind_entry_cb_t cb,void *arg) +static int entry (uint64_t ip,struct thread *thread,bool symbols, +unwind_entry_cb_t cb,void *arg) {struct unwind_entry e;struct addr_location al;int ret;addr_location__init (&al); - e.ms.sym =thread__find_symbol (thread,PERF_RECORD_MISC_USER , ip, &al); +if (symbols) { +thread__find_symbol (thread,PERF_RECORD_MISC_USER , ip, &al); + }else { +/* +* Still find the DSO's separate debuginfo, which loading +* the symbols would have, for read_unwind_spec_debug_frame(). +*/ +thread__find_map (thread,PERF_RECORD_MISC_USER , ip, &al); +if (al.map) +dso__load_symsrc (map__dso (al.map)); + } + e.ms.sym = al.sym; e.ip = ip; e.ms.map = al.map; e.ms.thread =thread__get (al.thread);@@ -614,7 +626,7 @@ static int entry(uint64_t ip, struct thread *thread, unwind_entry_cb_t cb, voidint libunwind__get_entries (unwind_entry_cb_t cb,void *arg,struct thread *thread,struct perf_sample *sample,int max_stack, -bool best_effort) +bool symbols,bool best_effort) {struct unwind_info *ui;uint64_t first_ip;@@ -640,7 +652,7 @@ int libunwind__get_entries(unwind_entry_cb_t cb, void *arg,if (max_stack ==1 ) {/* Special case for a single entry. */ - ret =entry (first_ip, thread, cb, arg); + ret =entry (first_ip, thread, symbols, cb, arg);return ret ? (ret == -ENOMEM ? -ENOMEM :0 ) :1 ; }
tools/perf/util/unwind-libunwind.c int libunwind__get_entries(unwind_entry_cb_t cb, void *arg, · line 677
Quoted 5 lines
tools/perf/util/unwind-libunwind.c +1
-1@@ -662,7 +674,7 @@ int libunwind__get_entries(unwind_entry_cb_t cb, void *arg,int j = callchain_param.order ==ORDER_CALLEE ? i : ui->cur_ip - i -1 ;if (ui->ips[j]) { - ret =entry (ui->ips[j], thread, cb, arg); + ret =entry (ui->ips[j], thread, symbols, cb, arg);
[Severity: Medium]
Does calling dso__load_symsrc() inside entry() happen too late for read_unwind_spec_debug_frame() during unwinding?
In libunwind__get_entries(), all stack stepping runs to completion before entry() is called on the collected IPs:
tools/perf/util/unwind-libunwind.c:libunwind__get_entries() {
do {
ret = libunwind_arch__unwind_step(ui);
if (ret < 0)
goto out;
} while (ret);
/*
* Display what we got based on the order setup.
*/
for (i = 0; i < ui->cur_ip; i++) {
int j = callchain_param.order == ORDER_CALLEE ? i : ui->cur_ip - i - 1;
if (ui->ips[j]) {
ret = entry(ui->ips[j], thread, symbols, cb, arg);
if (ret)
break;
entries++;
}
}
}While libunwind_arch__unwind_step() is stepping the stack, __libunwind__find_proc_info() looks up the map via find_map() (which only calls thread__find_map()) and then invokes read_unwind_spec_debug_frame():
tools/perf/util/unwind-libunwind.c:__libunwind__find_proc_info() {
if (ret < 0 && !read_unwind_spec_debug_frame(dso, ui->machine, &segbase)) {
...
symfile = dso__symsrc_filename(dso) ?: dso__name(dso);
}Inside read_unwind_spec_debug_frame(), it attempts to open dso__symsrc_filename(dso):
tools/perf/util/unwind-libunwind.c:read_unwind_spec_debug_frame() {
if (ofs <= 0) {
fd = open(dso__symsrc_filename(dso), O_RDONLY);
if (fd >= 0) {
ofs = elf_section_offset(fd, ".debug_frame");
close(fd);
}
}
}When callers pass symbols=false (such as build_id__mark_dso_hit(), perf_event__inject_buildid(), get_leaf_frame_caller_aarch64(), or test_dwarf_unwind__thread()), neither dso__load() nor dso__load_symsrc() has been called before libunwind_arch__unwind_step() runs. As a result, dso__symsrc_filename(dso) is still NULL when read_unwind_spec_debug_frame() and __libunwind__find_proc_info() check it, so unwinding fails for frames that rely on .debug_frame in separate debuginfo discovered by dso__load_symsrc() (and if libunwind_arch__unwind_step() returns a negative error, the goto out path skips the entry() loop entirely).
Should dso__load_symsrc(dso) be called in __libunwind__find_proc_info() or read_unwind_spec_debug_frame() before reading dso__symsrc_filename(dso) instead of in entry()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Namhyung Kim
maintainerHello,
Quoted 23 lines · Matthew Wood
When perf only needs to know which DSOs a callchain passes through (as build-id marking does at the end of 'perf record' and in 'perf inject -b') it resolves the callchain without symbols. The DWARF unwinders ignore that and look up the symbol of every frame anyway, which loads and demangles the whole symbol table of each DSO they visit.
On arm64 every frame pointer sample also gets a short DWARF unwind to find the leaf function's caller; since v6.17 (commit
29be60c93d2d, "perf build-id: Mark DSO in sample callchains") the end of 'perf record -a --call-graph fp' loads the symbols of every user binary it sampled. On a 128-CPU arm64 host,perf record -a -e cycles:u --call-graph fp -- sleep 2never finishes writing perf.data, with over 1GB of heap growth in 5 seconds.
Skipping the symbol lookup isn't enough because loading the symbols also finds two things the unwinders depend on: the DSO's build-id, which libdw needs to fall back to the build-id cache, and any separate debuginfo file, through
dso__symsrc_filename(), which is how both unwinders find a .debug_frame there.
Quoted 5 lines
1: add
dso__load_symsrc(), which does the same search for debuginfo
and the build-id as symbol loading, but doesn't read any symbols
2: pass the symbols flag down to the unwinders and honour it, using
dso__load_symsrc()when symbols aren't wanted, which also makes the arm64 leaf frame caller unwind skip symbols
Please don't. I've added dso__dbginfo_type() to handle debug files and that's the correct way to get the file. Probably dso__debuginfo() would work for you.
I was working on cleaning up this area, but sidetracked by other things. I hope to find time for it soon but you can go with the above helpers.
Thanks,
Namhyung
Quoted 74 lines
Testing compared v7.3-rc7 with this series, on x86 and natively on a CentOS Stream 10 arm64 host:
- The 'perf test' dwarf unwind and 'perf inject' DWARF callchain conversion tests pass on both, also in a
REFCNT_CHECKINGbuild on x86. The libdw multi-threaded unwind and addr2line inline unwinding tests pass on x86; on the arm64 host the former is skipped and the latter fails both with and without this series.- 'perf inject -b' on an x86 'perf record --call-graph dwarf' profile of a Python workload goes from 0.19s and 69MB max RSS to 0.10s and 33MB, with the same build-ids injected.
- A test library built with -fno-asynchronous-unwind-tables, so that its CFI is only in .debug_frame, then split with 'objcopy --only-keep-debug', sits between two other DSOs in the sampled stacks. On both architectures, with the debuginfo found either through .gnu_debuglink or only in perf's build-id cache, 'perf script' output for a --call-graph dwarf profile, and on arm64 for an fp profile, is unchanged with both libdw and libunwind, and 'perf inject -b' unwinds the same frames and marks the same DSOs. v1, which only passed the flag down, lost every frame past the library in all but 5 of 4045 samples in 'perf script'.
libunwind's .debug_frame support couldn't be exercised: EL9's x86 libunwind is built without it, and on arm64 perf's libunwind support has been compiled out since v7.2, as
libunwind-arm64.cchecksHAVE_LIBUNWIND_ARM64_SUPPORTwhile Makefile.config definesHAVE_LIBUNWIND_AARCH64_SUPPORT. With that macro defined by hand, and a cast for the build error it then hits, arm64 libunwind results are also the same with and without this series. That breakage is independent of this series.Not tested: the build-id cache fallback for DSOs in another mount namespace when the MMAP2 events don't carry build-ids.
Changes since v2:
- Instead of a separate debuginfo lookup, reuse dso__load()'s search and stop before reading symbols (dso__load_symsrc()) so candidate files are checked against the build-id, --symfs, mount namespaces and chroots are honoured, and the search is done once per DSO
- Drop movingdso__read_build_id()out of 'perf inject'
- Extend the dwarf unwind test to unwind without symbols, check that no symbols get loaded, and compare with unwinding with symbols
- Link:https://lore.kernel.org/all/[email protected]/Changes since v1:
- Complete re-work on approach based on feedback
- Link: https://lore.kernel.org/linux-perf-users/[email protected]/Matthew Wood (2):
perf symbol: Adddso__load_symsrc()
perf unwind: Don't look up symbols when the caller doesn't want themtools/perf/tests/dwarf-unwind.c | 71 ++++++++++++++++--- .../util/arm64-frame-pointer-unwind-support.c | 7 +- tools/perf/util/dso.h | 11 +++ tools/perf/util/machine.c | 7 +- tools/perf/util/symbol.c | 40 +++++++++-- tools/perf/util/symbol.h | 1 + tools/perf/util/unwind-libdw.c | 17 ++++- tools/perf/util/unwind-libdw.h | 1 + tools/perf/util/unwind-libunwind.c | 22 ++++-- tools/perf/util/unwind.c | 7 +- tools/perf/util/unwind.h | 11 ++- 11 files changed, 164 insertions(+), 31 deletions(-)base-commit:
24a19b953e2a3d0ea5a8c4951a0ab8f32720c907Signature · 1 line
2.53.0-Meta
Quoted 37 lines · Namhyung Kim
Hello,
Quoted 23 lines · Matthew Wood
When perf only needs to know which DSOs a callchain passes through (as build-id marking does at the end of 'perf record' and in 'perf inject -b') it resolves the callchain without symbols. The DWARF unwinders ignore that and look up the symbol of every frame anyway, which loads and demangles the whole symbol table of each DSO they visit.
On arm64 every frame pointer sample also gets a short DWARF unwind to find the leaf function's caller; since v6.17 (commit
29be60c93d2d, "perf build-id: Mark DSO in sample callchains") the end of 'perf record -a --call-graph fp' loads the symbols of every user binary it sampled. On a 128-CPU arm64 host,perf record -a -e cycles:u --call-graph fp -- sleep 2never finishes writing perf.data, with over 1GB of heap growth in 5 seconds.
Skipping the symbol lookup isn't enough because loading the symbols also finds two things the unwinders depend on: the DSO's build-id, which libdw needs to fall back to the build-id cache, and any separate debuginfo file, through
dso__symsrc_filename(), which is how both unwinders find a .debug_frame there.Quoted 5 lines
1: add
dso__load_symsrc(), which does the same search for debuginfo
and the build-id as symbol loading, but doesn't read any symbols
2: pass the symbols flag down to the unwinders and honour it, using
dso__load_symsrc()when symbols aren't wanted, which also makes the arm64 leaf frame caller unwind skip symbolsPlease don't. I've added
dso__dbginfo_type()to handle debug files and that's the correct way to get the file. Probablydso__debuginfo()would work for you.
Quoted 6 lines
I was working on cleaning up this area, but sidetracked by other things. I hope to find time for it soon but you can go with the above helpers.
Thanks,
Namhyung
Hi Namhyung, thanks for the feedback. I'll look into those helpers and see about using them instead.
Can you confirm if the premise of the issue I'm reporting and trying to fix seems
valid? Would saving this unnecessary work of symbol lookup when they're unused be part of your cleanup?
Regards,
Mat
Namhyung Kim
maintainerQuoted 41 lines · Matthew Wood
Quoted 40 lines · Namhyung Kim
Hello,
Quoted 23 lines · Matthew Wood
When perf only needs to know which DSOs a callchain passes through (as build-id marking does at the end of 'perf record' and in 'perf inject -b') it resolves the callchain without symbols. The DWARF unwinders ignore that and look up the symbol of every frame anyway, which loads and demangles the whole symbol table of each DSO they visit.
On arm64 every frame pointer sample also gets a short DWARF unwind to find the leaf function's caller; since v6.17 (commit
29be60c93d2d, "perf build-id: Mark DSO in sample callchains") the end of 'perf record -a --call-graph fp' loads the symbols of every user binary it sampled. On a 128-CPU arm64 host,perf record -a -e cycles:u --call-graph fp -- sleep 2never finishes writing perf.data, with over 1GB of heap growth in 5 seconds.
Skipping the symbol lookup isn't enough because loading the symbols also finds two things the unwinders depend on: the DSO's build-id, which libdw needs to fall back to the build-id cache, and any separate debuginfo file, through
dso__symsrc_filename(), which is how both unwinders find a .debug_frame there.Quoted 5 lines
1: add
dso__load_symsrc(), which does the same search for debuginfo
and the build-id as symbol loading, but doesn't read any symbols
2: pass the symbols flag down to the unwinders and honour it, using
dso__load_symsrc()when symbols aren't wanted, which also makes the arm64 leaf frame caller unwind skip symbolsPlease don't. I've added
dso__dbginfo_type()to handle debug files and that's the correct way to get the file. Probablydso__debuginfo()would work for you.I was working on cleaning up this area, but sidetracked by other things. I hope to find time for it soon but you can go with the above helpers.
Quoted 6 lines
Quoted 3 lines
Thanks,
NamhyungHi Namhyung, thanks for the feedback. I'll look into those helpers and see about using them instead.
Great.
Quoted 5 lines
Can you confirm if the premise of the issue I'm reporting and trying to fix seems
valid? Would saving this unnecessary work of symbol lookup when they're unused be part of your cleanup?
Basically there are 3 main use cases for DSO.
1. symbol table - most analysis need symbol names
2. binary contents - for perf annotate or instruction analysis
3. debug information - srcline, inlined symbols, stack unwinding, ...
Due to split files, those information can be found in different files. So we now have 3 types of binary type info to handle the cases independently. New helpers will give you a write path name for your use case already.
Thanks,
Namhyung