ia64/xen-unstable

view tools/xenballoon/xenballoond @ 19731:01748ccc4da3

Intel VT-d: fix Stoakley boot issue with iommu=1

Signed-off-by: Weidong Han <Weidong.han@intel.com>
Signed-off-by: Allen Kay <allen.m.kay@intel.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jun 05 09:25:50 2009 +0100 (2009-06-05)
parents 0c5183e764d8
children 8373971089a7
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 # Version: 080630
9 #
10 # Two "policies" are implemented:
11 # - Selfballooning: Adjust memory periodically, with no (or little) input
12 # from domain0. Target memory is determined solely by the
13 # Committed_AS line in /proc/meminfo, but parameters may adjust
14 # the rate at which the target is achieved.
15 # - Directed ballooning: Adjust memory solely as directed by domain0
16 #
17 # Under some circumstances, "output" may also be generated; the contents
18 # of /proc/meminfo and /proc/vmstat may be periodically placed on xenbus.
19 #
20 # If xenbus is running and the /usr/bin/xenstore-* tools are installed,
21 # "xenbus is enabled".
22 #
23 # Parameters are documented in /etc/sysconfig/xenballoon.conf. Although
24 # some are not used with directed ballooning, all must be set properly.
25 # If xenbus is enabled, some of these parameters may be overridden by values
26 # set by domain0 via xenbus.
28 minmb() {
29 RETVAL=$XENBALLOON_MINMEM
30 if [ $RETVAL -ne 0 ]; then
31 return $RETVAL
32 fi
33 kb=`cat $XENBALLOON_MAXMEMFILE`
34 let "mb=$kb/1024"
35 let "pages=$kb/4"
36 # this algorithm from drivers/xen/balloon/balloon.c:minimum_target()
37 # which was added to balloon.c in 2008 to avoid ballooning too small
38 # it is unnecessary here except to accomodate pre-2008 balloon drivers
39 # note that ranges are adjusted because a VM with "memory=1024"
40 # gets somewhat less than 1024MB
41 if [ $mb -lt 125 ]; then
42 let RETVAL="$(( 8 + ($pages >> 9) ))"
43 elif [ $mb -lt 500 ]; then
44 let RETVAL="$(( 40 + ($pages >> 10) ))"
45 elif [ $mb -lt 2000 ]; then
46 let RETVAL="$(( 104 + ($pages >> 11) ))"
47 else
48 let RETVAL="$(( 296 + ($pages >> 13) ))"
49 fi
50 return # value returned in RETVAL in mB
51 }
53 curkb() {
54 kb=`grep MemTotal /proc/meminfo | sed 's/ */ /' | \
55 cut -f2 -d' '`
56 RETVAL=$kb
57 return # value returned in RETVAL in kB
58 }
60 downhysteresis() {
61 RETVAL=$XENBALLOON_AUTO_DOWNHYSTERESIS
62 if [ $xenstore_enabled = "true" ]; then
63 if xenstore-exists memory/downhysteresis ; then
64 RETVAL=`xenstore-read memory/downhysteresis`
65 fi
66 fi
67 return
68 }
70 uphysteresis() {
71 RETVAL=$XENBALLOON_AUTO_UPHYSTERESIS
72 if [ $xenstore_enabled = "true" ]; then
73 if xenstore-exists memory/uphysteresis ; then
74 RETVAL=`xenstore-read memory/uphysteresis`
75 fi
76 fi
77 return
78 }
80 selfballoon_eval() {
81 if [ $xenstore_enabled = "true" ]; then
82 if xenstore-exists memory/selfballoon; then
83 RETVAL=`xenstore-read memory/selfballoon`
84 if [ $RETVAL -eq 1 ]; then
85 selfballoon_enabled=true
86 return
87 fi
88 fi
89 fi
90 selfballoon_enabled=$XENBALLOON_SELF
91 return
92 }
94 selftarget() {
95 tgtkb=`grep Committed_AS /proc/meminfo | sed 's/ */ /' | cut -f2 -d' '`
96 minmb
97 let "minbytes=$RETVAL*1024*1024"
98 let "tgtbytes=$tgtkb*1024"
99 if [ $tgtbytes -lt $minbytes ]; then
100 let "tgtbytes=$minbytes"
101 fi
102 RETVAL=$tgtbytes # value returned in RETVAL in bytes
103 return
104 }
106 # $1 == 1 means use selftarget, else target in kB
107 balloon_to_target() {
108 if [ "$1" -eq 1 ]; then
109 selftarget
110 tgtbytes=$RETVAL
111 else
112 let "tgtbytes=$(( $1 * 1024 ))"
113 fi
114 curkb
115 let "curbytes=$RETVAL*1024"
116 if [ $curbytes -gt $tgtbytes ]; then
117 downhysteresis
118 downhys=$RETVAL
119 if [ $downhys -ne 0 ]; then
120 let "tgtbytes=$(( $curbytes - \
121 ( ( $curbytes - $tgtbytes ) / $downhys ) ))"
122 fi
123 else if [ $curbytes -lt $tgtbytes ]; then
124 uphysteresis
125 uphys=$RETVAL
126 let "tgtbytes=$(( $curbytes + \
127 ( ( $tgtbytes - $curbytes ) / $uphys ) ))"
128 fi
129 fi
130 echo $tgtbytes > /proc/xen/balloon
131 if [ $xenstore_enabled = "true" ]; then
132 let "tgtkb=$(( $tgtbytes/1024 ))"
133 xenstore-write memory/selftarget $tgtkb
134 fi
135 }
137 send_memory_stats() {
138 if [ ! $xenstore_enabled = "true" ]; then
139 return
140 fi
141 if [ $XENBALLOON_SEND_MEMINFO ]; then
142 xenstore-write memory/meminfo "`cat /proc/meminfo`"
143 fi
144 if [ $XENBALLOON_SEND_VMSTAT ]; then
145 xenstore-write memory/vmstat "`cat /proc/vmstat`"
146 fi
147 if [ $XENBALLOON_SEND_UPTIME ]; then
148 xenstore-write memory/uptime "`cat /proc/uptime`"
149 fi
150 }
152 if [ ! -f /proc/xen/balloon ]; then
153 echo "$0: no balloon driver installed"
154 exit 0
155 fi
156 if [ ! -f /proc/meminfo ]; then
157 echo "$0: can't read /proc/meminfo"
158 exit 0
159 fi
160 xenstore_enabled=true
161 if [ -f /usr/bin/xenstore-exists -a -f /usr/bin/xenstore-read -a \
162 -f /usr/bin/xenstore-write ]; then
163 xenstore_enabled=true
164 else
165 echo "$0: missing /usr/bin/xenstore-* tools, disabling directed ballooning"
166 xenstore_enabled=false
167 fi
169 . /etc/sysconfig/xenballoon.conf
171 while true;
172 do
173 # handle special case for PV domains with hot-add memory
174 if [ ! -f $XENBALLOON_MAXMEMFILE ]; then
175 maxkb=0
176 else
177 maxkb=`cat $XENBALLOON_MAXMEMFILE`
178 fi
179 curkb=`grep MemTotal /proc/meminfo | sed 's/ */ /' | cut -f2 -d' '`
180 if [ $curkb -gt $maxkb ]; then
181 echo $curkb > $XENBALLOON_MAXMEMFILE
182 fi
183 interval=$XENBALLOON_INTERVAL
184 # do self-ballooning
185 selfballoon_eval
186 if [ $selfballoon_enabled = "true" ]; then
187 balloon_to_target 1
188 interval=$XENBALLOON_SELF_INTERVAL
189 # or do directed ballooning
190 elif [ $xenstore_enabled = "true" ]; then
191 if xenstore-exists memory/target ; then
192 tgtkb=`xenstore-read memory/target`
193 balloon_to_target $tgtkb
194 fi
195 interval=$XENBALLOON_INTERVAL
196 fi
197 send_memory_stats
198 if [ $xenstore_enabled = "true" ]; then
199 if xenstore-exists memory/interval ; then
200 interval=`xenstore-read memory/interval`
201 fi
202 fi
203 sleep $interval
204 done &