]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - etc/rc.d/power_profile
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / etc / rc.d / power_profile
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$
9 #
10
11 # PROVIDE: power_profile
12 # REQUIRE: FILESYSTEMS syslogd
13 # KEYWORD: nojail nostart
14
15 . /etc/rc.subr
16
17 name="power_profile"
18 stop_cmd=':'
19 LOGGER="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 #
30 sysctl_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         [ -n "${value}" ] && sysctl ${node}=${value}
54 }
55
56 if [ $# -ne 1 ]; then
57         err 1 "Usage: $0 [0x00|0x01]"
58 fi
59 load_rc_config $name
60
61 # Find the next state (performance or economy).
62 state=$1
63 case ${state} in
64 0x01 | '')
65         ${LOGGER} "changed to 'performance'"
66         profile="performance"
67         ;;
68 0x00)
69         ${LOGGER} "changed to 'economy'"
70         profile="economy"
71         ;;
72 *)
73         echo "Usage: $0 [0x00|0x01]"
74         exit 1
75 esac
76
77 # Set the various sysctls based on the profile's values.
78 node="hw.acpi.cpu.cx_lowest"
79 highest_value="C1"
80 lowest_value="`(sysctl -n dev.cpu.0.cx_supported | \
81         awk '{ print "C" split($0, a) }' -) 2> /dev/null`"
82 eval value=\$${profile}_cx_lowest
83 sysctl_set
84
85 node="dev.cpu.0.freq"
86 highest_value="`(sysctl -n dev.cpu.0.freq_levels | \
87         awk '{ split($0, a, "[/ ]"); print a[1] }' -) 2> /dev/null`"
88 lowest_value="`(sysctl -n dev.cpu.0.freq_levels | \
89         awk '{ split($0, a, "[/ ]"); print a[length(a) - 1] }' -) 2> /dev/null`"
90 eval value=\$${profile}_cpu_freq
91 sysctl_set
92
93 exit 0