#!/bin/bash
 
# Define variables
sm_version="1.1a"		# SOM Monitor Version
monitor_delay=5			# Monitor delay

# Temperature variables
declare -i temp_curr		# Current Temperature
temp_curr=0

#Governor control variables
declare -i gov_throttle_up	# Throttle Up Temperature Limit
declare -i gov_throttle_down	# Throttle Down Temperature Limit
declare -i gov_throttle_count	# Throttle Count
declare -i gov_user_freq	# Throttle Down userspace frequency index
gov_throttle_up=70000		# Throttle Up Temperature Limit
gov_throttle_down=95000		# Throttle Down Temperature Limit
gov_throttle_count=0		# Throttle Count
#gov_up_cmd="ondemand"		# Throttle Up Command
gov_up_cmd="performance"	# Throttle Up Command
gov_down_user_cmd="userspace"	# Throttle Down Command
gov_user_freq=920000		# Throttle Down userspace frequency

# project.exe niceness level control
declare -i nice_PID		# project.exe PID
declare -i nice_level		# project.exe niceness level
nice_PID=0			# project.exe PID
nice_level=0			# project.exe niceness level
nice_line=""			# String to hold project.exe information line


# Force performance governor
echo "performance" | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor


##############################
# Monitor loop
#

while true
do

	# Niceness Control
	# Determine current niceness level...
	#nice_line=$(ps ax -o pid,ni,cmd | egrep project.exe | egrep -v grep)
	nice_line=$(ps ax -o pid,ni,cmd | egrep ./bin/debug/project.exe | egrep -v grep)
	nice_PID=$(echo "$nice_line" | awk '{print $1}')
	nice_level=$(echo "$nice_line" | awk '{print $2}')

	# If a seemingly good PID value and a nice level of >= 0, change it
	if [ $nice_PID -ge 0 -a $nice_level -ge 0 ]
	then
		echo "*** Invalid nice level ***"
		sudo renice -5 -p $nice_PID
	fi

	# SOM Temperature
	temp_curr=`cat /sys/bus/platform/devices/temp_sensor_hwmon.0/temp1_input`

	# Current Governor
	gov_cpu0="$(sudo cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)"
	gov_cpu1="$(sudo cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor)"

	# Throttle Control
	# Determine if we need to throttle down...
	if [ $temp_curr -ge $gov_throttle_down ]
	then
		if  [ "$gov_down_user_cmd" != "$gov_cpu0" ] ||  [ "$gov_down_user_cmd" != "$gov_cpu1" ]
		then
			echo "*** CRITICAL TEMPERATURE RISE ***"
			echo "Throttling Down to $gov_down_user_cmd, $gov_user_freq"
			echo $gov_down_user_cmd | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
			echo $gov_user_freq | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_setspeed
			((gov_throttle_count++))
		fi
	fi

	# Determine if we need to throttle up...
	if [ $temp_curr -le $gov_throttle_up ]
	then
		if  [ "$gov_up_cmd" != "$gov_cpu0" ] ||  [ "$gov_up_cmd" != "$gov_cpu1" ]
		then
			echo "Throttling Up to $gov_up_cmd"
			echo $gov_up_cmd | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
			((gov_throttle_count++))
		fi
	fi

	# Now, delay and repeat
	sleep $monitor_delay

done

#
# Monitor loop
##############################

