@@ -45,27 +45,7 @@ def _query_cpu() -> int | None:
45
45
"""
46
46
cpu_quota , avail_cpu = None , None
47
47
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 ():
69
49
# Cgroupv2 systems
70
50
with open ("/sys/fs/cgroup/cpu.max" , encoding = "utf-8" ) as file :
71
51
line = file .read ().rstrip ()
@@ -77,6 +57,28 @@ def _query_cpu() -> int | None:
77
57
if str_cpu_quota != "max" :
78
58
cpu_quota = int (str_cpu_quota )
79
59
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 )
80
82
81
83
# In K8s Pods also a fraction of a single core could be available
82
84
# As multiprocessing is not able to run only a "fraction" of process
0 commit comments