Skip to content

cgroups: change memory usage calculation to match Docker#2455

Merged
openshift-merge-bot[bot] merged 1 commit into
containers:mainfrom
giuseppe:change-calculation-used-memory
Jun 24, 2025
Merged

cgroups: change memory usage calculation to match Docker#2455
openshift-merge-bot[bot] merged 1 commit into
containers:mainfrom
giuseppe:change-calculation-used-memory

Conversation

@giuseppe

@giuseppe giuseppe commented Jun 12, 2025

Copy link
Copy Markdown
Member

This changes the memory usage calculation to follow Docker's approach:

  • For cgroup v2: memory.current - memory.stat['inactive_file']
  • For cgroup v1: memory.usage_in_bytes - memory.stat['total_inactive_file']

The previous calculation used anonymous memory only, but Docker's calculation provides more accurate memory usage by excluding inactive file cache.

Closes: #2454

Summary by Sourcery

Align cgroup memory usage calculation with Docker by subtracting inactive file caches for both v1 and v2

Enhancements:

  • Compute cgroup v2 memory usage as memory.current minus memory.stat['inactive_file']
  • Compute cgroup v1 memory usage as memory.usage_in_bytes minus memory.stat['total_inactive_file']

This changes the memory usage calculation to follow Docker's approach:

- For cgroup v2: memory.current - memory.stat['inactive_file']
- For cgroup v1: memory.usage_in_bytes - memory.stat['total_inactive_file']

The previous calculation used anonymous memory only, but Docker's
calculation provides more accurate memory usage by excluding
inactive file cache.

Closes: containers#2454

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
@sourcery-ai

sourcery-ai Bot commented Jun 12, 2025

Copy link
Copy Markdown

Reviewer's Guide

This PR refactors the cgroups memory usage calculation to align with Docker’s methodology by subtracting inactive file cache from total memory usage in both cgroup v2 and cgroup v1, replacing the previous anonymous‐only approach.

Sequence diagram: cgroup v2 memory data retrieval

sequenceDiagram
    participant Handler as linuxMemHandler
    participant FS as FileSystem
    Handler->>FS: Read "memory.current"
    FS-->>Handler: current_value
    Handler->>FS: Read "inactive_file" from "memory.stat"
    FS-->>Handler: inactive_file_value
Loading

Sequence diagram: cgroup v1 memory data retrieval

sequenceDiagram
    participant Handler as linuxMemHandler
    participant FS as FileSystem
    Handler->>FS: Read "memory.usage_in_bytes"
    FS-->>Handler: usage_in_bytes_value
    Handler->>FS: Read "total_inactive_file" from "memory.stat"
    FS-->>Handler: total_inactive_file_value
Loading

Class diagram for modified linuxMemHandler

classDiagram
    class linuxMemHandler {
        +Stat(ctr *CgroupControl, m *cgroups.Stats) error
    }
Loading

Flow diagram for updated memory usage calculation logic

graph TD
    A[Start: Stat function] --> B{Is ctr.cgroup2?};
    B -- Yes --> C[memoryRoot = cgroup v2 path];
    C --> D[Read 'memory.current' --> current_val];
    D --> E[Read 'inactive_file' from 'memory.stat' --> inactiveFile_val];
    E --> F{inactiveFile_val < current_val?};
    F -- Yes --> G[memUsage.Usage.Usage = current_val - inactiveFile_val];
    F -- No --> H[memUsage.Usage.Usage = 0];
    G --> Z[Update m.Stats];
    H --> Z;

    B -- No --> I[memoryRoot = cgroup v1 path];
    I --> J[Read 'memory.usage_in_bytes' --> usageInBytes_val];
    J --> K[Read 'total_inactive_file' from 'memory.stat' --> totalInactiveFile_val];
    K --> L{totalInactiveFile_val < usageInBytes_val?};
    L -- Yes --> M[memUsage.Usage.Usage = usageInBytes_val - totalInactiveFile_val];
    L -- No --> N[memUsage.Usage.Usage = 0];
    M --> Z;
    N --> Z;
    Z --> End[Return error status];
Loading

File-Level Changes

Change Details Files
Switch cgroup v2 usage to memory.current - inactive_file
  • Read memory.current with readFileAsUint64
  • Read inactive_file via readFileByKeyAsUint64
  • Subtract inactive_file from current if less
  • Removed anon-based usage calculation
