machineload

package
v0.14.2 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package machineload reads the machine's load and process table and decides, from one snapshot, whether programs outside the running work are keeping the machine busy (itd-2609231434459890, spc-2609231542463113).

It is a leaf: it imports only the standard library, so the one new primitive the load check needs brings no dependency with it. Everything that decides is a pure function of a Snapshot, so a two-day burner is a fixture value, never a process a test has to leave running. The per-platform readers only fill a Snapshot: macOS through sysctl and /bin/ps, Linux through /proc alone, and every other platform reports ErrUnsupported so the caller can say it could not check (read_other.go).

Nothing here writes to stdout, signals a process or writes a file.

Index

Constants

View Source
const (
	TriggerStray   = "stray"
	TriggerExtreme = "extreme"
)

The two triggers.

View Source
const (
	// RemedyGroup: `pgrep -g <pgid>`, then `kill -- -<pgid>`.
	RemedyGroup = "group"
	// RemedyProcess: `ps -o pid=,comm= -p <pid>`, then `kill <pid>`.
	RemedyProcess = "process"

	// WhyMixedGroup: the stray's group holds a program the warning does not name.
	WhyMixedGroup = "mixed-group"
	// WhyCallersGroup: the stray's group is the check's own, or an ancestor's.
	WhyCallersGroup = "callers-group"
)

The two remedy forms, and why a stray gets the process form.

View Source
const (
	KeyStrayMinutes = "stray-minutes"
	KeyExtremeLoad  = "extreme-load"
	KeyWaitLoad     = "wait-load"

	// DefaultStrayMinutes is how long a process may run at nearly all the CPU it
	// could get before it reads as a stray.
	DefaultStrayMinutes = 30
	// DefaultExtremeFactor times the online core count is the extreme load.
	DefaultExtremeFactor = 4
	// DefaultWaitFactor times the online core count is the waiter's limit: the
	// waiter opens only while the one-minute load is strictly below twice the
	// core count. Once was too low: the baseline load of a busy multi-session
	// machine already sits around one core-count, so a limit of the cores kept
	// every waiter shut for hours (between 20 and 100 on 16 cores, 2026-10-11)
	// and sent sessions back to hand-written loops. Serialising the work is the
	// job of the race-run condition and the hold, not of the load number. One
	// race run adds about a core-count of load, so admitting below twice the
	// cores bounds the peak near three times them, under the extreme limit's
	// DefaultExtremeFactor of four.
	DefaultWaitFactor = 2
)

The three keys, and their bounds.

View Source
const GoProgram = "go"

GoProgram is the base name of the go command's executable, as the process table names it (Proc.Name, the kernel's record of what was executed, never a word of a command line).

View Source
const LimitsFileName = "load-limits"

LimitsFileName is the settings file's name under ~/.abcd.noindex/.

View Source
const MaxOwnNamed = 20

MaxOwnNamed caps how many of the caller's own strays a warning names; the rest are counted.

View Source
const NearFullShare = 0.9

NearFullShare is the part of its fair share (FairShare) at which a process is using nearly all the CPU it could get: on a machine loaded no higher than its cores, 0.9 of a core.

Variables

View Source
var ErrUnsupported = errors.New("machineload: this platform's load and process table are not read")

ErrUnsupported is returned by Read on a platform the readers do not cover.

Functions

func Descends added in v0.14.0

func Descends(procs []Proc, pid, ancestor int) bool

Descends reports whether pid is a descendant of ancestor (a process is not its own descendant).

func FairShare added in v0.11.0

func FairShare(snap Snapshot) float64

FairShare is the CPU, in cores, one runnable process can get on the machine as the snapshot finds it loaded: the online cores divided by the one-minute load, the runnable demand, and never more than one core. With no load reading, no core count, or a load no higher than the cores, it is one core. Forty busy loops on 16 cores each get 0.4 of a core, and each uses all of it.

func GateCandidates added in v0.14.1

func GateCandidates(snap Snapshot, self Self) []int

