2019-01-12 04:58:27 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2020-08-10 17:43:49 +00:00
|
|
|
"bufio"
|
2019-01-12 04:58:27 +00:00
|
|
|
"fmt"
|
2020-08-10 17:43:49 +00:00
|
|
|
"os"
|
2019-01-12 04:58:27 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
2020-05-04 20:46:48 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
2019-01-12 04:58:27 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-08-10 17:43:49 +00:00
|
|
|
cgroupCpuacctStat = "cpuacct.stat"
|
|
|
|
cgroupCpuacctUsageAll = "cpuacct.usage_all"
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
nanosecondsInSecond = 1000000000
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
userModeColumn = 1
|
|
|
|
kernelModeColumn = 2
|
|
|
|
cuacctUsageAllColumnsNumber = 3
|
|
|
|
|
|
|
|
// The value comes from `C.sysconf(C._SC_CLK_TCK)`, and
|
|
|
|
// on Linux it's a constant which is safe to be hard coded,
|
|
|
|
// so we can avoid using cgo here. For details, see:
|
|
|
|
// https://github.com/containerd/cgroups/pull/12
|
|
|
|
clockTicks uint64 = 100
|
|
|
|
)
|
2019-01-12 04:58:27 +00:00
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
type CpuacctGroup struct{}
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
func (s *CpuacctGroup) Name() string {
|
|
|
|
return "cpuacct"
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func (s *CpuacctGroup) Apply(path string, d *cgroupData) error {
|
|
|
|
return join(path, d.pid)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 20:46:09 +00:00
|
|
|
func (s *CpuacctGroup) Set(_ string, _ *configs.Resources) error {
|
2019-01-12 04:58:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CpuacctGroup) GetStats(path string, stats *cgroups.Stats) error {
|
2020-08-10 17:43:49 +00:00
|
|
|
if !cgroups.PathExists(path) {
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
userModeUsage, kernelModeUsage, err := getCpuUsageBreakdown(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-04 20:46:48 +00:00
|
|
|
totalUsage, err := fscommon.GetCgroupParamUint(path, "cpuacct.usage")
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
percpuUsage, err := getPercpuUsage(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
percpuUsageInKernelmode, percpuUsageInUsermode, err := getPercpuUsageInModes(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
stats.CpuStats.CpuUsage.TotalUsage = totalUsage
|
|
|
|
stats.CpuStats.CpuUsage.PercpuUsage = percpuUsage
|
2020-08-10 17:43:49 +00:00
|
|
|
stats.CpuStats.CpuUsage.PercpuUsageInKernelmode = percpuUsageInKernelmode
|
|
|
|
stats.CpuStats.CpuUsage.PercpuUsageInUsermode = percpuUsageInUsermode
|
2019-01-12 04:58:27 +00:00
|
|
|
stats.CpuStats.CpuUsage.UsageInUsermode = userModeUsage
|
|
|
|
stats.CpuStats.CpuUsage.UsageInKernelmode = kernelModeUsage
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns user and kernel usage breakdown in nanoseconds.
|
|
|
|
func getCpuUsageBreakdown(path string) (uint64, uint64, error) {
|
2021-04-14 18:11:13 +00:00
|
|
|
var userModeUsage, kernelModeUsage uint64
|
2019-01-12 04:58:27 +00:00
|
|
|
const (
|
|
|
|
userField = "user"
|
|
|
|
systemField = "system"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Expected format:
|
|
|
|
// user <usage in ticks>
|
|
|
|
// system <usage in ticks>
|
2021-07-02 08:43:15 +00:00
|
|
|
data, err := cgroups.ReadFile(path, cgroupCpuacctStat)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
2021-04-14 18:11:13 +00:00
|
|
|
fields := strings.Fields(data)
|
2020-05-04 20:46:48 +00:00
|
|
|
if len(fields) < 4 {
|
|
|
|
return 0, 0, fmt.Errorf("failure - %s is expected to have at least 4 fields", filepath.Join(path, cgroupCpuacctStat))
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
if fields[0] != userField {
|
|
|
|
return 0, 0, fmt.Errorf("unexpected field %q in %q, expected %q", fields[0], cgroupCpuacctStat, userField)
|
|
|
|
}
|
|
|
|
if fields[2] != systemField {
|
|
|
|
return 0, 0, fmt.Errorf("unexpected field %q in %q, expected %q", fields[2], cgroupCpuacctStat, systemField)
|
|
|
|
}
|
|
|
|
if userModeUsage, err = strconv.ParseUint(fields[1], 10, 64); err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
if kernelModeUsage, err = strconv.ParseUint(fields[3], 10, 64); err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return (userModeUsage * nanosecondsInSecond) / clockTicks, (kernelModeUsage * nanosecondsInSecond) / clockTicks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPercpuUsage(path string) ([]uint64, error) {
|
|
|
|
percpuUsage := []uint64{}
|
2021-07-02 08:43:15 +00:00
|
|
|
data, err := cgroups.ReadFile(path, "cpuacct.usage_percpu")
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return percpuUsage, err
|
|
|
|
}
|
2021-04-14 18:11:13 +00:00
|
|
|
for _, value := range strings.Fields(data) {
|
2019-01-12 04:58:27 +00:00
|
|
|
value, err := strconv.ParseUint(value, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return percpuUsage, fmt.Errorf("Unable to convert param value to uint64: %s", err)
|
|
|
|
}
|
|
|
|
percpuUsage = append(percpuUsage, value)
|
|
|
|
}
|
|
|
|
return percpuUsage, nil
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
|
|
|
|
func getPercpuUsageInModes(path string) ([]uint64, []uint64, error) {
|
|
|
|
usageKernelMode := []uint64{}
|
|
|
|
usageUserMode := []uint64{}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
file, err := cgroups.OpenFile(path, cgroupCpuacctUsageAll, os.O_RDONLY)
|
2020-08-10 17:43:49 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return usageKernelMode, usageUserMode, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
2021-07-02 08:43:15 +00:00
|
|
|
scanner.Scan() // skipping header line
|
2020-08-10 17:43:49 +00:00
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
lineFields := strings.SplitN(scanner.Text(), " ", cuacctUsageAllColumnsNumber+1)
|
|
|
|
if len(lineFields) != cuacctUsageAllColumnsNumber {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
usageInKernelMode, err := strconv.ParseUint(lineFields[kernelModeColumn], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Unable to convert CPU usage in kernel mode to uint64: %s", err)
|
|
|
|
}
|
|
|
|
usageKernelMode = append(usageKernelMode, usageInKernelMode)
|
|
|
|
|
|
|
|
usageInUserMode, err := strconv.ParseUint(lineFields[userModeColumn], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Unable to convert CPU usage in user mode to uint64: %s", err)
|
|
|
|
}
|
|
|
|
usageUserMode = append(usageUserMode, usageInUserMode)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Problem in reading %s line by line, %s", cgroupCpuacctUsageAll, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return usageKernelMode, usageUserMode, nil
|
|
|
|
}
|