power_profile revision 165875
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# $FreeBSD: head/etc/rc.d/power_profile 165875 2007-01-07 21:53:42Z njl $
9#
10
11# PROVIDE: power_profile
12# REQUIRE: mountcritlocal syslogd
13# KEYWORD: nojail nostart
14
15. /etc/rc.subr
16
17name="power_profile"
18LOGGER="logger -t power_profile -p daemon.notice"
19
20# Set a given sysctl node to a value.
21#
22# Variables:
23# $node: sysctl node to set with the new value
24# $value: HIGH for the highest performance value, LOW for the best
25#	  economy value, or the value itself.
26# $highest_value: maximum value for this sysctl, when $value is "HIGH"
27# $lowest_value: minimum value for this sysctl, when $value is "LOW"
28#
29sysctl_set ()
30{
31	# Check if the node exists
32	if [ -z "$(sysctl -n ${node} 2> /dev/null)" ]; then
33		return
34	fi
35
36	# Get the new value, checking for special types HIGH or LOW
37	case ${value} in
38	[Hh][Ii][Gg][Hh])
39		value=${highest_value}
40		;;
41	[Ll][Oo][Ww])
42		value=${lowest_value}
43		;;
44	[Nn][Oo][Nn][Ee])
45		return
46		;;
47	*)
48		;;
49	esac
50
51	# Set the desired value
52	[ -n "${value}" ] && sysctl ${node}=${value}
53}
54
55if [ $# -ne 1 ]; then
56	err 1 "Usage: $0 [0x00|0x01]"
57fi
58load_rc_config $name
59
60# Find the next state (performance or economy).
61state=$1
62case ${state} in
630x01 | '')
64	${LOGGER} "changed to 'performance'"
65	profile="performance"
66	;;
670x00)
68	${LOGGER} "changed to 'economy'"
69	profile="economy"
70	;;
71*)
72	echo "Usage: $0 [0x00|0x01]"
73	exit 1
74esac
75
76# Set the various sysctls based on the profile's values.
77node="hw.acpi.cpu.cx_lowest"
78highest_value="C1"
79lowest_value="`(sysctl -n dev.cpu.0.cx_supported | \
80	awk '{ print "C" split($0, a) }' -) 2> /dev/null`"
81eval value=\$${profile}_cx_lowest
82sysctl_set
83
84node="dev.cpu.0.freq"
85highest_value="`(sysctl -n dev.cpu.0.freq_levels | \
86	awk '{ split($0, a, "[/ ]"); print a[1] }' -) 2> /dev/null`"
87lowest_value="`(sysctl -n dev.cpu.0.freq_levels | \
88	awk '{ split($0, a, "[/ ]"); print a[length(a) - 1] }' -) 2> /dev/null`"
89eval value=\$${profile}_cpu_freq
90sysctl_set
91
92exit 0
93