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