From 80e2871d21f1f47469b4f78670929c7ca47fab4f Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Tue, 4 Jun 2019 20:55:46 -0700 Subject: [PATCH] stats: avoid cgo in collector Signed-off-by: Tonis Tiigi (cherry picked from commit cf104d85c35947c25dcd86cef19aa97fe31e4bbd) Signed-off-by: Kir Kolyshkin --- daemon/stats/collector_unix.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/daemon/stats/collector_unix.go b/daemon/stats/collector_unix.go index d476d6da77..417713f6ab 100644 --- a/daemon/stats/collector_unix.go +++ b/daemon/stats/collector_unix.go @@ -9,13 +9,9 @@ import ( "strings" "github.com/opencontainers/runc/libcontainer/system" + "golang.org/x/sys/unix" ) -/* -#include -*/ -import "C" - // platformNewStatsCollector performs platform specific initialisation of the // Collector structure. func platformNewStatsCollector(s *Collector) { @@ -70,13 +66,10 @@ func (s *Collector) getSystemCPUUsage() (uint64, error) { } func (s *Collector) getNumberOnlineCPUs() (uint32, error) { - i, err := C.sysconf(C._SC_NPROCESSORS_ONLN) - // According to POSIX - errno is undefined after successful - // sysconf, and can be non-zero in several cases, so look for - // error in returned value not in errno. - // (https://sourceware.org/bugzilla/show_bug.cgi?id=21536) - if i == -1 { + var cpuset unix.CPUSet + err := unix.SchedGetaffinity(0, &cpuset) + if err != nil { return 0, err } - return uint32(i), nil + return uint32(cpuset.Count()), nil }