ia64/xen-unstable

changeset 14403:460dac5742cf

Implement VM_metrics Xen-API class.

Signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Wed Mar 14 20:41:56 2007 +0000 (2007-03-14)
parents 192bee4b09a6
children 0c354aa0bb4a
files tools/python/xen/xend/XendAPI.py tools/python/xen/xend/XendDomainInfo.py tools/python/xen/xend/XendVMMetrics.py
line diff
     1.1 --- a/tools/python/xen/xend/XendAPI.py	Wed Mar 14 20:27:57 2007 +0000
     1.2 +++ b/tools/python/xen/xend/XendAPI.py	Wed Mar 14 20:41:56 2007 +0000
     1.3 @@ -31,6 +31,7 @@ from xen.xend.XendError import *
     1.4  from xen.xend.XendClient import ERROR_INVALID_DOMAIN
     1.5  from xen.xend.XendLogging import log
     1.6  from xen.xend.XendTask import XendTask
     1.7 +from xen.xend.XendVMMetrics import XendVMMetrics
     1.8  
     1.9  from xen.xend.XendAPIConstants import *
    1.10  from xen.util.xmlrpclib2 import stringify
    1.11 @@ -192,6 +193,16 @@ def valid_vm(func):
    1.12             _check_ref(XendDomain.instance().is_valid_vm,
    1.13                        'VM', func, *args, **kwargs)
    1.14  
    1.15 +def valid_vm_metrics(func):
    1.16 +    """Decorator to verify if vm_metrics_ref is valid before calling method.
    1.17 +
    1.18 +    @param func: function with params: (self, session, vm_metrics_ref, ...)
    1.19 +    @rtype: callable object
    1.20 +    """    
    1.21 +    return lambda *args, **kwargs: \
    1.22 +           _check_ref(XendVMMetrics.is_valid_vm_metrics,
    1.23 +                      'VM_metrics', func, *args, **kwargs)
    1.24 +
    1.25  def valid_network(func):
    1.26      """Decorator to verify if network_ref is valid before calling method.
    1.27  
    1.28 @@ -400,6 +411,7 @@ class XendAPI(object):
    1.29              'host_metrics' : valid_host_metrics,
    1.30              'network'      : valid_network,
    1.31              'VM'           : valid_vm,
    1.32 +            'VM_metrics'   : valid_vm_metrics,
    1.33              'VBD'          : valid_vbd,
    1.34              'VBD_metrics'  : valid_vbd_metrics,
    1.35              'VIF'          : valid_vif,
    1.36 @@ -990,11 +1002,9 @@ class XendAPI(object):
    1.37  
    1.38      VM_attr_ro = ['power_state',
    1.39                    'resident_on',
    1.40 -                  'memory_actual',
    1.41                    'memory_static_max',                  
    1.42                    'memory_static_min',
    1.43                    'VCPUs_number',
    1.44 -                  'VCPUs_utilisation',
    1.45                    'consoles',
    1.46                    'VIFs',
    1.47                    'VBDs',
    1.48 @@ -1002,6 +1012,7 @@ class XendAPI(object):
    1.49                    'tools_version',
    1.50                    'domid',
    1.51                    'is_control_domain',
    1.52 +                  'metrics'
    1.53                    ]
    1.54                    
    1.55      VM_attr_rw = ['name_label',
    1.56 @@ -1098,11 +1109,7 @@ class XendAPI(object):
    1.57      
    1.58      def VM_get_resident_on(self, session, vm_ref):
    1.59          return xen_api_success(XendNode.instance().uuid)
    1.60 -    
    1.61 -    def VM_get_memory_actual(self, session, vm_ref):
    1.62 -        dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
    1.63 -        return xen_api_todo() # unsupported by xc
    1.64 -    
    1.65 +       
    1.66      def VM_get_memory_static_max(self, session, vm_ref):
    1.67          dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
    1.68          return xen_api_success(dom.get_memory_static_max())
    1.69 @@ -1115,10 +1122,6 @@ class XendAPI(object):
    1.70          dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
    1.71          return xen_api_success(dom.getVCpuCount())
    1.72      
    1.73 -    def VM_get_VCPUs_utilisation(self, session, vm_ref):
    1.74 -        dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
    1.75 -        return xen_api_success(dom.get_vcpus_util())
    1.76 -    
    1.77      def VM_get_VIFs(self, session, vm_ref):
    1.78          dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
    1.79          return xen_api_success(dom.get_vifs())
    1.80 @@ -1139,6 +1142,10 @@ class XendAPI(object):
    1.81          dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
    1.82          return dom.get_tools_version()
    1.83  
    1.84 +    def VM_get_metrics(self, _, vm_ref):
    1.85 +        dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
    1.86 +        return xen_api_success(dom.get_metrics())
    1.87 +
    1.88      # attributes (rw)
    1.89      def VM_get_name_label(self, session, vm_ref):
    1.90          dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
    1.91 @@ -1418,11 +1425,9 @@ class XendAPI(object):
    1.92              'memory_static_max': xeninfo.get_memory_static_max(),
    1.93              'memory_dynamic_min': xeninfo.get_memory_dynamic_min(),
    1.94              'memory_dynamic_max': xeninfo.get_memory_dynamic_max(),
    1.95 -            'memory_actual': xeninfo.get_memory_static_min(),
    1.96              'VCPUs_policy': xeninfo.get_vcpus_policy(),
    1.97              'VCPUs_params': xeninfo.get_vcpus_params(),
    1.98              'VCPUs_number': xeninfo.getVCpuCount(),
    1.99 -            'VCPUs_utilisation': xeninfo.get_vcpus_util(),
   1.100              'actions_after_shutdown': xeninfo.get_on_shutdown(),
   1.101              'actions_after_reboot': xeninfo.get_on_reboot(),
   1.102              'actions_after_suspend': xeninfo.get_on_suspend(),
   1.103 @@ -1497,6 +1502,30 @@ class XendAPI(object):
   1.104          return XendTask.log_progress(0, 100, do_vm_func,
   1.105                                       "domain_unpause", vm_ref)
   1.106  
   1.107 +    # Xen API: Class VM_metrics
   1.108 +    # ----------------------------------------------------------------
   1.109 +
   1.110 +    VM_metrics_attr_ro = ['memory_actual',
   1.111 +                           'vcpus_number',
   1.112 +                           'vcpus_utilisation']
   1.113 +    VM_metrics_attr_rw = []
   1.114 +    VM_metrics_methods = []
   1.115 +
   1.116 +    def _VM_metrics_get(self, ref):
   1.117 +        return XendVMMetrics.get_by_uuid(ref)
   1.118 +
   1.119 +    def VM_metrics_get_record(self, _, ref):
   1.120 +        return xen_api_success(self._VM_metrics_get(ref).get_record())
   1.121 +
   1.122 +    def VM_metrics_get_memory_actual(self, _, ref):
   1.123 +        return xen_api_success(self._VM_metrics_get(ref).get_memory_actual())
   1.124 +
   1.125 +    def VM_metrics_get_vcpus_number(self, _, ref):
   1.126 +        return xen_api_success(self._VM_metrics_get(ref).get_vcpus_number())
   1.127 +
   1.128 +    def VM_metrics_get_vcpus_utilisation(self, _, ref):
   1.129 +        return xen_api_success(self._VM_metrics_get(ref).get_metrics_get_vcpus_utilisation())
   1.130 +
   1.131      # Xen API: Class VBD
   1.132      # ----------------------------------------------------------------
   1.133  
     2.1 --- a/tools/python/xen/xend/XendDomainInfo.py	Wed Mar 14 20:27:57 2007 +0000
     2.2 +++ b/tools/python/xen/xend/XendDomainInfo.py	Wed Mar 14 20:41:56 2007 +0000
     2.3 @@ -51,6 +51,8 @@ from xen.xend.xenstore.xswatch import xs
     2.4  from xen.xend.XendConstants import *
     2.5  from xen.xend.XendAPIConstants import *
     2.6  
     2.7 +from xen.xend.XendVMMetrics import XendVMMetrics
     2.8 +
     2.9  MIGRATE_TIMEOUT = 30.0
    2.10  BOOTLOADER_LOOPBACK_DEVICE = '/dev/xvdp'
    2.11  
    2.12 @@ -369,6 +371,8 @@ class XendDomainInfo:
    2.13              self._augmentInfo(priv)
    2.14  
    2.15          self._checkName(self.info['name_label'])
    2.16 +
    2.17 +        self.metrics = XendVMMetrics(uuid.createString(), self)
    2.18              
    2.19  
    2.20      #
    2.21 @@ -627,6 +631,10 @@ class XendDomainInfo:
    2.22          except RuntimeError, exn:
    2.23              raise XendError(str(exn))
    2.24  
    2.25 +
    2.26 +    def getDomInfo(self):
    2.27 +        return dom_get(self.domid)
    2.28 +
    2.29      #
    2.30      # internal functions ... TODO: re-categorised
    2.31      # 
    2.32 @@ -2060,6 +2068,8 @@ class XendDomainInfo:
    2.33          return self.info.get('pci_bus', '')
    2.34      def get_tools_version(self):
    2.35          return self.info.get('tools_version', {})
    2.36 +    def get_metrics(self):
    2.37 +        return self.metrics.get_uuid();
    2.38      
    2.39      def get_on_shutdown(self):
    2.40          after_shutdown = self.info.get('actions_after_shutdown')
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tools/python/xen/xend/XendVMMetrics.py	Wed Mar 14 20:41:56 2007 +0000
     3.3 @@ -0,0 +1,65 @@
     3.4 +#============================================================================
     3.5 +# This library is free software; you can redistribute it and/or
     3.6 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
     3.7 +# License as published by the Free Software Foundation.
     3.8 +#
     3.9 +# This library is distributed in the hope that it will be useful,
    3.10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    3.12 +# Lesser General Public License for more details.
    3.13 +#
    3.14 +# You should have received a copy of the GNU Lesser General Public
    3.15 +# License along with this library; if not, write to the Free Software
    3.16 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    3.17 +#============================================================================
    3.18 +# Copyright (c) 2006-2007 Xensource Inc.
    3.19 +#============================================================================
    3.20 +
    3.21 +from xen.xend.XendLogging import log
    3.22 +
    3.23 +instances = {}
    3.24 +
    3.25 +class XendVMMetrics:
    3.26 +    """VM Metrics."""
    3.27 +
    3.28 +    def get_by_uuid(_, uuid):
    3.29 +        return instances[uuid]
    3.30 +
    3.31 +    get_by_uuid = classmethod(get_by_uuid)
    3.32 +
    3.33 +    def is_valid_vm_metrics(_, uuid):
    3.34 +        return uuid in instances
    3.35 +
    3.36 +    is_valid_vm_metrics = classmethod(is_valid_vm_metrics)
    3.37 +   
    3.38 +    def __init__(self, uuid, xend_domain_instance):
    3.39 +        self.uuid = uuid
    3.40 +        self.xend_domain_instance = xend_domain_instance
    3.41 +        instances[uuid] = self
    3.42 +
    3.43 +    def get_uuid(self):
    3.44 +        return self.uuid
    3.45 +
    3.46 +    def get_memory_actual(self):
    3.47 +        return self.get_record()["memory_actual"]
    3.48 +
    3.49 +    def get_vcpus_number(self):
    3.50 +        return self.get_record()["vcpus_number"]
    3.51 +    
    3.52 +    def get_vcpus_utilisation(self):
    3.53 +        return self.xend_domain_instance.get_vcpus_util()
    3.54 +
    3.55 +    def get_record(self):
    3.56 +        domInfo = self.xend_domain_instance.getDomInfo()
    3.57 +        if domInfo:
    3.58 +            return { 'uuid'              : self.uuid,
    3.59 +                     'memory_actual'     : domInfo["mem_kb"] * 1024,
    3.60 +                     'vcpus_number'      : domInfo["online_vcpus"],
    3.61 +                     'vcpus_utilisation' : self.get_vcpus_utilisation()
    3.62 +                   }
    3.63 +        else:
    3.64 +            return { 'uuid'              : self.uuid,
    3.65 +                     'memory_actual'     : 0,
    3.66 +                     'vcpus_number'      : 0,
    3.67 +                     'vcpus_utilisation' : {}
    3.68 +                   }