diff options
| author | Jeff Epler <jepler@gmail.com> | 2019-11-18 09:53:12 -0600 |
|---|---|---|
| committer | Jeff Epler <jepler@gmail.com> | 2019-11-18 11:26:48 -0600 |
| commit | 568636d562b4c64d0095ff2ac921b79eb69e520d (patch) | |
| tree | bd7bf35348b009b33932096ed0b5be22ba545b92 /supervisor | |
| parent | 40a47d41dfea885ecfbc6a6fe1fc2ec5124a293b (diff) | |
run_background_tasks: Do nothing unless there has been a tick
This improves performance of running python code by 34%, based
on the "pystone" benchmark on metro m4 express at 5000 passes
(1127.65 -> 1521.6 passes/second).
In addition, by instrumenting the tick function and monitoring on an
oscilloscope, the time actually spent in run_background_tasks() on
the metro m4 decreases from average 43% to 0.5%. (however, there's
some additional overhead that is moved around and not accounted for
in that "0.5%" figure, each time supervisor_run_background_tasks_if_tick
is called but no tick has occurred)
On the CPB, it increases pystone from 633 to 769, a smaller percentage
increase of 21%. I did not measure the time actually spent in
run_background_tasks() on CPB.
Testing performed: on metro m4 and cpb, run pystone adapted from python3.4
(change time.time to time.monotonic for sub-second resolution)
Besides running a 5000 pass test, I also ran a 50-pass test while
scoping how long an output pin was set. Average: 34.59ms or 1445/s on m4,
67.61ms or 739/s on cbp, both matching the other pystone result reasonably
well.
import pystone
import board
import digitalio
import time
d = digitalio.DigitalInOut(board.D13)
d.direction = digitalio.Direction.OUTPUT
while True:
d.value = 0
time.sleep(.01)
d.value = 1
pystone.main(50)
Diffstat (limited to 'supervisor')
| -rw-r--r-- | supervisor/shared/tick.c | 12 | ||||
| -rw-r--r-- | supervisor/shared/tick.h | 1 |
2 files changed, 13 insertions, 0 deletions
diff --git a/supervisor/shared/tick.c b/supervisor/shared/tick.c index d4a6ac5d7..7dc5a52d3 100644 --- a/supervisor/shared/tick.c +++ b/supervisor/shared/tick.c @@ -24,10 +24,13 @@ * THE SOFTWARE. */ +#include <stdatomic.h> + #include "supervisor/shared/tick.h" #include "supervisor/filesystem.h" #include "supervisor/shared/autoreload.h" +static atomic_bool tick_up; static volatile uint64_t ticks_ms; #if CIRCUITPY_GAMEPAD @@ -44,6 +47,7 @@ void supervisor_tick(void) { ticks_ms ++; + atomic_store(&tick_up, true); #if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0 filesystem_tick(); @@ -74,3 +78,11 @@ uint64_t supervisor_ticks_ms64() { uint32_t supervisor_ticks_ms32() { return ticks_ms; } + +extern void run_background_tasks(void); + +void supervisor_run_background_tasks_if_tick() { + if (atomic_exchange(&tick_up, false)) { + run_background_tasks(); + } +} diff --git a/supervisor/shared/tick.h b/supervisor/shared/tick.h index 3defeb108..b66273449 100644 --- a/supervisor/shared/tick.h +++ b/supervisor/shared/tick.h @@ -33,5 +33,6 @@ extern void supervisor_tick(void); extern uint32_t supervisor_ticks_ms32(void); extern uint64_t supervisor_ticks_ms64(void); +extern void supervisor_run_background_if_tick(void); #endif |
