You are not logged in.

#1 2012-01-25 19:26:52

noMaster
Member
Registered: 2009-07-05
Posts: 70

[SOLVED] Help with a simple script

Hi guys! A problem is connected with switching cpu governors. Usually I set it by commands and made alias in .bashrc:

sudo cpufreq-set -c 0 -g performance && sudo cpufreq-set -c 1 -g performance
sudo cpufreq-set -c 0 -g ondemand && sudo cpufreq-set -c 1 -g ondemand
sudo cpufreq-set -c 0 -g powersave && sudo cpufreq-set -c 1 -g powersave

Could you help me with script which can read system information about current cpu governor and switches the next governor

Last edited by noMaster (2012-01-27 06:36:55)

Offline

#2 2012-01-25 19:38:20

eldragon
Member
From: Buenos Aires
Registered: 2008-11-18
Posts: 1,029

Re: [SOLVED] Help with a simple script

can i ask why?

here you can read the current governor for cpu0

/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

work from there wink

Offline

#3 2012-01-25 19:42:04

JuseBox
Member
Registered: 2009-11-27
Posts: 260

Re: [SOLVED] Help with a simple script

Can you please be more specific with what you are trying to accomplish?


Linux ArchLinux 3.2.8-1-ARCH
#1 SMP PREEMPT Mon Feb 27 21:51:46 CET 2012 x86_64 AMD FX(tm)-8120 Eight-Core Processor AuthenticAMD GNU/Linux
8192MB DDR3 1300MHz | Asus m5a97 | GeForce GTX 550 Ti | 120 GB SSD

Offline

#4 2012-01-26 05:09:59

noMaster
Member
Registered: 2009-07-05
Posts: 70

Re: [SOLVED] Help with a simple script

JuseBox wrote:

Can you please be more specific with what you are trying to accomplish?

I need that script for set it on hotkey and change cpu states. Unfortunately I havent scripting experience

I make one thing but it doesnt work for me

### Toggle Super Hybrid Engine ##################################
she_names=("performance" "ondemand" "powersave")
she_value_performance=(	"0"	"performance")
she_value_ondemand=(	"1"	"ondemand")
she_value_powersave=(	"2" "powersave")

# Find current
she_current=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor`

if   [[ "${she_current}" == "${she_value_performance[0]}" ]]; then
    she_next=${she_value_ondemand[0]}
elif [[ "${she_current}" == "${she_value_ondeman[1]}" ]]; then
    she_next=${she_value_powersave[0]}
elif [[ "${she_current}" == "${she_value_powersave[1]}" ]]; then
    she_next=${she_value_performance[0]}
fi

function she_toggle() {
    if [[ "${she_names[${she_current}]}" != "${she_names[${she_next}]}" ]]; then
		cmd="/usr/bin/notify-send -t 5000 SHE changed"
        echo ${she_next} > ${she_current}
    else
        cmd="/usr/bin/notify-send -t 5000 SHE original"
	fi
}

#################################################################
case $1 in
#    "debug")
#        print_debug
#    ;;
    "powersave")
        she_next=2
        she_toggle
    ;;
    "normal")
        she_next=1
        she_toggle
    ;;
    "performance")
        she_next=0
        she_toggle
    ;;
    *)
        she_toggle
    ;;
esac

### End of file #################################################

Last edited by noMaster (2012-01-26 08:46:30)

Offline

#5 2012-01-26 14:51:07

JuseBox
Member
Registered: 2009-11-27
Posts: 260

Re: [SOLVED] Help with a simple script

noMaster wrote:
JuseBox wrote:

Can you please be more specific with what you are trying to accomplish?

I need that script for set it on hotkey and change cpu states. Unfortunately I havent scripting experience

I make one thing but it doesnt work for me

### Toggle Super Hybrid Engine ##################################
she_names=("performance" "ondemand" "powersave")
she_value_performance=(	"0"	"performance")
she_value_ondemand=(	"1"	"ondemand")
she_value_powersave=(	"2" "powersave")

# Find current
she_current=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor`

if   [[ "${she_current}" == "${she_value_performance[0]}" ]]; then
    she_next=${she_value_ondemand[0]}
