Skip to content

Commit 24b7bad

Browse files
committed
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 <baofeng.tian@intel.com>
1 parent d9aed37 commit 24b7bad

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

app/perf_overlay.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
CONFIG_PERFORMANCE_COUNTERS=y
2+
CONFIG_SYS_HEAP_RUNTIME_STATS=y

zephyr/wrapper.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
#include <zephyr/arch/xtensa/cache.h>
3535
#endif
3636

37+
#if CONFIG_SYS_HEAP_RUNTIME_STATS
38+
#include <zephyr/sys/sys_heap.h>
39+
#endif
40+
3741
LOG_MODULE_REGISTER(zephyr, CONFIG_SOF_LOG_LEVEL);
3842

3943
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
108112
{
109113
k_spinlock_key_t key;
110114
void *ret;
115+
#if CONFIG_SYS_HEAP_RUNTIME_STATS
116+
struct sys_memory_stats stats;
117+
#endif
111118

112119
key = k_spin_lock(&h->lock);
113120
ret = sys_heap_aligned_alloc(&h->heap, min_align, bytes);
114121
k_spin_unlock(&h->lock, key);
115122

123+
#if CONFIG_SYS_HEAP_RUNTIME_STATS
124+
sys_heap_runtime_stats_get(&h->heap, &stats);
125+
tr_info(&zephyr_tr, "heap allocated: %u free: %u max allocated: %u",
126+
stats.allocated_bytes, stats.free_bytes, stats.max_allocated_bytes);
127+
#endif
128+
116129
return ret;
117130
}
118131

0 commit comments

Comments
 (0)