debuggers.hg

view tools/python/xen/xend/osdep.py @ 19975:e845326ae203

python: Remove tab indents.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Jul 14 14:43:19 2009 +0100 (2009-07-14)
parents 23f9857f642f
children 4fc621f62ed1
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 _xend_autorestart = {
24 "NetBSD": True,
25 "Linux": True,
26 "SunOS": False,
27 }
29 _vif_script = {
30 "SunOS": "vif-vnic"
31 }
33 PROC_XEN_BALLOON = '/proc/xen/balloon'
34 SYSFS_XEN_MEMORY = '/sys/devices/system/xen_memory/xen_memory0'
36 def _linux_balloon_stat_proc(label):
37 """Returns the value for the named label, or None if an error occurs."""
39 xend2linux_labels = { 'current' : 'Current allocation',
40 'target' : 'Requested target',
41 'low-balloon' : 'Low-mem balloon',
42 'high-balloon' : 'High-mem balloon',
43 'limit' : 'Xen hard limit' }
45 f = file(PROC_XEN_BALLOON, 'r')
46 try:
47 for line in f:
48 keyvalue = line.split(':')
49 if keyvalue[0] == xend2linux_labels[label]:
50 values = keyvalue[1].split()
51 if values[0].isdigit():
52 return int(values[0])
53 else:
54 return None
55 return None
56 finally:
57 f.close()
59 def _linux_balloon_stat_sysfs(label):
60 sysfiles = { 'target' : 'target_kb',
61 'current' : 'info/current_kb',
62 'low-balloon' : 'info/low_kb',
63 'high-balloon' : 'info/high_kb',
64 'limit' : 'info/hard_limit_kb' }
66 name = os.path.join(SYSFS_XEN_MEMORY, sysfiles[label])
67 f = file(name, 'r')
69 val = f.read().strip()
70 if val.isdigit():
71 return int(val)
72 return None
74 def _linux_balloon_stat(label):
75 if os.access(PROC_XEN_BALLOON, os.F_OK):
76 return _linux_balloon_stat_proc(label)
77 elif os.access(SYSFS_XEN_MEMORY, os.F_OK):
78 return _linux_balloon_stat_sysfs(label)
79 return None
81 def _solaris_balloon_stat(label):
82 """Returns the value for the named label, or None if an error occurs."""
84 import fcntl
85 import array
86 DEV_XEN_BALLOON = '/dev/xen/balloon'
87 BLN_IOCTL_CURRENT = 0x42410001
88 BLN_IOCTL_TARGET = 0x42410002
89 BLN_IOCTL_LOW = 0x42410003
90 BLN_IOCTL_HIGH = 0x42410004
91 BLN_IOCTL_LIMIT = 0x42410005
92 label_to_ioctl = { 'current' : BLN_IOCTL_CURRENT,
93 'target' : BLN_IOCTL_TARGET,
94 'low-balloon' : BLN_IOCTL_LOW,
95 'high-balloon' : BLN_IOCTL_HIGH,
96 'limit' : BLN_IOCTL_LIMIT }
98 f = file(DEV_XEN_BALLOON, 'r')
99 try:
100 values = array.array('L', [0])
101 if fcntl.ioctl(f.fileno(), label_to_ioctl[label], values, 1) == 0:
102 return values[0]
103 else:
104 return None
105 finally:
106 f.close()
108 _balloon_stat = {
109 "SunOS": _solaris_balloon_stat
110 }
112 def _linux_get_cpuinfo():
113 cpuinfo = {}
114 f = file('/proc/cpuinfo', 'r')
115 try:
116 p = -1
117 d = {}
118 for line in f:
119 keyvalue = line.split(':')
120 if len(keyvalue) != 2:
121 continue
122 key = keyvalue[0].strip()
123 val = keyvalue[1].strip()
124 if key == 'processor':
125 if p != -1:
126 cpuinfo[p] = d
127 p = int(val)
128 d = {}
129 else:
130 d[key] = val
131 cpuinfo[p] = d
132 return cpuinfo
133 finally:
134 f.close()
136 def _solaris_get_cpuinfo():
137 cpuinfo = {}
139 # call kstat to extrace specific cpu_info output
140 cmd = "/usr/bin/kstat -p -c misc -m cpu_info"
141 kstatoutput = commands.getoutput (cmd)
143 # walk each line
144 for kstatline in kstatoutput.split('\n'):
146 # split the line on
147 # module:cpu #:module#:name value
148 (module, cpunum, combo, namevalue) = kstatline.split (":")
150 # check to see if this cpunum is already a key. If not,
151 # initialize an empty hash table
152 if not cpuinfo.has_key (int(cpunum)):
153 cpuinfo[int(cpunum)] = {}
155 # split the namevalue output on whitespace
156 data = namevalue.split()
158 # the key will be data[0]
159 key = data[0]
161 # check the length of the data list. If it's larger than
162 # 2, join the rest of the list together with a space.
163 # Otherwise, value is just data[1]
164 if len (data) > 2:
165 value = ' '.join (data[1:])
166 else:
167 value = data[1]
169 # add this key/value pair to the cpuhash
170 cpuinfo[int(cpunum)][key] = value
172 # Translate Solaris tokens into what Xend expects
173 for key in cpuinfo.keys():
174 cpuinfo[key]["flags"] = ""
175 cpuinfo[key]["model name"] = cpuinfo[key]["brand"]
176 cpuinfo[key]["cpu MHz"] = cpuinfo[key]["clock_MHz"]
178 # return the hash table
179 return cpuinfo
181 _get_cpuinfo = {
182 "SunOS": _solaris_get_cpuinfo
183 }
185 def _default_prefork(name):
186 pass
188 def _default_postfork(ct, abandon=False):
189 pass
191 # call this for long-running processes that should survive a xend
192 # restart
193 def _solaris_prefork(name):
194 from xen.lowlevel import process
195 return process.activate(name)
197 def _solaris_postfork(ct, abandon=False):
198 from xen.lowlevel import process
199 process.clear(ct)
200 if abandon:
201 process.abandon_latest()
203 _get_prefork = {
204 "SunOS": _solaris_prefork
205 }
207 _get_postfork = {
208 "SunOS": _solaris_postfork
209 }
211 def _get(var, default=None):
212 return var.get(os.uname()[0], default)
214 xend_autorestart = _get(_xend_autorestart)
215 vif_script = _get(_vif_script, "vif-bridge")
216 lookup_balloon_stat = _get(_balloon_stat, _linux_balloon_stat)
217 get_cpuinfo = _get(_get_cpuinfo, _linux_get_cpuinfo)
218 prefork = _get(_get_prefork, _default_prefork)
219 postfork = _get(_get_postfork, _default_postfork)