ubuntu 笔记本电源管理优化指南

系统 3634 0

ubuntu 笔记本电源管理优化指南

作者:yuk
版本:1.0
免责声明:以下操作可能会引起数据丢失,本人不承担任何责任。
版权声明:可以自由转载,但原作者请务必保留;

论坛里看到许多朋友抱怨linux电源管理很糟糕,说什么电池使用比win下短了很多。实际上linux提供很多电源优化的功能,只不过默认情况下的设置没有考虑电源使用罢了。下面我以ubuntu edgy为例,系统的讨论一下笔记本电源管理优化。
目标:最长的电池使用时间

概述
笔记本由许多不同的组件组成,各个组件的耗电不用,引用gentoo power management的一张图,其耗电比重如下图所示:
Power Budget For Each Component

 

 

 

 

 

 

 

 

 

 

 

 

从上图我们可以看到主要的耗电部件是液晶显示器、CPU、芯片组和硬盘。虽然我们可以在BIOS里面设置操作系统无关的电源管理模式,但是在操作系统之上我们可以设置一些更smart的电源管理模式自动适应各种环境。
本文分下面几个部分分别讨论各个部件的电源管理优化。

0. 前提
在讨论各个设备的电源管理优化之前,先确定我们是否满足下面一些前提条件。

BOIS
首先你要检查一下你的BOIS的电源管理设置,先关闭所有bois支持的电源管理方式,只通过操作系统设置电源管理,然后再恢复BOIS的电源管理。
kernel
acpi支持,edgy默认内核是支持acpi的,如果你自定义编译了内核,记住要把电源管理相关的模块选上

     Power Management Options --->
  [*] Power Management Support
  [ ] Software Suspend

  ACPI( Advanced Configuration and Power Interface ) Support --->
    [*] ACPI Support
    [ ]   Sleep States
    [ ]     /proc/acpi/sleep (deprecated)
    [*]   AC Adapter
    [*]   Battery
    <M>   Button
    <M>   Video
    [ ]   Generic Hotkey
    <M>   Fan
    <M>   Processor
    <M>     Thermal Zone
    < >   ASUS/Medion Laptop Extras
    < >   IBM ThinkPad Laptop Extras
    < >   Toshiba Laptop Extras
    (0)   Disable ACPI for systems before Jan 1st this year
    [ ]   Debug Statements
    [*]   Power Management Timer Support
    < >   ACPI0004,PNP0A05 and PNP0A06 Container Driver (EXPERIMENTAL)

  CPU Frequency Scaling --->
    [*] CPU Frequency scaling
    [ ]   Enable CPUfreq debugging
    < >   CPU frequency translation statistics
    [ ]     CPU frequency translation statistics details
          Default CPUFreq governor (userspace)
    <*>   'performance' governor
    <*>   'powersave' governor
    <*>   'ondemand' cpufreq policy governor
    <*>   'conservative' cpufreq governor
    <*>   CPU frequency table helpers
    <M> ACPI Processor P-States driver
    <*> CPUFreq driver for your processor
  

 1.显示器
如上图所示,LCD是最耗电的部件,这一节我们讨论如何降低LCD的耗电
首先,尽可能调低亮度,这个不多说手动调节就可以了;
然后设置 DPMS(Display Power Management Signaling)
修改sudo vi /etc/X11/xorg.conf文件

      Section "ServerLayout"
  Identifier  [...]
  [...]
  Option  "BlankTime"  "5"  # Blank the screen after 5 minutes (Fake)
  Option  "StandbyTime"  "10"  # Turn off screen after 10 minutes (DPMS)
  Option  "SuspendTime"  "20"  # Full suspend after 20 minutes
  Option  "OffTime"  "30"  # Turn off after half an hour
  [...]
  EndSection

  [...]

  Section "Monitor"
  Identifier  [...]
  Option  "DPMS"  "true"
  [...]
  EndSection
  

 但似乎xorg的dpms有bug不能关闭LCD,只能把屏幕变黑
google搜索了一下发现是和acpi冲突只要重新启动acpi就可以了
手动设置屏幕关闭,

      sudo /etc/init.d/acpid restart
  xset dpms force off
  

 2.cpu

移动版本的CPU支持频率和电压的动态调整,在大多数情况下你的CPU是没必要全速运行的,尤其在电池支持下,我们可以强制使CPU运行在最低频率。

