sys

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 15 Imported by: 2

README

sys package

The sys package provides lightweight runtime visibility into host and container resources, including:

  • CPU count and load estimation
  • memory statistics
  • container-aware reporting on Linux
  • simple, dependency-free fallbacks when preferred sources are unavailable

Table of Contents

CPU reporting

Public function include:

Function name Description
NumCPU() returns the effective CPU count used by the process
MaxParallelism() derives internal parallelism from NumCPU()
GoEnvMaxprocs() adjusts GOMAXPROCS downward when needed
MaxLoad() returns CPU utilization as a percentage
MaxLoad2() returns CPU utilization plus a boolean isExtreme indicating CPU starvation
HighLoadWM() derives a high-load watermark from CPU count
LoadAverage() reads system load averages
Effective CPU count

At startup, the package initializes a process-wide CPU count:

  • default: runtime.NumCPU()
  • container override: cgroup-based CPU quota when detected

The package employs prioritized capability detection:

  1. Attempt cgroup v2 (cpu.max) parsing.
  2. Fall back to cgroup v1 (cpu.cfs_quota_us).
  3. If both fail or are missing, fallback to host runtime.NumCPU().

Errors from both v1 and v2 paths are aggregated and reported to stderr.

Utilization model

CPU utilization is sampled as a delta of cumulative CPU time over wall-clock time:

  • cumulative CPU usage is read from the best available source
  • the previous raw sample is cached in a tracker
  • utilization is computed as:

delta_cpu_usage / (delta_wall_time * NumCPU)

Utilization and throttling are computed atomically in a single pass from the same time delta (specifically, to synchronize "Extreme Load" signal based on throttling).

A sample gap older than the configured stale interval is discarded and treated as a reset. This avoids reporting a long-window average as if it were current load.

Linux source hierarchy

On Linux, CPU usage currently follows this order:

  1. cgroup v2: cpu.stat
    • usage_usec
    • throttled_usec
  2. cgroup v1: cpuacct.usage
  3. host fallback: /proc/stat
  4. last-resort fallback: /proc/loadavg

The cgroup v2 path is preferred because it provides both cumulative usage and throttling information.

CPU starvation vs utilization

MaxLoad2() distinguishes between:

  • high utilization: CPU is busy
  • CPU starvation: the process or container is being throttled and cannot get CPU time when needed

In cgroup v2 environments, throttling is read from cpu.stat and used only for the extreme/starvation path. A container may therefore be treated as under extreme CPU pressure even if raw utilization is below the normal extreme-load threshold.

This is intentional: throttling indicates lack of CPU availability, not just high usage.

Memory reporting

Public functions include:

  • MemStat.Get() populates memory statistics
  • MemStat.Str() formats a compact summary
Host memory

Host memory is read from /proc/meminfo.

The implementation uses:

  • MemTotal
  • MemFree
  • MemAvailable when present
  • Cached
  • Buffers
  • SwapTotal
  • SwapFree

If MemAvailable is not present, ActualFree falls back to:

MemFree + BuffCache

Derived values include:

  • Used
  • ActualUsed
  • SwapUsed
Container memory

Current Linux container memory support is based on cgroup v1 memory files:

  • memory.limit_in_bytes
  • memory.usage_in_bytes
  • memory.stat (total_cache)

Behavior today:

  • if not containerized, return host memory
  • if containerized but no effective memory limit is configured, return host memory
  • if cgroup memory files are unavailable or unreadable, return host memory
  • swap statistics are always host statistics

This is intentionally conservative, but it also means that cgroup v2 memory-only environments are not yet handled explicitly.

Fallback

The package generally follows these rules:

  • preferred source first
  • degrade to older or coarser source when necessary
  • for CPU, preserve a usable percentage whenever possible
  • for memory, prefer host stats over failing when container-specific files cannot be read

This makes the package resilient across bare-metal, VMs, containers, and mixed deployment environments, at the cost of occasionally returning an approximation instead of a strict container-scoped answer.

Current limitations and future plans

Container detection

Some Linux logic still branches early on a process-wide containerized flag, derived at node startup from simple string matching on /proc/1/cgroup.

This is simple, but it can mis-detect certain deployment environments and steer source selection down the wrong path.

cgroup v2 memory

CPU reporting already uses cgroup v2, but memory reporting does not yet have a dedicated cgroup v2 path.

