#!/bin/sh

# Configure parameters for lmbench.
# 1.23 98/07/31 lm@lm.bitmover.com

OS=`/usr/pkg/libexec/lmbench/os`
L='====================================================================='
echo $L; 
cat<<EOF;

		L M B E N C H   C ON F I G U R A T I O N
		----------------------------------------

You need to configure some parameters to lmbench.  Once you have configured
these parameters, you may do multiple runs by saying

	"make rerun"

in the src subdirectory.

NOTICE: please do not have any other activity on the system if you can
help it.  Things like the second hand on your xclock or X perfmeters
are not so good when benchmarking.  In fact, X is not so good when
benchmarking.

EOF

# Figure out echo.
if [ `echo -n "foo" | wc -l` -eq 0 ]
then	ECHON="-n"; ECHOC=
else	ECHON= ; ECHOC='\c'
fi

############################################################################
# Timing granulairty, loop overhead, etc.
############################################################################
echo $L; echo "";
echo "Hang on, we are calculating your timing granularity."
/usr/pkg/libexec/lmbench/msleep 250
ENOUGH=`/usr/pkg/libexec/lmbench/enough`
export ENOUGH 
echo "OK, it looks like you can time stuff down to $ENOUGH usec resolution."
echo ""
echo "Hang on, we are calculating your timing overhead."
/usr/pkg/libexec/lmbench/msleep 250
TIMING_O=`/usr/pkg/libexec/lmbench/timing_o`
export TIMING_O
echo "OK, it looks like your gettimeofday() costs $TIMING_O usecs."
echo ""
echo "Hang on, we are calculating your loop overhead."
/usr/pkg/libexec/lmbench/msleep 250
LOOP_O=`/usr/pkg/libexec/lmbench/loop_o`
export LOOP_O
echo "OK, it looks like your benchmark loop costs $LOOP_O usecs."
echo ""

############################################################################
# Figure out memory size.
############################################################################
if [ -r /proc/meminfo ]
then	TMP=`grep 'Mem:' /proc/meminfo | awk '{print $2}'`
	if [ X$TMP != X ]
	then	TMP=`expr $TMP / 1048576`
		TMP=`expr $TMP \* 7`
		TMP=`expr $TMP / 10`
	fi
	if [ X$TMP != X ]
	then	MB=$TMP
	fi
else	MB=32
fi

echo $L
cat<<EOF;

Several benchmarks operate on a range of memory.  This memory should be
sized such that it is at least 4 times as big as the external cache[s]
on your system.   It should be no more than 80% of your physical memory.

The bigger the range, the more accurate the results, but larger sizes
take somewhat longer to run the benchmark.

EOF
echo $ECHON "MB [default $MB] $ECHOC"
read TMP
if [ X$TMP != X ]
then	MB=$TMP
fi
# Certain machines tend to barf when you try and bcopy 8MB.
# Figure out how much we can use.
echo "Checking to see if you have $MB MB; please wait for a moment..."
AVAILMEM=`/usr/pkg/libexec/lmbench/memsize $MB`
AVAILMEM=`/usr/pkg/libexec/lmbench/memsize $MB`
AVAILMEM=`/usr/pkg/libexec/lmbench/memsize $MB`
if [ $AVAILMEM -lt 8 ]
then    echo $0 aborted: Not enough memory, only ${AVAILMEM}MB available. 
	exit 1
fi
if [ $AVAILMEM -lt 16 ]
then	echo Warning: you have only ${AVAILMEM}MB available memory. 
	echo Some benchmark results will be less meaningful. 
fi
MB=`expr $AVAILMEM / 2`

############################################################################
# Memory strides for lat_mem
############################################################################
echo $L
cat<<EOF;

This benchmark measures, by default, memory latency for a number of
different strides.  That can take a long time and is most useful if you
are trying to figure out your cache line size or if your cache line size
is greater than 128 bytes.

If you are planning on sending in these results, please don't do a fast
run.

If you are measuring software only, you probably want to do a fast run,
so answer yes below.  Answering yes means that we measure memory latency
with a 128 byte stride.  

EOF

echo $ECHON "FASTMEM [default no] $ECHOC"
read fast
case "$fast" in
    [yY]*) FASTMEM=YES;;
    *)	   FASTMEM=NO;;
esac

############################################################################
# File system latency
############################################################################
echo $L
cat<<EOF;

