procstat

package
v0.106.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 9 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, 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

This section is empty.

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 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.

Types

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
}

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