GateCandidates returns, in pid order, the processes the gate asks for their arguments: those executing go (a race run), make (a preflight) or abcd (a waiter), outside self's process and its ancestors, and the ancestors executing abcd, so a waiter can tell that an ancestor is a waiter running the command this one is part of.

func GoTestParallelism added in v0.14.1

func GoTestParallelism(snap Snapshot) int

GoTestParallelism is the -p a `go test` run started now should take, so one run cannot saturate the machine (iss-2610102020442624): the cores the machine can still give, which is twice the online cores (the waiter's default limit, DefaultWaitFactor) less the one-minute load rounded up, never more than the cores (go's own default) and never fewer than a quarter of them, or one, so a run on a loaded machine still makes progress. An unreadable load leaves go's default of the cores.

func ParseLimits

func ParseLimits(data []byte, cores int) (Limits, *LimitsFault)

ParseLimits reads a settings file's bytes. Any fault makes the whole file unusable: it returns the defaults for BOTH limits, never a mix of the file's and the defaults, with the fault.

func RaceRuns added in v0.14.0

func RaceRuns(snap Snapshot, self Self, args map[int][]string) []int

RaceRuns returns, in pid order, the candidates whose arguments (args, keyed by pid, the program name dropped) start a race run. A candidate with no arguments read (gone by the time they were asked for) is not one.

func ReadArgs added in v0.14.0

func ReadArgs(pids []int) (map[int][]string, error)

ReadArgs reads the exact argv of each of pids, the program name dropped, from /proc/<pid>/cmdline. A pid that has exited is absent from the result.

Types

type Ahead added in v0.14.1

type Ahead struct {
	// Preflights are the make preflights running, not queued at their gate.
	Preflights []int
	// Waiters are the waiters queued ahead of this one (a lower pid) or
	// running the command they gate.
	Waiters []int
}

Ahead is what the process table holds ahead of a waiter at the gate.

func QueueAhead added in v0.14.1

func QueueAhead(snap Snapshot, self Self, args map[int][]string) Ahead

QueueAhead reads what is ahead of self at the gate from one snapshot and the candidates' arguments (args, keyed by pid, the program name dropped). A waiter with a child other than ps is running its command. A preflight with a waiter beneath it that is not running a command is queued at its gate, and that waiter is its place in the queue. A waiter beneath a waiter running its command takes that ancestor's place, ahead of every queued waiter.

type Limits

type Limits struct {
	// StrayMinutes: a process older than this at nearly all the CPU it could get
	// is a stray.
	StrayMinutes int
	// ExtremeLoad: a one-minute load average strictly above this is extreme. It is
	// an absolute load, in the online-core unit the LOAD rule's cap uses.
	ExtremeLoad float64
	// StrayFromFile and ExtremeFromFile say which limits the settings file set.
	StrayFromFile   bool
	ExtremeFromFile bool
	// WaitLoad: the waiter (`implement load --wait`) opens only while the
	// one-minute load average is strictly below this.
	WaitLoad     float64
	WaitFromFile bool
}

Limits are the two limits the classifier applies, and the waiter's.

func DefaultLimits

func DefaultLimits(cores int) Limits

DefaultLimits derives the defaults from the online core count: 30 minutes, four times the cores, and twice the cores.

type LimitsFault

type LimitsFault struct {
	Line  int
	Class string
}

LimitsFault is why a settings file is unusable. It names the line (zero when the fault is the file's, not a line's) and the fault class, and never a value: a value is whatever the caller typed, and the report is printed and logged.

func (*LimitsFault) Error

func (f *LimitsFault) Error() string

type OtherStrays

type OtherStrays struct {
	Count int
	Cores float64
}

OtherStrays is every other account's strays: how many, and how many cores they use between them. The type has no field that could hold a name, a command line, a uid or an account, so no renderer can leak one (TestOtherStraysTypeCarriesNoIdentity).

type OwnStray

type OwnStray struct {
	Name  string
	PID   int
	PGID  int
	Age   time.Duration
	Share float64
}

OwnStray is one of the caller's own strays, named in full.

