Skip to content

Count physical NICs by structure so idle-stop can fire on enp*s* hosts (#919) - #936

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/919
Oct 15, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/919

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #919

Cause

sysReadNetworkBytes matched only eth0/ens*, so enp39s0 (m8a, AL2023) read 0 bytes. getNetworkBytes treated prev == 0 as the first call, so every tick returned the 1,000,000 assume-active placeholder. Idle-stop could never fire, and status printed the placeholder as Network: 976.6 KB/min.

Changes

  • Linux (pkg/agent/netdev.go, new and untagged so it is tested on every OS): interfaces are picked by structure. Every interface with a /sys/class/net/<if>/device link is summed. lo, docker0, veth, br-*, tun and tailscale have no such link. There is no name list. When nothing matches, the reader returns errNoPhysicalInterface rather than zeros. /proc/net/dev is now split on the colon, so eth0:4294967296 (no space) also parses.
  • agent: the first sample is tracked with a netSampled bool. networkBytes() returns (bytes, measured, why), like cpuUsage() (spawn status reports CPU 0.0% on a 100%-busy instance: two different "unknown" sentinels rendered as measurements #734).
  • Semantics of unknown (decided explicitly): unknown is treated as active, the same as an unmeasured CPU reading, because this check stops instances. An unknown that never resolves would disable idle-stop, so it now (a) is rare, because detection is structural, and (b) logs one WARNING that says idle-stop will not trigger on the host. Each tick also logs Not idle: network traffic could not be measured (<why>); assuming active instead of the fake byte count. A counter that goes backwards (ENI detach, reset) is reported as unknown, not as a negative "idle" delta.
  • Windows: when Get-NetAdapterStatistics returns no adapters, the old code produced [int64]$null = 0, which looked like a measured zero. It now reports unknown.
  • metrics: the Prometheus GetNetworkStats had the same name filter and always used the label eth0. It now uses the shared reader and the real interface names, and emits no sample when nothing is found.
  • spored status / spawn status: prints Network: unknown (single sample; assuming active) instead of 976.6 KB/min. The JSON output gains network_measured and network_unknown. A one-shot status always has a single sample, which matches the CPU line.

Tests

Each test below was run against a revert that compiles and failed:

revert failing test
name match eth0/ens TestReadNetDevBytes_CountsPhysicalInterfacesWhateverTheirName/enp39s0_only, /multi-ENI
count everything except lo .../docker0,_veth,_bridge,_tun_and_tailscale_are_not_counted, TestReadNetDevBytes_NoPhysicalInterfaceIsUnknown
no-iface returns zeros TestReadNetDevBytes_NoPhysicalInterfaceIsUnknown
prev == 0 first-sample test TestNetworkBytes_ZeroCountersAreNotAPermanentFirstSample
warn every call / unknown reported as measured TestNetworkBytes_UnknownIsAssumedActiveAndWarnedOnce
drop backwards-counter guard TestNetworkBytes_BackwardsCounterIsUnknown
status prints the placeholder TestStatusTable_UnmeasuredNetworkIsNotPrintedAsAValue

The fixtures are /proc/net/dev dumps plus a fake /sys/class/net with device symlinks: enp39s0 only (the reported counters), ens5, eth0 in no-space format, ens5 plus docker0/veth/br-/tun/tailscale noise, and multi-ENI.

make check is green apart from the known Docker-only TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker, and make check-nested is green.

Not verified

  • I did not run this on a real instance. The device link is what the kernel creates for ENA (PCI) and Xen netfront (vif), but I have not observed it on m8a.
  • The Windows PowerShell change was compiled and vetted only.

Conflicts with #916: the edits touch separate hunks in agent.go, sys_*.go and cmd/spored/main.go, and none are adjacent to #916's changes.

@codecov

codecov Bot commented Oct 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.36232% with 17 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/agent/agent.go 64.00% 9 Missing ⚠️
pkg/agent/metrics.go 0.00% 4 Missing ⚠️
pkg/agent/netdev.go 93.33% 1 Missing and 1 partial ⚠️
pkg/agent/sys_linux.go 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

#919)

The idle network check matched only "eth0"/"ens*", so the enp39s0 on
m8a/AL2023 read 0 bytes. getNetworkBytes took prev==0 as "first call" and
returned the 1,000,000 assume-active placeholder on every tick, so idle-stop
could never trigger and status printed it as 976.6 KB/min of traffic.

- Linux: sum every interface with a /sys/class/net/<if>/device link (lo,
  docker0, veth, bridges, tun, tailscale have none). No name matching. Report
  "unknown" when none is found instead of zeros. Parse /proc/net/dev on the
  colon so "eth0:4294967296" (no space) also parses.
- agent: track the first sample with a bool. networkBytes() returns
  (bytes, measured, why), as cpuUsage does (#734). Unknown is still treated as
  active, and the unreadable case logs one warning that idle-stop will not
  trigger. A counter that goes backwards is unknown, not a negative "idle".
- Windows: no adapters returned 0 ([int64]$null), not unknown.
- metrics: the Prometheus network stat used the same filter and the "eth0"
  label. It now uses the shared reader and the real interface names.
- spored status: prints "Network: unknown (...; assuming active)" and adds
  network_measured / network_unknown to the JSON output.

Fixes #919 (#919)
@scttfrdmn
scttfrdmn merged commit 862f097 into main Oct 15, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the fix/919 branch October 15, 2026 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant