Skip to content

Commit

Permalink
BUG: Handle more Cgroups v1/v2 memory limit caveats
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorunic authored and oktalz committed Jul 30, 2024
1 parent c8ecdf9 commit 77b50c5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions fs/etc/s6-overlay/s6-rc.d/haproxy/run
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#!/command/with-contenv sh

MEMLIMIT=$(free -m | awk '/Mem:/ {print int($2 * 2 / 3)}')
MEMLIMIT=$(free -m | awk '/Mem:/ {printf "%d\n", int($2 * 2 / 3)}')

CG_LIMIT_FILE="/sys/fs/cgroup/memory/memory.limit_in_bytes"
if [ -f "/sys/fs/cgroup/cgroup.controllers" ]; then
CG_LIMIT_FILE="/sys/fs/cgroup/memory.max"
fi

if [ -r "${CG_LIMIT_FILE}" ]; then
MEMLIMIT_CG=$(awk '{print int($1 / 1024 / 1024 * 2 / 3)}' "${CG_LIMIT_FILE}")
if grep -q '^max$' "${CG_LIMIT_FILE}"; then
MEMLIMIT_CG="${MEMLIMIT}"
else
MEMLIMIT_CG=$(awk '{printf "%d\n", int($1 / 1024 / 1024 * 2 / 3)}' "${CG_LIMIT_FILE}")
fi

if [ "${MEMLIMIT_CG}" -gt 0 ]; then
if [ "${MEMLIMIT_CG}" -lt "${MEMLIMIT}" ]; then
Expand Down
8 changes: 6 additions & 2 deletions fs/etc/s6-overlay/s6-rc.d/ingress-controller/run
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#!/command/with-contenv sh

MEMLIMIT=$(free -m | awk '/Mem:/ {print int($2 / 3)}')
MEMLIMIT=$(free -m | awk '/Mem:/ {printf "%d\n", int($2 / 3)}')

CG_LIMIT_FILE="/sys/fs/cgroup/memory/memory.limit_in_bytes"
if [ -f "/sys/fs/cgroup/cgroup.controllers" ]; then
CG_LIMIT_FILE="/sys/fs/cgroup/memory.max"
fi

if [ -r "${CG_LIMIT_FILE}" ]; then
MEMLIMIT_CG=$(awk '{print int($1 / 1024 / 1024 / 3)}' "${CG_LIMIT_FILE}")
if grep -q '^max$' "${CG_LIMIT_FILE}"; then
MEMLIMIT_CG="${MEMLIMIT}"
else
MEMLIMIT_CG=$(awk '{printf "%d\n", int($1 / 1024 / 1024 / 3)}' "${CG_LIMIT_FILE}")
fi

if [ "${MEMLIMIT_CG}" -gt 0 ]; then
if [ "${MEMLIMIT_CG}" -lt "${MEMLIMIT}" ]; then
Expand Down

0 comments on commit 77b50c5

Please # to comment.