This benchmark measures, by default, file system latency.  That can
take a long time on systems with old style file systems (i.e., UFS,
FFS, etc.).  Linux' ext2fs and Sun's tmpfs are fast enough that this
test is not painful.

If you are planning on sending in these results, please don't do a fast
run.

If you want to skip the file system latency tests, answer "yes" below.

EOF

echo $ECHON "SLOWFS [default no] $ECHOC"
read slow
case "$slow" in
    [yY]*) SLOWFS=YES;;
    *)	   SLOWFS=NO;;
esac

############################################################################
# Disk bandwidth/seek times
############################################################################
echo $L
cat<<EOF;

This benchmark can measure disk zone bandwidths and seek times.  These can
be turned into whizzy graphs that pretty much tell you everything you might
need to know about the performance of your disk.  

This takes a while and requires read access to a disk drive.  
Write is not measured, see disk.c to see how if you want to do so.

If you want to skip the disk tests, hit return below.

If you want to include disk tests, then specify the path to the disk
device, such as /dev/sda.  For each disk that is readable, you'll be
prompted for a one line description of the drive, i.e., 

	Iomega IDE ZIP
or
	HP C3725S 2GB on 10MB/sec NCR SCSI bus

EOF

echo $ECHON "DISKS [default none] $ECHOC"
read disks
if [ X"$disks" != X ]
then	
	for i in $disks
	do	if [ -r $i ]
		then	/usr/pkg/libexec/lmbench/flushdisk $i
			if [ $? -eq 1 ]
			then	echo "Must be root to run disk benchmarks."
				echo "Root is needed to flush the buffer cache"
				exit 1
			fi
			echo $ECHON "$i is a $ECHOC"
			read x
			DISK_DESC="$DISK_DESC[${i}:${x}] "
			DISKS="$DISKS${i} "
		else	echo "Can't read $i, skipping it."
		fi
	done
fi

############################################################################
# Processor speed
############################################################################
echo $L
echo ""
echo "Calculating mhz, please wait for a moment..."
MHZ=`/usr/pkg/libexec/lmbench/mhz`
cat > /dev/tty <<EOF
I think your CPU mhz is 

	$MHZ
	
but I am frequently wrong.  If that is the wrong Mhz, type in your
best guess as to your processor speed.  It doesn't have to be exact,
but if you know it is around 100, say 100.  

Type in just the Mhz part, like 200 or 500 for you lucky Alpha owners.

EOF
echo $ECHON "Processor mhz [default $MHZ] $ECHOC"
read mhz
if [ -n "$mhz" ]
then	MHZ=$mhz
fi


############################################################################
# /usr/tmp?
############################################################################
echo $L
AGAIN=Y
while [ $AGAIN = Y ]
do
	cat<<EOF;

We need a place to store a $MB Mbyte file as well as create and delete a
large number of small files.  We default to /usr/tmp.  If /usr/tmp is a
memory resident file system (i.e., tmpfs), pick a different place.
Please specify a directory that has enough space and is a local file
system.

EOF
	echo $ECHON "FSDIR [default /usr/tmp] $ECHOC"
	read FSDIR
	if [ X$FSDIR = X ]
	then	FSDIR=/usr/tmp
	else	mkdir -p $FSDIR 2>/dev/null
	fi
	if [ -d $FSDIR -a -w $FSDIR ]
	then	AGAIN=N
		FILE=$FSDIR/XXX
	else	echo $FSDIR is not a directory or is not writable
	fi
done

############################################################################
# Remote networking
############################################################################
echo $L
cat<<EOF;

If you are running on an idle network and there are other, identically
configured systems, on the same wire (no gateway between you and them),
and you have rsh access to them, then you should run the network part
of the benchmarks to them.  Please specify any such systems as a space
separated list such as: ether-host fddi-host hippi-host.

EOF
echo $ECHON "REMOTE [default none] $ECHOC"
read REMOTE

if [ "X$REMOTE" != X ]
then	cat<<EOF;

Thanks for doing remote testing, that is a hard thing to get.  Could you
do me one more favor and tell me the networking you think will be used
to get to each of the remote hosts.  By networking I mean one of the
following (or whatever you use if you use something else):

ethernet		aka 10baseT, thinnet, thicknet, etc
ethernet-100		aka 100baseT, 100VG
fddi			aka cddi
hippi
others?

Please type it just like the above if you can, it makes parsing easier.

