summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/core/malloc.c24
-rw-r--r--src/include/ipxe/malloc.h2
2 files changed, 23 insertions, 3 deletions
diff --git a/src/core/malloc.c b/src/core/malloc.c
index 32c203532..6ddc08b7a 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 */
diff --git a/src/include/ipxe/malloc.h b/src/include/ipxe/malloc.h
index dd158b8e6..1878978fd 100644
--- a/src/include/ipxe/malloc.h
+++ b/src/include/ipxe/malloc.h
@@ -22,6 +22,8 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <valgrind/memcheck.h>
extern size_t freemem;
+extern size_t usedmem;
+extern size_t maxusedmem;
extern void * __malloc alloc_memblock ( size_t size, size_t align,
size_t offset );