howto - switch off powersaving automagically when on psu

Discussion in 'Linux' started by HappyHarry, Mar 1, 2009.

  1. HappyHarry

    HappyHarry

    Joined:
    Nov 28, 2008
    Messages:
    159
    Likes Received:
    0
    hi guys, i know that when using the battery powersaving options are great for lengthening the operating time you have and i'm all for that :cool: but when your connected to the psu these powersaving options hamper your system performance and i'm not for that at all :-D So with this in mind i have written a little script that gets called when the psu is connected or disconnected and changes some of the powersaving options to suitable levels. firstly i need to point out that if you have any of these options i use in my script set in your init scripts you will need to comment them out or remove them for this script to work. first creatle a file with the following as it's contents and save it in /etc/acpi/events/psu.conf
    Code:
    # this is an acpi event watcher to turn on/off powersaving 
    # options when the psu is present or not.
    event=ac_adapter.*
    action=/usr/bin/aao-powertweaks.sh 
    
    then create another file with the following as it's content and save it in /usr/bin/aao-powertweaks.sh
    Code:
    #!/bin/sh
    
    # On Battery
    if [[ "`cat /proc/acpi/ac_adapter/ACAD/state|grep "off-line"|wc -l`" == "1" ]]; then	
      logger "Turning on powersaving measures"
      # Uncomment the next line and replace "yourhomedir" with your own home directory name to check the script is working
      # date >> /home/yourhomedir/acpilog ; echo battery >> /home/yourhomedir/acpilog	
      # enable scaling on both cores
      echo "ondemand" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
      echo "ondemand" > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor
      # tweak ondemand settings -- over 60% cpu usage will up the speed (default is 80%)
      echo 60 > /sys/devices/system/cpu/cpu0/cpufreq/ondemand/up_threshold
      echo 60 > /sys/devices/system/cpu/cpu1/cpufreq/ondemand/up_threshold
      # tweak sampling rate - /sys/devices/system/cpu/cpu0/cpufreq/ondemand/sampling_rate_max
      echo 800000 > /sys/devices/system/cpu/cpu0/cpufreq/ondemand/sampling_rate
      # laptop mode on (write to hd less often)
      echo 5 > /proc/sys/vm/laptop_mode
      # hd click (stop hard drive death click)
      /sbin/hdparm -B 200 /dev/sda >/dev/null 2>&1
      # longer writebacks
      echo 1500 > /proc/sys/vm/dirty_writeback_centisecs
      # turn on multicore tunable
      echo 1 > /sys/devices/system/cpu/sched_smt_power_savings
    fi
    
    # On PSU
    if [[ "`cat /proc/acpi/ac_adapter/ACAD/state|grep "on-line"|wc -l`" == "1" ]]; then
      logger "Turning off powersaving measures"
      # Uncomment the next line and replace "yourhomedir" with your own home directory name to check the script is working
      # date >> /home/yourhomedir/acpilog ; echo psu >> /home/yourhomedir/acpilog
      # disable freq scaling on both cores
      echo "performance" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
      echo "performance" > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor
      # laptop mode off (write to hd when required)
      echo 0 > /proc/sys/vm/laptop_mode
      # hd click (stop hard drive death click)
      /sbin/hdparm -B 254 /dev/sda >/dev/null 2>&1
      # shorter writebacks
      echo 500 > /proc/sys/vm/dirty_writeback_centisecs
      #turn off multicore tunable
      echo 0 > /sys/devices/system/cpu/sched_smt_power_savings
    fi
    
    then open a terminal and type,
    Code:
    su -c "chmod +x /usr/bin/aao-powertweaks.sh"
    
    then enter your root password. this makes the script executable.

    in the script you can see that in both sections i have added a couple of lines of code for debugging, when you are saving the file remember to uncomment the line and also replace "yourhomedir" with the actual name of your home dir, for instance mine reads

    date >> /home/phil/acpilog ; echo battery >> /home/phil/acpilog

    once you have it up and running you can delete these lines or comment them back out. once you have the files in place re-boot your system and when back to the desktop plug in and unplug the psu and check the acpilog file to see if it's working, if you have done everything correctly it should be working fine, if not start at the begining and try again ;-).

    if anyone would like to suggest any other tuning options to include in either section please feel free, but be sure to share them here so others (and me of course lol) can gain from them :)

    i hope this is usefull to some of you
     
    HappyHarry, Mar 1, 2009
    #1
  2. HappyHarry

    HappyHarry

    Joined:
    Nov 28, 2008
    Messages:
    159
    Likes Received:
    0
    has anyone else tried this? i also have a way to get these settings changed at bootup time too using a hal callout, if anyone is interested i'll post how to do that too :)
     
    HappyHarry, Mar 4, 2009
    #2
  3. HappyHarry

    HappyHarry

    Joined:
    Nov 28, 2008
    Messages:
    159
    Likes Received:
    0
    this is still working perfectly for me :cool: has anyone else tried it out yet?

    phil
     
    HappyHarry, Jun 4, 2009
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.