#!/bin/bash
# This benchmark simple uses /dev/cpu_dma_latency to set the requested
# latency. It outputs nothing. The point is to run turbostat as a monitor
# and determine if it's behaving as expected

P=cpudmalatency-bench
DEFAULT_VERSION=0
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
SERVERSIDE_COMMAND=none
SERVERSIDE_NAME=`date +%Y%m%d-%H%M-%S`

while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--serverside-command)
		SERVERSIDE_COMMAND=$2
		shift 2
		;;
	--serverside-name)
		SERVERSIDE_NAME=$2
		shift 2
		;;
	--duration)
		CPUDMALATENCY_DURATION=$2
		shift 2
		;;
	--quiet)
		CPUDMALATENCY_QUIET=$2
		shift 2
		;;
	--latency)
		CPUDMALATENCY_LATENCY=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ "$TASKSET_SERVER" != "" ]; then
	echo TASKSET_SERVER: $TASKSET_SERVER
	echo TASKSET_CLIENT: $TASKSET_CLIENT
fi
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi
# Include monitor hooks
. $SHELLPACK_INCLUDE/include-monitor.sh

LINESTART=`grep -n "==== BEGIN cpudmalatency.pl" $0 | tail -1 | awk -F : '{print $1}'`
LINEEND=`grep -n "==== END cpudmalatency.pl" $0 | tail -1 | awk -F : '{print $1}'`
if [ "$LINEEND" = "" ]; then
	LINECOUNT=`wc -l $0 | awk '{print $1}'`
fi
if [ "$LINESTART" = "" ]; then
	die Failed to find start of file cpudmalatency.pl
fi
echo Extracting $SHELLPACK_TEMP/cpudmalatency.pl
sed -n $((LINESTART+1)),$((LINEEND-1))p $0 > $SHELLPACK_TEMP/cpudmalatency.pl
sed -i -e "s/LATENCY/$CPUDMALATENCY_LATENCY/" $SHELLPACK_TEMP/cpudmalatency.pl
sed -i -e "s/DURATION/$CPUDMALATENCY_DURATION/" $SHELLPACK_TEMP/cpudmalatency.pl
sed -i -e "s/QUIET/$CPUDMALATENCY_QUIET/" $SHELLPACK_TEMP/cpudmalatency.pl
chmod a+x $SHELLPACK_TEMP/cpudmalatency.pl

monitor_pre_hook $LOGDIR_RESULTS cpudmalatency
$TIME_CMD -o $LOGDIR_RESULTS/time $SHELLPACK_TEMP/cpudmalatency.pl
monitor_post_hook $LOGDIR_RESULTS cpudmalatency

exit $SHELLPACK_SUCCESS
==== BEGIN cpudmalatency.pl ====
#!/usr/bin/perl
sleep(QUIET);
open(CPU, ">/dev/cpu_dma_latency") || die "Failed to open cpu_dma_latency";
syswrite CPU, LATENCY;
sleep(DURATION);
close(CPU);
sleep(QUIET);
==== END cpudmalatency.pl ====
#### Description cpudmalatency
#### Details cpudmalatency-bench 4
