Siddhesh Poyarekar 67b30d
#!/usr/bin/bash
Siddhesh Poyarekar 67b30d
# This script can be invoked as follows:
Siddhesh Poyarekar 67b30d
#
Siddhesh Poyarekar 67b30d
# glibc-bench-compare [options] <build> [BUILD]
Siddhesh Poyarekar 67b30d
#
Siddhesh Poyarekar 67b30d
# Options may be one of the following:
Siddhesh Poyarekar 67b30d
#
Siddhesh Poyarekar 67b30d
# -t		The BUILD arguments are task ids and not a version-release string
Siddhesh Poyarekar 67b30d
# -a ARCH	Do comparison for ARCH architecture
Siddhesh Poyarekar 67b30d
#
Siddhesh Poyarekar 67b30d
# If any of the above options are given, both BUILD arguments must be given.
Siddhesh Poyarekar 67b30d
# Otherwise, if only one BUILD is specified, then it is compared against the
Siddhesh Poyarekar 67b30d
# installed glibc.
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
# Silence the pushd/popd messages
Siddhesh Poyarekar 67b30d
pushd() {
Siddhesh Poyarekar 67b30d
	command pushd "$@" > /dev/null 2>&1
Siddhesh Poyarekar 67b30d
}
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
popd() {
Siddhesh Poyarekar 67b30d
	command popd "$@" > /dev/null 2>&1
Siddhesh Poyarekar 67b30d
}
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
# Clean up any downloaded files before we exit
Siddhesh Poyarekar 67b30d
trap "rm -rf /tmp/glibc-bench-compare.$BASHPID.*" EXIT
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
task=0
Siddhesh Poyarekar 67b30d
arch=$(uname -i)
Siddhesh Poyarekar 67b30d
options=0
Siddhesh Poyarekar 67b30d
path=0
Siddhesh Poyarekar 67b30d
installed=
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
# Look for any commandline options
Siddhesh Poyarekar 67b30d
while getopts ":tpa:" opt; do
Siddhesh Poyarekar 67b30d
	case $opt in
Siddhesh Poyarekar 67b30d
		p)
Siddhesh Poyarekar 67b30d
		path=1
Siddhesh Poyarekar 67b30d
		;;
Siddhesh Poyarekar 67b30d
		t)
Siddhesh Poyarekar 67b30d
		task=1
Siddhesh Poyarekar 67b30d
		options=1
Siddhesh Poyarekar 67b30d
		echo "Not implemented."
Siddhesh Poyarekar 67b30d
		exit 1
Siddhesh Poyarekar 67b30d
		;;
Siddhesh Poyarekar 67b30d
		a)
Siddhesh Poyarekar 67b30d
		arch=$OPTARG
Siddhesh Poyarekar 67b30d
		options=1
Siddhesh Poyarekar 67b30d
		;;
Siddhesh Poyarekar 67b30d
		*)
Siddhesh Poyarekar 67b30d
		;;
Siddhesh Poyarekar 67b30d
	esac
