Skip to content

Commit 2240807

Browse files
committed
Prefer cgroupsv2 in _query_cpu
1 parent ad9f26c commit 2240807

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

pylint/lint/run.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,7 @@ def _query_cpu() -> int | None:
4545
"""
4646
cpu_quota, avail_cpu = None, None
4747

48-
if Path("/sys/fs/cgroup/cpu/cpu.cfs_quota_us").is_file():
49-
with open("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", encoding="utf-8") as file:
50-
# Not useful for AWS Batch based jobs as result is -1, but works on local linux systems
51-
cpu_quota = int(file.read().rstrip())
52-
53-
if (
54-
cpu_quota
55-
and cpu_quota != -1
56-
and Path("/sys/fs/cgroup/cpu/cpu.cfs_period_us").is_file()
57-
):
58-
with open("/sys/fs/cgroup/cpu/cpu.cfs_period_us", encoding="utf-8") as file:
59-
cpu_period = int(file.read().rstrip())
60-
# Divide quota by period and you should get num of allotted CPU to the container,
61-
# rounded down if fractional.
62-
avail_cpu = int(cpu_quota / cpu_period)
63-
elif Path("/sys/fs/cgroup/cpu/cpu.shares").is_file():
64-
with open("/sys/fs/cgroup/cpu/cpu.shares", encoding="utf-8") as file:
65-
cpu_shares = int(file.read().rstrip())
66-
# For AWS, gives correct value * 1024.
67-
avail_cpu = int(cpu_shares / 1024)
68-
elif Path("/sys/fs/cgroup/cpu.max").is_file():
48+
if Path("/sys/fs/cgroup/cpu.max").is_file():
6949
# Cgroupv2 systems
7050
with open("/sys/fs/cgroup/cpu.max", encoding="utf-8") as file:
7151
line = file.read().rstrip()
@@ -77,6 +57,28 @@ def _query_cpu() -> int | None:
7757
if str_cpu_quota != "max":
7858
cpu_quota = int(str_cpu_quota)
7959
avail_cpu = int(cpu_quota / cpu_period)
60+
else:
61+
# Cgroupv1 systems
62+
if Path("/sys/fs/cgroup/cpu/cpu.cfs_quota_us").is_file():
63+
with open("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", encoding="utf-8") as file:
64+
# Not useful for AWS Batch based jobs as result is -1, but works on local linux systems
65+
cpu_quota = int(file.read().rstrip())
66+
67+
if (
68+
cpu_quota
69+
and cpu_quota != -1
70+
and Path("/sys/fs/cgroup/cpu/cpu.cfs_period_us").is_file()
71+
):
72+
with open("/sys/fs/cgroup/cpu/cpu.cfs_period_us", encoding="utf-8") as file:
73+
cpu_period = int(file.read().rstrip())
74+
# Divide quota by period and you should get num of allotted CPU to the container,
75+
# rounded down if fractional.
76+
avail_cpu = int(cpu_quota / cpu_period)
77+
elif Path("/sys/fs/cgroup/cpu/cpu.shares").is_file():
78+
with open("/sys/fs/cgroup/cpu/cpu.shares", encoding="utf-8") as file:
79+
cpu_shares = int(file.read().rstrip())
80+
# For AWS, gives correct value * 1024.
81+
avail_cpu = int(cpu_shares / 1024)
8082

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

0 commit comments

Comments
 (0)