Files
how-many-subshells/how-many-subshells.sh

55 lines
1.3 KiB
Bash
Raw Normal View History

2024-07-14 17:10:06 +08:00
#!/usr/bin/env bash
NPROC=$(grep -c ^processor /proc/cpuinfo)
RESULTS_FOLDER=$(mktemp --dir)
# Checking ulimit
if [[ $(ulimit) == "unlimited" ]];then
ULIMIT_YES="no"
while [[ $ULIMIT_YES == "no" ]]
do
read -p "ulimit is unlimited. This may not go well for the system. Are you sure? (yes/no): " ULIMIT_YES
if [[ $ULIMIT_YES == "yes" ]];then
break
elif [[ $ULIMIT_YES == "no" ]];then
exit
fi
done
fi
# Run the job with n number of nprocs
find ./jobs -type f -name "*.sh" | \
while read JOB_NAME
do
printf "Starting on job $JOB_NAME...\n"
SCALE=1
CURRENT=9998
BEFORE=9999
RESULT_FILE="$RESULTS_FOLDER/$(basename $JOB_NAME)"
2024-07-15 23:18:51 +08:00
#printf "Result file: $(readlink -f $RESULT_FILE)\n"
# Keep scaling up until the runtime is slower than the previous result
2024-07-14 17:10:06 +08:00
while [[ $(echo "$CURRENT < $BEFORE" | bc) -eq 1 ]]
do
BEFORE=$CURRENT
PARALLEL=$(( $NPROC * $SCALE ))
CURRENT=$( \
/bin/time --format "%e" \
bash -c "seq 1 10000000 | xargs -P $PARALLEL bash $JOB_NAME >/dev/null 2>&1" 2>&1 | \
tee -a $RESULT_FILE \
)
(( SCALE++ ))
done
# Display the results
2024-07-15 23:18:51 +08:00
printf "nproc\tseconds\n"
awk \
-v nproc="$NPROC" \
'{print NR*nproc, "\t", $0}' \
$RESULTS_FOLDER/*
2024-07-14 17:10:06 +08:00
done
2024-07-15 23:18:51 +08:00
# Clean up
2024-07-14 17:10:06 +08:00
rm -rf $RESULTS_FOLDER