procs

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 12 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 procs reads what processes on this computer use — memory and CPU time from ps, the whole machine's memory and CPU from the system — and adds them up by process tree. The TUI's resource monitor and the phone gateway's GET /api/resources both count conch this way: a pane is its program and everything that program started.

Index

Constants

This section is empty.

Variables

View Source
var RunPS = func(ctx context.Context) ([]byte, error) {

	return exec.CommandContext(ctx, "ps", "-A", "-o", "pid=,ppid=,rss=,time=,comm=").Output()
}

RunPS lists every process; tests replace it.

Functions

func BusyPercent

func BusyPercent(prev, cur Ticks) (float64, bool)

BusyPercent is how busy the computer was between two readings, 0–100.

func Children

func Children(procs map[int]Proc) map[int][]int

Children maps each pid to its children, in pid order.

func Elapsed

func Elapsed(prev, cur *Sample) (time.Duration, bool)

Elapsed is the time between two samples, when CPU use can be worked out from them: there is an earlier sample, it did not fail, and the clock went forward.

func Parse

func Parse(out []byte) map[int]Proc

Parse reads ps output, skipping lines it can't parse.

func ParseCPUTime

func ParseCPUTime(s string) (time.Duration, bool)

ParseCPUTime reads ps's time column: [DD-][HH:]MM:SS[.ss]. Linux prints 00:01:02, macOS 1:02.34 (minutes may pass 59 there).

func Spent

func Spent(prev, cur *Sample, pid int) time.Duration

Spent is Used for one process.

func Tree

func Tree(root int, children map[int][]int, procs map[int]Proc, claimed map[int]bool) []int

Tree lists root and its descendants that aren't claimed yet, claiming them. Each pid is taken once, so a parent loop in ps's list (a pid reused while ps was reading) can't send it round forever.

func Used

func Used(prev, cur *Sample, pids []int) time.Duration

Used is the CPU time pids spent between prev and cur. A process missing from prev, or whose time went down (its pid was reused), counts all its time as new.

Types

type Memory

type Memory struct {
	Total, Used int64
}

Memory is the whole computer's memory, in bytes.

Used is memory nothing else can have without something being pushed out: on Linux MemTotal - MemAvailable, which is what free(1) calls used; on macOS app memory (anonymous pages less purgeable ones) + wired + what the compressor occupies, which is Activity Monitor's "Memory Used". Neither counts the file cache, which the system gives back as it is needed.

func ParseMeminfo

func ParseMeminfo(b []byte) (Memory, error)

ParseMeminfo reads /proc/meminfo. A kernel older than MemAvailable (3.14) is given free + buffers + cache as available.

func ParseVMStat

func ParseVMStat(b []byte, total int64) (Memory, error)

ParseVMStat reads vm_stat's pages with total, from hw.memsize: used is app memory + wired + compressed, as Activity Monitor adds it up. An older vm_stat without anonymous pages counts active ones as the apps'.

func ReadMemory

func ReadMemory() (Memory, error)

ReadMemory reads the computer's memory: /proc/meminfo on Linux, sysctl and vm_stat on macOS.

type Proc

type Proc struct {
	PID, PPID int
	RSS       int64         // resident memory, KiB
	CPU       time.Duration // CPU time used so far
	Comm      string
}

Proc is one process from ps.

type Sample

type Sample struct {
	At    time.Time
	Procs map[int]Proc
	Err   error
}

Sample is every process on this computer at one moment.

func Take

func Take(timeout time.Duration) Sample

Take runs ps, giving it timeout. A ps that fails but still prints its list (a process it couldn't read) is as good as one that didn't.

func (*Sample) RSS

func (s *Sample) RSS(pids []int) int64

RSS adds up the resident memory of pids in s, in KiB.

type Ticks

type Ticks struct {
	Busy, Total uint64
}

Ticks is the whole computer's CPU time since boot, in the kernel's ticks: two of them taken apart say how busy it was in between.

func ParseProcStat

func ParseProcStat(b []byte) (Ticks, bool)

ParseProcStat reads the first line of /proc/stat: user nice system idle iowait irq softirq steal, then guest time, which user already counts. Idle and waiting for disk are not busy.

func ReadTicks

func ReadTicks() (Ticks, bool)

ReadTicks reads the computer's CPU time from /proc/stat. Only Linux keeps one there; elsewhere ok is false and CPU use is worked out from the processes instead.

Jump to

Keyboard shortcuts

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