@@ -105,25 +105,30 @@ func statMemory(dirPath string, stats *cgroups.Stats) error {
105105 memoryUsage , err := getMemoryDataV2 (dirPath , "" )
106106 if err != nil {
107107 if errors .Is (err , unix .ENOENT ) && dirPath == UnifiedMountpoint {
108- // The root cgroup does not have memory.{current,max}
108+ // The root cgroup does not have memory.{current,max,peak }
109109 // so emulate those using data from /proc/meminfo and
110110 // /sys/fs/cgroup/memory.stat
111111 return rootStatsFromMeminfo (stats )
112112 }
113113 return err
114114 }
115115 stats .MemoryStats .Usage = memoryUsage
116- swapUsage , err := getMemoryDataV2 (dirPath , "swap" )
116+ swapOnlyUsage , err := getMemoryDataV2 (dirPath , "swap" )
117117 if err != nil {
118118 return err
119119 }
120+ stats .MemoryStats .SwapOnlyUsage = swapOnlyUsage
121+ swapUsage := swapOnlyUsage
120122 // As cgroup v1 reports SwapUsage values as mem+swap combined,
121123 // while in cgroup v2 swap values do not include memory,
122124 // report combined mem+swap for v1 compatibility.
123125 swapUsage .Usage += memoryUsage .Usage
124126 if swapUsage .Limit != math .MaxUint64 {
125127 swapUsage .Limit += memoryUsage .Limit
126128 }
129+ // The `MaxUsage` of mem+swap cannot simply combine mem with
130+ // swap. So set it to 0 for v1 compatibility.
131+ swapUsage .MaxUsage = 0
127132 stats .MemoryStats .SwapUsage = swapUsage
128133
129134 return nil
@@ -138,6 +143,7 @@ func getMemoryDataV2(path, name string) (cgroups.MemoryData, error) {
138143 }
139144 usage := moduleName + ".current"
140145 limit := moduleName + ".max"
146+ maxUsage := moduleName + ".peak"
141147
142148 value , err := fscommon .GetCgroupParamUint (path , usage )
143149 if err != nil {
@@ -157,6 +163,14 @@ func getMemoryDataV2(path, name string) (cgroups.MemoryData, error) {
157163 }
158164 memoryData .Limit = value
159165
166+ // `memory.peak` since kernel 5.19
167+ // `memory.swap.peak` since kernel 6.5
168+ value , err = fscommon .GetCgroupParamUint (path , maxUsage )
169+ if err != nil && ! os .IsNotExist (err ) {
170+ return cgroups.MemoryData {}, err
171+ }
172+ memoryData .MaxUsage = value
173+
160174 return memoryData , nil
161175}
162176
0 commit comments