-
Notifications
You must be signed in to change notification settings - Fork 950
/
Copy pathstats_helpers.go
293 lines (258 loc) · 7.19 KB
/
stats_helpers.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"strings"
"sync"
"time"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/client"
"github.com/alibaba/pouch/pkg/log"
"github.com/docker/go-units"
"github.com/pkg/errors"
)
// StatsEntry represents the statistics data collected from a container
type StatsEntry struct {
container string
name string
id string
cpuPercentage float64
memory float64
memoryLimit float64
memoryPercentage float64
networkRx float64
networkTx float64
blockRead float64
blockWrite float64
pidsCurrent uint64
err error
}
// StatsEntryWithLock represents an entity to store containers statistics synchronously
type StatsEntryWithLock struct {
mutex sync.Mutex
StatsEntry
}
// Name return the name of container
func (s StatsEntry) Name() string {
return s.name
}
// ID return the id of container
func (s StatsEntry) ID() string {
var shortLen = 12
if len(s.id) > shortLen {
return s.id[:shortLen]
}
return s.id
}
// CPUPerc return the cpu usage percentage
func (s StatsEntry) CPUPerc() string {
if s.err != nil {
return fmt.Sprintf("--")
}
return fmt.Sprintf("%.2f%%", s.cpuPercentage)
}
// MemUsage return memory usage
func (s StatsEntry) MemUsage() string {
if s.err != nil {
return fmt.Sprintf("-- / --")
}
return fmt.Sprintf("%s / %s", units.BytesSize(s.memory), units.BytesSize(s.memoryLimit))
}
// MemPerc return memory percentage
func (s StatsEntry) MemPerc() string {
if s.err != nil {
return fmt.Sprintf("--")
}
return fmt.Sprintf("%.2f%%", s.memoryPercentage)
}
// NetIO return net IO usage of container
func (s StatsEntry) NetIO() string {
if s.err != nil {
return fmt.Sprintf("--")
}
return fmt.Sprintf("%s / %s", units.HumanSizeWithPrecision(s.networkRx, 3), units.HumanSizeWithPrecision(s.networkTx, 3))
}
// BlockIO return block IO usage of container
func (s StatsEntry) BlockIO() string {
if s.err != nil {
return fmt.Sprintf("--")
}
return fmt.Sprintf("%s / %s", units.HumanSizeWithPrecision(s.blockRead, 3), units.HumanSizeWithPrecision(s.blockWrite, 3))
}
// PIDs return current pid of container
func (s StatsEntry) PIDs() string {
if s.err != nil {
return fmt.Sprintf("--")
}
return fmt.Sprintf("%d", s.pidsCurrent)
}
// GetStatsEntry return the StatsEntry of StatsEntryWithLock
func (s *StatsEntryWithLock) GetStatsEntry() StatsEntry {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.StatsEntry
}
// SetError will set error of StatsEntryWithLock
func (s *StatsEntryWithLock) SetError(err error) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.err = err
}
// GetError will return the error string of StatsEntryWithLock
func (s *StatsEntryWithLock) GetError() error {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.err != nil {
return fmt.Errorf("failed to stats container %s, err = %v", s.id, s.err.Error())
}
return nil
}
func collect(ctx context.Context, s *StatsEntryWithLock, cli client.CommonAPIClient, streamStats bool, waitFirst *sync.WaitGroup) {
log.With(nil).Debugf("collecting stats for %s", s.container)
var (
getFirst bool
previousCPU uint64
previousSystem uint64
errCh = make(chan error, 1)
dataCh = make(chan struct{}, 1)
)
defer func() {
// double check
if !getFirst {
getFirst = true
waitFirst.Done()
}
}()
response, err := cli.ContainerStats(ctx, s.container, streamStats)
if err != nil {
s.SetError(err)
return
}
defer response.Close()
dec := json.NewDecoder(response)
go func() {
for {
var (
v types.ContainerStats
memPercent, cpuPercent float64
blkRead, blkWrite uint64
mem, memLimit float64
pidsStatsCurrent uint64
)
if err := dec.Decode(&v); err != nil {
dec = json.NewDecoder(io.MultiReader(dec.Buffered(), response))
errCh <- err
break
}
// first time PrecpuStats will be nil
if v.PrecpuStats != nil && v.PrecpuStats.CPUUsage != nil {
previousCPU = v.PrecpuStats.CPUUsage.TotalUsage
previousSystem = v.PrecpuStats.SyetemCPUUsage
}
cpuPercent = calculateCPUPercentUnix(previousCPU, previousSystem, v.CPUStats)
blkRead, blkWrite = calculateBlockIO(v.BlkioStats)
mem = calculateMemUsageUnixNoCache(v.MemoryStats)
memLimit = float64(v.MemoryStats.Limit)
memPercent = calculateMemPercentUnixNoCache(memLimit, mem)
pidsStatsCurrent = v.PidsStats.Current
netRx, netTx := calculateNetwork(v.Networks)
s.mutex.Lock()
s.name = v.Name
s.id = v.ID
s.cpuPercentage = cpuPercent
s.memory = mem
s.memoryLimit = memLimit
s.memoryPercentage = memPercent
s.networkRx = netRx
s.networkTx = netTx
s.blockRead = float64(blkRead)
s.blockWrite = float64(blkWrite)
s.pidsCurrent = pidsStatsCurrent
s.mutex.Unlock()
dataCh <- struct{}{}
if !streamStats {
return
}
}
}()
for {
select {
case <-time.After(2 * time.Second):
// zero out the values if we have not received an update within
// the specified duration.
s.SetError(errors.New("timeout waiting for stats"))
//FIXME(ZYecho): should retry when timeout?
return
case err := <-errCh:
s.SetError(err)
return
case <-dataCh:
// if this is the first stat you get, release WaitGroup
if !getFirst {
getFirst = true
waitFirst.Done()
}
if !streamStats {
return
}
}
}
}
func calculateCPUPercentUnix(previousCPU, previousSystem uint64, cpuStats *types.CPUStats) float64 {
if cpuStats == nil || cpuStats.CPUUsage == nil {
return 0.0
}
var (
cpuPercent = 0.0
// calculate the change for the cpu usage of the container in between readings
cpuDelta = float64(cpuStats.CPUUsage.TotalUsage) - float64(previousCPU)
// calculate the change for the entire system between readings
systemDelta = float64(cpuStats.SyetemCPUUsage) - float64(previousSystem)
onlineCPUs = float64(cpuStats.OnlineCpus)
)
if onlineCPUs == 0.0 {
onlineCPUs = float64(len(cpuStats.CPUUsage.PercpuUsage))
}
if systemDelta > 0.0 && cpuDelta > 0.0 {
cpuPercent = (cpuDelta / systemDelta) * onlineCPUs * 100.0
}
return cpuPercent
}
func calculateBlockIO(blkio *types.BlkioStats) (uint64, uint64) {
var blkRead, blkWrite uint64
for _, bioEntry := range blkio.IoServiceBytesRecursive {
switch strings.ToLower(bioEntry.Op) {
case "read":
blkRead = blkRead + bioEntry.Value
case "write":
blkWrite = blkWrite + bioEntry.Value
}
}
return blkRead, blkWrite
}
func calculateNetwork(network map[string]types.NetworkStats) (float64, float64) {
var rx, tx float64
for _, v := range network {
rx += float64(v.RxBytes)
tx += float64(v.TxBytes)
}
return rx, tx
}
// calculateMemUsageUnixNoCache calculate memory usage of the container.
// Page cache is intentionally excluded to avoid misinterpretation of the output.
func calculateMemUsageUnixNoCache(mem *types.MemoryStats) float64 {
if mem == nil || len(mem.Stats) == 0 {
return 0.0
}
return float64(mem.Usage - mem.Stats["cache"])
}
func calculateMemPercentUnixNoCache(limit float64, usedNoCache float64) float64 {
// MemoryStats.Limit will never be 0 unless the container is not running and we haven't
// got any data from cgroup
if limit != 0 {
return usedNoCache / limit * 100.0
}
return 0
}