sysinfo

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 14 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 sysinfo reads the kernel's own counters: /proc/stat for CPU time, /proc/meminfo for memory, and statfs for storage. The pure-Go counterpart of crates/wayle-sysinfo's read path.

Index

Constants

This section is empty.

Variables

View Source
var (
	ProcRoot = "/proc"
	SysRoot  = "/sys"
)

ProcRoot and SysRoot are the procfs and sysfs mounts; tests repoint them at fake trees.

Functions

func AutoBytes

func AutoBytes(bytes uint64) string

AutoBytes is bytesize 2's Display: IEC (1024-based) units with one decimal, "KiB" through "EiB", plain bytes below a kibibyte. The unit is picked from ln(size)/ln(1024), as its std build does.

func CPUTemperature

func CPUTemperature(sensor string) (float32, bool)

CPUTemperature is find_cpu_temperature: a named sensor by label substring (case-insensitive), or for "auto" the first sensor matching each pattern in turn. A sensor is an input that reads; of inputs sharing a label the first that reads is the sensor. Without any hwmon sensor, thermal_zone0 stands in, unlabeled.

func CoreFrequencyMHz

func CoreFrequencyMHz(core int, cpuinfo func() uint64) uint64

CoreFrequencyMHz is get_cpu_frequency: the core's cpufreq scaling_cur_freq (kHz), else cpuinfo, the first MHz line of /proc/cpuinfo (cpuinfoMHz), else 0. Read passes one cpuinfo for all its cores, so a machine without cpufreq reads /proc/cpuinfo once per poll, not once per core.

func GiB

func GiB(bytes uint64) string

GiB renders a byte count the way the Rust module does: one decimal.

func ReadNetTotals

func ReadNetTotals() (map[string]NetTotals, error)

ReadNetTotals reads every interface's rx/tx byte totals once.

func SelectInterface

func SelectInterface(totals map[string]NetTotals, iface string) (string, bool)

SelectInterface is netstat's helpers.rs select_interface: "auto" picks the busiest non-loopback interface, anything else matches by exact name (ok=false when absent).

func Usage

func Usage(prev, cur CPUTimes) float32

Usage is sysinfo's CpuUsage percent between two readings: the work delta over the total delta, capped at 100. Against a zero reading (the first refresh) it is the average since boot.

Types

type CPU

type CPU struct {
	UsagePercent       float32
	AvgFrequencyMHz    uint64
	MaxFrequencyMHz    uint64
	BusiestCoreFreqMHz uint64
	// TemperatureC is the matched sensor's reading; ok is false
	// without one.
	TemperatureC   float32
	HasTemperature bool
	Cores          []Core
}

CPU is polling/cpu.rs's CpuData.

type CPUReader

type CPUReader struct {

	// Sensor is the temp-sensor key: "auto" or a label substring.
	Sensor string
	// OmitFrequency and OmitTemperature leave out what the caller does
	// not show, which is most of a read's cost: a file per core and a
	// query of the sensor.
	OmitFrequency, OmitTemperature bool
	// contains filtered or unexported fields
}

CPUReader keeps the previous counters between polls, as the Rust poll loop keeps its System.

func (*CPUReader) Close added in v0.2.0

func (r *CPUReader) Close()

Close releases what the reader keeps open; a later Read opens it again.

func (*CPUReader) Read

func (r *CPUReader) Read() (CPU, error)

Read samples the counters, frequencies, and temperature.

type CPUStat

type CPUStat struct {
	Global CPUTimes
	Cores  []CPUTimes
}

CPUStat is the aggregate line and one per core, in core order.

func ReadCPUStat

func ReadCPUStat() (CPUStat, error)

ReadCPUStat parses /proc/stat's cpu lines.

type CPUTimes

type CPUTimes struct {
	Work, Total uint64
}

CPUTimes is one /proc/stat cpu line reduced the way the sysinfo crate does: work is user, nice, system, irq, and softirq (with guest time taken out of user and nice, where the kernel already counts it); total adds idle, iowait, steal, and the guest time back.

type Core

type Core struct {
	UsagePercent float32
	FrequencyMHz uint64
}

Core is one core's usage and frequency.

type Disk

type Disk struct {
	MountPoint     string
	Filesystem     string
	TotalBytes     uint64
	AvailableBytes uint64
	UsedBytes      uint64
}

Disk is one mounted filesystem from the sysinfo crate's disk list.

func Disks

func Disks() []Disk

Disks reads /proc/mounts the way sysinfo's get_all_list does: the ignored filesystems and mount points under /sys, /proc, and /run (except /run/media) are skipped, one disk per mount point, and a filesystem statvfs reports as empty is left out.

func DisksAt added in v0.2.0

func DisksAt(paths []string) []Disk

DisksAt is Disks for the mount points in paths: statvfs on any other mount would only cost a syscall, and can wake a sleeping or remote one.

type Memory

type Memory struct {
	Total     uint64
	Available uint64
	SwapTotal uint64
	SwapFree  uint64
}

Memory is one /proc/meminfo snapshot, in bytes.

func ReadMemory

func ReadMemory() (Memory, error)

ReadMemory parses the meminfo totals once.

func (Memory) SwapPercent

func (m Memory) SwapPercent() float64

SwapPercent maps swap usage onto 0..100.

func (Memory) UsagePercent

func (m Memory) UsagePercent() float64

UsagePercent maps used onto 0..100.

func (Memory) Used

func (m Memory) Used() uint64

Used returns total minus available (sysinfo's definition).

type MemoryReader added in v0.2.0

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

MemoryReader polls the meminfo totals through a kept descriptor.

func (*MemoryReader) Close added in v0.2.0

func (r *MemoryReader) Close()

Close releases the descriptor; a later Read opens it again.

func (*MemoryReader) Read added in v0.2.0

func (r *MemoryReader) Read() (Memory, error)

Read parses the totals in place; a missing line is an error, not a zero.

type NetReader added in v0.2.0

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

NetReader polls /proc/net/dev through a kept descriptor: one file however many interfaces there are, rather than two per interface in sysfs.

func (*NetReader) Close added in v0.2.0

func (r *NetReader) Close()

Close releases the descriptor; a later Read opens it again.

func (*NetReader) Read added in v0.2.0

func (r *NetReader) Read() (map[string]NetTotals, error)

Read parses the totals in place. A line that does not parse skips its interface.

type NetTotals

type NetTotals struct {
	Rx uint64
	Tx uint64
}

NetTotals is one interface's lifetime byte counters.

type Rate

type Rate struct {
	Interface string
	RxPerSec  uint64
	TxPerSec  uint64
}

Rate is one interface's bytes-per-second over the sample window.

func NetRate

func NetRate(name string, prev, now NetTotals, elapsed float64) (Rate, error)

NetRate computes the per-second rate of one interface between two total snapshots (elapsed in seconds).

type Storage

type Storage struct {
	UsagePercent   float32
	UsedBytes      uint64
	TotalBytes     uint64
	AvailableBytes uint64
	// Filesystem is the one disk's type; Multiple marks several.
	Filesystem string
	Multiple   bool
}

Storage is helpers.rs's StorageSnapshot: the mount points together.

func AggregateStorage

func AggregateStorage(disks []Disk, paths []string) (Storage, bool)

AggregateStorage is aggregate_storage: the disks whose mount point is one of paths exactly, summed; false when none is (or no path is given).

Jump to

Keyboard shortcuts

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