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 v11 2/5] perf symbol: Drop num_by_name and sorted_by_name from struct symbols
Date: Mon, 30 Nov 2026 14:04:33 -0800 [thread overview]
Message-ID: <20261130220437.4099852-3-irogers@google.com> (raw)
In-Reply-To: <20261130220437.4099852-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 a6f667e9dedc..aa9fb69bf6e5 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -243,9 +243,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)
@@ -258,9 +256,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)
@@ -532,8 +528,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);
}
@@ -767,8 +761,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;
}
@@ -871,17 +863,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;
@@ -889,8 +873,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;
}
@@ -900,7 +882,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);
@@ -909,7 +891,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,
@@ -994,8 +976,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;
}
@@ -1037,7 +1017,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];
}
@@ -1061,10 +1041,10 @@ struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_
symbols = dso__named_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);
}
}
@@ -1110,7 +1090,7 @@ void dso__sort_by_name(struct dso *dso)
struct symbols *symbols = dso__named_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);
@@ -1127,7 +1107,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;
@@ -1389,8 +1369,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++) {
@@ -1463,8 +1441,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++) {
@@ -1618,8 +1594,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);
@@ -1648,8 +1622,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 64fdf402d219..e6e511920926 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -114,26 +114,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);
};
--
2.56.1.839.g22a8290db9-goog
next prev parent reply other threads:[~2026-11-30 22:04 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 22:04 [PATCH v11 0/5] perf symbol: Fix alias end fixup and optimize sorted array Ian Rogers
2026-11-30 22:04 ` [PATCH v11 1/5] perf symbol: Fix symbols__fixup_end() for equal-start zero-sized aliases Ian Rogers
2026-11-30 22:04 ` Ian Rogers [this message]
2026-11-30 22:04 ` [PATCH v11 3/5] perf symbol: Replace sorted boolean with sorted_cnt prefix length Ian Rogers
2026-11-30 22:04 ` [PATCH v11 4/5] perf symbol: Add secondary sorted run for dso__insert_symbol() Ian Rogers
2026-11-30 22:04 ` [PATCH v11 5/5] perf test symbols: Add sorted insertion and equal-start alias tests Ian Rogers
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=20261130220437.4099852-3-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
all inboxes | Powered by JetHome®