linux电源管理

太过美好的事物,总是让人怀念。也或许只有到了真正失去的那一天才想起了初心,时间和距离真的很容易让人忘记许多,忘记了为之感动的那双眼眸,忘记了曾经想要守护的那个微笑。自私和贪婪逐渐掩盖了这一切,成长的代价真的很大,如果我还会遇见一个美好的人,我一定要时刻提醒自己,不忘初心。

以前没太在意电源的管理,在意的只是如何优化磁盘,今天看了一个视频,才发现原来电源也有这么多需要注意的地方

使用到了一个软件叫powertop,是intel提供的,它主要是优化一些设置,在空闲状态下让指定组件进入休眠状态,进一步来降低功耗。

启动

这个软件需要root权限,命令行启动,便可以查看一些默认设置

1
sudo powertop

在tunable页可以看到许多为bad的选项,其中大部分可以优化为good。但是先不要急着修改,因为这样手动修改之后下次开机就不会生效了,所以我们需要导出修改的命令,利用脚本在开机以后去自动修改。

导出报告

1
sudo powertop -r

这样便会在当前目录生成一个html的报告:powertop.html,我们在浏览器中打开它。可以看到已经把优化语句给出来了,如果手动开启了的话,除非下次开机,否则不会给出这些语句的。

Software Settings in Need of Tuning

Description Script
VM writeback timeout echo ‘1500’ > ‘/proc/sys/vm/dirty_writeback_centisecs’;
Enable Audio codec power management echo ‘1’ > ‘/sys/module/snd_hda_intel/parameters/power_save’;
NMI watchdog should be turned off echo ‘0’ > ‘/proc/sys/kernel/nmi_watchdog’;
Runtime PM for I2C Adapter i2c-12 (i915 gmbus dpd) echo ‘auto’ > ‘/sys/bus/i2c/devices/i2c-12/device/power/control’;
Runtime PM for I2C Adapter i2c-9 (i915 gmbus dpb) echo ‘auto’ > ‘/sys/bus/i2c/devices/i2c-9/device/power/control’;
Runtime PM for I2C Adapter i2c-10 (i915 gmbus dpc) echo ‘auto’ > ‘/sys/bus/i2c/devices/i2c-10/device/power/control’;
Runtime PM for I2C Adapter i2c-11 (i915 gmbus misc) echo ‘auto’ > ‘/sys/bus/i2c/devices/i2c-11/device/power/control’;
Autosuspend for USB device Flash Card Reader/Writer [Generic] echo ‘auto’ > ‘/sys/bus/usb/devices/1-4/power/control’;
Autosuspend for USB device USB Receiver [Logitech] echo ‘auto’ > ‘/sys/bus/usb/devices/1-1/power/control’;
Runtime PM for PCI Device Intel Corporation SSD 660P Series echo ‘auto’ > ‘/sys/bus/pci/devices/0000:04:00.0/power/control’;
Runtime PM for PCI Device Intel Corporation Cannon Point-LP PCI Express Root Port #9 echo ‘auto’ > ‘/sys/bus/pci/devices/0000:00:1d.0/power/control’;
Runtime PM for PCI Device Intel Corporation Cannon Point-LP SPI Controller echo ‘auto’ > ‘/sys/bus/pci/devices/0000:00:1f.5/power/control’;
Runtime PM for PCI Device Intel Corporation Cannon Point-LP High Definition Audio Controller echo ‘auto’ > ‘/sys/bus/pci/devices/0000:00:1f.3/power/control’;
Runtime PM for PCI Device Intel Corporation Cannon Point-LP Shared SRAM echo ‘auto’ > ‘/sys/bus/pci/devices/0000:00:14.2/power/control’;
Runtime PM for PCI Device Intel Corporation Cannon Point-LP CNVi [Wireless-AC] echo ‘auto’ > ‘/sys/bus/pci/devices/0000:00:14.3/power/control’;
Runtime PM for PCI Device Intel Corporation Cannon Point-LP LPC Controller echo ‘auto’ > ‘/sys/bus/pci/devices/0000:00:1f.0/power/control’;
Runtime PM for PCI Device Intel Corporation UHD Graphics 620 (Whiskey Lake) echo ‘auto’ > ‘/sys/bus/pci/devices/0000:00:02.0/power/control’;
Runtime PM for PCI Device Intel Corporation Coffee Lake HOST and DRAM Controller echo ‘auto’ > ‘/sys/bus/pci/devices/0000:00:00.0/power/control’;
Runtime PM for disk sda echo ‘auto’ > ‘/sys/block/sda/device/power/control’;
Runtime PM for PCI Device Intel Corporation Cannon Point-LP Thermal Controller echo ‘auto’ > ‘/sys/bus/pci/devices/0000:00:12.0/power/control’;

