Skip to content

Commit

Permalink
build: update start-odk server memory calc to work for cgroup v2
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Jan 16, 2025
1 parent f0b2a90 commit e5b2cc2
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions files/service/scripts/start-odk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,50 @@ fi
echo "starting cron.."
cron -f &

MEMTOT=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes)
if [ "$MEMTOT" -gt "1100000" ]
then
export WORKER_COUNT=4
else
export WORKER_COUNT=1
fi
get_cgroup_version() {
# The max memory calculation is different between cgroup v1 & v2
local cgroup_type
cgroup_type=$(stat -fc %T /sys/fs/cgroup/)
if [ "$cgroup_type" == "cgroup2fs" ]; then
echo "v2"
else
echo "v1"
fi
}

get_memory_limit() {
local cgroup_version
cgroup_version=$(get_cgroup_version)

if [ "$cgroup_version" == "v2" ]; then
local memtot
memtot=$(cat /sys/fs/cgroup/memory.max)
if [ "$memtot" == "max" ]; then
# No cgroup memory limit; fallback to system's total memory
memtot=$(grep MemTotal /proc/meminfo | awk '{print $2 * 1024}')
fi
# Force memtot to be an integer (not scientific notation e+09)
printf "%.0f\n" "$memtot"
else
# cgroup v1
local memtot
memtot=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes)
# Force memtot to be an integer
printf "%.0f\n" "$memtot"
fi
}

determine_worker_count() {
local memtot=$1
if [ "$memtot" -gt 1100000 ]; then
echo 4
else
echo 1
fi
}

MEMTOT=$(get_memory_limit)
export WORKER_COUNT=$(determine_worker_count "$MEMTOT")
echo "using $WORKER_COUNT worker(s) based on available memory ($MEMTOT).."

echo "starting server."
Expand Down

0 comments on commit e5b2cc2

Please sign in to comment.