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: 2018-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2018-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -74,6 +74,53 @@ typedef struct {
size_t max_blocks; ///< Capacity of array of task block info structs
} heap_task_info_params_t;
/** @brief Structure providing details about memory usage of a given task on a heap. */
typedef struct {
const char *name; ///< Pointer to the name of the heap defined in soc_memory_types[]
uint32_t caps; ///< All caps supported by the heap (ORED)
size_t size; ///< The available size of the heap
size_t current_usage; ///< The current usage of a given task on the heap
size_t peak_usage; ///< The peak usage since startup on a given task on the heap
size_t alloc_count; ///< The current number of allocation by a given task on the heap
heap_task_block_t *alloc_stat; ///< Pointer to an array of allocation stats for a given task on the heap
} heap_stat_t;
/** @brief Structure providing details about a task. */
typedef struct {
char name[configMAX_TASK_NAME_LEN]; ///< Name of the task
TaskHandle_t handle; ///< Pointer to the task handle.
bool is_alive; ///< Information whether the task is alive (true) or deleted (false)
size_t overall_peak_usage; ///< Information about the memory peak usage across all heaps of a given task
size_t overall_current_usage; ///< Information about the memory current usage across all heaps of a given task
size_t heap_count; ///< Number of different heaps the task has used since its creation
heap_stat_t *heap_stat; ///< Pointer to an array containing statistics of the heaps used by the task
} task_stat_t;
/**
* @brief User interface containing the statistics of a given task
* and the associated memory usage of the task on each heap.
*/
typedef struct {
task_stat_t stat; ///< Statistics of the task
size_t heap_count; ///< size of user defined heap_stat array
heap_stat_t *heap_stat_start; ///> Pointer to the start to the user defined heap_stat array
size_t alloc_count; ///< size of user defined alloc_stat array
heap_task_block_t *alloc_stat_start; ///> Pointer to the start to the user defined alloc_stat array
} heap_single_task_stat_t;
/**
* @brief User interface containing the statistics of all tasks and the associated
* memory usage of those tasks on each heap they use.
*/
typedef struct {
size_t task_count; ///< user defined size of heap_single_task_stat_t array
task_stat_t *stat_arr; ///< Pointer to the user defined array of heap_single_task_stat_t
size_t heap_count; ///< size of user defined heap_stat array
heap_stat_t *heap_stat_start; ///> Pointer to the start to the user defined heap_stat array
size_t alloc_count; ///< size of user defined alloc_stat array
heap_task_block_t *alloc_stat_start; ///> Pointer to the start to the user defined alloc_stat array
} heap_all_tasks_stat_t;
/**
* @brief Return per-task heap allocation totals and lists of blocks.
*
@@ -89,6 +136,115 @@ typedef struct {
*/
extern size_t heap_caps_get_per_task_info(heap_task_info_params_t *params);
/**
* @brief Return per-task heap memory usage and associated allocation information on each heap
* for all tasks.
*
* For each task that has allocated memory from the heap, return information of memory usage and
* allocation information of the task on each heap the task has used.
*
* @param tasks_stat Structure to hold the memory usage statistics of all tasks
* (@see heap_all_tasks_stat_t).
* @return ESP_OK if the information were gathered successfully.
* ESP_ERR_INVALID_ARG if the user defined field in heap_all_tasks_stat_t are not set properly
*/
esp_err_t heap_caps_get_all_task_stat(heap_all_tasks_stat_t *tasks_stat);
/**
* @brief Return heap memory usage and associated allocation information on each heap for a given task.
*
* @param[in] task_handle handle of the task. If NULL, the function will get the current task
* handle and return the statistics of this task.
* @param[out] task_stat Structure to hold the memory usage statistics of the task defined by task_handle
* @return ESP_OK if the information were gathered successfully.
* ESP_ERR_INVALID_ARG if the user defined field in heap_single_task_stat_t are not set properly
*/
esp_err_t heap_caps_get_single_task_stat(heap_single_task_stat_t *task_stat, TaskHandle_t task_handle);
/**
* @brief Print heap memory usage and associated allocation information on each heap for all created tasks
* since startup (running and deleted ones when CONFIG_HEAP_TRACK_DELETED_TASKS is enabled).
*
* @note This function is an alternative to heap_caps_get_all_task_stat if the goal is just to print information
* and not manipulate them.
*/
void heap_caps_print_all_task_stat(void);
/**
* @brief Print summary information of all tasks
*
* @note The information printed by this function is an array formatted log of task_stat_t content for each running
* task (and deleted ones if HEAP_TRACK_DELETED_TASKS is enabled)
*/
void heap_caps_print_all_task_stat_overview(void);
/**
* @brief Print heap memory usage and associated allocation information on each heap for a given task.
*
* @note This function is an alternative to heap_caps_get_single_task_stat if the goal is just to print information
* and not manipulate them.
*
* @param task_handle The task handle of the task to get memory usage and associated allocation information from.
*/
void heap_caps_print_single_task_stat(TaskHandle_t task_handle);
/**
* @brief Print summary information of a given task
*
* @note The information printed by this function is an array formatted log of task_stat_t content for the given
* task. This function will not print the task summary information if the given task is deleted and
* HEAP_TRACK_DELETED_TASKS is disabled.
*
* @param task_handle The task handle of the task to get memory usage and associated allocation information from.
*/
void heap_caps_print_single_task_stat_overview(TaskHandle_t task_handle);
/**
* @brief Allocate the memory used to store the heap and alloc statistics and fill task_stat
* with the pointer to those allocations and the number of heaps and allocs statistics available
* for the given task.
*
* @note If NULL is passed as parameter for the task_handle, the information on the currently running
* task will be returned. This function should be called prior to heap_caps_get_single_task_stat() if the user
* wishes to use dynamic allocation to store statistics.
*
* @param task_handle The task from which to get the information. If NULL,
* this function will return the number of heap used by the calling task.
* @param task_stat Structure containing information filled by this function.
* @return ESP_OK if the memory necessary to gather the statistics was allocated successfully.
* ESP_FAIL if not enough memory space is available to store all statistics.
*/
esp_err_t heap_caps_alloc_single_task_stat_arrays(heap_single_task_stat_t *task_stat, TaskHandle_t task_handle);
/**
* @brief Free the memory allocated to store heap and alloc statistics by calling
* heap_caps_alloc_single_task_stat_arrays.
*
* @param task_stat Structure from which to free the allocated memory used to store statistics
*/
void heap_caps_free_single_task_stat_arrays(heap_single_task_stat_t *task_stat);
/**
* @brief Allocate the memory used to store the tasks, heaps and allocs statistics and fill tasks_stat
* with the pointer to those allocations and the number of tasks, heaps and allocs statistics available.
*
* @note This function should be called prior to heap_caps_get_all_task_stat() if the user
* wishes to use dynamic allocation to store statistics.
*
* @param tasks_stat Structure containing information filled by this function.
* @return ESP_OK if the memory necessary to gather the statistics was allocated successfully.
* ESP_FAIL if not enough memory space is available to store all statistics.
*/
esp_err_t heap_caps_alloc_all_task_stat_arrays(heap_all_tasks_stat_t *tasks_stat);
/**
* @brief Free the memory allocated to store task, heap and alloc statistics
* by calling heap_caps_alloc_all_task_stat_arrays.
*
* @param tasks_stat Structure from which to free the allocated memory used to store statistics
*/
void heap_caps_free_all_task_stat_arrays(heap_all_tasks_stat_t *tasks_stat);
#ifdef __cplusplus
}
#endif