Repository navigation
fix(cpu): count only the CPUs the process can use (affinity and cgroup quota) - #1476
manunicholasjacob wants to merge 1 commit into
Conversation
|
Thanks for this contribution, which will unlock some use cases on Kubernetes which we couldn't adress, due to our lack of knowledge of the way cgroups work. |
Outside SLURM, count_cpus() returned psutil.cpu_count(), the machine's logical core count, ignoring a restricted CPU affinity mask (taskset, docker run --cpuset-cpus, a cpuset) and a cgroup CPU quota (docker run --cpus, a Kubernetes CPU limit). In a container pinned or limited to a few cores on a large host, and when the CPU model is absent from the power registry, the TDP fallback estimates threads * DEFAULT_POWER_PER_CORE against the host count, so the CPU power and the reported cpu_count come out far too high. Honour the affinity mask where the OS exposes one (os.sched_getaffinity) and a cgroup v2 cpu.max / v1 cpu.cfs_quota_us quota, rounded up, falling back to psutil.cpu_count() off Linux. The SLURM path is unchanged.
2379a4f to
977df8b
Compare
|
glad this is useful I tested the branch on a Raspberry Pi 5 (4 cores, aarch64, cgroup v2) to check how it handles both CPU affinity and cgroup quotas. Here are the results from the actual runs using
A few observations from these results: The baseline remains unchanged, so there is no impact when running without CPU restrictions. With CPU pinning, the process is restricted to two cores. With a CFS quota, affinity still reports all four cores, but the When both restrictions are present, the implementation uses the lower of the two limits. This also affects the downstream power estimates. When the CPU model is not available in the registry, CodeCarbon uses For example, on the Raspberry Pi, a two-core limit results in an estimated 8 W instead of 16 W. On a typical 64-core node, the same two-core container would previously have been estimated at 256 W instead of approximately 8 W. This also affects the resulting energy and CO2 estimates. I also verified that the quota test was running with an actual cgroup limit ( I'd be happy to add the benchmark script to the repository or put together a short documentation note explaining the cgroup behaviour if that would be helpful. |
Description
In
codecarbon/core/util.py,count_cpus()currently returnspsutil.cpu_count()in the non-SLURM case. This reports the total number of logical CPUs on the host, even when the process has access to only a subset of them.There are two constraints that need to be considered:
taskset, Docker cpusets, or cpuset cgroups can limit which CPUs a process can use. On Linux,os.sched_getaffinity(0)provides the CPUs available to the current process.--cpusor Kubernetes CPU limits can restrict CPU usage independently of affinity. These limits can also be fractional.The CPU count is used when estimating CPU power consumption. If the CPU model is not available in CodeCarbon's registry, the estimate is calculated using the number of threads multiplied by
DEFAULT_POWER_PER_CORE(4 W). For example, a container limited to two CPUs running on a 64-core host could be reported as using 256 W instead of approximately 8 W. This significantly overestimates power consumption and affects the resulting energy and CO2 calculations.Related Issue
No existing issue or PR covers this (checked before opening).
Motivation and Context
Update
count_cpus()to report the effective number of CPUs available to the process rather than the total host count:os.sched_getaffinity(0)when available to determine the number of CPUs allowed by the affinity mask._read_cgroup_cpu_limit/_cgroup_cpu_limithelpers, which already support both cgroup v1 and v2 and returnNonewhen no CPU limit is configured.psutil.cpu_count()behaviour when neither affinity nor a CPU quota imposes a restriction.CodeCarbon already does allocation-aware counting on SLURM (parsing
NumCPUs=); this extends the same intent to the common container and cpuset case so the power model and metadata are not computed against cores the process cannot use.How Has This Been Tested?
tests/test_core_util.py, run locally:psutil.cpu_count();cpu.maxand v1cfs_quota/cfs_periodparsing (no root, no real cgroup);black, isort (
--profile black) and flake8 (--config=.flake8) are clean on the two changed files.Types of changes
AI Usage Disclosure
I wrote this implementation, reusing my own cgroup-limit technique from numexpr PR #584 (
_read_cgroup_cpu_limit/_cgroup_cpu_limitand thesched_getaffinitypreference). An AI assistant produced an initial draft, which I rewrote in my own style, and the test scaffolding, which I reviewed.Checklist: