ia64/xen-unstable

view tools/python/xen/xend/osdep.py @ 18801:17911073a90a

tools: use sysfs interface to balloon driver if present

The pvops dom0 kernel does not expose the balloon driver via
/proc/xen, so use the sysfs interface.

Signed-off-by: Jeremy Fitzhardinge <jeremy.fitzhardinge@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Nov 17 15:54:31 2008 +0000 (2008-11-17)
parents 0bf73f557f41
children e261fa202e39
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
22 _scripts_dir = {
23 "Linux": "/etc/xen/scripts",
24 "SunOS": "/usr/lib/xen/scripts",
25 }
27 _xend_autorestart = {
28 "NetBSD": True,
29 "Linux": True,
30 "SunOS": False,
31 }
33 _pygrub_path = {
34 "SunOS": "/usr/lib/xen/bin/pygrub"
35 }
37 _vif_script = {
38 "SunOS": "vif-vnic"
39 }
41 PROC_XEN_BALLOON = '/proc/xen/balloon'
42 SYSFS_XEN_MEMORY = '/sys/devices/system/xen_memory/xen_memory0'
44 def _linux_balloon_stat_proc(label):
45 """Returns the value for the named label, or None if an error occurs."""
47 xend2linux_labels = { 'current' : 'Current allocation',
48 'target' : 'Requested target',
49 'low-balloon' : 'Low-mem balloon',
50 'high-balloon' : 'High-mem balloon',
51 'limit' : 'Xen hard limit' }
53 f = file(PROC_XEN_BALLOON, 'r')
54 try:
55 for line in f:
56 keyvalue = line.split(':')
57 if keyvalue[0] == xend2linux_labels[label]:
58 values = keyvalue[1].split()
59 if values[0].isdigit():
60 return int(values[0])
61 else:
62 return None
63 return None
64 finally:
65 f.close()
67 def _linux_balloon_stat_sysfs(label):
68 sysfiles = { 'target' : 'target_kb',
69 'current' : 'info/current_kb',
70 'low-balloon' : 'info/low_kb',
71 'high-balloon' : 'info/high_kb',
72 'limit' : 'info/hard_limit_kb' }
74 name = os.path.join(SYSFS_XEN_MEMORY, sysfiles[label])
75 f = file(name, 'r')
77 val = f.read().strip()
78 if val.isdigit():
79 return int(val)
80 return None
82 def _linux_balloon_stat(label):
83 if os.access(PROC_XEN_BALLOON, os.F_OK):
84 return _linux_balloon_stat_proc(label)
85 elif os.access(SYSFS_XEN_MEMORY, os.F_OK):
86 return _linux_balloon_stat_sysfs(label)
88 return None
90 def _solaris_balloon_stat(label):
91 """Returns the value for the named label, or None if an error occurs."""
93 import fcntl
94 import array
95 DEV_XEN_BALLOON = '/dev/xen/balloon'
96 BLN_IOCTL_CURRENT = 0x42410001
97 BLN_IOCTL_TARGET = 0x42410002
98 BLN_IOCTL_LOW = 0x42410003
99 BLN_IOCTL_HIGH = 0x42410004
100 BLN_IOCTL_LIMIT = 0x42410005
101 label_to_ioctl = { 'current' : BLN_IOCTL_CURRENT,
102 'target' : BLN_IOCTL_TARGET,
103 'low-balloon' : BLN_IOCTL_LOW,
104 'high-balloon' : BLN_IOCTL_HIGH,
105 'limit' : BLN_IOCTL_LIMIT }
107 f = file(DEV_XEN_BALLOON, 'r')
108 try:
109 values = array.array('L', [0])
110 if fcntl.ioctl(f.fileno(), label_to_ioctl[label], values, 1) == 0:
111 return values[0]
112 else:
113 return None
114 finally:
115 f.close()
117 _balloon_stat = {
118 "SunOS": _solaris_balloon_stat
119 }
121 def _linux_get_cpuinfo():
122 cpuinfo = {}
123 f = file('/proc/cpuinfo', 'r')
124 try:
125 p = -1
126 d = {}
127 for line in f:
128 keyvalue = line.split(':')
129 if len(keyvalue) != 2:
130 continue
131 key = keyvalue[0].strip()
132 val = keyvalue[1].strip()
133 if key == 'processor':
134 if p != -1:
135 cpuinfo[p] = d
136 p = int(val)
137 d = {}
138 else:
139 d[key] = val
140 cpuinfo[p] = d
141 return cpuinfo
142 finally:
143 f.close()
145 _get_cpuinfo = {
146 }
148 def _get(var, default=None):
149 return var.get(os.uname()[0], default)
151 scripts_dir = _get(_scripts_dir, "/etc/xen/scripts")
152 xend_autorestart = _get(_xend_autorestart)
153 pygrub_path = _get(_pygrub_path, "/usr/bin/pygrub")
154 vif_script = _get(_vif_script, "vif-bridge")
155 lookup_balloon_stat = _get(_balloon_stat, _linux_balloon_stat)
156 get_cpuinfo = _get(_get_cpuinfo, _linux_get_cpuinfo)