Documentation
¶
Overview ¶
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, 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, Solaris/Illumos and Windows. ErrNotSupported is returned in case a function is not supported on a particular platform.
Index ¶
- Variables
- func GetConfigured() (int, error)
- func GetKernelMax() (int, error)
- func GetOffline() (int, error)
- func GetOnline() (int, error)
- func GetPossible() (int, error)
- func GetPresent() (int, error)
- func ListOffline() ([]int, error)
- func ListOnline() ([]int, error)
- func ListPossible() ([]int, error)
- func ListPresent() ([]int, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotSupported = errors.New("function not supported")
ErrNotSupported is the error returned when the function is not supported.
Functions ¶
func GetConfigured ¶ added in v0.3.0
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 GetKernelMax ¶
GetKernelMax returns the maximum number of CPUs allowed by the kernel configuration. This function is only supported on Linux and Windows systems.
func GetOffline ¶
GetOffline returns the number of offline CPUs, i.e. CPUs that are not online because they have been hotplugged off or exceed the limit of CPUs allowed by the kernel configuration (see GetKernelMax). This function is only supported on Linux systems.
Example ¶
package main
import (
"fmt"
"os"
"github.com/tklauser/numcpus"
)
func main() {
offline, err := numcpus.GetOffline()
if err != nil {
fmt.Fprintf(os.Stderr, "GetOffline: %v\n", err)
}
fmt.Printf("# of offline CPUs: %v\n", offline)
}
func GetOnline ¶
GetOnline returns the number of CPUs that are online and being scheduled.
Example ¶
package main
import (
"fmt"
"os"
"github.com/tklauser/numcpus"
)
func main() {
online, err := numcpus.GetOnline()
if err != nil {
fmt.Fprintf(os.Stderr, "GetOnline: %v\n", err)
}
fmt.Printf("# of online CPUs: %v\n", online)
}
func GetPossible ¶
GetPossible returns the number of possible CPUs, i.e. CPUs that have been allocated resources and can be brought online if they are present.
Example ¶
package main
import (
"fmt"
"os"
"github.com/tklauser/numcpus"
)
func main() {
possible, err := numcpus.GetPossible()
if err != nil {
fmt.Fprintf(os.Stderr, "GetPossible: %v\n", err)
}
fmt.Printf("# of possible CPUs: %v\n", possible)
}
func GetPresent ¶
GetPresent returns the number of CPUs present in the system.
Example ¶
package main
import (
"fmt"
"os"
"github.com/tklauser/numcpus"
)
func main() {
present, err := numcpus.GetPresent()
if err != nil {
fmt.Fprintf(os.Stderr, "GetPresent: %v\n", err)
}
fmt.Printf("# of present CPUs: %v\n", present)
}
func ListOffline ¶ added in v0.9.0
ListOffline returns the list of offline CPUs. See GetOffline for details on when a CPU is considered offline.
func ListOnline ¶ added in v0.9.0
ListOnline returns the list of CPUs that are online and being scheduled.
func ListPossible ¶ added in v0.9.0
ListPossible returns the list of possible CPUs. See GetPossible for details on when a CPU is considered possible.
func ListPresent ¶ added in v0.9.0
ListPresent returns the list of present CPUs. See GetPresent for details on when a CPU is considered present.
Types ¶
This section is empty.