volcano.sh/volcano@v1.9.0/pkg/scheduler/plugins/numaaware/policy/policy.go (about) 1 /* 2 Copyright 2021 The Volcano Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain 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, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package policy 18 19 import ( 20 "k8s.io/klog/v2" 21 "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/bitmask" 22 ) 23 24 func filterProvidersHints(providersHints []map[string][]TopologyHint) [][]TopologyHint { 25 var allProviderHints [][]TopologyHint 26 for _, hints := range providersHints { 27 // If hints is nil, insert a single, preferred any-numa hint into allProviderHints. 28 if len(hints) == 0 { 29 klog.Infof("[numatopo] Hint Provider has no preference for NUMA affinity with any resource") 30 allProviderHints = append(allProviderHints, []TopologyHint{{nil, true}}) 31 continue 32 } 33 34 // Otherwise, accumulate the hints for each resource type into allProviderHints. 35 for resource := range hints { 36 if hints[resource] == nil { 37 klog.Infof("[numatopo] Hint Provider has no preference for NUMA affinity with resource '%s'", resource) 38 allProviderHints = append(allProviderHints, []TopologyHint{{nil, true}}) 39 continue 40 } 41 42 if len(hints[resource]) == 0 { 43 klog.Infof("[numatopo] Hint Provider has no possible NUMA affinities for resource '%s'", resource) 44 allProviderHints = append(allProviderHints, []TopologyHint{{nil, false}}) 45 continue 46 } 47 48 allProviderHints = append(allProviderHints, hints[resource]) 49 } 50 } 51 return allProviderHints 52 } 53 54 func mergeFilteredHints(numaNodes []int, filteredHints [][]TopologyHint) TopologyHint { 55 // Set the default affinity as an any-numa affinity containing the list 56 // of NUMA Nodes available on this machine. 57 defaultAffinity, _ := bitmask.NewBitMask(numaNodes...) 58 59 // Set the bestHint to return from this function as {nil false}. 60 // This will only be returned if no better hint can be found when 61 // merging hints from each hint provider. 62 bestHint := TopologyHint{defaultAffinity, false} 63 iterateAllProviderTopologyHints(filteredHints, func(permutation []TopologyHint) { 64 // Get the NUMANodeAffinity from each hint in the permutation and see if any 65 // of them encode unpreferred allocations. 66 mergedHint := mergePermutation(numaNodes, permutation) 67 // Only consider mergedHints that result in a NUMANodeAffinity > 0 to 68 // replace the current bestHint. 69 if mergedHint.NUMANodeAffinity.Count() == 0 { 70 return 71 } 72 73 // If the current bestHint is non-preferred and the new mergedHint is 74 // preferred, always choose the preferred hint over the non-preferred one. 75 if mergedHint.Preferred && !bestHint.Preferred { 76 bestHint = mergedHint 77 return 78 } 79 80 // If the current bestHint is preferred and the new mergedHint is 81 // non-preferred, never update bestHint, regardless of mergedHint's 82 // narowness. 83 if !mergedHint.Preferred && bestHint.Preferred { 84 return 85 } 86 87 // If mergedHint and bestHint has the same preference, only consider 88 // mergedHints that have a narrower NUMANodeAffinity than the 89 // NUMANodeAffinity in the current bestHint. 90 if !mergedHint.NUMANodeAffinity.IsNarrowerThan(bestHint.NUMANodeAffinity) { 91 return 92 } 93 94 // In all other cases, update bestHint to the current mergedHint 95 bestHint = mergedHint 96 }) 97 98 return bestHint 99 } 100 101 // Iterate over all permutations of hints in 'allProviderHints [][]TopologyHint'. 102 // 103 // This procedure is implemented as a recursive function over the set of hints 104 // in 'allproviderHints[i]'. It applies the function 'callback' to each 105 // permutation as it is found. It is the equivalent of: 106 // 107 // for i := 0; i < len(providerHints[0]); i++ 108 // 109 // for j := 0; j < len(providerHints[1]); j++ 110 // for k := 0; k < len(providerHints[2]); k++ 111 // ... 112 // for z := 0; z < len(providerHints[-1]); z++ 113 // permutation := []TopologyHint{ 114 // providerHints[0][i], 115 // providerHints[1][j], 116 // providerHints[2][k], 117 // ... 118 // providerHints[-1][z] 119 // } 120 // callback(permutation) 121 func iterateAllProviderTopologyHints(allProviderHints [][]TopologyHint, callback func([]TopologyHint)) { 122 // Internal helper function to accumulate the permutation before calling the callback. 123 var iterate func(i int, accum []TopologyHint) 124 iterate = func(i int, accum []TopologyHint) { 125 // Base case: we have looped through all providers and have a full permutation. 126 if i == len(allProviderHints) { 127 callback(accum) 128 return 129 } 130 131 // Loop through all hints for provider 'i', and recurse to build the 132 // permutation of this hint with all hints from providers 'i++'. 133 for j := range allProviderHints[i] { 134 iterate(i+1, append(accum, allProviderHints[i][j])) 135 } 136 } 137 iterate(0, []TopologyHint{}) 138 } 139 140 // Merge a TopologyHints permutation to a single hint by performing a bitwise-AND 141 // of their affinity masks. The hint shall be preferred if all hits in the permutation 142 // are preferred. 143 func mergePermutation(numaNodes []int, permutation []TopologyHint) TopologyHint { 144 // Get the NUMANodeAffinity from each hint in the permutation and see if any 145 // of them encode unpreferred allocations. 146 preferred := true 147 defaultAffinity, _ := bitmask.NewBitMask(numaNodes...) 148 var numaAffinities []bitmask.BitMask 149 for _, hint := range permutation { 150 // Only consider hints that have an actual NUMANodeAffinity set. 151 if hint.NUMANodeAffinity == nil { 152 numaAffinities = append(numaAffinities, defaultAffinity) 153 } else { 154 numaAffinities = append(numaAffinities, hint.NUMANodeAffinity) 155 } 156 157 if !hint.Preferred { 158 preferred = false 159 } 160 } 161 162 // Merge the affinities using a bitwise-and operation. 163 mergedAffinity := bitmask.And(defaultAffinity, numaAffinities...) 164 // Build a mergedHint from the merged affinity mask, indicating if an 165 // preferred allocation was used to generate the affinity mask or not. 166 return TopologyHint{mergedAffinity, preferred} 167 }