2014-06-10 13:38:01 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package docker
|
2014-06-10 13:38:01 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-11-27 09:25:14 +08:00
|
|
|
|
|
|
|
common "github.com/shirou/gopsutil/common"
|
2014-11-27 09:32:35 +08:00
|
|
|
cpu "github.com/shirou/gopsutil/cpu"
|
2014-06-10 13:38:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type CgroupMemStat struct {
|
2014-12-27 22:42:00 +08:00
|
|
|
ContainerID string `json:"container_id"`
|
2014-08-16 02:05:26 +08:00
|
|
|
Cache uint64 `json:"cache"`
|
|
|
|
RSS uint64 `json:"rss"`
|
|
|
|
RSSHuge uint64 `json:"rss_huge"`
|
|
|
|
MappedFile uint64 `json:"mapped_file"`
|
|
|
|
Pgpgin uint64 `json:"pgpgin"`
|
|
|
|
Pgpgout uint64 `json:"pgpgout"`
|
|
|
|
Pgfault uint64 `json:"pgfault"`
|
|
|
|
Pgmajfault uint64 `json:"pgmajfault"`
|
|
|
|
InactiveAnon uint64 `json:"inactive_anon"`
|
|
|
|
ActiveAnon uint64 `json:"active_anon"`
|
|
|
|
InctiveFile uint64 `json:"inactive_file"`
|
|
|
|
ActiveFile uint64 `json:"active_file"`
|
|
|
|
Unevictable uint64 `json:"unevictable"`
|
|
|
|
HierarchicalMemoryLimit uint64 `json:"hierarchical_memory_limit"`
|
|
|
|
TotalCache uint64 `json:"total_cache"`
|
|
|
|
TotalRSS uint64 `json:"total_rss"`
|
|
|
|
TotalRSSHuge uint64 `json:"total_rss_huge"`
|
|
|
|
TotalMappedFile uint64 `json:"total_mapped_file"`
|
|
|
|
TotalPgpgIn uint64 `json:"total_pgpgin"`
|
|
|
|
TotalPgpgOut uint64 `json:"total_pgpgout"`
|
|
|
|
TotalPgFault uint64 `json:"total_pgfault"`
|
|
|
|
TotalPgMajFault uint64 `json:"total_pgmajfault"`
|
|
|
|
TotalInactiveAnon uint64 `json:"total_inactive_anon"`
|
|
|
|
TotalActiveAnon uint64 `json:"total_active_anon"`
|
|
|
|
TotalInactiveFile uint64 `json:"total_inactive_file"`
|
|
|
|
TotalActiveFile uint64 `json:"total_active_file"`
|
|
|
|
TotalUnevictable uint64 `json:"total_unevictable"`
|
2014-06-10 13:38:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDockerIDList returnes a list of DockerID.
|
|
|
|
// This requires certain permission.
|
|
|
|
func GetDockerIDList() ([]string, error) {
|
|
|
|
out, err := exec.Command("docker", "ps", "-q", "--no-trunc").Output()
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
lines := strings.Split(string(out), "\n")
|
|
|
|
ret := make([]string, 0, len(lines))
|
|
|
|
|
|
|
|
for _, l := range lines {
|
|
|
|
ret = append(ret, l)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CgroupCPU returnes specified cgroup id CPU status.
|
|
|
|
// 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/
|
2014-11-27 09:32:35 +08:00
|
|
|
func CgroupCPU(containerid string, base string) (*cpu.CPUTimesStat, error) {
|
2014-06-10 13:38:01 +08:00
|
|
|
if len(base) == 0 {
|
|
|
|
base = "/sys/fs/cgroup/cpuacct/docker"
|
|
|
|
}
|
|
|
|
path := path.Join(base, containerid, "cpuacct.stat")
|
|
|
|
|
2015-02-11 02:36:19 +08:00
|
|
|
lines, err := common.ReadLines(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-10 13:38:01 +08:00
|
|
|
// empty containerid means all cgroup
|
|
|
|
if len(containerid) == 0 {
|
|
|
|
containerid = "all"
|
|
|
|
}
|
2014-11-27 09:32:35 +08:00
|
|
|
ret := &cpu.CPUTimesStat{CPU: containerid}
|
2014-06-10 13:38:01 +08:00
|
|
|
for _, line := range lines {
|
|
|
|
fields := strings.Split(line, " ")
|
|
|
|
if fields[0] == "user" {
|
2015-02-13 22:14:36 +08:00
|
|
|
user, err := strconv.ParseFloat(fields[1], 64)
|
2014-06-10 13:38:01 +08:00
|
|
|
if err == nil {
|
2015-02-13 22:14:36 +08:00
|
|
|
ret.User = float64(user)
|
2014-06-10 13:38:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if fields[0] == "system" {
|
2015-02-13 22:14:36 +08:00
|
|
|
system, err := strconv.ParseFloat(fields[1], 64)
|
2014-06-10 13:38:01 +08:00
|
|
|
if err == nil {
|
2015-02-13 22:14:36 +08:00
|
|
|
ret.System = float64(system)
|
2014-06-10 13:38:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2014-11-27 09:32:35 +08:00
|
|
|
func CgroupCPUDocker(containerid string) (*cpu.CPUTimesStat, error) {
|
2014-06-10 13:38:01 +08:00
|
|
|
return CgroupCPU(containerid, "/sys/fs/cgroup/cpuacct/docker")
|
|
|
|
}
|
|
|
|
|
|
|
|
func CgroupMem(containerid string, base string) (*CgroupMemStat, error) {
|
|
|
|
if len(base) == 0 {
|
|
|
|
base = "/sys/fs/cgroup/memory/docker"
|
|
|
|
}
|
|
|
|
path := path.Join(base, containerid, "memory.stat")
|
|
|
|
// empty containerid means all cgroup
|
|
|
|
if len(containerid) == 0 {
|
|
|
|
containerid = "all"
|
|
|
|
}
|
Don't ignore err when getting CgroupMemDocker stats
Fixes panic: runtime error: index out of range
goroutine 10 [running]:
testing.func·006()
/usr/local/go/src/testing/testing.go:441 +0x181
github.com/shirou/gopsutil/docker.CgroupMem(0x586b30, 0x6, 0x5a87d0, 0x1c, 0x0, 0x0, 0x0)
/home/jwilder/go/src/github.com/shirou/gopsutil/docker/docker_linux.go:119 +0xf48
github.com/shirou/gopsutil/docker.CgroupMemDocker(0x586b30, 0x6, 0x0, 0x0, 0x0)
/home/jwilder/go/src/github.com/shirou/gopsutil/docker/docker_linux.go:184 +0x57
If the ID passed to the CGroupMemDocker does not exist, you can get
a panic at runtime. This can happen when a container exits before
calling the func.
2015-02-11 02:20:45 +08:00
|
|
|
lines, err := common.ReadLines(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-10 13:38:01 +08:00
|
|
|
ret := &CgroupMemStat{ContainerID: containerid}
|
|
|
|
for _, line := range lines {
|
|
|
|
fields := strings.Split(line, " ")
|
|
|
|
v, err := strconv.ParseUint(fields[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch fields[0] {
|
|
|
|
case "cache":
|
|
|
|
ret.Cache = v
|
|
|
|
case "rss":
|
|
|
|
ret.RSS = v
|
|
|
|
case "rss_huge":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.RSSHuge = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "mapped_file":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.MappedFile = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "pgpgin":
|
|
|
|
ret.Pgpgin = v
|
|
|
|
case "pgpgout":
|
|
|
|
ret.Pgpgout = v
|
|
|
|
case "pgfault":
|
|
|
|
ret.Pgfault = v
|
|
|
|
case "pgmajfault":
|
|
|
|
ret.Pgmajfault = v
|
|
|
|
case "inactive_anon":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.InactiveAnon = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "active_anon":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.ActiveAnon = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "inactive_file":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.InctiveFile = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "active_file":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.ActiveFile = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "unevictable":
|
|
|
|
ret.Unevictable = v
|
|
|
|
case "hierarchical_memory_limit":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.HierarchicalMemoryLimit = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "total_cache":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.TotalCache = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "total_rss":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.TotalRSS = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "total_rss_huge":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.TotalRSSHuge = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "total_mapped_file":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.TotalMappedFile = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "total_pgpgin":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.TotalPgpgIn = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "total_pgpgout":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.TotalPgpgOut = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "total_pgfault":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.TotalPgFault = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "total_pgmajfault":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.TotalPgMajFault = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "total_inactive_anon":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.TotalInactiveAnon = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "total_active_anon":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.TotalActiveAnon = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "total_inactive_file":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.TotalInactiveFile = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "total_active_file":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.TotalActiveFile = v
|
2014-06-10 13:38:01 +08:00
|
|
|
case "total_unevictable":
|
2014-08-16 02:05:26 +08:00
|
|
|
ret.TotalUnevictable = v
|
2014-06-10 13:38:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CgroupMemDocker(containerid string) (*CgroupMemStat, error) {
|
|
|
|
return CgroupMem(containerid, "/sys/fs/cgroup/memory/docker")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m CgroupMemStat) String() string {
|
|
|
|
s, _ := json.Marshal(m)
|
|
|
|
return string(s)
|
|
|
|
}
|