@@ -145,30 +145,54 @@ calc() {
145145
146146# Based on the cgroup limits, figure out the max number of core we should utilize
147147core_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
157157 echo $( calc ' ceil($1/$2)' " ${cpu_quota} " " ${cpu_period} " )
158158 fi
159159 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
160175 fi
161176}
162177
163178max_memory () {
164179 # High number which is the max limit until which memory is supposed to be
165180 # 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+
167191 if [ -r " ${mem_file} " ]; then
168192 local max_mem_cgroup=" $( cat ${mem_file} ) "
169193 local max_mem_meminfo_kb=" $( cat /proc/meminfo | awk ' /MemTotal/ {print $2}' ) "
170194 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} ]
172196 then
173197 echo " ${max_mem_cgroup} "
174198 fi
0 commit comments