Skip to content

Commit

Permalink
Read container memory limit from cgroup (v1 and v2)
Browse files Browse the repository at this point in the history
Uses memory.high if available (recommended way of setting a functioning soft limit), then first falls back to memory.max (e.g. from 'docker run -m'), then to memory.low (e.g. from 'docker run --memory-reservation'), and finally to memory.min.

Falls back to direct reading of '/sys/fs/cgroup/memory/memory.limit_in_bytes' for cases where that exists, but no full cgroupfs is mounted (e.g. on Heroku).

Limit enforcement (now to 8 TB) is still in place this way - a Docker v1 value will be read, and run into the limit for unrestricted containers, without hitting the fallback and getting returned.

GUS-W-16052317
  • Loading branch information
dzuelke committed Jun 25, 2024
1 parent 823966e commit 5fc3f45
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
7 changes: 6 additions & 1 deletion lib/environment.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ write_profile() {
local bp_dir="$1"
local build_dir="$2"
mkdir -p "$build_dir/.profile.d"
cp "$bp_dir"/profile/* "$build_dir/.profile.d/"
local restore_extglob
restore_extglob=$(shopt -p extglob) # will return "shopt -u extglob" (or "-s")
cp "$bp_dir"/profile/!(WEB_CONCURRENCY.sh) "$build_dir/.profile.d/"
$restore_extglob
# concatenate these two together
cat "$bp_dir"/etc/cgroups.sh "$bp_dir"/profile/WEB_CONCURRENCY.sh > "$build_dir/.profile.d/WEB_CONCURRENCY.sh"
}

write_ci_profile() {
Expand Down
19 changes: 15 additions & 4 deletions profile/WEB_CONCURRENCY.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,28 @@ log_concurrency() {
detect_memory() {
local default=$1

if [ -e /sys/fs/cgroup/memory/memory.limit_in_bytes ]; then
echo $(($(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) / 1048576))
local memory_limit
memory_limit=$(cgroup_util_read_cgroup_memory_limit_with_fallback) && {
echo $(( memory_limit / 1024 / 1024 ))
return
}

if (($? == 99)); then
dne_memory
else
echo "$default"
fi
fi
}

dne_memory() {
echo "129024"
}

bound_memory() {
local detected=$1
# Memory is bound to the maximum memory of known dyno types: ~126 GB
local max_detected_memory=129024
local max_detected_memory
max_detected_memory=$(dne_memory)
if (( detected > max_detected_memory )); then
echo "$max_detected_memory"
else
Expand Down

0 comments on commit 5fc3f45

Please sign in to comment.