github.com/kubewharf/katalyst-core@v0.5.3/pkg/scheduler/plugins/noderesourcetopology/balanced_allocation.go (about) 1 /* 2 Copyright 2022 The Katalyst 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 noderesourcetopology 18 19 import ( 20 "gonum.org/v1/gonum/stat" 21 v1 "k8s.io/api/core/v1" 22 "k8s.io/apimachinery/pkg/api/resource" 23 "k8s.io/apimachinery/pkg/util/sets" 24 "k8s.io/kubernetes/pkg/scheduler/framework" 25 ) 26 27 func balancedAllocationScoreStrategy(requested, allocatable v1.ResourceList, resourceToWeightMap resourceToWeightMap, alignedResource sets.String) int64 { 28 resourceFractions := make([]float64, 0) 29 30 // We don't care what kind of resources are being requested, we just iterate all of them. 31 // If NUMA zone doesn't have the requested resource, the score for that resource will be 0. 32 for resourceName := range requested { 33 if alignedResource != nil && !alignedResource.Has(resourceName.String()) { 34 continue 35 } 36 resourceFraction := fractionOfCapacity(requested[resourceName], allocatable[resourceName]) 37 // if requested > capacity the corresponding NUMA zone should never be preferred 38 if resourceFraction > 1 { 39 return 0 40 } 41 resourceFractions = append(resourceFractions, resourceFraction) 42 } 43 44 variance := stat.Variance(resourceFractions, nil) 45 46 // Since the variance is between positive fractions, it will be positive fraction. 1-variance lets the 47 // score to be higher for node which has least variance and multiplying it with `MaxNodeScore` provides the scaling 48 // factor needed. 49 return int64((1 - variance) * float64(framework.MaxNodeScore)) 50 } 51 52 func fractionOfCapacity(requested, capacity resource.Quantity) float64 { 53 if capacity.Value() == 0 { 54 return 1 55 } 56 return float64(requested.Value()) / float64(capacity.Value()) 57 }