Debian Codegraph @

usr/sbin/lm-profiler

raw
1 #! /bin/sh
2
3 # This script assists you in achieving very high power savings on laptops.
4 # It can detect programs that perform regular, non-bursty disk operations,
5 # and network services that listen on external addresses. When started,
6 # lm-profiler will run for 10 minutes (or a configured number of minutes),
7 # after which it will provide a series of recommendations.
8 #
9 # It will try to find init scripts for any programs that it recommends for
10 # stopping, and it will ask if you want to place links to those scripts in
11 # /etc/laptop-mode/batt-stop, so that laptop mode tools will automatically
12 # stop those daemons when battery mode is detected.
13 #
14 #
15 # This script is a part of Laptop Mode Tools.
16 #
17 # Configuration options for this script can be found in
18 # /etc/laptop-mode/lm-profiler.conf.
19 #
20 # Maintainer: Bart Samwel ([email protected])
21 # Adapted from initial version written by Jan Polacek ([email protected]).
22
23
24 #
25 # Read configuration.
26 #
27
28 # Defaults
29
30 PROFILE_RUN_LENGTH=600
31 ACTIVITY_INTERVAL_MAX=150
32 ACTIVITY_INTERVAL_MIN=5
33 RECOMMEND_DEFAULT_SERVICES=1
34 DEFAULT_SERVICES="anacron cron atd"
35 DEF_IGNORE_PROGRAMS="pdflush journald flush- XFree86 acpid apmd lm-profiler dmesg syslogd awk sed grep mc bc xfs cat diff uniq vi mv sort sleep"
36 IGNORE_PROGRAMS="$DEF_IGNORE_PROGRAMS"
37 RECOMMEND_NETWORK_SERVICES=1
38 DEF_IGNORE_NETWORK_SERVICES="perl" # Some daemons run on perl, not very informative
39 IGNORE_NETWORK_SERVICES="$DEF_IGNORE_NETWORK_SERVICES"
40 VERBOSE_OUTPUT=0
41 if [ -f /etc/laptop-mode/lm-profiler.conf ] ; then
42 . /etc/laptop-mode/lm-profiler.conf
43 fi
44
45 #
46 # Internal variables
47 #
48
49 DEBUG=0
50 ######################################################################
51
52 if [ $DEBUG -eq 1 ]; then
53 set -eux
54 fi
55
56 if [ "$VERBOSE_OUTPUT" -eq 1 ] ; then
57 OUTPUT="/dev/stdout"
58 else
59 OUTPUT="/dev/null"
60 fi
61
62 if [ ! `id -u` -eq 0 ]; then
63 echo "Only root can run profiler."
64 exit 0
65 fi
66
67 WORKDIR=`mktemp -d -t lm-profiler.XXXXXX`
68
69
70 start_profiling(){
71 # Turn on disk access profilling
72 if [ -f /proc/sys/vm/block_dump ]; then
73 echo "1" > /proc/sys/vm/block_dump
74 else
75 echo "/proc/sys/vm/block_dump does not exist, exiting."
76 exit 1
77 fi
78 }
79
80 stop_profiling(){
81 # Turn off disk access profilling
82 echo "0" > /proc/sys/vm/block_dump
83 }
84
85 # Create a commandline for grep, checking for the presence of all
86 # strings in a space-separated list passed as the first parameter.
87 format_params(){
88 for PARAM in $1 ; do
89 echo -n "-e $PARAM "
90 done
91 }
92
93 # Detect all processes that have accessed the disk since the last
94 # invocation of dmesg. The results are written to files called
95 # "write_accesses_N" and "read_accesses_N", where N is the first
96 # parameter of this function.
97 process_dmesg_diff(){
98 LEFT="$WORKDIR/dmesg_prev"
99 RIGHT="$WORKDIR/dmesg_next"
100 dmesg > $RIGHT
101 if [ -s $LEFT ] && [ -s $RIGHT ]; then
102 # The following command is long and complicated. It does
103 # the following things, separately for READ and WRITE
104 # accesses:
105 # 1. Retrieve only new lines, using diff.
106 # 2. Drop the first line -- it is probably a truncated
107 # version of an earlier line.
108 # 3. Parse out the name of the process.
109 # 4. Filter out IGNORE_PROGRAMS.
110 # 5. Write process name to output.
111
112 diff -u $LEFT $RIGHT \
113 |grep '^+' \
114 |grep -o '[^[:space:]]*([0-9]*): WRITE' \
115 |sed '1d' \
116 |awk -v FS="(" '{print $1}' \
117 |grep -v `format_params "$IGNORE_PROGRAMS"` \
118 |sort \
119 |uniq \
120 > $WORKDIR/write_accesses_$1
121
122 diff -u $LEFT $RIGHT \
123 |grep '^+' \
124 |grep -o '[^[:space:]]*([0-9]*): READ' \
125 |sed '1d' \
126 |awk -v FS="(" '{print $1}' \
127 |grep -v `format_params "$IGNORE_PROGRAMS"` \
128 |sort \
129 |uniq \
130 > $WORKDIR/read_accesses_$1
131
132 mv $RIGHT $LEFT
133 WRITE_ACCESSES_FOUND=0
134 for ACCESS in $(cat $WORKDIR/write_accesses_$1) ; do
135 if [ $WRITE_ACCESSES_FOUND -eq 0 ] ; then
136 printf '\r \rWrite accesses at %d/%d in lm-profiler run:' "$1" "$PROFILE_RUN_LENGTH"
137 WRITE_ACCESSES_FOUND=1
138 fi
139 echo -n " $ACCESS"
140 done
141 if [ $WRITE_ACCESSES_FOUND -ne 0 ] ; then
142 echo ""
143 fi
144
145 READ_ACCESSES_FOUND=0
146 for ACCESS in $(cat $WORKDIR/read_accesses_$1) ; do
147 if [ $READ_ACCESSES_FOUND -eq 0 ] ; then
148 printf '\r \rRead accesses at %d/%d in lm-profiler run:' "$1" "$PROFILE_RUN_LENGTH"
149 READ_ACCESSES_FOUND=1
150 fi
151 echo -n " $ACCESS"
152 done
153 if [ $READ_ACCESSES_FOUND -ne 0 ] ; then
154 echo ""
155 fi
156 else
157 echo "No dmesg data found to profile, exiting."
158 exit 1
159 fi
160 }
161
162 # Attempt to find an init script for ithe process given as an argument
163 findinit(){
164 INITDIR=
165 if [ -d /etc/init.d ] ; then
166 INITDIR=/etc/init.d
167 elif [ -d /etc/rc.d/init.d ] ; then
168 INITDIR=/etc/rc.d/init.d
169 fi
170 if [ "$INITDIR" != "" ] ; then
171 INIT=`ls $INITDIR/ |grep ^$1$ |head -n 1`
172 if [ -z "$INIT" ]; then
173 INIT=`grep $1 $INITDIR/* |sed s/:.*// |head -n 1`
174 else
175 INIT="$INITDIR/$INIT"
176 fi
177 if [ ! -z "$INIT" ] && [ -x $INIT ]; then
178 echo "$INIT"
179 fi
180 fi
181 }
182
183 # Look for names of running network services
184 profilenet(){
185 netstat -anp |grep ^tcp.*LISTEN |grep -v "Program name" |awk -v FS="/" '{print $2}' |sort |uniq |\
186 tr -d ['(',')','[',']']
187 }
188
189
190 #
191 # PROFILING RUN
192 #
193
194 # Disable profiling if the script gets interrupted.
195 trap "stop_profiling; echo; exit 10" EXIT HUP INT ABRT QUIT SEGV TERM
196
197 SECONDS_DONE=
198 echo "Profiling run started."
199 dmesg > $WORKDIR/dmesg_prev
200 start_profiling
201 echo > $WORKDIR/write_accesses_$SECONDS_DONE
202 echo > $WORKDIR/read_accesses_$SECONDS_DONE
203 SECONDS_DONE=0
204 while [ $SECONDS_DONE -le $PROFILE_RUN_LENGTH ] ; do
205 printf '\r%d seconds elapsed, %d remaining. \b\b\b\b\b\b\b\b\b' "$SECONDS_DONE" "$(($PROFILE_RUN_LENGTH - $SECONDS_DONE))"
206 sleep 1
207 SECONDS_DONE=$(($SECONDS_DONE + 1))
208 process_dmesg_diff $SECONDS_DONE
209 done
210 printf '\r \r'
211 stop_profiling
212
213 echo "Write frequency : "; cat $WORKDIR/write_accesses_* | sed -e 's/^[ \t]*//;s/[ \t]*$//' -e '/^$/ d' | sort | uniq -c | sort -n
214 echo "Read frequency : "; cat $WORKDIR/read_accesses_* | sed -e 's/^[ \t]*//;s/[ \t]*$//' -e '/^$/ d' | sort | uniq -c | sort -n
215 echo;
216
217 NETPROFILE=`profilenet`
218 echo "Profiling run completed."
219
220 #
221 # OUTPUT
222 #
223 ALREADY_SEEN=
224 if [ "$RECOMMEND_DEFAULT_SERVICES" -ne 0 ] ; then
225 for SERVICE in $DEFAULT_SERVICES ; do
226 echo
227 echo "Program: \"$SERVICE\""
228 echo "Reason: standard recommendation (program may not be running)"
229 INIT=`findinit $SERVICE`
230 if [ "$INIT" = "" ] ; then
231 echo "Init script: none"
232 echo "If you want to disable this program, you should do so manually."
233 else
234 echo "Init script: $INIT (GUESSED)"
235 echo
236 echo -n "Do you want to disable this service in battery mode? [y/N]: "
237 read ANSWER
238 if ( echo "$ANSWER" | grep -i ^y > /dev/null ) ; then
239 ln -fs $INIT /etc/laptop-mode/batt-stop/`echo $INIT | sed 's/.*\///g'`
240 fi
241 fi
242 ALREADY_SEEN="$ALREADY_SEEN $SERVICE"
243 done
244 fi
245
246
247
248
249 if [ "$RECOMMEND_NETWORK_SERVICES" -ne 0 ] ; then
250 for SERVICE in $NETPROFILE ; do
251 if ( echo " $IGNORE_NETWORK_SERVICES " | grep -v " $SERVICE " > /dev/null ) ; then
252 echo
253 echo "Program: \"$SERVICE\""
254 echo "Reason: listens on network, may not be needed offline."
255 INIT=`findinit $SERVICE`
256 if [ "$INIT" = "" ] ; then
257 echo "Init script: none"
258 echo "If you want to disable this program, you should do so manually."
259 else
260 echo "Init script: $INIT (GUESSED)"
261 echo
262 echo -n "Do you want to disable this service in battery mode? [y/N]: "
263 read ANSWER
264 if ( echo "$ANSWER" | grep -i ^y > /dev/null ) ; then
265 ln -fs $INIT /etc/laptop-mode/batt-stop/`echo $INIT | sed 's/.*\///g'`
266 fi
267 fi
268 ALREADY_SEEN="$ALREADY_SEEN $SERVICE"
269 fi
270 done
271 fi
272
273 SECONDS_LEFT=$PROFILE_RUN_LENGTH
274 while [ $SECONDS_LEFT -gt 0 ] ; do
275 for SERVICE in `cat $WORKDIR/*_accesses_$SECONDS_LEFT` ; do
276 if ( echo " $ALREADY_SEEN " | grep -v " $SERVICE " > /dev/null ) ; then
277 CUR_COMPARE_SECONDS=$(($SECONDS_LEFT - $ACTIVITY_INTERVAL_MIN))
278 while [ $CUR_COMPARE_SECONDS -gt $(($SECONDS_LEFT - $ACTIVITY_INTERVAL_MAX)) -a $CUR_COMPARE_SECONDS -gt 0 ] ; do
279 if ( grep "^$SERVICE$" $WORKDIR/*_accesses_$CUR_COMPARE_SECONDS > /dev/null ) ; then
280 if ( echo " $ALREADY_SEEN " | grep -v " $SERVICE " > /dev/null ) ; then
281 echo
282 echo "Program: \"$SERVICE\""
283 echo "Reason: disk access."
284 INIT=`findinit $SERVICE`
285 if [ "$INIT" = "" ] ; then
286 echo "Init script: none"
287 echo "If you want to disable this program, you should do so manually."
288 else
289 echo "Init script: $INIT (GUESSED)"
290 echo
291 echo -n "Do you want to disable this service in battery mode? [y/N]: "
292 fi
293 read ANSWER
294 if ( echo "$ANSWER" | grep -i ^y > /dev/null ) ; then
295 if [ -e $INIT ] ; then
296 ln -fs $INIT /etc/laptop-mode/batt-stop/`echo $INIT | sed 's/.*\///g'`
297 fi
298 fi
299 ALREADY_SEEN="$ALREADY_SEEN $SERVICE"
300 fi
301 fi
302 CUR_COMPARE_SECONDS=$(($CUR_COMPARE_SECONDS - 1))
303 done
304 fi
305 done
306 SECONDS_LEFT=$(($SECONDS_LEFT - 1))
307 done