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

procstats

package
v0.62.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package procstats samples what this install is costing the machine: the desktop process, the processes it parents (a terminal's shell, an agent), and the Go runtime's own view of itself.

The process numbers and the Go numbers answer different questions and are kept apart deliberately: RSS is what the OS charges for, while heap-in-use is what this program asked for, and on a cgo-heavy shell the gap between them is the native side — the part the Go runtime cannot see.

What the walk cannot reach: on macOS the WebKit processes that render the UI are XPC services launchd parents, not children of this process, and their only link back to the app is a responsible-pid the public API does not expose. They are excluded rather than guessed at — a WebContent process on the machine may well belong to another app.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GoRuntime

type GoRuntime struct {
	Goroutines     int     `json:"goroutines"`
	GOMAXPROCS     int     `json:"gomaxprocs"`
	NumCPU         int     `json:"numCpu"`
	HeapAllocBytes uint64  `json:"heapAllocBytes"`
	HeapSysBytes   uint64  `json:"heapSysBytes"`
	HeapObjects    uint64  `json:"heapObjects"`
	StackSysBytes  uint64  `json:"stackSysBytes"`
	TotalSysBytes  uint64  `json:"totalSysBytes"`
	NextGCBytes    uint64  `json:"nextGcBytes"`
	GCCount        uint32  `json:"gcCount"`
	LastGCUnixMs   int64   `json:"lastGcUnixMs"`
	LastPauseMs    float64 `json:"lastPauseMs"`
	TotalPauseMs   float64 `json:"totalPauseMs"`
}

GoRuntime is the Go runtime's own accounting for this process.

type Process

type Process struct {
	PID  int32  `json:"pid"`
	Name string `json:"name"`
	// RSSBytes is resident set size — the memory the OS is actually holding
	// for this process, which is the number Activity Monitor shows.
	RSSBytes uint64 `json:"rssBytes"`
	// CPUPercent is the share of one core used since the previous sample, so
	// it can exceed 100 on a process using several cores. The first sample for
	// a process reports its average since it started, there being nothing to
	// difference against yet.
	CPUPercent float64 `json:"cpuPercent"`
	Threads    int32   `json:"threads"`
}

Process is one sampled OS process.

type Sampler

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

Sampler holds the previous sample so CPU can be reported as a rate. It is safe for concurrent use; a UI polling it and a report assembling one at the same time must not race on the mark table.

func New

func New(pid int32) *Sampler

New returns a sampler for the current process, started now.

func (*Sampler) RegisterMetrics

func (s *Sampler) RegisterMetrics() metric.Registration

RegisterMetrics samples this process tree during each metric collection. The caller must unregister the returned callback when the sampler is no longer in use.

func (*Sampler) Sample

func (s *Sampler) Sample(ctx context.Context) (Stats, error)

Sample reads the process tree and the Go runtime. It returns an error only when this process cannot be read at all; a child that exits mid-walk is dropped from the sample rather than failing it, which is the common case on a machine doing anything.

type Stats

type Stats struct {
	SampledAt time.Time `json:"sampledAt"`
	UptimeMs  int64     `json:"uptimeMs"`
	Process   Process   `json:"process"`
	// Children is the process tree below this one, biggest first — the part a
	// Go-only view misses entirely.
	Children []Process `json:"children"`
	// ChildrenTruncated reports that the tree was larger than the walk's cap,
	// so the totals below are a floor rather than the whole picture.
	ChildrenTruncated bool      `json:"childrenTruncated"`
	Go                GoRuntime `json:"go"`
}

Stats is one sample: the app process, its descendants, and the Go runtime.

func (Stats) TotalCPUPercent

func (s Stats) TotalCPUPercent() float64

TotalCPUPercent is the whole tree's CPU share, this process included.

func (Stats) TotalRSSBytes

func (s Stats) TotalRSSBytes() uint64

TotalRSSBytes is the whole tree's resident memory, this process included.

Jump to

Keyboard shortcuts

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