2021-08-23 21:25:25 +00:00
|
|
|
//go:build linux
|
2020-06-11 19:34:47 +00:00
|
|
|
// +build linux
|
|
|
|
|
2018-08-25 21:08:12 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes 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 runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
2021-08-06 14:18:17 +00:00
|
|
|
"os"
|
2018-08-25 21:08:12 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
libcontainercgroups "github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NumCPU returns the number of logical CPUs usable by the current process.
|
|
|
|
// If CPU cgroups limits are configured, use cfs_quota_us / cfs_period_us
|
|
|
|
// as formula
|
2022-12-29 22:09:29 +00:00
|
|
|
//
|
|
|
|
// https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt
|
2018-08-25 21:08:12 +00:00
|
|
|
func NumCPU() int {
|
2024-05-20 12:37:46 +00:00
|
|
|
return NumCPUWithCustomPath("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NumCPUWithCustomPath(path string) int {
|
2018-08-25 21:08:12 +00:00
|
|
|
cpus := runtime.NumCPU()
|
|
|
|
|
2024-05-20 12:37:46 +00:00
|
|
|
cgroupVersionCheckPath := path
|
|
|
|
|
|
|
|
if cgroupVersionCheckPath == "" {
|
|
|
|
cgroupVersionCheckPath = "/sys/fs/cgroup/"
|
|
|
|
}
|
|
|
|
|
|
|
|
cgroupVersion := GetCgroupVersion(cgroupVersionCheckPath)
|
2023-03-31 05:58:43 +00:00
|
|
|
cpuQuota := int64(-1)
|
|
|
|
cpuPeriod := int64(-1)
|
2023-03-30 16:48:58 +00:00
|
|
|
|
2023-03-31 05:52:14 +00:00
|
|
|
if cgroupVersion == 1 {
|
2024-05-20 12:37:46 +00:00
|
|
|
cgroupPath := ""
|
|
|
|
if path == "" {
|
|
|
|
cgroupPathRd, err := libcontainercgroups.FindCgroupMountpoint("", "cpu")
|
|
|
|
if err != nil {
|
|
|
|
return cpus
|
|
|
|
}
|
|
|
|
cgroupPath = cgroupPathRd
|
|
|
|
} else {
|
|
|
|
cgroupPath = path
|
2023-04-25 15:19:07 +00:00
|
|
|
}
|
2023-03-31 05:53:34 +00:00
|
|
|
cpuQuota = readCgroupFileToInt64(cgroupPath, "cpu.cfs_quota_us")
|
|
|
|
cpuPeriod = readCgroupFileToInt64(cgroupPath, "cpu.cfs_period_us")
|
2023-03-30 16:48:58 +00:00
|
|
|
} else if cgroupVersion == 2 {
|
2024-05-20 12:37:46 +00:00
|
|
|
cgroupPath := "/sys/fs/cgroup/"
|
|
|
|
if path != "" {
|
|
|
|
cgroupPath = path
|
|
|
|
}
|
|
|
|
cpuQuota, cpuPeriod = readCgroup2FileToInt64Tuple(cgroupPath, "cpu.max")
|
2023-03-31 05:52:14 +00:00
|
|
|
}
|
2018-08-25 21:08:12 +00:00
|
|
|
|
|
|
|
if cpuQuota == -1 || cpuPeriod == -1 {
|
|
|
|
return cpus
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(math.Ceil(float64(cpuQuota) / float64(cpuPeriod)))
|
|
|
|
}
|
|
|
|
|
2024-05-20 12:37:46 +00:00
|
|
|
func GetCgroupVersion(cgroupPath string) int64 {
|
2023-03-31 05:36:12 +00:00
|
|
|
// /sys/fs/cgroup/cgroup.controllers will not exist with cgroupsv1
|
2024-05-20 12:37:46 +00:00
|
|
|
if _, err := os.Stat(filepath.Join(cgroupPath, "cgroup.controllers")); err == nil {
|
2023-03-31 05:52:14 +00:00
|
|
|
return 2
|
2023-03-31 05:36:12 +00:00
|
|
|
}
|
2023-03-30 16:48:58 +00:00
|
|
|
|
2024-05-18 00:44:18 +00:00
|
|
|
return 1
|
|
|
|
}
|
2023-03-30 16:48:58 +00:00
|
|
|
|
2024-05-20 12:37:46 +00:00
|
|
|
func readCgroup2StringToInt64Tuple(cgroupString string) (quota, period int64) {
|
2023-03-30 16:48:58 +00:00
|
|
|
// file contents looks like: $MAX $PERIOD
|
|
|
|
// $MAX can have value "max" indicating no limit
|
2023-03-31 05:36:12 +00:00
|
|
|
// it is possible for $PERIOD to be unset
|
2023-03-30 16:48:58 +00:00
|
|
|
|
2024-05-20 12:37:46 +00:00
|
|
|
values := strings.Fields(cgroupString)
|
2023-03-30 16:48:58 +00:00
|
|
|
|
|
|
|
if values[0] == "max" {
|
2023-03-31 05:52:14 +00:00
|
|
|
return -1, -1
|
2023-03-30 16:48:58 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 05:52:14 +00:00
|
|
|
cpuQuota, err := strconv.ParseInt(values[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return -1, -1
|
2023-03-30 16:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(values) == 1 {
|
2023-07-16 17:18:32 +00:00
|
|
|
return cpuQuota, 100000
|
2023-03-30 16:48:58 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 05:52:14 +00:00
|
|
|
cpuPeriod, err := strconv.ParseInt(values[1], 10, 64)
|
2023-03-30 16:48:58 +00:00
|
|
|
if err != nil {
|
2023-03-31 05:52:14 +00:00
|
|
|
return -1, -1
|
2023-03-30 16:48:58 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 05:52:14 +00:00
|
|
|
return cpuQuota, cpuPeriod
|
2023-03-30 16:48:58 +00:00
|
|
|
}
|
|
|
|
|
2024-05-20 12:37:46 +00:00
|
|
|
func readCgroup2FileToInt64Tuple(cgroupPath, cgroupFile string) (quota, period int64) {
|
2021-08-06 14:18:17 +00:00
|
|
|
contents, err := os.ReadFile(filepath.Join(cgroupPath, cgroupFile))
|
2018-08-25 21:08:12 +00:00
|
|
|
if err != nil {
|
2024-05-20 12:37:46 +00:00
|
|
|
return -1, -1
|
2018-08-25 21:08:12 +00:00
|
|
|
}
|
|
|
|
|
2024-05-20 12:37:46 +00:00
|
|
|
return readCgroup2StringToInt64Tuple(string(contents))
|
|
|
|
}
|
|
|
|
|
|
|
|
func readCgroupStringToInt64(contents string) int64 {
|
|
|
|
strValue := strings.TrimSpace(contents)
|
2018-08-25 21:08:12 +00:00
|
|
|
if value, err := strconv.ParseInt(strValue, 10, 64); err == nil {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1
|
|
|
|
}
|
2024-05-20 12:37:46 +00:00
|
|
|
|
|
|
|
func readCgroupFileToInt64(cgroupPath, cgroupFile string) int64 {
|
|
|
|
contents, err := os.ReadFile(filepath.Join(cgroupPath, cgroupFile))
|
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return readCgroupStringToInt64(string(contents))
|
|
|
|
}
|