procstat

package
v0.178.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 14 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 procstat reads the machine's process table and what each process costs — CPU and resident memory — on macOS, Linux and Windows.

It only reads. Nothing here signals, opens or writes to another process; rule 7 forbids signalling a process Caprock did not start (the doctor's confirmed stop, ADR-059, lives in internal/daemon), and reading the figures a process manager shows is not that.

The table comes from one call where the OS has one (sysctl kern.proc.all on macOS) and from gopsutil elsewhere; per-process CPU time and resident size come from gopsutil on every OS. CPU is a percentage of one core between two samples, so a process tree using three cores reads 300%.

Index

Constants

View Source
const (
	PressureNormal   = "normal"
	PressureWarn     = "warn"
	PressureCritical = "critical"
)

Pressure levels, as the OS reports them. "" is a machine whose OS keeps no such signal (Windows) or would not give it.

Variables

This section is empty.

Functions

func Cmdline

func Cmdline(ctx context.Context, pid int32) []string

Cmdline is a process's argument vector; nil when it cannot be read (another user's process, or one that has exited).

func Environ

func Environ(ctx context.Context, pid int32) []string

Environ is a process's environment; nil when it cannot be read.

func Exe added in v0.113.0

func Exe(ctx context.Context, pid int32) string

Exe is the absolute path of a process's executable as the OS reports it (symlinks resolved on macOS and Linux); "" when it cannot be read.

func HasTTY added in v0.116.0

func HasTTY(p Proc) bool

HasTTY is whether p has a controlling terminal: tty_nr, the seventh field of /proc/<pid>/stat, is not zero. A process that cannot be read says no.

func IsInterpreter added in v0.116.0

func IsInterpreter(name string) bool

IsInterpreter is whether a program name (as the process table or argv[0] gives it) is an interpreter or a shell that runs a script named after it.

func Label

func Label(name string, argv []string) string

Label is a short name for a process a person recognises: the program, and for an interpreter the script it runs. name is the fallback when argv is unreadable.

func ProcMemory added in v0.149.0

func ProcMemory(pid int32) (uint64, error)

ProcMemory is one process's own memory, as the resource meter counts it (ownMemory: the physical footprint on macOS, RssAnon+RssShmem on Linux, private bytes on Windows).

func ResponsiblePID added in v0.113.0

func ResponsiblePID(int32) int32

ResponsiblePID is macOS only; elsewhere a web view's processes are the app's own children and the process tree says whose they are.

func SelfDiskIO added in v0.113.0

func SelfDiskIO() (read, written uint64, err error)

SelfDiskIO is the bytes this process has caused to be read from and written to storage since it started (read_bytes and write_bytes in /proc/self/io): real I/O, not reads the page cache answered.

func StartMs added in v0.116.0

func StartMs(ctx context.Context, pid int32) (int64, bool)

StartMs is when pid started, unix ms, as the table reports it; false when there is no such process. A pid whose start differs from the one a caller judged is another process: the check to make before every signal.

Types

type MachineStat added in v0.133.0

type MachineStat struct {
	// CPUs is the logical cores the OS reports.
	CPUs int
	// Load1 is the one-minute load average; HasLoad is false where the OS
	// keeps none (Windows).
	Load1   float64
	HasLoad bool
	// MemTotal is the physical memory; MemAvailable what can be taken
	// without swapping, as the OS judges it (macOS: free and inactive pages;
	// Linux: MemAvailable; Windows: available physical memory).
	MemTotal     uint64
	MemAvailable uint64
}

MachineStat is the machine as a whole: how many cores, how busy, how much memory and how much of it is free to take (ADR-067, the install hook's "this machine is loaded and that one is not").

func Machine added in v0.133.0

func Machine(ctx context.Context) (MachineStat, error)

Machine reads the machine-wide figures. A figure the OS would not give is left zero; the error is the first one met.

type Proc

type Proc struct {
	PID  int32
	PPID int32
	// Name is the OS's short name for the process (macOS truncates it to 16
	// bytes); Label is better for showing a person.
	Name string
	// UID is the owner's user id, -1 where the table does not say (Windows).
	UID int64
	// StartMs is when the process started, unix ms, 0 when unknown. A pid
	// with a different start is a different process.
	StartMs int64
	// contains filtered or unexported fields
}

Proc is one row of the process table.

type Sampler

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

Sampler turns cumulative CPU time into a rate. It is not safe for concurrent use.

func NewSampler

func NewSampler() *Sampler

NewSampler returns a sampler reading real processes.

func (*Sampler) Sample

func (s *Sampler) Sample(t *Table, pids []int32) map[int32]Usage

Sample reads every pid in t that is listed in pids. A process that has gone since the table was read is left out. Processes not sampled this time are forgotten, so a pid reused later starts fresh.

type SysMem added in v0.149.0

type SysMem struct {
	// Total is the physical memory. Used is what Activity Monitor calls
	// Memory Used on macOS (app memory, wired and compressed), Total minus
	// MemAvailable on Linux, and Total minus available physical memory on
	// Windows: in each case, what the OS could not hand out without taking
	// it from someone.
	Total uint64
	Used  uint64
	// SwapTotal and SwapUsed are the swap space and the part in use;
	// HasSwap is false where they were not read (Windows).
	SwapTotal uint64
	SwapUsed  uint64
	HasSwap   bool
	// Pressure is the OS's own verdict (PressureNormal, PressureWarn,
	// PressureCritical), "" where it keeps none.
	Pressure string
}

SysMem is the machine's memory at one moment.

func SystemMemory added in v0.149.0

func SystemMemory() (SysMem, error)

SystemMemory reads the machine's memory.

type Table

type Table struct {
	Procs    map[int32]Proc
	Children map[int32][]int32
}

Table is the process table at one moment, with each parent's children.

func Read

func Read(ctx context.Context) (*Table, error)

Read takes the process table.

func (*Table) Tree

func (t *Table) Tree(pid int32) []int32

Tree is pid and every process below it, pid first. A pid not in the table gives nil.

type Usage

type Usage struct {
	// CPUPct is percent of one core since the previous sample of the same
	// process; Warm is false on the first sample, when there is nothing to
	// compare with and CPUPct is 0.
	CPUPct float64
	Warm   bool
	// MemBytes is the process's own memory (ownMemory), not its RSS.
	MemBytes uint64
}

Usage is one process's cost.

Jump to

Keyboard shortcuts

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