在linux 2.6以后的内核就支持cpu频率的动态调整,有下面5种模式

    performance 将CPU频率设定在支持的最高频率,而不动态调节.
    powersave 将CPU频率设置为最低
    ondemand 快速动态调整CPU频率, Pentuim M的CPU可以使用
    conservative 与ondemand不同,平滑地调整CPU频率,适合于用电池工作时.
    userspace 用户模式,也就是长期以来都在用的那个模式。可以通过手动编辑配置文件进行配置

先安装相关软件cpufrequtils

    sudo apt-get install cpufrequtils
  

 查看你的cpu所支持的频率
cpufreq-info

 

code 写道
cpufrequtils 002: cpufreq-info (C) Dominik Brodowski 2004-2006
Report errors and bugs to linux@brodo.de, please.
analyzing CPU 0:
driver: powernow-k8
CPUs which need to switch frequency at the same time: 0
hardware limits: 800 MHz - 1.60 GHz
available frequency steps: 1.60 GHz, 800 MHz
available cpufreq governors: userspace, powersave, ondemand, conservative, performance
current policy: frequency should be within 800 MHz and 1.60 GHz.
The governor "ondemand" may decide which speed to use
within this range.
current CPU frequency is 800 MHz.

 需要进入那个模式直接修改proc文件,比如要进入powersave模式

code 写道
sudo -s
echo powersave > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

 其实有个实用程序laptop-mode可以方便的设置,在后面硬盘一节统一介绍

如果是用迅驰cpu,支持多个频率,你可以用powernowd更细化的调节cpu频率

3.显卡
各类显卡有自己的电源管理方式,我这里主要介绍ati powerplay,
首先要安装ati的fgrlx,请参考论坛wiki
运行aticonfig --lsp显示显卡可以运行的频率,选最低的就行了
我的输出:

code 写道
aticonfig --lsp
core/mem [flags]
-----------------
* 1: 100/150 MHz
2: 301/200 MHz [default state]

aticonfig --set-powerstate=0

 使显卡运行在最低频率上

也可以设置显卡动态调整频率
修改sudo vi /etc/X11/xorg.conf

code 写道
Section "Device"
[...]
Option "DynamicClocks" "on"
EndSection

 4.硬盘
我们先做一个粗略的计算,你的笔记本如果硬盘在节能模式下运行,能延长多少时间。
cat /proc/acpi/battery/BATx/info

写道
present: yes
design capacity: 6000 mAh
last full capacity: 3808 mAh
battery technology: rechargeable
design voltage: 14800 mV
design capacity warning: 209 mAh
design capacity low: 133 mAh
capacity granularity 1: 10 mAh
capacity granularity 2: 25 mAh
model number: JM-6
serial number: xxxxxxxxxxxxxxx
battery type: LION
OEM info: Hewlett-Packard

 电池功率为 3808 mAh x 14800 mV = 56.3584 Wh == 56wh
电池支持时间4小时
移动硬盘正常功率: 2.4w
移动硬盘standby功率: 0.25w
(根据自己型号google自己搜索)
56/4 = 14 w
14 - (2.4-0.25) = 11.85 w
56 / 11.85 = 4.725738397 小时
延长了0.73 小时,大约44分钟
硬盘是不可能一直处于standby状态的,实际的情况肯定会短一些。

下面我们讨论如何使硬盘更长时间的处于standby模式下

我们可以laptop-mode 软件达到这个效果,
首先我们启动laptop-mode,edgy默认设置是禁止laptop-mode运行的
修改 /etc/default/acpi-support

写道
ENABLE_LAPTOP_MODE=true

 sudo /etc/init.d/laptop-mode start


好了现在我们来详细配置,有中文注释的地方是我修改过的地方
修改/etc/laptop-mode/laptop-mode.conf

写道
# Enable laptop mode when on battery power.
# 在电池模式下启动laptop_mode
ENABLE_LAPTOP_MODE_ON_BATTERY=1


# Disable all data loss sensitive features when the battery level (in % of the
# battery capacity) reaches this value.
# 在低电量的情况下禁止laptop-mode
MINIMUM_BATTERY_CHARGE_PERCENT=10

# Disable data loss sensitive features when the battery reports its state
# as "critical".
# 在极底电量下禁止laptop_mode
DISABLE_LAPTOP_MODE_ON_CRITICAL_BATTERY_LEVEL=8

# The drives that laptop mode controls.
# Separate them by a space, e.g. HD="/dev/hda /dev/hdb". The default is a
# wildcard, which will get you all your IDE and SCSI/SATA drives.
# HD="/dev/[hs]d[abcdefgh]"
# 设定需要控制的硬盘
HD="/dev/hda"

