summaryrefslogtreecommitdiff
path: root/virtinst/CPU.py
blob: af3744e499b2a8476c891f49d9d51ca6c39dd385 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#
# Copyright 2010  Red Hat, Inc.
# Cole Robinson <crobinso@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free  Software Foundation; either version 2 of the License, or
# (at your option)  any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.

from virtinst.xmlbuilder import XMLBuilder, XMLProperty


class CPUFeature(XMLBuilder):
    """
    Class for generating <cpu> child <feature> XML
    """

    POLICIES = ["force", "require", "optional", "disable", "forbid"]

    _XML_ROOT_XPATH = "/domain/cpu/feature"
    _XML_PROP_ORDER = ["_xmlname", "policy"]

    def __init__(self, conn, name, parsexml=None, parsexmlnode=None):
        XMLBuilder.__init__(self, conn, parsexml, parsexmlnode)

        self._name = name
        self._xmlname = name

    def _get_name(self):
        return self._xmlname
    name = property(_get_name)

    def _name_xpath(self):
        return "./cpu/feature[@name='%s']/@name" % self._name
    _xmlname = XMLProperty(name="feature name",
                      make_getter_xpath_cb=_name_xpath,
                      make_setter_xpath_cb=_name_xpath)
    def _policy_xpath(self):
        return "./cpu/feature[@name='%s']/@policy" % self._name
    policy = XMLProperty(name="feature policy",
                         make_getter_xpath_cb=_policy_xpath,
                         make_setter_xpath_cb=_policy_xpath)


class CPU(XMLBuilder):
    """
    Class for generating <cpu> XML
    """

    MATCHS = ["minimum", "exact", "strict"]

    _XML_ROOT_XPATH = "/domain/cpu"
    _XML_PROP_ORDER = ["mode", "match", "model", "vendor",
                       "sockets", "cores", "threads", "_features"]

    def __init__(self, conn, parsexml=None, parsexmlnode=None):
        self._features = []
        XMLBuilder.__init__(self, conn, parsexml, parsexmlnode)

    def _parsexml(self, xml, node):
        XMLBuilder._parsexml(self, xml, node)

        for node in self._xml_node.children or []:
            if node.name != "feature":
                continue
            if not node.prop("name"):
                continue
            feature = CPUFeature(self.conn, node.prop("name"),
                                 parsexmlnode=self._xml_node)
            self._features.append(feature)

    def add_feature(self, name, policy="require"):
        feature = CPUFeature(self.conn, name, parsexmlnode=self._xml_node)
        feature.policy = policy
        self._features.append(feature)

    def remove_feature(self, feature):
        self._features.remove(feature)
        feature.clear()

    def _get_features(self):
        return self._features[:]
    features = property(_get_features)

    def copy_host_cpu(self):
        """
        Enact the equivalent of qemu -cpu host, pulling all info
        from capabilities about the host CPU
        """
        cpu = self.conn.caps.host.cpu
        if not cpu.model:
            raise ValueError(_("No host CPU reported in capabilities"))

        self.mode = "custom"
        self.match = "exact"
        self.model = cpu.model
        self.vendor = cpu.vendor

        for feature in self.features:
            self.remove_feature(feature)
        for name in cpu.features.names():
            self.add_feature(name)

    def vcpus_from_topology(self):
        """
        Determine the CPU count represented by topology, or 1 if
        no topology is set
        """
        self.set_topology_defaults()
        if self.sockets:
            return self.sockets * self.cores * self.threads
        return 1

    def set_topology_defaults(self, vcpus=None):
        """
        Fill in unset topology values, using the passed vcpus count if
        required
        """
        if (self.sockets is None and
            self.cores is None and
            self.threads is None):
            return

        if vcpus is None:
            if self.sockets is None:
                self.sockets = 1
            if self.threads is None:
                self.threads = 1
            if self.cores is None:
                self.cores = 1

        vcpus = int(vcpus or 0)
        if not self.sockets:
            if not self.cores:
                self.sockets = vcpus / self.threads
            else:
                self.sockets = vcpus / self.cores

        if not self.cores:
            if not self.threads:
                self.cores = vcpus / self.sockets
            else:
                self.cores = vcpus / (self.sockets * self.threads)

        if not self.threads:
            self.threads = vcpus / (self.sockets * self.cores)

        return


    ##################
    # XML properties #
    ##################

    def _set_model(self, val):
        if val:
            self.mode = "custom"
            if not self.match:
                self.match = "exact"
        return val
    model = XMLProperty(xpath="./cpu/model", set_converter=_set_model)

    match = XMLProperty(xpath="./cpu/@match")
    vendor = XMLProperty(xpath="./cpu/vendor")
    mode = XMLProperty(xpath="./cpu/@mode")

    sockets = XMLProperty(xpath="./cpu/topology/@sockets", is_int=True)
    cores = XMLProperty(xpath="./cpu/topology/@cores", is_int=True)
    threads = XMLProperty(xpath="./cpu/topology/@threads", is_int=True)