summaryrefslogtreecommitdiff
path: root/nova/virt/libvirt/cpu/api.py
blob: 1c17458d6b2f2c66e9a1ebc26399115254f6a54a (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
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from dataclasses import dataclass

from oslo_log import log as logging

import nova.conf
from nova import exception
from nova.i18n import _
from nova import objects
from nova.virt import hardware
from nova.virt.libvirt.cpu import core

LOG = logging.getLogger(__name__)

CONF = nova.conf.CONF


@dataclass
class Core:
    """Class to model a CPU core as reported by sysfs.

    It may be a physical CPU core or a hardware thread on a shared CPU core
    depending on if the system supports SMT.
    """

    # NOTE(sbauza): ident is a mandatory field.
    # The CPU core id/number
    ident: int

    @property
    def online(self) -> bool:
        return core.get_online(self.ident)

    @online.setter
    def online(self, state: bool) -> None:
        if state:
            core.set_online(self.ident)
        else:
            core.set_offline(self.ident)

    def __hash__(self):
        return hash(self.ident)

    def __eq__(self, other):
        return self.ident == other.ident

    def __str__(self):
        return str(self.ident)

    @property
    def governor(self) -> str:
        return core.get_governor(self.ident)

    def set_high_governor(self) -> None:
        core.set_governor(self.ident, CONF.libvirt.cpu_power_governor_high)

    def set_low_governor(self) -> None:
        core.set_governor(self.ident, CONF.libvirt.cpu_power_governor_low)


def power_up(instance: objects.Instance) -> None:
    if not CONF.libvirt.cpu_power_management:
        return
    if instance.numa_topology is None:
        return

    cpu_dedicated_set = hardware.get_cpu_dedicated_set() or set()
    pcpus = instance.numa_topology.cpu_pinning
    powered_up = set()
    for pcpu in pcpus:
        if pcpu in cpu_dedicated_set:
            pcpu = Core(pcpu)
            if CONF.libvirt.cpu_power_management_strategy == 'cpu_state':
                pcpu.online = True
            else:
                pcpu.set_high_governor()
            powered_up.add(str(pcpu))
    LOG.debug("Cores powered up : %s", powered_up)


def power_down(instance: objects.Instance) -> None:
    if not CONF.libvirt.cpu_power_management:
        return
    if instance.numa_topology is None:
        return

    cpu_dedicated_set = hardware.get_cpu_dedicated_set() or set()
    pcpus = instance.numa_topology.cpu_pinning
    powered_down = set()
    for pcpu in pcpus:
        if pcpu in cpu_dedicated_set:
            pcpu = Core(pcpu)
            if CONF.libvirt.cpu_power_management_strategy == 'cpu_state':
                pcpu.online = False
            else:
                pcpu.set_low_governor()
            powered_down.add(str(pcpu))
    LOG.debug("Cores powered down : %s", powered_down)


def power_down_all_dedicated_cpus() -> None:
    if not CONF.libvirt.cpu_power_management:
        return
    if (CONF.libvirt.cpu_power_management and
        not CONF.compute.cpu_dedicated_set
    ):
        msg = _("'[compute]/cpu_dedicated_set' is mandatory to be set if "
                "'[libvirt]/cpu_power_management' is set."
                "Please provide the CPUs that can be pinned or don't use the "
                "power management if you only use shared CPUs.")
        raise exception.InvalidConfiguration(msg)

    cpu_dedicated_set = hardware.get_cpu_dedicated_set() or set()
    for pcpu in cpu_dedicated_set:
        pcpu = Core(pcpu)
        if CONF.libvirt.cpu_power_management_strategy == 'cpu_state':
            pcpu.online = False
        else:
            pcpu.set_low_governor()
    LOG.debug("Cores powered down : %s", cpu_dedicated_set)


def validate_all_dedicated_cpus() -> None:
    if not CONF.libvirt.cpu_power_management:
        return
    cpu_dedicated_set = hardware.get_cpu_dedicated_set() or set()
    governors = set()
    cpu_states = set()
    for pcpu in cpu_dedicated_set:
        pcpu = Core(pcpu)
        # we need to collect the governors strategy and the CPU states
        governors.add(pcpu.governor)
        cpu_states.add(pcpu.online)
    if CONF.libvirt.cpu_power_management_strategy == 'cpu_state':
        # all the cores need to have the same governor strategy
        if len(governors) > 1:
            msg = _("All the cores need to have the same governor strategy"
                    "before modifying the CPU states. You can reboot the "
                    "compute node if you prefer.")
            raise exception.InvalidConfiguration(msg)
    elif CONF.libvirt.cpu_power_management_strategy == 'governor':
        # all the cores need to be online
        if False in cpu_states:
            msg = _("All the cores need to be online before modifying the "
                    "governor strategy.")
            raise exception.InvalidConfiguration(msg)