-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
py,esp32,rp2: Add new cstack API, check margin macro, fix esp32 stack check. #15605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
py,esp32,rp2: Add new cstack API, check margin macro, fix esp32 stack check. #15605
Conversation
f72fa70
to
874e330
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #15605 +/- ##
==========================================
- Coverage 98.43% 98.43% -0.01%
==========================================
Files 161 163 +2
Lines 21281 21293 +12
==========================================
+ Hits 20948 20959 +11
- Misses 333 334 +1 ☔ View full report in Codecov by Sentry. |
Code size report:
|
An alternative to this would be to still have the mp_stack_set_limit(stack_size - MICROPY_STACK_CHECK_MARGIN); That makes it more obvious that the limit is the stack size minus an explicit margin. Otherwise the margin is kind of hidden in the But, this alternative approach is more verbose (more duplicated code with the subtraction of the margin everywhere) so probably not any better than what this PR does. |
44b6a19
to
ec2e85a
Compare
@dpgeorge Have updated according to our discussion earlier, and updated the PR description. The project coverage check is failing because stackctrl.c is no longer covered. What's the best way to deal with that? |
Hmmmm... three options I can think of:
Option 3 makes it harder to easily remove the old functions when the time comes. Probably option 1 is the best. |
ec2e85a
to
5a0038f
Compare
Thanks, have pushed a commit to try that approach (and coverage seems happy, at least.) |
This is looking really good now! Just a few minor comments. |
5a0038f
to
1342367
Compare
1342367
to
15f53b5
Compare
Currently the stack limit margin is hard-coded in each port's call to `mp_stack_set_limit()`, but on threaded ports it's fiddlier and can lead to bugs (such as incorrect thread stack margin on esp32). This commit provides a new API to initialise the C Stack in one function call, with a config macro to set the margin. Where possible the new call is inlined to reduce code size in thread-free ports. Intended replacement for `MP_TASK_STACK_LIMIT_MARGIN` on esp32. The previous `stackctrl.h` API is still present and unmodified apart from a deprecation comment. However it's not available when the `MICROPY_PREVIEW_VERSION_2` macro is set. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
This change moves that complexity out into the stack checker and fixes the bug where stack margin wasn't set correctly by ESP32-C3 threads. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
Now passing on ESP32-S3 and ESP32-C3. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
Necessary to pass CI when testing the V2 preview APIs. Also adds an extra coverage test for the legacy stackctrl API, to maintain coverage and check for any regression. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
15f53b5
to
a6fa85d
Compare
Thanks for updating, now merged. |
@projectgus looks like this may have broken The BLE IRQ handler on esp32 has To fix it I suggest decreasing |
Summary
mp_stack_set_limit()
. Threaded ports have the extra challenge of adding a margin in the thread module. However only the value passed tomp_stack_set_limit()
needs to have the margin applied (i.e. if the user sets a thread stack size of N bytes, the tasks will have a real stack size of N bytes and the stack limit checker sets its value toN - margin
bytes.)MICROPY_STACK_CHECK_MARGIN
macro and a new cstack API which sets the stack origin and the stack limit (based off the stack size) in one call.MICROPY_PREVIEW_VERSION_2
.esp32
,unix
andrp2
ports are converted in this PR as initial examples.Testing
stress
andthread
tests on unix port, esp32 port (S3 and C3), and on rp2 port.stress
tests for the esp32 port.Trade-offs and Alternatives
Could have made a much smaller fix for the esp32 port race condition by adjusting the stack size before creating the thread, or by applying a mutex. However this approach seems easier to understand overall, and implements a single source of truth for the stack limit margin.
Follow-up work