#!/bin/sh

# lmbench - run the lmbench benchmark suite.
#
# Hacked into existence by Larry McVoy (lm@sun.com now lm@sgi.com).
# Copyright (c) 1994 Larry McVoy.  GPLed software.
# $Id: lmbench,v 1.17 1995/11/07 03:13:41 lm Exp $

# Make sure we can find: ./cmd, df, and netstat
PATH=.:$PATH:/etc:/usr/etc:/sbin:/usr/sbin
export PATH

if [ -f CONFIG ]
then	echo Using config in CONFIG
	. CONFIG
else	echo Using defaults > /dev/tty
fi

if [ X$FILE = X ]
then	FILE=/tmp/XXX
	touch $FILE || echo Can not create $FILE
fi
if [ X$MB = X ]
then	MB=8
fi
AVAILKB=`expr $MB \* 1024`

if [ X$FSDIR = X ]
then	FSDIR=/usr/tmp/lat_fs
fi

if [ X`../../scripts/os` = Xhpux ]
then	RSH=remsh
else	RSH=rsh
fi

# Figure out as much stuff as we can about this system.
# Sure would be nice if everyone had SGI's "hinv".
echo \[lmbench1.1 results for `uname -a`] 1>&2
echo \[DISK: ${DISK}] 1>&2
echo \[FILE: ${FILE}] 1>&2
echo \[FSDIR: ${FSDIR}] 1>&2
echo \[REMOTE: ${REMOTE}] 1>&2
echo \[NETWORKS: ${NETWORKS}] 1>&2
echo \[MB: ${MB}] 1>&2
echo \[`date`] 1>&2
echo \[`uptime`] 1>&2

netstat -i | while read i
do	echo \[net: "$i"] 1>&2
	set `echo $i`
	case $1 in
	    *ame)	;;
	    *)		ifconfig $1 | while read i
			do echo \[if: "$i"] 1>&2
			done
			;;
	esac
done

mount -p | while read i
do	echo \[mount: "$i"] 1>&2
done

# In case mount -p doesn't work.
df | while read i
do	echo \[df: "$i"] 1>&2
done

echo Calculating processor speed > /dev/tty
mhz 1>&2

echo Calculating system call overhead > /dev/tty
lat_syscall

echo Calculating signal handler overhead > /dev/tty
lat_sig

echo Calculating pipe latency > /dev/tty
lat_pipe

SERVERS="lat_udp lat_tcp lat_rpc lat_connect bw_tcp"

for server in $SERVERS; do $server -s; done
sleep 2;
for i in localhost
do
	echo Calculating local UDP latency > /dev/tty
	lat_udp $i; lat_udp -$i;
	echo Calculating local TCP latency > /dev/tty
	lat_tcp $i; lat_tcp -$i;
	echo Calculating local RPC latency > /dev/tty
	lat_rpc $i udp; lat_rpc $i tcp; lat_rpc -$i;
	echo Calculating local connect latency > /dev/tty
	lat_connect $i; lat_connect -$i;
	echo Calculating local TCP bandwidth > /dev/tty
	bw_tcp $i 20m; bw_tcp -$i 
done

for remote in $REMOTE 
do	rcp ../../scripts/os ${remote}:/tmp/os
	OS=`$RSH $remote -n /tmp/os`
	( cd ../$OS && rcp $SERVERS ${remote}:/tmp )
	for server in $SERVERS
	do	$RSH $remote -n /tmp/$server -s &
	done
	sleep 2
	echo "[ Networking remote to $remote: `$RSH $remote uname -a` ]" 1>&2
	echo Calculating remote host $remote UDP latency > /dev/tty
	lat_udp $remote; lat_udp -$remote;
	echo Calculating remote host $remote TCP latency > /dev/tty
	lat_tcp $remote; lat_tcp -$remote;
	echo Calculating remote host $remote RPC latency > /dev/tty
	lat_rpc $remote udp; lat_rpc $remote tcp; lat_rpc -$remote;
	echo Calculating remote host $remote connect latency > /dev/tty
	lat_connect $remote; lat_connect -$remote;
	echo Calculating remote host $remote TCP bandwidth > /dev/tty
	bw_tcp $remote 20m; bw_tcp -$remote 
	RM=/tmp/os
	for server in $SERVERS
	do	RM="/tmp/$server $RM"
	done
	$RSH $remote rm $RM
