github.com/kubewharf/katalyst-core@v0.5.3/pkg/scheduler/plugins/qosawarenoderesources/requested_to_capacity_ratio.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 qosawarenoderesources
    18  
    19  import (
    20  	"math"
    21  
    22  	kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
    23  	"k8s.io/kubernetes/pkg/scheduler/framework"
    24  	"k8s.io/kubernetes/pkg/scheduler/framework/plugins/helper"
    25  )
    26  
    27  const (
    28  	maxUtilization = 100
    29  )
    30  
    31  // buildRequestedToCapacityRatioScorerFunction allows users to apply bin packing
    32  // on core resources like CPU, Memory as well as extended resources like accelerators.
    33  func buildRequestedToCapacityRatioScorerFunction(scoringFunctionShape helper.FunctionShape, resourceToWeightMap resourceToWeightMap) func(resourceToValueMap, resourceToValueMap) int64 {
    34  	rawScoringFunction := helper.BuildBrokenLinearFunction(scoringFunctionShape)
    35  	resourceScoringFunction := func(requested, capacity int64) int64 {
    36  		if capacity == 0 || requested > capacity {
    37  			return rawScoringFunction(maxUtilization)
    38  		}
    39  
    40  		return rawScoringFunction(requested * maxUtilization / capacity)
    41  	}
    42  	return func(requested, allocatable resourceToValueMap) int64 {
    43  		var nodeScore, weightSum int64
    44  		for resource := range requested {
    45  			weight := resourceToWeightMap[resource]
    46  			resourceScore := resourceScoringFunction(requested[resource], allocatable[resource])
    47  			if resourceScore > 0 {
    48  				nodeScore += resourceScore * weight
    49  				weightSum += weight
    50  			}
    51  		}
    52  		if weightSum == 0 {
    53  			return 0
    54  		}
    55  		return int64(math.Round(float64(nodeScore) / float64(weightSum)))
    56  	}
    57  }
    58  
    59  func requestedToCapacityRatioScorer(weightMap resourceToWeightMap, shape []kubeschedulerconfig.UtilizationShapePoint) func(resourceToValueMap, resourceToValueMap) int64 {
    60  	shapes := make([]helper.FunctionShapePoint, 0, len(shape))
    61  	for _, point := range shape {
    62  		shapes = append(shapes, helper.FunctionShapePoint{
    63  			Utilization: int64(point.Utilization),
    64  			// MaxCustomPriorityScore may diverge from the max score used in the scheduler and defined by MaxNodeScore,
    65  			// therefore we need to scale the score returned by requested to capacity ratio to the score range
    66  			// used by the scheduler.
    67  			Score: int64(point.Score) * (framework.MaxNodeScore / kubeschedulerconfig.MaxCustomPriorityScore),
    68  		})
    69  	}
    70  
    71  	return buildRequestedToCapacityRatioScorerFunction(shapes, weightMap)
    72  }