Coverage for nova/scheduler/weights/cpu.py: 100%
11 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-24 11:16 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-24 11:16 +0000
1# Copyright (c) 2016, Red Hat Inc.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
16"""
17CPU Weigher. Weigh hosts by their CPU usage.
19The default is to spread instances across all hosts evenly. If you prefer
20stacking, you can set the 'cpu_weight_multiplier' option (by configuration
21or aggregate metadata) to a negative number and the weighing has the opposite
22effect of the default.
23"""
25import nova.conf
26from nova.scheduler import utils
27from nova.scheduler import weights
29CONF = nova.conf.CONF
32class CPUWeigher(weights.BaseHostWeigher):
33 minval = 0
35 def weight_multiplier(self, host_state):
36 """Override the weight multiplier."""
37 return utils.get_weight_multiplier(
38 host_state, 'cpu_weight_multiplier',
39 CONF.filter_scheduler.cpu_weight_multiplier)
41 def _weigh_object(self, host_state, weight_properties):
42 """Higher weights win. We want spreading to be the default."""
43 vcpus_free = (
44 host_state.vcpus_total * host_state.cpu_allocation_ratio -
45 host_state.vcpus_used)
46 return vcpus_free