pez-globo / pufferfish-software

All software for the Pufferfish ventilator.
Apache License 2.0
0 stars 1 forks source link

Make backend less wasteful of CPU #423

Open ethanjli opened 2 years ago

ethanjli commented 2 years ago

Currently the backend server performs a clock update every 1 ms. Based on the frontend performance improvements in #417, I suspect these clock updates are the major reason that the backend server consumes ~100% CPU on a single core almost all the time.

The simple (and quick and easy) way to reduce CPU usage is to reduce the rate of clock updates, e.g. to every 2 ms or every 5 ms. However, this seems likely to me to introduce extra timing jitter to time-based events, e.g. state synchronization sending. This is because we may receive a clock update, and then a deadline passes, and then the next clock update only comes several ms later. We should at least try reducing the clock update rate to 2 ms to see how CPU usage is impacted before trying any more complicated approaches.

The other approach I can think of is much (much) more complicated: anything which has at least one deadline for a next clock update it needs should announce the earliest deadline as part of its output, and then ventserver.integration._trio's clock update task needs to sleep until the earliest future deadline, after which it should send a clock update. We'd need to make sure that if it wakes up before the next deadline, it should keep sending clock updates (e.g. every 1 ms) until the earliest future deadline has passed, after which it should start sleeping again until the next deadline. If the earliest future deadline changes (e.g. a new, earlier deadline has been created), it should only sleep until that new deadline.

ethanjli commented 2 years ago

Note: anything which is using monotonic time will need to report the next clock update it needs