@@ -6,7 +6,11 @@ import (
66 v1 "github.com/containerd/cgroups/stats/v1"
77)
88
9- func GetMemInfoInByte () (total , free , used , cache float64 ) {
9+ type MemInfo struct {
10+ Total , Free , Used , Cache float64
11+ }
12+
13+ func GetMemInfoInByte () (memInfo MemInfo ) {
1014 stats := v1.Metrics {}
1115 mem := cgroups .NewMemory ("/sys/fs/cgroup/" , cgroups .IgnoreModules ("memsw" ))
1216 err := mem .Stat ("" , & stats )
@@ -20,22 +24,31 @@ func GetMemInfoInByte() (total, free, used, cache float64) {
2024 }
2125
2226 // convert Byte to MiB
27+ var total float64
2328 if stats .Memory .Usage .Limit / 1024 > hostMemInfo .MemTotal {
2429 total = float64 (hostMemInfo .MemTotal ) * 1024
2530 } else {
2631 total = float64 (stats .Memory .Usage .Limit )
2732 }
28- used = float64 (stats .Memory .Usage .Usage )
29- cache = float64 (stats .Memory .Cache )
30- free = total - used
31- return
33+ used : = float64 (stats .Memory .Usage .Usage )
34+ cache : = float64 (stats .Memory .Cache )
35+ free : = total - used
36+ return MemInfo { Total : total , Free : free , Used : used , Cache : cache }
3237}
3338
34- func GetTotalMemInMiB () (total , free , used , cache float64 ) {
35- total , free , used , cache = GetMemInfoInByte ()
36- return total / 1048576 , free / 1048576 , used / 1048576 , cache / 1048576
39+ func GetTotalMemInMiB () (memInfo MemInfo ) {
40+ memInfo = GetMemInfoInByte ()
41+ memInfo .Total = memInfo .Total / 1048576
42+ memInfo .Free = memInfo .Free / 1048576
43+ memInfo .Used = memInfo .Used / 1048576
44+ memInfo .Cache = memInfo .Cache / 1048576
45+ return memInfo
3746}
38- func GetTotalMemInKiB () (total , free , used , cache float64 ) {
39- total , free , used , cache = GetMemInfoInByte ()
40- return total / 1024 , free / 1024 , used / 1024 , cache / 1024
47+ func GetTotalMemInKiB () (memInfo MemInfo ) {
48+ memInfo = GetMemInfoInByte ()
49+ memInfo .Total = memInfo .Total / 1024
50+ memInfo .Free = memInfo .Free / 1024
51+ memInfo .Used = memInfo .Used / 1024
52+ memInfo .Cache = memInfo .Cache / 1024
53+ return memInfo
4154}
0 commit comments