Skip to content
Snippets Groups Projects
Commit 91c8d8e7 authored by Damien George's avatar Damien George
Browse files

Merge pull request #41 from pfalcon/more-mem-stats

Collect more memory statistics
parents 9b87b39a b372bfca
No related branches found
No related tags found
No related merge requests found
...@@ -2,8 +2,15 @@ ...@@ -2,8 +2,15 @@
#include <stdlib.h> #include <stdlib.h>
#include "misc.h" #include "misc.h"
#include "mpconfig.h"
#if MICROPY_MEM_STATS
static int total_bytes_allocated = 0; 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) { void *m_malloc(int num_bytes) {
if (num_bytes == 0) { if (num_bytes == 0) {
...@@ -14,7 +21,11 @@ void *m_malloc(int num_bytes) { ...@@ -14,7 +21,11 @@ void *m_malloc(int num_bytes) {
printf("could not allocate memory, allocating %d bytes\n", num_bytes); printf("could not allocate memory, allocating %d bytes\n", num_bytes);
return NULL; return NULL;
} }
#if MICROPY_MEM_STATS
total_bytes_allocated += num_bytes; total_bytes_allocated += num_bytes;
current_bytes_allocated += num_bytes;
UPDATE_PEAK();
#endif
return ptr; return ptr;
} }
...@@ -27,7 +38,11 @@ void *m_malloc0(int num_bytes) { ...@@ -27,7 +38,11 @@ void *m_malloc0(int num_bytes) {
printf("could not allocate memory, allocating %d bytes\n", num_bytes); printf("could not allocate memory, allocating %d bytes\n", num_bytes);
return NULL; return NULL;
} }
#if MICROPY_MEM_STATS
total_bytes_allocated += num_bytes; total_bytes_allocated += num_bytes;
current_bytes_allocated += num_bytes;
UPDATE_PEAK();
#endif
return ptr; return ptr;
} }
...@@ -41,7 +56,17 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) { ...@@ -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); printf("could not allocate memory, reallocating %d bytes\n", new_num_bytes);
return NULL; 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; return ptr;
} }
...@@ -49,8 +74,31 @@ void m_free(void *ptr, int num_bytes) { ...@@ -49,8 +74,31 @@ void m_free(void *ptr, int num_bytes) {
if (ptr != NULL) { if (ptr != NULL) {
free(ptr); free(ptr);
} }
#if MICROPY_MEM_STATS
current_bytes_allocated -= num_bytes;
#endif
} }
int m_get_total_bytes_allocated(void) { int m_get_total_bytes_allocated(void) {
#if MICROPY_MEM_STATS
return total_bytes_allocated; 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); ...@@ -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); void m_free(void *ptr, int num_bytes);
int m_get_total_bytes_allocated(void); int m_get_total_bytes_allocated(void);
int m_get_current_bytes_allocated(void);
int m_get_peak_bytes_allocated(void);
/** unichar / UTF-8 *********************************************/ /** unichar / UTF-8 *********************************************/
......
// 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
...@@ -187,7 +187,7 @@ $(BUILD)/%.o: $(PYSRC)/%.s ...@@ -187,7 +187,7 @@ $(BUILD)/%.o: $(PYSRC)/%.s
$(BUILD)/%.o: $(PYSRC)/%.S $(BUILD)/%.o: $(PYSRC)/%.S
$(CC) $(CFLAGS) -c -o $@ $< $(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.c mpconfig.h $(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h
$(CC) $(CFLAGS) -c -o $@ $< $(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h $(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h
......
#include <stdint.h>
// options to control how Micro Python is built // options to control how Micro Python is built
#define MICROPY_ENABLE_FLOAT (1) #define MICROPY_ENABLE_FLOAT (1)
......
...@@ -79,7 +79,7 @@ $(BUILD)/%.o: %.c ...@@ -79,7 +79,7 @@ $(BUILD)/%.o: %.c
$(BUILD)/%.o: $(PYSRC)/%.S $(BUILD)/%.o: $(PYSRC)/%.S
$(CC) $(CFLAGS) -c -o $@ $< $(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.c mpconfig.h $(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h
$(CC) $(CFLAGS) -c -o $@ $< $(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/emitnx64.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h $(BUILD)/emitnx64.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h
...@@ -92,7 +92,7 @@ $(BUILD)/emitnthumb.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 $(BUILD)/vm.o: $(PYSRC)/vm.c
$(CC) $(CFLAGS) -O3 -c -o $@ $< $(CC) $(CFLAGS) -O3 -c -o $@ $<
$(BUILD)/main.o: mpconfig.h $(BUILD)/main.o: mpconfigport.h
$(BUILD)/parse.o: $(PYSRC)/grammar.h $(BUILD)/parse.o: $(PYSRC)/grammar.h
$(BUILD)/compile.o: $(PYSRC)/grammar.h $(BUILD)/compile.o: $(PYSRC)/grammar.h
$(BUILD)/emitcpy.o: $(PYSRC)/emit.h $(BUILD)/emitcpy.o: $(PYSRC)/emit.h
......
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment