loadprobe

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: AGPL-3.0 Imports: 19 Imported by: ? 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 loadprobe samples worker load metrics — Intel i915 GPU video-engine utilization or container CPU usage — and exposes a smoothed reading plus a fallback signal to callers gating admission decisions on observed load.

The package supports Linux only. Non-Linux builds return ErrUnsupportedPlatform from probe constructors so callers can surface a fallback signal at startup without special-casing the build target.

Probe values must lie in the closed interval [0, 1]; the Sampler clamps values slightly outside this range rather than rejecting them, so a transient negative value (e.g. clock skew on a perf delta) does not knock the sampler into fallback mode.

Index

Constants

This section is empty.

Variables

View Source
var ErrCgroupUnconstrained = errors.New("loadprobe: cgroup cpu quota unconstrained")

ErrCgroupUnconstrained is returned by NewCgroupProbe when the cgroup CPU quota is unset (cpu.max reads "max ..."). The supplier consuming this package is expected to fall back to its static cap when this error is surfaced.

View Source
var ErrUnsupportedPlatform = errors.New("loadprobe: platform unsupported")

ErrUnsupportedPlatform is returned by probe constructors when the target platform cannot be probed (non-Linux build, missing kernel feature, etc.).

Functions

This section is empty.

Types

type CgroupOptions

type CgroupOptions struct {
	// CgroupRoot is the directory holding cpu.max and cpu.stat. Defaults to
	// "/sys/fs/cgroup" (the unified cgroup v2 hierarchy mounted at its
	// canonical path inside containers).
	CgroupRoot string
}

CgroupOptions configures the cgroup v2 CPU probe.

type CgroupProbe

type CgroupProbe struct {
	// contains filtered or unexported fields
}

CgroupProbe samples container CPU utilization from cgroup v2 cpu.stat. It returns the rolling cpu.usage_usec delta divided by the allowed CPU bandwidth (quota_us × Δwall_us / period_us) — i.e. 1.0 when the workload is consuming its full quota.

func NewCgroupProbe

func NewCgroupProbe(opts CgroupOptions) (*CgroupProbe, error)

NewCgroupProbe binds a probe to the cgroup v2 hierarchy under opts.CgroupRoot. Returns ErrCgroupUnconstrained when cpu.max reports "max" (no quota set), and a wrapped filesystem error when cpu.max or cpu.stat are missing — both treated as init failures by callers.

func (*CgroupProbe) Close

func (p *CgroupProbe) Close() error

Close marks the probe closed. The cgroup probe holds no kernel resources; Close exists to satisfy the Probe interface.

func (*CgroupProbe) Sample

func (p *CgroupProbe) Sample(ctx context.Context) (float64, error)

Sample reads cpu.stat and returns the wall-time-normalized CPU utilization since the previous Sample. The first call seeds the reference state and returns 0.

type IntelOptions

type IntelOptions struct {
	// SysRoot is the filesystem root prepended to /sys paths. Defaults to
	// "/" when empty. Used by tests to inject fake sysfs layouts.
	SysRoot string
}

IntelOptions configures the i915 PMU probe.

type IntelProbe

type IntelProbe struct {
	// contains filtered or unexported fields
}

IntelProbe samples Intel i915 video-engine utilization via perf_event_open against the device-specific PMU. Each engine-busy counter increments by nanoseconds-of-engine-busy; Sample sums the deltas across the device's vcs* engines and divides by elapsed wall time.

func NewIntelProbe

func NewIntelProbe(devicePath string, opts IntelOptions) (*IntelProbe, error)

NewIntelProbe binds a probe to the i915 device backing devicePath (e.g. "/dev/dri/renderD128"). The probe is bound to the same physical device the worker transcodes against — on multi-i915 hosts the matching per-device PMU is selected via the device-path → PCI BDF mapping.

Returns an error wrapping the underlying cause when the PMU cannot be resolved, the kernel rejects the perf_event_open call (EACCES, ENOTSUPP, ENOENT), or no vcs* engines are exposed. Callers can fold the error into the same fallback surface as a mid-stream failure via Failed(err, log).

