VectorLinux

Please login or register.

Login with username, password and session length
Advanced search  

News:

Visit our home page for VL info. For support and documentation, visit the Vector Linux Knowledge Center or search the Knowledge Center and this Forum using the search box above.

Author Topic: How to switch cpu freq depending on battery/ac (w/ vcpufreq)  (Read 2292 times)

wcs

  • Packager
  • Vectorian
  • ****
  • Posts: 1144
How to switch cpu freq depending on battery/ac (w/ vcpufreq)
« on: March 08, 2009, 01:43:50 pm »

With laptops, I found myself going to vcpufreq often, usually to put it in "ondemand" when on battery, and sometimes "performance" when on AC power. Not that "ondemand" doesn't do the job properly, but you might want it to go at full speed all the time.

A way to automate something like this is to create a script that checks the current state (battery or AC) and asks vcpufreq to go into the desired cpu frequency mode.

I put the following in a file called acpicpu:
Code: [Select]
#!/bin/sh                                                                                                                         
status=$(grep -o off-line /proc/acpi/ac_adapter/ACAD/state)
                                                                                                                             
if [ "$status" = "off-line" ]
then
              sed -i 's|\(Setting.ScalingGov=\).*|\1"ondemand"|' /etc/vcpufreq.conf
              /sbin/vcpufreq-loader.gambas
else   
              sed -i 's|\(Setting.ScalingGov=\).*|\1"performance"|' /etc/vcpufreq.conf
              /sbin/vcpufreq-loader.gambas
fi

Line number 2 is checking if the power cable is connected or not. You should change this to the right directory in your system (some have AC, I think, rather than ACAD). Also check if the state file says "off-line" when on battery.

The other lines then replace whatever governor is currently in vcpufreq's configuration file to what you desire when on battery or not: here it changes to "ondemand" when off-line (i.e, battery) and "performance" otherwise (i.e., cable). Then vcpufreq is called to change to the new configuration.

As root, I copied this new file to /usr/local/bin, and changed its permissions so that only root can execute it:
Code: [Select]
chmod u+x /usr/local/bin/acpicpu
A way to automate this when plugging or unplugging the power cable is to create another file with the following:
Code: [Select]
event=battery.*
action=/usr/local/bin/acpicpu

I copied this file inside /etc/acpi/events/ and called it cpu (though it can have any other name).

Finally, I wanted to have this run on boot, rather than having vcpufreq go with the previous configuration. That is, I wanted the right cpu governor when I boot the laptop with the cable on or off.

So I clicked "load on boot" in vcpufreq, but changed /etc/rc.d/rc.local.
Instead of this line at the end of rc.local:
Code: [Select]
/sbin/vcpufreq-loader.gambas
I commented the vcpufreq line and call the new script instead:
Code: [Select]
# vcpufreq is called by the acpiscript, according to AC state
#/sbin/vcpufreq-loader.gambas
/usr/local/bin/acpicpu

If you do all 3 steps, you can still use vcpufreq to change the governor manually.
Just remember that the governor will automatically change if the cable is plugged or unplugged, and your next configuration on boot will depend on whether you're running on battery and not on the last configuration you chose in vcpufreq.

Also, for this stuff to work, make sure the acpi daemon is running (VasmCC > Hardware > Hardware Set).

EDIT: To be fair, I don't find much use in running in "perfomance" whenever I'm on AC power. It has the disadvantage of making my fan spin much louder. The governors can be changed, though. Say, "ondemand" when on AC, and "powersave" when on battery. Just adjust the acpicpu script above.
« Last Edit: March 08, 2009, 01:56:15 pm by wcs »
Logged

Joe1962

  • Administrator
  • Vectorian
  • *****
  • Posts: 2499
    • Joe1962's Website
Re: How to switch cpu freq depending on battery/ac (w/ vcpufreq)
« Reply #1 on: March 10, 2009, 08:14:31 am »

This is very cool wcs! I will give some thought, time permitting, to integrating this in vcpufreq.
Logged
O'Neill (RE the Asgard): "Usually they ask nicely before they ignore us and do what they damn well please."
http://joe1962.bigbox.info
Running: VL 7 Std 64 + self-cooked XFCE-4.10

wcs

  • Packager
  • Vectorian
  • ****
  • Posts: 1144
Re: How to switch cpu freq depending on battery/ac (w/ vcpufreq)
« Reply #2 on: March 10, 2009, 09:58:55 am »

Quote
This is very cool wcs! I will give some thought, time permitting, to integrating this in vcpufreq.

That's great!
Thanks, Joe.
Logged