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
- Variables
- func Attribute(rs []Reading, pods map[string]PodGPU)
- func Emit(out sink.Sink, d host.Discovery, rs []Reading, at time.Time)
- func EmitExtras(out sink.Sink, d host.Discovery, rs []Reading, e Extras, at time.Time)
- func EmitXID(out sink.Sink, xs []XID, at time.Time)
- func ListPodResources(ctx context.Context, socket string) (map[string]PodGPU, error)
- func ParseNvidiaECC(r io.Reader, rs []Reading) error
- func ParsePodResources(b []byte) (map[string]PodGPU, error)
- func Replay(out sink.Sink, csv, ecc io.Reader, at time.Time) error
- func Run(ctx context.Context, raw json.RawMessage, out sink.Sink) error
- type Config
- type Extras
- type Link
- type MIG
- type PodGPU
- type Proc
- type Reading
- type XID
- type XIDTail
Constants ¶
const MaxProcsPerGPU = 20
MaxProcsPerGPU caps the per-process memory series per GPU (largest first).
Variables ¶
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).
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.
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.
var RocmArgs = []string{"--showuse", "--showmeminfo", "vram", "--showtemp", "--showpower", "--json"}
RocmArgs is the fixed argument list for rocm-smi.
Functions ¶
func Attribute ¶
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 ¶
Emit writes the hw.gpu.* metrics of the readings the discovery rules allow (matched against the GPU index).
func EmitExtras ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
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 Link ¶
Link is one active NVLink link with its speed in bytes per second.
func ParseNvlink ¶
ParseNvlink parses `nvidia-smi nvlink -s`. Inactive links ("<inactive>") are left out.
type MIG ¶
MIG is one multi-instance-GPU slice from `nvidia-smi -L`.
func ParseNvidiaL ¶
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 ¶
Proc is one process using a GPU. MemBytes is NaN-free: unreported memory is 0.
func ParseNvidiaProcs ¶
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 ParseNvidiaCSV ¶
ParseNvidiaCSV parses the output of nvidia-smi with NvidiaQuery. Memory is reported in MiB.
func ParseRocmJSON ¶
ParseRocmJSON parses `rocm-smi --json` output: {"card0": {"GPU use (%)": "5", ...}, ...}. rocm-smi renames its keys between releases, so fields are found by key fragment.
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.