Files
tidb/util/cgroup/cgroup_cpu_linux.go
2022-12-07 17:48:05 +08:00

60 lines
1.6 KiB
Go

// Copyright 2022 PingCAP, Inc.
//
// 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.
//go:build linux
package cgroup
import (
"math"
"os"
"runtime"
"strings"
)
// GetCgroupCPU returns the CPU usage and quota for the current cgroup.
func GetCgroupCPU() (CPUUsage, error) {
cpuusage, err := getCgroupCPU("/")
cpuusage.NumCPU = runtime.NumCPU()
return cpuusage, err
}
// CPUQuotaToGOMAXPROCS converts the CPU quota applied to the calling process
// to a valid GOMAXPROCS value.
func CPUQuotaToGOMAXPROCS(minValue int) (int, CPUQuotaStatus, error) {
quota, err := GetCgroupCPU()
if err != nil {
return -1, CPUQuotaUndefined, err
}
maxProcs := int(math.Ceil(quota.CPUShares()))
if minValue > 0 && maxProcs < minValue {
return minValue, CPUQuotaMinUsed, nil
}
return maxProcs, CPUQuotaUsed, nil
}
// InContainer returns true if the process is running in a container.
func InContainer() bool {
v, err := os.ReadFile(procPathCGroup)
if err != nil {
return false
}
if strings.Contains(string(v), "docker") ||
strings.Contains(string(v), "kubepods") ||
strings.Contains(string(v), "containerd") {
return true
}
return false
}