Skip to content

Exclude hibernated time from compute on Windows (#962) - #970

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/962
Oct 17, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/962

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #962
Stacked on #965 (→ #947 → #916). Review and merge those first; this diff is against fix/945.

This is unverified on a real Windows hibernation. No Windows instance was launched. The fix follows Microsoft's documentation of the two APIs and is tested through the stub hooks; whether the unbiased interrupt time really stops across an EC2 hibernation of a Windows instance has not been observed.

What changed

sysKernelClocks in pkg/agent/sys_windows.go no longer reports "not available". It reads, through lazy kernel32 procs (golang.org/x/sys/windows, already a dependency):

  • QueryUnbiasedInterruptTime → Monotonic role. Microsoft: "does not include time the system spends in sleep or hibernation"; "reflects only the time that the system is in the working state". Windows 7 / Server 2008 R2+.
  • QueryInterruptTime → Boottime role. The biased count, which keeps counting through sleep and hibernation. The unbiased page contrasts the two: the system uses biased time so that "relative timers that would have expired during sleep expire immediately upon waking". This is also what Go's monotonic clock reads on Windows, which is the cause of the bug. Windows 10 / Server 2016+.

Both are 100ns units that "begin at zero when the system starts", so they fit the {0,0} boot origin from #945. The docs are cited in the code comment. The unbiased count is read first, so the pair can only overstate the suspended time. If either proc is missing (before Server 2016), the clocks report false and compute is counted as before. I didn't use GetTickCount64: it does include sleep, but it is in milliseconds and comes from the tick count, so subtracting the unbiased interrupt time from it is not exact.

The units and the role mapping are in a platform-neutral interruptTimeClocks in kernelclock.go, so they are tested on every OS. It rejects pairs no system produces (biased behind unbiased, or values past the time.Duration range).

Scope: spored's own count only. Windows still has no boot id and no boot-scoped location, so nothing is published or checkpointed (ComputeClock needs a boot id before it publishes kernel readings). What changes is the daemon's in-process TotalComputeSeconds, which feeds spawn:compute-seconds, the stopped-instance compute in spawn status, and --cost-limit. spored status on Windows still reads the checkpoint and shows no hibernated time.

Tests

  • TestInterruptTimeClocks_MapsUnbiasedToMonotonic and TestInterruptTimeClocks_RejectsImpossiblePairs: platform-neutral.
  • TestWindowsDaemon_HibernationIsNotCountedAsCompute: platform-neutral, through SetKernelClocksForTest. It sets up a Windows-shaped daemon (no boot id, no boot time, no checkpoint, tag 70s) on fake interrupt-time counters, runs it for 10 minutes, then hibernates for 8 hours. It expects 670s, 8h hibernated, and no kernel readings published.
  • I watched these fail against two reverts that compile. With the roles swapped (Monotonic taken from the biased count), the total came out at 29470s instead of 670s. With interruptTimeClocks always returning false (as on Windows before this fix), both tests fail.
  • TestSysKernelClocks_ReadsBothInterruptTimes (//go:build windows) reads the real APIs. This repo's CI has no Windows runner, so it will not run in CI. It has only been compiled (GOOS=windows go test -c).
  • GOOS=windows go build ./... passes. GOOS=windows go vet passes for ./pkg/agent ./cmd/spored on amd64 and arm64. A repo-wide GOOS=windows go vet ./... stops on existing Unix-only test files in pkg/service and pkg/taskproto, which this PR does not touch.
  • make check passes apart from the known Docker-only TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker, which fails because no Docker daemon is running. make check-nested passes.

Caveats

  • Unverified on a real EC2 Windows hibernation, as above.
  • On Windows Server before 2016 there is no QueryInterruptTime, so behaviour there is unchanged.

@scttfrdmn
scttfrdmn changed the base branch from fix/945 to main October 17, 2026 11:58
…biased interrupt time (#962)

spored on Windows counted running time through Go's monotonic clock, which on
Windows is the biased interrupt time and keeps counting through sleep and
hibernation. sysKernelClocks now reads QueryUnbiasedInterruptTime (Monotonic
role, excludes sleep/hibernation) and QueryInterruptTime (Boottime role), so
the shared #945 kernel-clock logic measures the daemon's own count, and so
spawn:compute-seconds and --cost-limit, without the hibernation. Windows still
has no boot id, so the readings are not published or checkpointed.

Unverified against a real EC2 hibernation of a Windows instance. (#962)
@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Windows CI showed sysKernelClocks unreadable: QueryInterruptTime is not a
kernel32 export, so Find failed on every Windows version and the clocks were
silently disabled.
@scttfrdmn
scttfrdmn merged commit e86f948 into main Oct 17, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/962 branch October 17, 2026 12:30
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.

Windows: spored counts hibernated time as compute (Go's monotonic clock includes it)

1 participant