ia64/xen-unstable

changeset 4982:09d09fe7cd07

bitkeeper revision 1.1448.1.1 (428b5d756NfzYJ90kQUy0Q91tiUWhw)

scheduler.py:
Simplify scheduler by removing support for cancelling timeouts.
Also add a ``now'' function to run a function in a seperate thread.
XendDomain.py:
g/c gratuitous refresh() calls.
Drive domain restart off the virq signaling that a domain has terminated.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Wed May 18 15:21:25 2005 +0000 (2005-05-18)
parents 1034f1c883a8
children 8d9929ad652c
files tools/python/xen/xend/XendDomain.py tools/python/xen/xend/scheduler.py
line diff
     1.1 --- a/tools/python/xen/xend/XendDomain.py	Wed May 18 13:48:31 2005 +0000
     1.2 +++ b/tools/python/xen/xend/XendDomain.py	Wed May 18 15:21:25 2005 +0000
     1.3 @@ -111,35 +111,6 @@ class XendDomain:
     1.4          print 'onVirq>', val
     1.5          self.refresh()
     1.6  
     1.7 -    def schedule_later(self, _delay, _name, _fn, *args):
     1.8 -        """Schedule a function to be called later (if not already scheduled).
     1.9 -
    1.10 -        @param _delay: delay in seconds
    1.11 -        @param _name:  schedule name
    1.12 -        @param _fn:    function
    1.13 -        @param args:   arguments
    1.14 -        """
    1.15 -        self.scheduler.later(_delay, _name, _fn, args)
    1.16 -        
    1.17 -    def schedule_cancel(self, name):
    1.18 -        """Cancel a scheduled function call.
    1.19 -        
    1.20 -        @param name: schedule name to cancel
    1.21 -        """
    1.22 -        self.scheduler.cancel(name)
    1.23 -
    1.24 -    def domain_restarts_schedule(self, delay=1):
    1.25 -        """Schedule domain_restarts to be called later.
    1.26 -        
    1.27 -        @param delay: delay in seconds
    1.28 -        """
    1.29 -        self.schedule_later(delay, 'domain_restarts', self.domain_restarts)
    1.30 -        
    1.31 -    def domain_restarts_cancel(self):
    1.32 -        """Cancel any scheduled call of domain_restarts.
    1.33 -        """
    1.34 -        self.schedule_cancel('domain_restarts')
    1.35 -        
    1.36      def rm_all(self):
    1.37          """Remove all domain info. Used after reboot.
    1.38          """
    1.39 @@ -302,8 +273,6 @@ class XendDomain:
    1.40                 eserver.inject('xend.domain.exit', [name, id, 'crash']) 
    1.41              destroyed += 1
    1.42              self.final_domain_destroy(id)
    1.43 -        if self.domain_restarts_exist():
    1.44 -            self.domain_restarts_schedule()
    1.45  
    1.46      def refresh(self):
    1.47          """Refresh domain list from Xen.
    1.48 @@ -316,14 +285,17 @@ class XendDomain:
    1.49                  self.domain_lookup(id)
    1.50          # Remove entries for domains that no longer exist.
    1.51          # Update entries for existing domains.
    1.52 +        do_domain_restarts = False
    1.53          for d in self.domain_by_id.values():
    1.54              info = doms.get(d.id)
    1.55              if info:
    1.56                  d.update(info)
    1.57              elif d.restart_pending():
    1.58 -                pass
    1.59 +                do_domain_restarts = True
    1.60              else:
    1.61                  self._delete_domain(d.id)
    1.62 +        if do_domain_restarts:
    1.63 +            self.scheduler.now(self.domain_restarts)
    1.64  
    1.65      def update_domain(self, id):
    1.66          """Update the saved info for a domain.
    1.67 @@ -360,7 +332,6 @@ class XendDomain:
    1.68  
    1.69          @return: domain names
    1.70          """
    1.71 -        self.refresh()
    1.72          return self.domain_by_id.keys()
    1.73  
    1.74      def domains(self):
    1.75 @@ -368,7 +339,6 @@ class XendDomain:
    1.76  
    1.77          @return: domain objects
    1.78          """
    1.79 -        self.refresh()
    1.80          return self.domain_by_id.values()
    1.81      
    1.82      def domain_create(self, config):
    1.83 @@ -515,7 +485,6 @@ class XendDomain:
    1.84          """Process pending domain shutdowns.
    1.85          Destroys domains whose shutdowns have timed out.
    1.86          """
    1.87 -        self.schedule_cancel('domain_shutdowns')
    1.88          timeout = SHUTDOWN_TIMEOUT
    1.89          for shutdown in self.shutdowns_by_id.values():
    1.90              id = shutdown.getDomain()
    1.91 @@ -538,7 +507,7 @@ class XendDomain:
    1.92                  timeout = min(timeout, shutdown.getTimeout())
    1.93          if self.shutdowns_by_id:
    1.94              # Pending shutdowns remain - reschedule.
    1.95 -            self.schedule_later(timeout, 'domain_shutdowns', self.domain_shutdowns)
    1.96 +            self.scheduler.later(timeout, self.domain_shutdowns)
    1.97  
    1.98      def domain_restart_schedule(self, id, reason, force=False):
    1.99          """Schedule a restart for a domain if it needs one.
   1.100 @@ -563,7 +532,6 @@ class XendDomain:
   1.101          log.info('Scheduling restart for domain: name=%s id=%s', dominfo.name, dominfo.id)
   1.102          eserver.inject("xend.domain.restart",
   1.103                         [dominfo.name, dominfo.id, "schedule"])
   1.104 -        self.domain_restarts_schedule()
   1.105              
   1.106      def domain_restart_cancel(self, id):
   1.107          """Cancel any restart scheduled for a domain.
   1.108 @@ -583,7 +551,6 @@ class XendDomain:
   1.109      def domain_restarts(self):
   1.110          """Execute any scheduled domain restarts for domains that have gone.
   1.111          """
   1.112 -        self.domain_restarts_cancel()
   1.113          doms = self.xen_domains()
   1.114          for dominfo in self.restarts_by_id.values():
   1.115              print 'domain_restarts>', dominfo.name, dominfo.id
   1.116 @@ -597,13 +564,7 @@ class XendDomain:
   1.117              del self.restarts_by_name[dominfo.name]
   1.118              print 'domain_restarts> restarting: ', dominfo.name
   1.119              self.domain_restart(dominfo)
   1.120 -        if self.domain_restarts_exist():
   1.121 -            # Run again later if any restarts remain.
   1.122 -            self.domain_restarts_schedule(delay=10)
   1.123  
   1.124 -    def domain_restarts_exist(self):
   1.125 -        return len(self.restarts_by_id)
   1.126 -        
   1.127      def final_domain_destroy(self, id):
   1.128          """Final destruction of a domain..
   1.129  
   1.130 @@ -631,7 +592,6 @@ class XendDomain:
   1.131          """
   1.132          self.domain_restart_schedule(id, reason, force=True)
   1.133          val = self.final_domain_destroy(id)
   1.134 -        self.refresh()
   1.135          return val
   1.136  
   1.137      def domain_migrate(self, id, dst, live=False, resource=0):
     2.1 --- a/tools/python/xen/xend/scheduler.py	Wed May 18 13:48:31 2005 +0000
     2.2 +++ b/tools/python/xen/xend/scheduler.py	Wed May 18 15:21:25 2005 +0000
     2.3 @@ -2,50 +2,28 @@ import threading
     2.4  
     2.5  class Scheduler:
     2.6  
     2.7 -    def __init__(self):
     2.8 -        self.lock = threading.Lock()
     2.9 -        self.schedule = {}
    2.10 -
    2.11 -    def later(self, _delay, _name, _fn, args, kwargs={}):
    2.12 -        """Schedule a function to be called later (if not already scheduled).
    2.13 +    def later(self, _delay, _fn, args=(), kwargs={}):
    2.14 +        """Schedule a function to be called later.
    2.15  
    2.16          @param _delay: delay in seconds
    2.17 -        @param _name:  schedule name
    2.18          @param _fn:    function
    2.19          @param args:   arguments (list)
    2.20          @param kwargs  keyword arguments (map)
    2.21          """
    2.22 -        try:
    2.23 -            self.lock.acquire()
    2.24 -            if self.schedule.get(_name): return
    2.25 -            runargs = [ _name, _fn, args, kwargs ]
    2.26 -            timer = threading.Timer(_delay, self._run, args=runargs)
    2.27 -            self.schedule[_name] = timer
    2.28 -        finally:
    2.29 -            self.lock.release()
    2.30 +        runargs = [ _fn, args, kwargs ]
    2.31 +        timer = threading.Timer(_delay, self._run, args=runargs)
    2.32          timer.start()
    2.33  
    2.34 -    def cancel(self, name):
    2.35 -        """Cancel a scheduled function call.
    2.36 -        
    2.37 -        @param name: schedule name to cancel
    2.38 -        """
    2.39 -        timer = self._remove(name)
    2.40 -        if timer:
    2.41 -            timer.cancel()
    2.42 +    def now(self, _fn, args=(), kwargs={}):
    2.43 +        """Schedule a function to be called now.
    2.44  
    2.45 -    def _remove(self, name):
    2.46 -        try:
    2.47 -            self.lock.acquire()
    2.48 -            timer = self.schedule.get(name)
    2.49 -            if timer:
    2.50 -                del self.schedule[name]
    2.51 -            return timer
    2.52 -        finally:
    2.53 -            self.lock.release()
    2.54 +        @param _fn:    function
    2.55 +        @param args:   arguments (list)
    2.56 +        @param kwargs  keyword arguments (map)
    2.57 +        """
    2.58 +        runargs = [ _fn, args, kwargs ]
    2.59 +        thread = threading.Thread(target=self._run, args=runargs)
    2.60 +        thread.start()
    2.61  
    2.62 -    def _run(self, name, fn, args, kwargs):
    2.63 -        self._remove(name)
    2.64 +    def _run(self, fn, args, kwargs):
    2.65          fn(*args, **kwargs)
    2.66 -
    2.67 -