sampler

package
v0.15.6 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: EPL-2.0 Imports: 7 Imported by: 1 module, 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 sampler reads the load of the host it runs on: CPU per core, memory, network rates and the process list. It is pure Go without cgo: libSystem through purego on macOS, /proc on Linux, kernel32, ntdll and iphlpapi on Windows. It knows nothing about windows or charts.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupported = errors.New("sampler: this OS is not supported")

ErrUnsupported is returned by New on an OS without a source.

Functions

This section is empty.

Types

type Host

type Host struct {
	Name   string
	OS     string // "macOS 26.0", "Windows 11 (build 26100)", the distribution on Linux
	CPU    string // brand string, empty where the OS has none
	Cores  int    // logical cores
	Memory uint64 // bytes of RAM
}

Host is what does not change while the program runs.

type Memory

type Memory struct {
	Total, Used, App, Wired, Compressed, Cached, Free uint64
	SwapTotal, SwapUsed                               uint64
}

Memory is in bytes. Used is Total minus what the system can hand out at once. App, Wired and Compressed split Used where the OS tells (macOS); Cached is file cache that can be dropped.

type Process

type Process struct {
	PID  int
	Name string
	CPU  float64
	RSS  uint64
}

Process is one row of the process list. CPU is percent of one core, so it passes 100 for a process that runs on several.

type Sampler

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

Sampler turns the counters of a source into rates.

func New

func New() (*Sampler, error)

New opens the source of the running OS.

func (*Sampler) Close

func (s *Sampler) Close()

Close releases the source.

func (*Sampler) Host

func (s *Sampler) Host() Host

Host describes the machine.

func (*Sampler) Sample

func (s *Sampler) Sample(procs bool) (Snapshot, error)

Sample reads the host; with procs it reads the process list too. Call it from one goroutine.

type Snapshot

type Snapshot struct {
	Time    time.Time
	CPU     float64   // 0..1, all cores
	User    float64   // share of CPU in user code, 0..1 of all cores
	System  float64   // share in the kernel
	Cores   []float64 // 0..1 each
	Load    [3]float64
	HasLoad bool
	Uptime  time.Duration
	Mem     Memory
	// RxRate and TxRate are bytes per second over all non-loopback interfaces that are up.
	RxRate, TxRate float64
	Procs          []Process // nil when this sample did not read them
}

Snapshot is one reading. Rates (CPU, RxRate, TxRate) are over the time since the previous Sample and are zero in the first one.

Jump to

Keyboard shortcuts

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