Skip to content

Commit

Permalink
hard-code maximum value that serves as the 'silly' threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
dzuelke committed Jun 21, 2024
1 parent 7bfbe5c commit b4dc58e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
4 changes: 1 addition & 3 deletions bin/heroku-php-apache2
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,11 @@ fpm_command=( php-fpm --pid "$fpm_pidfile" --nodaemonize -y "$fpm_config_tmp" ${
httpd_command=( httpd -D NO_DETACH -c "Include $httpd_config" )

ram=
# a hard maximum we never want to exceed, e.g. in case of silly values
memory_maximum=$(( 8*1024*1024*1024*1024 ))
# attempt to read a "correct" limit from cgroupfs
# this will handle cgroups v1 and v2, and, for v2, prefer memory.high over memory.max over memory.low
if [[ -d "/proc" ]]; then # check for /proc, just in case someone is running this on e.g. macOS
[[ $verbose ]] && echo "Checking cgroupfs for memory limits..." >&2
ram=$(CGROUP_UTIL_VERBOSE=${verbose:+1} cgroup_util_read_cgroup_memory_limit "$memory_maximum") || {
ram=$(CGROUP_UTIL_VERBOSE=${verbose:+1} cgroup_util_read_cgroup_memory_limit) || {
# 99 means the limit was exceeded; in verbose mode, a message was then already printed
if (( $? == 99)); then
ram="512M"
Expand Down
4 changes: 1 addition & 3 deletions bin/heroku-php-nginx
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,11 @@ fpm_command=( php-fpm --pid "$fpm_pidfile" --nodaemonize -y "$fpm_config_tmp" ${
nginx_command=( nginx -c "$nginx_main" -g "pid $nginx_pidfile; include $nginx_config;" )

ram=
# a hard maximum we never want to exceed, e.g. in case of silly values
memory_maximum=$(( 8*1024*1024*1024*1024 ))
# attempt to read a "correct" limit from cgroupfs
# this will handle cgroups v1 and v2, and, for v2, prefer memory.high over memory.max over memory.low
if [[ -d "/proc" ]]; then # check for /proc, just in case someone is running this on e.g. macOS
[[ $verbose ]] && echo "Checking cgroupfs for memory limits..." >&2
ram=$(CGROUP_UTIL_VERBOSE=${verbose:+1} cgroup_util_read_cgroup_memory_limit "$memory_maximum") || {
ram=$(CGROUP_UTIL_VERBOSE=${verbose:+1} cgroup_util_read_cgroup_memory_limit) || {
# 99 means the limit was exceeded; in verbose mode, a message was then already printed
if (( $? == 99)); then
ram="512M"
Expand Down
34 changes: 16 additions & 18 deletions bin/util/cgroups.sh
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,16 @@ cgroup_util_read_cgroupv2_memory_limit() {
}

# reads a cgroup v1 (memory.limit_in_bytes) or v2 (memory.high, fallback to memory.max, fallback to memory.low, fallback to memory.min)
# optional argument is the maximum memory to allow for any value (e.g. Docker may give 8 Exabytes for unlimited containers); no value is returned if this value is exceeded, and it defaults to the value read from "free"
# if env var CGROUP_UTIL_PROCFS_ROOT is passed, it will be used instead of '/proc' to find '/proc/self/cgroup', '/proc/self/mountinfo' etc (useful for testing, defaults to '/proc')
# if env var CGROUP_UTIL_CGROUPFS_PREFIX is passed, it will be prepended to any /sys/fs/cgroup or similar path used (useful for testing, defaults to '')
# pass a value for env var CGROUP_UTIL_VERBOSE to enable verbose mode
cgroup_util_read_cgroup_memory_limit() {
local usage="Usage: ${FUNCNAME[0]} [MEMORY_MAXIMUM]"

if [[ -z "${CGROUP_UTIL_PROCFS_ROOT-}" ]]; then
local CGROUP_UTIL_PROCFS_ROOT=/proc
fi

local maximum=${1-}
if [[ -z "$maximum" ]]; then
maximum=$(set -o pipefail; free -b | awk 'NR == 2 { print $4 }') || {
echo "Could not determine maximum RAM from 'free'" >&2
return 2
}
fi
# this value is used as a threshold for "silly" maximums returned e.g. by Docker on a cgroups v1 system
local maximum=$((8 * 1024 * 1024 * 1024 * 1024)) # 8 TB

local controller=memory

Expand Down Expand Up @@ -199,17 +191,23 @@ cgroup_util_read_cgroup_memory_limit() {
fi
}

# reads a cgroup v1 (memory.limit_in_bytes) or v2 (memory.high, fallback to memory.max, fallback to memory.low, fallback to memory.min)
# optional argument is a file path to fall back to for reading a default value, useful e.g. when reading on a system that has a "fake" limit info file (defaults to '/sys/fs/cgroup/memory/memory.limit_in_bytes')
# if env var CGROUP_UTIL_PROCFS_ROOT is passed, it will be used instead of '/proc' to find '/proc/self/cgroup', '/proc/self/mountinfo' etc (useful for testing, defaults to '/proc')
# if env var CGROUP_UTIL_CGROUPFS_PREFIX is passed, it will be prepended to any /sys/fs/cgroup or similar path used (useful for testing, defaults to '')
# pass a value for env var CGROUP_UTIL_VERBOSE to enable verbose mode
cgroup_util_read_cgroup_memory_limit_with_fallback() {
local fallback=${1-"/sys/fs/cgroup/memory/memory.limit_in_bytes"}

cgroup_util_read_cgroup_memory_limit_with_fallback "$@"
local retval=$?
cgroup_util_read_cgroup_memory_limit_with_fallback || {
local retval=$?

if ((retval != 99)) && [[ -r "$fallback" ]]; then
[[ -n ${CGROUP_UTIL_VERBOSE-} ]] && echo "Reading fallback limit from '${fallback}'" >&2
cat "$fallback"
return
fi
if ((retval != 99)) && [[ -r "$fallback" ]]; then
[[ -n ${CGROUP_UTIL_VERBOSE-} ]] && echo "Reading fallback limit from '${fallback}'" >&2
cat "$fallback"
return
fi

return "$retval"
return "$retval"
}
}

0 comments on commit b4dc58e

Please sign in to comment.