stats: avoid cgo in collector

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
(cherry picked from commit cf104d85c3)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Tonis Tiigi 2019-06-04 20:55:46 -07:00 committed by Kir Kolyshkin
parent 4ef8f6d323
commit 80e2871d21
1 changed files with 5 additions and 12 deletions

View File

@ -9,13 +9,9 @@ import (
"strings"
"github.com/opencontainers/runc/libcontainer/system"
"golang.org/x/sys/unix"
)
/*
#include <unistd.h>
*/
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
}