Difference between revisions of "Power Management"

From gr0x0rd
Jump to navigation Jump to search
 
(14 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
If you have a laptop, you will probably want to make a few measures to extend your battery life, such as throttling down your CPU, dimming your LCD backlight, and other things. In this case you will want to make sure the '''laptop''' USE flag is set in your ''/etc/make.conf''. If you have a dell laptop, you will also want to add the '''dell''' USE flag. Chances are you have already built some of the packages that accept these USE flags, so you may want to rebuild all affected packages on your system.  
 
If you have a laptop, you will probably want to make a few measures to extend your battery life, such as throttling down your CPU, dimming your LCD backlight, and other things. In this case you will want to make sure the '''laptop''' USE flag is set in your ''/etc/make.conf''. If you have a dell laptop, you will also want to add the '''dell''' USE flag. Chances are you have already built some of the packages that accept these USE flags, so you may want to rebuild all affected packages on your system.  
 
  '''$''' sudo emerge -avDN world
 
  '''$''' sudo emerge -avDN world
 +
Before emerging any new packages, you may also want to confirm you have everything you need enabled in your kernel settings.
 +
<pre>
 +
Power management and ACPI options  --->
 +
  [*] ACPI (Advanced Configuration and Power Interface) Support  --->
 +
  ...
 +
  <*>  AC Adapter                                                                                 
 +
  <*>  Battery                                                                                     
 +
  <*>  Button                                                                                     
 +
  <*>  Video                                                                                     
 +
  <*>  Fan                                                                                       
 +
  ...                                                                                   
 +
  <*>  Processor                                                                                 
 +
  < >    Processor Aggregator                                                                     
 +
  <*>    Thermal Zone
 +
</pre>
 
If it hasn't been installed already, you'll want to install the acpi (Advanced Configuration and Power Interface) daemon.
 
If it hasn't been installed already, you'll want to install the acpi (Advanced Configuration and Power Interface) daemon.
  '''$''' sudo emerge -av apcid
+
  '''$''' sudo emerge -av acpid
 
Once the package is emerged, we'll start the apci daemon and add it to startup.
 
Once the package is emerged, we'll start the apci daemon and add it to startup.
 
  '''$''' sudo /etc/init.d/acpid start
 
  '''$''' sudo /etc/init.d/acpid start
 
  '''$''' sudo rc-update add acpid default
 
  '''$''' sudo rc-update add acpid default
The next we'll create a "battery" runlevel for your system.  
+
The Gentoo power management guide suggests you create a battery runlevel to manage your system while running on battery mode. The drawback to this is having to manage multiple runlevels, and having to add new services to both runlevels. Thankfully, power management can be done fairly easily using the default script provided.
  '''$''' cd /etc/runlevels
+
  '''$''' sudo nano -w /etc/acpi/default.sh
'''$''' sudo cp -a default battery
+
=== /etc/acpi/default.sh ===
This action creates a copy of the default runlevel called battery. ''From now on, whenever you add a service to the default runlevel, if you want it to start up when you are running only on your battery, you will need to add it to the battery runlevel as well.'' The next step will be to create a script that can intercept from the acpid whether or not your system is running on batteries and be able to switch to the appropriate runlevel. This script was blatantly stolen from the gentoo handbook.
+
 
'''$''' sudo mkdir /etc/acpi/actions
+
By default, the only action set for the acpi deamon is to initiate shutdown when the power button is pressed. This isn't ideal, especially from within X, where you can use features from your window manager to control your power settings; it's much better to ask what to do in this case. Comment the line containing ''/sbin/init 0'' out, and when doing so, be sure to add a comment as to why.
'''$''' sudo nano -w /etc/acpi/actions/pmg_switch_runlevel.sh
+
...
=== /etc/acpi/actions/pmg_switch_runlevel.sh ===
+
button)
#!/bin/bash
+
  case "$action" in
 +
  power)
 +
    # the following line commented out so xfce power manager prompts for action when power button is pressed
 +
    #      /sbin/init 0
 +
    ;;
 +
...
 +
If you are using a laptop, chances are you will want to perform some actions when you close the lid, such as turn off the display or go into sleep mode by suspending the system to ram. This can be done manually in this file, but most users will find it preferable to leverage a graphical application to manage detailed power settings. However, you can manually set commands by editing the corresponding section. I have added some commented commands for reference.
 +
...
 +
  lid)
 +
    #      xset dpms force off
 +
    #      if [ "x" != "x$(grep -o open /proc/acpi/button/lid/LID/state)" ]
 +
    #              logger "lid opened"
 +
    #        fi
 +
    #      /usr/sbin/pm-suspend
 +
    ;;
 +
...
 +
