From 24b7bad115ea484ebb50aa059ad95f3431dcb2f6 Mon Sep 17 00:00:00 2001 From: Baofeng Tian Date: Thu, 3 Nov 2022 14:06:52 +0800 Subject: [PATCH] Memory: add support for memory heap profiling Heap memory profiling is based on runtime zephyr API. It will print out each memory allocation with allocated bytes and free bytes, display as below: zephyr: heap allocatd: 22c0 free: 152a90 max allocated: 1000 Signed-off-by: Baofeng Tian --- app/perf_overlay.conf | 1 + zephyr/wrapper.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/app/perf_overlay.conf b/app/perf_overlay.conf index 0da2a5414399..5c031fe97c59 100644 --- a/app/perf_overlay.conf +++ b/app/perf_overlay.conf @@ -1 +1,2 @@ CONFIG_PERFORMANCE_COUNTERS=y +CONFIG_SYS_HEAP_RUNTIME_STATS=y diff --git a/zephyr/wrapper.c b/zephyr/wrapper.c index 9c9939db2620..af201a7ade5a 100644 --- a/zephyr/wrapper.c +++ b/zephyr/wrapper.c @@ -34,6 +34,10 @@ #include #endif +#if CONFIG_SYS_HEAP_RUNTIME_STATS +#include +#endif + LOG_MODULE_REGISTER(zephyr, CONFIG_SOF_LOG_LEVEL); extern K_KERNEL_STACK_ARRAY_DEFINE(z_interrupt_stacks, CONFIG_MP_NUM_CPUS, @@ -108,11 +112,20 @@ static void *heap_alloc_aligned(struct k_heap *h, size_t min_align, size_t bytes { k_spinlock_key_t key; void *ret; +#if CONFIG_SYS_HEAP_RUNTIME_STATS + struct sys_memory_stats stats; +#endif key = k_spin_lock(&h->lock); ret = sys_heap_aligned_alloc(&h->heap, min_align, bytes); k_spin_unlock(&h->lock, key); +#if CONFIG_SYS_HEAP_RUNTIME_STATS + sys_heap_runtime_stats_get(&h->heap, &stats); + tr_info(&zephyr_tr, "heap allocated: %u free: %u max allocated: %u", + stats.allocated_bytes, stats.free_bytes, stats.max_allocated_bytes); +#endif + return ret; }