diff options
author | Damien George <damien.p.george@gmail.com> | 2014-01-03 10:49:55 -0800 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-01-03 10:49:55 -0800 |
commit | 91c8d8e71c7780eb17f5f73bdde89eece42e7e98 (patch) | |
tree | 4f814850702894ca8a54fa2e7a7c6027ee3302b4 /py | |
parent | 9b87b39ab47f381521377d3a9456a0a1651f4cae (diff) | |
parent | b372bfca21ccab593359ef25a0a0c6bf697c8586 (diff) | |
download | micropython-91c8d8e71c7780eb17f5f73bdde89eece42e7e98.tar.gz micropython-91c8d8e71c7780eb17f5f73bdde89eece42e7e98.zip |
Merge pull request #41 from pfalcon/more-mem-stats
Collect more memory statistics
Diffstat (limited to 'py')
-rw-r--r-- | py/malloc.c | 50 | ||||
-rw-r--r-- | py/misc.h | 2 | ||||
-rw-r--r-- | py/mpconfig.h | 13 |
3 files changed, 64 insertions, 1 deletions
diff --git a/py/malloc.c b/py/malloc.c index c65d38a968..4f01dc63f5 100644 --- a/py/malloc.c +++ b/py/malloc.c @@ -2,8 +2,15 @@ #include <stdlib.h> #include "misc.h" +#include "mpconfig.h" +#if MICROPY_MEM_STATS static int total_bytes_allocated = 0; +static int current_bytes_allocated = 0; +static int peak_bytes_allocated = 0; + +#define UPDATE_PEAK() { if (current_bytes_allocated > peak_bytes_allocated) peak_bytes_allocated = current_bytes_allocated; } +#endif void *m_malloc(int num_bytes) { if (num_bytes == 0) { @@ -14,7 +21,11 @@ void *m_malloc(int num_bytes) { printf("could not allocate memory, allocating %d bytes\n", num_bytes); return NULL; } +#if MICROPY_MEM_STATS total_bytes_allocated += num_bytes; + current_bytes_allocated += num_bytes; + UPDATE_PEAK(); +#endif return ptr; } @@ -27,7 +38,11 @@ void *m_malloc0(int num_bytes) { printf("could not allocate memory, allocating %d bytes\n", num_bytes); return NULL; } +#if MICROPY_MEM_STATS total_bytes_allocated += num_bytes; + current_bytes_allocated += num_bytes; + UPDATE_PEAK(); +#endif return ptr; } @@ -41,7 +56,17 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) { printf("could not allocate memory, reallocating %d bytes\n", new_num_bytes); return NULL; } - total_bytes_allocated += new_num_bytes; +#if MICROPY_MEM_STATS + // At first thought, "Total bytes allocated" should only grow, + // after all, it's *total*. But consider for example 2K block + // shrunk to 1K and then grown to 2K again. It's still 2K + // allocated total. If we process only positive increments, + // we'll count 3K. + int diff = new_num_bytes - old_num_bytes; + total_bytes_allocated += diff; + current_bytes_allocated += diff; + UPDATE_PEAK(); +#endif return ptr; } @@ -49,8 +74,31 @@ void m_free(void *ptr, int num_bytes) { if (ptr != NULL) { free(ptr); } +#if MICROPY_MEM_STATS + current_bytes_allocated -= num_bytes; +#endif } int m_get_total_bytes_allocated(void) { +#if MICROPY_MEM_STATS return total_bytes_allocated; +#else + return -1; +#endif +} + +int m_get_current_bytes_allocated(void) { +#if MICROPY_MEM_STATS + return current_bytes_allocated; +#else + return -1; +#endif +} + +int m_get_peak_bytes_allocated(void) { +#if MICROPY_MEM_STATS + return peak_bytes_allocated; +#else + return -1; +#endif } @@ -32,6 +32,8 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes); void m_free(void *ptr, int num_bytes); int m_get_total_bytes_allocated(void); +int m_get_current_bytes_allocated(void); +int m_get_peak_bytes_allocated(void); /** unichar / UTF-8 *********************************************/ diff --git a/py/mpconfig.h b/py/mpconfig.h new file mode 100644 index 0000000000..17c5a770c4 --- /dev/null +++ b/py/mpconfig.h @@ -0,0 +1,13 @@ +// This file contains default configuration settings for MicroPython. +// You can override any of these options using mpconfigport.h file located +// in a directory of your port. + +#include <mpconfigport.h> + +// Any options not explicitly set in mpconfigport.h will get default +// values below. + +// Whether to collect memory allocation stats +#ifndef MICROPY_MEM_STATS +#define MICROPY_MEM_STATS (1) +#endif |