ia64/xen-unstable

view tools/python/xen/xend/osdep.py @ 19326:e261fa202e39

xend: Add support for cpuinfo data on Solaris

Signed-off-by: Russell Blaine <russell.blaine@sun.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Mar 12 10:57:44 2009 +0000 (2009-03-12)
parents 17911073a90a
children e2de0e760a0d
line source
1 #!/usr/bin/env python
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of version 2.1 of the GNU Lesser General Public
5 # License as published by the Free Software Foundation.
6 #
7 # This library is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # Lesser General Public License for more details.
11 #
12 # You should have received a copy of the GNU Lesser General Public
13 # License along with this library; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 #
17 # Copyright 2006 Sun Microsystems, Inc. All rights reserved.
18 # Use is subject to license terms.
20 import os
21 import commands
23 _scripts_dir = {
24 "Linux": "/etc/xen/scripts",
25 "SunOS": "/usr/lib/xen/scripts",
26 }
28 _xend_autorestart = {
29 "NetBSD": True,
30 "Linux": True,
31 "SunOS": False,
32 }
34 _pygrub_path = {
35 "SunOS": "/usr/lib/xen/bin/pygrub"
36 }
38 _vif_script = {
39 "SunOS": "vif-vnic"
40 }
42 PROC_XEN_BALLOON = '/proc/xen/balloon'
43 SYSFS_XEN_MEMORY = '/sys/devices/system/xen_memory/xen_memory0'
45 def _linux_balloon_stat_proc(label):
46 """Returns the value for the named label, or None if an error occurs."""
48 xend2linux_labels = { 'current' : 'Current allocation',
49 'target' : 'Requested target',
50 'low-balloon' : 'Low-mem balloon',
51 'high-balloon' : 'High-mem balloon',
52 'limit' : 'Xen hard limit' }
54 f = file(PROC_XEN_BALLOON, 'r')
55 try:
56 for line in f:
57 keyvalue = line.split(':')
58 if keyvalue[0] == xend2linux_labels[label]:
59 values = keyvalue[1].split()
60 if values[0].isdigit():
61 return int(values[0])
62 else:
63 return None
64 return None
65 finally:
66 f.close()
68 def _linux_balloon_stat_sysfs(label):
69 sysfiles = { 'target' : 'target_kb',
70 'current' : 'info/current_kb',
71 'low-balloon' : 'info/low_kb',
72 'high-balloon' : 'info/high_kb',
73 'limit' : 'info/hard_limit_kb' }
75 name = os.path.join(SYSFS_XEN_MEMORY, sysfiles[label])
76 f = file(name, 'r')
78 val = f.read().strip()
79 if val.isdigit():
80 return int(val)
81 return None
83 def _linux_balloon_stat(label):
84 if os.access(PROC_XEN_BALLOON, os.F_OK):
85 return _linux_balloon_stat_proc(label)
86 elif os.access(SYSFS_XEN_MEMORY, os.F_OK):
87 return _linux_balloon_stat_sysfs(label)
89 return None
91 def _solaris_balloon_stat(label):
92 """Returns the value for the named label, or None if an error occurs."""
94 import fcntl
95 import array
96 DEV_XEN_BALLOON = '/dev/xen/balloon'
97 BLN_IOCTL_CURRENT = 0x42410001
98 BLN_IOCTL_TARGET = 0x42410002
99 BLN_IOCTL_LOW = 0x42410003
100 BLN_IOCTL_HIGH = 0x42410004
101 BLN_IOCTL_LIMIT = 0x42410005
102 label_to_ioctl = { 'current' : BLN_IOCTL_CURRENT,
103 'target' : BLN_IOCTL_TARGET,
104 'low-balloon' : BLN_IOCTL_LOW,
105 'high-balloon' : BLN_IOCTL_HIGH,
106 'limit' : BLN_IOCTL_LIMIT }
108 f = file(DEV_XEN_BALLOON, 'r')
109 try:
110 values = array.array('L', [0])
111 if fcntl.ioctl(f.fileno(), label_to_ioctl[label], values, 1) == 0:
112 return values[0]
113 else:
114 return None
115 finally:
116 f.close()
118 _balloon_stat = {
119 "SunOS": _solaris_balloon_stat
120 }
122 def _linux_get_cpuinfo():
123 cpuinfo = {}
124 f = file('/proc/cpuinfo', 'r')
125 try:
126 p = -1
127 d = {}
128 for line in f:
129 keyvalue = line.split(':')
130 if len(keyvalue) != 2:
131 continue
132 key = keyvalue[0].strip()
133 val = keyvalue[1].strip()
134 if key == 'processor':
135 if p != -1:
136 cpuinfo[p] = d
137 p = int(val)
138 d = {}
139 else:
140 d[key] = val
141 cpuinfo[p] = d
142 return cpuinfo
143 finally:
144 f.close()
146 def _solaris_get_cpuinfo():
147 cpuinfo = {}
149 # call kstat to extrace specific cpu_info output
150 cmd = "/usr/bin/kstat -p -c misc -m cpu_info"
151 kstatoutput = commands.getoutput (cmd)
153 # walk each line
154 for kstatline in kstatoutput.split('\n'):
156 # split the line on
157 # module:cpu #:module#:name value
158 (module, cpunum, combo, namevalue) = kstatline.split (":")
160 # check to see if this cpunum is already a key. If not,
161 # initialize an empty hash table
162 if not cpuinfo.has_key (int(cpunum)):
163 cpuinfo[int(cpunum)] = {}
165 # split the namevalue output on whitespace
166 data = namevalue.split()
168 # the key will be data[0]
169 key = data[0]
171 # check the length of the data list. If it's larger than
172 # 2, join the rest of the list together with a space.
173 # Otherwise, value is just data[1]
174 if len (data) > 2:
175 value = ' '.join (data[1:])
176 else:
177 value = data[1]
179 # add this key/value pair to the cpuhash
180 cpuinfo[int(cpunum)][key] = value
182 # Translate Solaris tokens into what Xend expects
183 for key in cpuinfo.keys():
184 cpuinfo[key]["flags"] = ""
185 cpuinfo[key]["model name"] = cpuinfo[key]["brand"]
186 cpuinfo[key]["cpu MHz"] = cpuinfo[key]["clock_MHz"]
188 # return the hash table
189 return cpuinfo
191 _get_cpuinfo = {
192 "SunOS": _solaris_get_cpuinfo
193 }
195 def _get(var, default=None):
196 return var.get(os.uname()[0], default)
198 scripts_dir = _get(_scripts_dir, "/etc/xen/scripts")
199 xend_autorestart = _get(_xend_autorestart)
200 pygrub_path = _get(_pygrub_path, "/usr/bin/pygrub")
201 vif_script = _get(_vif_script, "vif-bridge")
202 lookup_balloon_stat = _get(_balloon_stat, _linux_balloon_stat)
203 get_cpuinfo = _get(_get_cpuinfo, _linux_get_cpuinfo)