Repository 32bit  Forum
Repository 64bit  Wiki

Cpuscaling

Da Slacky.eu.
Versione delle 10:46, 12 set 2006, autore: L1q1d (Discussione | contributi)

(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

Descrizione

Cpuscaling, script che permette di settare (tramite scrittura sul sysfs) un determinato range di frequenze per la cpu oppure di selezionare una modalità di comportamento predefinita tra quelle disponibili dal kernel, le quali vanno dal risparmio dell'energia (powersave), al pieno sfruttamento della cpu (performance), passando per governor intermedi che aumentano la frequenza della cpu in relazione ai programmi in esecuzione. Ovviamente tali feature devono essere presenti nel kernel (anche non built-in, ci pensa lo script a caricare i moduli).

Script

#!/bin/bash
#
# cpuscaling v. 0.0.2
# by Sergio Perticone <gall0ws [at] tiscali [dot] it>
#
# cpuscaling allows you to change the clock speed of CPU on the fly.
#
# suggest: put this in /usr/sbin
#
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#
# for limited user(s) you can use sudo(8), it works fine ;)
#
# add in your /etc/sudoers something like this:
#  -------------------------------------------------------
#   your_user  ALL=(ALL)   NOPASSWD: /usr/sbin/cpuscaling 
#  -------------------------------------------------------
# so:
#     $ sudo /usr/sbin/cpuscaling [ options ]
#
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#
# oh yes, it's (dirty) GPL code: <http://www.gnu.org/licenses/gpl.txt> 
#

# set some variables:
SYSDIR="/sys/devices/system/cpu/cpu0/cpufreq"
AVAILABLE_GOVERNORS="$SYSDIR/scaling_available_governors"
GOVERNOR="$SYSDIR/scaling_governor"
FREQMAX="$SYSDIR/scaling_max_freq"
FREQMIN="$SYSDIR/scaling_min_freq"
FREQCUR="$SYSDIR/scaling_cur_freq"
FREQS="$SYSDIR/scaling_available_frequencies"

function usage() {

  cat << END
Usage:
 cpuscaling [ governor ] | [ -f, --freqs ] | [ -M, --fmin ] | [ -m, --fmin ] FREQ 

  conservative		Tune governor to conservative
  ondemand		Tune governor to ondemand
  performance		Tune governor to performance
  powersave		Tune governor to powersave
  userspace		Tune governor to userspace

  -f, --freqs		Print frequences supported by your CPU
  -M, --fmax FREQ	Set max frequence
  -m, --fmin FREQ	Set min frequence
  -s, --state		Show current state

  -h, --help		Print this help and exit

Report bugs to <gall0ws [at] tiscali [dot] it>.
END
}

function only_root__exit() {

  echo "Only superuser can do it."
  exit 1
}

function something_wrong__exit() {

  echo "Failed. Please check your kernel settings."
  exit 1
}

function no_freq__exit() {

  echo "Please choose a frequence."
  exit 1
}

function show_state() {

  printf "governor:\t `cat $GOVERNOR`\n"
  printf "current :\t `cat $FREQCUR` Hz\n"

  [ "$GOVERNOR" == "performance" ] && exit 0

  [ "$GOVERNOR" == "conservative" ] && {
     printf "freq:\t `cat $FREQMIN` Hz\n"
     exit 0
  }
 
  printf "freq min:\t `cat $FREQMIN` Hz\n"
  printf "freq max:\t `cat $FREQMAX` Hz\n"
}

function show_freqs() {

  cat $FREQS
  exit 0
}

function freq_min() {

 [ "$UID" != "0" ] && only_root__exit

  echo -n $FREQ > $FREQMIN
  show_state
  exit 0
}

function freq_max() {

 [ "$UID" != "0" ] && only_root__exit

  echo -n $FREQ > $FREQMAX 
  show_state
  exit 0
}

function set_governor() {

  [ "$UID" != "0" ] && only_root__exit

# if governor seems not to be available then I try to load module:
  grep -q $NEW_GOVERNOR $AVAILABLE_GOVERNORS || {	      
    /sbin/modprobe cpufreq_$NEW_GOVERNOR || something_wrong__exit # mmm, check your config :/
  }
  echo -n $NEW_GOVERNOR > $GOVERNOR
  exit 0
}

# parser:
case "$1" in

'conservative'|'ondemand'|'performance'|'powersave'|'userspace')
  NEW_GOVERNOR=$@
  set_governor
  ;;

'--freqs'|'-f')
  show_freqs
  ;;

'--fmax'|'-M')
  [ -z "$2" ] && no_freq__exit
  FREQ="$2"
  freq_max
  ;;

'--fmin'|'-m')
  [ -z "$2" ] && no_freq__exit
  FREQ="$2" 
  freq_min
  ;;

'--state'|'-s')
  show_state
  ;;

'--help'|'-h'|'-wtf')
  usage
  ;;

'')
  echo "cpuscaling: too few arguments"
  echo "Try \`cpuscaling --help' for more information."
  ;;

*)
  echo "cpuscaling: invalid option -- $1"
  echo "Try \`cpuscaling --help' for more information." 

esac

## EOF ##

  • Data: 03 Jun 2006
  • Autore: Gallows
  • Versione: 0.0.2
Strumenti personali
Namespace

Varianti