From 43f1c8080ae209f41b95a9a390f0596c454b30d9 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 1 Jan 2014 23:04:25 +0200 Subject: [PATCH 1/5] m_realloc: Account only allocation size difference in total_bytes_allocated. --- py/malloc.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/py/malloc.c b/py/malloc.c index c65d38a968..a94edd3fe9 100644 --- a/py/malloc.c +++ b/py/malloc.c @@ -41,7 +41,12 @@ 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; + // 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. + total_bytes_allocated += new_num_bytes - old_num_bytes; return ptr; } From 02de0c57d233bf969ed3971c319a96dd9f998549 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 1 Jan 2014 23:15:47 +0200 Subject: [PATCH 2/5] Add new alloc metric: current_bytes_allocated. Unlike total_bytes_allocated, this tracks m_free()'s too. --- py/malloc.c | 12 +++++++++++- py/misc.h | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/py/malloc.c b/py/malloc.c index a94edd3fe9..13f0a8fc3e 100644 --- a/py/malloc.c +++ b/py/malloc.c @@ -4,6 +4,7 @@ #include "misc.h" static int total_bytes_allocated = 0; +static int current_bytes_allocated = 0; void *m_malloc(int num_bytes) { if (num_bytes == 0) { @@ -15,6 +16,7 @@ void *m_malloc(int num_bytes) { return NULL; } total_bytes_allocated += num_bytes; + current_bytes_allocated += num_bytes; return ptr; } @@ -28,6 +30,7 @@ void *m_malloc0(int num_bytes) { return NULL; } total_bytes_allocated += num_bytes; + current_bytes_allocated += num_bytes; return ptr; } @@ -46,7 +49,9 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) { // 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. - total_bytes_allocated += new_num_bytes - old_num_bytes; + int diff = new_num_bytes - old_num_bytes; + total_bytes_allocated += diff; + current_bytes_allocated += diff; return ptr; } @@ -54,8 +59,13 @@ void m_free(void *ptr, int num_bytes) { if (ptr != NULL) { free(ptr); } + current_bytes_allocated -= num_bytes; } int m_get_total_bytes_allocated(void) { return total_bytes_allocated; } + +int m_get_current_bytes_allocated(void) { + return current_bytes_allocated; +} diff --git a/py/misc.h b/py/misc.h index 9f83ab526f..383d3986a7 100644 --- a/py/misc.h +++ b/py/misc.h @@ -32,6 +32,7 @@ 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); /** unichar / UTF-8 *********************************************/ From 780f555b2e36b0d468209e0ad741e8261faf63e5 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 1 Jan 2014 23:42:21 +0200 Subject: [PATCH 3/5] Add new alloc metric: peak_bytes_allocated. This is just max value of current_bytes_allocated seen. --- py/malloc.c | 10 ++++++++++ py/misc.h | 1 + 2 files changed, 11 insertions(+) diff --git a/py/malloc.c b/py/malloc.c index 13f0a8fc3e..a3736cde4d 100644 --- a/py/malloc.c +++ b/py/malloc.c @@ -5,6 +5,9 @@ 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; } void *m_malloc(int num_bytes) { if (num_bytes == 0) { @@ -17,6 +20,7 @@ void *m_malloc(int num_bytes) { } total_bytes_allocated += num_bytes; current_bytes_allocated += num_bytes; + UPDATE_PEAK(); return ptr; } @@ -31,6 +35,7 @@ void *m_malloc0(int num_bytes) { } total_bytes_allocated += num_bytes; current_bytes_allocated += num_bytes; + UPDATE_PEAK(); return ptr; } @@ -52,6 +57,7 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) { int diff = new_num_bytes - old_num_bytes; total_bytes_allocated += diff; current_bytes_allocated += diff; + UPDATE_PEAK(); return ptr; } @@ -69,3 +75,7 @@ int m_get_total_bytes_allocated(void) { int m_get_current_bytes_allocated(void) { return current_bytes_allocated; } + +int m_get_peak_bytes_allocated(void) { + return peak_bytes_allocated; +} diff --git a/py/misc.h b/py/misc.h index 383d3986a7..153218ba2f 100644 --- a/py/misc.h +++ b/py/misc.h @@ -33,6 +33,7 @@ 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 *********************************************/ From ef18102b9ec9634653fd4a8286990e9e21ce2ae0 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 3 Jan 2014 03:06:25 +0200 Subject: [PATCH 4/5] Make it possible to turn off collecting memory stats (MICROPY_MEM_STATS). --- py/defaultconfig.h | 7 +++++++ py/malloc.c | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 py/defaultconfig.h diff --git a/py/defaultconfig.h b/py/defaultconfig.h new file mode 100644 index 0000000000..9829bf707d --- /dev/null +++ b/py/defaultconfig.h @@ -0,0 +1,7 @@ +// This file contains default configuration settings for MicroPython. +// You can override any of these options in mpconfig.h for your port. + +// Whether to collect memory allocation stats +#ifndef MICROPY_MEM_STATS +#define MICROPY_MEM_STATS (1) +#endif diff --git a/py/malloc.c b/py/malloc.c index a3736cde4d..1765eb6743 100644 --- a/py/malloc.c +++ b/py/malloc.c @@ -2,12 +2,16 @@ #include #include "misc.h" +#include "mpconfig.h" +#include "defaultconfig.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) { @@ -18,9 +22,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; } @@ -33,9 +39,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; } @@ -49,6 +57,7 @@ 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; } +#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 @@ -58,6 +67,7 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) { total_bytes_allocated += diff; current_bytes_allocated += diff; UPDATE_PEAK(); +#endif return ptr; } @@ -65,17 +75,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 } From b372bfca21ccab593359ef25a0a0c6bf697c8586 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 3 Jan 2014 17:15:53 +0200 Subject: [PATCH 5/5] Rename default config file to mpconfig.h, and port's to mpconfigport.h. mpconfig.h will automatically pull mpconfigport.h. --- py/defaultconfig.h | 7 ------- py/malloc.c | 1 - py/mpconfig.h | 13 +++++++++++++ stm/Makefile | 2 +- stm/{mpconfig.h => mpconfigport.h} | 2 ++ unix/Makefile | 4 ++-- unix/{mpconfig.h => mpconfigport.h} | 0 7 files changed, 18 insertions(+), 11 deletions(-) delete mode 100644 py/defaultconfig.h create mode 100644 py/mpconfig.h rename stm/{mpconfig.h => mpconfigport.h} (96%) rename unix/{mpconfig.h => mpconfigport.h} (100%) diff --git a/py/defaultconfig.h b/py/defaultconfig.h deleted file mode 100644 index 9829bf707d..0000000000 --- a/py/defaultconfig.h +++ /dev/null @@ -1,7 +0,0 @@ -// This file contains default configuration settings for MicroPython. -// You can override any of these options in mpconfig.h for your port. - -// Whether to collect memory allocation stats -#ifndef MICROPY_MEM_STATS -#define MICROPY_MEM_STATS (1) -#endif diff --git a/py/malloc.c b/py/malloc.c index 1765eb6743..4f01dc63f5 100644 --- a/py/malloc.c +++ b/py/malloc.c @@ -3,7 +3,6 @@ #include "misc.h" #include "mpconfig.h" -#include "defaultconfig.h" #if MICROPY_MEM_STATS static int total_bytes_allocated = 0; 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 + +// 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 diff --git a/stm/Makefile b/stm/Makefile index 018d31f491..d6c77e2bd7 100644 --- a/stm/Makefile +++ b/stm/Makefile @@ -187,7 +187,7 @@ $(BUILD)/%.o: $(PYSRC)/%.s $(BUILD)/%.o: $(PYSRC)/%.S $(CC) $(CFLAGS) -c -o $@ $< -$(BUILD)/%.o: $(PYSRC)/%.c mpconfig.h +$(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h $(CC) $(CFLAGS) -c -o $@ $< $(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h diff --git a/stm/mpconfig.h b/stm/mpconfigport.h similarity index 96% rename from stm/mpconfig.h rename to stm/mpconfigport.h index 1f9529e11b..4cea332f39 100644 --- a/stm/mpconfig.h +++ b/stm/mpconfigport.h @@ -1,3 +1,5 @@ +#include + // options to control how Micro Python is built #define MICROPY_ENABLE_FLOAT (1) diff --git a/unix/Makefile b/unix/Makefile index 271cf22654..fd5b6b43e0 100644 --- a/unix/Makefile +++ b/unix/Makefile @@ -79,7 +79,7 @@ $(BUILD)/%.o: %.c $(BUILD)/%.o: $(PYSRC)/%.S $(CC) $(CFLAGS) -c -o $@ $< -$(BUILD)/%.o: $(PYSRC)/%.c mpconfig.h +$(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h $(CC) $(CFLAGS) -c -o $@ $< $(BUILD)/emitnx64.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h @@ -92,7 +92,7 @@ $(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h $(BUILD)/vm.o: $(PYSRC)/vm.c $(CC) $(CFLAGS) -O3 -c -o $@ $< -$(BUILD)/main.o: mpconfig.h +$(BUILD)/main.o: mpconfigport.h $(BUILD)/parse.o: $(PYSRC)/grammar.h $(BUILD)/compile.o: $(PYSRC)/grammar.h $(BUILD)/emitcpy.o: $(PYSRC)/emit.h diff --git a/unix/mpconfig.h b/unix/mpconfigport.h similarity index 100% rename from unix/mpconfig.h rename to unix/mpconfigport.h