Help us shape the future of Go! Take the 2026 Go Developer Survey

resource

package
v0.57.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package resource samples this process's own resource usage — CPU, resident memory, Go runtime memory — so the UI can tell the user what ogcode actually costs on their machine.

The question is worth answering here because ogcode is not a thin CLI: it is a long-lived local daemon that links CGO parsers (MuPDF, tree-sitter) and spawns tool subprocesses. Most of that never shows up in Go's own heap numbers, and none of it is visible to the user without hunting through Activity Monitor.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Activity

type Activity struct {
	Label string `json:"label"`
	Done  int    `json:"done"`
	Total int    `json:"total"`
}

Activity names what the process is currently busy with. A spike in the graph with no explanation is worse than no graph at all — the user is left to guess whether their machine is being eaten by a bug — so long-running background work labels itself here and the UI can say what is going on.

type Sample

type Sample struct {
	At int64 `json:"at"` // unix milliseconds

	// RSS is the resident set size in bytes: what the OS actually holds in
	// physical memory for this process. It is the figure that matches Activity
	// Monitor and top, and it covers the CGO allocations (ONNX weights, MuPDF,
	// tree-sitter) that never appear in the Go numbers below.
	RSS uint64 `json:"rss"`

	// HeapInUse is the bytes of live heap objects; GoTotal is all memory the Go
	// runtime currently holds from the OS (heap, stacks, metadata) minus what it
	// has released back. Go returns memory lazily, so RSS routinely sits well
	// above HeapInUse after a spike — that divergence is normal rather than a
	// leak, which is why both are reported instead of one.
	HeapInUse uint64 `json:"heapInUse"`
	GoTotal   uint64 `json:"goTotal"`

	// CPUPercent is top-style: 100 means one core fully saturated, so on a
	// multi-core machine it can legitimately exceed 100.
	CPUPercent float64 `json:"cpuPercent"`

	Goroutines int `json:"goroutines"`
}

Sample is one measurement of the running process.

type Sampler

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

Sampler collects a rolling window of Samples on a fixed interval.

func NewSampler

func NewSampler(interval time.Duration, retain int) *Sampler

NewSampler returns a sampler retaining `retain` samples taken `interval` apart. It never fails: if the OS process handle is unavailable, the Go runtime numbers are still collected and RSS/CPU report zero.

func (*Sampler) AddWatcher

func (s *Sampler) AddWatcher()

AddWatcher and RemoveWatcher bracket a client that wants live samples. Sampling is suspended while the count is zero.

func (*Sampler) BeginTurn added in v0.50.0

func (s *Sampler) BeginTurn() time.Time

BeginTurn starts measuring a turn and returns the instant to hand back to EndTurn. Run suspends collection while nothing is watching, so a turn has to hold a watcher for its whole length or there is nothing to summarise.

func (*Sampler) EndTurn added in v0.50.0

func (s *Sampler) EndTurn(start time.Time) TurnStats

EndTurn releases the watcher BeginTurn took and folds the samples taken since into one summary. Concurrent turns are expected — the server shares a single runner — and each gets the window it overlapped, because these figures describe the whole process rather than any one turn's share of it.

func (*Sampler) Interval

func (s *Sampler) Interval() time.Duration

Interval is the gap between samples, so clients can size their own tick.

func (*Sampler) Latest

func (s *Sampler) Latest() (Sample, bool)

Latest returns the most recent sample, if any has been taken.

func (*Sampler) Meta

func (s *Sampler) Meta() Snapshot

Meta returns the reading context — cadence, core count, uptime — without copying the sample window, for callers that only need to frame a single sample.

func (*Sampler) RemoveWatcher

func (s *Sampler) RemoveWatcher()

func (*Sampler) Run

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

Run samples until ctx is cancelled.

func (*Sampler) Snapshot

func (s *Sampler) Snapshot() Snapshot

Snapshot returns Meta plus a copy of the retained window.

type Snapshot

type Snapshot struct {
	Interval int      `json:"interval"` // milliseconds between samples
	Cores    int      `json:"cores"`
	Uptime   int64    `json:"uptime"` // milliseconds since process start
	Samples  []Sample `json:"samples"`
}

Snapshot is the retained history plus the context needed to read it.

type TurnStats added in v0.50.0

type TurnStats struct {
	DurationMS int64 `json:"durationMs"`
	// CoveredMS is how much of the turn was actually measured. The sampler
	// keeps a four-minute window, so a longer turn reports only the samples
	// still retained, and a turn shorter than two ticks reports none.
	CoveredMS int64 `json:"coveredMs"`
	Samples   int   `json:"samples"`

	RSSMB     float64 `json:"rssMb"`     // last reading
	PeakRSSMB float64 `json:"peakRssMb"` // highest over the turn
	GoHeapMB  float64 `json:"goHeapMb"`  // last reading

	// CPUAvgPercent and CPUMaxPercent are top-style: 100 is one core fully
	// saturated, so both can legitimately exceed 100 on a multi-core machine.
	CPUAvgPercent float64 `json:"cpuAvgPercent"`
	CPUMaxPercent float64 `json:"cpuMaxPercent"`

	Goroutines     int `json:"goroutines"` // last reading
	PeakGoroutines int `json:"peakGoroutines"`

	Cores     int     `json:"cores"`
	HostRAMMB float64 `json:"hostRamMb"` // physical memory on this machine
}

TurnStats folds the samples taken during one turn into a single figure, for callers that want one row per turn rather than a time series. Memory is in megabytes, rounded to a tenth.

Jump to

Keyboard shortcuts

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