From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796076282; b=j+mrhgFWQfMmiXKZ4q9ojdz0jB4REfOZpFme0HnFB74wNO6l0nzOVn2qwTqnmDJQecCV gFssmqcwR69h+RKS32Dpmuls4Qh+xuCrCgr9jFxZHxQFPb2l49Dxw6nhvMWeNlK8092sR 2fACVIgsFw7eYC3qqnoWTf+UY31xxtpoSt1uhjOLuYbSf+DZMaIATI/Be35TnjHdGU6Ok X3tZq2osVuOmjaJoXQ0Z62r++poxYO5PsfNPQlKMP8oTc9pa84LD80nJX84pDKb0RAPZF rB5tq8wZMGcOrnr8/ebqJjYg9eN55RSVJwDoRp4vpc04ALYbvKbNS8hMwE2x75oSC1Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796076282; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=wsBsf1BBO9wYp3Myh4dxGMVL21hGYMFc5I/4HGO1D54=; b=wzIPZ/U7G7Mw0LXZRGfXHHSs6hw/K8DquRgPs+gMYvqYYa6nXunZlWATNQHl6NhT8k5n 3D5wPmdX01YOBi7u0EVgg9JNandT5fxdc6xLVA0yLFpqjJfmxSsRt98Mljupd8zZ4Kbbi pqhMv81M/HZD1BUiGrn9j7Hr7WEXWoD6vyKWCDLCOVBpkFG6PgjR1I+wV6MvgJmHFJvpH fXAq1bIcTMGqo+eR7Gdeb4Sr5kaRZzBI0myvhmuZIl0zIoC6nz2VzgIRuIPL8gT8lNZN5 RvkAKu5MgJCSt0nULF9hG94cvIwIOuezQxDfuHzQWnIovAmX/uxFtdQ1/BvKvPVDCuA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=MRp1p3xm; arc=none smtp.remote-ip=74.125.82.199 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26989606A31A for ; Mon, 30 Nov 2026 22:04:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 26989606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=MRp1p3xm Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-3854627c1c8so14167724eec.1 for ; Mon, 30 Nov 2026 14:04:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796076280; x=1796681080; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=wsBsf1BBO9wYp3Myh4dxGMVL21hGYMFc5I/4HGO1D54=; b=MRp1p3xmH45ipbH334cqTCkAJH782l41vRFy1d1vZZVs6m4CO2jx1PMZpLZMjVvSYG 1b+oN/hDKkULAWqA/7OHRIbFix1jtE2p0CP2/sCyftLKICmHOuIWEQCNHsAWH/jjL533 okjScxicp8W0lkR3rynwbEq9Cjbb5VkoR7Gh1itX7WfSboA58Ky+Npt4LQwFXaiPcNtD fMHyJqn1G1qLWC3eBmZB0WRijyVS2/ixKLvjeB72eD1D21/2lUqk9dk4IeerIEA7MqkP dWjrx29DzOJuJ/F/Twc/rLV+3MQRm4T87UTrYy62sR0anAs8qAHF2oS0hEp94OVKggMV o2jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796076280; x=1796681080; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=wsBsf1BBO9wYp3Myh4dxGMVL21hGYMFc5I/4HGO1D54=; b=vZzJzwvt705239duSYKikFuJL+g1Cf6t9VAyztu5J3yYK1WQtfl8yTrnbH3e8O6DIN fVN5Gd1MrKyDeUTulD6C422IPlQHM2TUGg+g4tWMmxD2AsRLtqj6CGDX1sDtiSjJk/xC +THOo6Jvv2Tnst9zEShr8KC31Len3PiyjZHmLXz3GJMXJEcba2P0f73mCx7f2U0zZFDY vVM8MFOPjTE+5iDaS8E4b7iZL0e01lHys/7lQnEOix3oE862k2BhwjO5RwUlTCXdedYt CjkO6zsGcxWs90LOpvIjOuN06JruT2MIliPY/RWSOvWfgiZ6R+d6X2P/XuCQWMIrLUh+ aamA== X-Forwarded-Encrypted: i=1; AL+hErHtreOYsZCJY/D/qsLVpYGQAG9loqYf7OhBcU4dQMBdc0fI6D5FDWuo3HDSIV8VXHEOcd9I7HxnnjSdDVg=@vger.kernel.org X-Gm-Message-State: APuU4Z+YyNpVTMLBm9AgD+xgX3G1MCdK7faVP3zFhi2mUJld+hzHWFlc /A+hbXkFXFhr0XcVdtK/rKabM1QLad/MNMs8XSunEgjkGiqo3MrjEt+gsWX4W614QeUBgN7K9UC 5zD3679c0GA== X-Received: from dyah27-n2.prod.google.com ([2002:a05:693c:83db:20b0:36a:209:32b]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:6187:10b0:386:a193:7eeb with SMTP id 5a478bee46e88-386a1938b8dmr29683251eec.30.1796076280040; Mon, 30 Nov 2026 14:04:40 -0800 (PST) Date: Mon, 30 Nov 2026 14:04:31 -0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261130220437.4099852-1-irogers@google.com> Subject: [PATCH v11 0/5] perf symbol: Fix alias end fixup and optimize sorted array From: Ian Rogers To: Namhyung Kim , Arnaldo Carvalho de Melo , Alireza Haghdoost , Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Jiri Olsa , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" This v11 series rebases onto perf-tools-next (4a7008f739a3, including Alireza's lazy symbol loading series) and splits out the 5 standalone struct symbols sorted-array bug fix, cleanup, and optimization patches from v10 so they can land cleanly on their own. The memory-shrinking patches (v10 patches 6-12) will follow in a separate series alongside their unlocked call sites and locking documentation, addressing Alireza's review feedback on v10. Summary of patches: 1. Fix symbols__fixup_end() so all symbols in an all-zero-sized equal-start group receive the synthesized end address rather than only the last entry, allowing choose_best_symbol() to reach its binding and name tie-breakers regardless of input order. 2. Remove redundant num_by_name and sorted_by_name fields from struct symbols (replaced by @cnt and @symbols_by_name != NULL || @cnt == 0). 3. Replace the @sorted boolean in struct symbols with @sorted_cnt prefix length tracking so post-sort appends only sort and merge the newly appended tail (or binary-insert in place for small appends), reducing 'perf bench internals symbols' append + lookup time by 90.3% (1551.533 usec -> 150.333 usec). 4. Add @sorted_tail_cnt tracking an optional secondary sorted run for out-of-order dso__insert_symbol() insertions with an amortized sqrt(N) merge threshold (keeping sizeof(struct symbols) unchanged at 88 bytes), further reducing append + lookup time by 71.4% (150.333 usec -> 43.000 usec; 3315.200 usec -> 294.000 usec at 100,000 symbols). 5. Unit tests for sorted insertion, unmerged-vs-merged two-run lookups, and equal-start symbol aliases (including both weak-then-global and global-then-weak kallsyms alias orderings). Dynamic Insertion Complexity & Trade-offs (Patch 4): With a primary sorted run of N = sorted_cnt entries and a secondary sorted run buffered up to B entries before merging: - Shifting pointers inside the secondary run costs sum_{t=0..B-1} O(t) = O(B^2) over B out-of-order inserts, or O(B) per insert. - Merging the secondary run into the primary run costs O(N + B) once every B inserts, or O(N / B) per insert. Minimizing O(B + N / B) balances the two terms at B^2 = N (B = sqrt(N), checked in integer arithmetic via sorted_tail_cnt^2 >= sorted_cnt with a 256-entry / 2 KB L1d-sized floor), giving: - Ascending insert: O(1) amortized (appends and advances sorted_cnt). - Out-of-order dynamic insert: O(sqrt(N)) amortized, O(N) on merge. - Bulk symbols__insert() + first lookup: O(1) per append + O(N) on natural runs (or O(K log K + N) for K unsorted appends). - Address lookup: O(log N) across at most two sorted runs. Alternative flat-array structures were also considered: - Implicit array BST (2i + 1, 2i + 2): Zero pointer overhead, but unbalanced sorted inserts blow up array capacity as O(2^k), and in-place rotations move O(subtree) = O(N) entries. - Index-based balanced BST (left/right indices): O(log N) worst-case even on sorted inputs, but adds 8-12 bytes per symbol (giving back up to half of the 24-byte rb_node savings) and loses contiguous binary-search cache locality. - Bentley-Saxe logarithmic runs (<= log2(N) power-of-2 sorted runs): Reduces out-of-order dynamic insert to O(log N) amortized with 0 bytes/symbol and O(1) sorted append, but degrades unmerged lookups from 2 binary searches to up to log2(N) binary searches (O(log^2 N)). - Packed Memory Array (gapped sorted array): O(log^2 N) insert and O(log N) lookup, but 2-4x pointer-array memory overhead, O(log^2 N) density rebalances even on sorted appends, and NULL-gap skipping in every linear pass. Because perf workloads are overwhelmingly lookup-heavy, bulk loads go through symbols__insert(), and upward-growing JIT/BPF allocations hit the O(1) ascending fast path (while even at N = 1,000,000 symbols an average sqrt(N)/2 tail shift moves 4 KB in L1d cache), two sorted runs keep lookups at O(log N) with zero per-symbol space overhead and minimal code complexity. v11: - Rebase onto perf-tools-next (4a7008f739a3) after Alireza's lazy symbol loading series merged. - Split out patches 1-5 (symbols__fixup_end() fix, struct symbols cleanup, sorted_cnt/sorted_tail_cnt optimizations, and unit tests) from the memory-shrinking patches (v10 patches 6-12), which will be posted separately with their unlocked call sites. - Update Fixes: trailer in patch 1 to 432746f8e0b6 ("perf symbols: Fixup symbol sizes before picking best ones") per Sashiko review. v10: https://lore.kernel.org/linux-perf-users/20261124223109.820609-1-irogers@google.com/ - Rebase onto perf-tools-next (148690b95cc4). - Add patch 1 fixing symbols__fixup_end() to update all entries in an all-zero-sized equal-start group so choose_best_symbol() reaches STB_WEAK/STB_GLOBAL tie-breakers regardless of symbol order, and test both weak-then-global and global-then-weak orderings in patch 5 (reported by Sashiko on v9 patch 4). v9: https://lore.kernel.org/linux-perf-users/20261124032902.3859564-1-irogers@google.com/ - Drop patches 1-6 from v8 (merged into perf-tools-next) and rebase onto perf-tools-next (964e8b1c9c79). - Remove redundant num_by_name, sorted_by_name, and sorted booleans from struct symbols in favor of (cnt, sorted_cnt, sorted_tail_cnt) and expand the struct symbols kernel-doc. - Split v8 patch 7/10 into three patches (sorted prefix tracking, secondary sorted run for dso__insert_symbol(), and unit tests) with fresh -O2 'perf bench internals symbols' measurements. - Split v8 patch 8/10 into four patches and v8 patch 10/10 into two test patches. v8: https://lore.kernel.org/linux-perf-users/20261122050405.80335-1-irogers@google.com/ - Rebase onto perf-tools-next (eb79d2fbc230). - Fix checkpatch.pl false-positive typo error in struct symbols field ordering and context_unsafe test helper warnings. v7: https://lore.kernel.org/linux-perf-users/20261121071651.67118-1-irogers@google.com/ - Rebase onto perf-tools-next (017fcfa4a4ab), adding Clang -Wthread-safety context analysis annotations (__guarded_by, __acquires, __releases, __must_hold). v6: https://lore.kernel.org/linux-perf-users/20261117185305.108003-1-irogers@google.com/ - Split the monolithic symbols rb_tree-to-array conversion into smaller patches for reviewability (requested by Namhyung). v5: https://lore.kernel.org/linux-perf-users/20261116063435.2817923-1-irogers@google.com/ - Replace heuristic periodic shrinking with explicit perf_session__* allocation wrappers that shrink on allocation failure. v4: https://lore.kernel.org/linux-perf-users/20261113171734.2188066-1-irogers@google.com/ - Address review feedback on symbol sorting, alias handling, and shrinking safety. v3: https://lore.kernel.org/linux-perf-users/20261110220803.2503416-1-irogers@google.com/ - Add symbol table array conversion and DSO/symbol shrinking. v2: https://lore.kernel.org/linux-perf-users/20261027231513.816148-1-irogers@google.com/ - Reference count DSOs in dso_symbol and fix map_symbol lifecycles. v1: https://lore.kernel.org/lkml/20260928075237.3055101-1-irogers@google.com/ - Initial RFC. Ian Rogers (5): perf symbol: Fix symbols__fixup_end() for equal-start zero-sized aliases perf symbol: Drop num_by_name and sorted_by_name from struct symbols perf symbol: Replace sorted boolean with sorted_cnt prefix length perf symbol: Add secondary sorted run for dso__insert_symbol() perf test symbols: Add sorted insertion and equal-start alias tests tools/perf/tests/symbols.c | 273 +++++++++++++++++++++++++++-- tools/perf/util/dso.c | 2 +- tools/perf/util/symbol.c | 340 ++++++++++++++++++++++++++----------- tools/perf/util/symbol.h | 55 ++++-- 4 files changed, 546 insertions(+), 124 deletions(-) base-commit: 4a7008f739a30ee4c22d9f21af362a5128f2628d -- 2.56.1.839.g22a8290db9-goog