Skip to content

Commit b3c8ab3

Browse files
committed
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 <[email protected]>
1 parent f450e94 commit b3c8ab3

File tree

12 files changed

+27
-33
lines changed

12 files changed

+27
-33
lines changed

ports/minimal/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void gc_collect(void) {
6969
gc_collect_start();
7070
gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
7171
gc_collect_end();
72-
gc_dump_info();
72+
gc_dump_info(&mp_plat_print);
7373
}
7474
#endif
7575

ports/nrf/modules/machine/modmachine.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ STATIC mp_obj_t machine_info(mp_uint_t n_args, const mp_obj_t *args) {
133133

134134
if (n_args == 1) {
135135
// arg given means dump gc allocation table
136-
gc_dump_alloc_table();
136+
gc_dump_alloc_table(&mp_plat_print);
137137
}
138138

139139
return mp_const_none;

ports/powerpc/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void gc_collect(void) {
108108
gc_collect_start();
109109
gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
110110
gc_collect_end();
111-
gc_dump_info();
111+
gc_dump_info(&mp_plat_print);
112112
}
113113

114114
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {

ports/renesas-ra/modmachine.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ STATIC mp_obj_t machine_info(size_t n_args, const mp_obj_t *args) {
154154

155155
if (n_args == 1) {
156156
// arg given means dump gc allocation table
157-
gc_dump_alloc_table();
157+
gc_dump_alloc_table(&mp_plat_print);
158158
}
159159

160160
return mp_const_none;

ports/stm32/modmachine.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ STATIC mp_obj_t machine_info(size_t n_args, const mp_obj_t *args) {
244244

245245
if (n_args == 1) {
246246
// arg given means dump gc allocation table
247-
gc_dump_alloc_table();
247+
gc_dump_alloc_table(print);
248248
}
249249

250250
return mp_const_none;

ports/teensy/modpyb.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ STATIC mp_obj_t pyb_info(uint n_args, const mp_obj_t *args) {
111111

112112
if (n_args == 1) {
113113
// arg given means dump gc allocation table
114-
gc_dump_alloc_table();
114+
gc_dump_alloc_table(&mp_plat_print);
115115
}
116116

117117
return mp_const_none;

ports/unix/gccollect.c

-5
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
#if MICROPY_ENABLE_GC
3535

3636
void gc_collect(void) {
37-
// gc_dump_info();
38-
3937
gc_collect_start();
4038
gc_helper_collect_regs_and_stack();
4139
#if MICROPY_PY_THREAD
@@ -45,9 +43,6 @@ void gc_collect(void) {
4543
mp_unix_mark_exec();
4644
#endif
4745
gc_collect_end();
48-
49-
// printf("-----\n");
50-
// gc_dump_info();
5146
}
5247

5348
#endif // MICROPY_ENABLE_GC

ports/zephyr/main.c

-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ void gc_collect(void) {
186186
gc_collect_start();
187187
gc_collect_root(&dummy, ((mp_uint_t)MP_STATE_THREAD(stack_top) - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
188188
gc_collect_end();
189-
// gc_dump_info();
190189
}
191190

192191
#if !MICROPY_READER_VFS

py/gc.c

+15-15
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ void *gc_alloc(size_t n_bytes, unsigned int alloc_flags) {
726726
#endif
727727

728728
#if EXTENSIVE_HEAP_PROFILING
729-
gc_dump_alloc_table();
729+
gc_dump_alloc_table(&mp_plat_print);
730730
#endif
731731

732732
return ret_ptr;
@@ -806,7 +806,7 @@ void gc_free(void *ptr) {
806806
GC_EXIT();
807807

808808
#if EXTENSIVE_HEAP_PROFILING
809-
gc_dump_alloc_table();
809+
gc_dump_alloc_table(&mp_plat_print);
810810
#endif
811811
}
812812

@@ -960,7 +960,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
960960
GC_EXIT();
961961

962962
#if EXTENSIVE_HEAP_PROFILING
963-
gc_dump_alloc_table();
963+
gc_dump_alloc_table(&mp_plat_print);
964964
#endif
965965

966966
return ptr_in;
@@ -985,7 +985,7 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
985985
#endif
986986

987987
#if EXTENSIVE_HEAP_PROFILING
988-
gc_dump_alloc_table();
988+
gc_dump_alloc_table(&mp_plat_print);
989989
#endif
990990

991991
return ptr_in;
@@ -1019,23 +1019,23 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
10191019
}
10201020
#endif // Alternative gc_realloc impl
10211021

1022-
void gc_dump_info(void) {
1022+
void gc_dump_info(const mp_print_t *print) {
10231023
gc_info_t info;
10241024
gc_info(&info);
1025-
mp_printf(&mp_plat_print, "GC: total: %u, used: %u, free: %u\n",
1025+
mp_printf(print, "GC: total: %u, used: %u, free: %u\n",
10261026
(uint)info.total, (uint)info.used, (uint)info.free);
1027-
mp_printf(&mp_plat_print, " No. of 1-blocks: %u, 2-blocks: %u, max blk sz: %u, max free sz: %u\n",
1027+
mp_printf(print, " No. of 1-blocks: %u, 2-blocks: %u, max blk sz: %u, max free sz: %u\n",
10281028
(uint)info.num_1block, (uint)info.num_2block, (uint)info.max_block, (uint)info.max_free);
10291029
}
10301030

1031-
void gc_dump_alloc_table(void) {
1031+
void gc_dump_alloc_table(const mp_print_t *print) {
10321032
GC_ENTER();
10331033
static const size_t DUMP_BYTES_PER_LINE = 64;
10341034
for (mp_state_mem_area_t *area = &MP_STATE_MEM(area); area != NULL; area = NEXT_AREA(area)) {
10351035
#if !EXTENSIVE_HEAP_PROFILING
10361036
// When comparing heap output we don't want to print the starting
10371037
// pointer of the heap because it changes from run to run.
1038-
mp_printf(&mp_plat_print, "GC memory layout; from %p:", area->gc_pool_start);
1038+
mp_printf(print, "GC memory layout; from %p:", area->gc_pool_start);
10391039
#endif
10401040
for (size_t bl = 0; bl < area->gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) {
10411041
if (bl % DUMP_BYTES_PER_LINE == 0) {
@@ -1048,7 +1048,7 @@ void gc_dump_alloc_table(void) {
10481048
}
10491049
if (bl2 - bl >= 2 * DUMP_BYTES_PER_LINE) {
10501050
// there are at least 2 lines containing only free blocks, so abbreviate their printing
1051-
mp_printf(&mp_plat_print, "\n (%u lines all free)", (uint)(bl2 - bl) / DUMP_BYTES_PER_LINE);
1051+
mp_printf(print, "\n (%u lines all free)", (uint)(bl2 - bl) / DUMP_BYTES_PER_LINE);
10521052
bl = bl2 & (~(DUMP_BYTES_PER_LINE - 1));
10531053
if (bl >= area->gc_alloc_table_byte_len * BLOCKS_PER_ATB) {
10541054
// got to end of heap
@@ -1058,7 +1058,7 @@ void gc_dump_alloc_table(void) {
10581058
}
10591059
// print header for new line of blocks
10601060
// (the cast to uint32_t is for 16-bit ports)
1061-
mp_printf(&mp_plat_print, "\n%08x: ", (uint)(bl * BYTES_PER_BLOCK));
1061+
mp_printf(print, "\n%08x: ", (uint)(bl * BYTES_PER_BLOCK));
10621062
}
10631063
int c = ' ';
10641064
switch (ATB_GET_KIND(area, bl)) {
@@ -1151,9 +1151,9 @@ void gc_dump_alloc_table(void) {
11511151
c = 'm';
11521152
break;
11531153
}
1154-
mp_printf(&mp_plat_print, "%c", c);
1154+
mp_printf(print, "%c", c);
11551155
}
1156-
mp_print_str(&mp_plat_print, "\n");
1156+
mp_print_str(print, "\n");
11571157
}
11581158
GC_EXIT();
11591159
}
@@ -1185,13 +1185,13 @@ void gc_test(void) {
11851185
}
11861186

11871187
printf("Before GC:\n");
1188-
gc_dump_alloc_table();
1188+
gc_dump_alloc_table(&mp_plat_print);
11891189
printf("Starting GC...\n");
11901190
gc_collect_start();
11911191
gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void *));
11921192
gc_collect_end();
11931193
printf("After GC:\n");
1194-
gc_dump_alloc_table();
1194+
gc_dump_alloc_table(&mp_plat_print);
11951195
}
11961196
#endif
11971197

py/gc.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
#include <stdbool.h>
3030
#include <stddef.h>
31-
#include "py/mpconfig.h"
31+
#include "py/mpprint.h"
3232

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

@@ -72,7 +72,7 @@ typedef struct _gc_info_t {
7272
} gc_info_t;
7373

7474
void gc_info(gc_info_t *info);
75-
void gc_dump_info(void);
76-
void gc_dump_alloc_table(void);
75+
void gc_dump_info(const mp_print_t *print);
76+
void gc_dump_alloc_table(const mp_print_t *print);
7777

7878
#endif // MICROPY_INCLUDED_PY_GC_H

py/modmicropython.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args) {
8181
mp_printf(&mp_plat_print, "stack: " UINT_FMT "\n", mp_stack_usage());
8282
#endif
8383
#if MICROPY_ENABLE_GC
84-
gc_dump_info();
84+
gc_dump_info(&mp_plat_print);
8585
if (n_args == 1) {
8686
// arg given means dump gc allocation table
87-
gc_dump_alloc_table();
87+
gc_dump_alloc_table(&mp_plat_print);
8888
}
8989
#else
9090
(void)n_args;

shared/runtime/pyexec.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input
167167
#if MICROPY_ENABLE_GC
168168
// run collection and print GC info
169169
gc_collect();
170-
gc_dump_info();
170+
gc_dump_info(&mp_plat_print);
171171
#endif
172172
}
173173
#endif

0 commit comments

Comments
 (0)