ia64/xen-unstable

changeset 9221:cc303cdf8809

The attached patch cleans up the parsing of /proc/xen/balloon that
occurs in xend. Currently, the parsing is hard-coded; it expects that
the desired number is on the first line. My patch is more robust; it
lets values be retrieved by a label. It also exposes a few functions to
retrieve the current allocation and the target allocation.

By abstracting this a little better, I'll be able to address a few
other issues in subsequent patches. Mostly, this allows for better
error checking and guidance for the user. For example, by exposing
get_dom0_current_alloc(), we can (elsewhere, in a later patch) calculate
how large of a VM could potentially be started. This is useful, as it
lets simple VM-creation GUIs guide the user towards reasonable memory
values for new VMs.

Signed-off-by: Charles Coffing <ccoffing@novell.com>
author emellor@leeni.uk.xensource.com
date Fri Mar 10 11:05:59 2006 +0100 (2006-03-10)
parents e0741ec5ec52
children c5286130a96e 83a882b3d807
files tools/python/xen/xend/balloon.py
line diff
     1.1 --- a/tools/python/xen/xend/balloon.py	Fri Mar 10 10:54:25 2006 +0100
     1.2 +++ b/tools/python/xen/xend/balloon.py	Fri Mar 10 11:05:59 2006 +0100
     1.3 @@ -27,7 +27,8 @@ from XendLogging import log
     1.4  from XendError import VmError
     1.5  
     1.6  
     1.7 -PROC_XEN_BALLOON = "/proc/xen/balloon"
     1.8 +PROC_XEN_BALLOON = '/proc/xen/balloon'
     1.9 +
    1.10  BALLOON_OUT_SLACK = 1 # MiB.  We need this because the physinfo details are
    1.11                        # rounded.
    1.12  RETRY_LIMIT = 10
    1.13 @@ -39,6 +40,47 @@ RETRY_LIMIT = 10
    1.14  # such requirements.
    1.15  SLEEP_TIME_GROWTH = 0.1
    1.16  
    1.17 +# A mapping between easy-to-remember labels and the more verbose
    1.18 +# label actually shown in the PROC_XEN_BALLOON file.
    1.19 +labels = { 'current'      : 'Current allocation',
    1.20 +           'target'       : 'Requested target',
    1.21 +           'low-balloon'  : 'Low-mem balloon',
    1.22 +           'high-balloon' : 'High-mem balloon',
    1.23 +           'limit'        : 'Xen hard limit' }
    1.24 +
    1.25 +def _get_proc_balloon(label):
    1.26 +    """Returns the value for the named label.  Returns None if the label was
    1.27 +       not found or the value was non-numeric."""
    1.28 +
    1.29 +    f = file(PROC_XEN_BALLOON, 'r')
    1.30 +    try:
    1.31 +        for line in f:
    1.32 +            keyvalue = line.split(':')
    1.33 +            if keyvalue[0] == label:
    1.34 +                values = keyvalue[1].split()
    1.35 +                if values[0].isdigit():
    1.36 +                    return int(values[0])
    1.37 +                else:
    1.38 +                    return None
    1.39 +        return None
    1.40 +    finally:
    1.41 +        f.close()
    1.42 +
    1.43 +def get_dom0_current_alloc():
    1.44 +    """Returns the current memory allocation (in MiB) of dom0."""
    1.45 +
    1.46 +    kb = _get_proc_balloon(labels['current'])
    1.47 +    if kb == None:
    1.48 +        raise VmError('Failed to query current memory allocation of dom0.')
    1.49 +    return kb / 1024
    1.50 +
    1.51 +def get_dom0_target_alloc():
    1.52 +    """Returns the target memory allocation (in MiB) of dom0."""
    1.53 +
    1.54 +    kb = _get_proc_balloon(labels['target'])
    1.55 +    if kb == None:
    1.56 +        raise VmError('Failed to query target memory allocation of dom0.')
    1.57 +    return kb / 1024
    1.58  
    1.59  def free(required):
    1.60      """Balloon out memory from the privileged domain so that there is the
    1.61 @@ -88,7 +130,7 @@ def free(required):
    1.62                  log.debug("Balloon: free %d; need %d.", free_mem, need_mem)
    1.63  
    1.64              if dom0_min_mem > 0:
    1.65 -                dom0_alloc = _get_dom0_alloc()
    1.66 +                dom0_alloc = get_dom0_current_alloc()
    1.67                  new_alloc = dom0_alloc - (need_mem - free_mem)
    1.68  
    1.69                  if (new_alloc >= dom0_min_mem and
    1.70 @@ -121,20 +163,3 @@ def free(required):
    1.71  
    1.72      finally:
    1.73          del xc
    1.74 -
    1.75 -
    1.76 -def _get_dom0_alloc():
    1.77 -    """Return current allocation memory of dom0 (in MiB). Return 0 on error"""
    1.78 -
    1.79 -    f = file(PROC_XEN_BALLOON, 'r')
    1.80 -    try:
    1.81 -        line = f.readline()
    1.82 -        for x in line.split():
    1.83 -            for n in x:
    1.84 -                if not n.isdigit():
    1.85 -                    break
    1.86 -            else:
    1.87 -                return int(x) / 1024
    1.88 -        return 0
    1.89 -    finally:
    1.90 -        f.close()