...

Source file src/k8s.io/kubernetes/pkg/kubelet/kuberuntime/helpers_linux.go

Documentation: k8s.io/kubernetes/pkg/kubelet/kuberuntime

     1  //go:build linux
     2  // +build linux
     3  
     4  /*
     5  Copyright 2018 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package kuberuntime
    21  
    22  import (
    23  	"k8s.io/kubernetes/pkg/kubelet/cm"
    24  	"math"
    25  )
    26  
    27  const (
    28  	milliCPUToCPU = 1000
    29  
    30  	// 100000 microseconds is equivalent to 100ms
    31  	quotaPeriod = 100000
    32  	// 1000 microseconds is equivalent to 1ms
    33  	// defined here:
    34  	// https://github.com/torvalds/linux/blob/cac03ac368fabff0122853de2422d4e17a32de08/kernel/sched/core.c#L10546
    35  	minQuotaPeriod = 1000
    36  )
    37  
    38  // milliCPUToQuota converts milliCPU to CFS quota and period values
    39  // Input parameters and resulting value is number of microseconds.
    40  func milliCPUToQuota(milliCPU int64, period int64) (quota int64) {
    41  	// CFS quota is measured in two values:
    42  	//  - cfs_period_us=100ms (the amount of time to measure usage across)
    43  	//  - cfs_quota=20ms (the amount of cpu time allowed to be used across a period)
    44  	// so in the above example, you are limited to 20% of a single CPU
    45  	// for multi-cpu environments, you just scale equivalent amounts
    46  	// see https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt for details
    47  	if milliCPU == 0 {
    48  		return
    49  	}
    50  
    51  	// we then convert your milliCPU to a value normalized over a period
    52  	quota = (milliCPU * period) / milliCPUToCPU
    53  
    54  	// quota needs to be a minimum of 1ms.
    55  	if quota < minQuotaPeriod {
    56  		quota = minQuotaPeriod
    57  	}
    58  
    59  	return
    60  }
    61  
    62  // sharesToMilliCPU converts CpuShares (cpu.shares) to milli-CPU value
    63  // TODO(vinaykul,InPlacePodVerticalScaling): Address issue that sets min req/limit to 2m/10m before beta
    64  // See: https://github.com/kubernetes/kubernetes/pull/102884#discussion_r662552642
    65  func sharesToMilliCPU(shares int64) int64 {
    66  	milliCPU := int64(0)
    67  	if shares >= int64(cm.MinShares) {
    68  		milliCPU = int64(math.Ceil(float64(shares*milliCPUToCPU) / float64(cm.SharesPerCPU)))
    69  	}
    70  	return milliCPU
    71  }
    72  
    73  // quotaToMilliCPU converts cpu.cfs_quota_us and cpu.cfs_period_us to milli-CPU value
    74  func quotaToMilliCPU(quota int64, period int64) int64 {
    75  	if quota == -1 {
    76  		return int64(0)
    77  	}
    78  	return (quota * milliCPUToCPU) / period
    79  }
    80  

View as plain text