Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tklauser/numcpus
Go package providing information about the number of CPUs in the system
https://github.com/tklauser/numcpus
bsd cpu cputopology go golang linux numa offline online unix
Last synced: 3 days ago
JSON representation
Go package providing information about the number of CPUs in the system
- Host: GitHub
- URL: https://github.com/tklauser/numcpus
- Owner: tklauser
- License: apache-2.0
- Created: 2018-12-13T16:31:43.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2025-01-21T10:08:13.000Z (16 days ago)
- Last Synced: 2025-01-25T23:41:12.139Z (12 days ago)
- Topics: bsd, cpu, cputopology, go, golang, linux, numa, offline, online, unix
- Language: Go
- Homepage: https://pkg.go.dev/github.com/tklauser/numcpus
- Size: 1.03 MB
- Stars: 26
- Watchers: 3
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# numcpus
[![Go Reference](https://pkg.go.dev/badge/github.com/tklauser/numcpus.svg)](https://pkg.go.dev/github.com/tklauser/numcpus)
[![GitHub Action Status](https://github.com/tklauser/numcpus/workflows/Tests/badge.svg)](https://github.com/tklauser/numcpus/actions?query=workflow%3ATests)Package numcpus provides information about the number of CPUs in the system.
It gets the number of CPUs (online, offline, present, possible, configured or
kernel maximum) on Linux, Darwin, FreeBSD, NetBSD, OpenBSD, DragonflyBSD or
Solaris/Illumos 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.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.## Usage
```Go
package mainimport (
"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("online CPUs: %v\n", online)possible, err := numcpus.GetPossible()
if err != nil {
fmt.Fprintf(os.Stderr, "GetPossible: %v\n", err)
}
fmt.Printf("possible CPUs: %v\n", possible)
}
```## References
* [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)