电源管理/向导

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Power management/Guide and the translation is 76% complete.
Outdated translations are marked like this.


在最近几年,良好的电源管理成为一个用户寻求完美的笔记本的一个重要条件。同时操作系统也要支持电源管理/节能。在这篇向导中,我们将会介绍如何在 Gentoo Linux 中以一种灵活的,自动化的方式设置好电源管理。

Article status
This article has some todo items:
  • Add instructions for kernel configuration of remaining CPU frequency drivers

介绍

关于本文档

本文档描述了如何为笔记本型电脑设置电源管理。尽管这当中的一部分设置可被用于其它类型的平台的电源管理,但是这并不是本文的目的。如果你要在非笔记本的系统上执行这些设定,请小心,以免带来不可预期的影响。

在本文档中,主要是介绍 laptop mode tools 这个工具,它提供了许多相关的功能。不过我们也会介绍一些别的能提供更详细的,更有针对性的设置的工具。在这种情况下,为了避免造成冲突, laptop mode tools 提供的相同的功能必须被禁用。

关于 laptop_mode

laptop_mode 是一种由内核内置的用于管理输入/输出的设置,比如使硬盘在合适的时候进行休眠(并且在队操作之后不会立刻被唤醒)来节能。

关于 laptop-mode-tools

Laptop Mode Toolsapp-laptop/laptop-mode-tools 提供,它使用户可以调整电源管理功能,也可以用于管理上文中提到的 Linux 内核中的 laptop_mode 设置,同时还有一些附加的功能用于管理系统中与电源管理有关的设置。

Linux 内核配置

启用电源管理的最小化内核设置

Portage sys-kernel 中有不同的内核源代码。我们建议使用 sys-kernel/gentoo-sources,但如果需要高级休眠支持,则可能需要 sys-kernel/tuxonice-sources

要在 Linux 内核中启用适当的电源管理功能,请启用以下设置:

内核 Minimum kernel setup for Power Management
Power management and ACPI options --->
  -*- Device power management core functionality
  [*] ACPI (Advanced Configuration and Power Interface) Support --->
    <*> AC Adapter
    <*> Battery
    -*- Button
    -*- Video
    <*> Fan
    <*> Processor
    <*> Thermal Zone
  
  [*] CPU Frequency scaling --->
        Default CPUFreq governor (ondemand)  --->
    -*- 'performance' governor
    <*> 'powersave' governor
    <*> 'userspace' governor for userspace frequency scaling
    -*- 'ondemand' cpufreq policy governor
    <*> 'conservative' cpufreq governor
    <*> ACPI Processor P-States driver

Do not forget to enable the CPU frequency scaling driver for the CPU. For the SandyBridge and higher Intel core series of processors, userspace, ondemand, and conservative governors are unnecessary; performance should be selected as the default, and Intel P state control should be selected instead of ACPI Processor P-States driver. [1]

In this case, the userspace, ondemand, and conservative governors are unnecessary. The performance governor should be selected as the default. [2]

内核 英特尔 Sandy Bridge 架构和更新的处理器的内核设置
Power management and ACPI options --->
  -*- Device power management core functionality
  [*] ACPI (Advanced Configuration and Power Interface) Support --->
    <*> AC Adapter
    <*> Battery
    -*- Button
    -*- Video
    <*> Fan
    <*> Processor
    <*> Thermal Zone
  
  [*] CPU Frequency scaling --->
        Default CPUFreq governor (performance)  --->
    -*- 'performance' governor
    <*> Intel P state control

Enabling additional drivers

For Intel processors used in laptops since 2011 you may want to enable additional advanced features - Thermal and powercap sysfs.

Thermal sysfs driver

内核 Generic Thermal sysfs driver
Device Drivers --->
  -*- Generic Thermal sysfs driver --->
    (0)   Emergency poweroff delay in milli-seconds                                                                              
    [*]   Expose thermal sensors as hwmon device                                                                                
    -*-   Enable writable trip points                                                                                          
          Default Thermal governor (step_wise)  --->                                                                           
    [*]   Fair-share thermal governor                                                                                           
    -*-   Step_wise thermal governor                                                                                            
    -*-   Bang Bang thermal governor                                                                                            
    -*-   User_space thermal governor                                                                                           
    [ ]   Thermal emulation mode support
    [*]   Power allocator thermal governor   
    <M>   Intel PowerClamp idle injection driver
    <M>   X86 package temperature thermal driver
    < >   Intel SoCs DTS thermal driver                                                                                          
          ACPI INT340X thermal drivers  --->                                                                                     
             <M> ACPI INT340X thermal drivers
    <M>   Intel PCH Thermal Reporting Driver

