From 856780f55288ec9e9bfae6c6a7459ca12fb61c5d Mon Sep 17 00:00:00 2001
From: Giuseppe Scrivano <gscrivan@redhat.com>
Date: Tue, 2 Jul 2019 14:14:16 +0200
Subject: stats: use runtime.NumCPU when percpu counters are not available

in the cgroup v2 implementation we don't have yet percpu times.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
---
 libpod/stats.go | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/libpod/stats.go b/libpod/stats.go
index da383e9d9..eb5ed95c4 100644
--- a/libpod/stats.go
+++ b/libpod/stats.go
@@ -3,6 +3,7 @@
 package libpod
 
 import (
+	"runtime"
 	"strings"
 	"syscall"
 	"time"
@@ -105,7 +106,11 @@ func calculateCPUPercent(stats *cgroups.Metrics, previousCPU, previousSystem uin
 	if systemDelta > 0.0 && cpuDelta > 0.0 {
 		// gets a ratio of container cpu usage total, multiplies it by the number of cores (4 cores running
 		// at 100% utilization should be 400% utilization), and multiplies that by 100 to get a percentage
-		cpuPercent = (cpuDelta / systemDelta) * float64(len(stats.CPU.Usage.PerCPU)) * 100
+		nCPUS := len(stats.CPU.Usage.PerCPU)
+		if nCPUS == 0 {
+			nCPUS = runtime.NumCPU()
+		}
+		cpuPercent = (cpuDelta / systemDelta) * float64(nCPUS) * 100
 	}
 	return cpuPercent
 }
-- 
cgit v1.2.3-54-g00ecf