summaryrefslogtreecommitdiffstats
path: root/src/core
diff options
context:
space:
mode:
authorMichael Brown2017-03-22 19:01:25 +0100
committerMichael Brown2017-03-22 19:01:25 +0100
commitc90b4d82b7f98e8c470ed3199e6f59c0082c095a (patch)
treedbc4e45d85e6c9d12fdd3eaf42be7ef357d1b82d /src/core
parent[coverity] Add Coverity user model (diff)
downloadipxe-c90b4d82b7f98e8c470ed3199e6f59c0082c095a.tar.gz
ipxe-c90b4d82b7f98e8c470ed3199e6f59c0082c095a.tar.xz
ipxe-c90b4d82b7f98e8c470ed3199e6f59c0082c095a.zip
[malloc] Track maximum heap usage
Track the current and maximum heap usage, and display the maximum during shutdown when DEBUG=malloc is enabled. Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/core')
-rw-r--r--src/core/malloc.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/core/malloc.c b/src/core/malloc.c
index 32c20353..6ddc08b7 100644
--- a/src/core/malloc.c
+++ b/src/core/malloc.c
@@ -93,6 +93,12 @@ static LIST_HEAD ( free_blocks );
/** Total amount of free memory */
size_t freemem;
+/** Total amount of used memory */
+size_t usedmem;
+
+/** Maximum amount of used memory */
+size_t maxusedmem;
+
/**
* Heap size
*
@@ -351,8 +357,11 @@ void * alloc_memblock ( size_t size, size_t align, size_t offset ) {
VALGRIND_MAKE_MEM_NOACCESS ( pre,
sizeof ( *pre ) );
}
- /* Update total free memory */
+ /* Update memory usage statistics */
freemem -= actual_size;
+ usedmem += actual_size;
+ if ( usedmem > maxusedmem )
+ maxusedmem = usedmem;
/* Return allocated block */
DBGC2 ( &heap, "Allocated [%p,%p)\n", block,
( ( ( void * ) block ) + size ) );
@@ -474,8 +483,9 @@ void free_memblock ( void *ptr, size_t size ) {
VALGRIND_MAKE_MEM_NOACCESS ( block, sizeof ( *block ) );
}
- /* Update free memory counter */
+ /* Update memory usage statistics */
freemem += actual_size;
+ usedmem -= actual_size;
check_blocks();
valgrind_make_blocks_noaccess();
@@ -629,10 +639,17 @@ void * zalloc ( size_t size ) {
* @c start must be aligned to at least a multiple of sizeof(void*).
*/
void mpopulate ( void *start, size_t len ) {
+
/* Prevent free_memblock() from rounding up len beyond the end
* of what we were actually given...
*/
- free_memblock ( start, ( len & ~( MIN_MEMBLOCK_SIZE - 1 ) ) );
+ len &= ~( MIN_MEMBLOCK_SIZE - 1 );
+
+ /* Add to allocation pool */
+ free_memblock ( start, len );
+
+ /* Fix up memory usage statistics */
+ usedmem += len;
}
/**
@@ -656,6 +673,7 @@ struct init_fn heap_init_fn __init_fn ( INIT_EARLY ) = {
*/
static void shutdown_cache ( int booting __unused ) {
discard_all_cache();
+ DBGC ( &heap, "Maximum heap usage %zdkB\n", ( maxusedmem >> 10 ) );
}
/** Memory allocator shutdown function */