#!/bin/csh
#
# Stress our mother-board 2.5V / 1.25V regulator circuitry as much as
# possible.
#
# takes args start, stop, cycle, or status

setenv PATH_OK `where repeatn`
if ("X$PATH_OK" == "X") then
	setenv PATH "${PATH}:/usr/bin/isi_hwtools"
	echo "PATH extended to ${PATH}"
endif

set mode="help";
if ($# == 1) then
    switch ($1)
    case "start":
    case "-start":
	set mode="start";	breaksw;
    case "cycle":
    case "-cycle":
	set mode="cycle";	breaksw;
    case "stop":
    case "-stop":
	set mode="stop";	breaksw;
    case "status":
    case "-status":
	set mode="status";	breaksw;
    #default -- leave mode at help
    endsw
endif

if ($mode == "help") then
    echo "mobo_therm_stress [start|cycle|stop|status]"
    echo "start -- initiates a memstress, repeated nvram dds, and 6 repeated"
    echo "         disk dd's as determined to most heat up D32 on a 4201."
    echo "cycle -- same as start, but runs flat out for an hour, then goes"
    echo "         idle for 10 mins, then repeats.  Prints temps right before"
    echo "         every state change."
    echo "stop  -- print one final time and then kill all the processes"
    echo "         started above."
    echo "status-- print ps and top output to help show system state."
    exit 1
endif

### If status, check and exit
if ($mode == "status") then
    ps -auxwww | grep -E "(mobo_therm_stress|repeatn|memstress|dd)" | grep -v status
    top -Itnu | tail +8 | head -30
    exit 0
endif


### First process stop, because we want to stop prior runs
### (regardless of the current command)
if ($mode == "stop") then
    ### print one final temperature reading before stopping
    /usr/bin/isi_hwtools/isi_hw_status -Tt
endif

### now kill everything
alias killgrep    'kill `ps -auwx | grep -i \!* | grep -v grep | awk '"'"'{ print $2 }'"'"'`'

killall mobo_therm_stress memstress dd
killgrep repeatn
killall mobo_therm_stress memstress dd

if ($mode == "stop") then
    ### and if it really was stop, we're done.
    exit 0
endif


#### at this point, we're either mode=start or cycle... both of
#### which want to start everything.

#Push as much data across the memory bus as possible.  This uses 100% cpu
#if it can, and the more the better... so try to keep other cpu usage down.

limits -d 512M memstress >& /dev/null &


#Push as much data across the nvram PCI bus as possible.  This is a very
#efficient DMA, so uses very little CPU (so lets more memstress run).

repeatn -t 0 -n 0 dd if=/dev/mnv0 of=/dev/null bs=16384k count=32 >& /dev/null &


#Start up some drives.  First, turn off any software-recoveries that might
#slow things down.

sysctl hw.twe0.try_twereset=0
sysctl hw.twe0.try_ecc_recover=0

#Now start up some drives.  Note that 6 drives fills the 3ware's PCI bus
#and uses less CPU than all nine drives (so lets more memstress run).
#Note the "-t 1" means a 1-second delay before restarting just in case the
#drive fails... that way we only restart/die once per second at worst.

test -e /dev/twed0 && repeatn -t 1 -n 0 dd if=/dev/twed0 of=/dev/null bs=64k &
test -e /dev/twed2 && repeatn -t 1 -n 0 dd if=/dev/twed2 of=/dev/null bs=64k &
test -e /dev/twed3 && repeatn -t 1 -n 0 dd if=/dev/twed3 of=/dev/null bs=64k &
test -e /dev/twed5 && repeatn -t 1 -n 0 dd if=/dev/twed5 of=/dev/null bs=64k &
test -e /dev/twed6 && repeatn -t 1 -n 0 dd if=/dev/twed6 of=/dev/null bs=64k &
test -e /dev/twed7 && repeatn -t 1 -n 0 dd if=/dev/twed7 of=/dev/null bs=64k &


#Could also start some network traffic, but that requires external
#connections, and generally doesn't affect temperature much (whatever we
#gain in the extra PCI access we lose in lost memstress time).



if ($mode == "cycle") then
    #Cycle -- 
    #Now put in some time-dependence, to give the chip some thermal cycling.
    #Start or stop all the above processes every 5 minutes.  Also print out
    #time and temperature for reference.

    set ONTIME  = 3600 #60 mins on
    set OFFTIME = 600  #10 mins off
    setenv ISI_HW_STATUS `where isi_hw_status`

    repeatn -t 0 -n 0 "\
	date; ${ISI_HW_STATUS} -Tt ; \
	killall -CONT memstress dd ; sleep ${ONTIME} ; \
	${ISI_HW_STATUS} -Tt ; \
	killall -STOP memstress dd ; sleep 2 ; \
	killall -STOP memstress dd ; sleep ${OFFTIME}" &

    # FYI, on an D32=OnSemi board, this causes D32 to oscillate from 80.8C to
    # 132.4C over and over [ambient around 32C, CPU does 40-55C with fan3 off]
endif
