podman/vendor/github.com/tklauser/numcpus
Matt Heon d9c388e2fe Change to using gopsutil for cross-OS process ops
Instead of trying to write out own code to do basic process
operations (e.g. checking if a PID is still running in a multi-OS
friendly manner), use shirou/gopsutil, a multi-platform library
that should abstract all the complexity away. Unlike our previous
approach on Windows, this one should actually work.

Signed-off-by: Matt Heon <mheon@redhat.com>
2023-10-31 10:14:06 -04:00
..
.cirrus.yml Change to using gopsutil for cross-OS process ops 2023-10-31 10:14:06 -04:00
LICENSE Change to using gopsutil for cross-OS process ops 2023-10-31 10:14:06 -04:00
README.md Change to using gopsutil for cross-OS process ops 2023-10-31 10:14:06 -04:00
numcpus.go Change to using gopsutil for cross-OS process ops 2023-10-31 10:14:06 -04:00
numcpus_bsd.go Change to using gopsutil for cross-OS process ops 2023-10-31 10:14:06 -04:00
numcpus_linux.go Change to using gopsutil for cross-OS process ops 2023-10-31 10:14:06 -04:00
numcpus_solaris.go Change to using gopsutil for cross-OS process ops 2023-10-31 10:14:06 -04:00
numcpus_unsupported.go Change to using gopsutil for cross-OS process ops 2023-10-31 10:14:06 -04:00
numcpus_windows.go Change to using gopsutil for cross-OS process ops 2023-10-31 10:14:06 -04:00

README.md

numcpus

Go Reference GitHub Action Status

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

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("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