From b3c8ab37ec86d19277e9d1dd10b5741ab93022ed Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 9 Mar 2023 13:50:21 +1100 Subject: [PATCH] py/gc: Make gc_dump_info/gc_dump_alloc_table take a printer as argument. So that callers can redirect the output if needed. Signed-off-by: Damien George --- ports/minimal/main.c | 2 +- ports/nrf/modules/machine/modmachine.c | 2 +- ports/powerpc/main.c | 2 +- ports/renesas-ra/modmachine.c | 2 +- ports/stm32/modmachine.c | 2 +- ports/teensy/modpyb.c | 2 +- ports/unix/gccollect.c | 5 ----- ports/zephyr/main.c | 1 - py/gc.c | 30 +++++++++++++------------- py/gc.h | 6 +++--- py/modmicropython.c | 4 ++-- shared/runtime/pyexec.c | 2 +- 12 files changed, 27 insertions(+), 33 deletions(-) diff --git a/ports/minimal/main.c b/ports/minimal/main.c index 881a6d2364..4eb6ca65af 100644 --- a/ports/minimal/main.c +++ b/ports/minimal/main.c @@ -69,7 +69,7 @@ void gc_collect(void) { gc_collect_start(); gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t)); gc_collect_end(); - gc_dump_info(); + gc_dump_info(&mp_plat_print); } #endif diff --git a/ports/nrf/modules/machine/modmachine.c b/ports/nrf/modules/machine/modmachine.c index c689f45297..369d957ff1 100644 --- a/ports/nrf/modules/machine/modmachine.c +++ b/ports/nrf/modules/machine/modmachine.c @@ -133,7 +133,7 @@ STATIC mp_obj_t machine_info(mp_uint_t n_args, const mp_obj_t *args) { if (n_args == 1) { // arg given means dump gc allocation table - gc_dump_alloc_table(); + gc_dump_alloc_table(&mp_plat_print); } return mp_const_none; diff --git a/ports/powerpc/main.c b/ports/powerpc/main.c index ed82c517eb..11013f1755 100644 --- a/ports/powerpc/main.c +++ b/ports/powerpc/main.c @@ -108,7 +108,7 @@ void gc_collect(void) { gc_collect_start(); gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t)); gc_collect_end(); - gc_dump_info(); + gc_dump_info(&mp_plat_print); } mp_lexer_t *mp_lexer_new_from_file(const char *filename) { diff --git a/ports/renesas-ra/modmachine.c b/ports/renesas-ra/modmachine.c index 7db36298db..ecb028983c 100644 --- a/ports/renesas-ra/modmachine.c +++ b/ports/renesas-ra/modmachine.c @@ -154,7 +154,7 @@ STATIC mp_obj_t machine_info(size_t n_args, const mp_obj_t *args) { if (n_args == 1) { // arg given means dump gc allocation table - gc_dump_alloc_table(); + gc_dump_alloc_table(&mp_plat_print); } return mp_const_none; diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index 9f85e35811..dee9689e3b 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -244,7 +244,7 @@ STATIC mp_obj_t machine_info(size_t n_args, const mp_obj_t *args) { if (n_args == 1) { // arg given means dump gc allocation table - gc_dump_alloc_table(); + gc_dump_alloc_table(print); } return mp_const_none; diff --git a/ports/teensy/modpyb.c b/ports/teensy/modpyb.c index 3939607ad0..b48fecc18f 100644 --- a/ports/teensy/modpyb.c +++ b/ports/teensy/modpyb.c @@ -111,7 +111,7 @@ STATIC mp_obj_t pyb_info(uint n_args, const mp_obj_t *args) { if (n_args == 1) { // arg given means dump gc allocation table - gc_dump_alloc_table(); + gc_dump_alloc_table(&mp_plat_print); } return mp_const_none; diff --git a/ports/unix/gccollect.c b/ports/unix/gccollect.c index 79b17663c3..94c9c61ea4 100644 --- a/ports/unix/gccollect.c +++ b/ports/unix/gccollect.c @@ -34,8 +34,6 @@ #if MICROPY_ENABLE_GC void gc_collect(void) { - // gc_dump_info(); - gc_collect_start(); gc_helper_collect_regs_and_stack(); #if MICROPY_PY_THREAD @@ -45,9 +43,6 @@ void gc_collect(void) { mp_unix_mark_exec(); #endif gc_collect_end(); - - // printf("-----\n"); - // gc_dump_info(); } #endif // MICROPY_ENABLE_GC diff --git a/ports/zephyr/main.c b/ports/zephyr/main.c index 869449e7df..a2dfb16602 100644 --- a/ports/zephyr/main.c +++ b/ports/zephyr/main.c @@ -186,7 +186,6 @@ void gc_collect(void) { gc_collect_start(); gc_collect_root(&dummy, ((mp_uint_t)MP_STATE_THREAD(stack_top) - (mp_uint_t)&dummy) / sizeof(mp_uint_t)); gc_collect_end(); - // gc_dump_info(); } #if !MICROPY_READER_VFS diff --git a/py/gc.c b/py/gc.c index f11c34aefe..ba5c569d50 100644 --- a/py/gc.c +++ b/py/gc.c @@ -726,7 +726,7 @@ found: #endif #if EXTENSIVE_HEAP_PROFILING - gc_dump_alloc_table(); + gc_dump_alloc_table(&mp_plat_print); #endif return ret_ptr; @@ -806,7 +806,7 @@ void gc_free(void *ptr) { GC_EXIT(); #if EXTENSIVE_HEAP_PROFILING - gc_dump_alloc_table(); + gc_dump_alloc_table(&mp_plat_print); #endif } @@ -960,7 +960,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) { GC_EXIT(); #if EXTENSIVE_HEAP_PROFILING - gc_dump_alloc_table(); + gc_dump_alloc_table(&mp_plat_print); #endif return ptr_in; @@ -985,7 +985,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) { #endif #if EXTENSIVE_HEAP_PROFILING - gc_dump_alloc_table(); + gc_dump_alloc_table(&mp_plat_print); #endif return ptr_in; @@ -1019,23 +1019,23 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) { } #endif // Alternative gc_realloc impl -void gc_dump_info(void) { +void gc_dump_info(const mp_print_t *print) { gc_info_t info; gc_info(&info); - mp_printf(&mp_plat_print, "GC: total: %u, used: %u, free: %u\n", + mp_printf(print, "GC: total: %u, used: %u, free: %u\n", (uint)info.total, (uint)info.used, (uint)info.free); - mp_printf(&mp_plat_print, " No. of 1-blocks: %u, 2-blocks: %u, max blk sz: %u, max free sz: %u\n", + mp_printf(print, " No. of 1-blocks: %u, 2-blocks: %u, max blk sz: %u, max free sz: %u\n", (uint)info.num_1block, (uint)info.num_2block, (uint)info.max_block, (uint)info.max_free); } -void gc_dump_alloc_table(void) { +void gc_dump_alloc_table(const mp_print_t *print) { GC_ENTER(); static const size_t DUMP_BYTES_PER_LINE = 64; for (mp_state_mem_area_t *area = &MP_STATE_MEM(area); area != NULL; area = NEXT_AREA(area)) { #if !EXTENSIVE_HEAP_PROFILING // When comparing heap output we don't want to print the starting // pointer of the heap because it changes from run to run. - mp_printf(&mp_plat_print, "GC memory layout; from %p:", area->gc_pool_start); + mp_printf(print, "GC memory layout; from %p:", area->gc_pool_start); #endif for (size_t bl = 0; bl < area->gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) { if (bl % DUMP_BYTES_PER_LINE == 0) { @@ -1048,7 +1048,7 @@ void gc_dump_alloc_table(void) { } if (bl2 - bl >= 2 * DUMP_BYTES_PER_LINE) { // there are at least 2 lines containing only free blocks, so abbreviate their printing - mp_printf(&mp_plat_print, "\n (%u lines all free)", (uint)(bl2 - bl) / DUMP_BYTES_PER_LINE); + mp_printf(print, "\n (%u lines all free)", (uint)(bl2 - bl) / DUMP_BYTES_PER_LINE); bl = bl2 & (~(DUMP_BYTES_PER_LINE - 1)); if (bl >= area->gc_alloc_table_byte_len * BLOCKS_PER_ATB) { // got to end of heap @@ -1058,7 +1058,7 @@ void gc_dump_alloc_table(void) { } // print header for new line of blocks // (the cast to uint32_t is for 16-bit ports) - mp_printf(&mp_plat_print, "\n%08x: ", (uint)(bl * BYTES_PER_BLOCK)); + mp_printf(print, "\n%08x: ", (uint)(bl * BYTES_PER_BLOCK)); } int c = ' '; switch (ATB_GET_KIND(area, bl)) { @@ -1151,9 +1151,9 @@ void gc_dump_alloc_table(void) { c = 'm'; break; } - mp_printf(&mp_plat_print, "%c", c); + mp_printf(print, "%c", c); } - mp_print_str(&mp_plat_print, "\n"); + mp_print_str(print, "\n"); } GC_EXIT(); } @@ -1185,13 +1185,13 @@ void gc_test(void) { } printf("Before GC:\n"); - gc_dump_alloc_table(); + gc_dump_alloc_table(&mp_plat_print); printf("Starting GC...\n"); gc_collect_start(); gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void *)); gc_collect_end(); printf("After GC:\n"); - gc_dump_alloc_table(); + gc_dump_alloc_table(&mp_plat_print); } #endif diff --git a/py/gc.h b/py/gc.h index bb4204b06f..8431c0a6cf 100644 --- a/py/gc.h +++ b/py/gc.h @@ -28,7 +28,7 @@ #include #include -#include "py/mpconfig.h" +#include "py/mpprint.h" void gc_init(void *start, void *end); @@ -72,7 +72,7 @@ typedef struct _gc_info_t { } gc_info_t; void gc_info(gc_info_t *info); -void gc_dump_info(void); -void gc_dump_alloc_table(void); +void gc_dump_info(const mp_print_t *print); +void gc_dump_alloc_table(const mp_print_t *print); #endif // MICROPY_INCLUDED_PY_GC_H diff --git a/py/modmicropython.c b/py/modmicropython.c index c717926e66..bdb1e8b9b4 100644 --- a/py/modmicropython.c +++ b/py/modmicropython.c @@ -81,10 +81,10 @@ mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args) { mp_printf(&mp_plat_print, "stack: " UINT_FMT "\n", mp_stack_usage()); #endif #if MICROPY_ENABLE_GC - gc_dump_info(); + gc_dump_info(&mp_plat_print); if (n_args == 1) { // arg given means dump gc allocation table - gc_dump_alloc_table(); + gc_dump_alloc_table(&mp_plat_print); } #else (void)n_args; diff --git a/shared/runtime/pyexec.c b/shared/runtime/pyexec.c index b6e7d8fe68..40491650e4 100644 --- a/shared/runtime/pyexec.c +++ b/shared/runtime/pyexec.c @@ -167,7 +167,7 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input #if MICROPY_ENABLE_GC // run collection and print GC info gc_collect(); - gc_dump_info(); + gc_dump_info(&mp_plat_print); #endif } #endif