From nobody Fri Sep 25 06:47:05 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B904E4C4F7C; Tue, 15 Sep 2026 18:43:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789497807; cv=none; b=A+i4V7MvPoO+TbiVJBuRfFgfZI5qELwa1WIhJQBvSNH98L7yKJ+uoBSA2FfDREXR8V90WRxHZ3H8R94xp3QkcEqTIy1HdvjqotCMMBt2fI+KMpO0skBE3Zz695uGb8TIuqFI54+tUGtRn/hVBwLl9XEZNclQ0qfaHWuUpCB+js4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789497807; c=relaxed/simple; bh=/dIazRIjw4I+09Yy6gtE4cbjra2g1GCXOKQikqvST/4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TX9/veF+ypR6dYOK8m3wmPQW1lTLeCmAj0acqjrMc+QDo4vvg9DUjumxx/xNY3lcJYd9r7wAnR/MFf7KjnsjlJKjZs7ENkwDR1JSMG+etn2TiYiiIlGo/cWmlawPx8EVjqyN21kH3C+rAHqMIT3bRIYDIjAJav8r0GjPFikSkMQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CgZUUUie; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CgZUUUie" Received: by smtp.kernel.org (Postfix) with ESMTPS id 5FE0FC2BCFB; Tue, 15 Sep 2026 18:43:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1789497807; bh=/dIazRIjw4I+09Yy6gtE4cbjra2g1GCXOKQikqvST/4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=CgZUUUieTH+mI//2+iZhDk2c7hZv07VPljAUCx5S7yF/E/TqLdxD99Yo8blDJXdra 7Zs/FcovmbqfXiZ32imeTRxYersaBDlufapcWXU4gkDG+GCWKpErLnIFjzmY8xjtJO 6zEc0oH1s4purt2pv/K8gc4D8SIB8ftN2lCO4GbX2XFloMK44gshUvYOzB5aXudHve T2n8gmhf9OFvpb1rumMXWeUyZ9iiHYHfcmIv7tWjp8AFombStRgchfdvsiu0b2+VBL ymTtjnbyEEdmiBIGjAjCmi9bZApKVi5WUaaBkRUtMKvmQfNd8/sb7JaDB0bklbbORM qpqT9YfCyJR6Q== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3FFDFC88E75; Tue, 15 Sep 2026 18:43:27 +0000 (UTC) From: Alireza Haghdoost via B4 Relay Date: Tue, 15 Sep 2026 11:42:43 -0700 Subject: [PATCH 1/4] perf symbols: Fix broken ELF_C_READ_MMAP fallback guard Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260915-perf-symbol-memory-send-v1-1-1d3360e21f07@uber.com> References: <20260915-perf-symbol-memory-send-v1-0-1d3360e21f07@uber.com> In-Reply-To: <20260915-perf-symbol-memory-send-v1-0-1d3360e21f07@uber.com> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Andrii Nakryiko , Alexei Starovoitov Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Alireza Haghdoost X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1591; i=haghdoost@uber.com; h=from:subject:message-id; bh=LDEl2QVI/AYSvnwqi8o+0DSPTV8sI0VoVdKogCTfNxc=; b=owEBiQJ2/ZANAwAIAVNlBDxl2ALYAcsmYgBqqZHNgzZ/cAO9jMToADlL/aGKgYXyBbsp9BakV 5moWgzgEWuJAk8EAAEIADkWIQS5+sFL3gX/8PrA0S1TZQQ8ZdgC2AUCaqmRzRsUgAAAAAAEAA5t YW51MiwyLjUrMS4xMiwwLDMACgkQU2UEPGXYAtj3vg//bgjLUD0a+1mbY4GQoeHz8tP0yt8Y1ga DPd5KO7zFi2SKSwsK0o0ihKCjDVd3nKlRY/PDIBsLR8oEsxrwVHvEgjZRPj1Wve7JxFmLicVMfR wgp4eeQKuUUdpEgjpLXn9RQyhMQwzScdN3vDp61c11P3QKoU9Dclo5TV2Yrz8I7SdeOAnrzqp95 XCEZ/RdvfZ5s3oNEs37vUtOzGEzX2rnZlwT7bzfK8geqoO+hmJZhPm+ujfcf8animO06VuuS9Ij OhmIiPS4ZfGt1OuKsnrqESGe2pjU+l2e6IDfFOhqdoosgzpXBUIguWVaNI5aztI0yzAyRrZqK2+ BxHzhFMfHkzsAf3gaOrWTrQK8YzZWgndROKRQ3lLb8z/FWhF1i6xW/9vkdRWuV1xhjUU4NYtrXG KoCWe0OYjMZD0W8FcpCcjRS9uVAJniwGRrCm4oK4B6i2b5q7bMhP9ktzi44lGyKbbvhD22+AvMt Ahe4aqrN1BvT0DIRhUdijzNxIWggIHtaq16t8hZkaP3ofsz5BrS3hMIqxokHpGJgbgqhgppo8WI 0e0DDUKyW7BKmDzxKecmxOdNdVuxaJJBtTh4FbIEzMmyOYWiGbZRsIAlqJycHv4KQMjtxYAMD9j arcBqvJk4BffF5iCV/AxuJODGqx8tf2TB/rPP+kTaA7ZqWYdYQ6c= X-Developer-Key: i=haghdoost@uber.com; a=openpgp; fpr=B9FAC14BDE05FFF0FAC0D12D5365043C65D802D8 X-Endpoint-Received: by B4 Relay for haghdoost@uber.com/default with auth_id=764 X-Original-From: Alireza Haghdoost Reply-To: haghdoost@uber.com From: Alireza Haghdoost This patch makes perf mmap ELF files. This was the intended behavior, regressed by commit 22dd1ac91a77 ("tools: Remove feature-libelf-mmap feature detection"). ELF_C_READ_MMAP is an Elf_Cmd enumerator, not a preprocessor macro, so the #ifdef is false on every libelf that provides it. objtool uses ELF_C_READ_MMAP without an #ifdef guard and has been fine for five years. elf_getdata() still allocates when translation is needed (32-on-64 or cross-endian), and libelf falls back to internal reads on non-seekable fds. Fixes: 22dd1ac91a77 ("tools: Remove feature-libelf-mmap feature detection") Signed-off-by: Alireza Haghdoost Assisted-by: Kimi:K3 --- tools/perf/util/symbol.h | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index d0bac824c79c..e5cef16b240d 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -57,15 +57,8 @@ static inline bool is_livepatch_symbol(const char *str) return strstarts(str, KLP_SYM_PREFIX); } =20 -/* - * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP; - * for newer versions we can use mmap to reduce memory usage: - */ -#ifdef ELF_C_READ_MMAP -# define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP -#else -# define PERF_ELF_C_READ_MMAP ELF_C_READ -#endif +/* libelf falls back to internal reads when mmap fails (e.g. non-seekable = fd). */ +#define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP =20 #ifdef HAVE_LIBELF_SUPPORT Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, --=20 Git-155) From nobody Fri Sep 25 06:47:05 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D0EBE4C67EF; Tue, 15 Sep 2026 18:43:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789497807; cv=none; b=lm7VPDV+jQoVh988lGtn+rtxLtiQ74Yoq8sX7EtvTVb5S+rohzmmONlPCHW7TNrUyIqqHAVQBCuHDnePBR2PX+OqJ2OTC14nxhUWcU1iDlZX6AxZwNRGN6va2cXkRWNJV2FPG61UQf42q7c0BPZO5MNbx4Czdbwpbidl+YnKsqs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789497807; c=relaxed/simple; bh=Vm9IxCQNKUZeBnEWC4b4icLjW7nJhQlUJhzoBAoyx6U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=iUpOhjAGEPynepw8HXyCUkZYU7/5j9jN1vih4R6+BXkMBkObCgF39GyRPA3eLQgHdlx2Uwjv5P7y7uwlwD+pLdeFdeB66mNhbLVEnJ+djyssVurha9ZAynM9k2rxrM4Zc4ESHixJoIj6kYQbakKT9O4yyFs9sZFPAaoP/7sYf1E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=j9CQ4nXk; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="j9CQ4nXk" Received: by smtp.kernel.org (Postfix) with ESMTPS id 6EDC0C2BCF5; Tue, 15 Sep 2026 18:43:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1789497807; bh=Vm9IxCQNKUZeBnEWC4b4icLjW7nJhQlUJhzoBAoyx6U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=j9CQ4nXkQdY0XMevyPf72zx4ctqWxmlL+atr6LUYswZERzBEcrlS1J7T9c+7wfDYK GjKvskBgQxrhfbCZiF2ZvTK0eF8D7MDaha4nrNsiyx4y+2drQ3z1lXlymeFyXS6+PK 3wHpJvavR7BQ7T1UE5FSDvQCnu7dPd+gAvw32UHqQj4vWuD8JI8+t2QOx5qAyP+cUp pvEkzrKVPRtK4Opf1FPDyCiHANukN8dSD2yyXw44EgZxES7xgBcHXG96ILFGrV/EV3 5Aixczg3HXxsxKH/YnuDnxNlNITulaHwG376r/h1a16kUp+VIskmvnzTAjY6pGkTQk VTv4wGe8T3AEw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4E306C88E7D; Tue, 15 Sep 2026 18:43:27 +0000 (UTC) From: Alireza Haghdoost via B4 Relay Date: Tue, 15 Sep 2026 11:42:44 -0700 Subject: [PATCH 2/4] perf script: Add --max-symbol-bytes to bound ELF symbol memory Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260915-perf-symbol-memory-send-v1-2-1d3360e21f07@uber.com> References: <20260915-perf-symbol-memory-send-v1-0-1d3360e21f07@uber.com> In-Reply-To: <20260915-perf-symbol-memory-send-v1-0-1d3360e21f07@uber.com> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Andrii Nakryiko , Alexei Starovoitov Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Alireza Haghdoost X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=7050; i=haghdoost@uber.com; h=from:subject:message-id; bh=ROWk7pABVt3iw22u/peSOyqFXvN2NxseymMNxuWT3tI=; b=owEBiQJ2/ZANAwAIAVNlBDxl2ALYAcsmYgBqqZHNdWMtxhEN9pZYvvKe+/n1ivHsf4/ShMTJa n4K/RQOvjGJAk8EAAEIADkWIQS5+sFL3gX/8PrA0S1TZQQ8ZdgC2AUCaqmRzRsUgAAAAAAEAA5t YW51MiwyLjUrMS4xMiwwLDMACgkQU2UEPGXYAthPvg/+IpzFsdAxb8DYmCgISsUuFMJMTXyoMj5 n52tfl7YV4Q/RWnRo10Wnlj4gusYSHdxKc4iN1YD7R2zIhGP1y7xkRr+gWphVZPxeAthql8gG/9 W7FS7UkuHYHtcOBZ0aG6WLXQmVTrwMt63M3Ne2uIYyt6/Q9YOeWeJ9LLAFXou/D2zDQ3mMLAUUl d+FhdI47Aux5QUiIeXDXUaR7p3wdLDBAMZ1W/j52h7gdQJSKoDvTSmi9OfWsPUSZBrSJwOttzNz EptE8AV7ee0NtQnAEZoiiegwtajZ4m3Jh6DhO1jTpB5M+FDi9G+DpU+wvkeEmaPXlcpV1tUIw26 4zO3KiAm0yF/xgUGhZoWG1BUrKYtWRZlwNHtSHWExfWMc9t7hK12FSa/IETNtsNXm3+iuFyx6Ud +qvDFSjfSc+14Fxs+oVN6Fq2PRdHEIidEvsDPfkGwfpOGTiwSsznDaRHWzP4vaYBvTdannC/vRL p6zDo8LvxNMIyfEygy+X3dBP+S4eX1eFf6yGblcNzwYjP/kYRULJSm5VSKVa592hLuZzvgWMkwE Us1VI2dEkiwcfXSR91KuaCnK2LWLzR9cI+7HfhvtdJXCrQVRu71zRYtY9PdNOKD3SyHs1EghoTa xY9VOkbg598Zxiw+p7fIntm6NoSeDZPiFJTwxQp4WFMzsrCC2I10= X-Developer-Key: i=haghdoost@uber.com; a=openpgp; fpr=B9FAC14BDE05FFF0FAC0D12D5365043C65D802D8 X-Endpoint-Received: by B4 Relay for haghdoost@uber.com/default with auth_id=764 X-Original-From: Alireza Haghdoost Reply-To: haghdoost@uber.com From: Alireza Haghdoost perf script eagerly materializes every ELF symbol into an rb-tree kept until process exit. Large profiles can therefore consume substantial anonymous memory, causing perf script to be OOM-killed or forcing the kernel to reclaim memory from co-located workloads. This patch adds --max-symbol-bytes to bound struct symbol allocations. Once the budget is reached, the ELF loader stops loading symbols, warns once, and lets unresolved addresses appear as [unknown]. This allows users to bound the memory footprint upfront and explicitly choose between complete symbolization and avoiding unbounded host memory pressure. perf record already provides a similar --max-size option to bound disk usage. The counter includes every symbol__new() allocation, but this patch enforces the limit only in the ELF loader, which is the source of the unbounded memory growth addressed here. In this path, reaching the limit can safely produce [unknown] symbols. Other loaders currently treat a failed symbol allocation as an error. Capping those paths would therefore require separate changes whose complexity may outweigh the potential memory savings. Sizes require a B/K/M/G suffix. Zero, the default, means unlimited. Signed-off-by: Alireza Haghdoost Assisted-by: Kimi:K3 --- tools/perf/builtin-script.c | 31 +++++++++++++++++++++++++++++++ tools/perf/util/symbol-elf.c | 7 +++++++ tools/perf/util/symbol.c | 29 +++++++++++++++++++++++++++-- tools/perf/util/symbol.h | 3 +++ tools/perf/util/symbol_conf.h | 1 + 5 files changed, 69 insertions(+), 2 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index ad8ca08ceb5f..50fa6ca6455a 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -68,6 +68,7 @@ #include "util/thread.h" #include "util/thread_map.h" #include "util/time-utils.h" +#include "util/units.h" #include "util/tool.h" #include "util/trace-event.h" #include "util/unwind.h" @@ -4035,6 +4036,33 @@ static int parse_callret_trace(const struct option *= opt __maybe_unused, return 0; } =20 +static int parse_max_symbol_bytes(const struct option *opt, + const char *str, int unset) +{ + unsigned long *max_bytes =3D (unsigned long *)opt->value; + static struct parse_tag size_tags[] =3D { + { .tag =3D 'B', .mult =3D 1 }, + { .tag =3D 'K', .mult =3D 1 << 10 }, + { .tag =3D 'M', .mult =3D 1 << 20 }, + { .tag =3D 'G', .mult =3D 1 << 30 }, + { .tag =3D 0 }, + }; + unsigned long bytes; + + if (unset) { + *max_bytes =3D 0; + return 0; + } + + bytes =3D parse_tag_value(str, size_tags); + if (bytes !=3D (unsigned long)-1) { + *max_bytes =3D bytes; + return 0; + } + + return -1; +} + int cmd_script(int argc, const char **argv) { bool show_full_info =3D false; @@ -4135,6 +4163,9 @@ int cmd_script(int argc, const char **argv) "Set the maximum stack depth when parsing the callchain, " "anything beyond the specified depth will be ignored. " "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STA= CK_DEPTH)), + OPT_CALLBACK(0, "max-symbol-bytes", &symbol_conf.max_symbol_bytes, + "size", "Limit bytes for ELF struct symbol (e.g. 128M; 0=3Dunlimite= d)", + parse_max_symbol_bytes), OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"), OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to pre= vious 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 e955c3feddcd..914e42d21f70 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1634,6 +1634,13 @@ dso__load_sym_internal(struct dso *dso, struct map *= map, struct symsrc *syms_ss, int is_label =3D elf_sym__is_label(&sym); const char *section_name; bool used_opd =3D false; + if (symbol_conf.max_symbol_bytes && + symbol__bytes_used() >=3D symbol_conf.max_symbol_bytes) { + pr_warning_once("perf: symbol memory budget exceeded (%lu bytes), " + "remaining symbols will be [unknown]\n", + symbol_conf.max_symbol_bytes); + break; + } =20 if (!is_label && !elf_sym__filter(&sym)) continue; diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 3587ad243159..62a4f91c2f5d 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -310,14 +310,35 @@ void symbols__fixup_end(struct rb_root_cached *symbol= s, bool is_kallsyms) curr->end =3D roundup(curr->start, 4096) + 4096; } =20 +static size_t symbol_bytes_used; + +size_t symbol__bytes_used(void) +{ + return symbol_bytes_used; +} + +void symbol__account_bytes(size_t bytes) +{ + symbol_bytes_used +=3D bytes; +} + +void symbol__unaccount_bytes(size_t bytes) +{ + symbol_bytes_used -=3D bytes; +} + struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const = char *name) { size_t namelen =3D strlen(name) + 1; - struct symbol *sym =3D calloc(1, (symbol_conf.priv_size + - sizeof(*sym) + namelen)); + size_t alloc_size =3D symbol_conf.priv_size + sizeof(struct symbol) + nam= elen; + struct symbol *sym; + + sym =3D calloc(1, alloc_size); if (sym =3D=3D NULL) return NULL; =20 + symbol__account_bytes(alloc_size); + if (symbol_conf.priv_size) { if (symbol_conf.init_annotation) { struct annotation *notes =3D (void *)sym; @@ -341,6 +362,9 @@ struct symbol *symbol__new(u64 start, u64 len, u8 bindi= ng, u8 type, const char * =20 void symbol__delete(struct symbol *sym) { + size_t alloc_size =3D symbol_conf.priv_size + sizeof(struct symbol) + + sym->namelen + 1; + if (symbol_conf.priv_size) { if (symbol_conf.init_annotation) { struct annotation *notes =3D symbol__annotation(sym); @@ -348,6 +372,7 @@ void symbol__delete(struct symbol *sym) annotation__exit(notes); } } + symbol__unaccount_bytes(alloc_size); free(((void *)sym) - symbol_conf.priv_size); } =20 diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index e5cef16b240d..0d5d3792aac1 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -228,6 +228,9 @@ void symbol__elf_init(void); int symbol__annotation_init(void); =20 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const = char *name); +size_t symbol__bytes_used(void); +void symbol__account_bytes(size_t bytes); +void symbol__unaccount_bytes(size_t bytes); size_t __symbol__fprintf_symname_offs(const struct symbol *sym, const struct addr_location *al, bool unknown_as_addr, diff --git a/tools/perf/util/symbol_conf.h b/tools/perf/util/symbol_conf.h index 71f60081a85b..6a16c5badd5e 100644 --- a/tools/perf/util/symbol_conf.h +++ b/tools/perf/util/symbol_conf.h @@ -120,6 +120,7 @@ struct symbol_conf { int pad_output_len_dso; int group_sort_idx; int addr_range; + unsigned long max_symbol_bytes; DECLARE_BITMAP(parallelism_filter, MAX_NR_CPUS + 1); }; =20 --=20 Git-155) From nobody Fri Sep 25 06:47:05 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D0E0F4C67EB; Tue, 15 Sep 2026 18:43:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789497807; cv=none; b=NbWtmlBIo/gp3ab+XOJeE6nrk1YUl53udyBNsO9rqUAGjPRuXjpdJOYvY4Gx2qt4zosDLvd9Z/bBMEShEtL6BlNUM0awClnhhr710P8CmDrx3gU0k8nERfb5RK8ctqtdURHh7okWPPvfQBNl8M4bPEgMLN7BOO1jOnwd/NEk63s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789497807; c=relaxed/simple; bh=UBZKcjGed4W4LMfRj+gTRDr+YuVWqC+Td78pdEe6oVI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=I3EOaImDz+Q28KhLu+PGVRU/e6uI7KO8GPC3A1xryZ9E0EEpR72GefnPSslIxn866oumMEZXIitLjFIzOijM4je9eeVyFmPyIVvLRvz9KTUmjwB1Yuuyc8FINWpagYqUMpwnC3NddvVtW/A8cqd8JreZQSm0Hlw8nJXQMXa5CM8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=I6U/DyWt; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="I6U/DyWt" Received: by smtp.kernel.org (Postfix) with ESMTPS id 7C2FAC2BCFD; Tue, 15 Sep 2026 18:43:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1789497807; bh=UBZKcjGed4W4LMfRj+gTRDr+YuVWqC+Td78pdEe6oVI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=I6U/DyWt3B8MHVY4GxlvlpJ1RTba2Ks9/Tooxl9+H44PHxtLKlQdU2WPQzmsn99Cl zO9XZGl6wf8x/Zziz0qSyA4fJzU1na4WYOM/tB1NqjmzmpVh0SX9ScuCp8YejVPUjq 9YW62gZD4jzb7sKZbokMhlvY9Wtmprx6yHYxDQLe+54eViqvfl+5Ocj70wPxm/V9L7 vwn2MSDQDxU4WJbGGrDs+I7Tiyqaanv3tQRuC5nhWsI62QIKg9pQR/0Vgtd6qTztJC ZaTuQ/ZhrKlTRykF0JpLHrvnRHJKGOxtVKQm46OY2hAtSkvZihqCCFwu+202Ozz8+N 6i2O5eHoXLp/w== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5ED61C982C4; Tue, 15 Sep 2026 18:43:27 +0000 (UTC) From: Alireza Haghdoost via B4 Relay Date: Tue, 15 Sep 2026 11:42:45 -0700 Subject: [PATCH 3/4] perf script: Add --lazy-load-symbols for lazy symbol loading Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260915-perf-symbol-memory-send-v1-3-1d3360e21f07@uber.com> References: <20260915-perf-symbol-memory-send-v1-0-1d3360e21f07@uber.com> In-Reply-To: <20260915-perf-symbol-memory-send-v1-0-1d3360e21f07@uber.com> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Andrii Nakryiko , Alexei Starovoitov Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Alireza Haghdoost X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=30218; i=haghdoost@uber.com; h=from:subject:message-id; bh=jFw7sO3kz+mVtLHYUiqXROYZ+zzT3PvEnh/L5HxR6dw=; b=owEBiQJ2/ZANAwAIAVNlBDxl2ALYAcsmYgBqqZHO7Z0FCJQBWilgLzF4Fay7UG35dYku1GVXZ Fm2O1BqQU+JAk8EAAEIADkWIQS5+sFL3gX/8PrA0S1TZQQ8ZdgC2AUCaqmRzhsUgAAAAAAEAA5t YW51MiwyLjUrMS4xMiwwLDMACgkQU2UEPGXYAtjZTg//TyRXHPQ6ge6PPUyRtqCKRzt2ITBfOHw CtDBTsNWDq1ukdvZgzR0BnfAaNiiTizi6+mL6G8fNQz1d6IwKeT8OfoXOsxNoJqEOJWCf7c5Mll fhWDHI8jBuYHoB8a61noZQyOvnlwSFu/H+lpr7IBSZEpL/0NyAJiMCVDsfdtEG9ujcXROob2G/6 esMIR/Fg7cs9iaVPPqU4Ery+vpNW6Xzv3yHllXJqJSCqUHWVo+DgGz4Cg5jc3l1ah/uoo89y5Jn dBlaPJDDAynAZpq6MvT/WhaFgCxR11B8zpE5tQPnBZSv8bfD9h5A68bTblx3cZNBGD1ndSizaTm ATtRuwjYovKuABqKsZ5pofC7vVAm5ZlvIqertBEEWzaQtyPsgggWODSI8xQuuYn9ydxPgscQu3j 1W5Z9UviKZiD5zx12jj4ME8Hwg+XxXJm6f4pWTmH8goakCUaw3ki1tSWM8J2PqvyafoWe1HWg6Q 4XQElAFWPf95Xcwjhxs1CqIRZzBRbhE1WJJNUuccXZxHJAlM1u1NlxPiLtaR8mf4RsKUa2TV0Z2 atqxQhHXQu8NtsVFWKC4acHV/5rz/H6MJGvNy/o1w0i+a06boUG2VsTBbzaNs2cfpl+P3F8zx/u wEiljYyTDIiUPxeueYENqBumB/y1vGJTCEzgFtBkAt6JQR3BuQxY= X-Developer-Key: i=haghdoost@uber.com; a=openpgp; fpr=B9FAC14BDE05FFF0FAC0D12D5365043C65D802D8 X-Endpoint-Received: by B4 Relay for haghdoost@uber.com/default with auth_id=764 X-Original-From: Alireza Haghdoost Reply-To: haghdoost@uber.com From: Alireza Haghdoost perf script eagerly materializes eligible symbols from every DSO encountered in samples. On a production fixture, it loaded about 765k symbols to resolve about 45k distinct (DSO, symbol) frames, exceeding the memory available in a memory-constrained cgroup. This patch adds --lazy-load-symbols for userspace ELF DSOs. It builds a compact sorted index, resolves sampled addresses by binary search, reads symbol names with pread(), and caches resolved symbols in the existing rb-tree. Lazy lookup retains one CLOEXEC file descriptor per indexed DSO. If the descriptor cannot be retained, perf discards the index and eagerly loads that DSO instead. On the same fixture, peak RssAnon drops from 265 MiB to 39 MiB and wall time from 3.1 seconds to 1.85 seconds. Memory optimizations usually cost time; this one does not because lazy loading skips many unnecessary calloc() calls and demangling operations. Output was byte-identical on the tested x86-64 workloads and an aarch64 capture. Lazy loading is most effective when samples reference only a small fraction of the available symbols, such as profiles spanning many large DSOs. It still builds an index proportional to the total symbol count. Eager loading remains available for dense symbol coverage or cases requiring its broader ELF and architecture support. This does not claim full parity with the eager loader. Lazy loading supports the common userspace ELF symtab/dynsym case, with these known differences: - .gnu_debugdata merging and PPC64 .opd are not handled. - SHT_NOBITS re-reading, IFUNC PLT naming, and exact-tie alias ordering are unreachable or output-equivalent on x86-64. Signed-off-by: Alireza Haghdoost Assisted-by: Kimi:K3 --- tools/perf/builtin-script.c | 4 +- tools/perf/util/dso.c | 10 + tools/perf/util/dso.h | 29 ++ tools/perf/util/map.c | 9 +- tools/perf/util/symbol-elf.c | 560 +++++++++++++++++++++++++++++++++++= +++- tools/perf/util/symbol-minimal.c | 11 + tools/perf/util/symbol.c | 58 ++-- tools/perf/util/symbol.h | 4 + tools/perf/util/symbol_conf.h | 1 + 9 files changed, 664 insertions(+), 22 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 50fa6ca6455a..81e56378d3d2 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -4037,7 +4037,7 @@ static int parse_callret_trace(const struct option *o= pt __maybe_unused, } =20 static int parse_max_symbol_bytes(const struct option *opt, - const char *str, int unset) + const char *str, int unset) { unsigned long *max_bytes =3D (unsigned long *)opt->value; static struct parse_tag size_tags[] =3D { @@ -4166,6 +4166,8 @@ int cmd_script(int argc, const char **argv) OPT_CALLBACK(0, "max-symbol-bytes", &symbol_conf.max_symbol_bytes, "size", "Limit bytes for ELF struct symbol (e.g. 128M; 0=3Dunlimite= d)", parse_max_symbol_bytes), + OPT_BOOLEAN(0, "lazy-load-symbols", &symbol_conf.lazy_load_symbols, + "Resolve symbols lazily instead of loading full symtabs"), OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"), OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to pre= vious event"), OPT_BOOLEAN('I', "show-info", &show_full_info, diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 42bfe30a3b51..196cd27e79e2 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1755,6 +1755,16 @@ void dso__delete(struct dso *dso) =20 dso__data_close(dso); auxtrace_cache__free(RC_CHK_ACCESS(dso)->auxtrace_cache); + if (RC_CHK_ACCESS(dso)->ondemand) { + struct dso_ondemand *od =3D RC_CHK_ACCESS(dso)->ondemand; + + free(od->sorted); + symbol__unaccount_bytes(od->nr_alloc * sizeof(struct sym_idx)); + if (od->fd >=3D 0) + close(od->fd); + free(od); + RC_CHK_ACCESS(dso)->ondemand =3D NULL; + } dso_cache__free(dso); dso__free_a2l(dso); dso__free_libdw(dso); diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index 55c4aaa53c38..50c4d64e5081 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -282,6 +282,23 @@ struct dso_bpf_prog { struct perf_env *env; }; =20 +struct sym_idx { + u64 start; /* adjusted st_value (same space as sym->start) */ + u64 end; /* start + st_size, or next start if st_size=3D=3D0 */ + u32 name_off; /* symbol's st_name: offset into the strtab */ + u8 binding; + u8 type; +}; + +struct dso_ondemand { + int fd; /* kept open for pread */ + u64 strtab_offset; /* file offset of strtab section */ + u64 strtab_size; + struct sym_idx *sorted; + u32 nr_sorted; /* deduped count */ + u32 nr_alloc; /* allocated count, for accounting */ +}; + struct auxtrace_cache; =20 DECLARE_RC_STRUCT(dso) { @@ -308,6 +325,7 @@ DECLARE_RC_STRUCT(dso) { char *symsrc_filename; struct nsinfo *nsinfo; struct auxtrace_cache *auxtrace_cache; + struct dso_ondemand *ondemand; union { /* Tool specific area */ void *priv; u64 db_id; @@ -448,6 +466,16 @@ static inline void dso__set_auxtrace_cache(struct dso = *dso, struct auxtrace_cach RC_CHK_ACCESS(dso)->auxtrace_cache =3D cache; } =20 +static inline struct dso_ondemand *dso__ondemand(struct dso *dso) +{ + return RC_CHK_ACCESS(dso)->ondemand; +} + +static inline void dso__set_ondemand(struct dso *dso, struct dso_ondemand = *od) +{ + RC_CHK_ACCESS(dso)->ondemand =3D od; +} + static inline struct dso_bpf_prog *dso__bpf_prog(struct dso *dso) { return &RC_CHK_ACCESS(dso)->bpf_prog; @@ -823,6 +851,7 @@ int dso__read_binary_type_filename(const struct dso *ds= o, enum dso_binary_type t const char *root_dir, char *filename, size_t size); bool is_kernel_module(const char *pathname, int cpumode); bool dso__needs_decompress(struct dso *dso); +struct symbol *dso__find_symbol_ondemand(struct dso *dso, u64 addr); int dso__decompress_kmodule_fd(struct dso *dso, const char *name); int dso__decompress_kmodule_path(struct dso *dso, const char *name, char *pathname, size_t len); diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 41cdddc987ee..68ffa01c9f9e 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -382,10 +382,17 @@ int map__load(struct map *map) =20 struct symbol *map__find_symbol(struct map *map, u64 addr) { + struct dso *dso; + struct symbol *sym; + if (map__load(map) < 0) return NULL; =20 - return dso__find_symbol(map__dso(map), addr); + dso =3D map__dso(map); + sym =3D dso__find_symbol(dso, addr); + if (!sym && dso__ondemand(dso)) + sym =3D dso__find_symbol_ondemand(dso, addr); + return sym; } =20 struct symbol *map__find_symbol_by_name_idx(struct map *map, const char *n= ame, size_t *idx) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 914e42d21f70..e4d77e46e883 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -600,6 +601,32 @@ static int dso__synthesize_plt_got_symbols(struct dso = *dso, Elf *elf, * And always look at the original dso, not at debuginfo packages, that * have the PLT data stripped out (shdr_rel_plt.sh_type =3D=3D SHT_NOBITS). */ +static void dso__clip_ondemand_symbols_at(struct dso *dso, u64 addr) +{ + struct dso_ondemand *od =3D dso__ondemand(dso); + struct sym_idx *sym; + u32 lo =3D 0, hi, mid; + + if (!od) + return; + + hi =3D od->nr_sorted; + while (lo < hi) { + mid =3D (lo + hi) / 2; + if (od->sorted[mid].start < addr) + lo =3D mid + 1; + else + hi =3D mid; + } + + if (!lo) + return; + + sym =3D &od->sorted[lo - 1]; + if (sym->end > addr) + sym->end =3D addr; +} + int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) { uint32_t idx; @@ -623,6 +650,8 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct= symsrc *ss) if (!elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL)) return 0; =20 + dso__clip_ondemand_symbols_at(dso, shdr_plt.sh_offset); + /* * A symbol from a previous section (e.g. .init) can have been expanded * by symbols__fixup_end() to overlap .plt. Truncate it before adding @@ -1515,6 +1544,504 @@ static int dso__process_kernel_symbol(struct dso *d= so, struct map *map, return 0; } =20 +/* + * On-demand symbol loading: build a sorted in-memory index of + * (adjusted_start, end, name strtab offset, binding, type) from the ELF + * symtab, then resolve individual addresses via binary search. Symbol + * names are read from the file's string table with pread() at lookup + * time, so the steady-state footprint is only the index itself, not + * the full set of struct symbol allocations. + */ + +static void symbol_budget_warning(void) +{ + pr_warning_once("perf: symbol memory budget exceeded (%lu bytes), " + "remaining symbols will be [unknown]\n", + symbol_conf.max_symbol_bytes); +} + +static int cmp_sym_idx(const void *a, const void *b) +{ + const struct sym_idx *sa =3D a, *sb =3D b; + + if (sa->start !=3D sb->start) + return sa->start < sb->start ? -1 : 1; + /* + * qsort is not stable, so make the order total: tiebreak on + * name_off (the symbol's st_name, a strtab offset) so that an + * exact start tie keeps a deterministic first entry regardless of + * how qsort permutes equal-start aliases. + * + * Note: st_name is a strtab offset, not a symtab index, and the two + * are not monotonic -- so on an exact heuristic tie this is + * deterministic but NOT necessarily eager's symtab order (eager + * falls to arch__choose_best_symbol -> SYMBOL_A, i.e. insertion + * order). See "Known divergences" in the commit message. + */ + if (sa->name_off !=3D sb->name_off) + return sa->name_off < sb->name_off ? -1 : 1; + return 0; +} + +/* + * Return true if a symbol should be included in the on-demand index. + * This mirrors the filter used by the eager dso__load_sym_internal() + * loop: functions, objects and IFUNCs, plus STT_NOTYPE "labels" in + * text/data sections, all restricted to SHF_ALLOC sections. + */ +static bool ondemand_sym_ok(Elf *elf, Elf_Data *secstrs, + const GElf_Sym *sym, u32 sh_link, + uint16_t e_machine) +{ + Elf_Scn *sym_sec; + GElf_Shdr sym_shdr; + int is_label =3D elf_sym__is_label(sym); + const char *name; + + if (!is_label && !elf_sym__filter((GElf_Sym *)sym)) + return false; + + if (sym->st_shndx =3D=3D SHN_ABS) + return false; + + sym_sec =3D elf_getscn(elf, sym->st_shndx); + if (!sym_sec) + return false; + if (!gelf_getshdr(sym_sec, &sym_shdr)) + return false; + if (!(sym_shdr.sh_flags & SHF_ALLOC)) + return false; + + if (is_label && (!secstrs || !elf_sec__filter(&sym_shdr, secstrs))) + return false; + + name =3D elf_strptr(elf, sh_link, sym->st_name); + if (!name) + return false; + + /* + * Reject ARM/AArch64/RISC-V "mapping symbols" ($a/$d/$t/$x), as + * the eager loop does. They are zero-size STT_NOTYPE labels in + * allocated sections that would otherwise be indexed and fill + * forward over real functions, misattributing everything after + * them. + */ + if (e_machine =3D=3D EM_ARM || e_machine =3D=3D EM_AARCH64) { + if (name[0] =3D=3D '$' && strchr("adtx", name[1]) && + (name[2] =3D=3D '\0' || name[2] =3D=3D '.')) + return false; + } + if (e_machine =3D=3D EM_RISCV) { + if (name[0] =3D=3D '$' && strchr("dx", name[1])) + return false; + } + + return true; +} + +static int dso__build_ondemand_index(struct dso *dso, struct symsrc *syms_= ss, + struct symsrc *runtime_ss, + int dynsym) +{ + struct dso_ondemand *od; + Elf *elf =3D syms_ss->elf; + GElf_Ehdr ehdr =3D syms_ss->ehdr; + GElf_Shdr shdr; + GElf_Shdr strshdr; + Elf_Scn *strscn, *sec_strndx; + Elf_Data *syms; + GElf_Sym sym; + Elf_Data *secstrs =3D NULL; + size_t i; + u32 count =3D 0, j; + u64 nr_entries, strtab_offset; + + if (dynsym) + shdr =3D syms_ss->dynshdr; + else + shdr =3D syms_ss->symshdr; + + syms =3D elf_getdata(dynsym ? syms_ss->dynsym : syms_ss->symtab, NULL); + if (!syms) + return -1; + + if (!shdr.sh_entsize) + return 0; + + nr_entries =3D shdr.sh_size / shdr.sh_entsize; + if (nr_entries > UINT32_MAX) + return -EOVERFLOW; + + /* + * File offset of the string table linked from the symbol table. + * Symbol names are pread() from the file at lookup time, so we + * only need the offset and size here, not the strings themselves. + */ + strscn =3D elf_getscn(elf, shdr.sh_link); + if (!strscn || !gelf_getshdr(strscn, &strshdr)) + return -1; + strtab_offset =3D strshdr.sh_offset; + + /* + * Section name string table, used to match the eager path's + * elf_sec__filter() (text/data section check for STT_NOTYPE labels). + */ + sec_strndx =3D elf_getscn(elf, ehdr.e_shstrndx); + if (sec_strndx) + secstrs =3D elf_getdata(sec_strndx, NULL); + + /* Count symbols that pass the filter (same filter as fill below) */ + for (i =3D 0; i < nr_entries; i++) { + if (!gelf_getsym(syms, i, &sym)) + continue; + if (ondemand_sym_ok(elf, secstrs, &sym, shdr.sh_link, + ehdr.e_machine)) + count++; + } + + if (!count) + return 0; + if (count > SIZE_MAX / sizeof(*od->sorted)) + return -EOVERFLOW; + + /* + * Account the index against the symbol memory budget: at 24 + * bytes/symbol it is the dominant on-demand cost and must count + * toward --max-symbol-bytes just like struct symbol allocations do. + */ + if (symbol_conf.max_symbol_bytes && + symbol__bytes_used() + count * sizeof(struct sym_idx) > + symbol_conf.max_symbol_bytes) { + symbol_budget_warning(); + return 0; /* fall back to the eager loader's per-symbol budget */ + } + + od =3D zalloc(sizeof(*od)); + if (!od) + return -1; + + od->sorted =3D malloc(count * sizeof(*od->sorted)); + if (!od->sorted) { + free(od); + return -1; + } + od->nr_alloc =3D count; /* allocated; the deduped count may shrink */ + + /* Fill the index with adjusted addresses */ + j =3D 0; + for (i =3D 0; i < nr_entries; i++) { + u64 adjusted; + GElf_Phdr phdr; + + if (!gelf_getsym(syms, i, &sym)) + continue; + if (!ondemand_sym_ok(elf, secstrs, &sym, shdr.sh_link, + ehdr.e_machine)) + continue; + + adjusted =3D sym.st_value; + + /* ARM Thumb bit fix (same as eager path, FUNC only) */ + if ((ehdr.e_machine =3D=3D EM_ARM) && + (GELF_ST_TYPE(sym.st_info) =3D=3D STT_FUNC) && + (adjusted & 1)) + --adjusted; + + /* + * Program header adjustment, identical to the eager loop: + * read the PT_LOAD containing the symbol from the runtime + * ELF (the debug-info file may have zeroed p_offset), and + * fall back to the section-header bias when no program + * header matches -- exactly what the eager path does when + * elf_read_program_header fails. + */ + if (elf_read_program_header(runtime_ss->elf, adjusted, + &phdr) =3D=3D 0) { + adjusted -=3D phdr.p_vaddr - phdr.p_offset; + } else { + Elf_Scn *sym_sec =3D elf_getscn(elf, sym.st_shndx); + GElf_Shdr sym_shdr; + + if (sym_sec && gelf_getshdr(sym_sec, &sym_shdr)) + adjusted -=3D sym_shdr.sh_addr - sym_shdr.sh_offset; + } + + od->sorted[j].start =3D adjusted; + od->sorted[j].end =3D sym.st_size; /* st_size for now, converted later */ + od->sorted[j].name_off =3D sym.st_name; /* strtab-relative */ + od->sorted[j].binding =3D GELF_ST_BIND(sym.st_info); + od->sorted[j].type =3D GELF_ST_TYPE(sym.st_info); + j++; + } + + /* Sort by adjusted start address */ + qsort(od->sorted, count, sizeof(*od->sorted), cmp_sym_idx); + + /* Alias dedup: keep only the best symbol for each start address */ + if (!symbol_conf.allow_aliases) { + u32 out =3D 0; + + for (i =3D 0; i < count; i++) { + u32 best =3D i; + const char *na =3D NULL, *nb; + char *da =3D NULL, *db; + + /* name_off is the strtab index (st_name) */ + na =3D elf_strptr(elf, shdr.sh_link, + od->sorted[best].name_off); + if (na) { + da =3D dso__demangle_sym(dso, 0, na); + if (da) + na =3D da; + } + + /* Find the best among all entries with this start */ + for (j =3D i + 1; j < count && + od->sorted[j].start =3D=3D od->sorted[i].start; j++) { + nb =3D elf_strptr(elf, shdr.sh_link, + od->sorted[j].name_off); + if (!na || !nb) + continue; + + /* Demangle for comparison, matching eager path */ + db =3D dso__demangle_sym(dso, 0, nb); + if (db) + nb =3D db; + + /* od->sorted[].end holds st_size at this point */ + if (choose_best_symbol_raw( + od->sorted[best].end, + od->sorted[best].type, + od->sorted[best].binding, na, + od->sorted[j].end, + od->sorted[j].type, + od->sorted[j].binding, nb) =3D=3D SYMBOL_B) { + best =3D j; + free(da); + da =3D db; + na =3D nb; + } else { + free(db); + } + } + + free(da); + od->sorted[out++] =3D od->sorted[best]; + i =3D j - 1; /* skip past all aliases of this start */ + } + + if (out < count) { + struct sym_idx *shrunk; + + shrunk =3D realloc(od->sorted, out * sizeof(*od->sorted)); + if (shrunk) { + od->sorted =3D shrunk; + od->nr_alloc =3D out; + } + } + count =3D out; + } + + /* Convert st_size to end addresses */ + for (i =3D 0; i < count; i++) { + u64 size =3D od->sorted[i].end; /* was st_size */ + + if (size > 0) + od->sorted[i].end =3D od->sorted[i].start + size; + else if (i + 1 < count) + od->sorted[i].end =3D od->sorted[i + 1].start; + else + /* Match symbols__fixup_end's last-symbol formula. */ + od->sorted[i].end =3D roundup(od->sorted[i].start, 4096) + 4096; + } + + /* + * Keep a private fd open for pread() of symbol names. Dup with + * O_CLOEXEC so children don't inherit it, and so that + * symsrc__destroy() can close the original regardless of whether + * it is a real file or a temporary debugdata extraction. + * + * If the dup fails (e.g. fd exhaustion), decline by returning 0 + * without setting the index: the caller falls back to the eager + * loader so the DSO still gets symbols rather than going symbol-less. + */ + od->fd =3D fcntl(syms_ss->fd, F_DUPFD_CLOEXEC, 0); + if (od->fd < 0) { + free(od->sorted); + free(od); + return 0; + } + od->strtab_offset =3D strtab_offset; + od->strtab_size =3D strshdr.sh_size; + od->nr_sorted =3D count; + + symbol__account_bytes(od->nr_alloc * sizeof(*od->sorted)); + + dso__set_ondemand(dso, od); + + pr_debug("%s: on-demand index: %u symbols\n", + dso__long_name(dso), count); + + return 1; +} + +/* + * Read a NUL-terminated symbol name from the file's string table at + * file offset @off. The fast path uses a stack buffer; if the NUL is + * not found within it (names can exceed 1 KiB for template-heavy C++ + * mangled names), grow a heap buffer geometrically from 4 KiB, doubling + * until the terminator is found or the strtab is exhausted. This keeps + * a single long name cheap even when it sits near the start of a large + * (tens of MB) strtab, while bounding a corrupt/missing terminator by + * the remaining strtab size. + * + * Returns a pointer to the name (either @buf or a heap allocation) and + * sets *@to_free to the buffer that must be free()d (NULL for @buf). + * Returns NULL on read error or if no NUL terminator exists within the + * strtab bounds. + */ +static const char *ondemand_read_name(int fd, u64 strtab_offset, + u64 strtab_size, u64 name_off, + char *buf, size_t buflen, + char **to_free) +{ + ssize_t n; + u64 remain; + u64 file_off; + size_t cap; + + *to_free =3D NULL; + + /* name_off is strtab-relative (the symbol's st_name). */ + if (name_off >=3D strtab_size) + return NULL; + file_off =3D strtab_offset + name_off; + remain =3D strtab_size - name_off; + + /* + * Fast path: stack buffer, expect the name to fit. Cap at the + * remaining strtab so a missing terminator can't read past the + * section into adjacent file data. + */ + n =3D pread(fd, buf, min((u64)(buflen - 1), remain), file_off); + if (n <=3D 0) + return NULL; + buf[n] =3D '\0'; + if (memchr(buf, '\0', n)) + return buf; + + /* + * Slow path: the name is longer than buflen. Grow a heap buffer + * geometrically, doubling until the terminator appears, so a long + * name costs O(name length), not O(remaining strtab size). + */ + cap =3D 4096; + for (;;) { + char *tmp; + size_t want =3D cap; + + if (want > remain) + want =3D remain; + if (want =3D=3D 0) + break; + + tmp =3D *to_free ? realloc(*to_free, want + 1) : malloc(want + 1); + if (!tmp) { + free(*to_free); + *to_free =3D NULL; + return NULL; + } + *to_free =3D tmp; + + n =3D pread(fd, *to_free, want, file_off); + if (n <=3D 0) { + free(*to_free); + *to_free =3D NULL; + return NULL; + } + (*to_free)[n] =3D '\0'; + + if (memchr(*to_free, '\0', n)) + return *to_free; + + /* + * Read the whole remaining strtab (or hit EOF) with no + * terminator: corrupt file, bail instead of re-reading. + */ + if (want >=3D remain || (u64)n >=3D remain) + break; + + /* Avoid size_t overflow on absurdly large strtabs. */ + if (cap > SIZE_MAX / 2) + break; + cap *=3D 2; + } + + free(*to_free); + *to_free =3D NULL; + return NULL; +} + +struct symbol *dso__find_symbol_ondemand(struct dso *dso, u64 addr) +{ + struct dso_ondemand *od =3D dso__ondemand(dso); + u32 lo, hi, mid; + const char *name; + char namebuf[1024]; + char *name_heap =3D NULL; + char *demangled; + struct symbol *s =3D NULL; + + if (!od || !od->sorted || od->fd < 0) + return NULL; + + lo =3D 0; + hi =3D od->nr_sorted; + while (lo < hi) { + mid =3D (lo + hi) / 2; + if (addr < od->sorted[mid].start) + hi =3D mid; + else if (addr >=3D od->sorted[mid].end) + lo =3D mid + 1; + else + goto found; + } + + /* Not found */ + return NULL; + +found: + /* + * Check the budget before doing any name I/O or demangling, so an + * over-budget DSO stops paying pread+demangle on every later miss. + */ + if (symbol_conf.max_symbol_bytes && + symbol__bytes_used() >=3D symbol_conf.max_symbol_bytes) { + symbol_budget_warning(); + return NULL; + } + + name =3D ondemand_read_name(od->fd, od->strtab_offset, od->strtab_size, + od->sorted[mid].name_off, + namebuf, sizeof(namebuf), &name_heap); + if (!name) + return NULL; + + demangled =3D dso__demangle_sym(dso, 0, name); + if (demangled) + name =3D demangled; + + s =3D symbol__new(od->sorted[mid].start, + od->sorted[mid].end - od->sorted[mid].start, + od->sorted[mid].binding, + od->sorted[mid].type, name); + free(demangled); + free(name_heap); + if (s) + __symbols__insert(dso__symbols(dso), s); + return s; +} + static int dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *sy= ms_ss, struct symsrc *runtime_ss, int kmodule, int dynsym) @@ -1626,6 +2153,35 @@ dso__load_sym_internal(struct dso *dso, struct map *= map, struct symsrc *syms_ss, if (kmodule && adjust_kernel_syms) max_text_sh_offset =3D max_text_section(runtime_ss->elf, &runtime_ss->eh= dr); =20 + /* + * On-demand mode: build a sorted index and resolve symbols + * per-address instead of loading the entire symtab. + * Only for userspace ELF DSOs. + */ + if (symbol_conf.lazy_load_symbols && !dso__kernel(dso) && !kmodule) { + int oret =3D 0; + + /* Use symtab if available, fall back to dynsym (e.g. vdso) */ + if (!dynsym && syms_ss->symtab) + oret =3D dso__build_ondemand_index(dso, syms_ss, + runtime_ss, 0); + else if (dynsym && !dso__ondemand(dso) && syms_ss->dynsym) + oret =3D dso__build_ondemand_index(dso, syms_ss, + runtime_ss, 1); + + /* + * On hard error, propagate it. If an index was built, the + * DSO resolves on demand; skip the eager loop below. If the + * build declined (oret =3D=3D 0, no index -- e.g. no usable + * symbols, or fd duplication failed), continue with the eager + * loop so the DSO still gets symbols. + */ + if (oret < 0) + return oret; + if (dso__ondemand(dso)) + return 1; + } + curr_dso =3D dso__get(dso); elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { struct symbol *f; @@ -1636,9 +2192,7 @@ dso__load_sym_internal(struct dso *dso, struct map *m= ap, struct symsrc *syms_ss, bool used_opd =3D false; if (symbol_conf.max_symbol_bytes && symbol__bytes_used() >=3D symbol_conf.max_symbol_bytes) { - pr_warning_once("perf: symbol memory budget exceeded (%lu bytes), " - "remaining symbols will be [unknown]\n", - symbol_conf.max_symbol_bytes); + symbol_budget_warning(); break; } =20 diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-mini= mal.c index 0a71d1463952..1a7088389d62 100644 --- a/tools/perf/util/symbol-minimal.c +++ b/tools/perf/util/symbol-minimal.c @@ -373,6 +373,17 @@ void symbol__elf_init(void) { } =20 +/* + * Without libelf there is no on-demand index to resolve against; the + * DSO can never be in on-demand mode, so this is only reached when a + * caller ignores the NULL from dso__ondemand(). + */ +struct symbol *dso__find_symbol_ondemand(struct dso *dso __maybe_unused, + u64 addr __maybe_unused) +{ + return NULL; +} + bool filename__has_section(const char *filename __maybe_unused, const char= *sec __maybe_unused) { return false; diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 62a4f91c2f5d..7e90f3c11832 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -155,60 +155,76 @@ int __weak arch__choose_best_symbol(struct symbol *sy= ma, return SYMBOL_A; } =20 -static int choose_best_symbol(struct symbol *syma, struct symbol *symb) +int choose_best_symbol_raw(u64 a_size, u8 a_type, u8 a_binding, + const char *a_name, + u64 b_size, u8 b_type, u8 b_binding, + const char *b_name) { s64 a; s64 b; size_t na, nb; =20 /* Prefer a symbol with non zero length */ - a =3D syma->end - syma->start; - b =3D symb->end - symb->start; - if ((b =3D=3D 0) && (a > 0)) + if ((b_size =3D=3D 0) && (a_size > 0)) return SYMBOL_A; - else if ((a =3D=3D 0) && (b > 0)) + else if ((a_size =3D=3D 0) && (b_size > 0)) return SYMBOL_B; =20 - if (symbol__type(syma) !=3D symbol__type(symb)) { - if (symbol__type(syma) =3D=3D STT_NOTYPE) + if (a_type !=3D b_type) { + if (a_type =3D=3D STT_NOTYPE) return SYMBOL_B; - if (symbol__type(symb) =3D=3D STT_NOTYPE) + if (b_type =3D=3D STT_NOTYPE) return SYMBOL_A; } =20 /* Prefer a non weak symbol over a weak one */ - a =3D symbol__binding(syma) =3D=3D STB_WEAK; - b =3D symbol__binding(symb) =3D=3D STB_WEAK; + a =3D a_binding =3D=3D STB_WEAK; + b =3D b_binding =3D=3D STB_WEAK; if (b && !a) return SYMBOL_A; if (a && !b) return SYMBOL_B; =20 /* Prefer a global symbol over a non global one */ - a =3D symbol__binding(syma) =3D=3D STB_GLOBAL; - b =3D symbol__binding(symb) =3D=3D STB_GLOBAL; + a =3D a_binding =3D=3D STB_GLOBAL; + b =3D b_binding =3D=3D STB_GLOBAL; if (a && !b) return SYMBOL_A; if (b && !a) return SYMBOL_B; =20 /* Prefer a symbol with less underscores */ - a =3D prefix_underscores_count(syma->name); - b =3D prefix_underscores_count(symb->name); + a =3D prefix_underscores_count(a_name); + b =3D prefix_underscores_count(b_name); if (b > a) return SYMBOL_A; else if (a > b) return SYMBOL_B; =20 /* Choose the symbol with the longest name */ - na =3D strlen(syma->name); - nb =3D strlen(symb->name); + na =3D strlen(a_name); + nb =3D strlen(b_name); if (na > nb) return SYMBOL_A; else if (na < nb) return SYMBOL_B; =20 - return arch__choose_best_symbol(syma, symb); + /* All heuristics tied: let the caller decide (arch hook, etc.) */ + return -1; +} + +static int choose_best_symbol(struct symbol *syma, struct symbol *symb) +{ + int ret; + + ret =3D choose_best_symbol_raw(syma->end - syma->start, + symbol__type(syma), symbol__binding(syma), syma->name, + symb->end - symb->start, + symbol__type(symb), symbol__binding(symb), symb->name); + if (ret < 0) + return arch__choose_best_symbol(syma, symb); + + return ret; } =20 void symbols__fixup_duplicate(struct rb_root_cached *symbols) @@ -1940,11 +1956,19 @@ int dso__load(struct dso *dso, struct map *map) } =20 #ifdef HAVE_LIBBFD_SUPPORT +#ifdef HAVE_LIBELF_SUPPORT + if (is_reg && !symbol_conf.lazy_load_symbols) +#else if (is_reg) +#endif bfdrc =3D dso__load_bfd_symbols(dso, name); #endif if (is_reg && bfdrc < 0) sirc =3D symsrc__init(ss, dso, name, symtab_type); +#if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBELF_SUPPORT) + if (is_reg && symbol_conf.lazy_load_symbols && sirc < 0) + bfdrc =3D dso__load_bfd_symbols(dso, name); +#endif =20 if (nsexit) nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index 0d5d3792aac1..db1b662988e3 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -231,6 +231,10 @@ struct symbol *symbol__new(u64 start, u64 len, u8 bind= ing, u8 type, const char * size_t symbol__bytes_used(void); void symbol__account_bytes(size_t bytes); void symbol__unaccount_bytes(size_t bytes); +int choose_best_symbol_raw(u64 a_size, u8 a_type, u8 a_binding, + const char *a_name, + u64 b_size, u8 b_type, u8 b_binding, + const char *b_name); size_t __symbol__fprintf_symname_offs(const struct symbol *sym, const struct addr_location *al, bool unknown_as_addr, diff --git a/tools/perf/util/symbol_conf.h b/tools/perf/util/symbol_conf.h index 6a16c5badd5e..0f8d044eba20 100644 --- a/tools/perf/util/symbol_conf.h +++ b/tools/perf/util/symbol_conf.h @@ -74,6 +74,7 @@ struct symbol_conf { no_buildid_mmap2, guest_code, lazy_load_kernel_maps, + lazy_load_symbols, keep_exited_threads, annotate_data_member, annotate_data_sample, --=20 Git-155) From nobody Fri Sep 25 06:47:05 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E56A64C680A; Tue, 15 Sep 2026 18:43:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789497808; cv=none; b=NN8tqWJswomYnVMeM3fMXHVTUqQ5C3MXZ4bVKCiAXyaTUQGE2WjOllhP3OgyDeOo3CPkD9mIowx+GTawnXXLa4MOURminR36pFL/iz67N7qQ5PaqEMeVcNN5zYGepeCEzW4VNyrs0IpgS1cMHaclz2RCorInoqwXiSu3INAkiIA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789497808; c=relaxed/simple; bh=BikG+MUQZlklxvTmT8pKAp/VLf/cXClH7BY/kzM+Cg8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CziQH6von/qr0fgGaSAJRjEODdOtQEsDz1eTrn8PM+cxYNC9Cvl7hcMsVoOI7lO+KJ+J1Ptlr8m19ZKuOMUp8q0+sbGoEgrBdGZDtNCnulUxn+eCDslsQ2EB+EdUZxthDM4E4BxWK0ehT2T6dSA0JI+427XDS4PxT4XFDDF/2Sg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=q/lkv6C8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="q/lkv6C8" Received: by smtp.kernel.org (Postfix) with ESMTPS id 8646EC2BD00; Tue, 15 Sep 2026 18:43:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1789497807; bh=BikG+MUQZlklxvTmT8pKAp/VLf/cXClH7BY/kzM+Cg8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=q/lkv6C8UeQV6S/Jv5KtJRj/z1UldAEhqnAvI+eWdxXtUb6F500jkcFVDbWEzXStY 5yRb6XJ2xKpdMUEpjwXUYHj2/qoddaMYurbog5tu6M39P7uMjv2OoVX+3kmPlnWHSe VvIegvP5RHv+ZjGzwg7dbZQLLQ3jtik0OKnvCp3B0SoC8FsOC+ZbH9ZmViMeXdVsTK q9ibkOTTh22H+v+9XV8qZz3AEiNj0h4Q/W775UwVFDv0KH/dprcLVaaqX8/hF59ZBc je6K7qGSl8S8BoxYz2PwHg7GptCrUBBlbQi3JtN159ZDNx5IcSOkaxv022Ewjj0bRG 3QqdDROQOVoEw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6DB8AC982C1; Tue, 15 Sep 2026 18:43:27 +0000 (UTC) From: Alireza Haghdoost via B4 Relay Date: Tue, 15 Sep 2026 11:42:46 -0700 Subject: [PATCH 4/4] perf script: Document and test --lazy-load-symbols and --max-symbol-bytes Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260915-perf-symbol-memory-send-v1-4-1d3360e21f07@uber.com> References: <20260915-perf-symbol-memory-send-v1-0-1d3360e21f07@uber.com> In-Reply-To: <20260915-perf-symbol-memory-send-v1-0-1d3360e21f07@uber.com> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Andrii Nakryiko , Alexei Starovoitov Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Alireza Haghdoost X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=6198; i=haghdoost@uber.com; h=from:subject:message-id; bh=ozeisEeLUlyeoorFaW7r4WB+nw0aEmnGGVlEP7mmb2o=; b=owEBiQJ2/ZANAwAIAVNlBDxl2ALYAcsmYgBqqZHOO79qSbapVu7FstSxOFn6KxUcXOhoUCYQ1 bXfEiLi0MCJAk8EAAEIADkWIQS5+sFL3gX/8PrA0S1TZQQ8ZdgC2AUCaqmRzhsUgAAAAAAEAA5t YW51MiwyLjUrMS4xMiwwLDMACgkQU2UEPGXYAth0mA/+Pt0cuUTq8AeRJ2ktRGG2FnZOgPD/HtS JOqLpmcMT5dRHYsgVYt0K6XyaVNBLT1KXAaQ3UIYyWv7ikqiyd2cTFrWXlVPij2wT6ItcL5blXF pAMifxDaeljgtOE0oqwG2+Q7IO0qvF+qtXZEmlqc2qn8DLyVrjX1JXIZXgw+6OLBYWv+iraL71H 62u7atJSwkfimQlKv6yO/5xrsFrx06Tq9/lGs/KBaB/R48/lK+F9/qgp2OvwoDvOY4B/+FTbTBW hdwzzPKgCUbWrDXmwjtkWiMHlnUG9L5hXTFZeWhKU6KeSyuIoSq0R6VUXYhNrjbS+lL+8FpbnPv +pz2zEL6mq1kt4u4IHpGFiBM+azREB4ma+riwivc6rkYHfkqNeGigunSIXoahnT5pViKEtUY7+g Re7/cvBUq8uyrtsi9VGO7N1JWAbaLEgRyfZAHSir0VPw/r14h5KIUaCXP4UYEkCKxJQJvdrlTdE t7bQPszpi0I20M7MkTBd7TQOFTXWDXc3Og8UTHLqquoZZz9Iio3loEmGddBg0sDNSEyR2DNo5lu KSoiqAqTkweB9lvanN5zphq7ThJUOqHpopk3vJK01Zm+yHtJDcH8yp0InOQoORgx0rCPd+dF5Wt vrUgMgj0wQcwfmywJr4bUFfExIDOqqe3HHLAr4GNur2OSfMgWKRU= X-Developer-Key: i=haghdoost@uber.com; a=openpgp; fpr=B9FAC14BDE05FFF0FAC0D12D5365043C65D802D8 X-Endpoint-Received: by B4 Relay for haghdoost@uber.com/default with auth_id=764 X-Original-From: Alireza Haghdoost Reply-To: haghdoost@uber.com From: Alireza Haghdoost Document both new options in perf-script.txt: their interaction, the memory tradeoff, that lazy loading applies only to userspace ELF DSOs (kernel DSOs and modules always load eagerly), and that output may differ from the default loader for some targets. Add a shell test that records a small profile with callchains and asserts: - --lazy-load-symbols produces output byte-identical to the eager loader, and that at least one symbol actually resolved (so the comparison can't pass vacuously on all-[unknown] output); - --max-symbol-bytes=3D1K, with and without lazy loading, forces [unknown] resolution with a single warning and exit code 0 (no crash). Signed-off-by: Alireza Haghdoost Assisted-by: Kimi:K3 --- tools/perf/Documentation/perf-script.txt | 24 +++++ tools/perf/tests/shell/script_lazy_load_symbols.sh | 120 +++++++++++++++++= ++++ 2 files changed, 144 insertions(+) diff --git a/tools/perf/Documentation/perf-script.txt b/tools/perf/Document= ation/perf-script.txt index 200ea25891d8..b5a90ff22342 100644 --- a/tools/perf/Documentation/perf-script.txt +++ b/tools/perf/Documentation/perf-script.txt @@ -412,6 +412,30 @@ include::itrace.txt[] =20 Default: 127 =20 +--lazy-load-symbols:: + Resolve symbols lazily instead of eagerly loading the full + symbol table of every DSO that appears in a sample. A compact + sorted index is built per DSO and only the addresses that appear + in samples are materialized into symbols, with names read from the + file's string table at lookup time. This sharply reduces memory + (and usually time) for profiles of large binaries where only a + small fraction of the symbol table is referenced. This applies only + to userspace ELF DSOs; kernel DSOs and modules always load eagerly. + Output may differ from the default loader for some targets + (e.g. PPC64 .opd, .gnu_debugdata, or split debuginfo). Default: off. + +--max-symbol-bytes:: + Limit the bytes held in struct symbol allocations (and, with + --lazy-load-symbols, the lazy index) for DSOs on the ELF symbol + loader path -- userspace DSOs plus vmlinux-as-ELF and kernel + modules. This is not a cap on all symbol memory or RSS: symbols + from kallsyms, JIT maps, PLT synthesis, and libbfd are counted + but not capped. Accepts a size with a B/K/M/G suffix (e.g. 128M). + When the budget is exceeded, further symbols resolve to [unknown] + and a warning is printed. This is a safety net independent of + --lazy-load-symbols and can be used with or without it. Default: 0 + (unlimited). + --ns:: Use 9 decimal places when displaying time (i.e. show the nanoseconds) =20 diff --git a/tools/perf/tests/shell/script_lazy_load_symbols.sh b/tools/per= f/tests/shell/script_lazy_load_symbols.sh new file mode 100755 index 000000000000..799c61e2f3e8 --- /dev/null +++ b/tools/perf/tests/shell/script_lazy_load_symbols.sh @@ -0,0 +1,120 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# perf script lazy symbol loading tests +# +# Verifies that --lazy-load-symbols produces output identical to the defau= lt +# eager symbol loader, and that --max-symbol-bytes caps symbol allocations +# (emitting [unknown] plus a warning) without crashing. + +set -e + +shelldir=3D$(dirname "$0") +# shellcheck source=3Dlib/perf_has_symbol.sh +. "${shelldir}"/lib/perf_has_symbol.sh + +testsym=3D"test_loop" + +skip_test_missing_symbol ${testsym} + +err=3D0 +temp_dir=3D$(mktemp -d /tmp/__perf_test.lazy_load.XXXXX) +perfdata=3D"${temp_dir}/perf.data" +eager_out=3D"${temp_dir}/eager.out" +lazy_out=3D"${temp_dir}/lazy.out" + +cleanup() { + 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 + +test_lazy_load_identical() { + echo "Lazy-load output matches eager loader" + + # Record a small profile with callchains so symbol resolution runs. + if ! perf record -o "${perfdata}" -g -- perf test -w thloop 2> /dev/null + then + echo "Lazy-load identical [Skipped record not supported]" + return + fi + + if ! perf script -i "${perfdata}" 2> /dev/null > "${eager_out}" || \ + ! perf script --lazy-load-symbols -i "${perfdata}" 2> /dev/null > "${l= azy_out}" + then + echo "Lazy-load identical [Failed perf script error]" + err=3D1 + return + fi + + # The comparison is only meaningful if something actually resolved; + # two all-[unknown] outputs would also match. + if ! grep -q "${testsym}" "${eager_out}" + then + echo "Lazy-load identical [Skipped no ${testsym} resolved]" + return + fi + + if ! cmp -s "${eager_out}" "${lazy_out}" + then + echo "Lazy-load identical [Failed output differs]" + err=3D1 + return + fi + echo "Lazy-load identical [Success]" +} + +test_max_symbol_bytes() { + echo "--max-symbol-bytes budget enforcement" + + # Depends on ${perfdata} from test_lazy_load_identical. + if [ ! -s "${perfdata}" ] + then + echo "--max-symbol-bytes budget [Skipped record not supported]" + return + fi + + # A tiny budget forces most symbols to be dropped as [unknown], + # with a single warning, and must not crash. + if ! perf script --max-symbol-bytes=3D1K -i "${perfdata}" > /dev/null \ + 2> "${temp_dir}/budget.err" + then + echo "--max-symbol-bytes budget [Failed nonzero exit]" + err=3D1 + return + fi + if ! grep -q "symbol memory budget exceeded" "${temp_dir}/budget.err" + then + echo "--max-symbol-bytes budget [Failed missing warning]" + err=3D1 + return + fi + + if ! perf script --lazy-load-symbols --max-symbol-bytes=3D1K \ + -i "${perfdata}" > /dev/null 2> "${temp_dir}/lazy-budget.err" + then + echo "--max-symbol-bytes lazy budget [Failed nonzero exit]" + err=3D1 + return + fi + warnings=3D$(grep -c "symbol memory budget exceeded" \ + "${temp_dir}/lazy-budget.err" || true) + if [ "${warnings}" -ne 1 ] + then + echo "--max-symbol-bytes lazy budget [Failed warning count: ${warnings}]" + err=3D1 + return + fi + echo "--max-symbol-bytes budget [Success]" +} + +test_lazy_load_identical +test_max_symbol_bytes + +cleanup +exit $err --=20 Git-155)