func (*IntelProbe) Close

func (p *IntelProbe) Close() error

Close releases all perf event fds. Safe to call multiple times.

func (*IntelProbe) Sample

func (p *IntelProbe) Sample(ctx context.Context) (float64, error)

Sample reads the perf counters, computes a wall-time-normalized utilization across all vcs engines, and clamps to [0, 1]. The first call seeds the reference state and returns 0.

type Probe

type Probe interface {
	// Sample returns the instantaneous load value, in the closed interval
	// [0, 1]. Implementations clamp at the boundary so callers can rely on
	// the bound; the Sampler also clamps as defense in depth.
	Sample(ctx context.Context) (float64, error)

	// Close releases any kernel resources held by the probe. After Close
	// returns, Sample must not be called.
	Close() error
}

Probe samples a single load metric. Each call computes a fresh utilization value relative to the elapsed wall time since the prior call.

Implementations that read counters and compute deltas must initialize their reference state on the first Sample so that the first returned value is meaningful (the convention in this package is to return 0 on the first sample and an actual delta-based reading from the second onward).

type Sampler

type Sampler struct {
	// contains filtered or unexported fields
}

Sampler runs a Probe in a background loop and exposes an EWMA-smoothed reading plus a fallback signal. The first sample failure (init or mid-stream) closes the channel returned by FailedC and stops the sampling loop; callers can observe the transition either by selecting on FailedC or by calling FailureReason.

Probe values outside [0, 1] are clamped before being mixed into the EWMA; out-of-range readings do not trigger fallback. Sample errors do.

func Failed

func Failed(reason error, log *slog.Logger) *Sampler

Failed returns a Sampler that is already in the failed state, with the given reason. Callers use this constructor when probe construction itself failed, so init failures and mid-stream failures share a single observation surface. Close on the returned sampler is a no-op.

A nil reason is replaced with a generic placeholder so FailureReason() is guaranteed non-nil whenever FailedC() is closed.

func NewSampler

func NewSampler(probe Probe, cfg SamplerConfig) *Sampler

NewSampler constructs a Sampler around probe. The sampling loop does not start until Start is called. A nil probe is treated as a construction failure: the returned sampler is already in the failed state with a descriptive reason, so callers don't need a separate nil check.

func (*Sampler) Close

func (s *Sampler) Close() error

Close stops the sampling loop and closes the underlying probe. Safe to call multiple times. After Close, subsequent Start calls are no-ops.

func (*Sampler) FailedC

func (s *Sampler) FailedC() <-chan struct{}

FailedC returns a channel that is closed when the sampler enters fallback mode. The name avoids colliding with the Failed package-level constructor.

func (*Sampler) FailureReason

func (s *Sampler) FailureReason() error

FailureReason returns the error that triggered fallback, or nil if the sampler has not failed.

func (*Sampler) Start

func (s *Sampler) Start(ctx context.Context)

Start launches the sampling goroutine. Repeated calls are no-ops, as are calls after Close. The goroutine exits when ctx is cancelled, when Close is called, or when the probe returns an error.

func (*Sampler) Value

func (s *Sampler) Value() float64

Value returns the current EWMA-smoothed reading, in the closed interval [0, 1]. Returns 0 before the first successful sample.

type SamplerConfig

type SamplerConfig struct {
	// Interval is the wall-time duration between probe samples. Defaults to
	// 500ms when zero.
	Interval time.Duration

	// SmoothingWindow is the number of samples used to derive the EWMA
	// smoothing factor (alpha = 2 / (SmoothingWindow + 1)). Defaults to 5
	// when zero or negative.
	SmoothingWindow int

	// Logger receives a warning when the sampler enters fallback mode. If
	// nil, slog.Default() is used.
	Logger *slog.Logger
}

SamplerConfig configures a Sampler.

Jump to

Keyboard shortcuts

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