#!/usr/bin/bash
# pistress

###SHELLPACK preamble pistress-bench v1.5

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargParam   --min-invgroups    PISTRESS_MIN_INVGROUPS
###SHELLPACK parseargParam   --max-invgroups    PISTRESS_MAX_INVGROUPS
###SHELLPACK parseargParam   --runtime		PISTRESS_RUNTIME
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

###SHELLPACK check_external_install_required rttestbuild rttestbuild-${VERSION} ${VERSION}
###SHELLPACK init_complete

cd $SHELLPACK_SOURCES/rttestbuild-${VERSION}-installed || die "Failed to change to rttest directory"

KCONFIG_FILE=/proc/config.gz
if [ -f $KCONFIG_FILE ]; then
    cat /proc/config.gz | gunzip > this.config
    HAS_FULL_RT=`grep CONFIG_PREEMPT_RT_FULL=y this.config`
    if [ ! -z $HAS_FULL_RT ]; then
	HAS_RCU_BOOST=`grep CONFIG_RCU_BOOST=y this.config`
	if [ -z $HAS_RCU_BOOST ]; then
	   echo "*** WARNING: kernel has PREEMPT_RT_FULL enabled but lacks RCU-boost. Therefore the 'pi_stress' program can trigger stalls, where real-time tasks starve blocked rcu reader critical regions as well as critical system threads. ***"
	fi
    fi
    rm this.config
fi

###SHELLPACK threads_large_stride_begin $PISTRESS_MIN_INVGROUPS $PISTRESS_MAX_INVGROUPS
	monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS
	echo Starting thread-groups $NR_THREADS/$PISTRESS_MAX_INVGROUPS
	save_rc ./pi_stress --groups=$NR_THREADS --duration=$PISTRESS_RUNTIME 2>&1 | \
		tee $LOGDIR_RESULTS/pistress-${NR_THREADS}.log
	recover_rc
	echo $? > $LOGDIR_RESULTS/pistress-${NR_THREADS}.status
	monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
###SHELLPACK threads_stride_end
exit $SHELLPACK_SUCCESS
