[PATCH v9 03/11] perf symbol: Add secondary sorted run for dso__insert_symbol()
From: Ian Rogers
Date: Mon Nov 23 2026 - 22:29:31 EST
Dynamic symbol resolution via dso__insert_symbol() can insert symbols
out of order (with sym->start < symbols[sorted_cnt - 1]->start) into an
already-sorted DSO symbol table. Binary-inserting directly into the
primary sorted run memmoves O(N) pointers on every insertion.
Add @sorted_tail_cnt to struct symbols tracking an optional secondary
start-address-sorted run at
symbols[sorted_cnt .. sorted_cnt + sorted_tail_cnt) (keeping
sizeof(struct symbols) unchanged at 88 bytes on 64-bit) and expand the
struct symbols kernel-doc to document the three array regions:
- Append ascending symbols in O(1) (advancing @sorted_cnt) and
binary-insert in place for small tables (cnt < 64).
- For larger out-of-order dynamic insertions when no unsorted bulk
entries are pending, insert into the secondary sorted run with an
amortized sqrt(N) merge threshold (sorted_tail_cnt >= 256 and
sorted_tail_cnt^2 >= sorted_cnt).
- Search both sorted runs under a read lock in symbols__find() while
preserving single-array highest start <= ip lookup semantics, and
merge into a single sorted run in symbols__sort_read_lock().
Using 'perf bench internals symbols':
Default (10,000 symbols, 200 appends):
Before:
Average append + lookup: 150.333 usec (+- 0.273 usec)
After (-71.4%):
Average append + lookup: 43.000 usec (+- 0.311 usec)
Large DSO (-n 100000 -a 500):
Before:
Average append + lookup: 3315.200 usec (+- 12.532 usec)
After (-91.1%):
Average append + lookup: 294.000 usec (+- 11.574 usec)
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/symbol.c | 151 ++++++++++++++++++++++++++++++++++++---
tools/perf/util/symbol.h | 38 +++++++---
2 files changed, 172 insertions(+), 17 deletions(-)
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index b9ddebdcd900..f3a3889f1062 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -221,6 +221,7 @@ void symbols__init(struct symbols *symbols)
symbols->symbols_by_name = NULL;
symbols->cnt = 0;
symbols->sorted_cnt = 0;
+ symbols->sorted_tail_cnt = 0;
symbols->allocated = 0;
}
@@ -234,6 +235,7 @@ static void symbols__delete_locked(struct symbols *symbols)
zfree(&symbols->symbols_by_name);
symbols->cnt = 0;
symbols->sorted_cnt = 0;
+ symbols->sorted_tail_cnt = 0;
symbols->allocated = 0;
}
@@ -435,12 +437,16 @@ static void symbols__sort_locked(struct symbols *symbols)
__must_hold(&symbols->lock)
{
unsigned int m = symbols->sorted_cnt;
+ unsigned int t = symbols->sorted_tail_cnt;
unsigned int k;
if (m >= symbols->cnt) {
symbols->sorted_cnt = symbols->cnt;
+ symbols->sorted_tail_cnt = 0;
return;
}
+ if (t > symbols->cnt - m)
+ t = 0;
k = symbols->cnt - m;
/*
@@ -451,10 +457,24 @@ static void symbols__sort_locked(struct symbols *symbols)
if (k <= 8 || symbols->cnt <= 64) {
symbols__insertion_sort_range(symbols->symbols, 0, m, symbols->cnt);
symbols->sorted_cnt = symbols->cnt;
+ symbols->sorted_tail_cnt = 0;
return;
}
- if (m <= 1) {
+ if (m + t == symbols->cnt) {
+ /*
+ * Both [0 .. m) and the secondary run [m .. cnt) are already
+ * sorted; merge them into a single sorted run.
+ */
+ struct symbol **tmp = malloc(min(m, k) * sizeof(struct symbol *));
+
+ if (tmp) {
+ symbols__merge_runs(symbols->symbols, 0, m, symbols->cnt, tmp);
+ free(tmp);
+ } else {
+ symbols__insertion_sort_range(symbols->symbols, 0, m, symbols->cnt);
+ }
+ } else if (m <= 1) {
struct symbol **tmp = malloc(((symbols->cnt + 1) / 2) * sizeof(struct symbol *));
if (tmp) {
@@ -476,11 +496,12 @@ static void symbols__sort_locked(struct symbols *symbols)
}
symbols->sorted_cnt = symbols->cnt;
+ symbols->sorted_tail_cnt = 0;
}
/*
* Acquire @symbols->lock for reading after ensuring @symbols->symbols is
- * sorted.
+ * fully sorted into a single contiguous run.
*/
void symbols__sort_read_lock(struct symbols *symbols)
{
@@ -711,14 +732,81 @@ static int __symbols__insert(struct symbols *symbols, struct symbol *sym)
if (symbols->sorted_cnt == symbols->cnt &&
(symbols->cnt == 0 ||
- symbol__less_or_equal(symbols->symbols[symbols->cnt - 1], sym)))
+ symbol__less_or_equal(symbols->symbols[symbols->cnt - 1], sym))) {
symbols->sorted_cnt = symbols->cnt + 1;
+ } else if (symbols->sorted_tail_cnt > 0 &&
+ symbols->sorted_cnt + symbols->sorted_tail_cnt == symbols->cnt &&
+ symbol__less_or_equal(symbols->symbols[symbols->cnt - 1], sym)) {
+ symbols->sorted_tail_cnt++;
+ }
symbols->symbols[symbols->cnt++] = sym;
zfree(&symbols->symbols_by_name);
return 0;
}
+static int __symbols__insert_sorted(struct symbols *symbols, struct symbol *sym)
+ __must_hold(&symbols->lock)
+{
+ unsigned int idx;
+
+ if (symbols->sorted_cnt + symbols->sorted_tail_cnt < symbols->cnt)
+ return __symbols__insert(symbols, sym);
+
+ if (symbols->cnt == symbols->allocated) {
+ unsigned int to_allocate = symbols->allocated ? symbols->allocated * 2 : 32;
+ struct symbol **temp = realloc(symbols->symbols,
+ sizeof(struct symbol *) * to_allocate);
+
+ if (!temp) {
+ symbol__delete(sym);
+ return -ENOMEM;
+ }
+ symbols->symbols = temp;
+ symbols->allocated = to_allocate;
+ }
+
+ if (symbols->sorted_tail_cnt == 0) {
+ if (symbols->cnt == 0 ||
+ symbol__less_or_equal(symbols->symbols[symbols->cnt - 1], sym)) {
+ symbols->symbols[symbols->cnt++] = sym;
+ symbols->sorted_cnt = symbols->cnt;
+ } else if (symbols->cnt < 64) {
+ idx = symbols__upper_bound(symbols->symbols, symbols->cnt, sym);
+ memmove(&symbols->symbols[idx + 1], &symbols->symbols[idx],
+ (symbols->cnt - idx) * sizeof(struct symbol *));
+ symbols->symbols[idx] = sym;
+ symbols->cnt++;
+ symbols->sorted_cnt = symbols->cnt;
+ } else {
+ /*
+ * Start a secondary sorted run at symbols[sorted_cnt]
+ * so out-of-order insertions do not memmove the entire
+ * primary sorted run on every call.
+ */
+ symbols->symbols[symbols->cnt++] = sym;
+ symbols->sorted_tail_cnt = 1;
+ }
+ } else {
+ idx = symbols->sorted_cnt +
+ symbols__upper_bound(&symbols->symbols[symbols->sorted_cnt],
+ symbols->sorted_tail_cnt, sym);
+ memmove(&symbols->symbols[idx + 1], &symbols->symbols[idx],
+ (symbols->cnt - idx) * sizeof(struct symbol *));
+ symbols->symbols[idx] = sym;
+ symbols->cnt++;
+ symbols->sorted_tail_cnt++;
+
+ if (symbols->sorted_tail_cnt >= 256 &&
+ (size_t)symbols->sorted_tail_cnt * symbols->sorted_tail_cnt >=
+ symbols->sorted_cnt)
+ symbols__sort_locked(symbols);
+ }
+
+ zfree(&symbols->symbols_by_name);
+ return 0;
+}
+
int symbols__insert(struct symbols *symbols, struct symbol *sym)
{
int ret;
@@ -737,7 +825,9 @@ static inline bool symbol__covers(const struct symbol *s, u64 ip)
return ip >= start && (ip < end || (ip == end && ip == start));
}
-static struct symbol *symbols__find_range(struct symbol **arr, unsigned int cnt, u64 ip)
+static struct symbol *symbols__find_range(struct symbol **arr, unsigned int cnt,
+ u64 ip, bool *has_candidate,
+ u64 *candidate_start)
{
unsigned int low = 0, high = cnt, idx;
u64 start;
@@ -750,8 +840,10 @@ static struct symbol *symbols__find_range(struct symbol **arr, unsigned int cnt,
else
high = mid;
}
- if (low == 0)
+ if (low == 0) {
+ *has_candidate = false;
return NULL;
+ }
/*
* Rewind across any equal-start group so a trailing zero-length or
@@ -760,6 +852,8 @@ static struct symbol *symbols__find_range(struct symbol **arr, unsigned int cnt,
*/
idx = low - 1;
start = arr[idx]->start;
+ *has_candidate = true;
+ *candidate_start = start;
while (idx > 0 && arr[idx - 1]->start == start)
idx--;
for (; idx < low; idx++) {
@@ -771,17 +865,43 @@ static struct symbol *symbols__find_range(struct symbol **arr, unsigned int cnt,
/*
* Binary search @symbols->symbols for a symbol covering @ip. Lazily sorts
- * @symbols under @symbols->lock if unsorted.
+ * @symbols under @symbols->lock if unsorted entries are present.
*/
static struct symbol *symbols__find(struct symbols *symbols, u64 ip)
{
struct symbol *res;
+ bool has_prefix;
+ u64 prefix_start = 0;
if (symbols == NULL)
return NULL;
- symbols__sort_read_lock(symbols);
- res = symbols__find_range(symbols->symbols, symbols->cnt, ip);
+ down_read(&symbols->lock);
+ while (symbols->sorted_cnt + symbols->sorted_tail_cnt < symbols->cnt) {
+ up_read(&symbols->lock);
+ down_write(&symbols->lock);
+ symbols__sort_locked(symbols);
+ up_write(&symbols->lock);
+ down_read(&symbols->lock);
+ }
+
+ res = symbols__find_range(symbols->symbols, symbols->sorted_cnt, ip,
+ &has_prefix, &prefix_start);
+ if (symbols->sorted_tail_cnt > 0) {
+ struct symbol *tail_res;
+ bool has_tail;
+ u64 tail_start = 0;
+
+ tail_res = symbols__find_range(&symbols->symbols[symbols->sorted_cnt],
+ symbols->sorted_tail_cnt, ip,
+ &has_tail, &tail_start);
+ if (has_tail) {
+ if (!has_prefix || tail_start > prefix_start)
+ res = tail_res;
+ else if (tail_start == prefix_start && !res)
+ res = tail_res;
+ }
+ }
up_read(&symbols->lock);
return res;
@@ -917,7 +1037,13 @@ static struct symbol *symbols__find_by_name(struct symbol *symbols[],
int dso__insert_symbol(struct dso *dso, struct symbol *sym)
{
- return symbols__insert(dso__symbols(dso), sym);
+ struct symbols *symbols = dso__symbols(dso);
+ int ret;
+
+ down_write(&symbols->lock);
+ ret = __symbols__insert_sorted(symbols, sym);
+ up_write(&symbols->lock);
+ return ret;
}
void dso__delete_symbol(struct dso *dso, struct symbol *sym)
@@ -932,6 +1058,8 @@ void dso__delete_symbol(struct dso *dso, struct symbol *sym)
symbols->cnt--;
if (i < symbols->sorted_cnt)
symbols->sorted_cnt--;
+ else if (i < symbols->sorted_cnt + symbols->sorted_tail_cnt)
+ symbols->sorted_tail_cnt--;
zfree(&symbols->symbols_by_name);
symbol__delete(sym);
break;
@@ -1277,6 +1405,7 @@ static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso)
root->symbols = NULL;
root->cnt = 0;
root->sorted_cnt = 0;
+ root->sorted_tail_cnt = 0;
root->allocated = 0;
zfree(&root->symbols_by_name);
up_write(&root->lock);
@@ -1349,6 +1478,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
root->symbols = NULL;
root->cnt = 0;
root->sorted_cnt = 0;
+ root->sorted_tail_cnt = 0;
root->allocated = 0;
zfree(&root->symbols_by_name);
up_write(&root->lock);
@@ -1497,6 +1627,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
root->symbols = old_symbols;
root->cnt = write_idx;
root->sorted_cnt = write_idx;
+ root->sorted_tail_cnt = 0;
root->allocated = old_allocated;
old_symbols = NULL;
} else {
@@ -1524,12 +1655,14 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
root->symbols = old_symbols;
root->cnt = write_idx;
root->sorted_cnt = (delta == 0) ? write_idx : min(write_idx, 1U);
+ root->sorted_tail_cnt = 0;
root->allocated = old_allocated;
old_symbols = NULL;
} else {
for (unsigned int j = 0; j < write_idx; j++)
__symbols__insert(root, old_symbols[j]);
root->sorted_cnt = min(root->cnt, 1U);
+ root->sorted_tail_cnt = 0;
}
zfree(&root->symbols_by_name);
up_write(&root->lock);
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 2b20bfb22fd1..1aa54c1f1680 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -99,11 +99,31 @@ struct symbol {
* struct symbols - A locked collection of symbols stored in a sorted array.
*
* The @symbols array of @cnt pointers (within an allocation of @allocated
- * slots) keeps a start-address-sorted prefix @symbols[0 .. @sorted_cnt)
- * followed by an optional unsorted tail @symbols[@sorted_cnt .. @cnt) from
- * bulk symbols__insert() appends. When @sorted_cnt == @cnt, the entire array
- * is sorted; otherwise the unsorted tail is lazily sorted and merged into the
- * prefix on lookup or iteration.
+ * slots) is partitioned into up to three contiguous regions:
+ *
+ * [0 .. @sorted_cnt)
+ * Primary run, sorted by ascending symbol start address.
+ * [@sorted_cnt .. @sorted_cnt + @sorted_tail_cnt)
+ * Optional secondary sorted run holding out-of-order dynamic insertions
+ * from dso__insert_symbol() whose start addresses precede
+ * @symbols[@sorted_cnt - 1]->start (so the two runs overlap in address
+ * range rather than forming one ascending sequence). Keeping @sorted_cnt
+ * at the boundary between the two runs avoids memmoving the primary run
+ * on every insertion while allowing O(log N) binary search across both
+ * runs under a read lock; once the secondary run reaches
+ * O(sqrt(@sorted_cnt)) entries (or a single contiguous run is required),
+ * the two runs are merged and @sorted_cnt advances to @cnt.
+ * [@sorted_cnt + @sorted_tail_cnt .. @cnt)
+ * Optional unsorted tail from bulk symbols__insert() appends, lazily
+ * sorted and merged into the primary run on lookup or iteration.
+ *
+ * Invariants:
+ * - @sorted_cnt == @cnt: all entries form a single sorted run
+ * (@sorted_tail_cnt is 0).
+ * - @sorted_cnt + @sorted_tail_cnt == @cnt: all entries reside in the two
+ * sorted runs and can be binary-searched without sorting.
+ * - @sorted_cnt + @sorted_tail_cnt < @cnt: unsorted entries are present and
+ * must be sorted under @lock for writing before searching.
*/
struct symbols {
/** @lock: Read/write semaphore synchronizing access to the arrays. */
@@ -118,11 +138,13 @@ struct symbols {
struct symbol **symbols_by_name __guarded_by(&lock);
/** @cnt: Total number of valid symbol pointers in @symbols. */
unsigned int cnt __guarded_by(&lock);
+ /** @sorted_cnt: Length of the primary start-address-sorted prefix. */
+ unsigned int sorted_cnt __guarded_by(&lock);
/**
- * @sorted_cnt: Length of the leading start-address-sorted prefix in
- * @symbols. The entire @symbols array is sorted iff @sorted_cnt == @cnt.
+ * @sorted_tail_cnt: Length of the secondary start-address-sorted run
+ * immediately following @sorted_cnt (0 when absent).
*/
- unsigned int sorted_cnt __guarded_by(&lock);
+ unsigned int sorted_tail_cnt __guarded_by(&lock);
/** @allocated: Capacity (in entries) of the @symbols allocation. */
unsigned int allocated __guarded_by(&lock);
};
--
2.56.1.809.ga0b61035b5-goog