Skip to content

Commit

Permalink
Add GetConfigured
Browse files Browse the repository at this point in the history
GetConfigured returns the number of configured CPUs in the system. This
matches what getconf _SC_NPROCESSORS_CONF returns on a Unix system.
  • Loading branch information
tklauser committed Aug 16, 2021
1 parent 55c926f commit 666abe6
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
[![GitHub Action Status](https://github.com/tklauser/numcpus/workflows/Tests/badge.svg)](https://github.com/tklauser/numcpus/actions?query=workflow%3ATests)
[![Go Report Card](https://goreportcard.com/badge/github.com/tklauser/numcpus)](https://goreportcard.com/report/github.com/tklauser/numcpus)

Package numcpus provides information about the number of CPU.
Package numcpus provides information about the number of CPU in a system.

It gets the number of CPUs (online, offline, present, possible or kernel
maximum) on a Linux, Darwin, FreeBSD, NetBSD, OpenBSD, DragonflyBSD or
It gets the number of CPUs (online, offline, present, possible, configured or
kernel maximum) on a Linux, Darwin, FreeBSD, NetBSD, OpenBSD, DragonflyBSD or
Solaris/Illumos system.

On Linux, the information is retrieved by reading the corresponding CPU
Expand Down Expand Up @@ -45,5 +45,5 @@ func main() {

## References

* [Linux kernel sysfs documenation for CPU attributes](https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-system-cpu)
* [Linux kernel sysfs documentation for CPU attributes](https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-system-cpu)
* [Linux kernel CPU topology documentation](https://www.kernel.org/doc/Documentation/cputopology.txt)
7 changes: 7 additions & 0 deletions numcpus.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ import "errors"
// ErrNotSupported is the error returned when the function is not supported.
var ErrNotSupported = errors.New("function not supported")

// GetConfigured returns the number of CPUs configured on the system. This
// function should return the same value as `getconf _SC_NPROCESSORS_CONF` on a
// unix system.
func GetConfigured() (int, error) {
return getConfigured()
}

// GetKernelMax returns the maximum number of CPUs allowed by the kernel
// configuration. This function is only supported on Linux systems.
func GetKernelMax() (int, error) {
Expand Down
5 changes: 5 additions & 0 deletions numcpus_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
"golang.org/x/sys/unix"
)

func getConfigured() (int, error) {
n, err := unix.SysctlUint32("hw.ncpu")
return int(n), err
}

func getKernelMax() (int, error) {
return 0, ErrNotSupported
}
Expand Down
23 changes: 23 additions & 0 deletions numcpus_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package numcpus

import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -55,6 +56,28 @@ func parseCPURange(cpus string) (int, error) {
return n, nil
}

func getConfigured() (int, error) {
d, err := os.Open(sysfsCPUBasePath)
if err != nil {
return 0, err
}
defer d.Close()
fis, err := d.Readdir(-1)
if err != nil {
return 0, err
}
count := 0
for _, fi := range fis {
if name := fi.Name(); fi.IsDir() && strings.HasPrefix(name, "cpu") {
_, err := strconv.ParseInt(name[3:], 10, 64)
if err == nil {
count++
}
}
}
return count, nil
}

func getKernelMax() (int, error) {
buf, err := ioutil.ReadFile(filepath.Join(sysfsCPUBasePath, "kernel_max"))
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions numcpus_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const (
_SC_NPROCESSORS_MAX = 516
)

func getConfigured() (int, error) {
n, err := unix.Sysconf(_SC_NPROCESSORS_CONF)
return int(n), err
}

func getKernelMax() (int, error) {
n, err := unix.Sysconf(_SC_NPROCESSORS_MAX)
return int(n), err
Expand Down
12 changes: 12 additions & 0 deletions numcpus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ func testGetconf(t *testing.T, got int, name, getconfWhich string) {
}
}

func TestGetConfigured(t *testing.T) {
n, err := numcpus.GetConfigured()
if errors.Is(err, numcpus.ErrNotSupported) {
t.Skipf("GetConfigured not supported on %s", runtime.GOOS)
} else if err != nil {
t.Fatalf("GetConfigured: %v", err)
}
t.Logf("Configured = %v", n)

testGetconf(t, n, "GetConfigured", "_NPROCESSORS_CONF")
}

func TestGetKernelMax(t *testing.T) {
n, err := numcpus.GetKernelMax()
if errors.Is(err, numcpus.ErrNotSupported) {
Expand Down
4 changes: 4 additions & 0 deletions numcpus_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package numcpus

func getConfigured() (int, error) {
return 0, ErrNotSupported
}

func getKernelMax() (int, error) {
return 0, ErrNotSupported
}
Expand Down

0 comments on commit 666abe6

Please sign in to comment.