forked from shirou/gopsutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_linux.go
184 lines (171 loc) · 5.17 KB
/
docker_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// +build linux
package gopsutil
import (
"encoding/json"
"os/exec"
"path"
"strconv"
"strings"
)
type CgroupMemStat struct {
ContainerID string `json:"containerid"`
Cache uint64 `json:"cache"`
RSS uint64 `json:"rss"`
Rss_huge uint64 `json:"rss_huge"`
Mapped_file uint64 `json:"mapped_file"`
Pgpgin uint64 `json:"pgpgin"`
Pgpgout uint64 `json:"pgpgout"`
Pgfault uint64 `json:"pgfault"`
Pgmajfault uint64 `json:"pgmajfault"`
Inactive_anon uint64 `json:"inactive_anon"`
Active_anon uint64 `json:"active_anon"`
Inactive_file uint64 `json:"inactive_file"`
Active_file uint64 `json:"active_file"`
Unevictable uint64 `json:"unevictable"`
Hierarchical_memory_limit uint64 `json:"hierarchical_memory_limit"`
Total_cache uint64 `json:"total_cache"`
Total_rss uint64 `json:"total_rss"`
Total_rss_huge uint64 `json:"total_rss_huge"`
Total_mapped_file uint64 `json:"total_mapped_file"`
Total_pgpgin uint64 `json:"total_pgpgin"`
Total_pgpgout uint64 `json:"total_pgpgout"`
Total_pgfault uint64 `json:"total_pgfault"`
Total_pgmajfault uint64 `json:"total_pgmajfault"`
Total_inactive_anon uint64 `json:"total_inactive_anon"`
Total_active_anon uint64 `json:"total_active_anon"`
Total_inactive_file uint64 `json:"total_inactive_file"`
Total_active_file uint64 `json:"total_active_file"`
Total_unevictable uint64 `json:"total_unevictable"`
}
// 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/
func CgroupCPU(containerid string, base string) (*CPUTimesStat, error) {
if len(base) == 0 {
base = "/sys/fs/cgroup/cpuacct/docker"
}
path := path.Join(base, containerid, "cpuacct.stat")
lines, _ := readLines(path)
// empty containerid means all cgroup
if len(containerid) == 0 {
containerid = "all"
}
ret := &CPUTimesStat{CPU: containerid}
for _, line := range lines {
fields := strings.Split(line, " ")
if fields[0] == "user" {
user, err := strconv.ParseFloat(fields[1], 32)
if err == nil {
ret.User = float32(user)
}
}
if fields[0] == "system" {
system, err := strconv.ParseFloat(fields[1], 32)
if err == nil {
ret.System = float32(system)
}
}
}
return ret, nil
}
func CgroupCPUDocker(containerid string) (*CPUTimesStat, error) {
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"
}
lines, _ := readLines(path)
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":
ret.Rss_huge = v
case "mapped_file":
ret.Mapped_file = v
case "pgpgin":
ret.Pgpgin = v
case "pgpgout":
ret.Pgpgout = v
case "pgfault":
ret.Pgfault = v
case "pgmajfault":
ret.Pgmajfault = v
case "inactive_anon":
ret.Inactive_anon = v
case "active_anon":
ret.Active_anon = v
case "inactive_file":
ret.Inactive_file = v
case "active_file":
ret.Active_file = v
case "unevictable":
ret.Unevictable = v
case "hierarchical_memory_limit":
ret.Hierarchical_memory_limit = v
case "total_cache":
ret.Total_cache = v
case "total_rss":
ret.Total_rss = v
case "total_rss_huge":
ret.Total_rss_huge = v
case "total_mapped_file":
ret.Total_mapped_file = v
case "total_pgpgin":
ret.Total_pgpgin = v
case "total_pgpgout":
ret.Total_pgpgout = v
case "total_pgfault":
ret.Total_pgfault = v
case "total_pgmajfault":
ret.Total_pgmajfault = v
case "total_inactive_anon":
ret.Total_inactive_anon = v
case "total_active_anon":
ret.Total_active_anon = v
case "total_inactive_file":
ret.Total_inactive_file = v
case "total_active_file":
ret.Total_active_file = v
case "total_unevictable":
ret.Total_unevictable = v
}
}
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)
}