From 7e57ac8fab85a880f2d07da8bac4c1f2778fca6e Mon Sep 17 00:00:00 2001 From: KEN Date: Thu, 11 Jan 2024 00:51:39 -0800 Subject: [PATCH] =?UTF-8?q?Improve=20Broker=E2=80=98s=20startup=20script?= =?UTF-8?q?=20=20for=20Accurate=20Memory=20Allocation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- images/broker/alpine/runbroker-customize.sh | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/images/broker/alpine/runbroker-customize.sh b/images/broker/alpine/runbroker-customize.sh index 842b9d83..19aa3e0c 100755 --- a/images/broker/alpine/runbroker-customize.sh +++ b/images/broker/alpine/runbroker-customize.sh @@ -55,7 +55,34 @@ calculate_heap_sizes() case "`uname`" in Linux) system_memory_in_mb=`free -m| sed -n '2p' | awk '{print $2}'` + if [ -f /sys/fs/cgroup/memory/memory.limit_in_bytes ]; then + system_memory_in_mb_in_docker=$(($(cat /sys/fs/cgroup/memory/memory.limit_in_bytes)/1024/1024)) + elif [ -f /sys/fs/cgroup/memory.max ]; then + system_memory_in_mb_in_docker=$(($(cat /sys/fs/cgroup/memory.max)/1024/1024)) + else + error_exit "Can not get memory, please check cgroup" + fi + if [ $system_memory_in_mb_in_docker -lt $system_memory_in_mb ];then + system_memory_in_mb=$system_memory_in_mb_in_docker + fi + system_cpu_cores=`egrep -c 'processor([[:space:]]+):.*' /proc/cpuinfo` + if [ -f /sys/fs/cgroup/cpu/cpu.cfs_quota_us ]; then + system_cpu_cores_in_docker=$(($(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us)/$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us))) + elif [ -f /sys/fs/cgroup/cpu.max ]; then + QUOTA=$(cut -d ' ' -f 1 /sys/fs/cgroup/cpu.max) + PERIOD=$(cut -d ' ' -f 2 /sys/fs/cgroup/cpu.max) + if [ "$QUOTA" == "max" ]; then # no limit, see https://docs.kernel.org/admin-guide/cgroup-v2.html#cgroup-v2-cpu + system_cpu_cores_in_docker=$system_cpu_cores + else + system_cpu_cores_in_docker=$(($QUOTA/$PERIOD)) + fi + else + error_exit "Can not get cpu, please check cgroup" + fi + if [ $system_cpu_cores_in_docker -lt $system_cpu_cores -a $system_cpu_cores_in_docker -ne 0 ];then + system_cpu_cores=$system_cpu_cores_in_docker + fi ;; FreeBSD) system_memory_in_bytes=`sysctl hw.physmem | awk '{print $2}'`