diff --git a/go.mod b/go.mod index 475c3a9..3f6fb0b 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/tklauser/numcpus go 1.11 -require golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71 +require golang.org/x/sys v0.0.0-20220111092808-5a964db01320 diff --git a/go.sum b/go.sum index a3fe495..7094701 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71 h1:ikCpsnYR+Ew0vu99XlDp55lGgDJdIMx3f4a18jfse/s= -golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY= +golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/numcpus.go b/numcpus.go index e5cc0ce..af59983 100644 --- a/numcpus.go +++ b/numcpus.go @@ -1,4 +1,4 @@ -// Copyright 2018 Tobias Klauser +// Copyright 2018-2022 Tobias Klauser // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,8 +15,8 @@ // Package numcpus provides information about the number of CPUs in the system. // // It gets the number of CPUs (online, offline, present, possible or kernel -// maximum) on Linux, Darwin, FreeBSD, NetBSD, OpenBSD, DragonflyBSD or -// Solaris/Illumos systems. +// maximum) on Linux, Darwin, FreeBSD, NetBSD, OpenBSD, DragonflyBSD, +// Solaris/Illumos or Windows systems. // // On Linux, the information is retrieved by reading the corresponding CPU // topology files in /sys/devices/system/cpu. @@ -24,9 +24,12 @@ // On BSD systems, the information is retrieved using the hw.ncpu and // hw.ncpuonline sysctls, if supported. // +// On Windows systems, the information is retrieved using the +// GetActiveProcessorCount and GetMaximumProcessorCount functions, respectively. +// // Not all functions are supported on Darwin, FreeBSD, NetBSD, OpenBSD, -// DragonflyBSD and Solaris/Illumos. ErrNotSupported is returned in case a -// function is not supported on a particular platform. +// DragonflyBSD, Solaris/Illumos and Windows. ErrNotSupported is returned in +// case a function is not supported on a particular platform. package numcpus import "errors" @@ -42,7 +45,7 @@ func GetConfigured() (int, error) { } // GetKernelMax returns the maximum number of CPUs allowed by the kernel -// configuration. This function is only supported on Linux systems. +// configuration. This function is only supported on Linux and Windows systems. func GetKernelMax() (int, error) { return getKernelMax() } diff --git a/numcpus_unsupported.go b/numcpus_unsupported.go index ef79066..4a0b7c4 100644 --- a/numcpus_unsupported.go +++ b/numcpus_unsupported.go @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build !darwin && !dragonfly && !freebsd && !linux && !netbsd && !openbsd && !solaris -// +build !darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris +//go:build !darwin && !dragonfly && !freebsd && !linux && !netbsd && !openbsd && !solaris && !windows +// +build !darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris,!windows package numcpus diff --git a/numcpus_windows.go b/numcpus_windows.go new file mode 100644 index 0000000..f7d5b40 --- /dev/null +++ b/numcpus_windows.go @@ -0,0 +1,41 @@ +// Copyright 2022 Tobias Klauser +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package numcpus + +import "golang.org/x/sys/windows" + +func getConfigured() (int, error) { + return int(windows.GetActiveProcessorCount(windows.ALL_PROCESSOR_GROUPS)), nil +} + +func getKernelMax() (int, error) { + return int(windows.GetMaximumProcessorCount(windows.ALL_PROCESSOR_GROUPS)), nil +} + +func getOffline() (int, error) { + return 0, ErrNotSupported +} + +func getOnline() (int, error) { + return int(windows.GetActiveProcessorCount(windows.ALL_PROCESSOR_GROUPS)), nil +} + +func getPossible() (int, error) { + return int(windows.GetActiveProcessorCount(windows.ALL_PROCESSOR_GROUPS)), nil +} + +func getPresent() (int, error) { + return int(windows.GetActiveProcessorCount(windows.ALL_PROCESSOR_GROUPS)), nil +}