powercap sysfs driver

Additionally, you can enable Running Average Power Limit – RAPL technology. Both PowerTOP and turbostat utilities are aware of RAPL. For RAPL enablement use:

内核 Generic powercap sysfs driver
Device Drivers --->
  [*] Generic powercap sysfs driver --->
    <M>   Intel RAPL Support

To make use of Intel's Linux thermal daemon you first need to emerge sys-power/thermald and enable it via:

root #rc-config add thermald

root #systemctl enable thermald
附注
dptfxtract from Intel may be used to auto generate a thermald.conf.xml for the system.

内核设置完成

有关更详细的配置说明,请参阅 电源管理/处理器#内核

如果有必要的话可以编译并安装新内核并重启。

使用 Laptop Mode Tools

安装

“Laptop mode tools”可以通过以下命令被很简单的安装:

root #emerge --ask app-laptop/laptop-mode-tools

这个软件包可能需要通过别的附加的,可选的 USE 旗标配置。 以下是这个软件包支持的 USE 旗标和它们的意义:

USE 旗标 描述 适用情况
acpi 依赖于 sys-power/acpid 。会自动根据系统的变化调整与节能相关的功能的开关。 笔记本不是特别旧(2003年左右及更晚)。
apm 依赖于 sys-apps/apmd 会自动根据系统的变化调整与节能相关的功能的开关。 非常老的笔记本。
bluetooth 依赖于 net-wireless/bluez,会启用 laptop-mode-tools 中相关的功能来管理蓝牙设置(根据电池电量来开/关蓝牙功能) 笔记本(和内核)支持蓝牙技术。
scsi 依赖于 sys-apps/sdparm,会启用 laptop-mode-tools 的与 SCSI (并不是 SATA) 磁盘运行参数。 笔记本使用 SCSI 磁盘。

注意: acpiapm 这两个 USE 旗标是互相冲突的。

  • apm USE 旗标会启用对 Advanced Power Management (高级电源管理)的支持,是老式笔记本 (公元 2000 年以前)的标准电源管理功能。
  • acpi USE 旗标会启用对 Advanced Configuration and Power Interface (高级配置和电源管理接口)的支持。 ACPI 是 APM 的“继任者”所有的现代笔记本都支持ACPI。

取决于你的系统,你需要设置 acpiapm 中的一个 USE 旗标。 本指南的剩余部分是以支持 ACPI 的笔记本作为范例的。

在设置好 USE 旗标后,用以下命令安装 laptop-mode-tools

root #emerge --ask laptop-mode-tools

配置

laptop-mode-tools 在刚刚被安装好时并不会自动启动。 我们可以从 /etc/laptop-mode/laptop-mode.conf 找到应该要如何配置本软件包(这是 laptop-mode-tools 的主要配置文件,该文件内的注释可以帮助你理解如何设置它)。

但这并不是 laptop-mode-tools 唯一的配置文件。 laptop-mode-tools 支持有独立配置文件的插件(和/或模块)。这些额外的配置文件会被放在 /etc/laptop-mode/conf.d。它们是根据插件或模块的名字来命名的(例如 intel-sata-powermgmt.conf).

每一个 laptop-mode-tools 配置文件里最重要的部分之一就是控制一些功能的开关。这在把 laptop-mode-tools 和一些别的电源管理服务(比如cpufreqd)同时使用时很有用。在这种情况下: 必须给laptop-mode-tools 设置 CONTROL_CPU_FREQUENCY=0:

文件 /etc/laptop-mode/conf.d/cpufreq.conf
CONTROL_CPU_FREQUENCY=0

接下来的几个段落用于帮助用户配置 laptop-mode-tools 套件 到合适于自己的情况。 当你完成配置后,启动 laptop-mode-tools 套件 并把它添加到系统自起。

