Linux Perf Users
 help / color / mirror / Atom feed
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 06/11] perf symbol: Add symbols__shrink() and symbols__try_delete_unannotated()
Date: Mon, 23 Nov 2026 19:28:57 -0800	[thread overview]
Message-ID: <20261124032902.3859564-7-irogers@google.com> (raw)
In-Reply-To: <20261124032902.3859564-1-irogers@google.com>

Add symbol-container memory shrinking helpers to struct symbols:
- symbols__shrink(): Reclaims the secondary name-sorted index
  (symbols_by_name, rebuilt on demand by dso__sort_by_name()) and trims
  unused over-allocated tail capacity in symbols->symbols while keeping
  all live struct symbol entries intact.
- symbols__try_delete_unannotated(): Deletes all symbols under
  symbols->lock when none have entries in the annotations hashmap, running
  an optional pre_delete callback before freeing the symbols.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/symbol.c | 61 ++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/symbol.h |  3 ++
 2 files changed, 64 insertions(+)

diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index f1abe3333ac0..1bcc36e011fc 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -253,6 +253,67 @@ void symbols__exit(struct symbols *symbols)
 	exit_rwsem(&symbols->lock);
 }
 
+bool symbols__try_delete_unannotated(struct symbols *symbols,
+				     void (*pre_delete)(void *ctx), void *ctx)
+{
+	down_write(&symbols->lock);
+	for (unsigned int i = 0; i < symbols->cnt; i++) {
+		if (symbol__annotated(symbols->symbols[i])) {
+			up_write(&symbols->lock);
+			return false;
+		}
+	}
+
+	if (pre_delete)
+		pre_delete(ctx);
+
+	symbols__delete_locked(symbols);
+	up_write(&symbols->lock);
+	return true;
+}
+
+/*
+ * Reclaim auxiliary memory from @symbols without freeing live symbol
+ * entries (whose lifetimes are owned by the containing struct dso):
+ * - Free the secondary name-sorted index (@symbols_by_name), which is
+ *   rebuilt on demand by dso__sort_by_name().
+ * - Trim unused over-allocated tail capacity in @symbols->symbols.
+ *
+ * Returns the number of buffers reclaimed/shrunk.
+ */
+size_t symbols__shrink(struct symbols *symbols)
+{
+	size_t freed = 0;
+
+	down_write(&symbols->lock);
+
+	if (symbols->symbols_by_name) {
+		zfree(&symbols->symbols_by_name);
+		freed++;
+	}
+
+	if (symbols->cnt == 0) {
+		if (symbols->allocated > 0) {
+			zfree(&symbols->symbols);
+			symbols->sorted_cnt = 0;
+			symbols->sorted_tail_cnt = 0;
+			symbols->allocated = 0;
+			freed++;
+		}
+	} else if (symbols->cnt < symbols->allocated) {
+		struct symbol **shrunk = realloc(symbols->symbols,
+						 symbols->cnt * sizeof(struct symbol *));
+		if (shrunk) {
+			symbols->symbols = shrunk;
+			symbols->allocated = symbols->cnt;
+			freed++;
+		}
+	}
+
+	up_write(&symbols->lock);
+	return freed;
+}
+
 /*
  * Order symbols by start address only, preserving insertion order among
  * symbols sharing the same start address (matching the previous rbtree
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 1aa54c1f1680..dd9be561618d 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -153,6 +153,9 @@ void symbol__delete(struct symbol *sym);
 void symbols__init(struct symbols *symbols);
 void symbols__exit(struct symbols *symbols);
 void symbols__delete(struct symbols *symbols);
+bool symbols__try_delete_unannotated(struct symbols *symbols,
+				     void (*pre_delete)(void *ctx), void *ctx);
+size_t symbols__shrink(struct symbols *symbols);
 void symbols__sort_read_lock(struct symbols *symbols) __acquires_shared(&symbols->lock);
 
 static inline u8 symbol__type(const struct symbol *sym)
-- 
2.56.1.809.ga0b61035b5-goog


  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 ` [PATCH v9 01/11] perf symbol: Drop num_by_name and sorted_by_name from struct symbols Ian Rogers
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 ` Ian Rogers [this message]
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-7-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