Skip to content

Instantly share code, notes, and snippets.

@tiagoapimenta
Last active May 21, 2023 16:55

Revisions

  1. tiagoapimenta revised this gist May 17, 2018. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions fanmonitor
    Original file line number Diff line number Diff line change
    @@ -151,8 +151,8 @@ while :; do
    elif [ $speed -gt $speed_60 ]; then
    change_to $speed_60
    fi
    if [ $downclock -ne 0 -a $clock -lt 4 ]; then
    clock_to 4
    if [ $downclock -ne 0 -a $clock -lt 5 ]; then
    clock_to 5
    fi
    elif [ $temp -lt 46000 ]; then
    if [ $speed -lt $speed_60 ]; then
    @@ -163,8 +163,8 @@ while :; do
    if [ $downclock -ne 0 ]; then
    if [ $clock -lt 3 ]; then
    clock_to 3
    elif [ $clock -gt 4 ]; then
    clock_to 4
    elif [ $clock -gt 5 ]; then
    clock_to 5
    fi
    fi
    elif [ $temp -lt 47000 ]; then
    @@ -194,4 +194,4 @@ while :; do
    fi
    fi
    sleep $interval
    done
    done
  2. tiagoapimenta revised this gist May 17, 2018. 1 changed file with 13 additions and 11 deletions.
    24 changes: 13 additions & 11 deletions fanmonitor
    Original file line number Diff line number Diff line change
    @@ -151,30 +151,32 @@ while :; do
    elif [ $speed -gt $speed_60 ]; then
    change_to $speed_60
    fi
    if [ $downclock -ne 0 ]; then
    clock_to 7
    if [ $downclock -ne 0 -a $clock -lt 4 ]; then
    clock_to 4
    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 -a $clock -lt 3 ]; then
    clock_to 3
    if [ $downclock -ne 0 ]; then
    if [ $clock -lt 3 ]; then
    clock_to 3
    elif [ $clock -gt 4 ]; then
    clock_to 4
    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 [ $downclock -ne 0 ]; then
    if [ $clock -lt 2 ]; then
    clock_to 2
    elif [ $clock -gt 3 ]; then
    clock_to 3
    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
    @@ -192,4 +194,4 @@ while :; do
    fi
    fi
    sleep $interval
    done
    done
  3. tiagoapimenta revised this gist May 17, 2018. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions fanmonitor
    Original file line number Diff line number Diff line change
    @@ -97,7 +97,7 @@ downclock=0
    clock=7
    clock_to() {
    clock=$1
    if [ "$clock" = 7 ]; then
    if [ "$clock" -ge 7 ]; then
    if [ $downclock -ne 0 ]; then
    downclock=0
    printf 'auto\n' > "$drm0"
    @@ -160,35 +160,35 @@ while :; do
    elif [ $speed -gt $speed_75 ]; then
    change_to $speed_75
    fi
    if [ $downclock -ne 0 -a $clock -lt 4 ]; then
    clock_to 4
    if [ $downclock -ne 0 -a $clock -lt 3 ]; then
    clock_to 3
    fi
    elif [ $temp -lt 48000 ]; then
    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 [ $downclock -ne 0 ]; then
    if [ $clock -lt 3 ]; then
    if [ $clock -lt 2 ]; then
    clock_to 2
    elif [ $clock -gt 3 ]; then
    clock_to 3
    elif [ $clock -gt 4 ]; then
    clock_to 4
    fi
    fi
    elif [ $temp -lt 49000 ]; then
    elif [ $temp -lt 48000 ]; then
    if [ $speed -lt $speed_90 ]; then
    change_to $speed_90
    fi
    if [ $clock -gt 3 ]; then
    clock_to 3
    if [ $clock -gt 2 ]; then
    clock_to 2
    fi
    else
    if [ $speed -ne $max ]; then
    change_to $max
    fi
    if [ $clock -gt 2 ]; then
    clock_to 2
    if [ $clock -gt 1 ]; then
    clock_to 1
    fi
    fi
    sleep $interval
  4. tiagoapimenta revised this gist May 17, 2018. 1 changed file with 130 additions and 133 deletions.
    263 changes: 130 additions & 133 deletions fanmonitor
    Original file line number Diff line number Diff line change
    @@ -3,20 +3,20 @@
    set -e

    if [ $(id -u) -ne 0 ]; then
    exec sudo -- "$0" "$@"
    exit 1
    exec sudo -- "$0" "$@"
    exit 1
    fi

    if [ $# -gt 0 -a "$1" = -d ]; then
    shift
    nohup "$0" "$@" > /dev/null 2>&1 &
    exit
    shift
    nohup "$0" "$@" > /dev/null 2>&1 &
    exit
    fi

    stop=0

    if [ $# -gt 0 -a \( "$1" = -s -o "$1" = -k \) ]; then
    stop=1
    stop=1
    fi

    interval=0.5
    @@ -31,48 +31,48 @@ 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
    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"
    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
    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"
    @@ -81,7 +81,7 @@ trap atexit EXIT
    trap atexit INT TERM

    if [ $stop -ne 0 ]; then
    exit
    exit
    fi

    read min < "${file}_min"
    @@ -96,103 +96,100 @@ speed_90=$(((max - min) * 9 / 10 + min))
    downclock=0
    clock=7
    clock_to() {
    clock="$1"
    if [ $downclock -eq 0 ]; then
    downclock=1
    printf 'manual\n' > "$drm0"
    fi
    printf '%s\n' "$clock" > "$drm1"
    clock=$1
    if [ "$clock" = 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"
    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
    printf 'auto\n' > "$drm0"
    downclock=0
    clock=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 -a $clock -lt 6 ]; then
    clock_to 6
    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 ]; then
    if [ $clock -lt 5 ]; then
    clock_to 5
    elif [ $clock -gt 6 ]; then
    clock_to 6
    fi
    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 4 ]; then
    clock_to 4
    elif [ $clock -gt 5 ]; then
    clock_to 5
    fi
    fi
    elif [ $temp -lt 48000 ]; then
    if [ $speed -lt $speed_75 ]; then
    change_to $speed_75
    elif [ $speed -gt $speed_90 ]; then
    change_to $speed_90
    fi
    if [ $downclock -ne 0 ]; then
    if [ $clock -lt 3 ]; then
    clock_to 3
    elif [ $clock -gt 4 ]; then
    clock_to 4
    fi
    fi
    elif [ $temp -lt 49000 ]; then
    if [ $speed -lt $speed_90 ]; then
    change_to $speed_90
    fi
    if [ $downclock -ne 0 -a $clock -gt 3 ]; then
    clock_to 3
    fi
    else
    if [ $speed -ne $max ]; then
    change_to $max
    fi
    if [ $clock -gt 2 ]; then
    clock_to 2
    fi
    fi
    sleep $interval
    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 ]; then
    clock_to 7
    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 -a $clock -lt 4 ]; then
    clock_to 4
    fi
    elif [ $temp -lt 48000 ]; then
    if [ $speed -lt $speed_75 ]; then
    change_to $speed_75
    elif [ $speed -gt $speed_90 ]; then
    change_to $speed_90
    fi
    if [ $downclock -ne 0 ]; then
    if [ $clock -lt 3 ]; then
    clock_to 3
    elif [ $clock -gt 4 ]; then
    clock_to 4
    fi
    fi
    elif [ $temp -lt 49000 ]; then
    if [ $speed -lt $speed_90 ]; then
    change_to $speed_90
    fi
    if [ $clock -gt 3 ]; then
    clock_to 3
    fi
    else
    if [ $speed -ne $max ]; then
    change_to $max
    fi
    if [ $clock -gt 2 ]; then
    clock_to 2
    fi
    fi
    sleep $interval
    done
  5. tiagoapimenta created this gist May 17, 2018.
    198 changes: 198 additions & 0 deletions fanmonitor
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,198 @@
    #!/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 [ $downclock -eq 0 ]; then
    downclock=1
    printf 'manual\n' > "$drm0"
    fi
    printf '%s\n' "$clock" > "$drm1"
    }

    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
    printf 'auto\n' > "$drm0"
    downclock=0
    clock=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 -a $clock -lt 6 ]; then
    clock_to 6
    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 ]; then
    if [ $clock -lt 5 ]; then
    clock_to 5
    elif [ $clock -gt 6 ]; then
    clock_to 6
    fi
    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 4 ]; then
    clock_to 4
    elif [ $clock -gt 5 ]; then
    clock_to 5
    fi
    fi
    elif [ $temp -lt 48000 ]; then
    if [ $speed -lt $speed_75 ]; then
    change_to $speed_75
    elif [ $speed -gt $speed_90 ]; then
    change_to $speed_90
    fi
    if [ $downclock -ne 0 ]; then
    if [ $clock -lt 3 ]; then
    clock_to 3
    elif [ $clock -gt 4 ]; then
    clock_to 4
    fi
    fi
    elif [ $temp -lt 49000 ]; then
    if [ $speed -lt $speed_90 ]; then
    change_to $speed_90
    fi
    if [ $downclock -ne 0 -a $clock -gt 3 ]; then
    clock_to 3
    fi
    else
    if [ $speed -ne $max ]; then
    change_to $max
    fi
    if [ $clock -gt 2 ]; then
    clock_to 2
    fi
    fi
    sleep $interval
    done