wakey wakey

This commit is contained in:
JackDoan
2026-07-22 18:49:19 -05:00
parent c2fbe215e6
commit c8a3994cde
9 changed files with 417 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
//go:build linux
package nebula
import (
"time"
"golang.org/x/sys/unix"
)
// suspendClockDelta returns CLOCK_BOOTTIME - CLOCK_MONOTONIC. CLOCK_MONOTONIC pauses while the system is suspended
// and CLOCK_BOOTTIME does not, so the difference only ever grows, and only by time spent suspended. Both reads are
// vDSO calls, cheap enough for a hot ticker.
//
// The pausing clock is read first so scheduling jitter between the two reads biases the delta positive; the
// wakeDetector clamps out the noise.
func suspendClockDelta() (time.Duration, bool) {
var mono, boot unix.Timespec
if err := unix.ClockGettime(unix.CLOCK_MONOTONIC, &mono); err != nil {
return 0, false
}
if err := unix.ClockGettime(unix.CLOCK_BOOTTIME, &boot); err != nil {
return 0, false
}
return time.Duration(boot.Nano() - mono.Nano()), true
}