xref: /freebsd/libexec/rc/rc.d/power_profile (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1#!/bin/sh
2#
3# Modify the power profile based on AC line state.  This script is
4# usually called from devd(8).
5#
6# Arguments: 0x00 (AC offline, economy) or 0x01 (AC online, performance)
7#
8#
9
10# PROVIDE: power_profile
11# REQUIRE: FILESYSTEMS syslogd
12# KEYWORD: nojail nostart
13
14. /etc/rc.subr
15
16name="power_profile"
17desc="Modify the power profile based on AC line state"
18stop_cmd=':'
19LOGGER="logger -t power_profile -p daemon.notice"
20
21# Set a given sysctl node to a value.
22#
23# Variables:
24# $node: sysctl node to set with the new value
25# $value: HIGH for the highest performance value, LOW for the best
26#	  economy value, or the value itself.
27# $highest_value: maximum value for this sysctl, when $value is "HIGH"
28# $lowest_value: minimum value for this sysctl, when $value is "LOW"
29#
30sysctl_set()
31{
32	# Check if the node exists
33	if [ -z "$(sysctl -n ${node} 2> /dev/null)" ]; then
34		return
35	fi
36
37	# Get the new value, checking for special types HIGH or LOW
38	case ${value} in
39	[Hh][Ii][Gg][Hh])
40		value=${highest_value}
41		;;
42	[Ll][Oo][Ww])
43		value=${lowest_value}
44		;;
45	[Nn][Oo][Nn][Ee])
46		return
47		;;
48	*)
49		;;
50	esac
51
52	# Set the desired value
53	if [ -n "${value}" ]; then
54		if ! sysctl ${node}=${value} > /dev/null 2>&1; then
55			warn "unable to set ${node}=${value}"
56		fi
57	fi
58}
59
60if [ $# -ne 1 ]; then
61	err 1 "Usage: $0 [0x00|0x01]"
62fi
63load_rc_config $name
64
65# Find the next state (performance or economy).
66state=$1
67case ${state} in
680x01 | '')
69	${LOGGER} "changed to 'performance'"
70	profile="performance"
71	;;
720x00)
73	${LOGGER} "changed to 'economy'"
74	profile="economy"
75	;;
76*)
77	echo "Usage: $0 [0x00|0x01]"
78	exit 1
79esac
80
81# Set the various sysctls based on the profile's values.
82node="hw.acpi.cpu.cx_lowest"
83highest_value="C1"
84lowest_value="Cmax"
85eval value=\$${profile}_cx_lowest
86sysctl_set
87
88node="dev.cpu.0.freq"
89highest_value="`(sysctl -n dev.cpu.0.freq_levels | \
90	awk '{ split($0, a, "[/ ]"); print a[1] }' -) 2> /dev/null`"
91lowest_value="`(sysctl -n dev.cpu.0.freq_levels | \
92	awk '{ split($0, a, "[/ ]"); print a[length(a) - 1] }' -) 2> /dev/null`"
93eval value=\$${profile}_cpu_freq
94sysctl_set
95
96exit 0
97