Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update start-odk.sh memory calculation to support cgroups v2 #876

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions files/service/scripts/start-odk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,51 @@ 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)
WORKER_COUNT=$(determine_worker_count "$MEMTOT")
export WORKER_COUNT
echo "using $WORKER_COUNT worker(s) based on available memory ($MEMTOT).."

echo "starting server."
Expand Down
Loading