Add get_cpus.sh functionality inline to nginx/run

This commit is contained in:
Felipe Santos 2024-06-05 16:34:17 -03:00
parent 111ec1597f
commit c2b98f9b5e
2 changed files with 47 additions and 8 deletions

View File

@ -30,9 +30,6 @@ RUN --mount=type=tmpfs,target=/tmp --mount=type=tmpfs,target=/var/cache/apt \
--mount=type=cache,target=/root/.ccache \ --mount=type=cache,target=/root/.ccache \
/deps/build_nginx.sh /deps/build_nginx.sh
# https://github.com/felipecrs/cgroup-scripts/commits/master/get_cpus.sh
ADD --link --chmod=755 "https://github.com/felipecrs/cgroup-scripts/raw/60f995e835f68e38e82b7cda4e169b96e8f3b5ee/get_cpus.sh" /usr/local/nginx/
FROM scratch AS go2rtc FROM scratch AS go2rtc
ARG TARGETARCH ARG TARGETARCH
WORKDIR /rootfs/usr/local/go2rtc/bin WORKDIR /rootfs/usr/local/go2rtc/bin

View File

@ -8,17 +8,59 @@ set -o errexit -o nounset -o pipefail
echo "[INFO] Starting NGINX..." echo "[INFO] Starting NGINX..."
# Taken from https://github.com/felipecrs/cgroup-scripts/commits/master/get_cpus.sh
function get_cpus() {
local quota=""
local period=""
if [ -f /sys/fs/cgroup/cgroup.controllers ]; then
if [ -f /sys/fs/cgroup/cpu.max ]; then
read -r quota period </sys/fs/cgroup/cpu.max
if [ "$quota" = "max" ]; then
quota=""
period=""
fi
else
echo "[WARN] /sys/fs/cgroup/cpu.max not found. Falling back to /proc/cpuinfo." >&2
fi
else
if [ -f /sys/fs/cgroup/cpu/cpu.cfs_quota_us ] && [ -f /sys/fs/cgroup/cpu/cpu.cfs_period_us ]; then
quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us)
period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us)
if [ "$quota" = "-1" ]; then
quota=""
period=""
fi
else
echo "[WARN] /sys/fs/cgroup/cpu/cpu.cfs_quota_us or /sys/fs/cgroup/cpu/cpu.cfs_period_us not found. Falling back to /proc/cpuinfo." >&2
fi
fi
local cpus
if [ -n "${quota}" ] && [ -n "${period}" ]; then
cpus=$((quota / period))
if [ "$cpus" -eq 0 ]; then
cpus=1
fi
else
cpus=$(grep -c processor /proc/cpuinfo)
fi
printf '%s' "$cpus"
}
function set_worker_processes() { function set_worker_processes() {
# Capture number of assigned CPUs to calculate worker processes # Capture number of assigned CPUs to calculate worker processes
local proc_count local cpus
proc_count=$(/usr/local/nginx/get_cpus.sh) cpus=$(get_cpus)
if [[ "$proc_count" -gt 4 ]]; then if [[ "${cpus}" -gt 4 ]]; then
proc_count=4 cpus=4
fi fi
# we need to catch any errors because sed will fail if user has bind mounted a custom nginx file # we need to catch any errors because sed will fail if user has bind mounted a custom nginx file
sed -i "s/worker_processes auto;/worker_processes ${proc_count};/" /usr/local/nginx/conf/nginx.conf || true sed -i "s/worker_processes auto;/worker_processes ${cpus};/" /usr/local/nginx/conf/nginx.conf || true
} }
set_worker_processes set_worker_processes