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 <karolx.trzcinski@linux.intel.com>
This commit is contained in:
Karol Trzcinski 2020-09-02 12:45:37 +02:00 committed by Liam Girdwood
parent 7c4c3c86e1
commit c4c177069e
2 changed files with 51 additions and 0 deletions

View File

@ -86,6 +86,14 @@ void free_heap(enum mem_zone zone);
void heap_trace_all(int force); void heap_trace_all(int force);
void heap_trace(struct mm_heap *heap, int size); 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 */ /* retrieve memory map pointer */
static inline struct mm *memmap_get(void) static inline struct mm *memmap_get(void)
{ {

View File

@ -1069,3 +1069,46 @@ void init_heap(struct sof *sof)
platform_shared_commit(memmap, sizeof(*memmap)); 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;
}