numcpus

package module
Version: v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2021 License: Apache-2.0 Imports: 6 Imported by: 1

README

numcpus

Go Reference GitHub Action Status Go Report Card

Package numcpus provides information about the number of CPU in a system.

It gets the number of CPUs (online, offline, present, possible, configured or kernel maximum) on a Linux, Darwin, FreeBSD, NetBSD, OpenBSD, DragonflyBSD or Solaris/Illumos system.

On Linux, the information is retrieved by reading the corresponding CPU topology files in /sys/devices/system/cpu.

Not all functions are supported on Darwin, FreeBSD, NetBSD, OpenBSD, DragonflyBSD and Solaris/Illumos.

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

Documentation

Overview

Package numcpus provides information about the number of CPU.

It gets the number of CPUs (online, offline, present, possible or kernel maximum) on a Linux, Darwin, FreeBSD, NetBSD, OpenBSD or DragonflyBSD system.

On Linux, the information is retrieved by reading the corresponding CPU topology files in /sys/devices/system/cpu.

Not all functions are supported on Darwin, FreeBSD, NetBSD, OpenBSD and DragonflyBSD.

Index

Examples

Constants

This section is empty.

Variables

View Source
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

func GetConfigured() (int, error)

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

func GetKernelMax() (int, error)

GetKernelMax returns the maximum number of CPUs allowed by the kernel configuration. This function is only supported on Linux systems.

func GetOffline

func GetOffline() (int, error)

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)
}
Output:

func GetOnline

func GetOnline() (int, error)

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)
}
Output:

func GetPossible

func GetPossible() (int, error)

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)
}
Output:

func GetPresent

func GetPresent() (int, error)

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)
}
Output:

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL