...
1# numcpus
2
3[](https://pkg.go.dev/github.com/tklauser/numcpus)
4[](https://github.com/tklauser/numcpus/actions?query=workflow%3ATests)
5
6Package numcpus provides information about the number of CPUs in the system.
7
8It gets the number of CPUs (online, offline, present, possible, configured or
9kernel maximum) on Linux, Darwin, FreeBSD, NetBSD, OpenBSD, DragonflyBSD or
10Solaris/Illumos systems.
11
12On Linux, the information is retrieved by reading the corresponding CPU
13topology files in `/sys/devices/system/cpu`.
14
15On BSD systems, the information is retrieved using the `hw.ncpu` and
16`hw.ncpuonline` sysctls, if supported.
17
18Not all functions are supported on Darwin, FreeBSD, NetBSD, OpenBSD,
19DragonflyBSD and Solaris/Illumos. ErrNotSupported is returned in case a
20function is not supported on a particular platform.
21
22## Usage
23
24```Go
25package main
26
27import (
28 "fmt"
29 "os"
30
31 "github.com/tklauser/numcpus"
32)
33
34func main() {
35 online, err := numcpus.GetOnline()
36 if err != nil {
37 fmt.Fprintf(os.Stderr, "GetOnline: %v\n", err)
38 }
39 fmt.Printf("online CPUs: %v\n", online)
40
41 possible, err := numcpus.GetPossible()
42 if err != nil {
43 fmt.Fprintf(os.Stderr, "GetPossible: %v\n", err)
44 }
45 fmt.Printf("possible CPUs: %v\n", possible)
46}
47```
48
49## References
50
51* [Linux kernel sysfs documentation for CPU attributes](https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-system-cpu)
52* [Linux kernel CPU topology documentation](https://www.kernel.org/doc/Documentation/cputopology.txt)
View as plain text