#!/bin/sh
# $Id: timing,v 1.92 2010/05/15 17:57:16 phil Exp $

# perform basic timing tests
# pb 6/4/94

if [ "$*" ]; then
	SNOBOL=$1
	shift
	ARGS="$*"
else
	SNOBOL=./xsnobol4
fi

# for cygwin
if [ -f ${SNOBOL}.exe ]; then
	EXT=.exe
fi
SNOBOL=${SNOBOL}$EXT

echo 'Date:'
date

for p in `echo $PATH | tr : ' '`; do
	if [ -f $p/uname ]; then
		UNAME=$p/uname
		break
	fi
done
if [ "$UNAME" ]; then
	echo ''
	echo 'uname -a:'
	$UNAME -a

	echo ''
	echo 'uname -n:'
	$UNAME -n

	echo ''
	echo 'uname -rs:'
	$UNAME -rs

	echo ''
	echo 'uname -m:'
	$UNAME -m

	# show hardware, if we know how;
	case `$UNAME` in
	AIX*)
		if [ -x /usr/sbin/lsattr ]; then
			for X in proc0 \
			    'sys0 -a realmem -a modelname' \
			    `(/usr/sbin/lsdev -C -c memory | cut -d' ' -f1) 2>/dev/null`; do
				echo ''
				echo /usr/sbin/lsattr -EHl $X
				/usr/sbin/lsattr -EHl $X 2>&1
			done
		fi
		if [ -x /usr/bin/oslevel ]; then
			echo ''
			echo /usr/bin/oslevel
			/usr/bin/oslevel
		fi
		;;
	*BSD*|Darwin)
		if [ -x /usr/sbin/sysctl ]; then
			SYSCTL=/usr/sbin/sysctl
		elif [ -x /sbin/sysctl ]; then
			SYSCTL=/sbin/sysctl
		else
			SYSCTL=
		fi
		if [ "$SYSCTL" ]; then
			echo ''
			echo ${SYSCTL}:
			$SYSCTL hw 2>/dev/null
			echo ''
			$SYSCTL machdep 2>/dev/null
		fi
		if [ -f /var/run/dmesg.boot ]; then
			echo ''
			echo /var/run/dmesg.boot
			grep -i '^cpu[0-9]*:' /var/run/dmesg.boot
			# Alpha:
			grep 'MHz' /var/run/dmesg.boot
		fi
		;;
	HP-UX)
		if [ -f /usr/bin/model ]; then
			echo ''
			echo /usr/bin/model:
			/usr/bin/model
		elif [ -f /bin/model ]; then
			echo ''
			echo /bin/model:
			/bin/model
		elif [ -f /usr/bin/getconf ]; then
			echo ''
			echo getconf MACHINE_MODEL:
			/usr/bin/getconf MACHINE_MODEL
		fi
		if [ -f /usr/bin/getconf ]; then
			echo ''
			# see CPUFlags script (URL in INSTALL)
			# for decode...
			echo getconf CPU_CHIP_TYPE:
			/usr/bin/getconf CPU_CHIP_TYPE
		fi

		# PA-RISC: can lookup arch using model output
		# in /usr/sam/lib/mo/sched.models

		# ia64 only
		if [ -f /usr/contrib/bin/machinfo ]; then
			echo ''
			echo /usr/contrib/bin/machinfo
			/usr/contrib/bin/machinfo
			echo ENDMACHINFO
		fi
		;;
	IRIX*)
		echo ''
		echo 'hinv:'
		hinv
		;;
	Linux)
		if [ -f /proc/cpuinfo ]; then
			echo ''
			echo /proc/cpuinfo:
			cat /proc/cpuinfo
			echo 'ENDCPUINFO'
		fi
		if [ -f /proc/meminfo ]; then
			echo ''
			echo /proc/meminfo:
			cat /proc/meminfo
		fi
		#
		# mandrake before redhat
		# lsb before debian
		for RELEASE in /etc/mandrake-release \
		    /etc/SuSe-release /etc/SuSE-release \
		    /etc/slackware-version /etc/turbolinux-release \
		    /etc/lsb-release /etc/debian_version \
		    /usr/lib/setup/slack-version-?.?? /etc/gentoo-release \
		    /etc/fedora-release /etc/redhat-release /etc/*-release; do
			if [ -f ${RELEASE} ]; then
				echo ''
				echo releasefile ${RELEASE}:
				cat ${RELEASE}
				break
			fi
		done
		if [ -f /usr/bin/emerge ]; then
			echo ''
			echo emerge info:
			/usr/bin/emerge info
			echo 'EMERGE END'
		fi
		if [ -f /usr/bin/lsb_release ]; then
			echo ''
			echo lsb_release -rd:
			/usr/bin/lsb_release -rd
		fi
		;;
	OSF1)
		if [ -f /usr/sbin/snmp_request ]; then
			echo ''
			echo 'snmp sysDescr:'
			/usr/sbin/snmp_request localhost public get \
							1.3.6.1.2.1.1.1.0
		fi
		;;
	SunOS)
		echo ''
		echo 'sunmodel:'
		./sunmodel 2>&1
		if [ `uname -m` = i86pc ]; then
			CPUID=1
		fi
		;;

	MINGW*|CYGWIN*)
		CPUID=1
		;;
	esac

	case `$UNAME -m` in
	*86|*x86*) CPUID=1;;
	esac
else
	for p in `echo $PATH | tr : ' '`; do
		if [ -x $p/hostname ]; then
			echo ''
			echo 'hostname:'
			$p/hostname
			break
		fi
	done
fi

# Solaris, Tru64
if [ -f /usr/sbin/psrinfo ]; then
	echo ''
	echo 'psrinfo -v:'
	/usr/sbin/psrinfo -v
fi

# NextStep, Darwin
if [ -f /usr/bin/hostinfo ]; then
	echo ''
	echo 'hostinfo:'
	/usr/bin/hostinfo
fi

# NextStep?, Darwin
if [ -f /usr/bin/sw_vers ]; then
	echo ''
	echo 'sw_vers:'
	/usr/bin/sw_vers
fi

# Darwin
if [ -x /usr/sbin/system_profiler ]; then
	echo ''
	echo /usr/sbin/system_profiler:
	/usr/sbin/system_profiler SPHardwareDataType SPSoftwareDataType
	echo END_SYSTEM_PROFILER
fi

if [ "$CPUID" ]; then
	if gcc -o cpuid cpuid.c 2>&1 >/dev/null; then
		echo ''
		echo 'cpuid:'
		./cpuid
	fi
fi

if [ -f 00README.CVS ]; then
	echo ''
	echo 'found 00README.CVS'
	if [ -f CVS/Entries ]; then
		echo ''
		echo 'CVS/Entries:'
		ls -l CVS/Entries
		cat CVS/Entries
	fi
fi

# kinda redundant, but timing script could be run on a different host!
if [ -x config.guess ]; then
	echo ''
	echo 'config.guess:'
	./config.guess
fi

echo ''
echo "ls -l config.m4 config.h build.c ${SNOBOL}:"
ls -l config.m4 config.h build.c $SNOBOL

echo ''
echo 'config.m4:'
cat config.m4

echo ''
echo 'config.h:'
cat config.h

echo ''
echo 'build.c:'
cat build.c

echo ''
echo "size ${SNOBOL}:"
size $SNOBOL

if [ -x /usr/bin/file ]; then
	echo ''
	echo "file ${SNOBOL}:"
	/usr/bin/file ${SNOBOL}
fi

if [ -x /usr/bin/uptime ]; then
	echo ''
	echo 'uptime:'
	/usr/bin/uptime
elif [ -x /usr/ucb/uptime ]; then
	echo ''
	echo 'uptime:'
	/usr/ucb/uptime
fi

echo ''
echo 'Ids:'
grep '^[*#]*[	 ]*$Id:' genc.sno timing.sno timing v311.sil History

echo ''
echo 'timing run of genc.sno:'
if [ "$ARGS" ]; then
	echo "	args: $ARGS"
fi

# stdout redirection fails with built in "time" (OpenBSD, AIX4)
if [ -x /usr/bin/time ]; then
	TIME=/usr/bin/time
elif [ -x /usr/5bin/time ]; then
	TIME=/usr/5bin/time
else
	TIME=time
fi

# run genc in a subdirectory to avoid trashing generated files!

TDIR=timdir.$$
rm -rf $TDIR
trap "rm -rf $TDIR; exit 1" 1 2 15
mkdir $TDIR
OUT=stdout
TAIL1='sed -n 2,$p'
(
	DEST=$TDIR/snobol4$EXT

	# AFS (Andrew File System) won't hard link across directories!
	# Can't symlink-- need to handle path relative to invocation directory!
	ln $SNOBOL $DEST 2>/dev/null || cp $SNOBOL $DEST

	cd $TDIR

LNFILES='../procs ../globals ../v311.sil ../host.sno'
	# AFS doesn't allow hard links across directories!
	ln $LNFILES . 2>/dev/null || ln -s $LNFILES .

	# need eval on Debian 3.0 (no "time" binary)
	# stderr to file "stderr", all but first line of stdout to "$OUT"
	eval "$TIME ./snobol4 $ARGS -s ../genc.sno v311.sil" 2>stderr | $TAIL1 >$OUT
)
# use snobol program to process output!!
$SNOBOL $ARGS -b timing.sno < $TDIR/stderr
# save exit status
STAT=$?

# check output regardless of timing.sno status
if $TAIL1 < snobol4.c | cmp -s - $TDIR/$OUT; then
	echo "genc.sno output checks out"
	rm -rf $TDIR
else
	echo "ERROR: genc.sno output is bad; compare $TDIR/$OUT with snobol4.c"
	STAT=2
fi

exit $STAT
