Skip to content

Commit 5fa2de0

Browse files
committed
Read container memory limit from cgroup (v1 and v2)
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
1 parent 823966e commit 5fa2de0

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [Unreleased]
44

5+
- Support reading container memory limits from cgroups (v1 and v2)
6+
57

68
## [v255] - 2024-06-21
79

lib/environment.sh

100644100755
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ write_profile() {
8686
local bp_dir="$1"
8787
local build_dir="$2"
8888
mkdir -p "$build_dir/.profile.d"
89-
cp "$bp_dir"/profile/* "$build_dir/.profile.d/"
89+
local restore_extglob
90+
restore_extglob=$(shopt -p extglob) # will return "shopt -u extglob" (or "-s"), for later
91+
shopt -s extglob
92+
cp "$bp_dir"/profile/!(WEB_CONCURRENCY.sh) "$build_dir/.profile.d/"
93+
$restore_extglob
94+
# concatenate these two together
95+
cat "$bp_dir"/etc/cgroups.sh "$bp_dir"/profile/WEB_CONCURRENCY.sh > "$build_dir/.profile.d/WEB_CONCURRENCY.sh"
9096
}
9197

9298
write_ci_profile() {

profile/WEB_CONCURRENCY.sh

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,28 @@ log_concurrency() {
3131
detect_memory() {
3232
local default=$1
3333

34-
if [ -e /sys/fs/cgroup/memory/memory.limit_in_bytes ]; then
35-
echo $(($(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) / 1048576))
34+
local memory_limit
35+
memory_limit=$(cgroup_util_read_cgroup_memory_limit_with_fallback) && {
36+
echo $(( memory_limit / 1024 / 1024 ))
37+
return
38+
}
39+
40+
if (($? == 99)); then
41+
dne_memory
3642
else
3743
echo "$default"
38-
fi
44+
fi
45+
}
46+
47+
dne_memory() {
48+
echo "129024"
3949
}
4050

4151
bound_memory() {
4252
local detected=$1
4353
# Memory is bound to the maximum memory of known dyno types: ~126 GB
44-
local max_detected_memory=129024
54+
local max_detected_memory
55+
max_detected_memory=$(dne_memory)
4556
if (( detected > max_detected_memory )); then
4657
echo "$max_detected_memory"
4758
else

0 commit comments

Comments
 (0)