Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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>
  • Loading branch information
mcb30 committed Mar 22, 2017
1 parent 65a3518 commit c90b4d8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/core/malloc.c
Expand Up @@ -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
*
Expand Down Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions src/include/ipxe/malloc.h
Expand Up @@ -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 );
Expand Down

0 comments on commit c90b4d8

Please sign in to comment.