ia64/xen-unstable

changeset 5932:2c0036a1cf4f

This patch does 2 jobs:

- Enforce the number of CPUs dom0 will take. See the new variable
"dom0-cpus" in xend-config.sxp (you will want to set this variable to
1 on SMP systems)

- Balloon out dom0 memory when creating domU, if there is not enough
free memory. The lowest level we will balloon out is configured via
the new variable "dom0-min-mem" in xend-config.sxp


I still have a doubt: where to put the code to enforce dom0-cpus. At
the moment I put it into
python/xen/xend/server/SrvDaemon.py, and hopefully that is resonable
enough. Any comment?



Signed-off-by: Nguyen Anh Quynh <aquynh@gmail.com>
author kaf24@firebug.cl.cam.ac.uk
date Fri Jul 29 10:36:53 2005 +0000 (2005-07-29)
parents 52260d8c2775
children c4cb2167d3c7
files tools/examples/xend-config.sxp tools/python/xen/xend/XendRoot.py tools/python/xen/xend/server/SrvDaemon.py tools/python/xen/xm/create.py
line diff
     1.1 --- a/tools/examples/xend-config.sxp	Fri Jul 29 10:36:11 2005 +0000
     1.2 +++ b/tools/examples/xend-config.sxp	Fri Jul 29 10:36:53 2005 +0000
     1.3 @@ -44,3 +44,11 @@
     1.4  # Setup script for enbd-backed block devices
     1.5  (block-enbd block-enbd)
     1.6  
     1.7 +# Dom0 will balloon out when needed to free memory for domU.
     1.8 +# dom0-min-mem is the lowest memory level (in MB) dom0 will get down to.
     1.9 +# If dom0-min-mem=0, dom0 will never balloon out.
    1.10 +(dom0-min-mem 0)
    1.11 +
    1.12 +# In SMP system, dom0 will use only CPUs in range [1,dom0-cpus]
    1.13 +# If dom0-cpus = 0, dom0 will take all cpus available
    1.14 +(dom0-cpus 0)
     2.1 --- a/tools/python/xen/xend/XendRoot.py	Fri Jul 29 10:36:11 2005 +0000
     2.2 +++ b/tools/python/xen/xend/XendRoot.py	Fri Jul 29 10:36:53 2005 +0000
     2.3 @@ -76,6 +76,10 @@ class XendRoot:
     2.4      """Default port xend serves consoles at. """
     2.5      console_port_base_default = '9600'
     2.6  
     2.7 +    dom0_min_mem_default = '0'
     2.8 +
     2.9 +    dom0_cpus_default = '0'
    2.10 +
    2.11      components = {}
    2.12  
    2.13      def __init__(self):
    2.14 @@ -329,6 +333,12 @@ class XendRoot:
    2.15      def get_vif_antispoof(self):
    2.16          return self.get_config_bool('vif-antispoof', 'yes')
    2.17  
    2.18 +    def get_dom0_min_mem(self):
    2.19 +        return self.get_config_int('dom0-min-mem', self.dom0_min_mem_default)
    2.20 +
    2.21 +    def get_dom0_cpus(self):
    2.22 +        return self.get_config_int('dom0-cpus', self.dom0_cpus_default)
    2.23 +
    2.24  def instance():
    2.25      """Get an instance of XendRoot.
    2.26      Use this instead of the constructor.
     3.1 --- a/tools/python/xen/xend/server/SrvDaemon.py	Fri Jul 29 10:36:11 2005 +0000
     3.2 +++ b/tools/python/xen/xend/server/SrvDaemon.py	Fri Jul 29 10:36:53 2005 +0000
     3.3 @@ -5,7 +5,6 @@
     3.4  ###########################################################
     3.5  
     3.6  import os
     3.7 -import os.path
     3.8  import signal
     3.9  import sys
    3.10  import threading
    3.11 @@ -16,6 +15,7 @@ import re
    3.12  import StringIO
    3.13  import traceback
    3.14  import time
    3.15 +import glob
    3.16  
    3.17  from xen.lowlevel import xu
    3.18  
    3.19 @@ -25,6 +25,7 @@ from xen.xend import EventServer; eserve
    3.20  from xen.xend.XendError import XendError
    3.21  from xen.xend.server import SrvServer
    3.22  from xen.xend.XendLogging import log
    3.23 +from xen.xend import XendRoot; xroot = XendRoot.instance()
    3.24  
    3.25  import channel
    3.26  import controller
    3.27 @@ -327,6 +328,7 @@ class Daemon:
    3.28          return self.cleanup(kill=True)
    3.29  
    3.30      def run(self):
    3.31 +        _enforce_dom0_cpus()
    3.32          try:
    3.33              log.info("Xend Daemon started")
    3.34              self.createFactories()
    3.35 @@ -363,6 +365,32 @@ class Daemon:
    3.36          #sys.exit(rc)
    3.37          os._exit(rc)
    3.38  
    3.39 +def _enforce_dom0_cpus():
    3.40 +    dn = xroot.get_dom0_cpus()
    3.41 +
    3.42 +    for d in glob.glob("/sys/devices/system/cpu/cpu*"):
    3.43 +        cpu = int(os.path.basename(d)[3:])
    3.44 +        if (dn == 0) or (cpu < dn):
    3.45 +            v = "1"
    3.46 +        else:
    3.47 +            v = "0"
    3.48 +        try:
    3.49 +            f = open("%s/online" %d, "r+")
    3.50 +            c = f.read(1)
    3.51 +            if (c != v):
    3.52 +                if v == "0":
    3.53 +                    log.info("dom0 is trying to give back cpu %d", cpu)
    3.54 +                else:
    3.55 +                    log.info("dom0 is trying to take cpu %d", cpu)
    3.56 +                f.seek(0)
    3.57 +                f.write(v)
    3.58 +                f.close()
    3.59 +                log.info("dom0 successfully enforced cpu %d", cpu)
    3.60 +            else:
    3.61 +                f.close()
    3.62 +        except:
    3.63 +            pass
    3.64 +
    3.65  def instance():
    3.66      global inst
    3.67      try:
     4.1 --- a/tools/python/xen/xm/create.py	Fri Jul 29 10:36:11 2005 +0000
     4.2 +++ b/tools/python/xen/xm/create.py	Fri Jul 29 10:36:53 2005 +0000
     4.3 @@ -1,4 +1,5 @@
     4.4  # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
     4.5 +# Copyright (C) 2005 Nguyen Anh Quynh <aquynh@gmail.com>
     4.6  
     4.7  """Domain creation.
     4.8  """
     4.9 @@ -7,10 +8,13 @@ import string
    4.10  import sys
    4.11  import socket
    4.12  
    4.13 +import xen.lowlevel.xc
    4.14 +
    4.15  from xen.xend import sxp
    4.16  from xen.xend import PrettyPrint
    4.17  from xen.xend.XendClient import server, XendError
    4.18  from xen.xend.XendBootloader import bootloader
    4.19 +from xen.xend import XendRoot; xroot = XendRoot.instance()
    4.20  from xen.util import blkif
    4.21  
    4.22  from xen.util import console_client
    4.23 @@ -644,6 +648,36 @@ def make_domain(opts, config):
    4.24                % (dom, console_port))
    4.25      return (dom, console_port)
    4.26  
    4.27 +def get_dom0_alloc():
    4.28 +    """Return current allocation memory of dom0 (in MB). Return 0 on error"""
    4.29 +    PROC_XEN_BALLOON = "/proc/xen/balloon"
    4.30 +
    4.31 +    f = open(PROC_XEN_BALLOON, "r")
    4.32 +    line = f.readline()
    4.33 +    for x in line.split():
    4.34 +        for n in x:
    4.35 +            if not n.isdigit():
    4.36 +                break
    4.37 +        else:
    4.38 +            f.close()
    4.39 +            return int(x)/1024
    4.40 +    f.close()
    4.41 +    return 0
    4.42 +
    4.43 +def balloon_out(dom0_min_mem, opts):
    4.44 +    """Balloon out to get memory for domU, if necessarily"""
    4.45 +    SLACK = 4
    4.46 +
    4.47 +    xc = xen.lowlevel.xc.new()
    4.48 +    pinfo = xc.physinfo()
    4.49 +    free_mem = pinfo['free_pages']/256
    4.50 +    if free_mem < opts.vals.memory + SLACK:
    4.51 +        need_mem = opts.vals.memory + SLACK - free_mem
    4.52 +        cur_alloc = get_dom0_alloc()
    4.53 +        if cur_alloc - need_mem >= dom0_min_mem:
    4.54 +            server.xend_domain_mem_target_set(0, cur_alloc - need_mem)
    4.55 +    del xc
    4.56 +
    4.57  def main(argv):
    4.58      opts = gopts
    4.59      args = opts.parse(argv)
    4.60 @@ -671,6 +705,10 @@ def main(argv):
    4.61      if opts.vals.dryrun:
    4.62          PrettyPrint.prettyprint(config)
    4.63      else:
    4.64 +        dom0_min_mem = xroot.get_dom0_min_mem()
    4.65 +        if dom0_min_mem != 0:
    4.66 +            balloon_out(dom0_min_mem, opts)
    4.67 +
    4.68          (dom, console) = make_domain(opts, config)
    4.69          if opts.vals.console_autoconnect:
    4.70              path = "/var/lib/xend/console-%s" % console