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 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 /** 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);