Skip to content

Commit

Permalink
runtimeVM: Calculate the WorkingSetBytes stats
Browse files Browse the repository at this point in the history
WorkingSetBytes is the bit that needs to be set in order to provide
kubelet the pod's memory information.

Although it's calculated in a slightly different way for "oci" runtime
types, the logic is quite similar for the "vm" runtime type, with the
only difference being where the TotalInactiveFile information comes
from.

This is the last bit needed in order to have `kubectl top pod $pod`
working, as shown below:
```
[fidencio@localhost cri-o]$ kubectl get pods
NAME             READY   STATUS    RESTARTS   AGE
example-fedora   1/1     Running   0          130m

[fidencio@localhost cri-o]$ kubectl get pod example-fedora -o yaml | grep runtimeClassName
      {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"app":"example-fedora-app"},"name":"example-fedora","namespace":"default"},"spec":{"containers":[{"args":["-m","http.server","8080"],"command":["python3"],"image":"fedora:33","name":"example-fedora","ports":[{"containerPort":8080}]}],"runtimeClassName":"kata"}}
        f:runtimeClassName: {}
  runtimeClassName: kata

[fidencio@localhost cri-o]$ kubectl top pod
NAME             CPU(cores)   MEMORY(bytes)
example-fedora   1m           9Mi
```

Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com>
  • Loading branch information
fidencio committed Jan 15, 2021
1 parent ccb4b72 commit 19c31c5
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions internal/oci/runtime_vm.go
Expand Up @@ -737,14 +737,15 @@ func (r *runtimeVM) ContainerStats(c *Container, _ string) (*ContainerStats, err

func metricsToCtrStats(c *Container, m *cgroups.Metrics) *ContainerStats {
var (
cpu float64
cpuNano uint64
memUsage uint64
memLimit uint64
memPerc float64
blockInput uint64
blockOutput uint64
pids uint64
cpu float64
cpuNano uint64
memUsage uint64
memLimit uint64
memPerc float64
workingSetBytes uint64
blockInput uint64
blockOutput uint64
pids uint64
)

if m != nil {
Expand All @@ -756,6 +757,14 @@ func metricsToCtrStats(c *Container, m *cgroups.Metrics) *ContainerStats {
memUsage = m.Memory.Usage.Usage
memLimit = getMemLimit(m.Memory.Usage.Limit)
memPerc = float64(memUsage) / float64(memLimit)
if memUsage > m.Memory.TotalInactiveFile {
workingSetBytes = memUsage - m.Memory.TotalInactiveFile
} else {
logrus.Debugf(
"unable to account working set stats: total_inactive_file (%d) > memory usage (%d)",
m.Memory.TotalInactiveFile, memUsage,
)
}

for _, entry := range m.Blkio.IoServiceBytesRecursive {
switch strings.ToLower(entry.Op) {
Expand All @@ -768,16 +777,17 @@ func metricsToCtrStats(c *Container, m *cgroups.Metrics) *ContainerStats {
}

return &ContainerStats{
Container: c.ID(),
CPU: cpu,
CPUNano: cpuNano,
SystemNano: time.Now().UnixNano(),
MemUsage: memUsage,
MemLimit: memLimit,
MemPerc: memPerc,
BlockInput: blockInput,
BlockOutput: blockOutput,
PIDs: pids,
Container: c.ID(),
CPU: cpu,
CPUNano: cpuNano,
SystemNano: time.Now().UnixNano(),
MemUsage: memUsage,
MemLimit: memLimit,
MemPerc: memPerc,
WorkingSetBytes: workingSetBytes,
BlockInput: blockInput,
BlockOutput: blockOutput,
PIDs: pids,
}
}

Expand Down

0 comments on commit 19c31c5

Please sign in to comment.