From 568636d562b4c64d0095ff2ac921b79eb69e520d Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 18 Nov 2019 09:53:12 -0600 Subject: [PATCH] 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) --- py/circuitpy_mpconfig.h | 4 ++-- supervisor/shared/tick.c | 12 ++++++++++++ supervisor/shared/tick.h | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 7853e4de95..3e83d0c510 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -653,8 +653,8 @@ extern const struct _mp_obj_module_t ustack_module; FLASH_ROOT_POINTERS \ NETWORK_ROOT_POINTERS \ -void run_background_tasks(void); -#define RUN_BACKGROUND_TASKS (run_background_tasks()) +void supervisor_run_background_tasks_if_tick(void); +#define RUN_BACKGROUND_TASKS (supervisor_run_background_tasks_if_tick()) // TODO: Used in wiznet5k driver, but may not be needed in the long run. #define MICROPY_THREAD_YIELD() diff --git a/supervisor/shared/tick.c b/supervisor/shared/tick.c index d4a6ac5d75..7dc5a52d32 100644 --- a/supervisor/shared/tick.c +++ b/supervisor/shared/tick.c @@ -24,10 +24,13 @@ * THE SOFTWARE. */ +#include + #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 3defeb1081..b662734492 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