Skip to content

Commit

Permalink
Prefer cgroupsv2 in _query_cpu
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicLavery committed Nov 26, 2024
1 parent ad9f26c commit 2240807
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions pylint/lint/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,7 @@ def _query_cpu() -> int | None:
"""
cpu_quota, avail_cpu = None, None

if Path("/sys/fs/cgroup/cpu/cpu.cfs_quota_us").is_file():
with open("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", encoding="utf-8") as file:
# Not useful for AWS Batch based jobs as result is -1, but works on local linux systems
cpu_quota = int(file.read().rstrip())

if (
cpu_quota
and cpu_quota != -1
and Path("/sys/fs/cgroup/cpu/cpu.cfs_period_us").is_file()
):
with open("/sys/fs/cgroup/cpu/cpu.cfs_period_us", encoding="utf-8") as file:
cpu_period = int(file.read().rstrip())
# Divide quota by period and you should get num of allotted CPU to the container,
# rounded down if fractional.
avail_cpu = int(cpu_quota / cpu_period)
elif Path("/sys/fs/cgroup/cpu/cpu.shares").is_file():
with open("/sys/fs/cgroup/cpu/cpu.shares", encoding="utf-8") as file:
cpu_shares = int(file.read().rstrip())
# For AWS, gives correct value * 1024.
avail_cpu = int(cpu_shares / 1024)
elif Path("/sys/fs/cgroup/cpu.max").is_file():
if Path("/sys/fs/cgroup/cpu.max").is_file():
# Cgroupv2 systems
with open("/sys/fs/cgroup/cpu.max", encoding="utf-8") as file:
line = file.read().rstrip()
Expand All @@ -77,6 +57,28 @@ def _query_cpu() -> int | None:
if str_cpu_quota != "max":
cpu_quota = int(str_cpu_quota)
avail_cpu = int(cpu_quota / cpu_period)
else:
# Cgroupv1 systems
if Path("/sys/fs/cgroup/cpu/cpu.cfs_quota_us").is_file():
with open("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", encoding="utf-8") as file:
# Not useful for AWS Batch based jobs as result is -1, but works on local linux systems
cpu_quota = int(file.read().rstrip())

if (
cpu_quota
and cpu_quota != -1
and Path("/sys/fs/cgroup/cpu/cpu.cfs_period_us").is_file()
):
with open("/sys/fs/cgroup/cpu/cpu.cfs_period_us", encoding="utf-8") as file:
cpu_period = int(file.read().rstrip())
# Divide quota by period and you should get num of allotted CPU to the container,
# rounded down if fractional.
avail_cpu = int(cpu_quota / cpu_period)
elif Path("/sys/fs/cgroup/cpu/cpu.shares").is_file():
with open("/sys/fs/cgroup/cpu/cpu.shares", encoding="utf-8") as file:
cpu_shares = int(file.read().rstrip())
# For AWS, gives correct value * 1024.
avail_cpu = int(cpu_shares / 1024)

# In K8s Pods also a fraction of a single core could be available
# As multiprocessing is not able to run only a "fraction" of process
Expand Down

0 comments on commit 2240807

Please sign in to comment.