From c4c177069ee26bf27e04922a1d9f72c33ce3152a Mon Sep 17 00:00:00 2001 From: Karol Trzcinski Date: Wed, 2 Sep 2020 12:45:37 +0200 Subject: [PATCH] alloc: Add possibility to read memory usage in runtime Such a function may be used to monitor memory leaks and system utilization. Signed-off-by: Karol Trzcinski --- src/include/sof/lib/mm_heap.h | 8 +++++++ src/lib/alloc.c | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/include/sof/lib/mm_heap.h b/src/include/sof/lib/mm_heap.h index 4fa5bb02f..8567c677d 100644 --- a/src/include/sof/lib/mm_heap.h +++ b/src/include/sof/lib/mm_heap.h @@ -86,6 +86,14 @@ void free_heap(enum mem_zone zone); void heap_trace_all(int force); void heap_trace(struct mm_heap *heap, int size); +/** Fetch runtime information about heap, like used and free memory space + * @param zone to check, see enum mem_zone. + * @param index heap index, eg. cpu core index for any *SYS* zone + * @param out output variable + * @return error code or zero + */ +int heap_info(enum mem_zone zone, int index, struct mm_info *out); + /* retrieve memory map pointer */ static inline struct mm *memmap_get(void) { diff --git a/src/lib/alloc.c b/src/lib/alloc.c index f98c396a1..51ea617a7 100644 --- a/src/lib/alloc.c +++ b/src/lib/alloc.c @@ -1069,3 +1069,46 @@ void init_heap(struct sof *sof) platform_shared_commit(memmap, sizeof(*memmap)); } + +int heap_info(enum mem_zone zone, int index, struct mm_info *out) +{ + struct mm *memmap = memmap_get(); + struct mm_heap *heap; + + if (!out) + goto error; + + switch (zone) { + case SOF_MEM_ZONE_SYS: + if (index >= PLATFORM_HEAP_SYSTEM) + goto error; + heap = memmap->system + index; + break; + case SOF_MEM_ZONE_SYS_RUNTIME: + if (index >= PLATFORM_HEAP_SYSTEM_RUNTIME) + goto error; + heap = memmap->system_runtime + index; + break; + case SOF_MEM_ZONE_RUNTIME: + if (index >= PLATFORM_HEAP_RUNTIME) + goto error; + heap = memmap->runtime + index; + break; + case SOF_MEM_ZONE_BUFFER: + if (index >= PLATFORM_HEAP_BUFFER) + goto error; + heap = memmap->buffer + index; + break; + default: + goto error; + } + + spin_lock(&memmap->lock); + *out = heap->info; + spin_unlock(&memmap->lock); + return 0; +error: + tr_err(&mem_tr, "heap_info(): failed for zone 0x%x index %d out ptr 0x%x", zone, index, + (uint32_t)out); + return -EINVAL; +}