Documentation
¶
Overview ¶
Package proc reads the Linux process table from /proc once and answers the questions beekeeper asks of it: who is whose parent, which session a process belongs to, how long it has run. Reading /proc directly instead of grepping `ps` output means a pattern never matches the command that looks for it.
Index ¶
- Constants
- func Alive(pid int) bool
- func AnonKiB(pid int) int
- func Background(ctx context.Context) context.Context
- func Cgroup(pid int) string
- func Command(ctx context.Context, name string, arg ...string) *exec.Cmd
- func Cwd(pid int) string
- func Environ(pid int) (map[string]string, error)
- func Held(pid int) []string
- func IsBackground(ctx context.Context) bool
- func Kubeconfig(c *exec.Cmd, files []string)
- func LookPath(name string) (string, error)
- func Named(name string, uid int) []int
- func References(pid int) ([]string, error)
- func Started(pid int) (time.Time, error)
- func Terminate(pids []int, grace time.Duration)
- func TerminateMatching(ps []*Process, grace time.Duration, match func(*Process) bool) []int
- func UID(pid int) int
- func WriteBytes(pid int) (int64, error)
- type Process
- type Scan
- type Table
- func (t *Table) Ancestors(pid int) []*Process
- func (t *Table) AnonKiB(pid int) int
- func (t *Table) Children(pid int) []*Process
- func (t *Table) Cwd(pid int) string
- func (t *Table) Descendants(pid int) []*Process
- func (t *Table) Environ(pid int) (map[string]string, error)
- func (t *Table) Orphaned(p *Process) bool
- func (t *Table) Process(pid int) *Process
- type UnknownError
- type UnknownProcess
Constants ¶
const Niceness = "10"
Niceness is the nice level of a command started under a Background context: behind every interactive process on a starved machine.
Variables ¶
This section is empty.
Functions ¶
func AnonKiB ¶
AnonKiB returns the anonymous resident memory of pid in KiB: what killing it would give back.
func Background ¶ added in v0.38.1
Background marks ctx: the commands Command starts under it run at nice Niceness.
func Command ¶ added in v0.38.1
Command is exec.CommandContext for a read the caller can do without: under a Background context it runs through nice (when the machine has it), and a command killed by ctx gives up its pipes after waitDelay.
func Held ¶ added in v0.136.0
Held returns the paths pid holds: its working directory, its executable and every file it has open, a deleted one by the path it had (readable for the caller's own processes).
func IsBackground ¶ added in v0.38.1
IsBackground reports whether ctx is marked by Background.
func Kubeconfig ¶ added in v0.149.0
Kubeconfig makes c read the kubeconfig files as kubectl merges a KUBECONFIG list; none, c keeps the environment's.
func LookPath ¶ added in v0.108.1
LookPath is exec.LookPath, then the Go tool directories for a bare name PATH lacks: beside the running binary, $GOBIN, every $GOPATH/bin, ~/go/bin and ~/.go/bin, in that order. A systemd user unit, the sandbox broker for one, runs with the service manager's PATH, the distribution's default without the directory go install writes to, where kind and the other Go CLIs live beside beekeeper. A tool found nowhere fails naming the directories looked in; a name with a path separator is exec's alone.
func Named ¶ added in v0.101.0
Named returns the PIDs of the live processes whose command name (comm) is name and which run as uid.
func References ¶ added in v0.171.0
References reads a process's files, mappings and temporary/build directory environment. An unreadable live process is an error: a pruner must keep candidates when it cannot establish whether that process uses them.
func Terminate ¶ added in v0.149.0
Terminate sends the processes pids SIGTERM, and SIGKILL to those still alive after grace.
func TerminateMatching ¶ added in v0.174.4
TerminateMatching rechecks the snapshot's start time and match before each signal, including escalation, so an exited PID reused by another process is never signalled. It returns the PIDs successfully sent TERM.
func WriteBytes ¶ added in v0.100.0
WriteBytes is what the live process pid caused to be written to storage (write_bytes of /proc/<pid>/io); another user's process is unreadable.
Types ¶
type Process ¶
type Process struct {
PID int
PPID int
Comm string
Args []string
Start time.Time
// StartTicks is Start as the kernel counts it: clock ticks since boot.
StartTicks int64
// CPU is the processor time it has burned (user and system).
CPU time.Duration
// RSSKiB is its resident memory, file-backed pages included.
RSSKiB int
}
Process is one entry of the table.
type Scan ¶
type Scan struct {
Refs []string
Commands []string
// Foreign lists the user's-uid processes the kernel refuses to read
// that run in another pid or user namespace (a container's, a kind
// node's pause container for one). Their paths name the container's
// filesystem, not the person's, so they are skipped, not unknown.
Foreign []UnknownProcess
}
Scan is one read of the user's processes' references.
func UserScan ¶
UserScan scans every numeric process entry, including entries the general process table cannot read. Unknown ownership fails closed. Kernel EACCES on helpers protected by dumpability, credentials or capabilities is expected; other errors on live user processes remain failures.
A process whose references cannot be read does not end the scan: the readable processes' references are returned with an *UnknownError naming the unreadable ones, so a caller can keep what they could reference while still deciding with the rest. An error on the process table itself is returned alone. A process the kernel refuses in another namespace is skipped into Scan.Foreign.
type Table ¶
Table is a snapshot of the process table.
func NewTable ¶ added in v0.149.0
NewTable is the table of the processes ps, linked by their parents: a process tree built in memory rather than read.
func ReadAt ¶ added in v0.13.1
ReadAt scans the process tree under root: /proc, or a copy of it (each process's stat, cmdline and environ, and the btime line of stat) captured into testdata.
func (*Table) AnonKiB ¶ added in v0.13.1
AnonKiB returns the anonymous resident memory of pid in the table's process tree.
func (*Table) Cwd ¶ added in v0.13.1
Cwd returns the working directory of pid in the table's process tree.
func (*Table) Descendants ¶
Descendants returns every process below pid, depth first.
func (*Table) Environ ¶ added in v0.13.1
Environ returns the environment of pid in the table's process tree.
type UnknownError ¶ added in v0.172.0
type UnknownError struct{ Processes []UnknownProcess }
UnknownError lists the processes whose references are unknown.
func (*UnknownError) Error ¶ added in v0.172.0
func (e *UnknownError) Error() string
func (*UnknownError) Unwrap ¶ added in v0.172.0
func (e *UnknownError) Unwrap() []error
type UnknownProcess ¶ added in v0.172.0
UnknownProcess is a process whose references could not be read.