Skip to content

Commit bbcea3f

Browse files
committed
gc: More verbose debugging
Add more DEBUG_printf statements to trace gc behaviour
1 parent 2547928 commit bbcea3f

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

py/gc.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ STATIC machine_uint_t gc_lock_depth;
113113
void gc_init(void *start, void *end) {
114114
// align end pointer on block boundary
115115
end = (void*)((machine_uint_t)end & (~(BYTES_PER_BLOCK - 1)));
116-
DEBUG_printf("Initializing GC heap: %p..%p = " UINT_FMT " bytes\n", start, end, end - start);
117116

118117
// calculate parameters for GC (T=total, A=alloc table, F=finaliser table, P=pool; all in bytes):
119118
// T = A + F + P
120119
// F = A * BLOCKS_PER_ATB / BLOCKS_PER_FTB
121120
// P = A * BLOCKS_PER_ATB * BYTES_PER_BLOCK
122121
// => T = A * (1 + BLOCKS_PER_ATB / BLOCKS_PER_FTB + BLOCKS_PER_ATB * BYTES_PER_BLOCK)
123122
machine_uint_t total_byte_len = (byte*)end - (byte*)start;
123+
DEBUG_printf("Initializing GC heap: %p..%p = " UINT_FMT " bytes\n", start, end, total_byte_len);
124124
#if MICROPY_ENABLE_FINALISER
125125
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);
126126
#else
@@ -268,6 +268,7 @@ STATIC void gc_sweep(void) {
268268

269269
case AT_TAIL:
270270
if (free_tail) {
271+
DEBUG_printf("gc_sweep(%p)\n",PTR_FROM_BLOCK(block));
271272
ATB_ANY_TO_FREE(block);
272273
}
273274
break;
@@ -347,7 +348,6 @@ void gc_info(gc_info_t *info) {
347348

348349
void *gc_alloc(machine_uint_t n_bytes, bool has_finaliser) {
349350
machine_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK;
350-
DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks);
351351

352352
// check if GC is locked
353353
if (gc_lock_depth > 0) {
@@ -401,6 +401,7 @@ void *gc_alloc(machine_uint_t n_bytes, bool has_finaliser) {
401401

402402
// get pointer to first block
403403
void *ret_ptr = (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK);
404+
DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks ptr %p)\n", n_bytes, n_blocks, ret_ptr);
404405

405406
// zero out the additional bytes of the newly allocated blocks
406407
// This is needed because the blocks may have previously held pointers
@@ -439,6 +440,7 @@ void gc_free(void *ptr_in) {
439440
}
440441

441442
machine_uint_t ptr = (machine_uint_t)ptr_in;
443+
DEBUG_printf("gc_free(%p)\n", ptr);
442444

443445
if (VERIFY_PTR(ptr)) {
444446
machine_uint_t block = BLOCK_FROM_PTR(ptr);
@@ -590,7 +592,7 @@ void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) {
590592
return NULL;
591593
}
592594

593-
DEBUG_printf("gc_realloc: allocating new block\n");
595+
DEBUG_printf("gc_realloc(%p -> %p)\n", ptr_in, ptr_out);
594596
memcpy(ptr_out, ptr_in, n_blocks * BYTES_PER_BLOCK);
595597
gc_free(ptr_in);
596598
return ptr_out;

0 commit comments

Comments
 (0)