With OpenRC:

root #rc-service laptop_mode start
root #rc-update add laptop_mode default

使用 systemd:

root #systemctl enable laptop-mode.service --now

laptop-mode-tools是怎么工作的

laptop_mode 服务 运行时,它会检测系统当前状态。以下是所有状态:

  • 电池使用(Battery),当笔记本使用电池作为电源时会进入此状态;在配置文件中以 BATT_ 作为在此模式下使用的配置的名称前缀;
  • 电源(AC),当笔记本使用外接电源时会进入此状态;在配置文件中以 AC_ 作为在此模式下使用的配置的名称前缀;
  • 笔记本模式(Laptop Mode), 当启用 laptop mode 时会被激活;在配置文件中以 LM_ 作为在此模式下使用的的配置的名称前缀;
  • 非笔记本模式(No Laptop Mode),当laptop mode被禁用时会被激活;在配置文件中以 NOLM_ 作为在此模式下使用的的配置的名称前缀。

AC/BATT_LM/NOLM_ 前缀可以以 AC_LM_ 的形式同时使用。

laptop_mode 服务 启动后,它会根据发生的时间切换到对应的模式(根据配置文件进行) 。举个例子,ENABLE_LAPTOP_MODE_ON_BATTERY=1 会使 laptop_mode 服务 在笔记本使用时切换到 laptop mode。在这时, laptop_mode 服务 会使用前缀为 LM_, LM_BATT_, BATT_LM_, 还有 BATT_ 的配置。

为了确保这些设置之间不会发生冲突,重复的相同设置是不允许的。在下面的例子中, 在 CPU_MAXFREQ 中的第一组设置是有效的,而之后的 CPU_GOVERNOR 中的设置则是无效的。


代码 Colliding settings
## Valid set
BATT_CPU_MAXFREQ=fastest
LM_AC_CPU_MAXFREQ=fastest
NOLM_AC_CPU_MAXFREQ=fastest
  
## Invalid set
BATT_CPU_MINFREQ=fastest
LM_AC_CPU_MINFREQ=fastest
# The following includes AC and BATT, but BATT is already defined
NOLM_CPU_MINFREQ=fastest

配置 CPU 频率管理器

laptop mode tools 中的 CPU 频率管理器允许用户调整 CPU 的频率。它支持设定「CPU 频率管理」,「最高和最低频率」。在这里我们需要使用的配置文件是:/etc/laptop-mode/conf.d/cpufreq.conf

CPU频率管理(CPU frequency governor) 是一个内核级的用来定义内核如何控制 CPU 频率的策略。我们在内核设置里就已经设置好了使用那种模式。我们再来看一遍:

  • performance 一直以最高频率工作;
  • powersave 一直以最低频率工作;
  • userspace 不进行任何设置,由用户进行选择(或者是由一些由用户执行的程序进行选择);
  • ondemand 当有负载时会把 CPU 频率调整到最高;
  • conservative 当有负载时会逐渐提升 CPU 频率;

在 电源使用(AC) 或 电池使用(Battery)模式之间切换,或时在 laptop mode 与 no laptop mode 之间切换时,程序会选择合适的 CPU 频率管理模式。

设置显示亮度

laptop mode tools 根据 /etc/laptop-mode/conf.d/lcd-brightness.conf 管理 LCD 显示器亮度 。

程序使用 /proc/acpi/video/VID/LCD/brightness 来管理 LCD 显示器亮度,但是近期的内核不再提供它了(bug 499544);我们需要把他更正为 /sys/class/backlight/acpi_video0/brightness

它的值可以设置为 0 到 /sys/class/backlight/acpi_video0/max_brightness 中写的最大亮度(0 指的是最低亮度)。

配置其它服务

laptop-mode-tools 的一个有趣的功能是支持在切换模式后重新加载部分服务(比如系统记录,system logger)。这由 /etc/laptop-mode/conf.d/configuration-file-control.conf 控制。

If enabled, the laptop_mode application will switch the configuration file(s) of the mentioned services with the same file, but suffixed with -nolm-ac , -lm-ac or -batt. It will then signal or reload the appropriate services so they can use the new configuration file.

使用 cpufreqd

