Skip to content

cgroup1: support stats collection for selected subsystems - #402

Closed
linrl3 wants to merge 1 commit into
containerd:mainfrom
linrl3:codex/cgroup1-stat-filter
Closed

linrl3 wants to merge 1 commit into
containerd:mainfrom
linrl3:codex/cgroup1-stat-filter

Conversation

@linrl3

@linrl3 linrl3 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

cgroup v1 stats collection visits every loaded subsystem, even when a caller only needs CPU and memory metrics. In particular, blkio opens /proc/partitions on every collection, contributing to the contention reported in #370.

Add cgroup1.StatFiltered(control, subsystems, handlers...) to skip unselected subsystems before path resolution or I/O. It reuses the existing collection and error-handling logic; an empty list preserves full collection. CPU throttling and usage remain separately selectable through Cpu and Cpuacct.

Keep the Cgroup interface and existing Stat behavior compatible. The helper dispatches to an optional StatFiltered method, including on third-party implementations, and reports errors.ErrUnsupported when unavailable. Existing callers must opt into the new helper to benefit.

Tests cover selected metrics, skipped blkio I/O and path lookups, duplicate/inactive names, error handlers, loaded/child/deleted cgroups, and custom implementations. A fixture-based benchmark compares full collection with CPU and memory subsets.

Fixes #370.

Validation on Linux arm64 with Go 1.22.12:

  • go test -exec sudo -race -coverprofile=/tmp/cgroups-370-coverage.txt -covermode=atomic ./... passed.
  • golangci-lint v2.6.2 run -D errcheck passed with 0 issues, matching the CI linter configuration.
  • Linux amd64 cross-build and Linux arm64 go vet ./... passed on the macOS host.

The Linux guest runs cgroup v2; the new v1 tests exercise real controller parsers against regular-file fixtures, without requiring a live v1 mount.

go test ./cgroup1 -run '^$' -bench '^BenchmarkStatFiltered$' -benchmem -count=3 (median of three runs, 2 vCPUs):

Selection ns/op B/op allocs/op
All 76,597 50,890 359
CPU 19,898 11,328 53
Memory 58,678 22,540 227
CPU + memory 59,988 33,311 271

These fixture results show reduced work when controllers are excluded; they do not measure kernel /proc/partitions contention or end-to-end CRI performance.

AI assistance: implementation, tests, and documentation were developed with OpenAI Codex.

Allow callers that only need CPU and memory metrics to skip other
controllers, including blkio and its reads of /proc/partitions.

Expose a package-level StatFiltered helper backed by an optional method
on the concrete cgroup. This preserves the existing Cgroup interface and
Stat behavior while allowing third-party implementations to opt in.
Retain the existing error handlers and metrics initialization, and treat
an empty filter as full collection, consistent with Add's filtering.

Add regression tests for skipped I/O and path resolution, CPU controller
selection, error handling and cgroup lifecycle behavior, plus a benchmark
using regular-file fixtures.

Fixes containerd#370

Assisted-by: OpenAI Codex
Signed-off-by: linrunlong <linrunlong@bytedance.com>
@linrl3 linrl3 closed this Oct 22, 2026
@linrl3
linrl3 deleted the codex/cgroup1-stat-filter branch October 22, 2026 17:01
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.

support filter subsystem on cgroup.Stat() function

1 participant