ia64/xen-unstable

annotate 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
rev   line source
kfraser@11855 1 #!/usr/bin/env python
kfraser@11855 2 #
kfraser@11855 3 # This library is free software; you can redistribute it and/or
kfraser@11855 4 # modify it under the terms of version 2.1 of the GNU Lesser General Public
kfraser@11855 5 # License as published by the Free Software Foundation.
kfraser@11855 6 #
kfraser@11855 7 # This library is distributed in the hope that it will be useful,
kfraser@11855 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
kfraser@11855 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
kfraser@11855 10 # Lesser General Public License for more details.
kfraser@11855 11 #
kfraser@11855 12 # You should have received a copy of the GNU Lesser General Public
kfraser@11855 13 # License along with this library; if not, write to the Free Software
kfraser@11855 14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
kfraser@11855 15 #
kfraser@11855 16
kfraser@11855 17 # Copyright 2006 Sun Microsystems, Inc. All rights reserved.
kfraser@11855 18 # Use is subject to license terms.
kfraser@11855 19
kfraser@11855 20 import os
kfraser@11855 21
kfraser@11855 22 _scripts_dir = {
kfraser@11855 23 "Linux": "/etc/xen/scripts",
kfraser@11855 24 "SunOS": "/usr/lib/xen/scripts",
kfraser@11855 25 }
kfraser@11855 26
kfraser@11860 27 _xend_autorestart = {
keir@16430 28 "NetBSD": True,
kfraser@11860 29 "Linux": True,
kfraser@11860 30 "SunOS": False,
kfraser@11860 31 }
kfraser@11860 32
Tim@13335 33 _pygrub_path = {
Tim@13335 34 "SunOS": "/usr/lib/xen/bin/pygrub"
Tim@13335 35 }
Tim@13335 36
john@13486 37 _vif_script = {
john@13486 38 "SunOS": "vif-vnic"
john@13486 39 }
john@13486 40
keir@18801 41 PROC_XEN_BALLOON = '/proc/xen/balloon'
keir@18801 42 SYSFS_XEN_MEMORY = '/sys/devices/system/xen_memory/xen_memory0'
keir@18801 43
keir@18801 44 def _linux_balloon_stat_proc(label):
keir@14686 45 """Returns the value for the named label, or None if an error occurs."""
keir@14686 46
keir@18129 47 xend2linux_labels = { 'current' : 'Current allocation',
keir@18129 48 'target' : 'Requested target',
keir@18129 49 'low-balloon' : 'Low-mem balloon',
keir@18129 50 'high-balloon' : 'High-mem balloon',
keir@18129 51 'limit' : 'Xen hard limit' }
keir@18129 52
keir@14686 53 f = file(PROC_XEN_BALLOON, 'r')
keir@14686 54 try:
keir@14686 55 for line in f:
keir@14686 56 keyvalue = line.split(':')
keir@18129 57 if keyvalue[0] == xend2linux_labels[label]:
keir@14686 58 values = keyvalue[1].split()
keir@14686 59 if values[0].isdigit():
keir@14686 60 return int(values[0])
keir@14686 61 else:
keir@14686 62 return None
keir@14686 63 return None
keir@14686 64 finally:
keir@14686 65 f.close()
keir@14686 66
keir@18801 67 def _linux_balloon_stat_sysfs(label):
keir@18801 68 sysfiles = { 'target' : 'target_kb',
keir@18801 69 'current' : 'info/current_kb',
keir@18801 70 'low-balloon' : 'info/low_kb',
keir@18801 71 'high-balloon' : 'info/high_kb',
keir@18801 72 'limit' : 'info/hard_limit_kb' }
keir@18801 73
keir@18801 74 name = os.path.join(SYSFS_XEN_MEMORY, sysfiles[label])
keir@18801 75 f = file(name, 'r')
keir@18801 76
keir@18801 77 val = f.read().strip()
keir@18801 78 if val.isdigit():
keir@18801 79 return int(val)
keir@18801 80 return None
keir@18801 81
keir@18801 82 def _linux_balloon_stat(label):
keir@18801 83 if os.access(PROC_XEN_BALLOON, os.F_OK):
keir@18801 84 return _linux_balloon_stat_proc(label)
keir@18801 85 elif os.access(SYSFS_XEN_MEMORY, os.F_OK):
keir@18801 86 return _linux_balloon_stat_sysfs(label)
keir@18801 87
keir@18801 88 return None
keir@18801 89
keir@14686 90 def _solaris_balloon_stat(label):
keir@14686 91 """Returns the value for the named label, or None if an error occurs."""
keir@14686 92
keir@14686 93 import fcntl
keir@14686 94 import array
keir@14686 95 DEV_XEN_BALLOON = '/dev/xen/balloon'
keir@15360 96 BLN_IOCTL_CURRENT = 0x42410001
keir@15360 97 BLN_IOCTL_TARGET = 0x42410002
keir@15360 98 BLN_IOCTL_LOW = 0x42410003
keir@15360 99 BLN_IOCTL_HIGH = 0x42410004
keir@15360 100 BLN_IOCTL_LIMIT = 0x42410005
keir@18129 101 label_to_ioctl = { 'current' : BLN_IOCTL_CURRENT,
keir@18129 102 'target' : BLN_IOCTL_TARGET,
keir@18129 103 'low-balloon' : BLN_IOCTL_LOW,
keir@18129 104 'high-balloon' : BLN_IOCTL_HIGH,
keir@18129 105 'limit' : BLN_IOCTL_LIMIT }
keir@14686 106
keir@14686 107 f = file(DEV_XEN_BALLOON, 'r')
keir@14686 108 try:
keir@14686 109 values = array.array('L', [0])
keir@14686 110 if fcntl.ioctl(f.fileno(), label_to_ioctl[label], values, 1) == 0:
keir@14686 111 return values[0]
keir@14686 112 else:
keir@14686 113 return None
keir@14686 114 finally:
keir@14686 115 f.close()
keir@14686 116
keir@14686 117 _balloon_stat = {
keir@14686 118 "SunOS": _solaris_balloon_stat
keir@14686 119 }
keir@14686 120
keir@18107 121 def _linux_get_cpuinfo():
keir@18107 122 cpuinfo = {}
keir@18107 123 f = file('/proc/cpuinfo', 'r')
keir@18107 124 try:
keir@18107 125 p = -1
keir@18107 126 d = {}
keir@18107 127 for line in f:
keir@18107 128 keyvalue = line.split(':')
keir@18107 129 if len(keyvalue) != 2:
keir@18107 130 continue
keir@18107 131 key = keyvalue[0].strip()
keir@18107 132 val = keyvalue[1].strip()
keir@18107 133 if key == 'processor':
keir@18107 134 if p != -1:
keir@18107 135 cpuinfo[p] = d
keir@18107 136 p = int(val)
keir@18107 137 d = {}
keir@18107 138 else:
keir@18107 139 d[key] = val
keir@18107 140 cpuinfo[p] = d
keir@18107 141 return cpuinfo
keir@18107 142 finally:
keir@18107 143 f.close()
keir@18107 144
keir@18107 145 _get_cpuinfo = {
keir@18107 146 }
keir@18107 147
kfraser@11860 148 def _get(var, default=None):
kfraser@11860 149 return var.get(os.uname()[0], default)
kfraser@11860 150
kfraser@11860 151 scripts_dir = _get(_scripts_dir, "/etc/xen/scripts")
kfraser@11860 152 xend_autorestart = _get(_xend_autorestart)
Tim@13335 153 pygrub_path = _get(_pygrub_path, "/usr/bin/pygrub")
john@13486 154 vif_script = _get(_vif_script, "vif-bridge")
keir@14686 155 lookup_balloon_stat = _get(_balloon_stat, _linux_balloon_stat)
keir@18107 156 get_cpuinfo = _get(_get_cpuinfo, _linux_get_cpuinfo)