type Proc

type Proc struct {
	PID  int
	PPID int
	PGID int
	// UID is the effective uid.
	UID uint32
	// Age is how long the process has existed.
	Age time.Duration
	// CPU is the process's cumulative user plus system CPU time.
	CPU time.Duration
	// Name is the executable's base name: never a path, never a command line.
	Name string
}

Proc is one process as the check sees it.

func (Proc) Share

func (p Proc) Share() float64

Share is the process's lifetime CPU share in cores: cumulative CPU time over age, so 1.0 is one core flat out since it started. A multi-threaded process can exceed 1.0. A process with no measurable age has no share.

type Remedy

type Remedy struct {
	// Form is RemedyGroup or RemedyProcess.
	Form string
	// PGID and PIDs are the group form's group and the strays it holds.
	PGID int
	PIDs []int
	// PID and Why are the process form's process and why it is not the group form.
	PID int
	Why string
}

Remedy is one way to stop some of the caller's own strays. It is a plan for the caller to carry out; nothing here signals anything.

type Self

type Self struct {
	PID int
	UID uint32
}

Self is the check's own invocation: its pid, whose parent chain is exempt, and its effective uid, which decides whose strays are the caller's own.

type Snapshot

type Snapshot struct {
	// Load1, Load5 and Load15 are the load averages over one, five and fifteen
	// minutes. They are meaningful only when HasLoad is true.
	Load1, Load5, Load15 float64
	HasLoad              bool
	// Cores is the online core count, the unit the load averages are read in.
	Cores int
	// CoresFallback is true when the online count could not be read and Cores is
	// runtime.NumCPU's count instead.
	CoresFallback bool
	// Procs is the process table. It is meaningful only when HasProcs is true: a
	// load average that reads while the process table does not is still a reading.
	Procs    []Proc
	HasProcs bool
}

Snapshot is one reading of the machine.

func Read

func Read() (Snapshot, error)

Read reads this Linux machine from /proc and /sys alone, so a minimal or missing ps does not matter: the load averages from /proc/loadavg, the online core count from /sys/devices/system/cpu/online (what glibc's sysconf(_SC_NPROCESSORS_ONLN) reads; runtime.NumCPU counts the affinity mask and can be smaller), and every numeric /proc/<pid>. A load average that reads while the process table does not is returned with the error, so the extreme trigger can still apply.

type Verdict

type Verdict struct {
	// Triggers holds TriggerStray and TriggerExtreme, in that order, when they
	// fire; empty is nothing to warn about.
	Triggers []string
	// Own is the caller's own strays, at most MaxOwnNamed, by pid; OwnMore counts
	// the rest.
	Own     []OwnStray
	OwnMore int
	// Others is every other account's strays, anonymously.
	Others OtherStrays
	// Remedy covers every stray in Own.
	Remedy []Remedy
}

Verdict is what one snapshot says.

func Classify

func Classify(snap Snapshot, lim Limits, self Self) Verdict

Classify decides what a snapshot says under the limits, for the invocation self. It is a pure function.

A process is a stray when it is older than the stray limit, its lifetime share is at least NearFullShare of its FairShare, and it is not the invocation or one of its ancestors: busy for its share, it uses nearly all the CPU it could get on the machine as loaded, so forty loops each at a fortieth of the machine are all strays, as one loop at a full core of an idle machine is (the product thinker's ruling H1 of 2026-09-25). The one snapshot decides; there is no second sample. The share test applies before the uid split, so the caller's own processes and every other account's are judged alike. Nothing is exempt by name: abcd's own lanes are exempt by time, because everything they start (go, compile, link, vet, the package test binaries) lives for seconds to minutes, so a hung test binary reads as a stray, as the intent's third scope condition says it must. A stray whose effective uid is the caller's is the caller's own; any other uid, root included, is another account's.

The extreme trigger fires when the one-minute load average is strictly above the extreme limit. The five- and fifteen-minute averages remember work that finished long ago, so they are reported and never decide.

Jump to

Keyboard shortcuts

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