# Should laptop mode tools control readahead?
# 启动 readahead
CONTROL_READAHEAD=1

# Read-ahead, in kilobytes. You can spin down the disk while playing MP3/OGG
# by setting the disk readahead to a reasonable size, e.g. 3072 (3 MB).
# Effectively, the disk will read a complete MP3 at once, and will then spin
# down while the MP3/OGG is playing. Don't set this too high, because the
# readahead is applied to _all_ files that are read from disk.
# 设定laptop_mode下的readahead buffer,如果是听mp3 3-5M,如果是看电影 8-10M
LM_READAHEAD=4096
NOLM_READAHEAD=128

# Should laptop mode tools control the maximum CPU frequency?
# 启动CPU频率控制
CONTROL_CPU_FREQUENCY=1

# Legal values are "slowest" for the slowest speed that your
# CPU is able to operate at, "fastest" for the fastest speed,
# "medium" for some value in the middle, or any value listed in
# /sys/devices/system/cpu/cpu*/cpufreq/scaling_available_frequencies.
# 设定电池模式下的CPU频率
BATT_CPU_MAXFREQ=slowest
BATT_CPU_MINFREQ=slowest
BATT_CPU_GOVERNOR=ondemand

 现在可以通过下面命令查看laptop-mode的状态
sudo /etc/init.d/laptop-mode status

使用tmpfs把临时文件放入内存中。tmpfs 就象虚拟磁盘(ramdisk),但又不完全一样。象虚拟磁盘一样,tmpfs 可以使用您的 RAM,但它也可以使用您的交换分区来存储。而且传统的虚拟磁盘是个块设备,并需要一个 mkfs 之类的命令才能真正地使用它,tmpfs 是一个文件系统,而不是块设备;您只是安装它,它就可以使用了。总而言之,这让 tmpfs 成为我有机会遇到的最好的基于 RAM 的文件系统。

修改/etc/fstab,把tmpfs mount 到/tmp上,并设置最大上限为32M。

写道
# <file system> <mount point> <type> <options> <dump> <pass>
tmpfs /tmp tmpfs size=32m 0 0

 如果你内存很多(1G以上)你完全可关闭swap

写道
sudo swapoff /dev/hdax

 现在你的硬盘应该把读写操作降得很低了。

5.其他优化

降低颜色深度
修改xorg.conf 改为16位色
修改sudo vi /etc/X11/xorg.conf

写道
section screen
DefaultDepth 16

 尽量少使用光驱

尽量不启动X,使用console
比如看片子的话可以在console下用mplayer -vo fbdev your.movie.file
听音乐可以使用mpg123 ogg123等等

无线网卡
在不使用wlan的时候关闭它

写道
sudo iwconfig wlan0 power off

 6. 让上面优化实现自动化
acpi能截获电源适配器事件
/etc/acpi/events/ac
在这个脚本中我们可以看到实际上调用的是/etc/acpi/power.sh脚本

写道
for x in /proc/acpi/ac_adapter/*; do
grep -q off-line $x/state

if [ $? = 0 ] && [ x$1 != xstop ]; then
for SCRIPT in /etc/acpi/battery.d/*.sh; do
. $SCRIPT
done
if [ x$ENABLE_LAPTOP_MODE = xtrue ]; then
(sleep 5 && laptop_mode_enable)&
fi
else
for SCRIPT in /etc/acpi/ac.d/*.sh; do
. $SCRIPT
done
if [ x$ENABLE_LAPTOP_MODE = xtrue ]; then
(sleep 5 && laptop_mode_disable)&
fi
fi
done

 在这段脚本中我们看到如果要让脚本在bettary模式下运行就放在/etc/acpi/battery.d/目录下,要让脚本在电源模式下于虚拟就把脚本放在/etc/acpi/ac.d/目录下

好了既然脚本能自动执行,要实现什么样的节能效果就看你的想象力了。



另附一些实用工具软件
查看硬盘温度
sudo hddtemp -n /dev/hda
查看cpu信息
sudo x86info
查看硬件信息
lshw

案例,本人hp ze22xx
在windows下便携模式下能运行大概2小时20分
在ubuntu下未优化能运行2小时左右
优化过后能运行2小时30分

参考
gentoo power management guide

http://www.gentoo.org/doc/en/power-management-guide.xml


Extending Battery Life with Laptop Mode

http://www.linuxjournal.com/article/7539


 

 

 

 

 

ubuntu 笔记本电源管理优化指南


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论