uthread/graphs/generate_graphs.sh
2025-03-15 18:17:07 +01:00

257 lines
6.2 KiB
Bash

#!/usr/bin/bash
# CONSTANTS
US="Équipe n°2 - 26137"
ROOT_PATH=".."
INSTALLED_BINARIES_PATH="$ROOT_PATH/install/bin"
TMP_PATH="./tmp"
BINARIES_PATH="$TMP_PATH/own"
PTHREAD_BINARIES_PATH="$TMP_PATH/pthread"
GRAPHS_OUTPUT="./out"
MIN_LEVEL=20
MAX_LEVEL=80
NB_THREAD_START=20
NB_THREAD_STOP=200
NB_THREAD_PAS=10
NB_YIELD_START=10
NB_YIELD_STOP=100
NB_YIELD_PAS=20
NB_PASS=100
set -e
# CREATE BINS
mkdir -p "$PTHREAD_BINARIES_PATH"
make -C "$ROOT_PATH" clean > /dev/null
make -C "$ROOT_PATH" pthreads > /dev/null
make -C "$ROOT_PATH" install > /dev/null
cp "$INSTALLED_BINARIES_PATH"/* "$PTHREAD_BINARIES_PATH" > /dev/null
mkdir -p "$BINARIES_PATH"
make -C "$ROOT_PATH" clean > /dev/null
make -C "$ROOT_PATH" > /dev/null
make -C "$ROOT_PATH" install > /dev/null
cp "$INSTALLED_BINARIES_PATH"/* "$BINARIES_PATH" > /dev/null
# FUNCTIONS
execute_test() {
bin=$1
shift 1
# shellcheck disable=SC2046
value="$($bin $(echo "$@" | xargs) 2>/dev/null | grep 'GRAPH' | sed 's/;SUCCESS//' | awk -F';' '{ print $NF }')"
if [ -z "$value" ] || [ "$value" = "0" ]
then
return 1
fi
echo "$value"
return 0
}
# 51-fibonacci
number_argument() {
for thread in $(seq 5 15)
do
time_own=0
time_pthread=0
for _ in $(seq 1 $NB_PASS)
do
value=$(execute_test "$1" "$thread")
result=$?
if [ ! $result = 0 ]
then
return $result
fi
value=$(printf "%.7f" "$value")
executed=$(bc -l <<< "scale=0;$value*100000/1")
time_own=$((time_own+executed))
value=$(execute_test "$2" "$thread")
result=$?
if [ ! $result = 0 ]
then
return $result
fi
value=$(printf "%.7f" "$value")
executed=$(bc -l <<< "scale=0;$value*100000/1")
time_pthread=$((time_pthread+executed))
done
time_pthread=$((time_pthread/NB_PASS))
echo "$thread, $time_own, $time_pthread"
done
return 0
}
# 21-create-many, 22-create-many-recursive, 23-create-many-once, 61-mutex, 62-mutex
only_thread_argument() {
for thread in $(seq $NB_THREAD_START $NB_THREAD_PAS $NB_THREAD_STOP)
do
time_own=0
time_pthread=0
for _ in $(seq 1 $NB_PASS)
do
executed=$(execute_test "$1" "$thread")
result=$?
if [ ! $result = 0 ]
then
return $result
fi
time_own=$time_own+$(("$executed"))
executed=$(execute_test "$2" "$thread")
result=$?
if [ ! $result = 0 ]
then
return $result
fi
time_pthread=$time_pthread+$(("$executed"))
done
time_own=$((time_own/NB_PASS))
time_pthread=$((time_pthread/NB_PASS))
echo "$thread, $time_own, $time_pthread"
done
return 0
}
# 31-switch-many, 32-switch-many-join, 33-switch-many-cascade
thread_yield_argument() {
for thread in $(seq $NB_THREAD_START $NB_THREAD_PAS $NB_THREAD_STOP)
do
for yield in $(seq $NB_YIELD_START $NB_YIELD_PAS $NB_YIELD_STOP)
do
time_own=0
time_pthread=0
for _ in $(seq 1 $NB_PASS)
do
executed=$(execute_test "$1" "$thread" "$yield")
result=$?
if [ ! $result = 0 ]
then
return $result
fi
time_own=$time_own+$(("$executed"))
executed=$(execute_test "$2" "$thread" "$yield")
result=$?
if [ ! $result = 0 ]
then
return $result
fi
time_pthread=$time_pthread+$(("$executed"))
done
time_own=$((time_own/NB_PASS))
time_pthread=$((time_pthread/NB_PASS))
echo "$thread, $yield, $time_own, $time_pthread"
done
done
return 0
}
# VERIFICATIONS
if ! which gnuplot 2>&1 >/dev/null
then
echo 'Please install gnuplot.'
exit 1
fi
if ! which bc 2>&1 >/dev/null
then
echo 'Please install bc.'
exit 1
fi
if [ ! -d "$BINARIES_PATH" ] || [ "$(find "$BINARIES_PATH" | wc -c)" = "0" ]
then
echo 'Binaries are not in `'"$BINARIES_PATH"'`.'
exit 2
fi
# LOGIC
mkdir -p "$GRAPHS_OUTPUT"
#for bin in $(find "$BINARIES_PATH" -type f | sort)
for bin in "$BINARIES_PATH"/62-mutex
do
name=$(echo "$bin" | awk -F'/' '{ print $NF }')
graph_filename="$GRAPHS_OUTPUT/$name.svg"
level=$(("$(echo "$name" | awk -F'-' '{ print $1 }')"))
if [ $MIN_LEVEL -gt $level ] || [ $MAX_LEVEL -lt $level ]
then
continue
fi
echo '[ ] Testing '"$name"'...'
if [ ! -x "$bin" ]
then
echo ' '"$name"' is not executable.'
chmod +x "$bin"
fi
echo -n '' > $TMP_PATH/data
SECONDS=0
gnu_commands="
set term svg;
set grid;
set title \"Comparaison des temps d'execution de $name\";"
case "$($bin)" in
"argument manquant: entier x pour lequel calculer fibonacci(x)")
if ! number_argument "$bin" "$PTHREAD_BINARIES_PATH/$name" >> $TMP_PATH/data
then
echo '[X] Command failed ('"$?"').'
continue;
fi
gnu_commands="$gnu_commands
set xlabel 'Nombre utilisé';
set ylabel 'Temps (µs)';
set output '$graph_filename';
plot '$TMP_PATH/data' using 1:2 with lines title '$US', '$TMP_PATH/data' using 1:3 with lines title 'Pthread';
"
;;
"argument manquant: nombre de threads")
if ! only_thread_argument "$bin" "$PTHREAD_BINARIES_PATH/$name" >> $TMP_PATH/data
then
echo '[X] Command failed ('"$?"').'
continue;
fi
gnu_commands="$gnu_commands
set xlabel 'Nombre de threads';
set ylabel 'Temps (µs)';
set output '$graph_filename';
plot '$TMP_PATH/data' using 1:2 with lines title '$US', '$TMP_PATH/data' using 1:3 with lines title 'Pthread';
"
;;
"arguments manquants: nombre de threads, puis nombre de yield")
if ! thread_yield_argument "$bin" "$PTHREAD_BINARIES_PATH/$name" >> $TMP_PATH/data
then
echo '[X] Command failed ('"$?"').'
continue;
fi
gnu_commands="$gnu_commands
set xlabel 'Nombre de threads';
set ylabel 'Nombre de yields';
set zlabel 'Temps (µs)';
set output '$graph_filename';
splot '$TMP_PATH/data' using 1:2:3 title '$US', '$TMP_PATH/data' using 1:2:4 title 'Pthread';
"
;;
*)
echo '[X] Can not determine which arguments are needed.'
continue
;;
esac
echo "[-] Execution finished. ($((SECONDS / 60))m$((SECONDS % 60))s)"
SECONDS=0
echo "$gnu_commands" | gnuplot
echo "[+] Plot generated at \`""$graph_filename""\`. ($((SECONDS / 60))m$((SECONDS % 60))s)"
done
rm -rf "$TMP_PATH"