Skip to content

Commit e80e97f

Browse files
authored
Improve Broker‘s startup script for Accurate Memory Allocation (#210)
This update prevents Out-Of-Memory (OOM) errors in Broker's pods by accurately adhering to their allocated memory, rather than relying on the host's total memory.
1 parent 674666f commit e80e97f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

images/broker/alpine/runbroker-customize.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,34 @@ calculate_heap_sizes()
5555
case "`uname`" in
5656
Linux)
5757
system_memory_in_mb=`free -m| sed -n '2p' | awk '{print $2}'`
58+
if [ -f /sys/fs/cgroup/memory/memory.limit_in_bytes ]; then
59+
system_memory_in_mb_in_docker=$(($(cat /sys/fs/cgroup/memory/memory.limit_in_bytes)/1024/1024))
60+
elif [ -f /sys/fs/cgroup/memory.max ]; then
61+
system_memory_in_mb_in_docker=$(($(cat /sys/fs/cgroup/memory.max)/1024/1024))
62+
else
63+
error_exit "Can not get memory, please check cgroup"
64+
fi
65+
if [ $system_memory_in_mb_in_docker -lt $system_memory_in_mb ];then
66+
system_memory_in_mb=$system_memory_in_mb_in_docker
67+
fi
68+
5869
system_cpu_cores=`egrep -c 'processor([[:space:]]+):.*' /proc/cpuinfo`
70+
if [ -f /sys/fs/cgroup/cpu/cpu.cfs_quota_us ]; then
71+
system_cpu_cores_in_docker=$(($(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us)/$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us)))
72+
elif [ -f /sys/fs/cgroup/cpu.max ]; then
73+
QUOTA=$(cut -d ' ' -f 1 /sys/fs/cgroup/cpu.max)
74+
PERIOD=$(cut -d ' ' -f 2 /sys/fs/cgroup/cpu.max)
75+
if [ "$QUOTA" == "max" ]; then # no limit, see https://docs.kernel.org/admin-guide/cgroup-v2.html#cgroup-v2-cpu
76+
system_cpu_cores_in_docker=$system_cpu_cores
77+
else
78+
system_cpu_cores_in_docker=$(($QUOTA/$PERIOD))
79+
fi
80+
else
81+
error_exit "Can not get cpu, please check cgroup"
82+
fi
83+
if [ $system_cpu_cores_in_docker -lt $system_cpu_cores -a $system_cpu_cores_in_docker -ne 0 ];then
84+
system_cpu_cores=$system_cpu_cores_in_docker
85+
fi
5986
;;
6087
FreeBSD)
6188
system_memory_in_bytes=`sysctl hw.physmem | awk '{print $2}'`

0 commit comments

Comments
 (0)