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 ¶
const ( TriggerStray = "stray" TriggerExtreme = "extreme" )
The two triggers.
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.
const ( KeyStrayMinutes = "stray-minutes" KeyExtremeLoad = "extreme-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 )
The two keys, and their bounds.
const LimitsFileName = "load-limits"
LimitsFileName is the settings file's name under ~/.abcd/.
const MaxOwnNamed = 20
MaxOwnNamed caps how many of the caller's own strays a warning names; the rest are counted.
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 ¶
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 FairShare ¶ added in v0.11.0
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 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.
Types ¶
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
}
Limits are the two limits the classifier applies.
func DefaultLimits ¶
DefaultLimits derives the defaults from the online core count: 30 minutes, and four times the cores.
type LimitsFault ¶
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 ¶
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 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.
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 ¶
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 ¶
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 ¶
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.