Skip to content

Commit 780f555

Browse files
committed
Add new alloc metric: peak_bytes_allocated.
This is just max value of current_bytes_allocated seen.
1 parent 02de0c5 commit 780f555

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

py/malloc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
static int total_bytes_allocated = 0;
77
static int current_bytes_allocated = 0;
8+
static int peak_bytes_allocated = 0;
9+
10+
#define UPDATE_PEAK() { if (current_bytes_allocated > peak_bytes_allocated) peak_bytes_allocated = current_bytes_allocated; }
811

912
void *m_malloc(int num_bytes) {
1013
if (num_bytes == 0) {
@@ -17,6 +20,7 @@ void *m_malloc(int num_bytes) {
1720
}
1821
total_bytes_allocated += num_bytes;
1922
current_bytes_allocated += num_bytes;
23+
UPDATE_PEAK();
2024
return ptr;
2125
}
2226

@@ -31,6 +35,7 @@ void *m_malloc0(int num_bytes) {
3135
}
3236
total_bytes_allocated += num_bytes;
3337
current_bytes_allocated += num_bytes;
38+
UPDATE_PEAK();
3439
return ptr;
3540
}
3641

@@ -52,6 +57,7 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) {
5257
int diff = new_num_bytes - old_num_bytes;
5358
total_bytes_allocated += diff;
5459
current_bytes_allocated += diff;
60+
UPDATE_PEAK();
5561
return ptr;
5662
}
5763

@@ -69,3 +75,7 @@ int m_get_total_bytes_allocated(void) {
6975
int m_get_current_bytes_allocated(void) {
7076
return current_bytes_allocated;
7177
}
78+
79+
int m_get_peak_bytes_allocated(void) {
80+
return peak_bytes_allocated;
81+
}

py/misc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void m_free(void *ptr, int num_bytes);
3333

3434
int m_get_total_bytes_allocated(void);
3535
int m_get_current_bytes_allocated(void);
36+
int m_get_peak_bytes_allocated(void);
3637

3738
/** unichar / UTF-8 *********************************************/
3839

0 commit comments

Comments
 (0)