@@ -145,30 +145,54 @@ calc() {
145
145
146
146
# Based on the cgroup limits, figure out the max number of core we should utilize
147
147
core_limit () {
148
- local cpu_period_file= " /sys/fs/cgroup/cpu/cpu.cfs_period_us "
149
- local cpu_quota_file= " /sys/fs/ cgroup/cpu/cpu.cfs_quota_us "
150
- if [ -r " ${cpu_period_file} " ] ; then
151
- local cpu_period= " $( cat ${cpu_period_file} ) "
152
-
153
- if [ -r " ${cpu_quota_file} " ] ; then
154
- local cpu_quota= " $( cat ${cpu_quota_file} ) "
155
- # cfs_quota_us == -1 --> no restrictions
156
- if [ ${cpu_quota:- 0} -ne -1 ]; then
148
+ if [ -r " /sys/fs/cgroup/cgroup.controllers " ] ; then
149
+ # cgroup v2
150
+ local cpu_file= " /sys/fs/cgroup/cpu.max "
151
+ if [ -r " ${cpu_file} " ] ; then
152
+ local cpu_quota= " $( cat ${cpu_file} | awk ' { print $1 } ' ) "
153
+ local cpu_period= " $( cat ${cpu_file} | awk ' { print $2 } ' ) "
154
+
155
+ # cfs_quota_us == max --> no restrictions
156
+ if [ " ${cpu_quota:- 0} " != " max " ]; then
157
157
echo $( calc ' ceil($1/$2)' " ${cpu_quota} " " ${cpu_period} " )
158
158
fi
159
159
fi
160
+ else
161
+ # cgroup v1
162
+ local cpu_period_file=" /sys/fs/cgroup/cpu/cpu.cfs_period_us"
163
+ local cpu_quota_file=" /sys/fs/cgroup/cpu/cpu.cfs_quota_us"
164
+ if [ -r " ${cpu_period_file} " ]; then
165
+ local cpu_period=" $( cat ${cpu_period_file} ) "
166
+
167
+ if [ -r " ${cpu_quota_file} " ]; then
168
+ local cpu_quota=" $( cat ${cpu_quota_file} ) "
169
+ # cfs_quota_us == -1 --> no restrictions
170
+ if [ ${cpu_quota:- 0} -ne -1 ]; then
171
+ echo $( calc ' ceil($1/$2)' " ${cpu_quota} " " ${cpu_period} " )
172
+ fi
173
+ fi
174
+ fi
160
175
fi
161
176
}
162
177
163
178
max_memory () {
164
179
# High number which is the max limit until which memory is supposed to be
165
180
# unbounded.
166
- local mem_file=" /sys/fs/cgroup/memory/memory.limit_in_bytes"
181
+
182
+ local mem_file=" "
183
+ if [ -r " /sys/fs/cgroup/cgroup.controllers" ]; then
184
+ # cgroup v2
185
+ mem_file=" /sys/fs/cgroup/memory.max"
186
+ else
187
+ # cgroup v1
188
+ mem_file=" /sys/fs/cgroup/memory/memory.limit_in_bytes"
189
+ fi
190
+
167
191
if [ -r " ${mem_file} " ]; then
168
192
local max_mem_cgroup=" $( cat ${mem_file} ) "
169
193
local max_mem_meminfo_kb=" $( cat /proc/meminfo | awk ' /MemTotal/ {print $2}' ) "
170
194
local max_mem_meminfo=" $( expr $max_mem_meminfo_kb \* 1024) "
171
- if [ ${max_mem_cgroup:- 0} != -1 ] && [ ${max_mem_cgroup:- 0} -lt ${max_mem_meminfo:- 0} ]
195
+ if [ ${max_mem_cgroup:- 0} != -1 ] && [ " ${max_mem_cgroup :- max} " != " max " ] && [ ${max_mem_cgroup:- 0} -lt ${max_mem_meminfo:- 0} ]
172
196
then
173
197
echo " ${max_mem_cgroup} "
174
198
fi
0 commit comments