procstats

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package procstats measures per-process CPU and RSS over a reset-to-snapshot interval using Linux procfs. The filesystem is injectable so parsing and interval behavior can be tested on every development platform.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collector

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

Collector owns a baseline and produces interval snapshots. Reset and Snapshot are serialized so a baseline cannot be changed mid-scan.

func New

func New(options ...Option) *Collector

New creates an idle collector. Call Reset immediately before the measured interval and Snapshot after it.

func (*Collector) Health

func (c *Collector) Health() Health

Health returns a copy of the most recent Reset or Snapshot health.

func (*Collector) Reset

func (c *Collector) Reset() error

Reset captures the interval baseline. Per-process failures are recorded in Health and do not fail Reset. A missing or malformed aggregate /proc/stat makes the interval unavailable and is returned to the caller.

func (*Collector) Snapshot

func (c *Collector) Snapshot() Snapshot

Snapshot reads the interval end and returns CPU and RSS top lists. Collection errors are reflected in Snapshot.Health instead of panicking or terminating the application.

type Health

type Health struct {
	Status  Status   `json:"status"`
	Partial bool     `json:"partial"`
	Dropped uint64   `json:"dropped"`
	Errors  []string `json:"errors,omitempty"`
}

Health describes collection failures without making the measured application fail. Errors is bounded; Dropped always contains the full count.

type Option

type Option func(*Collector)

Option configures a Collector.

func WithClock

func WithClock(now func() time.Time) Option

WithClock supplies wall time for interval metadata.

func WithClockTicks

func WithClockTicks(ticks uint64) Option

WithClockTicks sets USER_HZ used to convert process jiffies to seconds. Linux is normally 100; injection avoids a libc dependency and enables tests.

func WithFS

func WithFS(fsys fs.FS) Option

WithFS supplies a procfs-like filesystem. It is primarily intended for deterministic tests and non-Linux development hosts.

func WithPageSize

func WithPageSize(bytes uint64) Option

WithPageSize sets the byte size used for statm resident pages.

func WithProcRoot

func WithProcRoot(root string) Option

WithProcRoot selects a procfs root. The default is /proc.

func WithTopN

func WithTopN(n int) Option

WithTopN sets the maximum number of entries in each CPU and RSS list.

type Process

type Process struct {
	PID          int     `json:"pid"`
	Command      string  `json:"command"`
	CPUPercent   float64 `json:"cpuPercent"`
	CPUSeconds   float64 `json:"cpuSeconds"`
	RSSBytes     uint64  `json:"rssBytes"`
	DeltaJiffies uint64  `json:"deltaJiffies"`
	Starttime    uint64  `json:"starttime"`
	Appeared     bool    `json:"appeared,omitempty"`
	PIDReused    bool    `json:"pidReused,omitempty"`
}

Process is one process's reset-to-snapshot CPU delta and end-of-interval RSS.

type Snapshot

type Snapshot struct {
	StartedAt       time.Time `json:"startedAt"`
	EndedAt         time.Time `json:"endedAt"`
	IntervalJiffies uint64    `json:"intervalJiffies"`
	CPUs            int       `json:"cpus"`
	TopCPU          []Process `json:"topCPU"`
	TopRSS          []Process `json:"topRSS"`
	Health          Health    `json:"health"`
}

Snapshot is the process report for the current reset-to-snapshot interval. CPU percent follows top: one fully occupied core is 100%.

type Status

type Status string

Status summarizes whether a collector produced a complete interval.

const (
	StatusOK          Status = "ok"
	StatusPartial     Status = "partial"
	StatusUnavailable Status = "unavailable"
)

Jump to

Keyboard shortcuts

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