pkg/cgroups/memory_linux.go
Switch cgroup v1 usage to memory.usage_in_bytes - total_inactive_file
  • Read memory.usage_in_bytes with readFileAsUint64
  • Read total_inactive_file via readFileByKeyAsUint64
  • Subtract total_inactive_file from usage_in_bytes if less
  • Removed looping over anon fields
pkg/cgroups/memory_linux.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @giuseppe - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

return err
}

// Docker calculation: memory.current - memory.stat['inactive_file']

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Extract common memory usage logic into a helper

This calculation is repeated in both cgroup v1 and v2. Refactoring it into a shared utility function would improve maintainability and consistency.

Suggested implementation:

		// Docker calculation: memory.current - memory.stat['inactive_file']
		memUsage.Usage.Usage = calculateDockerMemUsage(current, inactiveFile)
		// Read memory.usage_in_bytes
		usageInBytes, err := readFileAsUint64(filepath.Join(memoryRoot, "memory.usage_in_bytes"))
func calculateDockerMemUsage(usage, inactiveFile uint64) uint64 {
	if inactiveFile < usage {
		return usage - inactiveFile
	}
	return 0
}

You should also update the cgroup v1 code path to use calculateDockerMemUsage instead of duplicating the logic.
If the v1 code path also does memUsage.Usage.Usage = 0; if inactiveFile < usageInBytes { memUsage.Usage.Usage = usageInBytes - inactiveFile }, replace it with memUsage.Usage.Usage = calculateDockerMemUsage(usageInBytes, inactiveFile).

@giuseppe

Copy link
Copy Markdown
Member Author

the difference with Docker is that when inactive_file is bigger than memory.current then 0 is returned instead of memory.current. Not sure what behavior is more correct in this race condition but the implementation in the current PR avoids the usage value to switch immediately from memory.current to a very small value when inactive_file is close to the usage.

@giuseppe

Copy link
Copy Markdown
Member Author

@containers/podman-maintainers PTAL

@Luap99 Luap99 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@openshift-ci

openshift-ci Bot commented Jun 16, 2025

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: giuseppe, Luap99, sourcery-ai[bot]

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@giuseppe

Copy link
Copy Markdown
Member Author

@mheon @flouthoc PTAL

@jankaluza

Copy link
Copy Markdown
Member

LGTM

@Luap99

Luap99 commented Jun 24, 2025

Copy link
Copy Markdown
Member

/lgtm

@openshift-ci openshift-ci Bot added the lgtm label Jun 24, 2025
@openshift-merge-bot openshift-merge-bot Bot merged commit 6769e5b into containers:main Jun 24, 2025
14 checks passed
akrzos added a commit to akrzos/jetlag that referenced this pull request Jun 10, 2026
…up usage

The registry container memory panel was reporting cgroup memory.current
which includes reclaimable page cache, inflating reported usage far beyond
actual container memory consumption. Add a working_set metric computed as
memory.current - inactive_file (the standard calculation used by podman
stats and Docker stats per containers/common#2455) and display it as the
primary series on the bastion dashboard.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
akrzos added a commit to akrzos/jetlag that referenced this pull request Jun 10, 2026
…up usage

The registry container memory panel was reporting cgroup memory.current
which includes reclaimable page cache, inflating reported usage far beyond
actual container memory consumption. Add a working_set metric computed as
memory.current - inactive_file (the standard calculation used by podman
stats and Docker stats per containers/common#2455) and display it as the
primary series on the bastion dashboard.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
akrzos added a commit to akrzos/jetlag that referenced this pull request Jun 10, 2026
…up usage

The registry container memory panel was reporting cgroup memory.current
which includes reclaimable page cache, inflating reported usage far beyond
actual container memory consumption. Add a working_set metric computed as
memory.current - inactive_file (the standard calculation used by podman
stats and Docker stats per containers/common#2455) and display it as the
primary series on the bastion dashboard.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
openshift-merge-bot Bot pushed a commit to redhat-performance/jetlag that referenced this pull request Jun 11, 2026
…up usage (#838)

The registry container memory panel was reporting cgroup memory.current
which includes reclaimable page cache, inflating reported usage far beyond
actual container memory consumption. Add a working_set metric computed as
memory.current - inactive_file (the standard calculation used by podman
stats and Docker stats per containers/common#2455) and display it as the
primary series on the bastion dashboard.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

podman stats shows misleading memory usage on cgroup v2

3 participants