Skip to content

heap: add heap debugging tools - #11264

Open
kv2019i wants to merge 3 commits into
thesofproject:mainfrom
kv2019i:202610-heap-debugging
Open

kv2019i wants to merge 3 commits into
thesofproject:mainfrom
kv2019i:202610-heap-debugging

Conversation

@kv2019i

@kv2019i kv2019i commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator

A series to help debugging heap usage issues. We already have infra to print logs for each alloc, but these were not documented for developers and didn't cover all the heaps (especially not newer "virtual heap" and "vregion").

This series add similar debugging for all major heap implementations (print out usage stats at each alloc), and adds an overlay to enable these all with a single overlay file.

Add vmh_log_alloc_stats() which prints only a single-line summary of
total allocated/free bytes; call it on every successful allocation in
virtual_heap_alloc() instead of the verbose vmh_log_stats(). Both
functions are compiled only when CONFIG_SYS_MEM_BLOCKS_RUNTIME_STATS
is set. This matches the debug capability in place for plain heap
allocs.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Add a single-line LOG_INF to z_impl_vregion_alloc_align() reporting
the lifetime allocator's used and free bytes after each successful
allocation. Gated on CONFIG_SYS_HEAP_RUNTIME_STATS.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Add a debug overlay file to enable alloc heap debugging, with
prints of memory usage via logging subsystem.

This is added as a separate file as this creates a notable increase in
logging traffic and is not something one wants enabled in all builds
(e.g. depends on the logging backend bandwidth).

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
@kv2019i

kv2019i commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author

@wjablon1 If you can check. This also adds on top of the commit 02c9391 you did.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Invalid vregion states can query uninitialized heap metadata, and the overlay does not reliably enable its INFO-level logs.

Review effort: Balanced
Findings: 3 Medium severity · 1 Low severity

Open (4)
What changed in this PR

Adds allocation-time diagnostics across SOF heap implementations and a unified debugging overlay.

Changes:

  • Logs vregion allocation statistics.
  • Adds aggregate virtual-heap usage logging after allocations.
  • Provides an overlay enabling runtime heap statistics.
File Description
zephyr/​lib/​vregion.c Logs lifetime and interim heap usage.
zephyr/​lib/​regions_mm.c Aggregates virtual-heap allocator statistics.
zephyr/​lib/​alloc.c Logs statistics after virtual-heap allocations.
zephyr/​include/​sof/​lib/​regions_mm.h Declares the statistics helper.
app/​debug_heap_allocs.conf Enables runtime statistics options.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +8 to +9
CONFIG_SYS_HEAP_RUNTIME_STATS=y
CONFIG_SYS_MEM_BLOCKS_RUNTIME_STATS=y
Comment thread zephyr/lib/vregion.c
Comment on lines +510 to +514
#if CONFIG_SYS_HEAP_RUNTIME_STATS
if (vr->type == VREGION_MEM_TYPE_LIFETIME) {
LOG_INF("lifetime alloc of %zu, used %zu, free %zu",
size, vr->lifetime.used,
vr->lifetime.size - vr->lifetime.used);
Comment thread zephyr/lib/vregion.c
Comment on lines +515 to +518
} else {
struct sys_memory_stats stats;

sys_heap_runtime_stats_get(&vr->interim.heap.heap, &stats);
Comment thread zephyr/lib/regions_mm.c
heap->logged = true;
}

void vmh_log_alloc_stats(struct vmh_heap *heap)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants