debuggers.hg

view tools/xenballoon/xenballoond @ 21067:b4a1832a916f

Update Xen version to 4.0.0-rc6
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 09 18:18:05 2010 +0000 (2010-03-09)
parents 8373971089a7
children b59f87f56b1e
line source
1 #!/bin/bash
2 #
3 # Copyright (C) 2008 Oracle Corporation and/or its affiliates.
4 # All rights reserved.
5 # Written by: Dan Magenheimer <dan.magenheimer@oracle.com>
6 #
7 # xenballoond - In-guest engine for Xen memory ballooning
8 # Original version: 080630
9 # Updated 0906XX: add tmem preswap auto-shrinking
10 #
11 # Two self-ballooning "policies" are implemented:
12 # - Selfballooning: Adjust memory periodically, with no (or little) input
13 # from domain0. Target memory is determined solely by the
14 # Committed_AS line in /proc/meminfo, but parameters may adjust
15 # the rate at which the target is achieved.
16 # - Directed ballooning: Adjust memory solely as directed by domain0
17 #
18 # Under some circumstances, "output" may also be generated; the contents
19 # of /proc/meminfo and /proc/vmstat may be periodically placed on xenbus.
20 #
21 # If xenbus is running and the /usr/bin/xenstore-* tools are installed,
22 # "xenbus is enabled".
23 #
24 # Parameters are documented in /etc/sysconfig/xenballoon.conf. Although
25 # some are not used with directed ballooning, all must be set properly.
26 # If xenbus is enabled, some of these parameters may be overridden by values
27 # set by domain0 via xenbus.
29 minmb() {
30 RETVAL=$XENBALLOON_MINMEM
31 if [ $RETVAL -ne 0 ]; then
32 return $RETVAL
33 fi
34 kb=`cat $XENBALLOON_MAXMEMFILE`
35 let "mb=$kb/1024"
36 let "pages=$kb/4"
37 # this algorithm from drivers/xen/balloon/balloon.c:minimum_target()
38 # which was added to balloon.c in 2008 to avoid ballooning too small
39 # it is unnecessary here except to accomodate pre-2008 balloon drivers
40 # note that ranges are adjusted because a VM with "memory=1024"
41 # gets somewhat less than 1024MB
42 if [ $mb -lt 125 ]; then
43 let RETVAL="$(( 8 + ($pages >> 9) ))"
44 elif [ $mb -lt 500 ]; then
45 let RETVAL="$(( 40 + ($pages >> 10) ))"
46 elif [ $mb -lt 2000 ]; then
47 let RETVAL="$(( 104 + ($pages >> 11) ))"
48 else
49 let RETVAL="$(( 296 + ($pages >> 13) ))"
50 fi
51 return # value returned in RETVAL in mB
52 }
54 curkb() {
55 kb=`grep MemTotal /proc/meminfo | sed 's/ */ /' | \
56 cut -f2 -d' '`
57 RETVAL=$kb
58 return # value returned in RETVAL in kB
59 }
61 downhysteresis() {
62 RETVAL=$XENBALLOON_AUTO_DOWNHYSTERESIS
63 if [ $xenstore_enabled = "true" ]; then
64 if xenstore-exists memory/downhysteresis ; then
65 RETVAL=`xenstore-read memory/downhysteresis`
66 fi
67 fi
68 return
69 }
71 uphysteresis() {
72 RETVAL=$XENBALLOON_AUTO_UPHYSTERESIS
73 if [ $xenstore_enabled = "true" ]; then
74 if xenstore-exists memory/uphysteresis ; then
75 RETVAL=`xenstore-read memory/uphysteresis`
76 fi
77 fi
78 return
79 }
81 selfballoon_eval() {
82 if [ $xenstore_enabled = "true" ]; then
83 if xenstore-exists memory/selfballoon; then
84 RETVAL=`xenstore-read memory/selfballoon`
85 if [ $RETVAL -eq 1 ]; then
86 selfballoon_enabled=true
87 return
88 fi
89 fi
90 fi
91 selfballoon_enabled=$XENBALLOON_SELF
92 return
93 }
95 selftarget() {
96 tgtkb=`grep Committed_AS /proc/meminfo | sed 's/ */ /' | cut -f2 -d' '`
97 minmb
98 let "minbytes=$RETVAL*1024*1024"
99 let "tgtbytes=$tgtkb*1024"
100 if [ $tgtbytes -lt $minbytes ]; then
101 let "tgtbytes=$minbytes"
102 fi
103 RETVAL=$tgtbytes # value returned in RETVAL in bytes
104 return
105 }
107 # $1 == 1 means use selftarget, else target in kB
108 balloon_to_target() {
109 if [ "$1" -eq 1 ]; then
110 selftarget
111 tgtbytes=$RETVAL
112 else
113 let "tgtbytes=$(( $1 * 1024 ))"
114 fi
115 curkb
116 let "curbytes=$RETVAL*1024"
117 if [ $curbytes -gt $tgtbytes ]; then
118 downhysteresis
119 downhys=$RETVAL
120 if [ $downhys -ne 0 ]; then
121 let "tgtbytes=$(( $curbytes - \
122 ( ( $curbytes - $tgtbytes ) / $downhys ) ))"
123 fi
124 else if [ $curbytes -lt $tgtbytes ]; then
125 uphysteresis
126 uphys=$RETVAL
127 let "tgtbytes=$(( $curbytes + \
128 ( ( $tgtbytes - $curbytes ) / $uphys ) ))"
129 fi
130 fi
131 echo $tgtbytes > /proc/xen/balloon
132 if [ $xenstore_enabled = "true" ]; then
133 let "tgtkb=$(( $tgtbytes/1024 ))"
134 xenstore-write memory/selftarget $tgtkb
135 fi
136 }
138 send_memory_stats() {
139 if [ ! $xenstore_enabled = "true" ]; then
140 return
141 fi
142 if [ $XENBALLOON_SEND_MEMINFO ]; then
143 xenstore-write memory/meminfo "`cat /proc/meminfo`"
144 fi
145 if [ $XENBALLOON_SEND_VMSTAT ]; then
146 xenstore-write memory/vmstat "`cat /proc/vmstat`"
147 fi
148 if [ $XENBALLOON_SEND_UPTIME ]; then
149 xenstore-write memory/uptime "`cat /proc/uptime`"
150 fi
151 }
154 curpreswappages() {
155 pages=$(cat $XENBALLOON_PRESWAP_SYSFILE)
156 RETVAL=$pages
157 return # value returned in RETVAL in pages
158 }
160 preswaphysteresis() {
161 RETVAL=$XENBALLOON_PRESWAP_HYSTERESIS
162 if [ $xenstore_enabled = "true" ]; then
163 if xenstore-exists memory/preswaphysteresis ; then
164 RETVAL=`xenstore-read memory/preswaphysteresis`
165 fi
166 fi
167 return
168 }
170 preswapinertia() {
171 RETVAL=$XENBALLOON_PRESWAP_INERTIA
172 if [ $xenstore_enabled = "true" ]; then
173 if xenstore-exists memory/preswapinertia ; then
174 RETVAL=`xenstore-read memory/preswapinertia`
175 fi
176 fi
177 return
178 }
180 send_preswap_stats() {
181 if [ ! $xenstore_enabled = "true" ]; then
182 return
183 fi
184 curpreswappages
185 preswap_pgs=$RETVAL
186 if [ $XENBALLOON_SEND_PRESWAP ]; then
187 xenstore-write memory/preswap "$preswap_pgs"
188 fi
189 }
191 shrink_preswap() {
192 if [ "$XENBALLOON_PRESWAP_SHRINK" = "false" ]; then
193 return
194 fi
195 if [ ! -f "$XENBALLOON_PRESWAP_SYSFILE" ]; then
196 return
197 fi
198 curpreswappages
199 preswaplast=$preswapnow
200 preswapnow=$RETVAL
201 if [ $preswapnow -eq 0 -o $preswapnow -ne $preswaplast ]; then
202 preswapinertia
203 preswapinertiacounter=$RETVAL
204 return
205 fi
206 let "preswapinertiacounter=$preswapinertiacounter-1"
207 if [ $preswapinertiacounter -ne 0 ]; then
208 return
209 fi
210 preswaphysteresis
211 preswaphys=$RETVAL
212 if [ $preswaphys -eq 0 ]; then
213 return
214 fi
215 let "tgtpreswappages=$(( $preswapnow - \
216 ( $preswapnow / $preswaphys ) ))"
217 preswapinertia
218 preswapinertiacounter=$RETVAL
219 echo $tgtpreswappages > "$XENBALLOON_PRESWAP_SYSFILE"
220 }
222 if [ ! -f /proc/xen/balloon ]; then
223 echo "$0: no balloon driver installed"
224 exit 0
225 fi
226 if [ ! -f /proc/meminfo ]; then
227 echo "$0: can't read /proc/meminfo"
228 exit 0
229 fi
230 xenstore_enabled=true
231 if [ -f /usr/bin/xenstore-exists -a -f /usr/bin/xenstore-read -a \
232 -f /usr/bin/xenstore-write ]; then
233 xenstore_enabled=true
234 else
235 echo "$0: missing /usr/bin/xenstore-* tools, disabling directed ballooning"
236 xenstore_enabled=false
237 fi
238 preswapnow=0
240 . /etc/sysconfig/xenballoon.conf
242 while true;
243 do
244 # handle special case for PV domains with hot-add memory
245 if [ ! -f $XENBALLOON_MAXMEMFILE ]; then
246 maxkb=0
247 else
248 maxkb=`cat $XENBALLOON_MAXMEMFILE`
249 fi
250 curkb=`grep MemTotal /proc/meminfo | sed 's/ */ /' | cut -f2 -d' '`
251 if [ $curkb -gt $maxkb ]; then
252 echo $curkb > $XENBALLOON_MAXMEMFILE
253 fi
254 interval=$XENBALLOON_INTERVAL
255 # do self-ballooning
256 selfballoon_eval
257 if [ $selfballoon_enabled = "true" ]; then
258 balloon_to_target 1
259 interval=$XENBALLOON_SELF_INTERVAL
260 # or do directed ballooning
261 elif [ $xenstore_enabled = "true" ]; then
262 if xenstore-exists memory/target ; then
263 tgtkb=`xenstore-read memory/target`
264 balloon_to_target $tgtkb
265 fi
266 interval=$XENBALLOON_INTERVAL
267 fi
268 shrink_preswap
269 send_memory_stats
270 send_preswap_stats
271 if [ $xenstore_enabled = "true" ]; then
272 if xenstore-exists memory/interval ; then
273 interval=`xenstore-read memory/interval`
274 fi
275 fi
276 sleep $interval
277 done &