Browse Source

[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 7 years ago
parent
commit
c90b4d82b7
2 changed files with 23 additions and 3 deletions
  1. 21
    3
      src/core/malloc.c
  2. 2
    0
      src/include/ipxe/malloc.h

+ 21
- 3
src/core/malloc.c View File

93
 /** Total amount of free memory */
93
 /** Total amount of free memory */
94
 size_t freemem;
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
  * Heap size
103
  * Heap size
98
  *
104
  *
351
 				VALGRIND_MAKE_MEM_NOACCESS ( pre,
357
 				VALGRIND_MAKE_MEM_NOACCESS ( pre,
352
 							     sizeof ( *pre ) );
358
 							     sizeof ( *pre ) );
353
 			}
359
 			}
354
-			/* Update total free memory */
360
+			/* Update memory usage statistics */
355
 			freemem -= actual_size;
361
 			freemem -= actual_size;
362
+			usedmem += actual_size;
363
+			if ( usedmem > maxusedmem )
364
+				maxusedmem = usedmem;
356
 			/* Return allocated block */
365
 			/* Return allocated block */
357
 			DBGC2 ( &heap, "Allocated [%p,%p)\n", block,
366
 			DBGC2 ( &heap, "Allocated [%p,%p)\n", block,
358
 				( ( ( void * ) block ) + size ) );
367
 				( ( ( void * ) block ) + size ) );
474
 		VALGRIND_MAKE_MEM_NOACCESS ( block, sizeof ( *block ) );
483
 		VALGRIND_MAKE_MEM_NOACCESS ( block, sizeof ( *block ) );
475
 	}
484
 	}
476
 
485
 
477
-	/* Update free memory counter */
486
+	/* Update memory usage statistics */
478
 	freemem += actual_size;
487
 	freemem += actual_size;
488
+	usedmem -= actual_size;
479
 
489
 
480
 	check_blocks();
490
 	check_blocks();
481
 	valgrind_make_blocks_noaccess();
491
 	valgrind_make_blocks_noaccess();
629
  * @c start must be aligned to at least a multiple of sizeof(void*).
639
  * @c start must be aligned to at least a multiple of sizeof(void*).
630
  */
640
  */
631
 void mpopulate ( void *start, size_t len ) {
641
 void mpopulate ( void *start, size_t len ) {
642
+
632
 	/* Prevent free_memblock() from rounding up len beyond the end
643
 	/* Prevent free_memblock() from rounding up len beyond the end
633
 	 * of what we were actually given...
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
  */
673
  */
657
 static void shutdown_cache ( int booting __unused ) {
674
 static void shutdown_cache ( int booting __unused ) {
658
 	discard_all_cache();
675
 	discard_all_cache();
676
+	DBGC ( &heap, "Maximum heap usage %zdkB\n", ( maxusedmem >> 10 ) );
659
 }
677
 }
660
 
678
 
661
 /** Memory allocator shutdown function */
679
 /** Memory allocator shutdown function */

+ 2
- 0
src/include/ipxe/malloc.h View File

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

Loading…
Cancel
Save