gpu

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 21 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 gpu reads GPU metrics from the vendor command-line tools at locally configured paths.

shortcut: NVML is not loaded dynamically; nvidia-smi is run instead (one process per poll). Upgrade to a purego NVML binding if a poll every few seconds proves too heavy.

Index

Constants

View Source
const MaxProcsPerGPU = 20

MaxProcsPerGPU caps the per-process memory series per GPU (largest first).

Variables

View Source
var NvidiaECCQuery = []string{
	"--query-gpu=index,ecc.errors.corrected.volatile.total,ecc.errors.uncorrected.volatile.total",
	"--format=csv,noheader,nounits",
}

NvidiaECCQuery asks nvidia-smi for the ECC counters (a second call: devices without ECC answer "N/A", and the main query's shape stays fixed).

View Source
var NvidiaProcQuery = []string{
	"--query-compute-apps=gpu_uuid,pid,process_name,used_gpu_memory",
	"--format=csv,noheader,nounits",
}

NvidiaProcQuery lists compute processes with their GPU memory.

View Source
var NvidiaQuery = []string{
	"--query-gpu=index,name,uuid,utilization.gpu,memory.used,memory.total,temperature.gpu,power.draw",
	"--format=csv,noheader,nounits",
}

NvidiaQuery is the fixed argument list for nvidia-smi.

View Source
var RocmArgs = []string{"--showuse", "--showmeminfo", "vram", "--showtemp", "--showpower", "--json"}

RocmArgs is the fixed argument list for rocm-smi.

Functions

func Attribute

func Attribute(rs []Reading, pods map[string]PodGPU)

Attribute marks each reading with the pod that holds it (matched by device UUID, then index). A reading not in pods is unallocated; Tracked says the lookup ran at all.

func Emit

func Emit(out sink.Sink, d host.Discovery, rs []Reading, at time.Time)

Emit writes the hw.gpu.* metrics of the readings the discovery rules allow (matched against the GPU index).

func EmitExtras

func EmitExtras(out sink.Sink, d host.Discovery, rs []Reading, e Extras, at time.Time)

EmitExtras writes hw.gpu.process.memory.usage (top MaxProcsPerGPU per GPU), hw.gpu.mig.instance (value 1 per slice) and hw.gpu.nvlink.speed plus hw.gpu.nvlink.links.active per GPU. The readings give the GPU index/uuid mapping needed to apply the discovery rules.

func EmitXID

func EmitXID(out sink.Sink, xs []XID, at time.Time)

EmitXID counts each event on hw.gpu.xid.errors (a delta of 1, by code and PCI address) and logs it at ERROR so the cause text is searchable.

func ListPodResources

func ListPodResources(ctx context.Context, socket string) (map[string]PodGPU, error)

ListPodResources asks the kubelet at socket (a local path from the agent's own config, e.g. /var/lib/kubelet/pod-resources/kubelet.sock) which pod holds which GPU.

func ParseNvidiaECC

func ParseNvidiaECC(r io.Reader, rs []Reading) error

ParseNvidiaECC fills the ECC fields of rs (matched by GPU index) from NvidiaECCQuery output. XID errors are not in nvidia-smi; they come from the kernel-log tailer (XIDLogPath, see XIDTail).

func ParsePodResources

func ParsePodResources(b []byte) (map[string]PodGPU, error)

ParsePodResources reads a ListPodResourcesResponse into device id -> pod container, keeping only devices of a GPU resource (nvidia.com/gpu, amd.com/gpu, ...). Field numbers follow k8s.io/kubelet/pkg/apis/podresources/v1: response 1 = pods; pod 1 name, 2 namespace, 3 containers; container 1 name, 2 devices; devices 1 resource_name, 2 device_ids.

func Replay

func Replay(out sink.Sink, csv, ecc io.Reader, at time.Time) error

Replay runs the poll's parse and emit steps over recorded nvidia-smi output (the main query's CSV, and optionally the ECC query's), for `wtctl integration test`.

func Run

func Run(ctx context.Context, raw json.RawMessage, out sink.Sink) error

Run polls until ctx ends.

Types

type Config

type Config struct {
	Enabled       *bool          `json:"enabled"`
	Interval      string         `json:"interval"`        // default 15s, at least 5s
	NvidiaSMIPath string         `json:"nvidia_smi_path"` // empty: nvidia-smi is not run
	RocmSMIPath   string         `json:"rocm_smi_path"`   // empty: rocm-smi is not run
	Discovery     host.Discovery `json:"discovery"`
	// Extended adds per-process memory, MIG slices and NVLink (default true; three more nvidia-smi calls).
	Extended *bool `json:"extended"`
	// XIDLogPath is a kernel log file to tail for NVIDIA Xid events; empty: not read.
	XIDLogPath string `json:"xid_log_path"`
	// PodResourcesSocket is the kubelet pod-resources socket (Kubernetes nodes, e.g.
	// /var/lib/kubelet/pod-resources/kubelet.sock); when set, hw.gpu.* carry the pod that holds
	// each GPU and hw.gpu.allocated is reported. Empty: not asked.
	PodResourcesSocket string `json:"pod_resources_socket"`
}

Config is the gpu module's config. Only Enabled and Interval can be set remotely; the tool paths are local-file only.

type Extras

type Extras struct {
	Procs []Proc
	MIGs  []MIG
	Links []Link
}

Extras are the optional per-poll NVIDIA readings beyond the base query.

func NvidiaExtras

func NvidiaExtras(ctx context.Context, path string) Extras

NvidiaExtras runs the extra queries; each is optional and a failure leaves its list empty.

type Link struct {
	GPUIndex string
	Link     string
	Speed    float64
}

Link is one active NVLink link with its speed in bytes per second.

func ParseNvlink(r io.Reader) []Link

ParseNvlink parses `nvidia-smi nvlink -s`. Inactive links ("<inactive>") are left out.

type MIG

type MIG struct {
	GPUIndex string
	Profile  string // e.g. 1g.5gb
	ID       string // the MIG-... uuid
}

MIG is one multi-instance-GPU slice from `nvidia-smi -L`.

func ParseNvidiaL

func ParseNvidiaL(r io.Reader) []MIG

ParseNvidiaL extracts MIG instances (attributed to the GPU line above them) from `nvidia-smi -L`.

type PodGPU

type PodGPU struct {
	Pod, Namespace, Container string
}

PodGPU is the pod container a device is assigned to.

type Proc

type Proc struct {
	GPUID    string
	PID      string
	Name     string
	MemBytes float64
}

Proc is one process using a GPU. MemBytes is NaN-free: unreported memory is 0.

func ParseNvidiaProcs

func ParseNvidiaProcs(r io.Reader) []Proc

ParseNvidiaProcs parses NvidiaProcQuery output (memory in MiB). A "No running processes" line or empty output is no processes.

type Reading

type Reading struct {
	Index, Name, ID, Vendor string
	UtilPercent             float64
	MemUsedBytes            float64
	TempC, PowerW           float64
	// ECC error counts since driver load; NaN when the device or driver does not report them.
	ECCCorrected, ECCUncorrected float64
	// Pod is the Kubernetes pod container the GPU is assigned to (Attribute); Tracked says the
	// kubelet was asked, so a nil Pod means unallocated rather than unknown.
	Pod     *PodGPU
	Tracked bool
}

Reading is one GPU at one poll. NaN fields are not reported by the device.

func Nvidia

func Nvidia(ctx context.Context, path string) ([]Reading, error)

Nvidia polls nvidia-smi at path.

func ParseNvidiaCSV

func ParseNvidiaCSV(r io.Reader) ([]Reading, error)

ParseNvidiaCSV parses the output of nvidia-smi with NvidiaQuery. Memory is reported in MiB.

func ParseRocmJSON

func ParseRocmJSON(b []byte) ([]Reading, error)

ParseRocmJSON parses `rocm-smi --json` output: {"card0": {"GPU use (%)": "5", ...}, ...}. rocm-smi renames its keys between releases, so fields are found by key fragment.

func Rocm

func Rocm(ctx context.Context, path string) ([]Reading, error)

Rocm polls rocm-smi at path.

type XID

type XID struct {
	PCI    string
	Code   int
	Detail string
}

XID is one NVIDIA driver Xid event from the kernel log.

func ParseXID

func ParseXID(r io.Reader) []XID

ParseXID returns the Xid events in kernel log text, in order.

type XIDTail

type XIDTail struct {
	Path string
	// contains filtered or unexported fields
}

XIDTail reads new Xid events from a kernel log file (for example /var/log/kern.log) on each Poll. The first poll starts at the end, so history is not replayed; a shrunken file (rotation) is re-read from its start. The agent only needs read access to that one file.

func (*XIDTail) Poll

func (x *XIDTail) Poll() ([]XID, error)

Poll returns the events appended since the last poll.

Jump to

Keyboard shortcuts

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