katalyst-core/pkg/util/qos/mem_enhancement.go

78 lines
2.4 KiB
Go

/*
Copyright 2022 The Katalyst Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package qos
import (
"strconv"
v1 "k8s.io/api/core/v1"
"github.com/kubewharf/katalyst-api/pkg/consts"
"github.com/kubewharf/katalyst-core/pkg/config/generic"
)
// IsPodNumaBinding checks whether the pod needs numa-binding
func IsPodNumaBinding(qosConf *generic.QoSConfiguration, pod *v1.Pod) bool {
qosLevel, _ := qosConf.GetQoSLevelForPod(pod)
if qosLevel != consts.PodAnnotationQoSLevelDedicatedCores {
return false
}
memoryEnhancement := ParseKatalystQOSEnhancement(qosConf.GetQoSEnhancementsForPod(pod), consts.PodAnnotationMemoryEnhancementKey)
return AnnotationsIndicateNUMABinding(memoryEnhancement)
}
func AnnotationsIndicateNUMABinding(annotations map[string]string) bool {
return annotations[consts.PodAnnotationMemoryEnhancementNumaBinding] ==
consts.PodAnnotationMemoryEnhancementNumaBindingEnable
}
func AnnotationsIndicateNUMAExclusive(annotations map[string]string) bool {
return AnnotationsIndicateNUMABinding(annotations) &&
annotations[consts.PodAnnotationMemoryEnhancementNumaExclusive] ==
consts.PodAnnotationMemoryEnhancementNumaExclusiveEnable
}
// GetRSSOverUseEvictThreshold parse the user specified threshold and checks if it's valid
func GetRSSOverUseEvictThreshold(qosConf *generic.QoSConfiguration, pod *v1.Pod) (threshold *float64, invalid bool) {
memoryEnhancement := ParseKatalystQOSEnhancement(qosConf.GetQoSEnhancementsForPod(pod), consts.PodAnnotationMemoryEnhancementKey)
thresholdStr, ok := memoryEnhancement[consts.PodAnnotationMemoryEnhancementRssOverUseThreshold]
if !ok {
return
}
parsedThreshold, parseErr := strconv.ParseFloat(thresholdStr, 64)
if parseErr != nil {
invalid = true
return
}
if !isValidRatioThreshold(parsedThreshold) {
invalid = true
return
}
threshold = &parsedThreshold
return
}
func isValidRatioThreshold(threshold float64) bool {
return threshold > 0
}