Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
15 changes: 9 additions & 6 deletions numcpus.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -15,18 +15,21 @@
// 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.
//
// 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"
Expand All @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions numcpus_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
41 changes: 41 additions & 0 deletions numcpus_windows.go
Original file line number Diff line number Diff line change
@@ -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
}