stats: avoid cgo in collector
@@ -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
}
