sys: heap: support maximum allocated bytes statistic
Besides the current allocated/free bytes, keep track of the maximum allocated bytes to help determine the heap size requirements. Also, provide a function to reset the statistic. Signed-off-by: Damian Krolik <damian.krolik@nordicsemi.no>
This commit is contained in:
parent
eba9c872b1
commit
5f5410a0cc
4 changed files with 37 additions and 3 deletions
|
@ -70,6 +70,7 @@ struct z_heap_stress_result {
|
||||||
struct sys_heap_runtime_stats {
|
struct sys_heap_runtime_stats {
|
||||||
size_t free_bytes;
|
size_t free_bytes;
|
||||||
size_t allocated_bytes;
|
size_t allocated_bytes;
|
||||||
|
size_t max_allocated_bytes;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -82,6 +83,17 @@ struct sys_heap_runtime_stats {
|
||||||
int sys_heap_runtime_stats_get(struct sys_heap *heap,
|
int sys_heap_runtime_stats_get(struct sys_heap *heap,
|
||||||
struct sys_heap_runtime_stats *stats);
|
struct sys_heap_runtime_stats *stats);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Reset the maximum heap usage.
|
||||||
|
*
|
||||||
|
* Set the statistic measuring the maximum number of allocated bytes to the
|
||||||
|
* current number of allocated bytes.
|
||||||
|
*
|
||||||
|
* @param heap Pointer to sys_heap
|
||||||
|
* @return -EINVAL if null pointer was passed, otherwise 0
|
||||||
|
*/
|
||||||
|
int sys_heap_runtime_stats_reset_max(struct sys_heap *heap);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** @brief Initialize sys_heap
|
/** @brief Initialize sys_heap
|
||||||
|
|
|
@ -422,6 +422,18 @@ int sys_heap_runtime_stats_get(struct sys_heap *heap,
|
||||||
|
|
||||||
stats->free_bytes = heap->heap->free_bytes;
|
stats->free_bytes = heap->heap->free_bytes;
|
||||||
stats->allocated_bytes = heap->heap->allocated_bytes;
|
stats->allocated_bytes = heap->heap->allocated_bytes;
|
||||||
|
stats->max_allocated_bytes = heap->heap->max_allocated_bytes;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sys_heap_runtime_stats_reset_max(struct sys_heap *heap)
|
||||||
|
{
|
||||||
|
if (heap == NULL) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
heap->heap->max_allocated_bytes = heap->heap->allocated_bytes;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,14 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "heap.h"
|
#include "heap.h"
|
||||||
|
|
||||||
|
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
|
||||||
|
static inline void increase_allocated_bytes(struct z_heap *h, size_t num_bytes)
|
||||||
|
{
|
||||||
|
h->allocated_bytes += num_bytes;
|
||||||
|
h->max_allocated_bytes = MAX(h->max_allocated_bytes, h->allocated_bytes);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static void *chunk_mem(struct z_heap *h, chunkid_t c)
|
static void *chunk_mem(struct z_heap *h, chunkid_t c)
|
||||||
{
|
{
|
||||||
chunk_unit_t *buf = chunk_buf(h);
|
chunk_unit_t *buf = chunk_buf(h);
|
||||||
|
@ -275,7 +283,7 @@ void *sys_heap_alloc(struct sys_heap *heap, size_t bytes)
|
||||||
mem = chunk_mem(h, c);
|
mem = chunk_mem(h, c);
|
||||||
|
|
||||||
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
|
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
|
||||||
h->allocated_bytes += chunksz_to_bytes(h, chunk_size(h, c));
|
increase_allocated_bytes(h, chunksz_to_bytes(h, chunk_size(h, c)));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_SYS_HEAP_LISTENER
|
#ifdef CONFIG_SYS_HEAP_LISTENER
|
||||||
|
@ -351,7 +359,7 @@ void *sys_heap_aligned_alloc(struct sys_heap *heap, size_t align, size_t bytes)
|
||||||
|
|
||||||
set_chunk_used(h, c, true);
|
set_chunk_used(h, c, true);
|
||||||
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
|
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
|
||||||
h->allocated_bytes += chunksz_to_bytes(h, chunk_size(h, c));
|
increase_allocated_bytes(h, chunksz_to_bytes(h, chunk_size(h, c)));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_SYS_HEAP_LISTENER
|
#ifdef CONFIG_SYS_HEAP_LISTENER
|
||||||
|
@ -425,7 +433,7 @@ void *sys_heap_aligned_realloc(struct sys_heap *heap, void *ptr,
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
|
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
|
||||||
h->allocated_bytes += split_size * CHUNK_UNIT;
|
increase_allocated_bytes(h, split_size * CHUNK_UNIT);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
free_list_remove(h, rc);
|
free_list_remove(h, rc);
|
||||||
|
@ -498,6 +506,7 @@ void sys_heap_init(struct sys_heap *heap, void *mem, size_t bytes)
|
||||||
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
|
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
|
||||||
h->free_bytes = 0;
|
h->free_bytes = 0;
|
||||||
h->allocated_bytes = 0;
|
h->allocated_bytes = 0;
|
||||||
|
h->max_allocated_bytes = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int nb_buckets = bucket_idx(h, heap_sz) + 1;
|
int nb_buckets = bucket_idx(h, heap_sz) + 1;
|
||||||
|
|
|
@ -72,6 +72,7 @@ struct z_heap {
|
||||||
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
|
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
|
||||||
size_t free_bytes;
|
size_t free_bytes;
|
||||||
size_t allocated_bytes;
|
size_t allocated_bytes;
|
||||||
|
size_t max_allocated_bytes;
|
||||||
#endif
|
#endif
|
||||||
struct z_heap_bucket buckets[0];
|
struct z_heap_bucket buckets[0];
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue