Skip to content

Commit 9d4e226

Browse files
committed
Track peak live memory via SP_FREE; report peak in banner
1 parent 2cc33a9 commit 9d4e226

3 files changed

Lines changed: 52 additions & 6 deletions

File tree

‎include/utils/tracked_alloc.h‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,61 @@
2121
#include <stddef.h>
2222
#include <stdlib.h>
2323

24-
extern size_t g_allocated_bytes;
24+
/* Platform shim for "how many usable bytes are at this malloc'd pointer".
25+
Apple's malloc_size, glibc's malloc_usable_size, MSVC's _msize. The
26+
returned size is the *usable* size (may exceed the requested size by a
27+
few bytes of allocator rounding), but alloc and free use the same
28+
function so the running totals stay symmetric. */
29+
#if defined(__APPLE__)
30+
#include <malloc/malloc.h>
31+
#define TRACKED_BLOCK_SIZE(p) malloc_size(p)
32+
#elif defined(_WIN32) || defined(_WIN64)
33+
#include <malloc.h>
34+
#define TRACKED_BLOCK_SIZE(p) _msize(p)
35+
#else
36+
#include <malloc.h>
37+
#define TRACKED_BLOCK_SIZE(p) malloc_usable_size(p)
38+
#endif
39+
40+
extern size_t g_allocated_bytes; /* current live bytes */
41+
extern size_t g_peak_bytes; /* high-water mark since last reset */
2542

2643
static inline void *SP_MALLOC(size_t size)
2744
{
2845
void *ptr = malloc(size);
29-
if (ptr) g_allocated_bytes += size;
46+
if (ptr)
47+
{
48+
g_allocated_bytes += TRACKED_BLOCK_SIZE(ptr);
49+
if (g_allocated_bytes > g_peak_bytes) g_peak_bytes = g_allocated_bytes;
50+
}
3051
return ptr;
3152
}
3253

3354
static inline void *SP_CALLOC(size_t count, size_t size)
3455
{
3556
void *ptr = calloc(count, size);
36-
if (ptr) g_allocated_bytes += count * size;
57+
if (ptr)
58+
{
59+
g_allocated_bytes += TRACKED_BLOCK_SIZE(ptr);
60+
if (g_allocated_bytes > g_peak_bytes) g_peak_bytes = g_allocated_bytes;
61+
}
3762
return ptr;
3863
}
3964

65+
static inline void SP_FREE(void *ptr)
66+
{
67+
if (ptr)
68+
{
69+
g_allocated_bytes -= TRACKED_BLOCK_SIZE(ptr);
70+
free(ptr);
71+
}
72+
}
73+
74+
/* Auto-route plain malloc/calloc/free in caller translation units through
75+
the tracked wrappers. Defined AFTER the wrapper bodies so SP_MALLOC /
76+
SP_CALLOC / SP_FREE themselves still see the real stdlib symbols. */
77+
#define malloc SP_MALLOC
78+
#define calloc SP_CALLOC
79+
#define free SP_FREE
80+
4081
#endif /* TRACKED_ALLOC_H */

‎src/problem.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ static void problem_lagrange_hess_fill_sparsity(problem *prob, int *iwork);
3232
problem *new_problem(expr *objective, expr **constraints, int n_constraints,
3333
bool verbose)
3434
{
35-
g_allocated_bytes = 0;
35+
/* Baseline the peak watermark to the current live bytes — peak then
36+
captures any growth during this problem's lifetime. Don't reset
37+
g_allocated_bytes itself: allocations made before new_problem are
38+
still alive, and their frees would subtract from this counter. */
39+
g_peak_bytes = g_allocated_bytes;
3640
problem *prob = (problem *) SP_CALLOC(1, sizeof(problem));
3741
if (!prob) return NULL;
3842

@@ -321,7 +325,7 @@ static inline void print_end_message(const Diff_engine_stats *stats)
321325
printf(" Lagrange Hessian (nnz): %d\n", stats->nnz_hessian);
322326
char mem_buf[64];
323327
format_memory(stats->memory_bytes, mem_buf, sizeof(mem_buf));
324-
printf(" Allocated memory: %s\n", mem_buf);
328+
printf(" Peak memory: %s\n", mem_buf);
325329

326330
printf("\nTiming (seconds):\n");
327331
printf(" Derivative structure (sparsity): %8.3f\n",
@@ -349,7 +353,7 @@ void free_problem(problem *prob)
349353

350354
if (prob->verbose)
351355
{
352-
prob->stats.memory_bytes = g_allocated_bytes;
356+
prob->stats.memory_bytes = g_peak_bytes;
353357
print_end_message(&prob->stats);
354358
}
355359

‎src/utils/tracked_alloc.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
#include "utils/tracked_alloc.h"
1919

2020
size_t g_allocated_bytes = 0;
21+
size_t g_peak_bytes = 0;

0 commit comments

Comments
 (0)