|
|
@ -89,11 +89,31 @@ func GetDockerIDListWithContext(ctx context.Context) ([]string, error) {
|
|
|
|
// containerID is same as docker id if you use docker.
|
|
|
|
// containerID is same as docker id if you use docker.
|
|
|
|
// If you use container via systemd.slice, you could use
|
|
|
|
// If you use container via systemd.slice, you could use
|
|
|
|
// containerID = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
|
|
|
|
// 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)
|
|
|
|
return CgroupCPUWithContext(context.Background(), containerID, base)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func CgroupCPUWithContext(ctx context.Context, containerID string, base string) (*cpu.TimesStat, error) {
|
|
|
|
func CgroupCPUUsage(containerID string, base string) (float64, error) {
|
|
|
|
|
|
|
|
return CgroupCPUUsageWithContext(context.Background(), containerID, base)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func CgroupCPUUsageWithContext(ctx context.Context, containerID, base string) (float64, error) {
|
|
|
|
|
|
|
|
usagefile := getCgroupFilePath(containerID, base, "cpuacct", "cpuacct.usage")
|
|
|
|
|
|
|
|
lines, err := common.ReadLinesOffsetN(usagefile, 0, 1)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return 0.0, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ns, err := strconv.ParseFloat(lines[0], 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return 0.0, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ns / 1e9, nil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func CgroupCPUWithContext(ctx context.Context, containerID string, base string) (*CgroupCPUStat, error) {
|
|
|
|
statfile := getCgroupFilePath(containerID, base, "cpuacct", "cpuacct.stat")
|
|
|
|
statfile := getCgroupFilePath(containerID, base, "cpuacct", "cpuacct.stat")
|
|
|
|
lines, err := common.ReadLines(statfile)
|
|
|
|
lines, err := common.ReadLines(statfile)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -103,31 +123,40 @@ func CgroupCPUWithContext(ctx context.Context, containerID string, base string)
|
|
|
|
if len(containerID) == 0 {
|
|
|
|
if len(containerID) == 0 {
|
|
|
|
containerID = "all"
|
|
|
|
containerID = "all"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret := &cpu.TimesStat{CPU: containerID}
|
|
|
|
ret := &CgroupCPUStat{}
|
|
|
|
|
|
|
|
ret.CPU = containerID
|
|
|
|
for _, line := range lines {
|
|
|
|
for _, line := range lines {
|
|
|
|
fields := strings.Split(line, " ")
|
|
|
|
fields := strings.Split(line, " ")
|
|
|
|
if fields[0] == "user" {
|
|
|
|
if fields[0] == "user" {
|
|
|
|
user, err := strconv.ParseFloat(fields[1], 64)
|
|
|
|
user, err := strconv.ParseFloat(fields[1], 64)
|
|
|
|
if err == nil {
|
|
|
|
if err == nil {
|
|
|
|
ret.User = float64(user)
|
|
|
|
ret.User = user / cpu.CPUTick
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if fields[0] == "system" {
|
|
|
|
if fields[0] == "system" {
|
|
|
|
system, err := strconv.ParseFloat(fields[1], 64)
|
|
|
|
system, err := strconv.ParseFloat(fields[1], 64)
|
|
|
|
if err == nil {
|
|
|
|
if err == nil {
|
|
|
|
ret.System = float64(system)
|
|
|
|
ret.System = system / cpu.CPUTick
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
usage, err := CgroupCPUUsageWithContext(ctx, containerID, base)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.Usage = usage
|
|
|
|
return ret, nil
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func CgroupCPUDocker(containerid string) (*cpu.TimesStat, error) {
|
|
|
|
func CgroupCPUDocker(containerid string) (*CgroupCPUStat, error) {
|
|
|
|
return CgroupCPUDockerWithContext(context.Background(), containerid)
|
|
|
|
return CgroupCPUDockerWithContext(context.Background(), containerid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func CgroupCPUDockerWithContext(ctx context.Context, containerid string) (*cpu.TimesStat, error) {
|
|
|
|
func CgroupCPUDockerUsageWithContext(ctx context.Context, containerid string) (float64, error) {
|
|
|
|
|
|
|
|
return CgroupCPUUsage(containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func CgroupCPUDockerWithContext(ctx context.Context, containerid string) (*CgroupCPUStat, error) {
|
|
|
|
return CgroupCPU(containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
|
|
|
|
return CgroupCPU(containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|