Skip to content

Feat/cgroup v2 support (#276)#277

Open
DhanashreePetare wants to merge 3 commits intoHSF:mainfrom
DhanashreePetare:feat/cgroup-v2-support
Open

Feat/cgroup v2 support (#276)#277
DhanashreePetare wants to merge 3 commits intoHSF:mainfrom
DhanashreePetare:feat/cgroup-v2-support

Conversation

@DhanashreePetare
Copy link
Copy Markdown

Cgroup v1/v2 Monitoring Support

Overview

Adds comprehensive cgroup (container) resource monitoring to prmon with automatic detection of cgroup v1, v2, and hybrid environments.

Changes

  • New monitor: cgroupmon with 17 metrics across CPU, memory, I/O, and process counts
  • Auto-detection: Detects cgroup version at runtime and adapts accordingly
  • Parser: Handles cpu.stat, memory.stat, io.stat for both v1 and v2
  • Tests: Python integration test + GTest skeleton with precooked fixtures
  • Docs: README section, ADDING_MONITORS example, CONTRIBUTING Windows guide
  • Disable flag: --disable cgroupmon if needed

Benefits

  • Better container/Kubernetes workload monitoring
  • More accurate I/O stats in containerized environments
  • Backward compatible with existing prmon deployments
  • Graceful degradation if cgroups unavailable

Testing

Python linting passes. CI will verify build and tests in Linux environments.

@amete
Copy link
Copy Markdown
Collaborator

amete commented Feb 3, 2026

Thanks a lot for the PR @DhanashreePetare.

Before we go further, I’d like to better understand the motivation and concrete use cases behind this change. The PR adds a fairly substantial amount of new code, and as maintainers we try to be cautious about introducing functionality unless there’s a clear and compelling need, since it also increases the long-term maintenance burden.

Could you elaborate a bit on the scenarios this is meant to support, and whether there are existing users or workflows that would benefit from it? That context would really help us evaluate how to move forward.

@DhanashreePetare
Copy link
Copy Markdown
Author

Thanks for the thoughtful review and for asking about motivation.

This PR is aimed at containerized and scheduler‑managed workloads where /proc no longer reflects actual resource limits or usage. On modern Linux distributions, cgroup v2 is now the default (e.g., Ubuntu 22.04+, Alma/RHEL 9+), and many HEP/HTC workflows run under Kubernetes, HTCondor, Slurm, or LSF, all of which enforce limits via cgroups. In those environments, prmon’s current /proc view can over‑report memory and under‑report I/O. The cgroup data is the authoritative source for the container’s real quota/usage.

Concrete scenarios this helps:

  • Grid jobs in containers (CVMFS/HTCondor/Singularity/Apptainer): cgroup v2 is increasingly common, and without it prmon misreports memory and I/O relative to enforced limits.
  • Kubernetes batch workflows: resource requests/limits are enforced by cgroups, so cgroup stats provide the correct signal for monitoring and post‑mortem accounting.
  • WLCG/HEP production pipelines: large‑scale job monitoring benefits from accurate cgroup I/O and memory data to avoid false alarms.

The implementation is automatic and safe: it detects v1/v2/hybrid, gracefully degrades when cgroups are absent, and can be disabled with --disable cgroupmon if not desired. The aim is to make prmon accurate in modern container environments while remaining backward‑compatible.

It would be great to hear on it, and I’m happy to adjust further implementation/changes if required based on maintainer's guidance.

@graeme-a-stewart
Copy link
Copy Markdown
Member

Hello @DhanashreePetare thank you for the explanations. @amete and I will look in more detail at the code and get back to you.

@graeme-a-stewart
Copy link
Copy Markdown
Member

Hello @DhanashreePetare

Thank you again for submitting this PR and apologies that many other constraints on our time prevented a more prompt review.

Serhan and I decided on a strategic approach to this that should inform the most significant changes to the PR:

  • We are in favour of this, having discussed with site colleagues, as cgroups definitely provides extra and useful information.
  • We decided to make this a future oriented PR, so we will only support cgroups v2. We have lived for a long time without it and most WLCG sites have now migrated to RHEL/Alma/Rocky 9 distributions, where cgroups v2 is supported. This means that cgroups v1 is a declining tail, we can live without it.
    • This will simplify the logic and reduce the number of lines of code, so it will be easier to maintain
  • We should report on focused metrics that are of interest to the payload owners. The current prmon metrics are the ones that have proven useful so we would include most of what you report, but exclude the following metrics that are less useful for the application owner:
    • "cgroup_mem_slab"
    • "cgroup_mem_pgfault"
    • "cgroup_mem_pgmajfault"

Can you update the PR along these lines and then we will do a more detailed review of the code?

Thank you!

Copy link
Copy Markdown
Member

@graeme-a-stewart graeme-a-stewart left a comment

Choose a reason for hiding this comment

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

See comment above for restructuring changes.

@graeme-a-stewart
Copy link
Copy Markdown
Member

Hello @DhanashreePetare

Is this an issue you would still like to work on? Please let us know and we can try to assign to another person if you are not able to continue.

Thanks.

@DhanashreePetare
Copy link
Copy Markdown
Author

sorry @graeme-a-stewart, very sorry for my late reply. Yes I will change the workflow according to the above provided changes, I will get back very soon with the changes.

Copy link
Copy Markdown
Member

@graeme-a-stewart graeme-a-stewart left a comment

Choose a reason for hiding this comment

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

Hello @DhanashreePetare - thanks a lot for the changes. There are still a few things to improve on with some further simplifications that will make the code even leaner.

Comment thread doc/CONTRIBUTING.md
- Use WSL2 (Ubuntu) or Docker Desktop to provide a Linux runtime.
- Run all builds and tests inside WSL2 or a Linux container.

For formatting on Windows before opening a pull request:
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.

Windows is not a supported environment for prmon, so while I appreciate your instructions, it's just adding noise. If one is using Windows or OS X then a container / WSL2 environment needs to be used and then you have your Linux for build, tests, formatting, etc.

Comment thread doc/CONTRIBUTING.md

```powershell
cmake -S . -B build
cmake --build build --target clang-format
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.

This is already documented (L59).

Comment thread package/src/cgroupmon.cpp
@@ -0,0 +1,291 @@
// Copyright (C) 2018-2025 CERN
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.

This is a new file, so it should be (C) for the dates the file existed, viz, 2025-2026 in this case.

Comment thread package/src/cgroupmon.cpp

std::string line;
// For cgroup v2, look for line starting with "0::"
// For cgroup v2, look for line starting with "0::"
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.

Duplicated comment.

Comment thread package/src/cgroupmon.cpp
// Find the cgroup filesystem mount point
std::string cgroupmon::find_cgroup_mount_point() {
return "/sys/fs/cgroup";
}
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.

This is now OTT for returning the same string every time (and you're not "detecting" anything). Instead just define a const std::string.

Comment thread package/src/cgroupmon.cpp
return;
}

hw_json["cgroup"]["version"] = "v2";
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.

This is not useful anymore as cgroups implies v2, and the mount point is fixed. Please remove.

Comment thread package/src/cgroupmon.h
#include "registry.h"

// Cgroup version enumeration
enum class CgroupVersion { NONE, V2 };
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.

I think this is no longer needed - see comments above w.r.t. detecting cgroupsv2. An enum is overdesign.

Comment thread package/src/cgroupmon.h
prmon::monitored_list cgroup_stats;

// Cgroup detection state
CgroupVersion cgroup_version;
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.

Not needed.

Comment thread package/src/cgroupmon.h
CgroupVersion cgroup_version;
bool valid;
std::string cgroup_path;
std::string cgroup_mount_point;
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.

This could now just be a const std::string.

Comment thread CGROUP_IMPLEMENTATION.md
@@ -0,0 +1,239 @@
# Cgroup v2 Support Implementation - Summary
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.

We really don't need this file. The historical record of the addition of the cgroups monitoring is in the pull request.

DhanashreePetare added 3 commits March 24, 2026 18:24
- Implement cgroupmon monitor with 17 metrics across CPU, memory, I/O, and process counts
- Support automatic detection of cgroup v1, v2, and hybrid environments
- Parse cgroup controllers: cpu.stat, memory.stat, io.stat for both versions
- Integrate with prmon build system and test infrastructure
- Add Python and GTest test harnesses with precooked cgroup v2 fixtures
- Update documentation: README, ADDING_MONITORS, CONTRIBUTING with usage and Windows dev notes
- Enable container resource tracking with --disable cgroupmon flag
- Provide more accurate I/O statistics for containerized workloads
…ead counting

- Remove unused cgroup_stat_update variable in update_stats()
- Fix read_single_value() to count lines for cgroup.procs and cgroup.threads
  instead of reading first value, since these are multi-line PID lists
… focused metrics

Based on maintainer guidance:
- Remove cgroup v1 and hybrid support (v2-only going forward)
- Remove 3 less useful metrics: cgroup_mem_slab, cgroup_mem_pgfault, cgroup_mem_pgmajfault
- Simplify logic for easier maintenance (14 metrics, no v1/v2 branching)
- Fix get_hardware_info() to avoid referencing removed enum values
- Update all documentation to reflect v2-only implementation

Changes:
- Enum: NONE, V1, V2, HYBRID → NONE, V2
- Metrics: 17 → 14 (removed: slab, pgfault, pgmajfault)
- Removed functions: read_cgroup_v1_stats, parse_*_v1 (4 functions)
- Simplified detection, path resolution, and hardware info logic
- Updated tests, README, ADDING_MONITORS, CGROUP_IMPLEMENTATION docs
@graeme-a-stewart
Copy link
Copy Markdown
Member

FYI, I just fixed the CMakeLists.txt conflict for you

@graeme-a-stewart
Copy link
Copy Markdown
Member

There is also a test failure, can you investigate it?

25/28 Testing: testUnits
25/28 Test: testUnits
Command: "/usr/bin/python3" "test_cpu.py" "--units" "--time" "3" "--slack" "0" "--interval" "1"
Directory: /tmp/build/package/tests
"testUnits" start time: Mar 24 17:33 UTC
Output:
----------------------------------------------------------
Will run for 3s using 1 process(es) and 1 thread(s)
[2026-03-24 17:33:21.529] [nvidiamon] [warning] Failed to load libnvidia-ml.so: libnvidia-ml.so.1: cannot open shared object file: No such file or directory
[2026-03-24 17:33:21.529] [nvidiamon] [warning] Both NVML and nvidia-smi failed. NVIDIA monitoring disabled.
F
======================================================================
FAIL: test_run_test_with_params (__main__.setup_configurable_test.<locals>.ConfigurableProcessMonitor)
Actual test runner
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/build/package/tests/test_cpu.py", line 99, in test_run_test_with_params
    self.assertEqual(
AssertionError: 3 != 0 : Wrong number of unit values for 'Avg' - missing parameters are {'cgroup_cpu_periods', 'cgroup_cpu_throttled', 'cgroup_mem_max'}

----------------------------------------------------------------------
Ran 1 test in 3.075s

N.B. You can ignore the nvidiamon warning - that needs to be lowered in severity

@graeme-a-stewart graeme-a-stewart removed the request for review from amete March 25, 2026 13:30
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.

3 participants