k8s.io/kubernetes@v1.29.3/pkg/kubelet/cm/topologymanager/policy_single_numa_node.go (about) 1 /* 2 Copyright 2019 The Kubernetes 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 topologymanager 18 19 type singleNumaNodePolicy struct { 20 // numaInfo represents list of NUMA Nodes available on the underlying machine and distances between them 21 numaInfo *NUMAInfo 22 opts PolicyOptions 23 } 24 25 var _ Policy = &singleNumaNodePolicy{} 26 27 // PolicySingleNumaNode policy name. 28 const PolicySingleNumaNode string = "single-numa-node" 29 30 // NewSingleNumaNodePolicy returns single-numa-node policy. 31 func NewSingleNumaNodePolicy(numaInfo *NUMAInfo, opts PolicyOptions) Policy { 32 return &singleNumaNodePolicy{numaInfo: numaInfo, opts: opts} 33 } 34 35 func (p *singleNumaNodePolicy) Name() string { 36 return PolicySingleNumaNode 37 } 38 39 func (p *singleNumaNodePolicy) canAdmitPodResult(hint *TopologyHint) bool { 40 return hint.Preferred 41 } 42 43 // Return hints that have valid bitmasks with exactly one bit set. 44 func filterSingleNumaHints(allResourcesHints [][]TopologyHint) [][]TopologyHint { 45 var filteredResourcesHints [][]TopologyHint 46 for _, oneResourceHints := range allResourcesHints { 47 var filtered []TopologyHint 48 for _, hint := range oneResourceHints { 49 if hint.NUMANodeAffinity == nil && hint.Preferred { 50 filtered = append(filtered, hint) 51 } 52 if hint.NUMANodeAffinity != nil && hint.NUMANodeAffinity.Count() == 1 && hint.Preferred { 53 filtered = append(filtered, hint) 54 } 55 } 56 filteredResourcesHints = append(filteredResourcesHints, filtered) 57 } 58 return filteredResourcesHints 59 } 60 61 func (p *singleNumaNodePolicy) Merge(providersHints []map[string][]TopologyHint) (TopologyHint, bool) { 62 filteredHints := filterProvidersHints(providersHints) 63 // Filter to only include don't cares and hints with a single NUMA node. 64 singleNumaHints := filterSingleNumaHints(filteredHints) 65 66 merger := NewHintMerger(p.numaInfo, singleNumaHints, p.Name(), p.opts) 67 bestHint := merger.Merge() 68 69 if bestHint.NUMANodeAffinity.IsEqual(p.numaInfo.DefaultAffinityMask()) { 70 bestHint = TopologyHint{nil, bestHint.Preferred} 71 } 72 73 admit := p.canAdmitPodResult(&bestHint) 74 return bestHint, admit 75 }