If you followed the advice in the [[CPU_Frequency_scaling | CPU frequency scaling]] section, you probably chose to use the ''ondemand'' CPU governor, which will provide more power to the CPU as needed. In the case that you'd rather throttle the CPU to its lowest level when running on batteries, you'll want to edit the ''ac_adapter'' section of the file. The following example also dims the LCD backlight when running on battery, and restores it to full brightness when running on ac power. Substitute for the location of your LCD brightness setting file. You may need to explore the subfolders of ''/proc/acpi/video/'' for a file called ''brightness'' to find yours. You can use the command ''cat brightness'' to see the levels you can set.
 +
...
 +
ac_adapter)
 +
  case "$value" in
 +
  # Add code here to handle when the system is unplugged
 +
  *0)
 +
    cpufreq-set -g powersave
 +
    logger "Running on battery."
 +
    echo -n 50 > /proc/acpi/video/GFX0/LCDD/brightness
 +
    ;;
 
   
 
   
# BEGIN configuration
+
  # Add code here to handle when the system is plugged in
RUNLEVEL_AC="default"
+
  *1)
RUNLEVEL_BATTERY="battery"
+
    cpufreq-set -g ondemand 
# END configuration
+
    logger "Running on ac power."
+
    echo -n 100 > /proc/acpi/video/GFX0/LCDD/brightness
if [ ! -d "/etc/runlevels/${RUNLEVEL_AC}" ]
+
    ;;
then
+
  ...
    logger "${0}: Runlevel ${RUNLEVEL_AC} does not exist. Aborting."
+
After making any changes to the ''default.sh'' file, it is wise to restart the acpi daemon.
    exit 1
+
  '''$''' sudo /etc/init.d/acpid restart
fi
+
When running xfce, managing power actions and events can be simplified by leveraging ''xfce4-power-manager''.
+
  '''$''' sudo emerge -av xfce4-power-manager
if [ ! -d "/etc/runlevels/${RUNLEVEL_BATTERY}" ]
+
Once the emerge has completed, you can access your power settings via Settings -> Xfce 4 Power Manager from your menu. You can easily set the icon to display on ac or battery power, and set actions for things such as closing the lid when the ac adapter is unplugged, or after running on batteries for a certain time period.
then
 
    logger "${0}: Runlevel ${RUNLEVEL_BATTERY} does not exist. Aborting."
 
    exit 1
 
fi
 
 
if on_ac_power
 
then
 
    if [[ "$(</var/lib/init.d/softlevel)" != "${RUNLEVEL_AC}" ]]
 
    then
 
        logger "Switching to ${RUNLEVEL_AC} runlevel"
 
          /sbin/rc ${RUNLEVEL_AC}
 
    fi
 
  elif [[ "$(</var/lib/init.d/softlevel)" != "${RUNLEVEL_BATTERY}" ]]
 
then
 
    logger "Switching to ${RUNLEVEL_BATTERY} runlevel"
 
    /sbin/rc ${RUNLEVEL_BATTERY}
 
fi
 
We'll also need to make this script executable.
 
'''$''' chmod 755 /etc/acpi/actions/pmg_switch_runlevel.sh
 
Next we will need to determine what events are thrown when you unplug or plug in your AC adapter.  
 
  '''$''' sudo tail -f /var/log/messages | grep "ACPI event"
 
You should see something like this:
 
Dec  7 18:10:30 moonbase3 logger: ACPI event unhandled: ac_adapter AC0 00000080 00000000
 
Dec  7 18:10:30 moonbase3 logger: ACPI event unhandled: processor CPU0 00000081 00000000
 
Dec  7 18:10:31 moonbase3 logger: ACPI event unhandled: processor CPU1 00000081 00000000
 
Dec  7 18:10:31 moonbase3 logger: ACPI event unhandled: processor CPU2 00000081 00000000
 
Dec  7 18:10:31 moonbase3 logger: ACPI event unhandled: processor CPU3 00000081 00000000
 
Dec  7 18:10:31 moonbase3 logger: ACPI event unhandled: battery BAT0 00000080 00000001
 
Dec  7 18:10:33 moonbase3 logger: ACPI event unhandled: ac_adapter AC0 00000080 00000001
 
Dec  7 18:10:33 moonbase3 logger: ACPI event unhandled: processor CPU0 00000081 00000000
 
Dec  7 18:10:33 moonbase3 logger: ACPI event unhandled: processor CPU1 00000081 00000000
 
Dec  7 18:10:33 moonbase3 logger: ACPI event unhandled: processor CPU2 00000081 00000000
 
Dec  7 18:10:34 moonbase3 logger: ACPI event unhandled: processor CPU3 00000081 00000000
 
Dec  7 18:10:34 moonbase3 logger: ACPI event unhandled: battery BAT0 00000080 00000001
 
What we're interested in is the info for the AC adapter and battery. We can see in the output above that events for the AC adapter are called ac_adapter, and battery events are called, well, battery. We'll create two files in ''/etc/acpi/actions/'' to intercept the events.  
 
  '''$''' sudo nano -w /etc/acpi/actions/pmg_ac_adapter
 