As a result, a container running under cgroup v2 memory controls may currently fall back to host memory statistics.

Hardcoded thresholds and stale samples

Several operational constants are currently compiled in, including:

  • stale CPU sample interval
  • CPU load thresholds
  • throttling threshold for extreme CPU starvation

CPU utilization is computed from sampled deltas and depends on periodic sampling. There is currently no synchronous (or asynchronous) path to refresh utilization sample on demand.

If the previous sample is considered stale, the package will return 0 (zero) utilization instead of a current value.

Future work

Planned follow-ups include:

  1. replace init-time container detection with capability-based source selection
    • try cgroup v2 first
    • then cgroup v1
    • then host fallbacks
  2. add cgroup v2 memory support
  3. revisit error handling for cgroup CPU quota parsing
  4. consider environment-variable overrides for selected thresholds and tunables

Documentation

Overview

Package sys provides methods to read system information

  • Copyright (c) 2018-2025, NVIDIA CORPORATION. All rights reserved.

Package sys provides methods to read system information

  • Copyright (c) 2018-2026, NVIDIA CORPORATION. All rights reserved.

Package sys provides methods to read system information

  • Copyright (c) 2018-2026, NVIDIA CORPORATION. All rights reserved.

Package sys provides methods to read system information

  • Copyright (c) 2018-2026, NVIDIA CORPORATION. All rights reserved.

Package sys provides methods to read system information

  • Copyright (c) 2018-2025, NVIDIA CORPORATION. All rights reserved.

Package sys provides methods to read system information

  • Copyright (c) 2018-2025, NVIDIA CORPORATION. All rights reserved.

Package sys provides methods to read system information

  • Copyright (c) 2018-2025, NVIDIA CORPORATION. All rights reserved.

Index

Constants

View Source
const (
	ExtremeLoad = 92
	HighLoad    = 82
)

CPU utilization thresholds (percentage, 0-100) HighLoad < HighLoadWM() < ExtremeLoad

Variables

This section is empty.

Functions

func Containerized

func Containerized() bool

func GoEnvMaxprocs added in v1.3.24

func GoEnvMaxprocs()

func HighLoadWM added in v1.3.26

func HighLoadWM() int

HighLoadWM: "high-load watermark" as a percentage. For 8 CPUs: max(100 - 100/8, 1) = 88 - between HighLoad(82) and ExtremeLoad(92). see also: (ExtremeLoad, HighLoad) defaults

func MaxLoad added in v1.3.26

func MaxLoad() (load float64)

MaxLoad returns CPU utilization percentage (0-100). See also: README.md in this package.

func MaxLoad2 added in v1.3.30

func MaxLoad2() (load float64, isExtreme bool)

MaxLoad2 returns CPU utilization percentage and whether the system is in an "extreme" CPU-starvation condition. In containers (cgroup v2): also check throttled_usec - if the container is being throttled, that's CPU starvation regardless of utilization percentage.

See also: README.md in this package.

func MaxParallelism added in v1.3.26

func MaxParallelism() int

number of intra-cluster broadcasting goroutines

func NumCPU

func NumCPU() int

func ProcFDSize added in v1.3.31

func ProcFDSize() int

Types

type LoadAvg

type LoadAvg struct {
	One, Five, Fifteen float64
}

func LoadAverage

func LoadAverage() (avg LoadAvg, _ error)

type MemStat

type MemStat struct {
	Total      uint64
	Used       uint64
	Free       uint64
	BuffCache  uint64
	ActualFree uint64
	ActualUsed uint64
	SwapTotal  uint64
	SwapFree   uint64
	SwapUsed   uint64
}

func (*MemStat) Get

func (mem *MemStat) Get() error

func (*MemStat) Str added in v1.3.26

func (mem *MemStat) Str(sb *cos.SB)

type ProcCPUStats

type ProcCPUStats struct {
	User     uint64
	System   uint64
	Total    uint64
	LastTime int64
	Percent  float64
}

type ProcMemStats

type ProcMemStats struct {
	Size     uint64
	Resident uint64
	Share    uint64
}

type ProcStats

type ProcStats struct {
	CPU ProcCPUStats
	Mem ProcMemStats
}

func ProcessStats

func ProcessStats(pid int) (ProcStats, error)

Jump to

Keyboard shortcuts

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