sysinfo

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 10 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.

View Source
var SysClassNet = "/sys/class/net"

SysClassNet is the interface directory; tests repoint it.

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.

func CoreFrequencyMHz

func CoreFrequencyMHz(core int) uint64

CoreFrequencyMHz is get_cpu_frequency: the core's cpufreq scaling_cur_freq (kHz), else the first MHz line of /proc/cpuinfo, else 0.

func GiB

func GiB(bytes uint64) string

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

func ReadNetDevTotals

func ReadNetDevTotals(path string) (map[string]NetTotals, error)

ReadNetDevTotals is an alternative reader over /proc/net/dev used by the tests to drive counter snapshots; production reads sysfs.

func ReadNetTotals

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

ReadNetTotals reads rx/tx byte totals per interface.

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
	// contains filtered or unexported fields
}

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

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.

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.

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

type Sensor struct {
	Label        string
	TemperatureC float32
}

Sensor is one hwmon temperature input, labeled the way the sysinfo crate labels its components.

func Sensors

func Sensors() []Sensor

Sensors lists the hwmon temperature inputs (sysinfo's Components): a sensor's label is its tempN_label, else "<chip> <model>" or "<chip> tempN". Without any hwmon sensor, thermal_zone0 stands in. The order is by hwmon directory, then by input number.

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