Siddhesh Poyarekar 67b30d
done
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
# Done, now shift all option arguments out.
Siddhesh Poyarekar 67b30d
shift $((OPTIND-1))
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
if [ $# -gt 2 ] || [ $# -eq 0 ] || [ $# -lt 2 -a $options -eq 1 ]; then
Siddhesh Poyarekar 67b30d
	echo "Usage: $0 [OPTIONS] <old> [new]"
Siddhesh Poyarekar 67b30d
	echo
Siddhesh Poyarekar 67b30d
	echo "OPTIONS:"
Siddhesh Poyarekar 67b30d
	echo -e "\t-t\tCompare two brew tasks"
Siddhesh Poyarekar 67b30d
	echo -e "\t-a ARCH\tGet rpms for the ARCH architecture"
Siddhesh Poyarekar 67b30d
	echo -e "\t-p\tCompare built rpms in two paths."
Siddhesh Poyarekar 67b30d
	echo -e "\t\tThis minimally needs glibc, glibc-common and glibc-benchtests"
Siddhesh Poyarekar 67b30d
	exit 1
Siddhesh Poyarekar 67b30d
fi
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
if [ -z $2 ]; then
Siddhesh Poyarekar 67b30d
	new="$1"
Siddhesh Poyarekar 67b30d
	old=$(rpm --queryformat "%{VERSION}-%{RELEASE}\n" -q glibc | head -1)
Siddhesh Poyarekar 67b30d
	installed=$old
Siddhesh Poyarekar 67b30d
else
Siddhesh Poyarekar 67b30d
	new="$2"
Siddhesh Poyarekar 67b30d
	old="$1"
Siddhesh Poyarekar 67b30d
fi
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
decompress_rpms() {
Siddhesh Poyarekar 67b30d
	# We were given a path to the rpms.  Figure out the version-release and
Siddhesh Poyarekar 67b30d
	# decompress the rpms.
Siddhesh Poyarekar 67b30d
	if [ -n $1 ]; then
Siddhesh Poyarekar 67b30d
		vr=$(rpm --queryformat="%{VERSION}-%{RELEASE}" -qp $1/glibc-2*.rpm | head -1)
Siddhesh Poyarekar 67b30d
		mkdir $vr && pushd $vr
Siddhesh Poyarekar 67b30d
	fi
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
	for r in $1*.rpm; do
Siddhesh Poyarekar 67b30d
		( rpm2cpio $r | cpio -di ) > /dev/null
Siddhesh Poyarekar 67b30d
	done
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
	if [ -n $1 ]; then
Siddhesh Poyarekar 67b30d
		popd
Siddhesh Poyarekar 67b30d
		echo $vr
Siddhesh Poyarekar 67b30d
	fi
Siddhesh Poyarekar 67b30d
}
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
# Get rpms for a build and decompress them
Siddhesh Poyarekar 67b30d
get_build() {
Siddhesh Poyarekar 67b30d
	echo "Processing build $1"
Siddhesh Poyarekar 67b30d
	mkdir $1 && pushd $1
Siddhesh Poyarekar 67b30d
	brew buildinfo "glibc-$1" |
Siddhesh Poyarekar 67b30d
	sed -n -e "s|/mnt/koji\(.\+$arch.\+\)|http://kojipkgs.fedoraproject.org\1|p" |
Siddhesh Poyarekar 67b30d
	while read url; do
Siddhesh Poyarekar 67b30d
		echo "Downloading $url"
Siddhesh Poyarekar 67b30d
		wget -q $url
Siddhesh Poyarekar 67b30d
	done
Siddhesh Poyarekar 67b30d
	decompress_rpms
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
	echo "Removing rpms"
Siddhesh Poyarekar 67b30d
	rm -f $1/*.rpm
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
	popd
Siddhesh Poyarekar 67b30d
}
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
# Run benchmarks for a build
Siddhesh Poyarekar 67b30d
run_bench() {
Siddhesh Poyarekar 67b30d
	if [ -z $1 ]; then
Siddhesh Poyarekar 67b30d
		make DETAILED=1 ver=$installed prefix= -f /usr/libexec/glibc-benchtests/bench.mk bench
Siddhesh Poyarekar 67b30d
	else
Siddhesh Poyarekar 67b30d
		make DETAILED=1 ver=$1 prefix=$PWD -f $1/usr/libexec/glibc-benchtests/bench.mk bench
Siddhesh Poyarekar 67b30d
	fi
Siddhesh Poyarekar 67b30d
}
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
# Get absolute paths if needed, since we will change into the working directory
Siddhesh Poyarekar 67b30d
# next.
Siddhesh Poyarekar 67b30d
if [ $path -eq 1 ]; then
Siddhesh Poyarekar 67b30d
	old_path=$(realpath $old)/
Siddhesh Poyarekar 67b30d
	new_path=$(realpath $new)/
Siddhesh Poyarekar 67b30d
fi
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
tmpdir=$(mktemp -p /tmp -d glibc-bench-compare.$$.XXXX)
Siddhesh Poyarekar 67b30d
pushd $tmpdir
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
# Get both builds.
Siddhesh Poyarekar 67b30d
if [ $path -eq 0 ]; then
Siddhesh Poyarekar 67b30d
	if [ -z $installed ]; then
Siddhesh Poyarekar 67b30d
		get_build $old
Siddhesh Poyarekar 67b30d
	fi
Siddhesh Poyarekar 67b30d
	get_build $new
Siddhesh Poyarekar 67b30d
else
Siddhesh Poyarekar 67b30d
	old=$(decompress_rpms $old_path)
Siddhesh Poyarekar 67b30d
	new=$(decompress_rpms $new_path)
Siddhesh Poyarekar 67b30d
fi
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
# make bench for each of those.
Siddhesh Poyarekar 67b30d
if [ -z $installed ]; then
Siddhesh Poyarekar 67b30d
	run_bench $old
Siddhesh Poyarekar 67b30d
else
Siddhesh Poyarekar 67b30d
	run_bench
Siddhesh Poyarekar 67b30d
fi
Siddhesh Poyarekar 67b30d
run_bench $new
Siddhesh Poyarekar 67b30d
Siddhesh Poyarekar 67b30d
# Now run the comparison script.
Siddhesh Poyarekar 67b30d
$old/usr/libexec/glibc-benchtests/compare_bench.py $old/usr/libexec/glibc-benchtests/benchout.schema.json \
Siddhesh Poyarekar 67b30d
	bench.$old.out bench.$new.out