Help us shape the future of Go! Take the 2026 Go Developer Survey

cpu

package
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package cpu reports the CPU quota the container this process runs in was given, for sizing work the Go runtime does not size for you.

Go 1.25 and later already derive GOMAXPROCS from the cgroup quota, so a Go worker pool scaled from runtime.GOMAXPROCS(0) is correct and this package adds nothing to it. What GOMAXPROCS cannot be used for is a thread count handed to an **external process**, because it rounds the quota up and never drops below 2. Measured on Go 1.27.2 under Docker on a 16-core host:

--cpus=0.5  GOMAXPROCS 2  (four times the quota)
--cpus=1.5  GOMAXPROCS 2
--cpus=3    GOMAXPROCS 3
--cpus=4.5  GOMAXPROCS 5

Over-subscribing the tightest quotas is the dangerous direction: the threads are throttled, they count against RLIMIT_NPROC, and an encoder that cannot start its threads exits having written nothing with a message naming no cause.

Use Count for the number to pass to ffmpeg's -threads, SVT-AV1's lp, VIPS_CONCURRENCY or any other pool outside the Go runtime, and Limit when the fraction itself matters.

Only Linux has cgroups; everywhere else Limit reports no quota and Count falls back to the machine's CPUs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count

func Count() int

Count is the number of CPUs a pool outside the Go runtime may use: the quota rounded **down**, at least 1, or the machine's CPUs when there is no quota.

Rounded down on purpose. A thread past the quota does not get more CPU — it gets throttled, and it still counts against RLIMIT_NPROC — while a thread fewer leaves at most a fraction of a core idle. GOMAXPROCS rounds the other way, which is why it is the wrong number to hand an external process.

func Limit

func Limit() (cpus float64, ok bool)

Limit returns the CPU quota of this container in CPUs: 2 for --cpus=2, 0.5 for --cpus=0.5. ok is false when there is no quota, which includes every non-Linux host and a Linux process outside a limited cgroup.

Prefer Count unless the fraction matters: a quota below one CPU is real, and a thread count has to be a whole number.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL