配置 Linux 系统在 AC 供电切换时自动调整 CPU 节能模式

UDev 真是个万能的东西啊(好像有点夸张)!这次我又要利用它的 rules 来实现当 AC 供电切换时自动调整 CPU 的节能模式。我的目标是当 AC 供电时各个 CPU 都是工作在 performance 模式;当电池供电时切换成 ondemand 模式。

内核模块依赖
1. acpi-cpufreq

CPU 模式切换程序 cpumode
文件路径:/usr/local/sbin/cpumode

#!/bin/bash
# Heiher <admin@heiher.info>
 
set -e
 
MODE=$1
 
cpu_set_mode()
{
	echo "$2" > "/sys/devices/system/cpu/$1/cpufreq/scaling_governor"
}
 
cpu_foreach()
{
	for i in /sys/devices/system/cpu/cpu?; do
		CPU=`basename $i`
		$1 ${CPU} $2
	done
}
 
case "${MODE}" in
	performance | ondemand)
		cpu_foreach cpu_set_mode ${MODE}
		;;
	*)
		echo "Usage: $0 [ performance | ondemand ]"
		;;
esac
 
exit 0

UDev 规则文件
文件路径:/etc/udev/rules.d/80-cpu-mode.rules

# Heiher <admin@heiher.info>
#
 
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_NAME}=="AC", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/usr/local/sbin/cpumode performance"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_NAME}=="AC", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/local/sbin/cpumode ondemand"

Over!

One thought on “配置 Linux 系统在 AC 供电切换时自动调整 CPU 节能模式”

Leave a Reply

Your email address will not be published. Required fields are marked *