Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Read cpu.stat regardless if controller enabled. #348

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jay-mckay
Copy link

The unified hierarchy provides the cpu.stat file for every cgroup, regardless if the CPU controller is enabled (in fact, setting the systemd property CPUAccounting=True does not enable this controller because of this fact). It provides the usage_usec, user_usec, and system_usec by default. Instead of reading the stat for each enabled controller (CPU and memory), just attempt to read them each time the Stat() function is called.

Attempting to read the memory.stat file even if memory accounting is not enabled seems insignificant (some other files always have a read attempt, such as memory.current), and eliminates finding and looping over enabled controllers.

resolves #347

@dcantah
Copy link
Member

dcantah commented Feb 3, 2025

Change looks good after the one comment is fixed

@thaJeztah
Copy link
Member

I see this PR branch also has some merge commits in it; we generally use a rebase flow to keep a clean commit history; when updating, could you do a rebase and squash the commits, so that there's only a single commit in the PR?

Let me know if you need help / instructions doing so!

The unified hierarchy provides the cpu.stat file for every cgroup,
regardless if the CPU controller is enabled (in fact, setting the
systemd property CPUAccounting=True does not enable this controller
because of this fact). It provides the usage_usec, user_usec,
and system_usec by default. Instead of reading the stat for each
enabled controller (CPU and memory), just attempt to read them
each time the Stat() function is called.

Attempting to read the memory.stat file even if memory accounting
is not enabled seems insignificant (some other files always have
a read attempt, such as memory.current), and eliminates finding and
looping over enabled controllers.

Resolves: containerd#347

Signed-off-by: Jackson McKay  <jackjaymckay@gmail.com>
@jay-mckay
Copy link
Author

Fix added and rebase complete, should only have one commit now. Thanks.

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

Left some thoughts / comments; not sure if I'm right though!

I did a quick draft while I was reviewing, and pushed it as a PR against your fork to show what it'd look like (if that approach is correct, merging it should make it show up in this PR as a separate commit; feel free to squash it though (probably better to);

@dcantah any thoughts? (also please double-check if my logic is correct)

Comment on lines +572 to +574
if !os.IsNotExist(err) {
return nil, err
}
Copy link
Member

Choose a reason for hiding this comment

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

So, while looking at this PR (and I think some of this is an existing issue) I wonder if it would make sense to skip assigning values in situations where the file does not exist, or if we should skip all of that if it's not there, and explicitly construct an empty &stats.MemoryStat{}.

@@ -630,6 +613,13 @@ func (c *Manager) Stat() (*stats.Metrics, error) {
SwapMaxUsage: getStatFileContentUint64(filepath.Join(c.path, "memory.swap.peak")),
Copy link
Member

Choose a reason for hiding this comment

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

I'm not entirely sure though if these can be present independently from memory.stat (perhaps someone else knows if that's something that we need to account for)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support cgroup v2 CPU stats when controller not enabled
3 participants