How to get stats for heap size usage when using nRF Connect SDK with CONFIG_COMMON_LIBC_MALLOC=y

As recommended by the documentation I am using the malloc/free in the common libc branch.

This code (in zephyr/lib/libc/common/source/stdlib/malloc.c) uses sys_heap_init() with a struct sys_heap that it creates (in my config with CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=-1) using a heap_base just after _end, and running to the end of RAM. (around 300K).

I would like to be able to log the heap current usage and high water mark during the run of the firmware.

I have found the posts about enabling CONFIG_SYS_HEAP_RUNTIME_STATS and using code like this:

    uint32_t heap_free = 0;
    uint32_t heap_used = 0;
    uint32_t heap_max_used = 0;
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
    extern struct k_heap _system_heap;
    struct sys_memory_stats stats;
    sys_heap_runtime_stats_get(&_system_heap.heap, &stats);
    heap_used = (uint32_t)stats.allocated_bytes;
    heap_max_used = (uint32_t)stats.max_allocated_bytes;
    heap_free = (uint32_t)stats.free_bytes;
#endif
    log_info("System Stats: Heap %d at boot, now : free %u, used %u, max used %u. Malloc says total alloc todate is %u", _boot_heap_size, heap_free, heap_used, heap_max_used, _allocated_total);
(note I calculate _boot_heap_size  myself with the same calculation used by malloc_prepare(), and track _allocated_total in my wrapper round malloc)
The values for stats.allocated_bytes, stats.max_allocated_bytes, stats.free_bytes are not at all as expected eg:
[00:57:03.272,064] <inf> base: System Stats: Heap 251192 at boot, now : free 3380, used 632, max used 912. Malloc says total alloc todate is 165901
Is this because the 'k_heap_system_heap' is not the same heap used by malloc (seems to be Z_LIBC_DATA static struct sys_heap z_malloc_heap; in malloc.c)?
How can i get the 'real' heap used by malloc in this case (without hacking malloc.c to make it not be static...)?
btw, sys_heap_print_info(&_system_heap.heap,true); gives me the same log output...


thanks
Related