Skip to content

fix: correct cgroup v2 swap memory calculation (#3823)#3845

Open
Elvand-Lie wants to merge 1 commit intogoogle:masterfrom
Elvand-Lie:fix-cgroupv2-swap
Open

fix: correct cgroup v2 swap memory calculation (#3823)#3845
Elvand-Lie wants to merge 1 commit intogoogle:masterfrom
Elvand-Lie:fix-cgroupv2-swap

Conversation

@Elvand-Lie
Copy link

Fixes #3823

Problem:
In cgroup v2 environments, cAdvisor is currently reporting zero swap metrics even when swap is active. This is due to handler.go applying legacy cgroup v1 math (SwapUsage - Usage). In v2, memory.swap.current is decoupled from RAM usage, so subtracting Usage incorrectly zeroes out the metric.

Solution:
Updated the IsCgroup2UnifiedMode() block to assign SwapUsage.Usage directly to ret.Memory.Swap without the legacy subtraction.

Notes:

  • Syntax verified with go fmt.
  • Shoutout to @AkshatDudeja77 for the initial investigation and identifying the regression point in handler.go.

@google-cla
Copy link

google-cla bot commented Feb 22, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@iholder101
Copy link
Contributor

Thank you very much for addressing this!

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.

cAdvisor does not expose swap metrics when running inside container (cgroup v2)

2 participants