pkg/sysinfo: unify NumCPU implementations

Use a single NumCPU implementation, so that the godoc string and the
fallback to runtime.NumCPU can be maintained in one place.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
This commit is contained in:
Tobias Klauser 2023-04-21 10:42:07 +02:00
parent 926a2461a0
commit fc6bfd2b96
4 changed files with 17 additions and 25 deletions

View File

@ -1,13 +1,13 @@
//go:build !linux && !windows
// +build !linux,!windows
package sysinfo
import (
"runtime"
)
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 returns [runtime.NumCPU].
func NumCPU() int {
if ncpu := numCPU(); ncpu > 0 {
return ncpu
}
return runtime.NumCPU()
}

View File

@ -4,7 +4,6 @@
package sysinfo
import (
"runtime"
"unsafe"
"golang.org/x/sys/unix"
@ -34,11 +33,3 @@ func numCPU() int {
}
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()
}

View File

@ -0,0 +1,10 @@
//go:build !linux && !windows
// +build !linux,!windows
package sysinfo
import "runtime"
func numCPU() int {
return runtime.NumCPU()
}

View File

@ -4,7 +4,6 @@
package sysinfo
import (
"runtime"
"unsafe"
"golang.org/x/sys/windows"
@ -28,11 +27,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()
}