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
- Variables
- func Descends(procs []Proc, pid, ancestor int) bool
- func FairShare(snap Snapshot) float64
- func GateCandidates(snap Snapshot, self Self) []int
- func GoTestParallelism(snap Snapshot, slots int) int
- func IsRaceCommand(cmd []string) bool
- func JustOpened(snap Snapshot, self Self, args map[int][]string, window time.Duration, ...) []int
- func ParseLimits(data []byte, cores int) (Limits, *LimitsFault)
- func RaceAhead(snap Snapshot, self Self, args map[int][]string) []int
- func RaceCommandWaiters(snap Snapshot, self Self, args map[int][]string) []int
- func RaceRuns(snap Snapshot, self Self, args map[int][]string) []int
- func ReadArgs(pids []int) (map[int][]string, error)
- type Ahead
- type Limits
- type LimitsFault
- type OtherStrays
- type OwnStray
- type Proc
- type Remedy
- type Self
- type Snapshot
- type Verdict
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" 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.
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).
const LimitsFileName = "load-limits"
LimitsFileName is the settings file's name under ~/.abcd.noindex/.
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.
const StartTolerance = 2
StartTolerance is how far, in whole seconds, two start times read from the process table may differ and still be one moment as far as the table can tell: a start time is the read's moment less the process's age, and macOS reports an age in whole seconds and Linux in clock ticks against an uptime read a moment apart, so one process read twice can differ by a second. The session-name register (sessionname) judges a recorded host by it, and the load gate's queue (QueueAhead) orders two waiters within it by pid instead.
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 Descends ¶ added in v0.14.0
Descends reports whether pid is a descendant of ancestor (a process is not its own descendant).
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 GateCandidates ¶ added in v0.14.1
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
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 divided among the slots preflights may be in their test stages at once (load.preflights; zero or less reads as one, and one is go's own default of the cores) and never fewer than a quarter of the cores, or one, so a run on a loaded machine still makes progress. With two slots, two concurrent stages each take at most half the cores: the load a second stage starts under lags the first stage's real demand by up to a minute, so the load alone would give each the whole machine. An unreadable load leaves the cores divided among the slots.
func IsRaceCommand ¶ added in v0.15.1
IsRaceCommand reports whether cmd, a command a waiter gates (the words after `--`, its program first), is a race run: its program is go and its go command is `test` with -race, read as a race run's arguments are (isRaceTest). A shell or a make that runs one is not read into.
func JustOpened ¶ added in v0.15.1
func JustOpened(snap Snapshot, self Self, args map[int][]string, window time.Duration, race bool) []int
JustOpened returns, in pid order, the waiters at one gate, the race gate's when race is set and the first gate's otherwise, outside self's process and its ancestors, that are running their command and opened within window of the snapshot: a waiter with a child other than ps is running its command, and it opened when its oldest such child started. An opener reads it just before it starts its command (implement.WaitLoad): two waiters whose start and pid disagree wait on neither, and across accounts no hold keeps them from opening in one moment, so each looks once more, and the one with the higher pid stands back from the other. A waiter at the other gate did not open beside this one: a race waiter's preflight passed its first gate earlier, and a first gate's opener never stands back from it, as an abcd released before the race gate, which reads no race waiter at all, does not. A child under a second old on macOS reads an age of zero, which is within any window.
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 RaceAhead ¶ added in v0.15.1
RaceAhead returns, in pid order, the race waiters (isRaceWaiter) ahead of self at the race gate: those queued ahead of it, by the rule the first gate's queue keeps (ahead by start and with the lower pid, queuedAhead), and those running their command, outside self's process and its ancestors. A waiter at a first gate, and a preflight in its unit stage, are not ahead of it there; the race runs that shut it are RaceRuns.
func RaceCommandWaiters ¶ added in v0.15.1
RaceCommandWaiters returns, in pid order, the first gate's waiters outside self's process and its ancestors that are running a command that is a race run (`--wait -- go test -race`, IsRaceCommand). Such a waiter is a race run for both gates from the moment it opens: its mark shows it running before the go test it starts is in the table, and once that go test is, RaceRuns names it too. A queued one is not.
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 (ahead by start, earlier
// or in the same tie group, and with a lower pid; queueOrder) or running
// the command they gate, in pid order.
Waiters []int
// Busy is the work in its test stages: each preflight in Preflights and
// each waiter in Waiters running its command, counted once where one runs
// beneath the other (`--wait -- make preflight` is one unit), in pid order.
Busy []int
// Queued is the waiters in Waiters queued ahead, not running a command.
Queued []int
}
Ahead is what the process table holds ahead of a waiter at the gate.
func QueueAhead ¶ added in v0.14.1
QueueAhead reads what is ahead of self at the first 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 first gate's 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. A race waiter (isRaceWaiter) is never in this queue: it waits at the race gate (RaceAhead), and a preflight whose waiter is one has passed its first gate and is in its test stages.
func (Ahead) Shut ¶ added in v0.15.1
Shut reports which of the queue's conditions hold the first gate shut when slots units of work may be in their test stages at once (the load.preflights setting; zero or less reads as one): every unit in Busy takes a slot, and the gate is shut on preflight or on waiter once they are all taken, by the kind of unit that takes them. A waiter queued ahead shuts it whatever slots is, so a free slot never lets a waiter jump the queue. With one slot this is the gate as it read before slots existed: any running preflight shuts it on preflight, and any waiter ahead or running its command on 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 ¶
DefaultLimits derives the defaults from the online core count: 30 minutes, four times the cores, and twice 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.