~ressu/+junk/xen-ubuntu

« back to all changes in this revision

Viewing changes to tools/python/xen/xend/XendVMMetrics.py

  • Committer: sami at haahtinen
  • Author(s): Bastian Blank
  • Date: 2010-09-03 15:14:28 UTC
  • Revision ID: sami@haahtinen.name-20100903151428-f88eg54n2fdnak41
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#============================================================================
 
2
# This library is free software; you can redistribute it and/or
 
3
# modify it under the terms of version 2.1 of the GNU Lesser General Public
 
4
# License as published by the Free Software Foundation.
 
5
#
 
6
# This library is distributed in the hope that it will be useful,
 
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
9
# Lesser General Public License for more details.
 
10
#
 
11
# You should have received a copy of the GNU Lesser General Public
 
12
# License along with this library; if not, write to the Free Software
 
13
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
14
#============================================================================
 
15
# Copyright (c) 2006-2007 Xensource Inc.
 
16
# Copyright (c) 2007 Tom Wilkie
 
17
#============================================================================
 
18
 
 
19
from xen.xend.XendLogging import log
 
20
from xen.xend.XendBase import XendBase
 
21
import xen.lowlevel.xc
 
22
 
 
23
xc = xen.lowlevel.xc.xc()
 
24
 
 
25
class XendVMMetrics(XendBase):
 
26
    """VM Metrics."""
 
27
 
 
28
    def getClass(self):
 
29
        return "VM_metrics"
 
30
    
 
31
    def getAttrRO(self):
 
32
        attrRO = ['memory_actual',
 
33
                  'VCPUs_number',
 
34
                  'VCPUs_utilisation',
 
35
                  'VCPUs_CPU',
 
36
                  'VCPUs_flags',
 
37
                  'VCPUs_params',
 
38
                  'state',
 
39
                  'start_time',
 
40
                  'last_updated']
 
41
        return XendBase.getAttrRO() + attrRO
 
42
 
 
43
    getClass    = classmethod(getClass)
 
44
    getAttrRO   = classmethod(getAttrRO)
 
45
 
 
46
    def __init__(self, uuid, xend_domain_instance):
 
47
        XendBase.__init__(self, uuid, {})
 
48
        self.xend_domain_instance = xend_domain_instance
 
49
        
 
50
    def get_memory_actual(self):
 
51
        domInfo = self.xend_domain_instance.getDomInfo()
 
52
        if domInfo:
 
53
            return domInfo["mem_kb"] * 1024
 
54
        else:
 
55
            return 0
 
56
 
 
57
    def get_VCPUs_number(self):
 
58
        domInfo = self.xend_domain_instance.getDomInfo()
 
59
        if domInfo:
 
60
            return domInfo["online_vcpus"]
 
61
        else:
 
62
            return 0
 
63
    
 
64
    def get_VCPUs_utilisation(self):
 
65
        return self.xend_domain_instance.get_vcpus_util()
 
66
 
 
67
    def get_VCPUs_CPU(self):
 
68
        domid = self.xend_domain_instance.getDomid()
 
69
        if domid is not None:
 
70
            vcpus_cpu = {}
 
71
            vcpus_max = self.xend_domain_instance.info['VCPUs_max']
 
72
            for i in range(0, vcpus_max):
 
73
                info = xc.vcpu_getinfo(domid, i)
 
74
                vcpus_cpu[i] = info['cpu']
 
75
            return vcpus_cpu
 
76
        else:
 
77
            return {}
 
78
 
 
79
    def get_VCPUs_flags(self):
 
80
        domid = self.xend_domain_instance.getDomid()
 
81
        if domid is not None:
 
82
            vcpus_flags = {}
 
83
            vcpus_max = self.xend_domain_instance.info['VCPUs_max']
 
84
            for i in range(0, vcpus_max):
 
85
                info = xc.vcpu_getinfo(domid, i)
 
86
                flags = []
 
87
                def set_flag(flag):
 
88
                    if info[flag] == 1:
 
89
                        flags.append(flag)
 
90
                set_flag('blocked')
 
91
                set_flag('online')
 
92
                set_flag('running')
 
93
                vcpus_flags[i] = flags
 
94
            return vcpus_flags
 
95
        else:
 
96
            return {}
 
97
 
 
98
    def get_state(self):
 
99
        try:
 
100
            domid = self.xend_domain_instance.getDomid()
 
101
            domlist = xc.domain_getinfo(domid, 1)
 
102
            if domlist and domid == domlist[0]['domid']:
 
103
                dominfo = domlist[0]
 
104
 
 
105
                states = []
 
106
                def addState(key):
 
107
                    if dominfo[key] == 1:
 
108
                        states.append(key)
 
109
 
 
110
                addState("running")
 
111
                addState("blocked")
 
112
                addState("paused")
 
113
                addState("dying")
 
114
                addState("crashed")
 
115
                addState("shutdown")
 
116
                return states
 
117
        except Exception, err:
 
118
            # ignore missing domain
 
119
            log.trace("domain_getinfo(%d) failed, ignoring: %s", domid, str(err))
 
120
        return []
 
121
 
 
122
    def get_VCPUs_params(self):
 
123
        domid = self.xend_domain_instance.getDomid()
 
124
        if domid is not None:
 
125
            params_live = {}
 
126
            vcpus_max = self.xend_domain_instance.info['VCPUs_max']
 
127
            for i in range(0, vcpus_max):
 
128
                info = xc.vcpu_getinfo(domid, i)
 
129
                params_live['cpumap%i' % i] = \
 
130
                    ",".join(map(str, info['cpumap']))
 
131
 
 
132
            params_live.update(xc.sched_credit_domain_get(domid))
 
133
            
 
134
            return params_live
 
135
        else:
 
136
            return {}
 
137
 
 
138
    def get_start_time(self):
 
139
        import xen.xend.XendAPI as XendAPI
 
140
        return XendAPI.datetime(
 
141
            self.xend_domain_instance.info.get("start_time", 0))
 
142
    
 
143
    def get_last_updated(self):
 
144
        import xen.xend.XendAPI as XendAPI
 
145
        return XendAPI.now()