警告
Package sys-power/cpufreqd is deprecated and has been removed from the Portage tree. sys-power/ncpufreqd can still be used for 2.6.x kernels.

安装

The cpufreqd application allows the user to manage CPU frequencies in a more granular approach than what laptop-mode-tools supports. But before we dive into the installation of cpufreqd, let us first look at the USE flags it supports.

USE flag Description Suggested when...
acpi Enable support for ACPI, allowing cpufreqd to be notified about specific events as well as govern power through the ACPI interface the laptop is not very old (around year 2003 and later)
apm Enable support for APM, allowing cpufreqd to be notified about specific events as well as govern power through the APM interface the laptop is very old
lm_sensors Enable support for the Linux hardware sensors (through sys-apps/lm_sensors), allowing to switch profiles based on hardware sensor results using advanced events through lm_sensors
nforce2 Enable support for NForce, allowing cpufreqd to change the NForce FSB clock and video card frequency an NVidia graphics card based on the NForce chipset is present
nvidia Enable support for NVidia graphical card configuration (through the NVidia nvclock interface), allowing cpufreqd to change the video card frequency of NVidia graphical cards an NVidia graphics card is present
pmu Enable the Power Management Unit plug-in of cpufreqd. This allows the software to poll the Linux kernel Power Supply interface, getting more detailed information on battery charge the laptop does not support ACPI or APM

The acpi, apm, and pmu USE overlap, so only one should be active. If the laptop is sufficiently recent, acpi is the best bet. If not, apm offers all that is needed. When even APM isn't supported, try using pmu.

配置好 USE 标志后,是时候安装 cpufreqd 了。

root #emerge --ask cpufreqd

配置

The cpufreqd application monitors the status of the system through several plugins. Based on the feedback it receives from those plugins, it will adjust the policy used to govern the CPU frequency.

cpufreqd can be configured by editing /etc/cpufreqd.conf. It contains three different sections:

  1. The [General]...[/General] section contains general configuration information.
  2. The [Profile]...[/Profile] section defines the policies that the cpufreqd daemon can switch to. The section is very similar to the information used when manually setting the CPU frequency policy using cpufreq-set.
  3. The [Rule]...[/Rule] section is the work-horse of the cpufreqd daemon, defining when the daemon decides to switch to a different profile.

快速浏览一个示例规则。

文件 /etc/cpufreqd.confcpufreqd 示例规则
[...]
  
[Profile]
name=On Demand High
minfreq=40%
maxfreq=100%
policy=ondemand
[/Profile]
  
[Rule]
name=AC Off - High Power
ac=off
battery_interval=70-100
profile=On Demand High
[/Rule]
  
[...]

In the above example, cpufreqd will switch the system to the On Demand High profile (also shown in the above excerpt). This profile by itself uses the ondemand governor with a minimum frequency of 40% (iow, a CPU of 2 GHz will have by this policy a minimum frequency of 800 MHz).

The cpufreqd application can offer a more granular approach on CPU frequency scaling. But not only that, but the CPU frequency scaling can be tweaked based on various other metrics available. The default configuration offers a sample rule: when a movie is watched, maximum performance is desired (unless the CPU temperature is getting too high).

When cpufreqd has been configured, it is time to start it (and make sure the service is loaded automatically). Make sure that CPU frequency handling by other tools (like laptop-mode-tools) is disabled!

root #rc-update add cpufreqd default
root #/etc/init.d/cpufreqd start

另请参阅

外部资源

  • Laptop Mode Tools Homepage, includes About laptop mode.
  • PowerTOP, an interactive application helping users to find out which processes are forcing wakeups on the CPU most often.
  • A ThinkWiki article on How to reduce power consumption (on Linux). This article offers an exhaustive list of measures one can take. However, it should be noted that the laptop mode tools implements the majority of these (if properly configured).

参考

  1. Dominik Brodowski. Intel P-State driver, CPU frequency and voltage scaling code in the Linux(TM) kernel. Retrieved 12 June 2016.
  2. Dominik Brodowski. Intel P-State driver, [https://www.kernel.org /doc/Documentation/cpu-freq/index.txt CPU frequency and voltage scaling code in the Linux(TM) kernel]. Retrieved 12 June 2016.