Skip to content

Commit

Permalink
merge issue-10491, but only stats_helpers.go file
Browse files Browse the repository at this point in the history
  • Loading branch information
arstercz committed Mar 5, 2023
1 parent 46201a0 commit 9bcaed3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plugins/inputs/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ var baseClient = MockClient{
},
CloseF: func() error {
return nil
}
},
}

func newClient(host string, tlsConfig *tls.Config) (Client, error) {
Expand Down
21 changes: 20 additions & 1 deletion plugins/inputs/docker/stats_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,27 @@ func calculateCPUPercentWindows(v *types.StatsJSON) float64 {

// CalculateMemUsageUnixNoCache calculate memory usage of the container.
// Page cache is intentionally excluded to avoid misinterpretation of the output.
//
// On Docker 19.03 and older, the result is `mem.Usage - mem.Stats["cache"]`.
// On new docker with cgroup v1 host, the result is `mem.Usage - mem.Stats["total_inactive_file"]`.
// On new docker with cgroup v2 host, the result is `mem.Usage - mem.Stats["inactive_file"]`.
//
// This definition is designed to be consistent with past values and the latest docker CLI
// * https://github.com/docker/cli/blob/6e2838e18645e06f3e4b6c5143898ccc44063e3b/cli/command/container/stats_helpers.go#L239
func CalculateMemUsageUnixNoCache(mem types.MemoryStats) float64 {
return float64(mem.Usage - mem.Stats["cache"])
// Docker 19.03 and older
if v, isOldDocker := mem.Stats["cache"]; isOldDocker && v < mem.Usage {
return float64(mem.Usage - v)
}
// 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)
}

func CalculateMemPercentUnixNoCache(limit float64, usedNoCache float64) float64 {
Expand Down

0 comments on commit 9bcaed3

Please sign in to comment.