Skip to content

Instantly share code, notes, and snippets.

@tiagoapimenta
Last active May 21, 2023 16:55
Show Gist options
  • Save tiagoapimenta/c1aa238091dc762f0a70eb3951af9f64 to your computer and use it in GitHub Desktop.
Save tiagoapimenta/c1aa238091dc762f0a70eb3951af9f64 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -e
if [ $(id -u) -ne 0 ]; then
exec sudo -- "$0" "$@"
exit 1
fi
if [ $# -gt 0 -a "$1" = -d ]; then
shift
nohup "$0" "$@" > /dev/null 2>&1 &
exit
fi
stop=0
if [ $# -gt 0 -a \( "$1" = -s -o "$1" = -k \) ]; then
stop=1
fi
interval=0.5
file='/sys/class/hwmon/hwmon0/pwm1'
file_temp='/sys/class/hwmon/hwmon0/temp1_input'
file0='/sys/class/hwmon/hwmon1/pwm3'
file1='/sys/class/hwmon/hwmon1/pwm4'
file2='/sys/class/hwmon/hwmon1/pwm5'
drm0='/sys/class/drm/card0/device/power_dpm_force_performance_level'
drm1='/sys/class/drm/card0/device/pp_dpm_sclk'
lock=/tmp/.fanmonitor
atexit() {
flock -u 9 2> /dev/null || :
exec 9>&-
if [ -f "$lock" ]; then
unlink "$lock"
fi
if [ -f "$lock.pid" ]; then
unlink "$lock.pid"
fi
printf '2\n' > "${file}_enable"
printf '5\n' > "${file0}_enable"
printf '5\n' > "${file1}_enable"
printf '5\n' > "${file2}_enable"
printf 'auto\n' > "$drm0"
exit
}
read pid _ < /proc/self/stat
if [ ! -f "$lock" ]; then
touch "$lock"
fi
exec 9> "$lock"
if ! flock -nx 9; then
if [ -f "$lock.pid" ]; then
read old_pid < "$lock.pid"
if [ -d "/proc/$old_pid" ]; then
kill "$old_pid"
sleep 2
if [ -d "/proc/$old_pid" ]; then
kill -9 "$old_pid"
sleep 2
if [ -d "/proc/$old_pid" ]; then
printf 'Error to stop old monitor.\n' >&2
exit 1
fi
fi
fi
fi
if ! flock -nx 9; then
printf 'Error to obtain lock.\n' >&2
exit 1
fi
fi
printf '%s\n' "$pid" > "$lock.pid"
trap atexit EXIT
trap atexit INT TERM
if [ $stop -ne 0 ]; then
exit
fi
read min < "${file}_min"
read max < "${file}_max"
read speed < "$file"
speed_30=$(((max - min) / 3 + min))
speed_50=$(((max - min) / 2 + min))
speed_60=$(((max - min) * 2 / 3 + min))
speed_75=$(((max - min) * 3 / 4 + min))
speed_90=$(((max - min) * 9 / 10 + min))
downclock=0
clock=7
clock_to() {
clock=$1
if [ "$clock" -ge 7 ]; then
if [ $downclock -ne 0 ]; then
downclock=0
printf 'auto\n' > "$drm0"
fi
else
if [ $downclock -eq 0 ]; then
downclock=1
printf 'manual\n' > "$drm0"
fi
printf '%s\n' "$clock" > "$drm1"
fi
}
first=1
change_to() {
speed=$1
if [ $first -ne 0 ]; then
first=0
printf '1\n' > "${file}_enable"
printf '1\n' > "${file0}_enable"
printf '1\n' > "${file1}_enable"
printf '1\n' > "${file2}_enable"
fi
printf '%s\n' "$1" > "$file"
printf '%s\n' "$1" > "$file0"
printf '%s\n' "$1" > "$file1"
printf '%s\n' "$1" > "$file2"
}
while :; do
read temp < "$file_temp"
if [ $temp -lt 40000 ]; then
if [ $speed -gt $speed_30 ]; then
change_to $speed_30
fi
if [ $downclock -ne 0 ]; then
clock_to 7
fi
elif [ $temp -lt 42000 ]; then
if [ $speed -lt $speed_30 ]; then
change_to $speed_30
elif [ $speed -gt $speed_50 ]; then
change_to $speed_50
fi
if [ $downclock -ne 0 ]; then
clock_to 7
fi
elif [ $temp -lt 44000 ]; then
if [ $speed -lt $speed_50 ]; then
change_to $speed_50
elif [ $speed -gt $speed_60 ]; then
change_to $speed_60
fi
if [ $downclock -ne 0 -a $clock -lt 5 ]; then
clock_to 5
fi
elif [ $temp -lt 46000 ]; then
if [ $speed -lt $speed_60 ]; then
change_to $speed_60
elif [ $speed -gt $speed_75 ]; then
change_to $speed_75
fi
if [ $downclock -ne 0 ]; then
if [ $clock -lt 3 ]; then
clock_to 3
elif [ $clock -gt 5 ]; then
clock_to 5
fi
fi
elif [ $temp -lt 47000 ]; then
if [ $speed -lt $speed_75 ]; then
change_to $speed_75
elif [ $speed -gt $speed_90 ]; then
change_to $speed_90
fi
if [ $clock -lt 2 ]; then
clock_to 2
elif [ $clock -gt 3 ]; then
clock_to 3
fi
elif [ $temp -lt 48000 ]; then
if [ $speed -lt $speed_90 ]; then
change_to $speed_90
fi
if [ $clock -gt 2 ]; then
clock_to 2
fi
else
if [ $speed -ne $max ]; then
change_to $max
fi
if [ $clock -gt 1 ]; then
clock_to 1
fi
fi
sleep $interval
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment