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)
This commit is contained in:
Jeff Epler 2019-11-18 09:53:12 -06:00
parent 40a47d41df
commit 568636d562
3 changed files with 15 additions and 2 deletions

View File

@ -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()

View File

@ -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();
}
}

View File

@ -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