From: Ian Rogers <irogers@google.com>
To: Namhyung Kim <namhyung@kernel.org>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Alireza Haghdoost <haghdoost@uber.com>,
Ian Rogers <irogers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>, Jiri Olsa <jolsa@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v9 01/11] perf symbol: Drop num_by_name and sorted_by_name from struct symbols
Date: Mon, 23 Nov 2026 19:28:52 -0800 [thread overview]
Message-ID: <20261124032902.3859564-2-irogers@google.com> (raw)
In-Reply-To: <20261124032902.3859564-1-irogers@google.com>
Every mutation to symbols->symbols frees symbols->symbols_by_name, and
symbols__sort_by_name_locked() copies all symbols->cnt pointers verbatim
before sorting by name. As a result:
- symbols->num_by_name always equals symbols->cnt whenever
symbols->symbols_by_name is non-NULL (and 0 otherwise).
- symbols->sorted_by_name is equivalent to
(symbols->symbols_by_name != NULL || symbols->cnt == 0). Treating
cnt == 0 as trivially sorted by name also avoids an unnecessary
read-to-write lock upgrade on empty DSOs in
symbols__sort_by_name_read_lock() and dso__sort_by_name().
Remove num_by_name and sorted_by_name from struct symbols and clarify
the @symbols_by_name kernel-doc.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/dso.c | 2 +-
tools/perf/util/symbol.c | 44 ++++++++--------------------------------
tools/perf/util/symbol.h | 16 +++++----------
3 files changed, 14 insertions(+), 48 deletions(-)
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 287f6812139c..ab0c87d0595e 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -1809,7 +1809,7 @@ bool dso__sorted_by_name(const struct dso *dso)
bool res;
down_read(&symbols->lock);
- res = symbols->sorted_by_name;
+ res = symbols->symbols_by_name != NULL || symbols->cnt == 0;
up_read(&symbols->lock);
return res;
}
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 35b08c05a2fd..03c4314935a5 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -221,9 +221,7 @@ void symbols__init(struct symbols *symbols)
symbols->symbols_by_name = NULL;
symbols->cnt = 0;
symbols->allocated = 0;
- symbols->num_by_name = 0;
symbols->sorted = true;
- symbols->sorted_by_name = false;
}
static void symbols__delete_locked(struct symbols *symbols)
@@ -236,9 +234,7 @@ static void symbols__delete_locked(struct symbols *symbols)
zfree(&symbols->symbols_by_name);
symbols->cnt = 0;
symbols->allocated = 0;
- symbols->num_by_name = 0;
symbols->sorted = true;
- symbols->sorted_by_name = false;
}
void symbols__delete(struct symbols *symbols)
@@ -510,8 +506,6 @@ void symbols__fixup_duplicate(struct symbols *symbols)
}
symbols->cnt = write_idx + 1;
zfree(&symbols->symbols_by_name);
- symbols->num_by_name = 0;
- symbols->sorted_by_name = false;
up_write(&symbols->lock);
}
@@ -691,8 +685,6 @@ static int __symbols__insert(struct symbols *symbols, struct symbol *sym)
symbols->symbols[symbols->cnt++] = sym;
zfree(&symbols->symbols_by_name);
- symbols->num_by_name = 0;
- symbols->sorted_by_name = false;
return 0;
}
@@ -795,17 +787,9 @@ static int symbols__sort_by_name_locked(struct symbols *symbols)
struct symbol **result;
unsigned int size = symbols->cnt;
- if (symbols->sorted_by_name)
+ if (symbols->symbols_by_name || size == 0)
return 0;
- zfree(&symbols->symbols_by_name);
- symbols->num_by_name = 0;
-
- if (size == 0) {
- symbols->sorted_by_name = true;
- return 0;
- }
-
result = malloc(sizeof(*result) * size);
if (!result)
return -ENOMEM;
@@ -813,8 +797,6 @@ static int symbols__sort_by_name_locked(struct symbols *symbols)
memcpy(result, symbols->symbols, sizeof(*result) * size);
qsort(result, size, sizeof(*result), symbols__sort_name_cmp);
symbols->symbols_by_name = result;
- symbols->num_by_name = size;
- symbols->sorted_by_name = true;
return 0;
}
@@ -824,7 +806,7 @@ static int symbols__sort_by_name_read_lock(struct symbols *symbols)
int err = 0;
down_read(&symbols->lock);
- while (!symbols->sorted_by_name) {
+ while (!symbols->symbols_by_name && symbols->cnt > 0) {
up_read(&symbols->lock);
down_write(&symbols->lock);
err = symbols__sort_by_name_locked(symbols);
@@ -833,7 +815,7 @@ static int symbols__sort_by_name_read_lock(struct symbols *symbols)
if (err)
break;
}
- return symbols->sorted_by_name ? 0 : err;
+ return (symbols->symbols_by_name || symbols->cnt == 0) ? 0 : err;
}
int symbol__match_symbol_name(const char *name, const char *str,
@@ -918,8 +900,6 @@ void dso__delete_symbol(struct dso *dso, struct symbol *sym)
(symbols->cnt - i - 1) * sizeof(struct symbol *));
symbols->cnt--;
zfree(&symbols->symbols_by_name);
- symbols->num_by_name = 0;
- symbols->sorted_by_name = false;
symbol__delete(sym);
break;
}
@@ -947,7 +927,7 @@ struct symbol *dso__next_symbol_by_name(struct dso *dso, size_t *idx)
symbols = dso__symbols(dso);
if (!symbols__sort_by_name_read_lock(symbols) &&
- *idx + 1 < symbols->num_by_name) {
+ *idx + 1 < symbols->cnt) {
++*idx;
res = symbols->symbols_by_name[*idx];
}
@@ -971,10 +951,10 @@ struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_
symbols = dso__symbols(dso);
if (!symbols__sort_by_name_read_lock(symbols)) {
s = symbols__find_by_name(symbols->symbols_by_name,
- symbols->num_by_name,
+ symbols->cnt,
name, SYMBOL_TAG_INCLUDE__NONE, idx);
if (!s) {
- s = symbols__find_by_name(symbols->symbols_by_name, symbols->num_by_name,
+ s = symbols__find_by_name(symbols->symbols_by_name, symbols->cnt,
name, SYMBOL_TAG_INCLUDE__DEFAULT_ONLY, idx);
}
}
@@ -987,7 +967,7 @@ void dso__sort_by_name(struct dso *dso)
struct symbols *symbols = dso__symbols(dso);
down_read(&symbols->lock);
- if (!symbols->sorted_by_name) {
+ if (!symbols->symbols_by_name && symbols->cnt > 0) {
up_read(&symbols->lock);
down_write(&symbols->lock);
symbols__sort_by_name_locked(symbols);
@@ -1004,7 +984,7 @@ int symbols__visit_by_name(struct symbols *symbols,
int err = symbols__sort_by_name_read_lock(symbols);
if (!err) {
- for (size_t i = 0; i < symbols->num_by_name; i++) {
+ for (size_t i = 0; i < symbols->cnt; i++) {
err = cb(symbols->symbols_by_name[i], data);
if (err)
break;
@@ -1266,8 +1246,6 @@ static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso)
root->allocated = 0;
root->sorted = true;
zfree(&root->symbols_by_name);
- root->num_by_name = 0;
- root->sorted_by_name = false;
up_write(&root->lock);
for (unsigned int i = 0; i < old_cnt; i++) {
@@ -1340,8 +1318,6 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
root->allocated = 0;
root->sorted = true;
zfree(&root->symbols_by_name);
- root->num_by_name = 0;
- root->sorted_by_name = false;
up_write(&root->lock);
for (i = 0; i < old_cnt; i++) {
@@ -1495,8 +1471,6 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
__symbols__insert(root, old_symbols[j]);
}
zfree(&root->symbols_by_name);
- root->num_by_name = 0;
- root->sorted_by_name = false;
up_write(&root->lock);
free(old_symbols);
@@ -1525,8 +1499,6 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
root->sorted = false;
}
zfree(&root->symbols_by_name);
- root->num_by_name = 0;
- root->sorted_by_name = false;
up_write(&root->lock);
free(old_symbols);
return -1;
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 8b8ab7e1e148..629f6b2fcd0a 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -102,26 +102,20 @@ struct symbols {
/** @lock: Read/write semaphore synchronizing access to the arrays. */
struct rw_semaphore lock;
/**
- * @symbols: Dynamically allocated array of symbol pointers, sorted by
- * address range [start, end) when @sorted is true.
+ * @symbols: Dynamically allocated array of @allocated symbol pointers,
+ * sorted by start address when @sorted is true.
*/
struct symbol **symbols __guarded_by(&lock);
/**
- * @symbols_by_name: Optional dynamically allocated array of symbol
- * pointers sorted lexicographically by sym->name.
+ * @symbols_by_name: Optional lazily built array of @cnt symbol pointers
+ * sorted lexicographically by sym->name; NULL when unbuilt, when @cnt
+ * is 0, or when invalidated by a symbol-table mutation.
*/
struct symbol **symbols_by_name __guarded_by(&lock);
/** @cnt: Number of valid entries in @symbols. */
unsigned int cnt __guarded_by(&lock);
/** @allocated: Capacity (in entries) of the @symbols allocation. */
unsigned int allocated __guarded_by(&lock);
- /** @num_by_name: Number of valid entries in @symbols_by_name. */
- unsigned int num_by_name __guarded_by(&lock);
- /**
- * @sorted_by_name: True when @symbols_by_name is populated and sorted
- * by name.
- */
- bool sorted_by_name __guarded_by(&lock);
/** @sorted: True when @symbols is sorted by address range. */
bool sorted __guarded_by(&lock);
};
base-commit: 964e8b1c9c795982886a6778cb0cd73c901ca4ca
--
2.56.1.809.ga0b61035b5-goog
next prev parent reply other threads:[~2026-11-24 3:29 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 3:28 [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
2026-11-24 3:28 ` Ian Rogers [this message]
2026-11-24 3:28 ` [PATCH v9 02/11] perf symbol: Replace sorted boolean with sorted_cnt prefix length Ian Rogers
2026-11-24 3:28 ` [PATCH v9 03/11] perf symbol: Add secondary sorted run for dso__insert_symbol() Ian Rogers
2026-11-24 3:28 ` [PATCH v9 04/11] perf test symbols: Add sorted insertion and equal-start alias tests Ian Rogers
2026-11-24 3:39 ` sashiko-bot
2026-11-24 3:28 ` [PATCH v9 05/11] perf dso: Synchronize dso_cache access and make dso->loaded atomic Ian Rogers
2026-11-24 3:28 ` [PATCH v9 06/11] perf symbol: Add symbols__shrink() and symbols__try_delete_unannotated() Ian Rogers
2026-11-24 3:28 ` [PATCH v9 07/11] perf dso: Add dso__shrink() and dso__try_evict() Ian Rogers
2026-11-24 3:28 ` [PATCH v9 08/11] perf session: Add dsos, machine, and perf_session shrinking Ian Rogers
2026-11-24 3:29 ` [PATCH v9 09/11] perf test symbols: Add symbol and DSO shrinking tests Ian Rogers
2026-11-24 3:29 ` [PATCH v9 10/11] perf session: Add dso__session() and session-scoped allocation wrappers Ian Rogers
2026-11-24 3:29 ` [PATCH v9 11/11] perf test symbols: Add session allocation wrapper and lock contention tests Ian Rogers
2026-11-24 3:49 ` [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124032902.3859564-2-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=haghdoost@uber.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox