github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/qos/oom_priority.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  	apiconsts "github.com/kubewharf/katalyst-api/pkg/consts"
    21  )
    22  
    23  const (
    24  	IgnoreOOMPriorityScore                int = -300
    25  	DefaultReclaimedCoresOOMPriorityScore int = -100
    26  	DefaultSharedCoresOOMPriorityScore    int = 0
    27  	DefaultDedicatedCoresOOMPriorityScore int = 100
    28  	DefaultSystemCoresOOMPriorityScore    int = 200
    29  	TopOOMPriorityScore                   int = 300
    30  )
    31  
    32  func AlignOOMPriority(qosLevel string, userSpecifiedScore *int) (oomPriorityScore int) {
    33  	// Adjust OOM priority score if user specified
    34  	if userSpecifiedScore != nil {
    35  		oomPriorityScore = *userSpecifiedScore
    36  
    37  		if oomPriorityScore == TopOOMPriorityScore {
    38  			return
    39  		}
    40  
    41  		// Adjust OOM Priority if it's out of range
    42  		switch qosLevel {
    43  		case apiconsts.PodAnnotationQoSLevelReclaimedCores:
    44  			if oomPriorityScore < DefaultReclaimedCoresOOMPriorityScore {
    45  				oomPriorityScore = DefaultReclaimedCoresOOMPriorityScore
    46  			} else if oomPriorityScore >= DefaultSharedCoresOOMPriorityScore {
    47  				oomPriorityScore = DefaultSharedCoresOOMPriorityScore - 1
    48  			}
    49  		case apiconsts.PodAnnotationQoSLevelSharedCores:
    50  			if oomPriorityScore < DefaultSharedCoresOOMPriorityScore {
    51  				oomPriorityScore = DefaultSharedCoresOOMPriorityScore
    52  			} else if oomPriorityScore >= DefaultDedicatedCoresOOMPriorityScore {
    53  				oomPriorityScore = DefaultDedicatedCoresOOMPriorityScore - 1
    54  			}
    55  		case apiconsts.PodAnnotationQoSLevelDedicatedCores:
    56  			if oomPriorityScore < DefaultDedicatedCoresOOMPriorityScore {
    57  				oomPriorityScore = DefaultDedicatedCoresOOMPriorityScore
    58  			} else if oomPriorityScore >= DefaultSystemCoresOOMPriorityScore {
    59  				oomPriorityScore = DefaultSystemCoresOOMPriorityScore - 1
    60  			}
    61  		case apiconsts.PodAnnotationQoSLevelSystemCores:
    62  			if oomPriorityScore < DefaultSystemCoresOOMPriorityScore {
    63  				oomPriorityScore = DefaultSystemCoresOOMPriorityScore
    64  			} else if oomPriorityScore >= TopOOMPriorityScore {
    65  				oomPriorityScore = TopOOMPriorityScore - 1
    66  			}
    67  		default:
    68  			// Invalid QoS level, set to minimum value
    69  			oomPriorityScore = DefaultReclaimedCoresOOMPriorityScore
    70  		}
    71  	} else {
    72  		// No user specified score, use default based on QoS level
    73  		switch qosLevel {
    74  		case apiconsts.PodAnnotationQoSLevelReclaimedCores:
    75  			oomPriorityScore = DefaultReclaimedCoresOOMPriorityScore
    76  		case apiconsts.PodAnnotationQoSLevelSharedCores:
    77  			oomPriorityScore = DefaultSharedCoresOOMPriorityScore
    78  		case apiconsts.PodAnnotationQoSLevelDedicatedCores:
    79  			oomPriorityScore = DefaultDedicatedCoresOOMPriorityScore
    80  		case apiconsts.PodAnnotationQoSLevelSystemCores:
    81  			oomPriorityScore = DefaultSystemCoresOOMPriorityScore
    82  		default:
    83  			// Invalid QoS level, set to minimum value
    84  			oomPriorityScore = DefaultReclaimedCoresOOMPriorityScore
    85  		}
    86  	}
    87  
    88  	return
    89  }