Skip to content

Commit 25b0a5d

Browse files
committed
fix(fabric8io-images#118): Check CGroupsV2 files when calculating max/init memory (based on fabric8io-images#113)
1 parent 7c1d123 commit 25b0a5d

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

fish-pepper/run-java-sh/fp-files/run-java.sh

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,22 @@ core_limit() {
156156
if [ ${cpu_quota:-0} -ne -1 ]; then
157157
echo $(calc 'ceil($1/$2)' "${cpu_quota}" "${cpu_period}")
158158
fi
159+
else
160+
# it may be Cgroups v2
161+
local cg2_mount_point="$(cat /proc/mounts | grep cgroup2 | awk '{ print $2 }')"
162+
if [ -n "${cg2_mount_point}" -a -r /proc/self/cgroup ]; then
163+
# /proc/self/cgroup may give e.g., "0::/user.slice/.../app.slice/xxx.scope"
164+
local cg2_path="$(cut -d ':' -f3- /proc/self/cgroup)"
165+
local cpu_file="${cg2_mount_point}${cg2_path}/cpu.max"
166+
if [ -r "${cpu_file}" ]; then
167+
# https://www.kernel.org/doc/Documentation/cgroup-v2.txt
168+
local quota="$(awk '{print $1}' ${cpu_file})"
169+
local duration="$(awk '{print $2}' ${cpu_file})"
170+
if [ "max" != "${quota}" ]; then
171+
echo $(calc 'ceil($1/$2)' "${quota}" "${duration}")
172+
fi
173+
fi
174+
fi
159175
fi
160176
fi
161177
}
@@ -164,14 +180,30 @@ max_memory() {
164180
# High number which is the max limit until which memory is supposed to be
165181
# unbounded.
166182
local mem_file="/sys/fs/cgroup/memory/memory.limit_in_bytes"
183+
local max_mem_meminfo_kb="$(cat /proc/meminfo | awk '/MemTotal/ {print $2}')"
184+
local max_mem_meminfo="$(expr $max_mem_meminfo_kb \* 1024)"
167185
if [ -r "${mem_file}" ]; then
168186
local max_mem_cgroup="$(cat ${mem_file})"
169-
local max_mem_meminfo_kb="$(cat /proc/meminfo | awk '/MemTotal/ {print $2}')"
170-
local max_mem_meminfo="$(expr $max_mem_meminfo_kb \* 1024)"
171187
if [ ${max_mem_cgroup:-0} != -1 ] && [ ${max_mem_cgroup:-0} -lt ${max_mem_meminfo:-0} ]
172188
then
173189
echo "${max_mem_cgroup}"
174190
fi
191+
else
192+
# it may be Cgroups v2
193+
local cg2_mount_point="$(cat /proc/mounts | grep cgroup2 | awk '{ print $2 }')"
194+
if [ -n "${cg2_mount_point}" -a -r /proc/self/cgroup ]; then
195+
# /proc/self/cgroup may give e.g., "0::/user.slice/.../app.slice/xxx.scope"
196+
local cg2_path="$(cut -d ':' -f3- /proc/self/cgroup)"
197+
mem_file="${cg2_mount_point}${cg2_path}/memory.max"
198+
if [ -r "${mem_file}" ]; then
199+
local max_mem_cgroup="$(cat ${mem_file})"
200+
if [ -z "${max_mem_cgroup}" -o "max" = "${max_mem_cgroup}" ]; then
201+
echo "${max_mem_meminfo}"
202+
else
203+
echo "${max_mem_cgroup}"
204+
fi
205+
fi
206+
fi
175207
fi
176208
}
177209

@@ -254,7 +286,7 @@ run_java_options() {
254286

255287
debug_options() {
256288
if [ -n "${JAVA_ENABLE_DEBUG:-}" ] || [ -n "${JAVA_DEBUG_ENABLE:-}" ] || [ -n "${JAVA_DEBUG:-}" ]; then
257-
local debug_port="${JAVA_DEBUG_PORT:-5005}"
289+
local debug_port="${JAVA_DEBUG_PORT:-5005}"
258290
local suspend_mode="n"
259291
if [ -n "${JAVA_DEBUG_SUSPEND:-}" ]; then
260292
if ! echo "${JAVA_DEBUG_SUSPEND}" | grep -q -e '^\(false\|n\|no\|0\)$'; then

0 commit comments

Comments
 (0)