#!/bin/sh
# $Id: timing,v 1.120 2020-12-05 20:05:29 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
			# mptable/acpi signatures may include hypervisor info:
			egrep -i '^(cpu[0-9]*:|MPTable|acpi0:|bios0:|.*MHz)|vbox|xen' /var/run/dmesg.boot
		fi
		if [ -f /bin/kenv ]; then
			echo ''
			echo /bin/kenv:
			/bin/kenv | grep ^smbios
		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 [ -x /usr/bin/lscpu ]; then
			echo ''
			echo lscpu:
			/usr/bin/lscpu
			echo 'ENDLSCPU'
		fi
		if [ -f /proc/cpuinfo ]; then
			echo ''
			echo /proc/cpuinfo:
			cat /proc/cpuinfo
			echo 'ENDCPUINFO'
		fi
		# memory size, mobo vendor, model, clocks (prefer base)
		for x in /proc/meminfo \
		    /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq \
		    /sys/devices/system/cpu/cpu0/cpufreq/base_frequency \
		    /sys/class/dmi/id/sys_vendor \
		    /sys/class/dmi/id/product_name; do
		    if [ -f $x ]; then
			echo ''
			echo ${x}:
			cat $x
		    fi
	        done
		#
		# 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/centos-release /etc/os-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
			/usr/bin/lsb_release -a 2>/dev/null || \
			    /usr/bin/lsb_release -rd 2>/dev/null || \
			    /usr/bin/lsb_release -r
		fi
		# look at disk model names for VM information
		if [ -d /sys/devices ]; then
		    echo ''
		    echo device model files:
		    find /sys/devices -name model -print0 | xargs -0 grep . /dev/null
		fi
		# look for evidence of VM type in PCI device names
		for p in /bin /usr/bin /sbin /usr/sbin `echo $PATH | tr : ' '`; do
		    if [ -x $p/lspci ]; then
			echo ''
			echo $p/lspci
			$p/lspci -v | egrep '^[0-9]|Subsystem:'
			echo 'END_LSPCI'
			break
		    fi
		done

		# Android
		for x in `echo $PATH | tr : ' '`; do
		    if [ -x $x/getprop ]; then
			echo ''
			echo getprop
			$x/getprop | \
			    egrep 'ro\.(product\.vendor|build\.version)'
			break
		    fi
		done
		;;
	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 [ -f /usr/sbin/smbios ]; then
		    echo ''
		    for x in BASEBOARD SYSTEM; do
			echo "smbios $x:"
			/usr/sbin/smbios -t SMB_TYPE_$x
		    done
		    echo 'end smbios'
		fi
		;;
	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

# cygwin (& mingw?)
for DIR in /cygdrive/c /c; do
    FULL=$DIR/windows/system32/systeminfo.exe
    if [ -x $FULL ]; then
	echo ''
	echo $FULL:
	$FULL 2>/dev/null | egrep 'OS|System [MT]'
	break
    fi
done

if [ -x cpuid ]; then
	echo ''
	echo 'cpuid:'
	./cpuid
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

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='../test/procs ../test/globals ../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"
# /usr/bin/time interferes with DYLD_LIBRARY path on MacOS 10.15
# (has long caused more pain than it's worth, now that getrusage
#  is widely available, no need to check that clock ticks correctly divined)
#	eval "$TIME ./snobol4 -x $ARGS -s ../test/bench.sno ../test/v311.sil" 2>stderr

	./snobol4 -x $ARGS -s ../test/bench.sno ../test/v311.sil 2>stderr
)
# use snobol program to process output!!
$SNOBOL $ARGS timing.sno < $TDIR/stderr
# save exit status
STAT=$?

if [ $STAT = 0 ]; then
    rm -rf $TDIR
else
    echo ERROR: timing.sno status: $STAT
fi
echo END
exit $STAT
