From f14921d41dc6ea9a368f3fcc10fe74bfa5e18a52 Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Thu, 1 Apr 2021 11:56:23 -0400 Subject: [PATCH] cli: Show memory usage instead of RSS If a task doesn't report RSS, let's use memory usage. --- command/alloc_status.go | 8 +++++++- command/helpers.go | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/command/alloc_status.go b/command/alloc_status.go index 4e21d18adff2..b9d2b46be5cb 100644 --- a/command/alloc_status.go +++ b/command/alloc_status.go @@ -586,7 +586,13 @@ func (c *AllocStatusCommand) outputTaskResources(alloc *api.Allocation, task str cpuUsage = fmt.Sprintf("%v/%v", math.Floor(cs.TotalTicks), cpuUsage) } if ms := ru.ResourceUsage.MemoryStats; ms != nil { - memUsage = fmt.Sprintf("%v/%v", humanize.IBytes(ms.RSS), memUsage) + // Nomad uses RSS as the top-level metric to report, for historical reasons, + // but it's not always measured (e.g. with cgroup-v2) + usage := ms.RSS + if usage == 0 && !stringsContain(ms.Measured, "RSS") { + usage = ms.Usage + } + memUsage = fmt.Sprintf("%v/%v", humanize.IBytes(usage), memUsage) } deviceStats = ru.ResourceUsage.DeviceStats } diff --git a/command/helpers.go b/command/helpers.go index 74b221bba406..177cf48c17c6 100644 --- a/command/helpers.go +++ b/command/helpers.go @@ -542,3 +542,14 @@ func (w *uiErrorWriter) Close() error { } return nil } + +// stringsContains returns true if s is present in the vs string slice +func stringsContain(vs []string, s string) bool { + for _, v := range vs { + if v == s { + return true + } + } + + return false +}