Skip to content

feat(monitor): report container-scoped disk, memory and CPU - #1

Closed
zouanbing wants to merge 1 commit into
mainfrom
feat/monitor-container-aware
Closed

zouanbing wants to merge 1 commit into
mainfrom
feat/monitor-container-aware

Conversation

@zouanbing

Copy link
Copy Markdown
Owner

Problem

Inside containers the system monitor reported host-level figures instead of the container's own resources:

  • Disk summed usage across every mount point, double-counting the same underlying disk — the overlay root plus host bind-mounts (/etc/hosts, /etc/resolv.conf) on Linux, and sibling APFS volumes / stray /Volumes/* mounts on macOS. A 512 GB Mac showed ~922 GB.
  • Memory used /proc/meminfo (host total) and ignored the cgroup limit: a --memory=512m container reported 8 GB total with a host-wide used %.
  • CPU used host core count and host-wide utilisation: a --cpus=2 container reported 10 cores.

Fix

  • disk: the overview now reads the root volume via disk.Usage(rootDiskPath()) ("/" on Unix, system drive on Windows) instead of enumerating and summing mount points — this is the disk the process can actually use (the container's writable layer under Docker).
  • memory: applyCgroupMemory honours the cgroup v2/v1 limit and working-set usage (current - inactive_file), and only overrides when a finite limit smaller than the host total is set.
  • cpu: applyCgroupCPU derives effective cores from the CFS quota (cpu.max / cfs_quota_us÷period) and recomputes utilisation from the cpu.stat usage delta over the sample window.

Robustness

All cgroup access is gated to Linux (runtime.GOOS == "linux") and every reader fails safe to host metrics on any problem — not Linux, no limit configured, unlimited sentinels, missing or malformed files, division by zero. Nothing panics. Bare-metal, non-Linux, and unlimited-container deployments are unchanged (host values, which are correct there).

Tests

New cgroup_test.go covers v2/v1 parsing, max/-1/huge-sentinel unlimited cases, malformed input, period=0, limit≥host fallback, the Linux gate, and applyCgroupMemory overrides. go test ./internal/monitor/ and go vet pass.

Files

  • internal/monitor/cgroup.go (new) — cgroup v2/v1 readers
  • internal/monitor/cgroup_test.go (new) — robustness tests
  • internal/monitor/service.go — wire disk/memory/CPU collectors

Inside containers the monitor reported host-level figures: disk usage
summed every mount point (double-counting the overlay plus host
bind-mounts on Linux, and sibling APFS volumes on macOS), while memory
and CPU ignored cgroup limits and showed the whole host.

- disk: read the root volume via disk.Usage instead of summing mounts
- memory: honour the cgroup v2/v1 limit and working-set usage
- cpu: derive effective cores and container usage from the CFS quota
  and cpu.stat

All cgroup access is gated to Linux and fails safe to host metrics, so
bare-metal and non-Linux deployments are unchanged. Adds cgroup_test.go
covering v2/v1, unlimited sentinels, malformed input and fallbacks.
@zouanbing zouanbing closed this Jul 10, 2026
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.

1 participant