Skip to content

Commit

Permalink
py/gc: Make gc_dump_info/gc_dump_alloc_table take a printer as argument.
Browse files Browse the repository at this point in the history
So that callers can redirect the output if needed.

Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Mar 9, 2023
1 parent f450e94 commit b3c8ab3
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 33 deletions.
2 changes: 1 addition & 1 deletion ports/minimal/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion ports/nrf/modules/machine/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ports/powerpc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion ports/renesas-ra/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ports/stm32/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ports/teensy/modpyb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions ports/unix/gccollect.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
1 change: 0 additions & 1 deletion ports/zephyr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions py/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ void *gc_alloc(size_t n_bytes, unsigned int alloc_flags) {
#endif

#if EXTENSIVE_HEAP_PROFILING
gc_dump_alloc_table();
gc_dump_alloc_table(&mp_plat_print);
#endif

return ret_ptr;
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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)) {
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions py/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include <stdbool.h>
#include <stddef.h>
#include "py/mpconfig.h"
#include "py/mpprint.h"

void gc_init(void *start, void *end);

Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions py/modmicropython.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion shared/runtime/pyexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b3c8ab3

Please sign in to comment.