Repository navigation
fix(cpu): count only the CPUs the process can use (affinity and cgroup quota) - #1476
Open
manunicholasjacob wants to merge 1 commit into
Open
manunicholasjacob wants to merge 1 commit into
manunicholasjacob wants to merge 1 commit into
Conversation
Member
|
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.
manunicholasjacob
force-pushed
the
feat/cpu-count-affinity-cgroup-quota
branch
from
November 5, 2026 20:42
2379a4f to
977df8b
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: