[v3 migration] change return type to CgroupCPUStat

This commit is contained in:
shirou 2020-10-26 23:17:17 +09:00
parent 6920de8509
commit 4b5a200e71
2 changed files with 16 additions and 10 deletions

View File

@ -89,7 +89,7 @@ func GetDockerIDListWithContext(ctx context.Context) ([]string, error) {
// containerID is same as docker id if you use docker.
// If you use container via systemd.slice, you could use
// containerID = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
func CgroupCPU(containerID string, base string) (*cpu.TimesStat, error) {
func CgroupCPU(containerID string, base string) (*CgroupCPUStat, error) {
return CgroupCPUWithContext(context.Background(), containerID, base)
}
@ -101,7 +101,7 @@ func CgroupCPUUsage(containerID string, base string) (float64, error) {
return CgroupCPUUsageWithContext(context.Background(), containerID, base)
}
func CgroupCPUWithContext(ctx context.Context, containerID string, base string) (*cpu.TimesStat, error) {
func CgroupCPUWithContext(ctx context.Context, containerID string, base string) (*CgroupCPUStat, error) {
statfile := getCgroupFilePath(containerID, base, "cpuacct", "cpuacct.stat")
lines, err := common.ReadLines(statfile)
if err != nil {
@ -111,7 +111,9 @@ func CgroupCPUWithContext(ctx context.Context, containerID string, base string)
if len(containerID) == 0 {
containerID = "all"
}
ret := &cpu.TimesStat{CPU: containerID}
ret := &CgroupCPUStat{}
ret.CPU = containerID
for _, line := range lines {
fields := strings.Split(line, " ")
if fields[0] == "user" {
@ -127,6 +129,11 @@ func CgroupCPUWithContext(ctx context.Context, containerID string, base string)
}
}
}
usage, err := CgroupCPUUsageWithContext(ctx, containerID, base)
if err != nil {
return nil, err
}
ret.Usage = usage
return ret, nil
}
@ -145,7 +152,7 @@ func CgroupCPUUsageWithContext(ctx context.Context, containerID, base string) (f
return ns / nanoseconds, nil
}
func CgroupCPUDocker(containerid string) (*cpu.TimesStat, error) {
func CgroupCPUDocker(containerid string) (*CgroupCPUStat, error) {
return CgroupCPUDockerWithContext(context.Background(), containerid)
}
@ -153,7 +160,7 @@ func CgroupCPUUsageDocker(containerid string) (float64, error) {
return CgroupCPUDockerUsageWithContext(context.Background(), containerid)
}
func CgroupCPUDockerWithContext(ctx context.Context, containerid string) (*cpu.TimesStat, error) {
func CgroupCPUDockerWithContext(ctx context.Context, containerid string) (*CgroupCPUStat, error) {
return CgroupCPU(containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
}

View File

@ -5,7 +5,6 @@ package docker
import (
"context"
cpu "github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/internal/common"
)
@ -33,19 +32,19 @@ func GetDockerIDListWithContext(ctx context.Context) ([]string, error) {
// containerid is same as docker id if you use docker.
// If you use container via systemd.slice, you could use
// containerid = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
func CgroupCPU(containerid string, base string) (*cpu.TimesStat, error) {
func CgroupCPU(containerid string, base string) (*CgroupCPUStat, error) {
return CgroupCPUWithContext(context.Background(), containerid, base)
}
func CgroupCPUWithContext(ctx context.Context, containerid string, base string) (*cpu.TimesStat, error) {
func CgroupCPUWithContext(ctx context.Context, containerid string, base string) (*CgroupCPUStat, error) {
return nil, ErrCgroupNotAvailable
}
func CgroupCPUDocker(containerid string) (*cpu.TimesStat, error) {
func CgroupCPUDocker(containerid string) (*CgroupCPUStat, error) {
return CgroupCPUDockerWithContext(context.Background(), containerid)
}
func CgroupCPUDockerWithContext(ctx context.Context, containerid string) (*cpu.TimesStat, error) {
func CgroupCPUDockerWithContext(ctx context.Context, containerid string) (*CgroupCPUStat, error) {
return CgroupCPU(containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
}