elif [[ "${she_current}" == "${she_value_ondeman[1]}" ]]; then
    she_next=${she_value_powersave[0]}
elif [[ "${she_current}" == "${she_value_powersave[1]}" ]]; then
    she_next=${she_value_performance[0]}
fi

function she_toggle() {
    if [[ "${she_names[${she_current}]}" != "${she_names[${she_next}]}" ]]; then
		cmd="/usr/bin/notify-send -t 5000 SHE changed"
        echo ${she_next} > ${she_current}
    else
        cmd="/usr/bin/notify-send -t 5000 SHE original"
	fi
}

#################################################################
case $1 in
#    "debug")
#        print_debug
#    ;;
    "powersave")
        she_next=2
        she_toggle
    ;;
    "normal")
        she_next=1
        she_toggle
    ;;
    "performance")
        she_next=0
        she_toggle
    ;;
    *)
        she_toggle
    ;;
esac

### End of file #################################################

I am looking over the script. Without it, do you think you could explain what your trying to accomplish? What states and when should they toggle? Something rough to get an idea? I think if I am thinking correctly this could be easier then it seems. I will help ya.


Linux ArchLinux 3.2.8-1-ARCH
#1 SMP PREEMPT Mon Feb 27 21:51:46 CET 2012 x86_64 AMD FX(tm)-8120 Eight-Core Processor AuthenticAMD GNU/Linux
8192MB DDR3 1300MHz | Asus m5a97 | GeForce GTX 550 Ti | 120 GB SSD

Offline

#6 2012-01-27 01:08:39

noMaster
Member
Registered: 2009-07-05
Posts: 70

Re: [SOLVED] Help with a simple script

I need that script provides switching cpu governor and that's all I need

Offline

#7 2012-01-27 01:35:22

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,033

Re: [SOLVED] Help with a simple script

You misspelled ondemand

elif [[ "${she_current}" == "${she_value_ondeman[1]}" ]]; then

You're just jealous because the voices only talk to me.

Offline

#8 2012-01-27 01:42:15

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED] Help with a simple script

#!/bin/bash

governors=(performance ondemand powersave)
performance=0 ondemand=1 powersave=2

gov_get() {
  cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
}

gov_set() {
  <<<"$1" tee | /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor >/dev/null
  notify-send "governor set to: $1"
}

gov_next() {
  local current next
  current=$(gov_get)
  next=${!current}
  printf '%s\n' ${governors[(next + 1) % 3]}
}

case $1 in
  performance|ondemand|powersave)
    gov_set "$1"
    ;;
  toggle)
    gov_set "$(gov_next)"
    ;;
  *)
    printf 'usage: %s <performance|ondemand|powersave|toggle>\n' "${0##*/}"
    printf '\ncurrent governor is: %s\n' "$(gov_get)"
    ;;
esac

Untested.

Offline

#9 2012-01-27 06:36:16

noMaster
Member
Registered: 2009-07-05
Posts: 70

Re: [SOLVED] Help with a simple script

falconindy, thank you for your script, it seems  very good, but it unfortunately doesnt work for me

sudo /home/ku/others/she.sh powersave
/home/ku/others/she.sh: line 11: /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor: Permission denied

I made own and it works, really amazing! My first script smile

###Governors
p1=("performance")
p2=("ondemand")
p3=("powersave")

### Current cpu governor
she_current=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor`

if   [[ "${she_current}" == "${p1}" ]]; then
		cpufreq-set -c 0 -g ondemand && cpufreq-set -c 1 -g ondemand
elif [[ "${she_current}" == "${p2}" ]]; then
		cpufreq-set -c 0 -g powersave && cpufreq-set -c 1 -g powersave
elif [[ "${she_current}" == "${p3}" ]]; then
		cpufreq-set -c 0 -g performance && cpufreq-set -c 1 -g performance
fi
moetunes wrote:

You misspelled ondemand

elif [[ "${she_current}" == "${she_value_ondeman[1]}" ]]; then

And, moetunes, especially thanks! When I made my script the problem was in that line. I read you post and fixed and it works for me.

Thank you all, guys

Offline

Board footer

Powered by FluxBB