Created
August 15, 2018 05:02
-
-
Save weldpua2008/2e467639d2a741febbeaa5e9b5d856d5 to your computer and use it in GitHub Desktop.
Calculate CPU affinity for NGINX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
conf=/etc/nginx/workerprocesses.conf | |
cpu=$(awk '/^processor/{n=$3} END{print n}' /proc/cpuinfo 2> /dev/null || echo 0) | |
[[ ${2:-0} -gt 0 ]] && cpu=$(($2 - 1)) | |
workers=$1 | |
if [[ "$workers" = "" ]] || [[ ${workers:-0} -le 0 ]];then | |
workers=$(( cpu * 75 /100 )) | |
[[ ${workers:-0} -le 0 ]] && exit | |
fi | |
# cpu=30 | |
# [[ $cpu -eq 0 ]] && exit; | |
n=$cpu | |
if [ $workers -gt $cpu ];then | |
echo "# Number of workers $workers is great $n" | |
exit | |
fi | |
start=$((cpu - workers )) | |
calculate_workers(){ | |
echo "worker_processes $workers;" | |
printf "worker_cpu_affinity " | |
for a in $( seq $start $cpu ); do | |
poss=$((n - a)) | |
pose=$((n - poss)) | |
if [ $pose -eq 0 ]; then | |
printf "%0${poss}d1" 0 | |
elif [ $poss -eq 0 ]; then | |
[ $poss -eq 0 ] && printf "1%0${pose}d" 0 | |
else | |
[ $pose -ne 0 ] && printf "%0${poss}d1%0${pose}d" 0 0 | |
fi | |
[ $cpu != $a ] && echo -n " " || echo ";" | |
done | |
} | |
[[ -f "${conf}" ]] && calculate_workers > $conf || calculate_workers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment