github.com/kubewharf/katalyst-core@v0.5.3/pkg/scheduler/plugins/noderesourcetopology/most_allocated.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  	v1 "k8s.io/api/core/v1"
    21  	"k8s.io/apimachinery/pkg/api/resource"
    22  	"k8s.io/apimachinery/pkg/util/sets"
    23  	"k8s.io/kubernetes/pkg/scheduler/framework"
    24  )
    25  
    26  // allocatable can be aggregated resource for multi numa,
    27  // because dedicated_cores with numa_exclusive pod may be allocated to more than one numa under numeric policy.
    28  func mostAllocatedScoreStrategy(requested, allocatable v1.ResourceList, resourceToWeightMap resourceToWeightMap, alignedResource sets.String) int64 {
    29  	var score int64 = 0
    30  	var weightSum int64 = 0
    31  
    32  	for resourceName := range requested {
    33  		// resources not in alignedResource will not be calculated,
    34  		// these resources may not be allocated to the same numas with alignedResource
    35  		if alignedResource != nil && !alignedResource.Has(resourceName.String()) {
    36  			continue
    37  		}
    38  		resourceScore := mostAllocatedScore(requested[resourceName], allocatable[resourceName])
    39  		weight := resourceToWeightMap.weight(resourceName)
    40  		score += resourceScore * weight
    41  		weightSum += weight
    42  	}
    43  
    44  	return score / weightSum
    45  }
    46  
    47  // The used capacity is calculated on a scale of 0-MaxNodeScore (MaxNodeScore is
    48  // constant with value set to 100).
    49  // 0 being the lowest priority and 100 being the highest.
    50  // The more allocated resources the node has, the higher the score is.
    51  func mostAllocatedScore(requested, capacity resource.Quantity) int64 {
    52  	if capacity.CmpInt64(0) == 0 {
    53  		return 0
    54  	}
    55  	if requested.Cmp(capacity) > 0 {
    56  		return 0
    57  	}
    58  
    59  	return requested.Value() * framework.MaxNodeScore / capacity.Value()
    60  }