feat(heap): Add feature to get peak heap usage

This feature keeps track of the per task peak memory usage.

- Update the heap_task_tracking example to make use of the new feature
Cleanup the implementation:
- multi_heap_get_free_size() is never used, remove it.
- Minor update in heap_caps_update_per_task_info_xx() funcitons.
- Update settting on block owner in heap_caps.c to work with the
get peak usage feature.

- Update heap_caps_update_per_task_info_free() to detect when it
is called to delete the memory allocated for a task TCB. Mark
the corresponding task in the statistic list as deleted.

- Add a Kconfig option dependant on HEAP_TASK_TRACKING being enabled
that force the deletion of the statistics related to deleted task
when set to true.

- In task tracking feature, add a current and peak memory usage
to the heap_stat_t structure to keep track of the current and
peak memory usage of the given task across all heaps.

- Fix missing block owner when allocating memory for heaps_array
in heap_caps_init.

- Keep the original implementation of the task tracking
for backward compatibility reasons.
This commit is contained in:
Guillaume Souchere
2025-02-14 08:47:29 +01:00
parent 47df2ed524
commit daf8f9edb6
16 changed files with 1368 additions and 75 deletions

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -12,6 +12,11 @@
#include "multi_heap.h"
#include "esp_log.h"
#include "heap_private.h"
#if CONFIG_HEAP_TASK_TRACKING
#include "esp_heap_task_info.h"
#include "esp_heap_task_info_internal.h"
#include "multi_heap_internal.h"
#endif
#ifdef CONFIG_HEAP_USE_HOOKS
#define CALL_HOOK(hook, ...) { \
@@ -72,6 +77,11 @@ HEAP_IRAM_ATTR void heap_caps_free( void *ptr)
void *block_owner_ptr = MULTI_HEAP_REMOVE_BLOCK_OWNER_OFFSET(ptr);
heap_t *heap = find_containing_heap(block_owner_ptr);
assert(heap != NULL && "free() target pointer is outside heap areas");
#if CONFIG_HEAP_TASK_TRACKING
heap_caps_update_per_task_info_free(heap, ptr);
#endif
multi_heap_free(heap->heap, block_owner_ptr);
CALL_HOOK(esp_heap_trace_free_hook, ptr);
@@ -145,6 +155,13 @@ HEAP_IRAM_ATTR NOINLINE_ATTR void *heap_caps_aligned_alloc_base(size_t alignment
ret = aligned_or_unaligned_alloc(heap->heap, MULTI_HEAP_ADD_BLOCK_OWNER_SIZE(size) + 4,
alignment, MULTI_HEAP_BLOCK_OWNER_SIZE()); // int overflow checked above
if (ret != NULL) {
#if CONFIG_HEAP_TASK_TRACKING
heap_caps_update_per_task_info_alloc(heap,
MULTI_HEAP_ADD_BLOCK_OWNER_OFFSET(ret),
multi_heap_get_full_block_size(heap->heap, ret),
get_all_caps(heap));
#endif
MULTI_HEAP_SET_BLOCK_OWNER(ret);
ret = MULTI_HEAP_ADD_BLOCK_OWNER_OFFSET(ret);
uint32_t *iptr = dram_alloc_to_iram_addr(ret, size + 4); // int overflow checked above
@@ -156,6 +173,13 @@ HEAP_IRAM_ATTR NOINLINE_ATTR void *heap_caps_aligned_alloc_base(size_t alignment
ret = aligned_or_unaligned_alloc(heap->heap, MULTI_HEAP_ADD_BLOCK_OWNER_SIZE(size),
alignment, MULTI_HEAP_BLOCK_OWNER_SIZE());
if (ret != NULL) {
#if CONFIG_HEAP_TASK_TRACKING
heap_caps_update_per_task_info_alloc(heap,
MULTI_HEAP_ADD_BLOCK_OWNER_OFFSET(ret),
multi_heap_get_full_block_size(heap->heap, ret),
get_all_caps(heap));
#endif
MULTI_HEAP_SET_BLOCK_OWNER(ret);
ret = MULTI_HEAP_ADD_BLOCK_OWNER_OFFSET(ret);
CALL_HOOK(esp_heap_trace_alloc_hook, ret, size, caps);
@@ -240,9 +264,25 @@ HEAP_IRAM_ATTR NOINLINE_ATTR void *heap_caps_realloc_base( void *ptr, size_t siz
if (compatible_caps && !ptr_in_diram_case && alignment<=UNALIGNED_MEM_ALIGNMENT_BYTES) {
// try to reallocate this memory within the same heap
// (which will resize the block if it can)
#if CONFIG_HEAP_TASK_TRACKING
size_t old_size = multi_heap_get_full_block_size(heap->heap, ptr);
TaskHandle_t old_task = MULTI_HEAP_GET_BLOCK_OWNER(ptr);
#endif
void *r = multi_heap_realloc(heap->heap, ptr, MULTI_HEAP_ADD_BLOCK_OWNER_SIZE(size));
if (r != NULL) {
MULTI_HEAP_SET_BLOCK_OWNER(r);
#if CONFIG_HEAP_TASK_TRACKING
heap_caps_update_per_task_info_realloc(heap,
MULTI_HEAP_ADD_BLOCK_OWNER_OFFSET(ptr),
MULTI_HEAP_ADD_BLOCK_OWNER_OFFSET(r),
old_size, old_task,
multi_heap_get_full_block_size(heap->heap, r),
get_all_caps(heap));
#endif
r = MULTI_HEAP_ADD_BLOCK_OWNER_OFFSET(r);
CALL_HOOK(esp_heap_trace_alloc_hook, r, size, caps);
return r;