Skip to content

Exclude hibernated time from compute: measure it on the kernel's monotonic clock (#945) - #965

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/945
Oct 17, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/945

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #945
Stacked on #947, which is stacked on #916. Merge those first; this PR's base is fix/928.

The bug

A hibernate-and-resume keeps the same boot. The boot_id, /run/spored/compute-clock.json and the same-boot checkpoint all survive it. spored status, and a spored restarted after the resume, measured running time as wall-now minus a wall timestamp written before the hibernation, so the whole hibernation counted as compute. That also fed --cost-limit.

A third case beyond the issue: the daemon's "count from the kernel's boot" origin came from /proc/uptime, which is CLOCK_BOOTTIME and includes suspend. A spored that started after a hibernation earlier in the boot counted that hibernation too.

Approach: kernel clocks rather than catching the event

I chose CLOCK_MONOTONIC and CLOCK_BOOTTIME over a systemd sleep hook or EC2 state-transition times:

  • A sleep hook only helps if it runs, and it adds a moving part to install.
  • EC2 transition times need the network and credentials.
  • The clocks need neither, and they are right however the suspend happened.

How it works:

Status

spored status adds hibernated_seconds and shows it in the elapsed breakdown, e.g. 8h 1m (1m 0s compute · 10s boot · 8h 0m hibernated). Hibernated time is subtracted before the stopped residue is computed, so it is not counted twice. It is included in the storage cost, because EBS bills while hibernated and compute does not. The EC2-side view of a stopped instance already shows EC2's StateReason (Client.UserInitiatedHibernate), so that view is unchanged.

