Skip to content

Commit 5a7661a

Browse files
tmediccicederom
authored andcommitted
interpreters/python: add patch to set _PyRuntime section
By setting a specific region for the `_PyRuntime` structure, it is possible to move it to the external memory, for instance, freeing the internal memory (this structure occupies around 140KiB).
1 parent e1e28eb commit 5a7661a

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

interpreters/python/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ $(CPYTHON_UNPACKNAME): $(CPYTHON_ZIP)
8080
$(Q) patch -p1 -d $(CPYTHON_UNPACKNAME) < patch$(DELIM)0009-include-nuttx-sys-select-header-to-define-FD_SETSIZE.patch
8181
$(Q) patch -p1 -d $(CPYTHON_UNPACKNAME) < patch$(DELIM)0010-check-for-the-d_ino-member-of-the-structure-dirent.patch
8282
$(Q) patch -p1 -d $(CPYTHON_UNPACKNAME) < patch$(DELIM)0011-avoid-redefinition-warning-if-UNUSED-is-already-defi.patch
83+
$(Q) patch -p1 -d $(CPYTHON_UNPACKNAME) < patch$(DELIM)0012-hack-place-_PyRuntime-structure-into-PSRAM-bss-regio.patch
8384

8485
$(HOSTPYTHON):
8586
mkdir -p $(HOSTBUILD)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
From d1e903f516849c535455904b3c3f8a33665c1a88 Mon Sep 17 00:00:00 2001
2+
From: Ivan Grokhotkov <[email protected]>
3+
Date: Wed, 23 Oct 2024 16:52:52 +0200
4+
Subject: [PATCH 12/12] hack: place _PyRuntime structure into PSRAM bss region,
5+
initialize later
6+
7+
_PyRuntime occupies around 100kB of RAM in .data region, making it
8+
hard to fit the interpreter into the available static RAM.
9+
This patch moves it into PSRAM using section attribute. Normally
10+
we shouldn't need this as we can specify placements in ldfragments,
11+
however in this specific case I couldn't get it to work.
12+
Since the structure is now in .bss, add a function which will assign
13+
it the initial value.
14+
15+
The proper fix might be to support .data segment on PSRAM in IDF,
16+
as well as to fix whatever ldgen issue prevents this variable from
17+
being moved to PSRAM.
18+
19+
Co-authored-by: Tiago Medicci Serrano <[email protected]>
20+
---
21+
Python/pylifecycle.c | 11 +++++++++++
22+
1 file changed, 11 insertions(+)
23+
24+
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
25+
index 1701a1cd217..2a8e544f0ac 100644
26+
--- a/Python/pylifecycle.c
27+
+++ b/Python/pylifecycle.c
28+
@@ -102,12 +102,23 @@ __attribute__((
29+
_PyRuntimeState _PyRuntime
30+
#if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
31+
__attribute__ ((section (".PyRuntime")))
32+
+#elif defined(ESP_PLATFORM)
33+
+__attribute__ ((section (".PyRuntime")))
34+
#endif
35+
= _PyRuntimeState_INIT(_PyRuntime, _Py_Debug_Cookie);
36+
_Py_COMP_DIAG_POP
37+
38+
static int runtime_initialized = 0;
39+
40+
+void _PyRuntime_Early_Init(void) {
41+
+#if defined(ESP_PLATFORM)
42+
+ // Normally, _PyRuntime is in .data and is initialized by the C runtime.
43+
+ // This function allows us to place it into external RAM .bss section
44+
+ // and initialize it manually, saving some internal RAM.
45+
+ _PyRuntime = (struct pyruntimestate) _PyRuntimeState_INIT(_PyRuntime, _Py_Debug_Cookie);
46+
+#endif
47+
+}
48+
+
49+
PyStatus
50+
_PyRuntime_Initialize(void)
51+
{
52+
--
53+
2.47.1
54+

0 commit comments

Comments
 (0)