Announcement

Collapse
No announcement yet.

Script to control CPU frequency depending on core temperatures

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Script to control CPU frequency depending on core temperatures

    I have written a small script that adjusts the CPU scaling frequency depending on core temperatures. Any feedback or comments are appreciated. This script is designed for a quad core system and has to be run as root.

    Adrian

    ----------------------------------------------------------------------------------------

    #!/bin/bash

    # interval time at which script checks for temperature changes and adjusts max. CPU speed
    t3="1"
    # maximum average temperature before cpu frequency is reduced
    temp_max="60"
    temp_core_max="65000"
    # random variables to keep the script run indefinitely
    count2="0"
    counttemp="22"
    # minimum frequency of your cpu in herz (change this to what you find in your /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq file)
    mintemp="2052000"
    # minimum frequency of your cpu in herz (change this to what you find in your /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq file)
    maxtemp="3078000"

    date > /home/adrian/coretemp.log

    while [ "$count2" != "$counttemp" ]
    do
    gov=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor`
    if [ "$gov" != "ondemand" ]
    then
    echo "ondemand" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
    echo "ondemand" > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor
    echo "ondemand" > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor
    echo "ondemand" > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor
    fi

    # read core temps
    temp0=`cat /sys/devices/platform/coretemp.0/temp1_input`
    temp1=`cat /sys/devices/platform/coretemp.1/temp1_input`
    temp2=`cat /sys/devices/platform/coretemp.2/temp1_input`
    temp3=`cat /sys/devices/platform/coretemp.3/temp1_input`

    # calculate average core temperature in celsius
    temp_avg=$(expr $temp0 + $temp1 + $temp2 + $temp3)
    temp_avg=$(expr $temp_avg / "4000")

    # reduce maximum CPU speed if either average core temperature ($temp_avg) is exceeded or if any individual core exceeds the temperature specified in $temp_core_max
    if [ "$temp_avg" -gt "$temp_max" ] || [ "$temp0" -gt "$temp_core_max" ] || [ "$temp1" -gt "$temp_core_max" ] || [ "$temp2" -gt "$temp_core_max" ] || [ "$temp3" -gt "$temp_core_max" ]
    then
    echo $mintemp > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
    echo $mintemp > /sys/devices/system/cpu/cpu1/cpufreq/scaling_max_freq
    echo $mintemp > /sys/devices/system/cpu/cpu2/cpufreq/scaling_max_freq
    echo $mintemp > /sys/devices/system/cpu/cpu3/cpufreq/scaling_max_freq
    # write values to log file - change user to your username
    echo "Coretemp: $temp0 $temp1 $temp2 $temp3 - avg: $temp_avg - CPU frequency: 2.05 Ghz" >> /home/user/coretemp.log
    sleep 1
    else
    echo $maxtemp > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
    echo $maxtemp > /sys/devices/system/cpu/cpu1/cpufreq/scaling_max_freq
    echo $maxtemp > /sys/devices/system/cpu/cpu2/cpufreq/scaling_max_freq
    echo $maxtemp > /sys/devices/system/cpu/cpu3/cpufreq/scaling_max_freq
    # write values to log file - change user to your username
    echo "Coretemp: $temp0 $temp1 $temp2 $temp3 - avg: $temp_avg - CPU frequency: 3.08 Ghz" >> /home/user/coretemp.log
    fi

    # wait for $t3 seconds
    sleep $t3

    done
    exit 0

Working...
X