这里面有一项是不能修改的,便是无线鼠标连得usb设备,否则会自动断电。创建命令如下:

1
vim ~/.local/bin/mypowertop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
# 使用powertop产生的报告,来优化电源的使用

#VM writeback timeout
echo '1500' > '/proc/sys/vm/dirty_writeback_centisecs';
#Enable Audio codec power management
echo '1' > '/sys/module/snd_hda_intel/parameters/power_save';
#NMI watchdog should be turned off
echo '0' > '/proc/sys/kernel/nmi_watchdog';
#Runtime PM for I2C Adapter i2c-12 (i915 gmbus dpd)
echo 'auto' > '/sys/bus/i2c/devices/i2c-12/device/power/control';
#Runtime PM for I2C Adapter i2c-9 (i915 gmbus dpb)
echo 'auto' > '/sys/bus/i2c/devices/i2c-9/device/power/control';
#Runtime PM for I2C Adapter i2c-10 (i915 gmbus dpc)
echo 'auto' > '/sys/bus/i2c/devices/i2c-10/device/power/control';
#Runtime PM for I2C Adapter i2c-11 (i915 gmbus misc)
echo 'auto' > '/sys/bus/i2c/devices/i2c-11/device/power/control';
#Autosuspend for USB device Flash Card Reader/Writer [Generic]
echo 'auto' > '/sys/bus/usb/devices/1-4/power/control';

#Autosuspend for USB device USB Receiver [Logitech] 这一项不优化,否则鼠标一会不使用就会自动停止对usb供电
#echo 'auto' > '/sys/bus/usb/devices/1-1/power/control';

#Runtime PM for PCI Device Intel Corporation SSD 660P Series
echo 'auto' > '/sys/bus/pci/devices/0000:04:00.0/power/control';
#Runtime PM for PCI Device Intel Corporation Cannon Point-LP PCI Express Root Port #9
echo 'auto' > '/sys/bus/pci/devices/0000:00:1d.0/power/control';
#Runtime PM for PCI Device Intel Corporation Cannon Point-LP SPI Controller
echo 'auto' > '/sys/bus/pci/devices/0000:00:1f.5/power/control';
#Runtime PM for PCI Device Intel Corporation Cannon Point-LP High Definition Audio Controller
echo 'auto' > '/sys/bus/pci/devices/0000:00:1f.3/power/control';
#Runtime PM for PCI Device Intel Corporation Cannon Point-LP Shared SRAM
echo 'auto' > '/sys/bus/pci/devices/0000:00:14.2/power/control';
#Runtime PM for PCI Device Intel Corporation Cannon Point-LP CNVi [Wireless-AC]
echo 'auto' > '/sys/bus/pci/devices/0000:00:14.3/power/control';
#Runtime PM for PCI Device Intel Corporation Cannon Point-LP LPC Controller
echo 'auto' > '/sys/bus/pci/devices/0000:00:1f.0/power/control';
#Runtime PM for PCI Device Intel Corporation UHD Graphics 620 (Whiskey Lake)
echo 'auto' > '/sys/bus/pci/devices/0000:00:02.0/power/control';
#Runtime PM for PCI Device Intel Corporation Coffee Lake HOST and DRAM Controller
echo 'auto' > '/sys/bus/pci/devices/0000:00:00.0/power/control';
#Runtime PM for disk sda
echo 'auto' > '/sys/block/sda/device/power/control';
#Runtime PM for PCI Device Intel Corporation Cannon Point-LP Thermal Controller
echo 'auto' > '/sys/bus/pci/devices/0000:00:12.0/power/control';

启用方式

  1. 可以将命令放到~/.xinitrc中,在打开图形界面时调用

由于需要使用到root权限,所以需要在将mypowertop这条命令改为不需要密码执行

1
2
sudo vim /etc/sudoer
narcissus ALL=(ALL) NOPASSWD: /home/narcissus/.local/bin/mypowertop

将命令加入.xinitrc

1
sudo mypowertop &>/dev/null &

  1. 也可以加入systemd,用systemd来开机时作为服务调用
1
vim /etc/systemd/system/powertop.service
1
2
3
4
5
6
7
8
9
[Unit]
Description=Powertop tunings

[Service]
ExecStart=/home/narcissus/.local/bin/mypowertop
RemainAfterExit=true

[Install]
WantedBy=multi-user.target

使用systemd自动调用它

1
sudo systemctl enable powertop