|
| 1 | +# |
| 2 | +# Copyright 2010 Red Hat, Inc. |
| 3 | +# Cole Robinson <crobinso@redhat.com> |
| 4 | +# |
| 5 | +# This program is free software; you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation; either version 2 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program; if not, write to the Free Software |
| 17 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | +# MA 02110-1301 USA. |
| 19 | + |
| 20 | +import re |
| 21 | + |
| 22 | +import libxml2 |
| 23 | + |
| 24 | +import _util |
| 25 | +import XMLBuilderDomain |
| 26 | +from XMLBuilderDomain import _xml_property |
| 27 | +from virtinst import _gettext as _ |
| 28 | + |
| 29 | +class DomainNumatune(XMLBuilderDomain.XMLBuilderDomain): |
| 30 | + """ |
| 31 | + Class for generating <numatune> XML |
| 32 | + """ |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def validate_cpuset(conn, val): |
| 36 | + if val is None or val == "": |
| 37 | + return |
| 38 | + |
| 39 | + if type(val) is not type("string") or len(val) == 0: |
| 40 | + raise ValueError(_("cpuset must be string")) |
| 41 | + if re.match("^[0-9,-^]*$", val) is None: |
| 42 | + raise ValueError(_("cpuset can only contain numeric, ',', or " |
| 43 | + "'-' characters")) |
| 44 | + |
| 45 | + pcpus = _util.get_phy_cpus(conn) |
| 46 | + for c in val.split(','): |
| 47 | + # Redundant commas |
| 48 | + if not c: |
| 49 | + continue |
| 50 | + |
| 51 | + if "-" in c: |
| 52 | + (x, y) = c.split('-', 1) |
| 53 | + x = int(x) |
| 54 | + y = int(y) |
| 55 | + if x > y: |
| 56 | + raise ValueError(_("cpuset contains invalid format.")) |
| 57 | + if x >= pcpus or y >= pcpus: |
| 58 | + raise ValueError(_("cpuset's pCPU numbers must be less " |
| 59 | + "than pCPUs.")) |
| 60 | + else: |
| 61 | + if c.startswith("^"): |
| 62 | + c = c[1:] |
| 63 | + c = int(c) |
| 64 | + |
| 65 | + if c >= pcpus: |
| 66 | + raise ValueError(_("cpuset's pCPU numbers must be less " |
| 67 | + "than pCPUs.")) |
| 68 | + |
| 69 | + @staticmethod |
| 70 | + def cpuset_str_to_tuple(conn, cpuset): |
| 71 | + DomainNumatune.validate_cpuset(conn, cpuset) |
| 72 | + pinlist = [False] * _util.get_phy_cpus(conn) |
| 73 | + |
| 74 | + entries = cpuset.split(",") |
| 75 | + for e in entries: |
| 76 | + series = e.split("-", 1) |
| 77 | + |
| 78 | + if len(series) == 1: |
| 79 | + pinlist[int(series[0])] = True |
| 80 | + continue |
| 81 | + |
| 82 | + start = int(series[0]) |
| 83 | + end = int(series[1]) |
| 84 | + |
| 85 | + for i in range(start, end + 1): |
| 86 | + pinlist[i] = True |
| 87 | + |
| 88 | + return tuple(pinlist) |
| 89 | + |
| 90 | + _dumpxml_xpath = "/domain/numatune" |
| 91 | + |
| 92 | + MEMORY_MODES = ["interleave", "strict", "preferred"] |
| 93 | + |
| 94 | + def __init__(self, conn, parsexml=None, parsexmlnode=None, caps=None): |
| 95 | + self._memory_nodeset = None |
| 96 | + self._memory_mode = None |
| 97 | + |
| 98 | + XMLBuilderDomain.XMLBuilderDomain.__init__(self, conn, parsexml, |
| 99 | + parsexmlnode, caps) |
| 100 | + if self._is_parse(): |
| 101 | + return |
| 102 | + |
| 103 | + def _get_memory_nodeset(self): |
| 104 | + return self._memory_nodeset |
| 105 | + def _set_memory_nodeset(self, val): |
| 106 | + self._memory_nodeset = val |
| 107 | + memory_nodeset = _xml_property(_get_memory_nodeset, |
| 108 | + _set_memory_nodeset, |
| 109 | + xpath="./numatune/memory/@nodeset") |
| 110 | + |
| 111 | + def _get_memory_mode(self): |
| 112 | + return self._memory_mode |
| 113 | + def _set_memory_mode(self, val): |
| 114 | + self._memory_mode = val |
| 115 | + memory_mode = _xml_property(_get_memory_mode, |
| 116 | + _set_memory_mode, |
| 117 | + xpath="./numatune/memory/@mode") |
| 118 | + |
| 119 | + def _get_memory_xml(self): |
| 120 | + if not self.memory_nodeset: |
| 121 | + return "" |
| 122 | + |
| 123 | + xml = " <memory" |
| 124 | + if self.memory_mode: |
| 125 | + xml += " mode='%s'" % self.memory_mode |
| 126 | + if self.memory_nodeset: |
| 127 | + xml += " nodeset='%s'" % self.memory_nodeset |
| 128 | + xml += "/>\n" |
| 129 | + return xml |
| 130 | + |
| 131 | + def _get_xml_config(self): |
| 132 | + mem_xml = self._get_memory_xml() |
| 133 | + if not mem_xml: |
| 134 | + return "" |
| 135 | + |
| 136 | + xml = " <numatune>\n" |
| 137 | + xml += mem_xml |
| 138 | + xml += " </numatune>" |
| 139 | + return xml |
0 commit comments