You are not logged in.

#1 2008-03-26 16:52:25

dyscoria
Member
Registered: 2008-01-10
Posts: 1,007

CPU Frequency Scaling Script

I use the userspace governor alot and wanted a way to change the setspeed quickly and on the fly (and I didn't like cpufrequtils much).
I also happen to be trying to learn how to bash script (badly), so this became my new project. Sharing for anyone that might be interested smile There's probably a really easy way to do this that i've missed, but this has been a good learning experience tongue

Copy and paste the script to the file "~/.scripts/cpufreq" and put "alias cpufreq='home/user/.scripts/cpufreq'" in your ~/.bashrc, although I think you can put it directly in your bashrc with a few changes, or even put it directly in /usr/bin (though I haven't tried doing that).

All you need to do is specify at the top what processors are affected (there are commented instructions for this in the script) and load the appropriate modules (see this wiki article) By default the only affected processor is just cpu0.

The script detects what governors and frequencies are available and provides a menu that you can pick from to change the settings.

cpufreq -i      # gives info on what governor is being used for the specified processors, and what speed it's running at.
                    # nb: this is the only option that can be run as normal user
cpufreq -f      # change the fixed frequency if using the userspace governor
cpufreq         # (no options) change the governor

Here's the script (i assume there's alot of things i've done badly, but i'm open to criticism as part of my learning process big_smile). My CPU's don't currently allow separate governors/frequencies for each one, and I don't think many people would have any real reason to do that, so i've not included that functionality (yet?):

#!/bin/bash
# specify the range of CPU's this script will affect, counting from zero.
# e.g. for 2 processors, you'd change it to read {0..1}
affected_processors=$(echo {0..0})

# start of script
affected_processors_colour=$(tput bold;tput setaf 2;echo $affected_processors;tput sgr0;tput bold)
arrow=$(tput bold;tput setaf 2;echo ' >';tput sgr0;tput bold)
available_freq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies)
available_governors=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors)
current_freq_colour=$(tput bold;tput setaf 2;echo $(( $(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq) / 1000 ));tput sgr0;tput bold)
current_governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
current_governor_colour=$(tput bold;tput setaf 2;echo $current_governor;tput sgr0;tput bold)

if [[ $1 == "-i" ]] # information on governor and frequency
then
    echo -e ""$arrow"The affected CPU's are: "$affected_processors_colour" \n"$arrow"The current governor is "$current_governor_colour" \n"$arrow"The current CPU frequency is "$current_freq_colour"Mhz"
    tput sgr0
    exit

elif [[ "$(whoami)" != "root" ]] # other functions require root privileges
then
    echo ""$arrow"This script should be run as root."
    tput sgr0
    exit

elif [[ $1 == "-f" && "$current_governor" == "userspace" ]] # change frequency for userspace governor
then
    echo -e "\n"$arrow"The current frequency is "$current_freq_colour"Mhz for the following processors: "$affected_processors_colour" \n"
    tput sgr0
    echo "The available CPU frequencies (in Khz) are:"
    select CHOICE_FREQ in $available_freq quit
do
    case "$CHOICE_FREQ" in
        "quit" )
        exit
        ;;
        "$CHOICE_FREQ" )
        correct_freq=undefined
        for freq in $available_freq
        do
            if [[ $freq == $CHOICE_FREQ ]]
            then
                for proc in $affected_processors
                do
                    echo "$CHOICE_FREQ" > /sys/devices/system/cpu/cpu"$proc"/cpufreq/scaling_setspeed
                    new_freq=$(tput bold;tput setaf 2;echo $(( $(cat /sys/devices/system/cpu/cpu"$proc"/cpufreq/scaling_cur_freq) / 1000 ));tput sgr0;tput bold)
                    echo -e "\n"$arrow"The frequency of cpu"$proc" is now set to "$new_freq"Mhz \n"
                    tput sgr0
                done
                correct_freq=yes
            fi
        done
        if [[ $correct_freq != "yes" ]]
        then
            echo -e "\n"$arrow"Invalid response! \n"
            tput sgr0
        fi
        ;;
    esac
done

elif [[ $# -eq 0 ]] # change governor
then
    echo -e "\n"$arrow"The current governor is "$current_governor_colour"for the following processors: "$affected_processors_colour" \n"
    tput sgr0
    echo "The available governors are:"
    select CHOICE_GOV in $available_governors quit
do
    case "$CHOICE_GOV" in
        "quit" )
        exit
        ;;
        "$CHOICE_GOV" )
        correct_gov=undefined
        for gov in $available_governors
        do
            if [[ $gov == $CHOICE_GOV ]]
            then
                for proc in $affected_processors
                do
                    echo "$CHOICE_GOV" > /sys/devices/system/cpu/cpu"$proc"/cpufreq/scaling_governor
                    new_governor=$(tput bold;tput setaf 2;cat /sys/devices/system/cpu/cpu"$proc"/cpufreq/scaling_governor;tput sgr0)
                    echo -e "\n"$arrow"The scaling governor of cpu"$proc" is now set to "$new_governor" \n"
                    tput sgr0
                done
                correct_gov=yes
            fi
        done
        if [[ $correct_gov != "yes" ]]
        then
            echo -e "\n"$arrow"Invalid response! \n"
            tput sgr0
        fi
        ;;
    esac
done

fi

Last edited by dyscoria (2008-03-26 16:57:15)


flack 2.0.6: menu-driven BASH script to easily tag FLAC files (AUR)
knock-once 1.2: BASH script to easily create/send one-time sequences for knockd (forum/AUR)

Offline

Board footer

Powered by FluxBB