github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/qos/mem_enhancement.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 qos
    18  
    19  import (
    20  	"strconv"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  
    24  	apiconsts "github.com/kubewharf/katalyst-api/pkg/consts"
    25  	"github.com/kubewharf/katalyst-core/pkg/config/generic"
    26  )
    27  
    28  func ParseMemoryEnhancement(qosConf *generic.QoSConfiguration, pod *v1.Pod) map[string]string {
    29  	if pod == nil || qosConf == nil {
    30  		return nil
    31  	}
    32  	return qosConf.GetQoSEnhancementKVs(pod, map[string]string{}, apiconsts.PodAnnotationMemoryEnhancementKey)
    33  }
    34  
    35  // IsPodNumaBinding checks whether the pod needs numa-binding
    36  func IsPodNumaBinding(qosConf *generic.QoSConfiguration, pod *v1.Pod) bool {
    37  	memoryEnhancement := ParseMemoryEnhancement(qosConf, pod)
    38  	return AnnotationsIndicateNUMABinding(memoryEnhancement)
    39  }
    40  
    41  // IsPodNumaExclusive checks whether the pod needs numa-exclusive
    42  func IsPodNumaExclusive(qosConf *generic.QoSConfiguration, pod *v1.Pod) bool {
    43  	isPodNumaBinding := IsPodNumaBinding(qosConf, pod)
    44  	if !isPodNumaBinding {
    45  		return false
    46  	}
    47  
    48  	isDedicatedPod, err := qosConf.CheckDedicatedQoS(pod, map[string]string{})
    49  	if err != nil || !isDedicatedPod {
    50  		return false
    51  	}
    52  
    53  	memoryEnhancement := ParseMemoryEnhancement(qosConf, pod)
    54  	return AnnotationsIndicateNUMAExclusive(memoryEnhancement)
    55  }
    56  
    57  func AnnotationsIndicateNUMABinding(annotations map[string]string) bool {
    58  	return annotations[apiconsts.PodAnnotationMemoryEnhancementNumaBinding] ==
    59  		apiconsts.PodAnnotationMemoryEnhancementNumaBindingEnable
    60  }
    61  
    62  func AnnotationsIndicateNUMAExclusive(annotations map[string]string) bool {
    63  	return AnnotationsIndicateNUMABinding(annotations) &&
    64  		annotations[apiconsts.PodAnnotationMemoryEnhancementNumaExclusive] ==
    65  			apiconsts.PodAnnotationMemoryEnhancementNumaExclusiveEnable
    66  }
    67  
    68  // GetRSSOverUseEvictThreshold parse the user specified threshold and checks if it's valid
    69  func GetRSSOverUseEvictThreshold(qosConf *generic.QoSConfiguration, pod *v1.Pod) (threshold *float64, invalid bool) {
    70  	memoryEnhancement := ParseMemoryEnhancement(qosConf, pod)
    71  	thresholdStr, ok := memoryEnhancement[apiconsts.PodAnnotationMemoryEnhancementRssOverUseThreshold]
    72  	if !ok {
    73  		return
    74  	}
    75  
    76  	parsedThreshold, parseErr := strconv.ParseFloat(thresholdStr, 64)
    77  	if parseErr != nil {
    78  		invalid = true
    79  		return
    80  	}
    81  
    82  	if !isValidRatioThreshold(parsedThreshold) {
    83  		invalid = true
    84  		return
    85  	}
    86  
    87  	threshold = &parsedThreshold
    88  	return
    89  }
    90  
    91  func isValidRatioThreshold(threshold float64) bool {
    92  	return threshold > 0
    93  }
    94  
    95  // GetOOMPriority parse the user specified oom priority from memory enhancement annotation
    96  func GetOOMPriority(qosConf *generic.QoSConfiguration, pod *v1.Pod) (priority *int, invalid bool) {
    97  	memoryEnhancement := ParseMemoryEnhancement(qosConf, pod)
    98  	oomPriorityStr, ok := memoryEnhancement[apiconsts.PodAnnotationMemoryEnhancementOOMPriority]
    99  	if !ok {
   100  		return
   101  	}
   102  
   103  	parsedOOMPriority, parseErr := strconv.Atoi(oomPriorityStr)
   104  	if parseErr != nil {
   105  		invalid = true
   106  		return
   107  	}
   108  
   109  	priority = &parsedOOMPriority
   110  	return
   111  }