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