Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetConfigured #18

Merged
merged 1 commit into from
Aug 16, 2021
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
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