Coverage for nova/scheduler/weights/pci.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-17 15:08 +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. 

15 

16""" 

17PCI Affinity Weigher. Weigh hosts by their PCI availability. 

18 

19Prefer hosts with PCI devices for instances with PCI requirements and vice 

20versa. Configure the importance of this affinitization using the 

21'pci_weight_multiplier' option (by configuration or aggregate metadata). 

22""" 

23 

24import nova.conf 

25from nova.scheduler import utils 

26from nova.scheduler import weights 

27 

28CONF = nova.conf.CONF 

29 

30# An arbitrary value used to ensure PCI-requesting instances are stacked rather 

31# than spread on hosts with PCI devices. The actual value of this filter is in 

32# the scarcity case, where there are very few PCI devices left in the cloud and 

33# we want to preserve the ones that do exist. To this end, we don't really mind 

34# if a host with 2000 PCI devices is weighted the same as one with 500 devices, 

35# as there's clearly no shortage there. 

36MAX_DEVS = 100 

37 

38 

39class PCIWeigher(weights.BaseHostWeigher): 

40 

41 def weight_multiplier(self, host_state): 

42 """Override the weight multiplier.""" 

43 return utils.get_weight_multiplier( 

44 host_state, 'pci_weight_multiplier', 

45 CONF.filter_scheduler.pci_weight_multiplier) 

46 

47 def _weigh_object(self, host_state, request_spec): 

48 """Higher weights win. We want to keep PCI hosts free unless needed. 

49 

50 Prefer hosts with the least number of PCI devices. If the instance 

51 requests PCI devices, this will ensure a stacking behavior and reserve 

52 as many totally free PCI hosts as possible. If PCI devices are not 

53 requested, this will ensure hosts with PCI devices are avoided 

54 completely, if possible. 

55 """ 

56 pools = host_state.pci_stats.pools if host_state.pci_stats else [] 

57 free = sum(pool['count'] for pool in pools) or 0 

58 

59 # reverse the "has PCI" values. For instances *without* PCI device 

60 # requests, this ensures we avoid the hosts with the most free PCI 

61 # devices. For the instances *with* PCI devices requests, this helps to 

62 # prevent fragmentation. If we didn't do this, hosts with the most PCI 

63 # devices would be weighted highest and would be used first which would 

64 # prevent instances requesting a larger number of PCI devices from 

65 # launching successfully. 

66 weight = MAX_DEVS - min(free, MAX_DEVS - 1) 

67 

68 return weight