Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions internal/oci/oci_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
package oci

import (
"bufio"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -110,6 +112,18 @@ func (r *runtimeOCI) containerStats(ctr *Container, cgroup string) (*ContainerSt
stats.NetInput, stats.NetOutput = getContainerNetIO(netNsPath)
}

totalInactiveFile, err := getTotalInactiveFile()
if err != nil { // nolint: gocritic
logrus.Warnf("error in memory working set stats retrieval: %v", err)
} else if stats.MemUsage > totalInactiveFile {
stats.WorkingSetBytes = stats.MemUsage - totalInactiveFile
} else {
logrus.Debugf(
"unable to account working set stats: total_inactive_file (%d) > memory usage (%d)",
totalInactiveFile, stats.MemUsage,
)
}

return stats, nil
}

Expand Down Expand Up @@ -162,3 +176,40 @@ func metricsToCtrStats(c *Container, m *cgroups.Metrics) *ContainerStats {
PIDs: pids,
}
}

// getTotalInactiveFile returns the value if `total_inactive_file` as integer
// from `/sys/fs/cgroup/memory/memory.stat`. It returns an error if the file is
// not parsable.
func getTotalInactiveFile() (uint64, error) {
// no cgroupv2 support right now
if isV2, err := cgroups.IsCgroup2UnifiedMode(); err == nil || isV2 {
return 0, nil
}
Comment on lines +185 to +187
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@giuseppe is this the right way to use this function? I cannot omit the check for the err because the linter will complain that I not checked that error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I think it is fine. Thanks for fixing it so quickly!


const memoryStat = "/sys/fs/cgroup/memory/memory.stat"
const totalInactiveFilePrefix = "total_inactive_file "
f, err := os.Open(memoryStat)
if err != nil {
return 0, err
}
defer f.Close()

scanner := bufio.NewScanner(f)
for scanner.Scan() {
if strings.Contains(scanner.Text(), totalInactiveFilePrefix) {
val, err := strconv.Atoi(
strings.TrimPrefix(scanner.Text(), totalInactiveFilePrefix),
)
if err != nil {
return 0, errors.Wrapf(err, "unable to parse total inactive file value")
}
return uint64(val), nil
}
}

if err := scanner.Err(); err != nil {
return 0, err
}

return 0, errors.Errorf("%q not found in %v", totalInactiveFilePrefix, memoryStat)
}
25 changes: 13 additions & 12 deletions internal/oci/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ import (

// ContainerStats contains the statistics information for a running container
type ContainerStats struct {
Container string
CPU float64
CPUNano uint64
SystemNano int64
MemUsage uint64
MemLimit uint64
MemPerc float64
NetInput uint64
NetOutput uint64
BlockInput uint64
BlockOutput uint64
PIDs uint64
Container string
CPU float64
CPUNano uint64
SystemNano int64
MemUsage uint64
MemLimit uint64
MemPerc float64
NetInput uint64
NetOutput uint64
BlockInput uint64
BlockOutput uint64
PIDs uint64
WorkingSetBytes uint64
}

// Returns the total number of bytes transmitted and received for the given container stats
Expand Down
2 changes: 1 addition & 1 deletion server/container_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *Server) buildContainerStats(stats *oci.ContainerStats, container *oci.C
},
Memory: &pb.MemoryUsage{
Timestamp: stats.SystemNano,
WorkingSetBytes: &pb.UInt64Value{Value: stats.MemUsage},
WorkingSetBytes: &pb.UInt64Value{Value: stats.WorkingSetBytes},
},
WritableLayer: writableLayer,
}
Expand Down