Add the following from the gentoo handbook:
 
# replace "ac_adapter" below with the event generated on your laptop
 
# For example, ac_adapter.* will match ac_adapter AC 00000080 00000000
 
event=ac_adapter.*
 
action=/etc/acpi/actions/pmg_switch_runlevel.sh %e
 
Do the same for the battery.
 
'''$''' sudo nano -w /etc/acpi/actions/pmg_battery
 
Add the following from the gentoo handbook:
 
# replace "battery" below with the event generated on your laptop
 
# For example, battery.* will match battery BAT0 00000080 00000001
 
event=battery.*
 
action=/etc/acpi/actions/pmg_switch_runlevel.sh %e
 
We'll need to restart the daemon for the changes to take effect.
 

Latest revision as of 20:04, 8 January 2011

Configuring Hardware - Power Management

If you have a laptop, you will probably want to make a few measures to extend your battery life, such as throttling down your CPU, dimming your LCD backlight, and other things. In this case you will want to make sure the laptop USE flag is set in your /etc/make.conf. If you have a dell laptop, you will also want to add the dell USE flag. Chances are you have already built some of the packages that accept these USE flags, so you may want to rebuild all affected packages on your system.

$ sudo emerge -avDN world

Before emerging any new packages, you may also want to confirm you have everything you need enabled in your kernel settings.

 Power management and ACPI options  --->
  [*] ACPI (Advanced Configuration and Power Interface) Support  --->
   ...
   <*>   AC Adapter                                                                                  
   <*>   Battery                                                                                      
   <*>   Button                                                                                      
   <*>   Video                                                                                       
   <*>   Fan                                                                                         
   ...                                                                                     
   <*>   Processor                                                                                  
   < >     Processor Aggregator                                                                       
   <*>     Thermal Zone 

If it hasn't been installed already, you'll want to install the acpi (Advanced Configuration and Power Interface) daemon.

$ sudo emerge -av acpid

Once the package is emerged, we'll start the apci daemon and add it to startup.

$ sudo /etc/init.d/acpid start
$ sudo rc-update add acpid default

The Gentoo power management guide suggests you create a battery runlevel to manage your system while running on battery mode. The drawback to this is having to manage multiple runlevels, and having to add new services to both runlevels. Thankfully, power management can be done fairly easily using the default script provided.

$ sudo nano -w /etc/acpi/default.sh

/etc/acpi/default.sh

By default, the only action set for the acpi deamon is to initiate shutdown when the power button is pressed. This isn't ideal, especially from within X, where you can use features from your window manager to control your power settings; it's much better to ask what to do in this case. Comment the line containing /sbin/init 0 out, and when doing so, be sure to add a comment as to why.

...
button)
 case "$action" in
  power)
   # the following line commented out so xfce power manager prompts for action when power button is pressed
   #       /sbin/init 0
   ;;
...

If you are using a laptop, chances are you will want to perform some actions when you close the lid, such as turn off the display or go into sleep mode by suspending the system to ram. This can be done manually in this file, but most users will find it preferable to leverage a graphical application to manage detailed power settings. However, you can manually set commands by editing the corresponding section. I have added some commented commands for reference.

...
  lid)
   #       xset dpms force off
   #       if [ "x" != "x$(grep -o open /proc/acpi/button/lid/LID/state)" ]
   #               logger "lid opened"
   #        fi
   #       /usr/sbin/pm-suspend
   ;;
...

If you followed the advice in the CPU frequency scaling section, you probably chose to use the ondemand CPU governor, which will provide more power to the CPU as needed. In the case that you'd rather throttle the CPU to its lowest level when running on batteries, you'll want to edit the ac_adapter section of the file. The following example also dims the LCD backlight when running on battery, and restores it to full brightness when running on ac power. Substitute for the location of your LCD brightness setting file. You may need to explore the subfolders of /proc/acpi/video/ for a file called brightness to find yours. You can use the command cat brightness to see the levels you can set.

...
ac_adapter)
 case "$value" in
  # Add code here to handle when the system is unplugged
  *0)
   cpufreq-set -g powersave
   logger "Running on battery."
   echo -n 50 > /proc/acpi/video/GFX0/LCDD/brightness
   ;;

  # Add code here to handle when the system is plugged in
  *1)
   cpufreq-set -g ondemand   
   logger "Running on ac power."
   echo -n 100 > /proc/acpi/video/GFX0/LCDD/brightness
   ;;
...

After making any changes to the default.sh file, it is wise to restart the acpi daemon.

$ sudo /etc/init.d/acpid restart

When running xfce, managing power actions and events can be simplified by leveraging xfce4-power-manager.

$ sudo emerge -av xfce4-power-manager

Once the emerge has completed, you can access your power settings via Settings -> Xfce 4 Power Manager from your menu. You can easily set the icon to display on ac or battery power, and set actions for things such as closing the lid when the ac adapter is unplugged, or after running on batteries for a certain time period.