8000 esp32: Enable automatic Python heap growth. · micropython/micropython@4909dcb · GitHub
[go: up one dir, main page]

Skip to content

Commit 4909dcb

Browse files
committed
esp32: Enable automatic Python heap growth.
Via MICROPY_GC_SPLIT_HEAP_AUTO feature flag added in previous commit. Tested on ESP32 GENERIC_SPIRAM and GENERIC_S3 configurations, with some worst-case allocation patterns and the standard test suite. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 6f19508 commit 4909dcb

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

docs/library/esp32.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,20 @@ Functions
5151
buffers and other data. This data is useful to get a sense of how much memory
5252
is available to ESP-IDF and the networking stack in particular. It may shed
5353
some light on situations where ESP-IDF operations fail due to allocation failures.
54-
The information returned is *not* useful to troubleshoot Python allocation failures,
55-
use `micropython.mem_info()` instead.
5654

5755
The capabilities parameter corresponds to ESP-IDF's ``MALLOC_CAP_XXX`` values but the
5856
two most useful ones are predefined as `esp32.HEAP_DATA` for data heap regions and
5957
`esp32.HEAP_EXEC` for executable regions as used by the native code emitter.
6058

59+
Free IDF heap memory in the `esp32.HEAP_DATA` region is available to be
60+
automatically added to the MicroPython heap to prevent a MicroPython
61+
allocation from failing. However, the information returned here is otherwise
62+
*not* useful to troubleshoot Python allocation failures, use
63+
`micropython.mem_info()` instead. The "max new split" value in
64+
`micropython.mem_info()` output corresponds to the largest free block of
65+
ESP-IDF heap that could be automatically added on demand to the MicroPython
66+
heap.
67+
6168
The return value is a list of 4-tuples, where each 4-tuple corresponds to one heap
6269
and contains: the total bytes, the free bytes, the largest free block, and
6370
the minimum free seen over time.

ports/esp32/gccollect.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,13 @@ void gc_collect(void) {
8080
}
8181

8282
#endif
83+
84+
#if MICROPY_GC_SPLIT_HEAP_AUTO
85+
86+
// The largest new region that is available to become Python heap is the largest
87+
// free block in the ESP-IDF system heap.
88+
size_t gc_get_max_new_split(void) {
89+
return heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT);
90+
}
91+
92+
#endif

ports/esp32/main.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@
7575
#define MP_TASK_STACK_LIMIT_MARGIN (1024)
7676
#endif
7777

78+
// Initial Python heap size. This starts small but adds new heap areas on
79+
// demand due to settings MICROPY_GC_SPLIT_HEAP & MICROPY_GC_SPLIT_HEAP_AUTO
80+
#define MP_TASK_HEAP_SIZE (64 * 1024)
81+
7882
int vprintf_null(const char *format, va_list ap) {
7983
// do nothing: this is used as a log target during raw repl mode
8084
return 0;
@@ -100,19 +104,13 @@ void mp_task(void *pvParameter) {
100104
ESP_LOGE("esp_init", "can't create event loop: 0x%x\n", err);
101105
}
102106

103-
// Allocate the uPy heap using malloc and get the largest available region,
104-
// limiting to 1/2 total available memory to leave memory for the OS.
105-
// When SPIRAM is enabled, this will allocate from SPIRAM.
106-
uint32_t caps = MALLOC_CAP_8BIT;
107-
size_t heap_total = heap_caps_get_total_size(caps);
108-
size_t mp_task_heap_size = MIN(heap_caps_get_largest_free_block(caps), heap_total / 2);
109-
void *mp_task_heap = heap_caps_malloc(mp_task_heap_size, caps);
107+
void *mp_task_heap = MP_PLAT_ALLOC_HEAP(MP_TASK_HEAP_SIZE);
110108

111109
soft_reset:
112110
// initialise the stack pointer for the main thread
113111
mp_stack_set_top((void *)sp);
114112
mp_stack_set_limit(MP_TASK_STACK_SIZE - MP_TASK_STACK_LIMIT_MARGIN);
115-
gc_init(mp_task_heap, mp_task_heap + mp_task_heap_size);
113+
gc_init(mp_task_heap, mp_task_heap + MP_TASK_HEAP_SIZE);
116114
mp_init();
117115
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
118116
readline_init0();

ports/esp32/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
6868
#define MICROPY_PY_THREAD_GIL (1)
6969
#define MICROPY_PY_THREAD_GIL_VM_DIVISOR (32)
7070

71+
#define MICROPY_GC_SPLIT_HEAP (1)
72+
#define MICROPY_GC_SPLIT_HEAP_AUTO (1)
73+
7174
// extended modules
7275
#ifndef MICROPY_ESPNOW
7376
#define MICROPY_ESPNOW (1)

0 commit comments

Comments
 (0)
0