From 2240807aa0d8947a990713d4974d2ffb47e0b1b4 Mon Sep 17 00:00:00 2001 From: Dominic Lavery Date: Tue, 26 Nov 2024 12:57:32 +0000 Subject: [PATCH] Prefer cgroupsv2 in _query_cpu --- pylint/lint/run.py | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/pylint/lint/run.py b/pylint/lint/run.py index 1088f9fee3e..68d50a8e7ee 100644 --- a/pylint/lint/run.py +++ b/pylint/lint/run.py @@ -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() @@ -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