Procházet zdrojové kódy

[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>
tags/v1.20.1
Michael Brown před 7 roky
rodič
revize
c90b4d82b7
2 změnil soubory, kde provedl 23 přidání a 3 odebrání
  1. 21
    3
      src/core/malloc.c
  2. 2
    0
      src/include/ipxe/malloc.h

+ 21
- 3
src/core/malloc.c Zobrazit soubor

@@ -93,6 +93,12 @@ static LIST_HEAD ( free_blocks );
93 93
 /** Total amount of free memory */
94 94
 size_t freemem;
95 95
 
96
+/** Total amount of used memory */
97
+size_t usedmem;
98
+
99
+/** Maximum amount of used memory */
100
+size_t maxusedmem;
101
+
96 102
 /**
97 103
  * Heap size
98 104
  *
@@ -351,8 +357,11 @@ void * alloc_memblock ( size_t size, size_t align, size_t offset ) {
351 357
 				VALGRIND_MAKE_MEM_NOACCESS ( pre,
352 358
 							     sizeof ( *pre ) );
353 359
 			}
354
-			/* Update total free memory */
360
+			/* Update memory usage statistics */
355 361
 			freemem -= actual_size;
362
+			usedmem += actual_size;
363
+			if ( usedmem > maxusedmem )
364
+				maxusedmem = usedmem;
356 365
 			/* Return allocated block */
357 366
 			DBGC2 ( &heap, "Allocated [%p,%p)\n", block,
358 367
 				( ( ( void * ) block ) + size ) );
@@ -474,8 +483,9 @@ void free_memblock ( void *ptr, size_t size ) {
474 483
 		VALGRIND_MAKE_MEM_NOACCESS ( block, sizeof ( *block ) );
475 484
 	}
476 485
 
477
-	/* Update free memory counter */
486
+	/* Update memory usage statistics */
478 487
 	freemem += actual_size;
488
+	usedmem -= actual_size;
479 489
 
480 490
 	check_blocks();
481 491
 	valgrind_make_blocks_noaccess();
@@ -629,10 +639,17 @@ void * zalloc ( size_t size ) {
629 639
  * @c start must be aligned to at least a multiple of sizeof(void*).
630 640
  */
631 641
 void mpopulate ( void *start, size_t len ) {
642
+
632 643
 	/* Prevent free_memblock() from rounding up len beyond the end
633 644
 	 * of what we were actually given...
634 645
 	 */
635
-	free_memblock ( start, ( len & ~( MIN_MEMBLOCK_SIZE - 1 ) ) );
646
+	len &= ~( MIN_MEMBLOCK_SIZE - 1 );
647
+
648
+	/* Add to allocation pool */
649
+	free_memblock ( start, len );
650
+
651
+	/* Fix up memory usage statistics */
652
+	usedmem += len;
636 653
 }
637 654
 
638 655
 /**
@@ -656,6 +673,7 @@ struct init_fn heap_init_fn __init_fn ( INIT_EARLY ) = {
656 673
  */
657 674
 static void shutdown_cache ( int booting __unused ) {
658 675
 	discard_all_cache();
676
+	DBGC ( &heap, "Maximum heap usage %zdkB\n", ( maxusedmem >> 10 ) );
659 677
 }
660 678
 
661 679
 /** Memory allocator shutdown function */

+ 2
- 0
src/include/ipxe/malloc.h Zobrazit soubor

@@ -22,6 +22,8 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
22 22
 #include <valgrind/memcheck.h>
23 23
 
24 24
 extern size_t freemem;
25
+extern size_t usedmem;
26
+extern size_t maxusedmem;
25 27
 
26 28
 extern void * __malloc alloc_memblock ( size_t size, size_t align,
27 29
 					size_t offset );

Načítá se…
Zrušit
Uložit