From bbcea3f62b08f3166b87a86e9c2c5ac9cc61b578 Mon Sep 17 00:00:00 2001 From: stijn Date: Mon, 16 Jun 2014 10:44:29 +0200 Subject: [PATCH 1/3] gc: More verbose debugging Add more DEBUG_printf statements to trace gc behaviour --- py/gc.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/py/gc.c b/py/gc.c index 30bae5054a..2f60ce50d9 100644 --- a/py/gc.c +++ b/py/gc.c @@ -113,7 +113,6 @@ STATIC machine_uint_t gc_lock_depth; void gc_init(void *start, void *end) { // align end pointer on block boundary end = (void*)((machine_uint_t)end & (~(BYTES_PER_BLOCK - 1))); - DEBUG_printf("Initializing GC heap: %p..%p = " UINT_FMT " bytes\n", start, end, end - start); // calculate parameters for GC (T=total, A=alloc table, F=finaliser table, P=pool; all in bytes): // T = A + F + P @@ -121,6 +120,7 @@ void gc_init(void *start, void *end) { // P = A * BLOCKS_PER_ATB * BYTES_PER_BLOCK // => T = A * (1 + BLOCKS_PER_ATB / BLOCKS_PER_FTB + BLOCKS_PER_ATB * BYTES_PER_BLOCK) machine_uint_t total_byte_len = (byte*)end - (byte*)start; + DEBUG_printf("Initializing GC heap: %p..%p = " UINT_FMT " bytes\n", start, end, total_byte_len); #if MICROPY_ENABLE_FINALISER gc_alloc_table_byte_len = total_byte_len * BITS_PER_BYTE / (BITS_PER_BYTE + BITS_PER_BYTE * BLOCKS_PER_ATB / BLOCKS_PER_FTB + BITS_PER_BYTE * BLOCKS_PER_ATB * BYTES_PER_BLOCK); #else @@ -268,6 +268,7 @@ STATIC void gc_sweep(void) { case AT_TAIL: if (free_tail) { + DEBUG_printf("gc_sweep(%p)\n",PTR_FROM_BLOCK(block)); ATB_ANY_TO_FREE(block); } break; @@ -347,7 +348,6 @@ void gc_info(gc_info_t *info) { void *gc_alloc(machine_uint_t n_bytes, bool has_finaliser) { machine_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK; - DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks); // check if GC is locked if (gc_lock_depth > 0) { @@ -401,6 +401,7 @@ found: // get pointer to first block void *ret_ptr = (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK); + DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks ptr %p)\n", n_bytes, n_blocks, ret_ptr); // zero out the additional bytes of the newly allocated blocks // This is needed because the blocks may have previously held pointers @@ -439,6 +440,7 @@ void gc_free(void *ptr_in) { } machine_uint_t ptr = (machine_uint_t)ptr_in; + DEBUG_printf("gc_free(%p)\n", ptr); if (VERIFY_PTR(ptr)) { machine_uint_t block = BLOCK_FROM_PTR(ptr); @@ -590,7 +592,7 @@ void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) { return NULL; } - DEBUG_printf("gc_realloc: allocating new block\n"); + DEBUG_printf("gc_realloc(%p -> %p)\n", ptr_in, ptr_out); memcpy(ptr_out, ptr_in, n_blocks * BYTES_PER_BLOCK); gc_free(ptr_in); return ptr_out; From def10cecd18bc41418a41d8130aab31c84874361 Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 18 Jun 2014 10:20:41 +0200 Subject: [PATCH 2/3] gc: Keep debug statements at beginning of scope where possible --- py/gc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/py/gc.c b/py/gc.c index 2f60ce50d9..d78b70a9cf 100644 --- a/py/gc.c +++ b/py/gc.c @@ -40,7 +40,7 @@ #if MICROPY_ENABLE_GC -#if 0 // print debugging info +#if 1 // print debugging info #define DEBUG_PRINT (1) #define DEBUG_printf DEBUG_printf #else // don't print debugging info @@ -113,6 +113,7 @@ STATIC machine_uint_t gc_lock_depth; void gc_init(void *start, void *end) { // align end pointer on block boundary end = (void*)((machine_uint_t)end & (~(BYTES_PER_BLOCK - 1))); + DEBUG_printf("Initializing GC heap: %p..%p = " UINT_FMT " bytes\n", start, end, (byte*)end - (byte*)start); // calculate parameters for GC (T=total, A=alloc table, F=finaliser table, P=pool; all in bytes): // T = A + F + P @@ -120,7 +121,6 @@ void gc_init(void *start, void *end) { // P = A * BLOCKS_PER_ATB * BYTES_PER_BLOCK // => T = A * (1 + BLOCKS_PER_ATB / BLOCKS_PER_FTB + BLOCKS_PER_ATB * BYTES_PER_BLOCK) machine_uint_t total_byte_len = (byte*)end - (byte*)start; - DEBUG_printf("Initializing GC heap: %p..%p = " UINT_FMT " bytes\n", start, end, total_byte_len); #if MICROPY_ENABLE_FINALISER gc_alloc_table_byte_len = total_byte_len * BITS_PER_BYTE / (BITS_PER_BYTE + BITS_PER_BYTE * BLOCKS_PER_ATB / BLOCKS_PER_FTB + BITS_PER_BYTE * BLOCKS_PER_ATB * BYTES_PER_BLOCK); #else @@ -348,6 +348,7 @@ void gc_info(gc_info_t *info) { void *gc_alloc(machine_uint_t n_bytes, bool has_finaliser) { machine_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK; + DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks); // check if GC is locked if (gc_lock_depth > 0) { @@ -401,7 +402,7 @@ found: // get pointer to first block void *ret_ptr = (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK); - DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks ptr %p)\n", n_bytes, n_blocks, ret_ptr); + DEBUG_printf("gc_alloc(%p)\n", ret_ptr); // zero out the additional bytes of the newly allocated blocks // This is needed because the blocks may have previously held pointers From 9acb5e4cf00c752f16a9606572ad440e2f545dbf Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 18 Jun 2014 12:29:03 +0200 Subject: [PATCH 3/3] gc: Turn off debugging info again --- py/gc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/gc.c b/py/gc.c index d78b70a9cf..90b7b89e5d 100644 --- a/py/gc.c +++ b/py/gc.c @@ -40,7 +40,7 @@ #if MICROPY_ENABLE_GC -#if 1 // print debugging info +#if 0 // print debugging info #define DEBUG_PRINT (1) #define DEBUG_printf DEBUG_printf #else // don't print debugging info