procusage

package
v1.19.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 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 procusage reports what a run cost this process in CPU and memory. Best-effort throughout: every reading degrades to zero rather than erroring, so nothing here can abort a scan.

This is the one place that reads OS resource counters directly instead of going through executor.Executor (AGENTS.md §2.1). The counters are in-process syscalls against our own PID, not spawned commands, so there is nothing for executor.Mock to intercept; tests swap the readUsage hook below instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendHistory

func AppendHistory(path string, rec Record) error

AppendHistory adds rec to the JSONL history at path, trimming to the newest maxHistoryRecords. An empty path is a no-op returning nil — paths.RunMetricsFile() is "" when the install dir is disabled.

Read-trim-rewrite rather than a bare append so the trim happens in the same atomic replace; the file is small enough that reading it per run costs nothing. It deliberately does not use atomicfile.WriteAtomic, which takes a .bak-<epoch> copy on every write — churn that makes sense for config.json and not for a file rewritten every run.

func CPUMillis

func CPUMillis() int64

CPUMillis is total CPU consumed so far, self plus children where they are attributed. Monotonic, so callers can difference it across phase boundaries — unlike peak RSS, which is a lifetime high-water mark and cannot be differenced.

func Report

func Report(log *progress.Logger, wall time.Duration, meta Meta, phases []Phase)

Report logs what the run cost and appends it to the local history. Both dispatch paths (community scan and enterprise telemetry) call this so the two produce comparable records.

Emitted at info, not debug: the enterprise ExecutionLogs payload carries whatever the run wrote to stderr at the configured level, and installs run at log_level=info, so a debug line would be absent from every log we can actually download.

Wholly best-effort — a failure here is logged at debug and never affects the run's outcome.

Types

type Meta

type Meta struct {
	Command          string
	InvocationMethod string
	ExecutionID      string
}

Meta identifies the run a Sample belongs to.

InvocationMethod and ExecutionID are enterprise-only: the community scan path has no execution ID, and detecting the invocation method costs a scheduler subprocess probe that isn't worth paying to label a local scan. Both are omitempty for that reason.

type Phase

type Phase struct {
	Name       string `json:"name"`
	DurationMs int64  `json:"duration_ms"`
	CPUMs      int64  `json:"cpu_ms,omitempty"`
}

Phase is one analysis phase's cost. Declared here rather than reusing telemetry.PhaseCompletion because telemetry imports this package.

type Record

type Record struct {
	Timestamp        time.Time `json:"ts"`
	AgentVersion     string    `json:"agent_version,omitempty"`
	Command          string    `json:"command,omitempty"`
	InvocationMethod string    `json:"invocation_method,omitempty"`
	OS               string    `json:"os,omitempty"`
	ExecutionID      string    `json:"execution_id,omitempty"`
	Usage            Sample    `json:"usage"`
	Phases           []Phase   `json:"phases,omitempty"`
}

Record is one line of run-metrics.jsonl: what the run was, plus what it cost.

func LoadHistory

func LoadHistory(path string) ([]Record, error)

LoadHistory reads the history back, skipping lines that don't parse so one truncated write never hides the rest of the file.

type Sample

type Sample struct {
	WallMs         int64 `json:"wall_ms"`
	CPUUserMs      int64 `json:"cpu_user_ms"`
	CPUSysMs       int64 `json:"cpu_sys_ms"`
	CPUChildUserMs int64 `json:"cpu_child_user_ms,omitempty"`
	CPUChildSysMs  int64 `json:"cpu_child_sys_ms,omitempty"`

	PeakRSSBytes uint64 `json:"peak_rss_bytes"`
	// MaxChildRSSBytes is the peak RSS of the single largest child, not a
	// sum over children — that is all getrusage(RUSAGE_CHILDREN) reports.
	MaxChildRSSBytes uint64 `json:"max_child_rss_bytes,omitempty"`

	// Go runtime accounting, not RSS: these read well below PeakRSSBytes
	// and are here to separate "our heap grew" from "a child was fat".
	GoHeapBytes uint64 `json:"go_heap_bytes"`
	GoSysBytes  uint64 `json:"go_sys_bytes"`
	Goroutines  int    `json:"goroutines"`
	NumGC       uint32 `json:"num_gc"`

	// LogicalCores is what CPU time has to be divided by to mean
	// anything. Recorded per sample because the history is compared
	// across machines with different core counts.
	LogicalCores int `json:"logical_cores,omitempty"`

	// Deliberately not omitempty: false is the meaningful value.
	ChildrenAttributed bool `json:"children_attributed"`
}

Sample is one resource reading for a completed run.

Child CPU is counted on Unix — executor.Run reaps subprocesses in-process, so getrusage attributes them to us — but not on Windows, which has no getrusage equivalent. ChildrenAttributed records which, so a Windows sample is never read as a Unix run that spawned nothing.

func Capture

func Capture(wall time.Duration) Sample

Capture takes a reading for a run that spanned wall.

func (Sample) CPUPercent

func (s Sample) CPUPercent() float64

CPUPercent is the share of the whole machine's CPU capacity the run consumed: CPU time over (wall time x cores). This is the "how much of the box did we eat" number, and it cannot exceed 100. Zero when wall time or the core count is unknown.

func (Sample) ChildCPUMs

func (s Sample) ChildCPUMs() int64

ChildCPUMs is CPU attributed to reaped subprocesses; always 0 where ChildrenAttributed is false.

func (Sample) CoresBusy

func (s Sample) CoresBusy() float64

CoresBusy is total CPU over wall time: the average number of cores the run kept busy. 1.0 means one core saturated for the whole run; above 1.0 means genuine parallelism. Zero when wall time is unknown.

Not expressed as a percentage on purpose — "96% of wall" reads as "used 96% of the CPU" when it actually meant 0.96 of one core, which on a 12-core box is 8% of the machine. Use CPUPercent for that.

func (Sample) SelfCPUMs

func (s Sample) SelfCPUMs() int64

SelfCPUMs is CPU attributed to this process alone.

func (Sample) String

func (s Sample) String() string

String renders the one-line summary logged at info.

func (Sample) TotalCPUMs

func (s Sample) TotalCPUMs() int64

TotalCPUMs is all CPU this run is known to have consumed.

Jump to

Keyboard shortcuts

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