From 59502268151f2ad2746321c5adefce40ea4f5f66 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Thu, 1 Oct 2026 19:58:22 +0300 Subject: [PATCH 1/3] heap: vmh: separate per-alloc stats from error stats 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 --- zephyr/include/sof/lib/regions_mm.h | 1 + zephyr/lib/alloc.c | 6 +++++- zephyr/lib/regions_mm.c | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/zephyr/include/sof/lib/regions_mm.h b/zephyr/include/sof/lib/regions_mm.h index 6a22bfe7fd92..0e3b51c8bdce 100644 --- a/zephyr/include/sof/lib/regions_mm.h +++ b/zephyr/include/sof/lib/regions_mm.h @@ -80,6 +80,7 @@ void vmh_get_default_heap_config(const struct sys_mm_drv_region *region, struct vmh_heap_config *cfg); #ifdef CONFIG_SYS_MEM_BLOCKS_RUNTIME_STATS void vmh_log_stats(struct vmh_heap *heap); +void vmh_log_alloc_stats(struct vmh_heap *heap); #endif /** * @brief Checks if ptr is in range of given memory range diff --git a/zephyr/lib/alloc.c b/zephyr/lib/alloc.c index 2ce344133917..3b2069b9de97 100644 --- a/zephyr/lib/alloc.c +++ b/zephyr/lib/alloc.c @@ -296,7 +296,11 @@ static void *virtual_heap_alloc(struct vmh_heap *heap, uint32_t flags, size_t by assert(align == 0 || IS_ALIGNED(mem, align)); if (flags & SOF_MEM_FLAG_COHERENT) - return sys_cache_uncached_ptr_get((__sparse_force void __sparse_cache *)mem); + mem = sys_cache_uncached_ptr_get((__sparse_force void __sparse_cache *)mem); + +#ifdef CONFIG_SYS_MEM_BLOCKS_RUNTIME_STATS + vmh_log_alloc_stats(heap); +#endif return mem; } diff --git a/zephyr/lib/regions_mm.c b/zephyr/lib/regions_mm.c index 9b8db4b504af..d0caf0a269b3 100644 --- a/zephyr/lib/regions_mm.c +++ b/zephyr/lib/regions_mm.c @@ -741,8 +741,28 @@ void vmh_log_stats(struct vmh_heap *heap) (stats.max_allocated_bytes / block_size), heap->out_of_blocks[idx]); } + heap->logged = true; } + +void vmh_log_alloc_stats(struct vmh_heap *heap) +{ + size_t tot_allocated = 0, tot_free = 0; + + for (int idx = 0; idx < MAX_MEMORY_ALLOCATORS_COUNT; idx++) { + if (!heap->physical_blocks_allocators[idx]) + continue; + + struct sys_memory_stats stats = {0}; + + sys_mem_blocks_runtime_stats_get(heap->physical_blocks_allocators[idx], &stats); + + tot_allocated += stats.allocated_bytes; + tot_free += stats.free_bytes; + } + + LOG_INF("allocated %zu free %zu", tot_allocated, tot_free); +} #endif /** From fc5244d7890ff27cd9c8c6b97455c8b1bcfbf9c8 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Thu, 1 Oct 2026 21:03:34 +0300 Subject: [PATCH 2/3] heap: vregion: log per-alloc stats when CONFIG_SYS_HEAP_RUNTIME_STATS 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 --- zephyr/lib/vregion.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/zephyr/lib/vregion.c b/zephyr/lib/vregion.c index 1653d48a9a66..9fd38ca6ad35 100644 --- a/zephyr/lib/vregion.c +++ b/zephyr/lib/vregion.c @@ -507,6 +507,22 @@ void *z_impl_vregion_alloc_align(struct vregion *vr, k_mutex_unlock(&vr->lock); +#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); + } else { + struct sys_memory_stats stats; + + sys_heap_runtime_stats_get(&vr->interim.heap.heap, &stats); + + LOG_INF("interim alloc of %zu, used %u, free %u, max %u", + size, stats.allocated_bytes, stats.free_bytes, stats.max_allocated_bytes); + + } +#endif + return p; } EXPORT_SYMBOL(z_impl_vregion_alloc_align); From b5363b4af9edfd891b15fa66ec0f0d048d20ab88 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Fri, 2 Oct 2026 17:59:41 +0300 Subject: [PATCH 3/3] app: add debug_heap_allocs.conf overlay file 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 --- app/debug_heap_allocs.conf | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 app/debug_heap_allocs.conf diff --git a/app/debug_heap_allocs.conf b/app/debug_heap_allocs.conf new file mode 100644 index 000000000000..19641ec1b302 --- /dev/null +++ b/app/debug_heap_allocs.conf @@ -0,0 +1,9 @@ +# Use this overlay to increase log verbosity for +# all runtime heap allocs. +# +# Logs are emitted from all heap types (system heap 'sof_heap', +# virtual heap allocator, vregions, ...) +# + +CONFIG_SYS_HEAP_RUNTIME_STATS=y +CONFIG_SYS_MEM_BLOCKS_RUNTIME_STATS=y