resource

package
v0.36.1 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 8 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 loads an ONNX embedding model for memory indexing, 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.

Sampling runs only while at least one client is watching. Measuring CPU has a cost of its own, and there is no point paying it with no UI open.

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) ClearActivity

func (s *Sampler) ClearActivity()

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) SetActivity

func (s *Sampler) SetActivity(a Activity)

SetActivity labels what the process is busy with; ClearActivity removes the label once the work is done. Both are safe to call from any goroutine.

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
	Activity *Activity `json:"activity,omitempty"`
	Samples  []Sample  `json:"samples"`
}

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

Jump to

Keyboard shortcuts

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