3030log = logging .getLogger ("qmemman.daemon.algo" )
3131
3232
33- # untrusted meminfo size is taken from xenstore key, thus its size is limited
34- # so splits do not require excessive memory
3533def sanitize_and_parse_meminfo (untrusted_meminfo ):
34+ # Untrusted meminfo size is read from xenstore, thus its size is limited
35+ # and splits do not require excessive memory.
3636 if not untrusted_meminfo :
3737 return None
38-
39- # new syntax - just one int
40- if untrusted_meminfo .isdigit ():
41- return int (untrusted_meminfo ) * 1024
42-
43- return None
38+ if not untrusted_meminfo .isdigit ():
39+ return None
40+ return int (untrusted_meminfo ) * 1024
4441
4542
46- # called when a domain updates its 'meminfo' xenstore key
43+ # Called when a domain updates its 'meminfo' xenstore key.
4744def refresh_meminfo_for_domain (domain , untrusted_xenstore_key ):
4845 domain .mem_used = sanitize_and_parse_meminfo (untrusted_xenstore_key )
4946
5047
5148def prefmem (domain ):
52- # dom0 is special, as it must have large cache, for vbds. Thus , give it
53- # a special boost
49+ # As dom0 must have large cache for vbds, give it a special boost.
50+ mem_used = domain . mem_used * CACHE_FACTOR
5451 if domain .domid == "0" :
55- return int (
56- min (
57- domain .mem_used * CACHE_FACTOR + DOM0_MEM_BOOST ,
58- domain .memory_maximum ,
59- )
60- )
61- return int (
62- max (
63- min (domain .mem_used * CACHE_FACTOR , domain .memory_maximum ),
64- MIN_PREFMEM ,
65- )
66- )
52+ mem_used += DOM0_MEM_BOOST
53+ return int (min (mem_used , domain .memory_maximum ))
54+ return int (max (min (mem_used , domain .memory_maximum ), MIN_PREFMEM ))
6755
6856
6957def memory_needed (domain ):
70- # do not change
71- # in balance(), "distribute total_available_memory proportionally to
72- # mempref" relies on this exact formula
58+ # Do not change. In balance(), "distribute total_available_memory
59+ # proportionally to mempref" relies on this exact formula.
7360 ret = prefmem (domain ) - domain .memory_actual
7461 return ret
7562
7663
77- # prepare list of (domain, memory_target) pairs that need to be passed
78- # to "xm memset" equivalent in order to obtain "memsize" of memory
79- # return empty list when the request cannot be satisfied
64+ # Prepare list of (domain, memory_target) pairs that need to be passed to "xm
65+ # memset" equivalent in order to obtain "memsize".
66+ # Returns empty list when the request cannot be satisfied.
8067def balloon (memsize , domain_dictionary ):
8168 log .debug (
8269 "balloon(memsize={!r}, domain_dictionary={!r})" .format (
@@ -87,9 +74,7 @@ def balloon(memsize, domain_dictionary):
8774 request = []
8875 available = 0
8976 for domid , dom in domain_dictionary .items ():
90- if dom .mem_used is None :
91- continue
92- if dom .no_progress :
77+ if dom .mem_used is None or dom .no_progress :
9378 continue
9479 need = memory_needed (dom )
9580 if need < 0 :
@@ -120,8 +105,8 @@ def balloon(memsize, domain_dictionary):
120105# get stuck. The surplus will return to the VM during "balance" call.
121106
122107
123- # redistribute positive "total_available_memory" of memory between domains,
124- # proportionally to prefmem
108+ # Redistribute positive "total_available_memory" of memory between domains,
109+ # proportionally to prefmem.
125110def balance_when_enough_memory (
126111 domain_dictionary , xen_free_memory , total_mem_pref , total_available_memory
127112):
@@ -133,28 +118,26 @@ def balance_when_enough_memory(
133118 )
134119
135120 target_memory = {}
136- # memory not assigned because of static max
121+ # Memory not assigned because of static max.
137122 left_memory = 0
138123 acceptors_count = 0
139124 for domid , dom in domain_dictionary .items ():
140- if dom .mem_used is None :
125+ if dom .mem_used is None or dom . no_progress :
141126 continue
142- if dom .no_progress :
143- continue
144- # distribute total_available_memory proportionally to mempref
127+ # Distribute total_available_memory proportionally to mempref.
145128 scale = 1.0 * prefmem (dom ) / total_mem_pref
146129 target_nonint = prefmem (dom ) + scale * total_available_memory
147- # prevent rounding errors
130+ # Prevent rounding errors.
148131 target = int (0.999 * target_nonint )
149- # do not try to give more memory than static max
132+ # Do not try to give more memory than static max.
150133 if target > dom .memory_maximum :
151134 left_memory += target - dom .memory_maximum
152135 target = dom .memory_maximum
153136 else :
154- # count domains which can accept more memory
137+ # Count domains which can accept more memory.
155138 acceptors_count += 1
156139 target_memory [domid ] = target
157- # distribute left memory across all acceptors
140+ # Distribute left memory across all acceptors.
158141 while left_memory > 0 and acceptors_count > 0 :
159142 log .info (
160143 "left_memory={} acceptors_count={}" .format (
@@ -179,9 +162,8 @@ def balance_when_enough_memory(
179162 target_memory [domid ] = target
180163 left_memory = new_left_memory
181164 acceptors_count = new_acceptors_count
182- # split target_memory dictionary to donors and acceptors
183- # this is needed to first get memory from donors and only then give it
184- # to acceptors
165+ # Split target_memory dictionary to donors and acceptors. This is needed to
166+ # first get memory from donors and only then give it to acceptors.
185167 donors_rq = []
186168 acceptors_rq = []
187169 for domid , target in target_memory .items ():
@@ -193,8 +175,8 @@ def balance_when_enough_memory(
193175 return donors_rq + acceptors_rq
194176
195177
196- # when not enough mem to make everyone be above prefmem, make donors be at
197- # prefmem, and redistribute anything left between acceptors
178+ # When not enough mem to make everyone be above prefmem, make donors be at
179+ # prefmem, and redistribute anything left between acceptors.
198180def balance_when_low_on_memory (
199181 domain_dictionary ,
200182 xen_free_memory ,
@@ -215,54 +197,52 @@ def balance_when_low_on_memory(
215197 dom = domain_dictionary [domid ]
216198 avail = - memory_needed (dom )
217199 if avail < 10 * 1024 * 1024 :
218- # probably we have already tried making it exactly at prefmem,
219- # give up
200+ # Probably we have already tried making it exactly at prefmem, give
201+ # up.
220202 continue
221203 squeezed_mem -= avail
222204 donors_rq .append ((domid , prefmem (dom )))
223- # the below can happen if initially xen free memory is below 50M
205+ # The below condition can happen if initially xen free memory is below 50M.
224206 if squeezed_mem < 0 :
225207 return donors_rq
226208 for domid in acceptors :
227209 dom = domain_dictionary [domid ]
228210 scale = 1.0 * prefmem (dom ) / total_mem_pref_acceptors
229211 target_nonint = dom .memory_actual + scale * squeezed_mem
230- # do not try to give more memory than static max
212+ # Do not try to give more memory than static max.
231213 target = min (int (0.999 * target_nonint ), dom .memory_maximum )
232214 acceptors_rq .append ((domid , target ))
233215 return donors_rq + acceptors_rq
234216
235217
236- # get memory information
237- # called before and after domain balances
238- # return a dictionary of various memory data points
218+ # Get memory information.
219+ # Called before and after domain balances.
220+ # Return a dictionary of various memory data points.
239221def memory_info (xen_free_memory , domain_dictionary ):
240222 log .debug (
241223 "memory_info(xen_free_memory={!r}, domain_dictionary={!r})" .format (
242224 xen_free_memory , domain_dictionary
243225 )
244226 )
245227
246- # sum of all memory requirements - in other words, the difference between
247- # memory required to be added to domains (acceptors) to make them be
248- # at their preferred memory, and memory that can be taken from domains
249- # (donors) that can provide memory. So, it can be negative when plenty
250- # of memory.
228+ # Sum of all memory requirements - in other words, the difference between
229+ # memory required to be added to domains (acceptors) to make them be at
230+ # their preferred memory, and memory that can be taken from domains
231+ # (donors) that can provide memory. So, it can be negative when plenty of
232+ # memory.
251233 total_memory_needed = 0
252234
253- # sum of memory preferences of all domains
235+ # Sum of memory preferences of all domains.
254236 total_mem_pref = 0
255237
256- # sum of memory preferences of all domains that require more memory
238+ # Sum of memory preferences of all domains that require more memory.
257239 total_mem_pref_acceptors = 0
258240
259241 donors = []
260242 acceptors = []
261- # pass 1: compute the above "total" values
243+ # Pass 1: compute the above "total" values.
262244 for domid , dom in domain_dictionary .items ():
263- if dom .mem_used is None :
264- continue
265- if dom .no_progress :
245+ if dom .mem_used is None or dom .no_progress :
266246 continue
267247 need = memory_needed (dom )
268248 if need < 0 or dom .memory_actual >= dom .memory_maximum :
@@ -286,10 +266,10 @@ def memory_info(xen_free_memory, domain_dictionary):
286266 return mem_dictionary
287267
288268
289- # redistribute memory across domains
290- # called when one of domains update its 'meminfo' xenstore key
291- # return the list of (domain, memory_target) pairs to be passed to
292- # "xm memset" equivalent
269+ # Redistribute memory across domains.
270+ # Called when one of domains update its 'meminfo' xenstore key.
271+ # Return the list of (domain, memory_target) pairs to be passed to "xm memset"
272+ # equivalent
293273def balance (xen_free_memory , domain_dictionary ):
294274 log .debug (
295275 "balance(xen_free_memory={!r}, domain_dictionary={!r})" .format (
0 commit comments