2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2024-06-18 08:29:45 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2017-05-08 13:51:30 -04:00
|
|
|
"github.com/docker/docker/client"
|
2017-03-27 21:21:59 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-08-07 05:52:40 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type stats struct {
|
2022-03-28 05:29:06 -04:00
|
|
|
mu sync.RWMutex
|
2018-10-23 11:05:44 -04:00
|
|
|
cs []*Stats
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-09-07 19:08:51 -04:00
|
|
|
// daemonOSType is set once we have at least one stat for a container
|
|
|
|
// from the daemon. It is used to ensure we print the right header based
|
|
|
|
// on the daemon platform.
|
|
|
|
var daemonOSType string
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
func (s *stats) add(cs *Stats) bool {
|
2016-09-08 13:11:39 -04:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2016-11-03 02:20:46 -04:00
|
|
|
if _, exists := s.isKnownContainer(cs.Container); !exists {
|
2016-09-08 13:11:39 -04:00
|
|
|
s.cs = append(s.cs, cs)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stats) remove(id string) {
|
|
|
|
s.mu.Lock()
|
|
|
|
if i, exists := s.isKnownContainer(id); exists {
|
|
|
|
s.cs = append(s.cs[:i], s.cs[i+1:]...)
|
|
|
|
}
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stats) isKnownContainer(cid string) (int, bool) {
|
|
|
|
for i, c := range s.cs {
|
2016-11-03 02:20:46 -04:00
|
|
|
if c.Container == cid {
|
2016-09-08 13:11:39 -04:00
|
|
|
return i, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, false
|
|
|
|
}
|
|
|
|
|
2024-06-18 08:29:45 -04:00
|
|
|
func collect(ctx context.Context, s *Stats, cli client.ContainerAPIClient, streamStats bool, waitFirst *sync.WaitGroup) {
|
2016-11-03 02:20:46 -04:00
|
|
|
logrus.Debugf("collecting stats for %s", s.Container)
|
2016-09-08 13:11:39 -04:00
|
|
|
var (
|
|
|
|
getFirst bool
|
|
|
|
previousCPU uint64
|
|
|
|
previousSystem uint64
|
|
|
|
u = make(chan error, 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// if error happens and we get nothing of stats, release wait group whatever
|
|
|
|
if !getFirst {
|
|
|
|
getFirst = true
|
|
|
|
waitFirst.Done()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-11-03 02:20:46 -04:00
|
|
|
response, err := cli.ContainerStats(ctx, s.Container, streamStats)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
2016-09-22 08:54:41 -04:00
|
|
|
s.SetError(err)
|
2016-09-08 13:11:39 -04:00
|
|
|
return
|
|
|
|
}
|
2016-09-07 19:08:51 -04:00
|
|
|
defer response.Body.Close()
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-09-07 19:08:51 -04:00
|
|
|
dec := json.NewDecoder(response.Body)
|
2016-09-08 13:11:39 -04:00
|
|
|
go func() {
|
|
|
|
for {
|
2016-09-07 19:08:51 -04:00
|
|
|
var (
|
2024-06-18 08:29:45 -04:00
|
|
|
v *container.StatsResponse
|
2016-11-29 03:17:35 -05:00
|
|
|
memPercent, cpuPercent float64
|
|
|
|
blkRead, blkWrite uint64 // Only used on Linux
|
2017-04-27 14:17:47 -04:00
|
|
|
mem, memLimit float64
|
2016-11-29 03:17:35 -05:00
|
|
|
pidsStatsCurrent uint64
|
2016-09-07 19:08:51 -04:00
|
|
|
)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
if err := dec.Decode(&v); err != nil {
|
2016-09-07 19:08:51 -04:00
|
|
|
dec = json.NewDecoder(io.MultiReader(dec.Buffered(), response.Body))
|
2016-09-08 13:11:39 -04:00
|
|
|
u <- err
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-09-07 19:08:51 -04:00
|
|
|
daemonOSType = response.OSType
|
|
|
|
|
|
|
|
if daemonOSType != "windows" {
|
|
|
|
previousCPU = v.PreCPUStats.CPUUsage.TotalUsage
|
|
|
|
previousSystem = v.PreCPUStats.SystemUsage
|
|
|
|
cpuPercent = calculateCPUPercentUnix(previousCPU, previousSystem, v)
|
|
|
|
blkRead, blkWrite = calculateBlockIO(v.BlkioStats)
|
2017-04-27 14:17:47 -04:00
|
|
|
mem = calculateMemUsageUnixNoCache(v.MemoryStats)
|
2016-09-22 08:54:41 -04:00
|
|
|
memLimit = float64(v.MemoryStats.Limit)
|
2017-04-27 14:17:47 -04:00
|
|
|
memPercent = calculateMemPercentUnixNoCache(memLimit, mem)
|
2016-09-22 08:54:41 -04:00
|
|
|
pidsStatsCurrent = v.PidsStats.Current
|
2016-09-07 19:08:51 -04:00
|
|
|
} else {
|
|
|
|
cpuPercent = calculateCPUPercentWindows(v)
|
|
|
|
blkRead = v.StorageStats.ReadSizeBytes
|
|
|
|
blkWrite = v.StorageStats.WriteSizeBytes
|
|
|
|
mem = float64(v.MemoryStats.PrivateWorkingSet)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-22 08:54:41 -04:00
|
|
|
netRx, netTx := calculateNetwork(v.Networks)
|
2018-10-23 11:05:44 -04:00
|
|
|
s.SetStatistics(StatsEntry{
|
2016-11-03 02:20:46 -04:00
|
|
|
Name: v.Name,
|
|
|
|
ID: v.ID,
|
2016-09-22 08:54:41 -04:00
|
|
|
CPUPercentage: cpuPercent,
|
|
|
|
Memory: mem,
|
2017-04-27 14:17:47 -04:00
|
|
|
MemoryPercentage: memPercent,
|
2016-09-22 08:54:41 -04:00
|
|
|
MemoryLimit: memLimit,
|
|
|
|
NetworkRx: netRx,
|
|
|
|
NetworkTx: netTx,
|
|
|
|
BlockRead: float64(blkRead),
|
|
|
|
BlockWrite: float64(blkWrite),
|
|
|
|
PidsCurrent: pidsStatsCurrent,
|
|
|
|
})
|
2016-09-08 13:11:39 -04:00
|
|
|
u <- nil
|
|
|
|
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.
|
2016-09-22 08:54:41 -04:00
|
|
|
s.SetErrorAndReset(errors.New("timeout waiting for stats"))
|
2016-09-08 13:11:39 -04:00
|
|
|
// if this is the first stat you get, release WaitGroup
|
|
|
|
if !getFirst {
|
|
|
|
getFirst = true
|
|
|
|
waitFirst.Done()
|
|
|
|
}
|
|
|
|
case err := <-u:
|
2016-12-28 03:28:32 -05:00
|
|
|
s.SetError(err)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// if this is the first stat you get, release WaitGroup
|
|
|
|
if !getFirst {
|
|
|
|
getFirst = true
|
|
|
|
waitFirst.Done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !streamStats {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 08:29:45 -04:00
|
|
|
func calculateCPUPercentUnix(previousCPU, previousSystem uint64, v *container.StatsResponse) float64 {
|
2016-09-08 13:11:39 -04:00
|
|
|
var (
|
|
|
|
cpuPercent = 0.0
|
|
|
|
// calculate the change for the cpu usage of the container in between readings
|
|
|
|
cpuDelta = float64(v.CPUStats.CPUUsage.TotalUsage) - float64(previousCPU)
|
|
|
|
// calculate the change for the entire system between readings
|
|
|
|
systemDelta = float64(v.CPUStats.SystemUsage) - float64(previousSystem)
|
2017-03-06 12:29:09 -05:00
|
|
|
onlineCPUs = float64(v.CPUStats.OnlineCPUs)
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
2017-03-06 12:29:09 -05:00
|
|
|
if onlineCPUs == 0.0 {
|
|
|
|
onlineCPUs = float64(len(v.CPUStats.CPUUsage.PercpuUsage))
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
if systemDelta > 0.0 && cpuDelta > 0.0 {
|
2017-03-06 12:29:09 -05:00
|
|
|
cpuPercent = (cpuDelta / systemDelta) * onlineCPUs * 100.0
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
return cpuPercent
|
|
|
|
}
|
|
|
|
|
2024-06-18 08:29:45 -04:00
|
|
|
func calculateCPUPercentWindows(v *container.StatsResponse) float64 {
|
2016-09-07 19:08:51 -04:00
|
|
|
// Max number of 100ns intervals between the previous time read and now
|
|
|
|
possIntervals := uint64(v.Read.Sub(v.PreRead).Nanoseconds()) // Start with number of ns intervals
|
|
|
|
possIntervals /= 100 // Convert to number of 100ns intervals
|
|
|
|
possIntervals *= uint64(v.NumProcs) // Multiple by the number of processors
|
|
|
|
|
|
|
|
// Intervals used
|
|
|
|
intervalsUsed := v.CPUStats.CPUUsage.TotalUsage - v.PreCPUStats.CPUUsage.TotalUsage
|
|
|
|
|
|
|
|
// Percentage avoiding divide-by-zero
|
|
|
|
if possIntervals > 0 {
|
|
|
|
return float64(intervalsUsed) / float64(possIntervals) * 100.0
|
|
|
|
}
|
|
|
|
return 0.00
|
|
|
|
}
|
|
|
|
|
2024-06-18 08:29:45 -04:00
|
|
|
func calculateBlockIO(blkio container.BlkioStats) (uint64, uint64) {
|
2017-10-12 11:44:03 -04:00
|
|
|
var blkRead, blkWrite uint64
|
2016-09-08 13:11:39 -04:00
|
|
|
for _, bioEntry := range blkio.IoServiceBytesRecursive {
|
2019-03-10 22:01:22 -04:00
|
|
|
if len(bioEntry.Op) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch bioEntry.Op[0] {
|
|
|
|
case 'r', 'R':
|
linting: address assorted issues found by gocritic
internal/test/builders/config.go:36:15: captLocal: `ID' should not be capitalized (gocritic)
func ConfigID(ID string) func(config *swarm.Config) {
^
internal/test/builders/secret.go:45:15: captLocal: `ID' should not be capitalized (gocritic)
func SecretID(ID string) func(secret *swarm.Secret) {
^
internal/test/builders/service.go:21:16: captLocal: `ID' should not be capitalized (gocritic)
func ServiceID(ID string) func(*swarm.Service) {
^
cli/command/image/formatter_history.go:100:15: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(c.h.CreatedBy, "\t", " ", -1)` (gocritic)
createdBy := strings.Replace(c.h.CreatedBy, "\t", " ", -1)
^
e2e/image/push_test.go:246:34: badCall: suspicious Join on 1 argument (gocritic)
assert.NilError(t, os.RemoveAll(filepath.Join(dir.Join("trust"))))
^
e2e/image/push_test.go:313:34: badCall: suspicious Join on 1 argument (gocritic)
assert.NilError(t, os.RemoveAll(filepath.Join(dir.Join("trust"))))
^
cli/config/configfile/file_test.go:185:2: assignOp: replace `c.GetAllCallCount = c.GetAllCallCount + 1` with `c.GetAllCallCount++` (gocritic)
c.GetAllCallCount = c.GetAllCallCount + 1
^
cli/command/context/inspect_test.go:20:58: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(si.MetadataPath, `\`, `\\`, -1)` (gocritic)
expected = strings.Replace(expected, "<METADATA_PATH>", strings.Replace(si.MetadataPath, `\`, `\\`, -1), 1)
^
cli/command/context/inspect_test.go:21:53: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(si.TLSPath, `\`, `\\`, -1)` (gocritic)
expected = strings.Replace(expected, "<TLS_PATH>", strings.Replace(si.TLSPath, `\`, `\\`, -1), 1)
^
cli/command/container/formatter_stats.go:119:46: captLocal: `Stats' should not be capitalized (gocritic)
func statsFormatWrite(ctx formatter.Context, Stats []StatsEntry, osType string, trunc bool) error {
^
cli/command/container/stats_helpers.go:209:4: assignOp: replace `blkRead = blkRead + bioEntry.Value` with `blkRead += bioEntry.Value` (gocritic)
blkRead = blkRead + bioEntry.Value
^
cli/command/container/stats_helpers.go:211:4: assignOp: replace `blkWrite = blkWrite + bioEntry.Value` with `blkWrite += bioEntry.Value` (gocritic)
blkWrite = blkWrite + bioEntry.Value
^
cli/command/registry/formatter_search.go:67:10: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(c.s.Description, "\n", " ", -1)` (gocritic)
desc := strings.Replace(c.s.Description, "\n", " ", -1)
^
cli/command/registry/formatter_search.go:68:9: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(desc, "\r", " ", -1)` (gocritic)
desc = strings.Replace(desc, "\r", " ", -1)
^
cli/command/service/list_test.go:164:5: assignOp: replace `tc.doc = tc.doc + " with quiet"` with `tc.doc += " with quiet"` (gocritic)
tc.doc = tc.doc + " with quiet"
^
cli/command/service/progress/progress.go:274:11: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(errMsg, "\n", " ", -1)` (gocritic)
errMsg = strings.Replace(errMsg, "\n", " ", -1)
^
cli/manifest/store/store.go:153:9: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(fileName, "/", "_", -1)` (gocritic)
return strings.Replace(fileName, "/", "_", -1)
^
cli/manifest/store/store.go:152:14: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(ref, ":", "-", -1)` (gocritic)
fileName := strings.Replace(ref, ":", "-", -1)
^
cli/command/plugin/formatter.go:79:10: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(c.p.Config.Description, "\n", "", -1)` (gocritic)
desc := strings.Replace(c.p.Config.Description, "\n", "", -1)
^
cli/command/plugin/formatter.go:80:9: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(desc, "\r", "", -1)` (gocritic)
desc = strings.Replace(desc, "\r", "", -1)
^
cli/compose/convert/service.go:642:23: captLocal: `DNS' should not be capitalized (gocritic)
func convertDNSConfig(DNS []string, DNSSearch []string) *swarm.DNSConfig {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-11-20 07:30:05 -05:00
|
|
|
blkRead += bioEntry.Value
|
2019-03-10 22:01:22 -04:00
|
|
|
case 'w', 'W':
|
linting: address assorted issues found by gocritic
internal/test/builders/config.go:36:15: captLocal: `ID' should not be capitalized (gocritic)
func ConfigID(ID string) func(config *swarm.Config) {
^
internal/test/builders/secret.go:45:15: captLocal: `ID' should not be capitalized (gocritic)
func SecretID(ID string) func(secret *swarm.Secret) {
^
internal/test/builders/service.go:21:16: captLocal: `ID' should not be capitalized (gocritic)
func ServiceID(ID string) func(*swarm.Service) {
^
cli/command/image/formatter_history.go:100:15: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(c.h.CreatedBy, "\t", " ", -1)` (gocritic)
createdBy := strings.Replace(c.h.CreatedBy, "\t", " ", -1)
^
e2e/image/push_test.go:246:34: badCall: suspicious Join on 1 argument (gocritic)
assert.NilError(t, os.RemoveAll(filepath.Join(dir.Join("trust"))))
^
e2e/image/push_test.go:313:34: badCall: suspicious Join on 1 argument (gocritic)
assert.NilError(t, os.RemoveAll(filepath.Join(dir.Join("trust"))))
^
cli/config/configfile/file_test.go:185:2: assignOp: replace `c.GetAllCallCount = c.GetAllCallCount + 1` with `c.GetAllCallCount++` (gocritic)
c.GetAllCallCount = c.GetAllCallCount + 1
^
cli/command/context/inspect_test.go:20:58: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(si.MetadataPath, `\`, `\\`, -1)` (gocritic)
expected = strings.Replace(expected, "<METADATA_PATH>", strings.Replace(si.MetadataPath, `\`, `\\`, -1), 1)
^
cli/command/context/inspect_test.go:21:53: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(si.TLSPath, `\`, `\\`, -1)` (gocritic)
expected = strings.Replace(expected, "<TLS_PATH>", strings.Replace(si.TLSPath, `\`, `\\`, -1), 1)
^
cli/command/container/formatter_stats.go:119:46: captLocal: `Stats' should not be capitalized (gocritic)
func statsFormatWrite(ctx formatter.Context, Stats []StatsEntry, osType string, trunc bool) error {
^
cli/command/container/stats_helpers.go:209:4: assignOp: replace `blkRead = blkRead + bioEntry.Value` with `blkRead += bioEntry.Value` (gocritic)
blkRead = blkRead + bioEntry.Value
^
cli/command/container/stats_helpers.go:211:4: assignOp: replace `blkWrite = blkWrite + bioEntry.Value` with `blkWrite += bioEntry.Value` (gocritic)
blkWrite = blkWrite + bioEntry.Value
^
cli/command/registry/formatter_search.go:67:10: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(c.s.Description, "\n", " ", -1)` (gocritic)
desc := strings.Replace(c.s.Description, "\n", " ", -1)
^
cli/command/registry/formatter_search.go:68:9: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(desc, "\r", " ", -1)` (gocritic)
desc = strings.Replace(desc, "\r", " ", -1)
^
cli/command/service/list_test.go:164:5: assignOp: replace `tc.doc = tc.doc + " with quiet"` with `tc.doc += " with quiet"` (gocritic)
tc.doc = tc.doc + " with quiet"
^
cli/command/service/progress/progress.go:274:11: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(errMsg, "\n", " ", -1)` (gocritic)
errMsg = strings.Replace(errMsg, "\n", " ", -1)
^
cli/manifest/store/store.go:153:9: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(fileName, "/", "_", -1)` (gocritic)
return strings.Replace(fileName, "/", "_", -1)
^
cli/manifest/store/store.go:152:14: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(ref, ":", "-", -1)` (gocritic)
fileName := strings.Replace(ref, ":", "-", -1)
^
cli/command/plugin/formatter.go:79:10: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(c.p.Config.Description, "\n", "", -1)` (gocritic)
desc := strings.Replace(c.p.Config.Description, "\n", "", -1)
^
cli/command/plugin/formatter.go:80:9: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(desc, "\r", "", -1)` (gocritic)
desc = strings.Replace(desc, "\r", "", -1)
^
cli/compose/convert/service.go:642:23: captLocal: `DNS' should not be capitalized (gocritic)
func convertDNSConfig(DNS []string, DNSSearch []string) *swarm.DNSConfig {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-11-20 07:30:05 -05:00
|
|
|
blkWrite += bioEntry.Value
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-12 11:44:03 -04:00
|
|
|
return blkRead, blkWrite
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2024-06-18 08:29:45 -04:00
|
|
|
func calculateNetwork(network map[string]container.NetworkStats) (float64, float64) {
|
2016-09-08 13:11:39 -04:00
|
|
|
var rx, tx float64
|
|
|
|
|
|
|
|
for _, v := range network {
|
|
|
|
rx += float64(v.RxBytes)
|
|
|
|
tx += float64(v.TxBytes)
|
|
|
|
}
|
|
|
|
return rx, tx
|
|
|
|
}
|
2017-04-27 14:17:47 -04:00
|
|
|
|
|
|
|
// calculateMemUsageUnixNoCache calculate memory usage of the container.
|
2020-04-03 01:33:43 -04:00
|
|
|
// Cache is intentionally excluded to avoid misinterpretation of the output.
|
|
|
|
//
|
|
|
|
// On cgroup v1 host, the result is `mem.Usage - mem.Stats["total_inactive_file"]` .
|
|
|
|
// On cgroup v2 host, the result is `mem.Usage - mem.Stats["inactive_file"] `.
|
|
|
|
//
|
|
|
|
// This definition is consistent with cadvisor and containerd/CRI.
|
|
|
|
// * https://github.com/google/cadvisor/commit/307d1b1cb320fef66fab02db749f07a459245451
|
|
|
|
// * https://github.com/containerd/cri/commit/6b8846cdf8b8c98c1d965313d66bc8489166059a
|
|
|
|
//
|
|
|
|
// On Docker 19.03 and older, the result was `mem.Usage - mem.Stats["cache"]`.
|
|
|
|
// See https://github.com/moby/moby/issues/40727 for the background.
|
2024-06-18 08:29:45 -04:00
|
|
|
func calculateMemUsageUnixNoCache(mem container.MemoryStats) float64 {
|
2020-04-03 01:33:43 -04:00
|
|
|
// cgroup v1
|
|
|
|
if v, isCgroup1 := mem.Stats["total_inactive_file"]; isCgroup1 && v < mem.Usage {
|
|
|
|
return float64(mem.Usage - v)
|
|
|
|
}
|
|
|
|
// cgroup v2
|
|
|
|
if v := mem.Stats["inactive_file"]; v < mem.Usage {
|
|
|
|
return float64(mem.Usage - v)
|
|
|
|
}
|
|
|
|
return float64(mem.Usage)
|
2017-04-27 14:17:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|