88#
99# This script is meant to be sourced.
1010
11+ if [ " ${SCRIPT_DEBUG} " = " true" ] ; then
12+ set -x
13+ fi
14+
15+ # query the resources based on cgroups version
16+ # cgroups v1 points to tmpfs
17+ # cgroups v2 points to cgroup2fs
18+ CGROUPS_VERSION=" v1"
19+ tmp_fs=$( stat -fc %T /sys/fs/cgroup)
20+ if [ " ${tmp_fs} " = " cgroup2fs" ]; then
21+ CGROUPS_VERSION=" v2"
22+ fi
23+
1124ceiling () {
1225 awk -vnumber=" $1 " -vdiv=" $2 " '
1326 function ceiling(x){
@@ -21,16 +34,36 @@ ceiling() {
2134
2235# Based on the cgroup limits, figure out the max number of core we should utilize
2336core_limit () {
24- local cpu_period_file=" /sys/fs/cgroup/cpu/cpu.cfs_period_us"
25- local cpu_quota_file=" /sys/fs/cgroup/cpu/cpu.cfs_quota_us"
26- if [ -r " ${cpu_period_file} " ]; then
27- local cpu_period=" $( cat ${cpu_period_file} ) "
28-
29- if [ -r " ${cpu_quota_file} " ]; then
30- local cpu_quota=" $( cat ${cpu_quota_file} ) "
31- # cfs_quota_us == -1 --> no restrictions
32- if [ " x$cpu_quota " != " x-1" ]; then
33- ceiling " $cpu_quota " " $cpu_period "
37+ if [ " ${CGROUPS_VERSION} " = " v1" ]; then
38+ local cpu_period_file=" /sys/fs/cgroup/cpu/cpu.cfs_period_us"
39+ local cpu_quota_file=" /sys/fs/cgroup/cpu/cpu.cfs_quota_us"
40+ if [ -r " ${cpu_period_file} " ]; then
41+ local cpu_period=" $( cat ${cpu_period_file} ) "
42+ if [ -r " ${cpu_quota_file} " ]; then
43+ local cpu_quota=" $( cat ${cpu_quota_file} ) "
44+ # cfs_quota_us == -1 --> no restrictions
45+ if [ " x$cpu_quota " != " x-1" ]; then
46+ ceiling " $cpu_quota " " $cpu_period "
47+ fi
48+ fi
49+ fi
50+ else
51+ # v2
52+ # on cgroupsv2 the period and quota a queried from the same file
53+ local cpu_max_file=" /sys/fs/cgroup/cpu.max"
54+ # when both are set we will have the following output:
55+ # $MAX $PERIOD
56+ # where the first number is the quota/max and the second is the period
57+ # if the quota/max is not set then we will have only the period set:
58+ # max 100000
59+ if [ -r " ${cpu_max_file} " ]; then
60+ local cpu_max=" $( cat ${cpu_max_file} ) "
61+ if [ " x$cpu_max " != " x" ]; then
62+ local cpu_quota=" $( echo $cpu_max | awk ' {print $1}' ) "
63+ local cpu_period=" $( echo $cpu_max | awk ' {print $2}' ) "
64+ if [ " $cpu_quota " != " max" ] && [ " x$cpu_period " != " x" ]; then
65+ ceiling " $cpu_quota " " $cpu_period "
66+ fi
3467 fi
3568 fi
3669 fi
@@ -41,14 +74,27 @@ max_unbounded() {
4174}
4275
4376container_memory () {
44- # High number which is the max limit unit which memory is supposed to be
45- # unbounded.
46- local mem_file=" /sys/fs/cgroup/memory/memory.limit_in_bytes"
4777 local max_mem_unbounded=" $( max_unbounded) "
48- if [ -r " ${mem_file} " ]; then
49- local max_mem=" $( cat ${mem_file} ) "
50- if [ ${max_mem} -lt ${max_mem_unbounded} ]; then
51- echo " ${max_mem} "
78+ # High number which is the max limit unit which memory is supposed to be unbounded.
79+ if [ " ${CGROUPS_VERSION} " = " v1" ]; then
80+ local mem_file=" /sys/fs/cgroup/memory/memory.limit_in_bytes"
81+ if [ -r " ${mem_file} " ]; then
82+ local max_mem=" $( cat ${mem_file} ) "
83+ if [ ${max_mem} -lt ${max_mem_unbounded} ]; then
84+ echo " ${max_mem} "
85+ fi
86+ fi
87+ else
88+ # v2
89+ local mem_file=" /sys/fs/cgroup/memory.max"
90+ if [ -r " ${mem_file} " ]; then
91+ local max_mem=" $( cat ${mem_file} ) "
92+ # if not set, it will contain only the string "max"
93+ if [ " $max_mem " != " max" ]; then
94+ if [ ${max_mem} -lt ${max_mem_unbounded} ]; then
95+ echo " ${max_mem} "
96+ fi
97+ fi
5298 fi
5399 fi
54100}
0 commit comments