Skip to content

Instantly share code, notes, and snippets.

@librehat
Created February 10, 2015 23:07
Show Gist options
  • Select an option

  • Save librehat/4a3dee3a090fe4df0523 to your computer and use it in GitHub Desktop.

Select an option

Save librehat/4a3dee3a090fe4df0523 to your computer and use it in GitHub Desktop.

Revisions

  1. librehat created this gist Feb 10, 2015.
    50 changes: 50 additions & 0 deletions setcpu.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    #!/bin/bash
    # change cpu frequency

    max_cap=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq`
    min_cap=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`

    # lowest ~ 1.71GHz by default
    max_freq=1710000
    min_freq=$min_cap

    # or you can specify by arguments
    while getopts ":a::i:" opt; do
    case $opt in
    a)
    max_freq=$OPTARG
    ;;
    i)
    min_freq=$OPTARG
    ;;
    \?)
    echo "Invalid option -$OPTARG"
    echo "Usage: ./setcpu.sh [OPTIONS]"
    echo "Options:"
    echo -e " -a:\tSpecify maximum CPU frequency"
    echo -e " -i:\tSpecify minimum CPU frequency"
    exit 1
    ;;
    esac
    done

    if [ $max_freq -gt $max_cap ]; then
    echo "ERROR: Maximum CPU frequency $max_freq is higher than the highest value you can set: $max_cap"
    exit 2
    fi

    if [ $min_freq -lt $min_cap ]; then
    echo "ERROR: Minimum CPU frequency $min_freq is lower than the lowest value you can set: $min_cap"
    exit 2
    fi

    if [ $max_freq -gt 1710000 ]; then
    echo "WARNING: Maximum CPU frequency $max_freq is a bit too high"
    fi

    # set one core affects cpu 0, 1, 2, 3 simultaneously
    su -c "echo $max_freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"
    su -c "echo $min_freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq"

    echo "Maximum CPU frequency is set to `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq`"
    echo "Minimum CPU frequency is set to `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq`"