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 v10 08/12] perf dso: Add dso__shrink() and dso__try_evict()
Date: Tue, 24 Nov 2026 14:31:05 -0800 [thread overview]
Message-ID: <20261124223109.820609-9-irogers@google.com> (raw)
In-Reply-To: <20261124223109.820609-1-irogers@google.com>
Add per-DSO memory shrinking and unreferenced DSO eviction helpers:
- dso__shrink(): Without blocking on contended locks, closes cached DSO
data file descriptors, frees cached 4KB data pages, addr2line handles,
and libdw DWARF contexts. When a reloadable user DSO in a dsos
collection has no external references (refcnt == 1) and no annotated
symbols or inlines, evicts its symbol table via
symbols__try_delete_unannotated() and marks dso->loaded false so it
reloads on demand; otherwise calls symbols__shrink().
- dso__try_evict(): Evicts an unreferenced user DSO (refcnt == 1, no
symbols, not kernel/vDSO/hit/header_build_id, and not on
dso__data_open) without blocking on locks.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/dso.c | 215 +++++++++++++++++++++++++++++++++++++++++-
tools/perf/util/dso.h | 9 ++
2 files changed, 222 insertions(+), 2 deletions(-)
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 83611d7ccaff..b6217c49dd14 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -17,6 +17,7 @@
#include <sys/types.h>
#include <unistd.h>
+#include "annotate.h"
#include "annotate-data.h"
#include "auxtrace.h"
#include "compress.h"
@@ -1874,7 +1875,7 @@ struct dso *dso__new(const char *name)
return dso__new_id(name, NULL);
}
-void dso__delete(struct dso *dso)
+static void __dso__delete(struct dso *dso, bool close_data)
{
if (dso__dsos(dso))
pr_err("DSO %s is still in rbtree when being deleted!\n", dso__long_name(dso));
@@ -1896,7 +1897,8 @@ void dso__delete(struct dso *dso)
RC_CHK_ACCESS(dso)->long_name_allocated = false;
}
- dso__data_close(dso);
+ if (close_data)
+ dso__data_close(dso);
auxtrace_cache__free(RC_CHK_ACCESS(dso)->auxtrace_cache);
dso_cache__free(dso);
dso__free_a2l(dso);
@@ -1908,6 +1910,215 @@ void dso__delete(struct dso *dso)
RC_CHK_FREE(dso);
}
+void dso__delete(struct dso *dso)
+{
+ __dso__delete(dso, /*close_data=*/true);
+}
+
+bool dso__can_shrink_symbols(const struct dso *dso)
+{
+ const char *name;
+
+ if (!dso || !dso__loaded(dso))
+ return false;
+
+ if (dso__kernel(dso) || dso__is_kmod(dso))
+ return false;
+
+ if (dso__is_vdso(dso))
+ return false;
+
+ switch (dso__binary_type(dso)) {
+ case DSO_BINARY_TYPE__KALLSYMS:
+ case DSO_BINARY_TYPE__GUEST_KALLSYMS:
+ case DSO_BINARY_TYPE__VMLINUX:
+ case DSO_BINARY_TYPE__GUEST_VMLINUX:
+ case DSO_BINARY_TYPE__KCORE:
+ case DSO_BINARY_TYPE__GUEST_KCORE:
+ case DSO_BINARY_TYPE__BPF_PROG_INFO:
+ case DSO_BINARY_TYPE__BPF_IMAGE:
+ case DSO_BINARY_TYPE__OOL:
+ case DSO_BINARY_TYPE__JAVA_JIT:
+ case DSO_BINARY_TYPE__GUEST_KMODULE:
+ case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
+ case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
+ case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
+ return false;
+ case DSO_BINARY_TYPE__DEBUGLINK:
+ case DSO_BINARY_TYPE__BUILD_ID_CACHE:
+ case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
+ case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
+ case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
+ case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
+ case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
+ case DSO_BINARY_TYPE__GNU_DEBUGDATA:
+ case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
+ case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
+ case DSO_BINARY_TYPE__NOT_FOUND:
+ default:
+ break;
+ }
+
+ switch (dso__symtab_type(dso)) {
+ case DSO_BINARY_TYPE__KALLSYMS:
+ case DSO_BINARY_TYPE__GUEST_KALLSYMS:
+ case DSO_BINARY_TYPE__VMLINUX:
+ case DSO_BINARY_TYPE__GUEST_VMLINUX:
+ case DSO_BINARY_TYPE__KCORE:
+ case DSO_BINARY_TYPE__GUEST_KCORE:
+ case DSO_BINARY_TYPE__BPF_PROG_INFO:
+ case DSO_BINARY_TYPE__BPF_IMAGE:
+ case DSO_BINARY_TYPE__OOL:
+ case DSO_BINARY_TYPE__JAVA_JIT:
+ case DSO_BINARY_TYPE__GUEST_KMODULE:
+ case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
+ case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
+ case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
+ case DSO_BINARY_TYPE__NOT_FOUND:
+ return false;
+ case DSO_BINARY_TYPE__DEBUGLINK:
+ case DSO_BINARY_TYPE__BUILD_ID_CACHE:
+ case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
+ case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
+ case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
+ case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
+ case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
+ case DSO_BINARY_TYPE__GNU_DEBUGDATA:
+ case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
+ case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
+ default:
+ break;
+ }
+
+ name = dso__long_name(dso);
+ if (!name || name[0] == '[' || is_perf_pid_map_name(name) ||
+ strstr(name, " (deleted)") != NULL)
+ return false;
+
+ return true;
+}
+
+static bool dso__has_annotated_inlines(struct dso *dso)
+{
+ struct rb_root_cached *tree = dso__inlined_nodes(dso);
+ struct rb_node *nd;
+
+ for (nd = rb_first_cached(tree); nd; nd = rb_next(nd)) {
+ struct inline_node *node = rb_entry(nd, struct inline_node, rb_node);
+ struct inline_list *ilist;
+
+ list_for_each_entry(ilist, &node->val, list) {
+ if (ilist->symbol && symbol__annotated(ilist->symbol))
+ return true;
+ }
+ }
+ return false;
+}
+
+static void dso__delete_inlines_and_srclines(void *ctx)
+{
+ struct dso *dso = ctx;
+
+ /* Free inlines before symbols, as inline frames reference symbols. */
+ inlines__tree_delete(dso__inlined_nodes(dso));
+ srcline__tree_delete(dso__srclines(dso));
+}
+
+size_t dso__shrink(struct dso *dso)
+{
+ size_t freed = 0;
+
+ if (!dso)
+ return 0;
+
+ if (mutex_trylock(&dso__data_open_lock)) {
+ if (dso__data(dso)->fd >= 0) {
+ close_dso(dso);
+ freed++;
+ }
+ mutex_unlock(&dso__data_open_lock);
+ }
+
+ if (!mutex_trylock(dso__lock(dso)))
+ return freed;
+
+ if (!dso__kernel(dso) && dso__binary_type(dso) != DSO_BINARY_TYPE__OOL &&
+ !RB_EMPTY_ROOT(&dso__data(dso)->cache)) {
+ __dso_cache__free(dso);
+ freed++;
+ }
+ if (dso__a2l(dso)) {
+ dso__free_a2l(dso);
+ freed++;
+ }
+#ifdef HAVE_LIBBFD_SUPPORT
+ if (dso__a2l_libbfd(dso)) {
+ dso__free_a2l_libbfd(dso);
+ freed++;
+ }
+#endif
+#ifdef HAVE_LIBDW_SUPPORT
+ if (dso__libdw(dso)) {
+ dso__free_libdw(dso);
+ freed++;
+ }
+#endif
+
+ if (dso__dsos(dso) && refcount_read(dso__refcnt(dso)) == 1 &&
+ dso__can_shrink_symbols(dso) && dso__has_symbols(dso) &&
+ !dso__has_annotated_inlines(dso) &&
+ symbols__try_delete_unannotated(dso__symbols(dso),
+ dso__delete_inlines_and_srclines, dso)) {
+ atomic_store_explicit(&RC_CHK_ACCESS(dso)->loaded, false,
+ memory_order_release);
+ freed++;
+ } else {
+ freed += symbols__shrink(dso__symbols(dso));
+ }
+
+ mutex_unlock(dso__lock(dso));
+ return freed;
+}
+
+/*
+ * Try to evict an unreferenced @dso from its owning struct dsos (whose write
+ * lock must be held by the caller) without blocking on any lock.
+ */
+bool dso__try_evict(struct dso *dso)
+{
+ bool can_evict = false;
+
+ if (!dso ||
+ dso__has_symbols(dso) ||
+ dso__header_build_id(dso) ||
+ dso__hit(dso) ||
+ dso__kernel(dso) ||
+ dso__is_vdso(dso) ||
+ refcount_read(dso__refcnt(dso)) != 1)
+ return false;
+
+ if (!mutex_trylock(&dso__data_open_lock))
+ return false;
+
+ if (dso__data(dso)->fd < 0 &&
+ list_empty(&dso__data(dso)->open_entry) &&
+ refcount_read(dso__refcnt(dso)) == 1 &&
+ mutex_trylock(dso__lock(dso))) {
+ can_evict = !dso__has_symbols(dso) && !dso__has_annotated_inlines(dso);
+ mutex_unlock(dso__lock(dso));
+ }
+ mutex_unlock(&dso__data_open_lock);
+ if (!can_evict)
+ return false;
+
+ dso__set_dsos(dso, NULL);
+ if (refcount_dec_and_test(&RC_CHK_ACCESS(dso)->refcnt))
+ __dso__delete(dso, /*close_data=*/false);
+ else
+ RC_CHK_PUT(dso);
+ return true;
+}
+
struct dso *dso__get(struct dso *dso)
{
struct dso *result;
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index 70a121fd7ecd..b0ee45a53f40 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -789,6 +789,15 @@ static inline bool dso__has_symbols(const struct dso *dso)
return res;
}
+static inline refcount_t *dso__refcnt(struct dso *dso)
+{
+ return &RC_CHK_ACCESS(dso)->refcnt;
+}
+
+bool dso__can_shrink_symbols(const struct dso *dso);
+bool dso__try_evict(struct dso *dso);
+size_t dso__shrink(struct dso *dso);
+
char *dso__filename_with_chroot(const struct dso *dso, const char *filename);
bool dso__sorted_by_name(const struct dso *dso);
--
2.56.1.831.g720f3f5937-goog
next prev parent reply other threads:[~2026-11-24 22:31 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 22:30 [PATCH v10 00/12] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
2026-11-24 22:30 ` [PATCH v10 01/12] perf symbol: Fix symbols__fixup_end() for equal-start zero-sized aliases Ian Rogers
2026-11-24 22:41 ` sashiko-bot
2026-11-24 22:30 ` [PATCH v10 02/12] perf symbol: Drop num_by_name and sorted_by_name from struct symbols Ian Rogers
2026-11-24 22:31 ` [PATCH v10 03/12] perf symbol: Replace sorted boolean with sorted_cnt prefix length Ian Rogers
2026-11-24 22:31 ` [PATCH v10 04/12] perf symbol: Add secondary sorted run for dso__insert_symbol() Ian Rogers
2026-11-24 22:31 ` [PATCH v10 05/12] perf test symbols: Add sorted insertion and equal-start alias tests Ian Rogers
2026-11-24 22:31 ` [PATCH v10 06/12] perf dso: Synchronize dso_cache access and make dso->loaded atomic Ian Rogers
2026-11-24 22:31 ` [PATCH v10 07/12] perf symbol: Add symbols__shrink() and symbols__try_delete_unannotated() Ian Rogers
2026-11-24 22:31 ` Ian Rogers [this message]
2026-11-24 22:31 ` [PATCH v10 09/12] perf session: Add dsos, machine, and perf_session shrinking Ian Rogers
2026-11-24 22:31 ` [PATCH v10 10/12] perf test symbols: Add symbol and DSO shrinking tests Ian Rogers
2026-11-24 22:31 ` [PATCH v10 11/12] perf session: Add dso__session() and session-scoped allocation wrappers Ian Rogers
2026-11-24 23:55 ` Alireza Haghdoost
2026-11-24 22:31 ` [PATCH v10 12/12] perf test symbols: Add session allocation wrapper and lock contention tests Ian Rogers
2026-11-24 22:54 ` [PATCH v10 00/12] 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=20261124223109.820609-9-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.