]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[malloc] Track maximum heap usage
authorMichael Brown <mcb30@ipxe.org>
Wed, 22 Mar 2017 18:01:25 +0000 (20:01 +0200)
committerMichael Brown <mcb30@ipxe.org>
Wed, 22 Mar 2017 18:01:25 +0000 (20:01 +0200)
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>
src/core/malloc.c
src/include/ipxe/malloc.h

index 32c2035327fcec62387e1cbda5d88a73bd225028..6ddc08b7a88772b730fa96e680e0f048534a88db 100644 (file)
@@ -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 */
index dd158b8e65eb34db2e473a0a6b585fe2f12020f9..1878978fd443ac7492510bca2c140e0109373bea 100644 (file)
@@ -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 );