Skip to content

Base idle time calculations on a CPU counter, not a wall clock#527

Merged
djs55 merged 1 commit intomoby:masterfrom
djs55:monotonic-clock-idle-time
Apr 22, 2021
Merged

Base idle time calculations on a CPU counter, not a wall clock#527
djs55 merged 1 commit intomoby:masterfrom
djs55:monotonic-clock-idle-time

Conversation

@djs55
Copy link
Copy Markdown
Collaborator

@djs55 djs55 commented Apr 18, 2021

Previously we used the wall clock via Unix.gettimeofday() to compute the idle time for "endpoints" (remote destinations). When a laptop wakes from sleep, the wall clock jumps forward and suddenly all the endpoints are idle and the connections are dropped.

This patch switches to a monotonic clock which is driven by a CPU counter. The counter is paused while the computer sleeps and therefore does not jump forwards on wake.

This patch also adds a log message whenever an "endpoint" is deleted, to make debugging problems easier.

Signed-off-by: David Scott dave@recoil.org

@djs55 djs55 force-pushed the monotonic-clock-idle-time branch from 9a4dee3 to efccf5a Compare April 18, 2021 15:38
Previously we used the wall clock via `Unix.gettimeofday()` to compute
the idle time for "endpoints" (remote destinations). When a laptop wakes
from sleep, the wall clock jumps forward and suddenly all the endpoints
are idle and the connections are dropped.

This patch switches to a monotonic clock which is driven by a CPU counter.
The counter is paused while the computer sleeps and therefore does not
jump forwards on wake.

Signed-off-by: David Scott <dave@recoil.org>
@djs55 djs55 force-pushed the monotonic-clock-idle-time branch from d2169a0 to ba39cbf Compare April 21, 2021 16:54

let touch t =
t.last_active_time <- Unix.gettimeofday ()
t.last_active_time_ns <- Clock.elapsed_ns t.clock
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clock.elapsed_ns calls into https://github.com/mirage/mirage-clock/blob/987d197802f80778b7b3daa642caee0c27a1ce43/unix/mclock.ml#L19 which is https://github.com/mirage/mirage-clock/blob/987d197802f80778b7b3daa642caee0c27a1ce43/unix/clock_stubs.c#L67

According to https://developer.apple.com/documentation/kernel/1462446-mach_absolute_time

Returns current value of a clock that increments monotonically in tick units (starting at an arbitrary point), this clock does not increment while the system is asleep.

Copy link
Copy Markdown

@StefanScherer StefanScherer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested a Docker Desktop build on a M1 machine leaving unattended over night and Docker is still working and docker commands from the command line still work as expected. The machine went to sleep mode (I have disabled the "Prevent from sleeping" system setting in macOS).

@djs55 djs55 merged commit 2fad6e3 into moby:master Apr 22, 2021
@djs55 djs55 deleted the monotonic-clock-idle-time branch April 22, 2021 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants