Skip to content

add support for cgroupv2#18708

Open
jsvd wants to merge 8 commits intoelastic:mainfrom
jsvd:cgroupv2
Open

add support for cgroupv2#18708
jsvd wants to merge 8 commits intoelastic:mainfrom
jsvd:cgroupv2

Conversation

@jsvd
Copy link
Copy Markdown
Member

@jsvd jsvd commented Feb 3, 2026

With ubuntu 24.04 dropping support for cgroupsv1, it's ideal for Logstash to support cgroupsv2, as other parts of the Stack already do too.

This change introduces cgroupsv2 support and testing for it.

I tested this in a ubuntu 24.04 vm on gcp:

joaoduarte@jsvd-cgroupv2:~$ cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04.4 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.4 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
joaoduarte@jsvd-cgroupv2:~$ stat -fc %T /sys/fs/cgroup/
cgroup2fs
joaoduarte@jsvd-cgroupv2:~$   ls /sys/fs/cgroup/cpu/ 2>&1
ls: cannot access '/sys/fs/cgroup/cpu/': No such file or directory
joaoduarte@jsvd-cgroupv2:~$   ls /sys/fs/cgroup/memory/ 2>&1
ls: cannot access '/sys/fs/cgroup/memory/': No such file or directory
joaoduarte@jsvd-cgroupv2:~$

And we can see the result of the stack monitoring metrics before the change (no cgroup data):

Screenshot 2026-03-20 at 10 23 34

Logs show error in capturing cgroup data:

joaoduarte@jsvd-cgroupv2:~/logstash-9.3.1$ bin/logstash -e "input { generator {} } output {null{} } " --debug  | grep -e "logstash.instrument.periodicpoller.cgroup"
[2026-03-20T10:30:50,159][DEBUG][logstash.instrument.periodicpoller.cgroup] One or more required cgroup files or directories not found: /proc/self/cgroup, /sys/fs/cgroup/cpuacct, /sys/fs/cgroup/cpu

And with this changeset:

Screenshot 2026-03-20 at 10 23 46

No more error in logs:

joaoduarte@jsvd-cgroupv2:~/logstash$ bin/logstash -e "input { generator {} } output {null{} } " --debug  | grep -e "logstash.instrument.periodicpoller.cgroup"
[2026-03-20T12:30:19,442][DEBUG][logstash.instrument.periodicpoller.cgroup] using cgroupv2

Also still works with v1:

joaoduarte@jsvd-cgroupv1:~/logstash$ bin/logstash -e "input { generator {} } output {null{} } " --debug  | grep -e "logstash.instrument.periodicpoller.cgroup"
Using system java: /usr/bin/java
[2026-03-20T12:35:17,788][DEBUG][logstash.instrument.periodicpoller.cgroup] using cgroupv1

fixes #14534

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 3, 2026

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)
  • /run exhaustive tests : Run the exhaustive tests Buildkite pipeline.

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Feb 3, 2026

This pull request does not have a backport label. Could you fix it @jsvd? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-8./d is the label to automatically backport to the 8./d branch. /d is the digit.
  • If no backport is necessary, please add the backport-skip label

@jsvd
Copy link
Copy Markdown
Member Author

jsvd commented Mar 16, 2026

run exhaustive tests

jsvd added 3 commits March 20, 2026 10:38
Separate CGroupV2Resources from CGroupResources for cleaner v1/v2
separation. Try v1 first, fall back to v2 with labeled debug logging.
@jsvd
Copy link
Copy Markdown
Member Author

jsvd commented Mar 20, 2026

run exhaustive tests

Detect v1 or v2 on first call and cache the result, avoiding repeated
"required cgroup files or directories not found" debug messages.
@jsvd
Copy link
Copy Markdown
Member Author

jsvd commented Mar 20, 2026

run exhaustive tests

@jsvd jsvd marked this pull request as ready for review March 20, 2026 12:55

private

def resolve_v2_path
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.

Will this be called often? Should we set an instance variable once then use that for subsequent calls?

Copy link
Copy Markdown
Member

@donoghuc donoghuc left a comment

Choose a reason for hiding this comment

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

Verified by building a container with this branch:
debug log:

[2026-03-20T19:56:53,429][DEBUG][logstash.instrument.periodicpoller.cgroup] using cgroupv2

/_node/stats/os

{
    "os": {
        "cgroup": {
            "cpuacct": {
                "control_group": "/",
                "usage_nanos": 20372769000
            },
            "cpu": {
                "stat": {
                    "number_of_times_throttled": 0,
                    "number_of_elapsed_periods": 0,
                    "time_throttled_nanos": 0
                },
                "control_group": "/",
                "cfs_quota_micros": -1,
                "cfs_period_micros": 100000
            }
        }
    }
}

Left a question/suggestion to "cache" result from a method in an instance var, but its not a blocker. The implementation looks solid 👍

@jsvd
Copy link
Copy Markdown
Member Author

jsvd commented Apr 1, 2026

run exhaustive tests

private

def resolve_v2_path
@v2_path ||= read_v2_path
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.

nice

Copy link
Copy Markdown
Member

@donoghuc donoghuc left a comment

Choose a reason for hiding this comment

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

Looks like we need to manage that state in tests. I tested these suggestions locally and it worked.

described_class.instance_variable_set(:@resolved, false)
described_class.instance_variable_set(:@active_resources, nil)
described_class.instance_variable_set(:@active_label, nil)
described_class.instance_variable_set(:@logged_empty, false)
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.

Suggested change
described_class.instance_variable_set(:@logged_empty, false)
described_class.instance_variable_set(:@logged_empty, false)
Cgroup::CGROUP_V2_RESOURCES.instance_variable_set(:@v2_path, nil)

@elasticmachine
Copy link
Copy Markdown

💛 Build succeeded, but was flaky

Failed CI Steps

History

@jsvd
Copy link
Copy Markdown
Member Author

jsvd commented Apr 2, 2026

run exhaustive tests

@jsvd jsvd requested a review from donoghuc April 2, 2026 10:11
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.

Add support for cgroup v2

3 participants