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 <damien@micropython.org>
This commit is contained in:
Damien George 2023-03-09 13:50:21 +11:00
parent f450e94ba0
commit b3c8ab37ec
12 changed files with 27 additions and 33 deletions

View File

@ -69,7 +69,7 @@ void gc_collect(void) {
gc_collect_start(); gc_collect_start();
gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t)); gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
gc_collect_end(); gc_collect_end();
gc_dump_info(); gc_dump_info(&mp_plat_print);
} }
#endif #endif

View File

@ -133,7 +133,7 @@ STATIC mp_obj_t machine_info(mp_uint_t n_args, const mp_obj_t *args) {
if (n_args == 1) { if (n_args == 1) {
// arg given means dump gc allocation table // arg given means dump gc allocation table
gc_dump_alloc_table(); gc_dump_alloc_table(&mp_plat_print);
} }
return mp_const_none; return mp_const_none;

View File

@ -108,7 +108,7 @@ void gc_collect(void) {
gc_collect_start(); gc_collect_start();
gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t)); gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
gc_collect_end(); gc_collect_end();
gc_dump_info(); gc_dump_info(&mp_plat_print);
} }
mp_lexer_t *mp_lexer_new_from_file(const char *filename) { mp_lexer_t *mp_lexer_new_from_file(const char *filename) {

View File

@ -154,7 +154,7 @@ STATIC mp_obj_t machine_info(size_t n_args, const mp_obj_t *args) {
if (n_args == 1) { if (n_args == 1) {
// arg given means dump gc allocation table // arg given means dump gc allocation table
gc_dump_alloc_table(); gc_dump_alloc_table(&mp_plat_print);
} }
return mp_const_none; return mp_const_none;

View File

@ -244,7 +244,7 @@ STATIC mp_obj_t machine_info(size_t n_args, const mp_obj_t *args) {
if (n_args == 1) { if (n_args == 1) {
// arg given means dump gc allocation table // arg given means dump gc allocation table
gc_dump_alloc_table(); gc_dump_alloc_table(print);
} }
return mp_const_none; return mp_const_none;

View File

@ -111,7 +111,7 @@ STATIC mp_obj_t pyb_info(uint n_args, const mp_obj_t *args) {
if (n_args == 1) { if (n_args == 1) {
// arg given means dump gc allocation table // arg given means dump gc allocation table
gc_dump_alloc_table(); gc_dump_alloc_table(&mp_plat_print);
} }
return mp_const_none; return mp_const_none;

View File

@ -34,8 +34,6 @@
#if MICROPY_ENABLE_GC #if MICROPY_ENABLE_GC
void gc_collect(void) { void gc_collect(void) {
// gc_dump_info();
gc_collect_start(); gc_collect_start();
gc_helper_collect_regs_and_stack(); gc_helper_collect_regs_and_stack();
#if MICROPY_PY_THREAD #if MICROPY_PY_THREAD
@ -45,9 +43,6 @@ void gc_collect(void) {
mp_unix_mark_exec(); mp_unix_mark_exec();
#endif #endif
gc_collect_end(); gc_collect_end();
// printf("-----\n");
// gc_dump_info();
} }
#endif // MICROPY_ENABLE_GC #endif // MICROPY_ENABLE_GC

View File

@ -186,7 +186,6 @@ void gc_collect(void) {
gc_collect_start(); 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_root(&dummy, ((mp_uint_t)MP_STATE_THREAD(stack_top) - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
gc_collect_end(); gc_collect_end();
// gc_dump_info();
} }
#if !MICROPY_READER_VFS #if !MICROPY_READER_VFS

30
py/gc.c
View File

@ -726,7 +726,7 @@ found:
#endif #endif
#if EXTENSIVE_HEAP_PROFILING #if EXTENSIVE_HEAP_PROFILING
gc_dump_alloc_table(); gc_dump_alloc_table(&mp_plat_print);
#endif #endif
return ret_ptr; return ret_ptr;
@ -806,7 +806,7 @@ void gc_free(void *ptr) {
GC_EXIT(); GC_EXIT();
#if EXTENSIVE_HEAP_PROFILING #if EXTENSIVE_HEAP_PROFILING
gc_dump_alloc_table(); gc_dump_alloc_table(&mp_plat_print);
#endif #endif
} }
@ -960,7 +960,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
GC_EXIT(); GC_EXIT();
#if EXTENSIVE_HEAP_PROFILING #if EXTENSIVE_HEAP_PROFILING
gc_dump_alloc_table(); gc_dump_alloc_table(&mp_plat_print);
#endif #endif
return ptr_in; return ptr_in;
@ -985,7 +985,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
#endif #endif
#if EXTENSIVE_HEAP_PROFILING #if EXTENSIVE_HEAP_PROFILING
gc_dump_alloc_table(); gc_dump_alloc_table(&mp_plat_print);
#endif #endif
return ptr_in; 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 #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_t info;
gc_info(&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); (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); (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(); GC_ENTER();
static const size_t DUMP_BYTES_PER_LINE = 64; 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)) { for (mp_state_mem_area_t *area = &MP_STATE_MEM(area); area != NULL; area = NEXT_AREA(area)) {
#if !EXTENSIVE_HEAP_PROFILING #if !EXTENSIVE_HEAP_PROFILING
// When comparing heap output we don't want to print the starting // When comparing heap output we don't want to print the starting
// pointer of the heap because it changes from run to run. // 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 #endif
for (size_t bl = 0; bl < area->gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) { for (size_t bl = 0; bl < area->gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) {
if (bl % DUMP_BYTES_PER_LINE == 0) { 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) { if (bl2 - bl >= 2 * DUMP_BYTES_PER_LINE) {
// there are at least 2 lines containing only free blocks, so abbreviate their printing // 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)); bl = bl2 & (~(DUMP_BYTES_PER_LINE - 1));
if (bl >= area->gc_alloc_table_byte_len * BLOCKS_PER_ATB) { if (bl >= area->gc_alloc_table_byte_len * BLOCKS_PER_ATB) {
// got to end of heap // got to end of heap
@ -1058,7 +1058,7 @@ void gc_dump_alloc_table(void) {
} }
// print header for new line of blocks // print header for new line of blocks
// (the cast to uint32_t is for 16-bit ports) // (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 = ' '; int c = ' ';
switch (ATB_GET_KIND(area, bl)) { switch (ATB_GET_KIND(area, bl)) {
@ -1151,9 +1151,9 @@ void gc_dump_alloc_table(void) {
c = 'm'; c = 'm';
break; 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(); GC_EXIT();
} }
@ -1185,13 +1185,13 @@ void gc_test(void) {
} }
printf("Before GC:\n"); printf("Before GC:\n");
gc_dump_alloc_table(); gc_dump_alloc_table(&mp_plat_print);
printf("Starting GC...\n"); printf("Starting GC...\n");
gc_collect_start(); gc_collect_start();
gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void *)); gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void *));
gc_collect_end(); gc_collect_end();
printf("After GC:\n"); printf("After GC:\n");
gc_dump_alloc_table(); gc_dump_alloc_table(&mp_plat_print);
} }
#endif #endif

View File

@ -28,7 +28,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include "py/mpconfig.h" #include "py/mpprint.h"
void gc_init(void *start, void *end); void gc_init(void *start, void *end);
@ -72,7 +72,7 @@ typedef struct _gc_info_t {
} gc_info_t; } gc_info_t;
void gc_info(gc_info_t *info); void gc_info(gc_info_t *info);
void gc_dump_info(void); void gc_dump_info(const mp_print_t *print);
void gc_dump_alloc_table(void); void gc_dump_alloc_table(const mp_print_t *print);
#endif // MICROPY_INCLUDED_PY_GC_H #endif // MICROPY_INCLUDED_PY_GC_H

View File

@ -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()); mp_printf(&mp_plat_print, "stack: " UINT_FMT "\n", mp_stack_usage());
#endif #endif
#if MICROPY_ENABLE_GC #if MICROPY_ENABLE_GC
gc_dump_info(); gc_dump_info(&mp_plat_print);
if (n_args == 1) { if (n_args == 1) {
// arg given means dump gc allocation table // arg given means dump gc allocation table
gc_dump_alloc_table(); gc_dump_alloc_table(&mp_plat_print);
} }
#else #else
(void)n_args; (void)n_args;

View File

@ -167,7 +167,7 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input
#if MICROPY_ENABLE_GC #if MICROPY_ENABLE_GC
// run collection and print GC info // run collection and print GC info
gc_collect(); gc_collect();
gc_dump_info(); gc_dump_info(&mp_plat_print);
#endif #endif
} }
#endif #endif