#!/bin/sh -e
# NAME=CPU burn
# DESCRIPTION=Basic CPU burn test makes CPU execute instructions that rapidly increase process temperature in infinite loop. Test makes special care This test  CPU itself or 
# DESTROYS_HDD=false
# IS_INTERACTIVE=false
# POWEROFF_DURING_TEST=false
# VERSION=0.1
# TAGS=cpu,stress
# DEPENDS=CPU
# VAR=TESTTIME:int:1800:Total time of CPU testing, sec

. /usr/share/inquisitor/functions-test

exit_handler()
{
	for I in $TMP_DIR/*.pid; do
		if [ -f "$I" ]; then
			stop_background "$I"
		fi
	done
	[ -d "$TMP_DIR" ] && rmdir "$TMP_DIR"
}

TMP_DIR=`mktemp -d`

# Detecting burn program
echo -n 'Detecting burners... '
if grep -q '^vendor_id.*:.*AuthenticAMD' /proc/cpuinfo; then
	BURN_PROGRAM=burnK7
else
	BURN_PROGRAM=burnP6
fi
echo -n $BURN_PROGRAM
echo_success

# Detect CPU cores quantity
CPU_QUANTITY=`grep '^processor' /proc/cpuinfo | wc -l`

# Start
for i in `seq 0 $(($CPU_QUANTITY - 1))`; do
	echo -n "Starting burn on core $i"
	start_background "$TMP_DIR/cpuburn$i.pid" "$i" "$BURN_PROGRAM"
	echo_success
done

test_promise_time $TESTTIME

echo -n "Burning for $TESTTIME..."
sleep $TESTTIME
echo_success

# Stop everything; fail if something is already dead
for i in `seq 0 $(($CPU_QUANTITY - 1))`; do
	echo -n "Stopping burn on core $i"
	if stop_background "$TMP_DIR/cpuburn$i.pid"; then
		echo_success
	else
		echo_failed
		test_failed "Burner $i died prematurely"
	fi
done

test_succeeded
