#! /bin/sh

KERNEL_VERSION="linux-3.2"
KERNEL_URL_PATH="www.kernel.org/pub/linux/kernel/v3.0/"
RESULT_FILE="compilation-timing.txt"

if [ "$#" -eq 0 ]
then
	echo "usage: $@ jobs_number..." >& 2
	exit 0
fi


if [ ! -d "${KERNEL_VERSION}" ]
then
	if [ ! -f "${KERNEL_VERSION}.tar.bz2" ]
	then
		wget "${KERNEL_URL_PATH}/${KERNEL_VERSION}.tar.bz2"
		if [ $? -ne 0 ] || [ ! -f "${KERNEL_VERSION}.tar.bz2" ]
		then
			echo "unable to obtain ${KERNEL_VERSION} archive" >&2
			exit 1
		fi
	fi
	tar xjf "${KERNEL_VERSION}.tar.bz2"
	if [ $? -ne 0 ]
	then
		echo "Error while uncompressing kernel archive" >&2
		exit 1
	fi
fi

cd "${KERNEL_VERSION}"

echo "# Timings of ${KERNEL_VERSION} compilations" >> "${RESULT_FILE}"
nb_cpu=$(grep "^processor" /proc/cpuinfo | wc -l)
echo "# Processors: ${nb_cpu}" >> "${RESULT_FILE}"
affinity=$(taskset -p $$ | sed -e 's/^.*://') >> "${RESULT_FILE}"
echo "# Affinity mask: ${affinity}" >> "${RESULT_FILE}"
for nb in "$@"
do
	echo "# Compiling with $nb simultaneous jobs" >> "${RESULT_FILE}"
	make mrproper 
	make i386_defconfig
	sync
	sleep 10 # Let's all calm down
	start=$(date "+%s")
	make -j $nb
	sync 
	end=$(date "+%s")
	# This script will fail during february 2038 ;-)
	echo "$nb     $((end - start))" >> "${RESULT_FILE}"
done



