-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
windows/schedule: Add micropython.schedule to windows port. #7999
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
Conversation
ports/windows/windows_mphal.c
Outdated
MICROPY_EVENT_POLL_HOOK | ||
} | ||
#else | ||
// TODO: POSIX et al. define usleep() as guaranteedly capable only of 1s sleep: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should delete the comment on line 265 if this is here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or actually: this is Windows, not exactly known for its POSIX compliance, moreover the msvc port supplies its own implementation of usleep, so tldr: this comment was copied from the unix port but probably doesn't matter or is even incorrect here.
@@ -128,6 +128,9 @@ | |||
#define MICROPY_ERROR_PRINTER (&mp_stderr_print) | |||
#define MICROPY_WARNINGS (1) | |||
#define MICROPY_PY_STR_BYTES_CMP_WARN (1) | |||
#ifndef MICROPY_ENABLE_SCHEDULER | |||
#define MICROPY_ENABLE_SCHEDULER (1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion this shouldn't be on by default: it never was, and it adds code to hot paths. I admit I didnt measure the impact, but it's non-zero which just feels too much for a feature which has otherwise been unused for the past years.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the scheduler queue is empty, it only adds a single if statement to the VM (conditional on MP_STATE_VM(sched_state)
) and even then, only after a branch instruction.
I will measure it on Linux and hopefully that can help guide the decision.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stinos I'm curious as to your use case of the windows port, are your usage performance sensitive?
Keeping things off be default is often an important strategy on embedded ports where size/speed is critical, vs the desktop ports where feautures like this would add an almost imeasurably negligent amount of performance hit?
That being said, this is also why I want to bring in the variant system from other ports too, to make it easier to create build profiles. That doesn't help msvc yet though...
micropython.schedule is used widely and regularly on other ports and is a key piece of architecture for subsystems like BLE. Sure, it's use in interrupts isn't going to translate to desktop any time soon, but it's still used in a lot other places.
In my opinion, the windows port is rarely used mainly because it's missing so so many features that are standard on pretty much every other port.
Most of the other parts of this PR are also needed to enable uselect, which is needed to use uasyncio, which is a standard recommended library for most users.
I'd rather use default enable/disable from other ports to guide new features, rather than history of this port.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want comment on being for or against enabling the scheduler by default, but from a technical point of view enabling it does not add any branches to the VM, it just changes
if (MP_STATE_THREAD(mp_pending_exception) != MP_OBJ_NULL) {
to
if (MP_STATE_VM(sched_state) == MP_SCHED_PENDING) {
(and then if there is something pending, either some code to run or an exception like KeyboardInterrupt, it has a little bit more work to do)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious as to your use case of the windows port, are your usage performance sensitive?
Sort of. We use it in 'production' at the heart of scientific experiments and most of the time the applications run loops (depending on situation period is around 5mSec, sometimes less, sometimes more) and each iteration must finish in time either because it's bound to ADC/DAC devices or because it generates graphics. Most of the critical things are done in C++ but we also run microPython code in those loops. tldr; it's not super critical and after having a second (proper) look this particuler feature won't do much. Apart from the changes to mp_hal_delay perhaps, if someone would rely on that having a certain accuracy but then that's rather their mistake :)
In my opinion, the windows port is rarely used mainly because it's missing so so many features that are standard on pretty much every other port.
That's an interesting thought; I always assumed it's rarely used because there's simply not a lot of practical use for it (there are enogh other Python versions for PC) and the community which does use MicroPython mostly does that on microcontrollers anyway and/or seem to be leaning towards unix.
I'd rather use default enable/disable from other ports to guide new features, rather than history of this port.
Fair point. Would also be interesting to see if there'd be more use if there would be more features on the windows ports.
@@ -201,6 +204,14 @@ extern const struct _mp_obj_module_t mp_module_time; | |||
|
|||
#define MICROPY_MPHALPORT_H "windows_mphal.h" | |||
|
|||
#define MICROPY_EVENT_POLL_HOOK \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be guarded by #if MICROPY_ENABLE_SCHEDULER
, to not change what mp_hal_delay_ms does in case scheduler is off?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MICROPY_EVENT_POLL_HOOK is also needed for a uselect branch I'm about to push up, but yes it probably is cleaner to guard this here, even if it's not switched as such in the unix port. Perhaps I should add a similar compile guard there too...
442b520
to
6952704
Compare
It's within the uncertainty limits of our performance testing on Linux (this is on a "quiet" PC I have set up specifically for this purpose).
Just for reference, here's the same thing on STM32F7. Note both cases here are with all uses of the scheduler disabled, so it's really just measuring the cost of having that extra check in the VM hook.
(I'm also trying to convince myself we should enable by default on Unix :) ) |
Thanks, seems fine. Just wondering: does any of these tests use exceptions heavily? Anyway had a better look at the code and it indeed looks like the impact should be really small. |
6952704
to
b3a80b5
Compare
b3a80b5
to
90625ba
Compare
90625ba
to
33da7d3
Compare
Merged in 30b6ce8 This is fully configurable, and with the new windows variants support can be disabled if needed. |
Thie PR adds the basic hooks to allow
micropython.schedule()
to work correctly on the windows port.It works (runs commands immediately) when run from repl.
Both mingw and mscv build passes
tests/micropython/schedule.py