Windows (filed #962)

Windows has no published clock and no checkpoint, so the cross-process cases do not arise there. But Go's Windows monotonic clock is KUSER_SHARED_DATA.InterruptTime, which keeps counting through sleep and hibernation. So the daemon's own in-process count on Windows does include the hibernation. sysKernelClocks is a documented stub there for now. #962 describes the QueryUnbiasedInterruptTime fix, which needs a Windows instance to verify.

Tests

New gates:

  • pkg/agent/hibernation_test.go:
  • cmd/spored/status_cost_test.go: TestStatusReport_HibernatedTimeIsNotCompute
  • pkg/agent/sys_linux_kernelclock_test.go: the real-kernel read, Linux only

Each gate was watched failing against a revert that compiles (go vet clean):

  • wall-clock thisBootRunning (4 fail)
  • legacy same-boot branch
  • boot-origin OriginKnown=false
  • validOrigin → true
  • boot-id check
  • status HibernatedTime()*0
  • checkpoint kernel fields dropped
  • clock kernel fields dropped
  • constructor wiring dropped

Existing resolveComputeStart tests go through a resolveWall helper, because the function now returns a struct. Their wall-clock expectations are unchanged.

make check: everything passes except the known TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker (no Docker daemon). make check-nested: all 9 modules clean. Cross-compiled for Linux and Windows, including the test binaries.

Caveats

  • Not reproduced on a live hibernation. No AWS resources were used.
  • EC2 bills the stopping phase while RAM is saved, and the restore boot. CLOCK_MONOTONIC counts neither, because the snapshot is taken before the image write. So compute undercounts by roughly that minute or two around each hibernation, and status shows it as hibernated. That errs in the direction the existing rules prefer.
  • TestSysKernelClocks_ReadsBothClocks runs only on Linux, so in CI. It was not run locally (macOS).

@scttfrdmn
scttfrdmn force-pushed the fix/945 branch 2 times, most recently from 1d585d7 to f981b02 Compare October 17, 2026 01:53
scttfrdmn added a commit that referenced this pull request Oct 17, 2026
…ion (#987)

A hibernated instance resumes the same spored process, which carried its
pre-hibernate state into the new run. The pre-stop hook and task record
were spent, so the next stop or TTL terminate skipped them. The DNS name
Cleanup deleted came back only at the hourly renewal, with the IP from
the first boot. lastActivityTime was hours old, so the first idle tick
hibernated again within a minute. The plugins Cleanup stopped stayed
stopped.

Detection runs on each monitor tick (checkResume) and needs no sleep
hook:
- The primary signal is CLOCK_BOOTTIME - CLOCK_MONOTONIC (the idea from
  #965) growing by 1s or more. Only a suspend moves it.
  sysSuspendedTime lives in suspend_linux.go / suspend_other.go, so it
  does not collide with #965's kernelclock.go.
- The backup signal is the wall clock running at least 5 minutes ahead
  of Go's monotonic clock between ticks.
- resumedBetween is pure and table-tested.

onResume re-arms preStopDone and taskFlushDone, restarts the idle timer
and warning, clears the stop backoff, resets the CPU, network and disk
baselines, and marks a completion file already acted on as spent.
checkCompletion ignores it until its mtime changes. onResume then
re-reads the public IP from IMDS and calls restoreAfterCleanup (renamed
from restoreAfterFailedStop), which re-registers DNS, the registry entry
and the plugins.

The new IP is held in a guarded currentIP field behind publicIP().
identity is shared unguarded with the provider, the plugin runtime and
off-loop goroutines, so it is not mutated. The DNS register and delete
calls and the ready-url hosts read publicIP().

(#987)
scttfrdmn added a commit that referenced this pull request Oct 17, 2026
…ion (#987)

A hibernated instance resumes the same spored process, which carried its
pre-hibernate state into the new run. The pre-stop hook and task record
were spent, so the next stop or TTL terminate skipped them. The DNS name
Cleanup deleted came back only at the hourly renewal, with the IP from
the first boot. lastActivityTime was hours old, so the first idle tick
hibernated again within a minute. The plugins Cleanup stopped stayed
stopped.

Detection runs on each monitor tick (checkResume) and needs no sleep
hook:
- The primary signal is CLOCK_BOOTTIME - CLOCK_MONOTONIC (the idea from
  #965) growing by 1s or more. Only a suspend moves it.
  sysSuspendedTime lives in suspend_linux.go / suspend_other.go, so it
  does not collide with #965's kernelclock.go.
- The backup signal is the wall clock running at least 5 minutes ahead
  of Go's monotonic clock between ticks.
- resumedBetween is pure and table-tested.

onResume re-arms preStopDone and taskFlushDone, restarts the idle timer
and warning, clears the stop backoff, resets the CPU, network and disk
baselines, and marks a completion file already acted on as spent.
checkCompletion ignores it until its mtime changes. onResume then
re-reads the public IP from IMDS and calls restoreAfterCleanup (renamed
from restoreAfterFailedStop), which re-registers DNS, the registry entry
and the plugins.

The new IP is held in a guarded currentIP field behind publicIP().
identity is shared unguarded with the provider, the plugin runtime and
off-loop goroutines, so it is not mutated. The DNS register and delete
calls and the ready-url hosts read publicIP().

(#987)
scttfrdmn added a commit that referenced this pull request Oct 17, 2026
…ion (#987)

A hibernated instance resumes the same spored process, which carried its
pre-hibernate state into the new run. The pre-stop hook and task record
were spent, so the next stop or TTL terminate skipped them. The DNS name
Cleanup deleted came back only at the hourly renewal, with the IP from
the first boot. lastActivityTime was hours old, so the first idle tick
hibernated again within a minute. The plugins Cleanup stopped stayed
stopped.

Detection runs on each monitor tick (checkResume) and needs no sleep
hook:
- The primary signal is CLOCK_BOOTTIME - CLOCK_MONOTONIC (the idea from
  #965) growing by 1s or more. Only a suspend moves it.
  sysSuspendedTime lives in suspend_linux.go / suspend_other.go, so it
  does not collide with #965's kernelclock.go.
- The backup signal is the wall clock running at least 5 minutes ahead
  of Go's monotonic clock between ticks.
- resumedBetween is pure and table-tested.

onResume re-arms preStopDone and taskFlushDone, restarts the idle timer
and warning, clears the stop backoff, resets the CPU, network and disk
baselines, and marks a completion file already acted on as spent.
checkCompletion ignores it until its mtime changes. onResume then
re-reads the public IP from IMDS and calls restoreAfterCleanup (renamed
from restoreAfterFailedStop), which re-registers DNS, the registry entry
and the plugins.

The new IP is held in a guarded currentIP field behind publicIP().
identity is shared unguarded with the provider, the plugin runtime and
off-loop goroutines, so it is not mutated. The DNS register and delete
calls and the ready-url hosts read publicIP().

(#987)
scttfrdmn added a commit that referenced this pull request Oct 17, 2026
…ion (#987) (#1020)

* Reset spored's per-boot state when the instance resumes from hibernation (#987)

A hibernated instance resumes the same spored process, which carried its
pre-hibernate state into the new run. The pre-stop hook and task record
were spent, so the next stop or TTL terminate skipped them. The DNS name
Cleanup deleted came back only at the hourly renewal, with the IP from
the first boot. lastActivityTime was hours old, so the first idle tick
hibernated again within a minute. The plugins Cleanup stopped stayed
stopped.

Detection runs on each monitor tick (checkResume) and needs no sleep
hook:
- The primary signal is CLOCK_BOOTTIME - CLOCK_MONOTONIC (the idea from
  #965) growing by 1s or more. Only a suspend moves it.
  sysSuspendedTime lives in suspend_linux.go / suspend_other.go, so it
  does not collide with #965's kernelclock.go.
- The backup signal is the wall clock running at least 5 minutes ahead
  of Go's monotonic clock between ticks.
- resumedBetween is pure and table-tested.

onResume re-arms preStopDone and taskFlushDone, restarts the idle timer
and warning, clears the stop backoff, resets the CPU, network and disk
baselines, and marks a completion file already acted on as spent.
checkCompletion ignores it until its mtime changes. onResume then
re-reads the public IP from IMDS and calls restoreAfterCleanup (renamed
from restoreAfterFailedStop), which re-registers DNS, the registry entry
and the plugins.

The new IP is held in a guarded currentIP field behind publicIP().
identity is shared unguarded with the provider, the plugin runtime and
off-loop goroutines, so it is not mutated. The DNS register and delete
calls and the ready-url hosts read publicIP().

(#987)

* Read the pre-stop hook's log in either encoding, for the Windows runner (#987)

* Resume tests: a quiet machine has no active sessions on Windows either (#987)
@scttfrdmn
scttfrdmn changed the base branch from fix/928 to main October 17, 2026 11:41
…tonic clock (#945)

Resume detection (#987) now reads its suspended time through the same
kernel-clock reader (readKernelClocks) instead of a second
CLOCK_BOOTTIME/CLOCK_MONOTONIC reader, so the compute clock and the
per-boot reset cannot disagree about a hibernation. suspend_linux.go and
suspend_other.go are folded into kernelclock.go / sys_*.go.
@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

@scttfrdmn
scttfrdmn merged commit bb98446 into main Oct 17, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/945 branch October 17, 2026 11:59
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.

Compute clock counts hibernated time after a resume (status, and a same-boot spored restart)

1 participant