ia64/xen-unstable

view tools/xm-test/lib/XmTestLib/Xm.py @ 7757:139aabe357a4

Various chmod a+x.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Thu Nov 10 12:00:09 2005 +0100 (2005-11-10)
parents ecb5997f7b25
children
line source
1 #!/usr/bin/python
3 """
4 Copyright (C) International Business Machines Corp., 2005
5 Author: Dan Smith <danms@us.ibm.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; under version 2 of the License.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 """
24 ##
25 ## These are miscellaneous utility functions that query xm
26 ##
28 import commands
29 import re
30 import os
31 import time
33 from Test import *;
35 class XmError(Exception):
36 def __init__(self, msg, trace="", status=0):
37 self.msg = msg
38 self.trace = trace
39 try:
40 self.status = int(status)
41 except Exception, e:
42 self.status = -1
44 def __str__(self):
45 return trace
47 def domid(name):
48 status, output = traceCommand("xm domid " + name);
50 if re.search("Traceback", output):
51 print "*** xm domid failed with:"
52 print output
53 for i in range(0,10):
54 status, output = traceCommand("xm list")
55 status, output = traceCommand("xm domid " + name);
56 if not re.search("Traceback", output):
57 break
59 try:
60 id = int(output);
61 except:
62 id = -1;
63 return id;
65 def domname(id):
66 status, output = traceCommand("xm domname " + str(id));
67 return output;
69 def isDomainRunning(domain):
70 id = domid(domain);
71 if id == -1:
72 return False;
73 else:
74 return True;
76 def getRunningDomains():
77 status, output = traceCommand("xm list");
78 if "Traceback" in output:
79 raise XmError("xm failed", trace=output, status=status)
81 lines = output.splitlines();
82 domains = [];
83 for l in lines[1:]:
84 elms = l.split(" ", 1);
85 domains.append(elms[0]);
86 return domains;
88 def destroyDomU(name):
89 status, output = traceCommand("xm destroy " + name, logOutput=False);
90 return status;
92 def destroyAllDomUs():
93 if verbose:
94 print "*** Cleaning all running domU's"
96 attempt = 0
97 trying = True
99 while trying:
100 try:
101 attempt += 1
102 domainList = getRunningDomains()
103 trying = False
104 except XmError, e:
105 if attempt >= 10:
106 FAIL("XM-TEST: xm list not responding")
107 time.sleep(1)
108 print e.trace
109 print "!!! Trying again to get a clean domain list..."
111 for d in domainList:
112 if not d == "Domain-0":
113 destroyDomU(d);
115 if verbose:
116 print "*** Finished cleaning domUs"
118 def getDomMem(domain):
119 status, output = traceCommand("xm list")
120 if status != 0:
121 if verbose:
122 print "xm list failed with %i" % status
123 return None
125 lines = re.split("\n", output)
126 for line in lines:
127 fields = re.sub(" +", " ", line).split()
128 if domain.isdigit():
129 if fields[1] == domain:
130 return int(fields[2])
131 else:
132 if fields[0] == domain:
133 return int(fields[2])
134 if verbose:
135 print "Did not find domain " + str(domain)
136 return None
138 def getDomInfo(domain, key, opts=None):
139 if opts:
140 cmd = "xm list %s" % opts
141 else:
142 cmd = "xm list"
144 status, output = traceCommand(cmd)
146 if status != 0:
147 if verbose:
148 print "xm list failed with %i" % status
149 return None
151 lines = output.split("\n")
153 # Get the key values from the first line headers
154 cleanHeader = re.sub("\([^\)]+\)", "", lines[0])
155 colHeaders = re.split(" +", cleanHeader)
157 doms = {}
159 for line in lines[1:]:
160 domValues = {}
161 values = re.split(" +", line)
162 i = 1
163 for value in values[1:]:
164 domValues[colHeaders[i]] = value
165 i += 1
166 doms[values[0]] = domValues
169 if doms.has_key(domain) and doms[domain].has_key(key):
170 return doms[domain].get(key)
172 return ""
174 def getVcpuInfo(domain):
176 status, output = traceCommand("xm vcpu-list %s" % domain)
178 lines = output.split("\n")
180 vcpus = {}
182 for line in lines[1:]:
183 cols = re.split(" +", line)
184 if cols[3] == '-':
185 vcpus[int(cols[2])] = None
186 else:
187 vcpus[int(cols[2])] = int(cols[3])
189 return vcpus
191 def getInfo(key):
193 info = {}
195 status, output = traceCommand("xm info")
196 lines = output.split("\n")
197 for line in lines:
198 match = re.match("^([A-z_]+)[^:]*: (.*)$", line)
199 if match:
200 info[match.group(1)] = match.group(2)
202 if info.has_key(key):
203 return info[key]
204 else:
205 return ""
207 def restartXend():
208 if verbose:
209 print "*** Restarting xend ..."
211 if os.access("/etc/init.d/xend", os.X_OK):
212 status, output = traceCommand("/etc/init.d/xend stop")
213 time.sleep(1)
214 status, output = traceCommand("/etc/init.d/xend start")
216 return status
218 else:
219 status, output = traceCommand("xend stop")
220 time.sleep(1)
221 status, output = traceCommand("xend start")
223 return status
225 def smpConcurrencyLevel():
226 cores = int(getInfo("cores_per_socket"))
227 threads = int(getInfo("threads_per_core"))
228 sockets = int(getInfo("sockets_per_node"))
230 return cores * sockets * threads
232 if __name__ == "__main__":
233 if isDomainRunning("0"):
234 print "Domain-0 is running; I must be working!"
235 else:
236 print "Domain-0 is not running; I may be broken!"
238 mem = getDomMem("Domain-0")
239 if not mem:
240 print "Failed to get memory for Domain-0!"
241 else:
242 print "Domain-0 mem: %i" % mem
244 cpu = getDomInfo("Domain-0", "CPU")
245 state = getDomInfo("Domain-0", "State")
247 print "Domain-0 CPU: " + cpu
248 print "Domain-0 state: " + state
250 v = getVcpuInfo("Domain-0")
251 for key in v.keys():
252 print "VCPU%i is on CPU %i" % (key, v[key])