pkg/sysinfo: unify NumCPU implementation

Use a single exported implementation, so that we can maintain the
GoDoc string in one place, and use non-exported functions for the
actual implementation (which were already in place).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-12-16 14:59:32 +01:00
parent 298d3aa8b8
commit 6919b9879b
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
4 changed files with 15 additions and 23 deletions

View file

@ -1,13 +1,15 @@
//go:build !linux && !windows
// +build !linux,!windows
package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
import (
"runtime"
)
// NumCPU returns the number of CPUs
// NumCPU returns the number of CPUs. On Linux and Windows, it returns
// the number of CPUs which are currently online. On other platforms,
// it's the equivalent of [runtime.NumCPU].
func NumCPU() int {
if ncpu := numCPU(); ncpu > 0 {
return ncpu
}
return runtime.NumCPU()
}

View file

@ -1,8 +1,6 @@
package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
import (
"runtime"
"golang.org/x/sys/unix"
)
@ -23,11 +21,3 @@ func numCPU() int {
return mask.Count()
}
// NumCPU returns the number of CPUs which are currently online
func NumCPU() int {
if ncpu := numCPU(); ncpu > 0 {
return ncpu
}
return runtime.NumCPU()
}

View file

@ -0,0 +1,9 @@
//go:build !linux && !windows
// +build !linux,!windows
package sysinfo
func numCPU() int {
// not implemented
return 0
}

View file

@ -1,7 +1,6 @@
package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
import (
"runtime"
"unsafe"
"golang.org/x/sys/windows"
@ -35,11 +34,3 @@ func numCPU() int {
ncpu := int(popcnt(uint64(mask)))
return ncpu
}
// NumCPU returns the number of CPUs which are currently online
func NumCPU() int {
if ncpu := numCPU(); ncpu > 0 {
return ncpu
}
return runtime.NumCPU()
}