done

if [ -c "$DISK" -a -r "$DISK" ]
then	 echo Calculating disk latency > /dev/tty
	lmdd label="Disk $DISK latency" if=$DISK bs=512 count=2000 print=2
fi

echo Calculating processes overhead > /dev/tty
cp hello /tmp/hello
lat_proc
rm /tmp/hello 

echo Calculating pipe bandwidth > /dev/tty
bw_pipe

if [ -c "$DISK" -a -r "$DISK" ]
then	echo Calculating raw disk bandwidth > /dev/tty
	lmdd label="Disk $DISK bandwidth" if=$DISK bs=56k count=100 print=3
fi

echo Calculating $FILE file write bandwidth > /dev/tty
rm -f $FILE
lmdd label="File $FILE write bandwidth: " of=$FILE move=${MB}m fsync=1 print=3

echo Calculating pagefault latency > /dev/tty
lat_pagefault $FILE

echo Calculating mmap latency > /dev/tty
echo \"mappings 1>&2
for i in 8 256 512 1024 2048 3072 4096 8192 16384 32768 65536
do	if [ $AVAILKB -ge $i ]
	then	lat_mmap ${i}k $FILE
	fi
done
echo "" 1>&2

echo Calculating file reread bandwidth > /dev/tty
echo \"read bandwidth 1>&2
for i in 16 32 64 128 192 256 384 512 \
	768 1024 1536 2048 2560 3072 3584 4096 \
	5120 6144 7168 8192 16384 32768 65536
do	if [ $AVAILKB -ge $i ]
	then	bw_file_rd ${i}k $FILE	2>/dev/null
		bw_file_rd ${i}k $FILE
	fi
done
echo "" 1>&2

echo Calculating file reread via mmap bandwidth > /dev/tty
echo \"Mmap read bandwidth 1>&2
for i in 16 32 64 128 192 256 384 512 \
	768 1024 1536 2048 2560 3072 3584 4096 \
	5120 6144 7168 8192 16384 32768 65536
do	if [ $AVAILKB -ge $i ]
	then	bw_mmap_rd ${i}k $FILE 2>/dev/null
		bw_mmap_rd ${i}k $FILE
	fi
done
echo "" 1>&2

rm -f $FILE

HALF=`expr $AVAILKB / 2`
echo Calculating bcopy bandwidth > /dev/tty
for type in libc unrolled
do	for align in aligned unaligned
	do	echo \"$type bcopy $align 1>&2
		for i in 64 128 512 1024 2048 4096 8192 16384 32768 65536
		do	if [ $HALF -ge $i ]
			then	bw_mem_cp ${i}k $type $align	# XXX?
			fi
		done
		echo "" 1>&2
	done
done

echo Calculating memory read bandwidth > /dev/tty
echo "Memory read bandwidth" 1>&2
for i in 1 2 4 8 16 32 64
do	if [ $MB -ge $i ]
	then	bw_mem_rd ${i}m
	fi
done
echo "" 1>&2

echo Calculating memory write bandwidth > /dev/tty
echo "Memory write bandwidth" 1>&2
for i in 1 2 4 8 16 32 64
do	if [ $MB -ge $i ]
	then	bw_mem_wr ${i}m
	fi
done
echo "" 1>&2

echo Calculating file system latency "(this can take a while)" > /dev/tty
echo '"File system latency' 1>&2
lat_fs $FSDIR
echo "" 1>&2

echo Calculating context switch overhead "(this can take 5-25 minutes)" > /dev/tty
for size in 0 4 16 32 64
do	echo "    Calculating ctx switch for ${size}KB sized processes" > /dev/tty
	lat_ctx -s $size 2 4 8 16 20
done

echo Calculating memory load latency "(this takes ~20 minutes)" > /dev/tty
echo "Memory load latency" 1>&2
LIST="8 16 32 64 128 512 1024 2048 4096"
for i in 8192 16384 32768 65536
do	if [ $AVAILKB -ge $i ]
	then	LIST="$LIST $i"
	fi
done
lat_mem_rd $MB $LIST

exit 0
