Documentation
¶
Overview ¶
Package governor is the resource-governance policy engine (SPEC §4.1, §10). It consumes idle/power signals, enforces the operator's serve policy, and guarantees instant-yield: on operator activity, in-flight requests are drained-or-cancelled within the configured grace window and the node marks itself YIELDED.
Index ¶
- Constants
- Variables
- type ErrNotServing
- type FakeIdleSource
- type FakePowerSource
- type FakeSleepInhibitor
- type Governor
- func (g *Governor) Admit(parent context.Context, id string) (context.Context, func(), error)
- func (g *Governor) AwakeReason() string
- func (g *Governor) Close()
- func (g *Governor) Inflight() int
- func (g *Governor) Policy() Policy
- func (g *Governor) Power() PowerStatus
- func (g *Governor) Run(ctx context.Context)
- func (g *Governor) SetEnrolled(enrolled bool)
- func (g *Governor) SetPolicy(p Policy)
- func (g *Governor) SetSleepInhibitor(i SleepInhibitor)
- func (g *Governor) State() State
- func (g *Governor) Subscribe() <-chan State
- type IdleSource
- type Policy
- type PowerSource
- type PowerStatus
- type SleepInhibitor
- type State
- type Window
Constants ¶
const ( AwakeReasonRequest = "Teraflock: inference request in flight" AwakeReasonServing = "Teraflock: serving the mesh" )
Reasons the governor holds the inhibitor for, as the OS shows them.
Variables ¶
var ErrNoIdleSource = errors.New("governor: idle detection not implemented on this platform")
ErrNoIdleSource is returned by stub platform sources.
var ErrNoSleepInhibitor = errors.New("governor: sleep inhibitor not implemented on this platform")
ErrNoSleepInhibitor is what the stub platform inhibitor returns from Hold: the governor logs it once and carries on without an assertion.
Functions ¶
This section is empty.
Types ¶
type ErrNotServing ¶
type ErrNotServing struct{ State State }
ErrNotServing is returned by Admit when the governor refuses work.
func (ErrNotServing) Error ¶
func (e ErrNotServing) Error() string
type FakeIdleSource ¶
type FakeIdleSource struct {
// contains filtered or unexported fields
}
FakeIdleSource / FakePowerSource are shared by tests here and in other packages (localapi, tunnel) that need a controllable governor.
func (*FakeIdleSource) Fail ¶
func (f *FakeIdleSource) Fail(err error)
func (*FakeIdleSource) Set ¶
func (f *FakeIdleSource) Set(d time.Duration)
type FakePowerSource ¶
type FakePowerSource struct {
// contains filtered or unexported fields
}
func (*FakePowerSource) Set ¶
func (f *FakePowerSource) Set(ps PowerStatus)
func (*FakePowerSource) Status ¶
func (f *FakePowerSource) Status(context.Context) (PowerStatus, error)
type FakeSleepInhibitor ¶ added in v0.9.0
type FakeSleepInhibitor struct {
// contains filtered or unexported fields
}
FakeSleepInhibitor records holds and releases for tests (here and in packages that build a governor with one).
func (*FakeSleepInhibitor) Counts ¶ added in v0.9.0
func (f *FakeSleepInhibitor) Counts() (holds []string, releases int)
Counts reports every reason Hold was called with, in order, and how many times Release was.
func (*FakeSleepInhibitor) Fail ¶ added in v0.9.0
func (f *FakeSleepInhibitor) Fail(err error)
Fail makes the next Holds return err (nil clears it).
func (*FakeSleepInhibitor) Held ¶ added in v0.9.0
func (f *FakeSleepInhibitor) Held() (bool, string)
Held reports whether the fake is held and with which reason.
func (*FakeSleepInhibitor) Hold ¶ added in v0.9.0
func (f *FakeSleepInhibitor) Hold(reason string) error
func (*FakeSleepInhibitor) Release ¶ added in v0.9.0
func (f *FakeSleepInhibitor) Release() error
type Governor ¶
type Governor struct {
// contains filtered or unexported fields
}
Governor evaluates Policy against live signals.
func New ¶
func New(p Policy, idle IdleSource, power PowerSource, clk clock.Clock, log *slog.Logger) *Governor
New builds a Governor. clk may be nil (wall clock).
func (*Governor) Admit ¶
Admit gates one request. On success it returns a request context that the governor cancels if the yield grace expires, plus a release func the caller MUST invoke when the request finishes (success or not).
func (*Governor) AwakeReason ¶ added in v0.9.0
AwakeReason reports why the sleep assertion is held ("" when it is not), for status output and tests.
func (*Governor) Close ¶ added in v0.9.0
func (g *Governor) Close()
Close releases the sleep assertion for good: later syncs are no-ops. Called on daemon exit; stop/exit must never leave the machine insomniac.
func (*Governor) Power ¶
func (g *Governor) Power() PowerStatus
Power returns the last observed power status (for heartbeats).
func (*Governor) SetEnrolled ¶ added in v0.9.0
SetEnrolled tells the governor whether the node is part of a mesh. An enrolled node that is serving stays awake so placements can reach it ("serve the mesh while you sleep" means the operator sleeps, not the machine); an unenrolled or standalone daemon only stays awake for the request it is answering right now.
func (*Governor) SetPolicy ¶
SetPolicy swaps the policy at runtime (`tera limits` / PUT /api/v1/limits). The next evaluate() applies it.
func (*Governor) SetSleepInhibitor ¶ added in v0.9.0
func (g *Governor) SetSleepInhibitor(i SleepInhibitor)
SetSleepInhibitor installs the platform inhibitor (nil = none: the OS sleeps whenever it likes, which is the pre-v0.7 behaviour). Call it before Run; the first sync happens on the next transition.
type IdleSource ¶
IdleSource reports how long user input has been quiet. Platform implementations live in idle_*.go; tests use FakeIdleSource.
func NewPlatformIdleSource ¶
func NewPlatformIdleSource() IdleSource
NewPlatformIdleSource returns the Linux idle source: systemd-logind's IdleHint / IdleSinceHint / LockedHint for the operator's seat session, read through `loginctl show-session`. The session is $XDG_SESSION_ID when the daemon runs inside a login session, else the first active seat session from `loginctl list-sessions` (a systemd --user service has no XDG_SESSION_ID). No logind, or no seat session at all (headless), is ErrNoIdleSource: the governor assumes idle with a one-time log line, which is the right default for servers.
Caveat: IdleHint is set by the desktop session (GNOME, KDE, sway with an idle daemon…); a bare X session without one leaves it at "no", so the node only counts as idle there while the screen is locked (LockedHint, set by any locker that tells logind — loginctl lock-session, GNOME, KDE, swaylock via swayidle…). Without either, serve_policy = always or scheduled is the operator's answer.
type Policy ¶
type Policy struct {
Serve string // always|idle-only|scheduled
IdleAfter time.Duration
YieldGrace time.Duration // drain-or-cancel window, default 2s
PollInterval time.Duration
ServeOnBattery bool
MaxTempCelsius float64
Schedule []Window
// KeepAwake lets the governor hold a system-sleep assertion while a
// request is in flight or while serving and enrolled (docs/sleep.md).
// False = flockd never touches power management.
KeepAwake bool
}
Policy is the operator's resource-governance configuration.
type PowerSource ¶
type PowerSource interface {
Status(ctx context.Context) (PowerStatus, error)
}
PowerSource reports battery/thermal state.
func NewPlatformPowerSource ¶
func NewPlatformPowerSource() PowerSource
NewPlatformPowerSource reads /sys/class/power_supply and thermal zones.
type PowerStatus ¶
PowerStatus is a battery/thermal snapshot. TempCelsius == 0 means unknown.
type SleepInhibitor ¶ added in v0.9.0
type SleepInhibitor interface {
// Hold takes the assertion, tagged with a reason the operator can read
// in the OS's own tooling (powercfg /requests, pmset -g assertions,
// systemd-inhibit --list). Holding an already-held inhibitor is a
// no-op; the first reason stays.
Hold(reason string) error
// Release drops the assertion. Releasing an unheld inhibitor is a
// no-op.
Release() error
}
SleepInhibitor keeps the operating system from going to sleep on its own while held. It is the one place flockd touches power management, and it is deliberately narrow: an inhibitor stops the *automatic idle* sleep timer only. It never keeps the display on, never blocks a sleep the operator asks for (lid, menu, power button), and never wakes the machine. Platform implementations live in power_*.go; the Governor decides when to hold it (see Governor.syncAwake) and tests use FakeSleepInhibitor.
func NewPlatformSleepInhibitor ¶ added in v0.9.0
func NewPlatformSleepInhibitor(log *slog.Logger) SleepInhibitor
NewPlatformSleepInhibitor returns the Linux inhibitor: a logind `--what=idle` inhibitor lock, taken through `systemd-inhibit` the way the idle source reads logind through `loginctl` (no D-Bus client library). `idle` is the one logind inhibitor class that matches the policy: it stops the *automatic* idle action (suspend after N minutes of no input) and nothing else — `sleep` would also block or delay a suspend the operator asks for, which the policy forbids. The lock is held while the child runs; the child is `tail --pid=<flockd pid>`, so it ends on its own if flockd dies without releasing. `systemd-inhibit --list` shows it as flockd with the reason. Desktops that consult only their own session inhibitors (not logind's) may sleep anyway; see docs/sleep.md.
type State ¶
type State int
State is the governor's serving decision.
const ( // StateServing: the node accepts and runs inference. StateServing State = iota // StateYielded: the operator is using the machine (idle-only policy). StateYielded // StatePausedBattery: on battery and serve_on_battery=false. StatePausedBattery // StatePausedThermal: above the temperature ceiling. StatePausedThermal // StateOutsideSchedule: serve_policy=scheduled and now is out of window. StateOutsideSchedule )
type Window ¶
Window is a daily serving window in local time. Overnight windows (start > end, e.g. 22:00-08:00) wrap across midnight.
func ParseWindows ¶
ParseWindows parses a config list like ["22:00-08:00", "12:00-13:00"].