EOF

	RSH=rsh
	case `uname -s` in
		*HP-UX*)	RSH=remsh;;
	esac
	for r in $REMOTE
	do	echo $ECHON "Network type for $r: $ECHOC"
		read n
		X=`$RSH $r echo foo`
		if [ X$X = Xfoo ]
		then	echo Remote access to $r worked, thanks.
		else	echo Remote access to $r did not work, please check and retry,
			exit 1
		fi
		NETWORKS="${NETWORKS}[ $r:$n ]"
	done
fi

############################################################################
# Multiple copies
############################################################################
echo $L
cat<<EOF;

If you are running on an MP machine and you want to try running
multiple copies of lmbench in parallel, you can specify how many here.

Using this option will make the benchmark run 100x slower (sorry).

This is experimental and results are undefined for the time being.

EOF
AGAIN=Y
while [ $AGAIN = Y ]
do	echo $ECHON "MULTIPLE COPIES [default 1] $ECHOC"
	read SYNC_MAX
	if [ "X$SYNC_MAX" != X ]
	then	case "$SYNC_MAX" in
		[0-9]|[0-9][0-9]|[0-9][0-9][0-9])
			AGAIN=N
			;;
		*)	echo "Please enter a number between 1 and 999"
			;;
		esac
	else	AGAIN=N
		SYNC_MAX=1
	fi
done

############################################################################
# Submit results?
############################################################################
echo $L
cat<<EOF;

There is a database of benchmark results that is shipped with new
releases of lmbench.  Your results can be included in the database
if you wish.  The more results the better, especially if they include
remote networking.  If your results are interesting, i.e., for a new
fast box, they may be made available on the lmbench web page, which is

	http://www.bitmover.com/lmbench

EOF

echo $ECHON "Mail results [default yes] $ECHOC"
read MAIL
case $MAIL in 
    [Nn]*)	MAIL=no
		echo OK, no results mailed.
		;;
    *)		MAIL=yes
		;;
esac

INFO=`/usr/pkg/libexec/lmbench/info`
if [ $MAIL = yes ]
then	if [ ! -f $INFO ]
	then	cp /usr/pkg/libexec/lmbench/info-template /usr/pkg/share/lmbench/$INFO
		chmod +w /usr/pkg/share/lmbench/$INFO
		REUSE=no
	else	
		REUSE=view
		while [ $REUSE = view ]
		do	echo ""
			echo $ECHON \
"Reuse previous description [default yes, other options: no|view] $ECHOC"
			read REUSE
			case $REUSE in 
	    		[Nn]*)	REUSE=no
				;;
			[Vv]*)	REUSE=view
				echo $L
				more /usr/pkg/share/lmbench/$INFO
				echo $L
				;;
	    		*)	REUSE=yes
				;;
			esac
		done
	fi

	if [ $REUSE = no ]
	then	EDITOR=vi
		echo $L
		cat<<EOF;

Please tell us about your machine.   There is a form we would like you
to fill out that we will make available with the results.  If you would
prefer to use a different editor, tell us the editor at the prompt.

If you want to skip filling out this form (please don't) then answer 
"none" at the prompt.

EOF
		echo $ECHON "Editor [default $EDITOR] $ECHOC"
		read TMP
		if [ X$TMP != X ]
		then	EDITOR=$TMP
		fi
		if [ X$EDITOR != "none" ]
		then	$EDITOR /usr/pkg/share/lmbench/`/usr/pkg/share/lmbench/info`
		fi
	fi
fi

echo $L
echo ""
echo "Confguration done, thanks."
cat <<EOF

There is a mailing list for discussing lmbench hosted at BitMover. 
Send mail to majordomo@bitmover.com to join the list.

EOF

VERSION=`/usr/pkg/libexec/lmbench/version`
CONFIG=/usr/pkg/share/lmbench/`/usr/pkg/libexec/lmbench/config`

cat <<EOF >$CONFIG
DISKS="$DISKS"
DISK_DESC="$DISK_DESC"
ENOUGH=$ENOUGH
FASTMEM="$FASTMEM"
FILE=$FILE
FSDIR=$FSDIR
INFO=$INFO
LOOP_O=$LOOP_O
MAIL=$MAIL
MB=$MB
MHZ="$MHZ"
MOTHERBOARD="$MOTHERBOARD"
NETWORKS="$NETWORKS"
PROCESSORS="$PROCESSORS"
REMOTE="$REMOTE"
SLOWFS="$SLOWFS"
SYNC_MAX="$SYNC_MAX"
TIMING_O=$TIMING_O
VERSION=$VERSION
EOF

exit 0
