wake_clock_linux.go 829 B

1234567891011121314151617181920212223242526
  1. //go:build linux
  2. package nebula
  3. import (
  4. "time"
  5. "golang.org/x/sys/unix"
  6. )
  7. // suspendClockDelta returns CLOCK_BOOTTIME - CLOCK_MONOTONIC. CLOCK_MONOTONIC pauses while the system is suspended
  8. // and CLOCK_BOOTTIME does not, so the difference only ever grows, and only by time spent suspended. Both reads are
  9. // vDSO calls, cheap enough for a hot ticker.
  10. //
  11. // The pausing clock is read first so scheduling jitter between the two reads biases the delta positive; the
  12. // wakeDetector clamps out the noise.
  13. func suspendClockDelta() (time.Duration, bool) {
  14. var mono, boot unix.Timespec
  15. if err := unix.ClockGettime(unix.CLOCK_MONOTONIC, &mono); err != nil {
  16. return 0, false
  17. }
  18. if err := unix.ClockGettime(unix.CLOCK_BOOTTIME, &boot); err != nil {
  19. return 0, false
  20. }
  21. return time.Duration(boot.Nano() - mono.Nano()), true
  22. }