Skip to content

Commit d0240bf

Browse files
committed
DEBUG: analyze GC deps
1 parent c718974 commit d0240bf

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

py/gc.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ bool gc_is_locked(void) {
209209
#endif
210210
#endif
211211

212+
bool gc_collect_debug = false;
213+
212214
// Take the given block as the topmost block on the stack. Check all it's
213215
// children: mark the unmarked child blocks and put those newly marked
214216
// blocks on the stack. When all children have been checked, pop off the
@@ -224,6 +226,10 @@ STATIC void gc_mark_subtree(size_t block) {
224226
n_blocks += 1;
225227
} while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
226228

229+
if (gc_collect_debug) {
230+
printf("collect: block=" UINT_FMT " bytes=" UINT_FMT "\n", block, n_blocks * BYTES_PER_BLOCK);
231+
}
232+
227233
// check this block's children
228234
void **ptrs = (void **)PTR_FROM_BLOCK(block);
229235
for (size_t i = n_blocks * BYTES_PER_BLOCK / sizeof(void *); i > 0; i--, ptrs++) {
@@ -233,12 +239,18 @@ STATIC void gc_mark_subtree(size_t block) {
233239
// Mark and push this pointer
234240
size_t childblock = BLOCK_FROM_PTR(ptr);
235241
if (ATB_GET_KIND(childblock) == AT_HEAD) {
242+
if (gc_collect_debug) {
243+
printf("collect: parent=" UINT_FMT " child=" UINT_FMT "\n", block, childblock);
244+
}
236245
// an unmarked head, mark it, and push it on gc stack
237246
TRACE_MARK(childblock, ptr);
238247
ATB_HEAD_TO_MARK(childblock);
239248
if (sp < MICROPY_ALLOC_GC_STACK_SIZE) {
240249
MP_STATE_MEM(gc_stack)[sp++] = childblock;
241250
} else {
251+
if (gc_collect_debug) {
252+
printf("collect: gc_stack_overflow: " UINT_FMT "\n", sp);
253+
}
242254
MP_STATE_MEM(gc_stack_overflow) = 1;
243255
}
244256
}
@@ -341,6 +353,9 @@ void gc_collect_start(void) {
341353
void **ptrs = (void **)(void *)&mp_state_ctx;
342354
size_t root_start = offsetof(mp_state_ctx_t, thread.dict_locals);
343355
size_t root_end = offsetof(mp_state_ctx_t, vm.qstr_last_chunk);
356+
if (gc_collect_debug) {
357+
printf("collect: mp_state=%p start=" UINT_FMT " end=" UINT_FMT "\n", ptrs, root_start, root_end);
358+
}
344359
gc_collect_root(ptrs + root_start / sizeof(void *), (root_end - root_start) / sizeof(void *));
345360

346361
#if MICROPY_ENABLE_PYSTACK
@@ -366,6 +381,9 @@ void gc_collect_root(void **ptrs, size_t len) {
366381
void *ptr = gc_get_ptr(ptrs, i);
367382
if (VERIFY_PTR(ptr)) {
368383
size_t block = BLOCK_FROM_PTR(ptr);
384+
if (gc_collect_debug) {
385+
printf("collect: root=%p i=" UINT_FMT " block=" UINT_FMT "\n", ptrs, i, block);
386+
}
369387
if (ATB_GET_KIND(block) == AT_HEAD) {
370388
// An unmarked head: mark it, and mark all its children
371389
TRACE_MARK(block, ptr);
@@ -467,7 +485,6 @@ void gc_set_oom_callback(gc_oom_callback_t func)
467485
void *gc_alloc(size_t n_bytes, unsigned int alloc_flags) {
468486
bool has_finaliser = alloc_flags & GC_ALLOC_FLAG_HAS_FINALISER;
469487
size_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK;
470-
DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks);
471488

472489
// check for 0 allocation
473490
if (n_blocks == 0) {
@@ -531,6 +548,10 @@ void *gc_alloc(size_t n_bytes, unsigned int alloc_flags) {
531548
// get starting and end blocks, both inclusive
532549
end_block = i;
533550
start_block = i - n_free + 1;
551+
if (n_bytes > 800) {
552+
printf("gc_alloc(" UINT_FMT ") -> " UINT_FMT " bytes @ block " UINT_FMT "\n",
553+
n_bytes, n_blocks * BYTES_PER_BLOCK, start_block);
554+
}
534555

535556
// Set last free ATB index to block after last block we found, for start of
536557
// next scan. To reduce fragmentation, we only do this if we were looking

0 commit comments

Comments
 (0)