Skip to content

GPU Monitoring

Denet provides comprehensive GPU monitoring for NVIDIA GPUs using the NVIDIA Management Library (NVML).

GPU monitoring is opt-in: pass --gpu (CLI) or enable_gpu=True (Python). Loading the NVIDIA driver library maps tens of MB of driver state into denet (about 19 MB extra peak memory on a laptop RTX 2000), so denet doesn't load it unless asked. All data comes from NVML directly; denet never runs nvidia-smi.

Apple Silicon Macs are also supported, experimentally: see Apple Silicon (experimental).

Features

  • GPU Utilization: Real-time GPU compute utilization percentage
  • Memory Monitoring: GPU memory usage, both total and per-process when available
  • Temperature Tracking: GPU temperature monitoring
  • Power Consumption: GPU power draw (power_usage, in milliwatts)
  • Multi-GPU Support: Monitor all NVIDIA GPUs in the system
  • Process-Specific: Track GPU memory usage per monitored process
  • Graceful Fallback: With --gpu but no usable GPU/NVML, denet warns and carries on

Requirements

  • NVIDIA GPU with driver support
  • NVIDIA CUDA toolkit or driver with NVML support
  • A build with the gpu feature: the PyPI wheels, the conda package's CLI, and cargo install denet --features gpu. The feature adds no runtime dependency: without --gpu / enable_gpu=True, no GPU library is loaded.

Installation

# CLI with GPU support (the conda package on prefix.dev, or from source)
cargo install denet --features gpu

Usage Examples

import denet
import json

# Create monitor with GPU monitoring turned on (off by default)
monitor = denet.ProcessMonitor(
    cmd=["python", "gpu_workload.py"],
    base_interval_ms=100,
    max_interval_ms=1000,
    store_in_memory=True,
    enable_gpu=True,
)

# Check GPU availability
if monitor.is_gpu_enabled():
    print(f"Found {monitor.gpu_device_count()} GPU(s)")

    # Get GPU summary
    gpu_summary = json.loads(monitor.get_gpu_summary())
    print(f"Total GPU memory: {gpu_summary['total_memory_gb']:.2f} GB")

    # Run monitoring
    monitor.run()

    # Analyze GPU usage in samples
    samples = monitor.get_samples()
    for sample_str in samples:
        sample = json.loads(sample_str)
        if sample.get("gpu"):
            gpu_data = sample["gpu"]
            max_util = gpu_data.get("max_gpu_utilization", 0)
            if max_util > 0:
                print(f"GPU utilization: {max_util}%")
                break
else:
    print("GPU monitoring not available")

Command Line GPU Output

With --gpu, the command line interface includes GPU information:

denet --gpu run python train_model.py
CPU: 45.2% | Memory: 2.1 GB | Threads: 8 | GPU: 85%, 3.2GB | Disk: 1.2MB rd, 856KB wr

GPU Data Structure

GPU metrics are included in the JSON output:

{
  "ts_ms": 1625184000000,
  "cpu_usage": 45.2,
  "mem_rss_kb": 2147483,
  "gpu": {
    "devices": [
      {
        "device_index": 0,
        "name": "NVIDIA GeForce RTX 4090",
        "utilization_gpu": 85,
        "utilization_memory": 78,
        "memory_total": 25757220864,
        "memory_used": 3221225472,
        "temperature": 65,
        "power_usage": 320000,
        "process_memory_usage": 1073741824
      }
    ],
    "total_memory_used": 3221225472,
    "total_memory_available": 25757220864,
    "max_gpu_utilization": 85,
    "max_memory_utilization": 78
  }
}

Energy (gpu_energy)

When the GPU exposes NVML's cumulative energy counter (total_energy_consumption, Volta and newer, ~2017+), each gpu block also carries:

"gpu_energy": { "package_joules": 12.4, "process_joules": 8.1 }
  • package_joules — whole-board energy over the sample interval (all processes + memory + static/idle draw), diffed from the NVML counter. Ground truth, measured.
  • process_joules — the slice attributed to the monitored process(es) by summed GPU-utilization share (Σ proc_util) / 100, clamped to the board total.

Caveat — whole-card vs per-process. NVML has no per-process energy counter; the counter is always the entire board. So process_joules is an estimate, and a coarser one than the CPU/RAPL equivalent: NVML's per-process "SM utilization" is the fraction of time a kernel was resident, not how many SMs or how much power it drew, and under MPS/concurrent contexts the per-process utilizations may not cleanly partition. Trust package_joules; treat process_joules as a guide. No counter (pre-Volta) → gpu_energy is omitted. No root or extra capability is needed (NVML runs as the invoking user).

Shared GPU. package_joules is the whole board, so on a multi-tenant GPU it includes other users' and the display's draw — it is not scoped to you. But process_joules is scoped: it multiplies the board total by the summed GPU utilization of your monitored pids only (NVML reports SM utilization per-pid, the same data nvidia-smi pmon shows). So a neighbor's kernels are never charged to you — they raise the board total while your util share stays what your kernels actually used, i.e. you're attributed your fraction of a larger pie, not their energy. Accuracy still hinges on the per-pid util proxy, which is coarse under MPS/MIG.

Counter reliability. The absolute values come straight from the driver's counter and are accurate on datacenter GPUs (A100/H100). On some laptop/Optimus dGPUs the counter is noisy and NVML polling itself perturbs the power state, so treat laptop readings as indicative, not billable.

PCIe throughput (pcie_tx_kbps, pcie_rx_kbps)

Each device in system_metrics carries the PCIe bus traffic NVML reports (nvmlDeviceGetPcieThroughput, Maxwell and newer, not on vGPUs):

{ "device_index": 0, "pcie_tx_kbps": 488, "pcie_rx_kbps": 1843200 }
  • pcie_tx_kbps: GPU → host, KB/s. pcie_rx_kbps: host → GPU, KB/s.
  • Snapshot, not average. The driver counts bytes over a 20 ms window during the call, so a short burst between samples can be missed. A bandwidth-bound transfer shows up reliably. A single 20 ms blip may not.
  • Device-wide. NVML has no per-process PCIe counter, so other processes' (and the display's) transfers are included.
  • Cost. Each read blocks ~20 ms in NVML, so TX+RX costs ~40 ms per device per sample. denet reads it once per sample and reuses it for the child-aggregated record. At very short --interval on multi-GPU hosts this sets the floor on the sampling period.
  • denet stats reports the peak TX/RX seen as Peak GPU PCIe TX/RX.

Apple Silicon (experimental)

On macOS, a build with the gpu feature reads the GPU from the IORegistry instead of NVML. It needs no root and loads no driver library. This backend is experimental: its sources are undocumented Apple internals that can change between macOS releases. --gpu prints a note saying so, and each record has collection_method: "apple-ioregistry (experimental)".

What it reports:

  • process_data[].gpu_time_ns: cumulative GPU time per process, summed over its Metal command queues. This is the GPU counterpart of CPU time and is probably what Activity Monitor's "GPU Time" column shows.
  • process_data[].gpu_utilization: the change in gpu_time_ns over the sample interval, as a percentage (clamped to 100).
  • system_metrics[0].system_utilization_gpu: device-wide utilisation.
  • system_metrics[0].memory_used: GPU-driver memory in use. Memory is unified, so there is no memory_total and no per-process GPU memory.
  • system_metrics[0].power_usage: average GPU power over the sample interval, in milliwatts like NVIDIA's, from IOReport's GPU Energy counter, a private macOS framework.
  • gpu_energy: GPU energy over the interval and the monitored processes' share of it by GPU time, as for NVIDIA (see Energy). The energy covers the GPU only, not the whole SoC.
  • system_metrics[0].freq_mhz: mean GPU clock over the part of the interval the GPU was clocked, from IOReport's P-state residency. Absent when the GPU was off the whole interval. max_freq_mhz is the top of the chip's P-state table (1278 MHz on an M1). A busy GPU (system_utilization_gpu near 100) running well under max_freq_mhz is being throttled, by heat or by power limits, or another process is holding the clock down.

  • system_metrics[0].temperature: the hottest GPU die sensor (GPU MTR Temp Sensor*), in °C, via sysinfo. These sensors sit at a 30 °C floor while the GPU is powered down, so 30 at idle means "off", not a measurement. Chips without that sensor (M1 Pro/Max) report the hottest SoC die sensor (PMU tdie*) instead: not GPU-specific, but under GPU load that is the sensor next to the GPU.

  • Summary: process_gpu_seconds (Process GPU Time in the CLI) is the monitored processes' total GPU time. It counts from each process's start, so an attached process includes time from before denet attached.

Caveats:

  • GPU time seems to be credited in arrears, roughly one command buffer behind (the counter didn't advance after a process's last submission). The work after a process's last submission can go uncounted: on a 4 s Metal burn with ~0.5 s command buffers, three runs read 1%, 1% and 10% low. Workloads with short command buffers lose little. For the same reason, one long kernel shows up in bursts rather than spread across the samples it spans.
  • On a big GPU the counter undercounts light work. Against Metal's own per-command-buffer GPU time (scripts/metal_burn.swift), an M1 (8 cores) agrees within 2% in every mode tried, but an M1 Max (32 cores) reads 15–35% low on grids too small to fill the GPU, and within 3% only when it is saturated (a typical ML workload):
workload M1 M1 Max
16M threads, 20 ms buffers 1.00 0.97
1M threads, 20 ms buffers 1.00 0.81
1M threads, 500 ms buffers 1.07 0.64
1M threads, 50% duty cycle 1.02 0.67
64K threads, 20 ms buffers 1.01 0.85

The likely reading is that the counter credits actual core activity while Metal's timestamps span the whole command buffer, idle cores included. system_utilization_gpu errs the other way: the 50% duty-cycle run read 70% on the M1 and 87% on the M1 Max. - If a process closes its Metal device, its time can step backwards; that interval reads 0%, and process_gpu_seconds keeps the highest value seen. - GPU time in the last interval before a process exits is lost, as with CPU time. - These sources were verified on an M1 and an M1 Max. Other chips or macOS releases may rename them; the fields then read null rather than failing. - macOS VMs (including GitHub's macOS runners) have no Apple GPU to read; --gpu warns and carries on.

Notes

  • GPU monitoring requires NVIDIA GPUs and drivers
  • NVML (NVIDIA Management Library) must be available on the system
  • If --gpu is given but no GPU is detected, denet warns and continues working normally
  • Without --gpu, the NVIDIA library is never loaded and no GPU fields are written
  • Per-process GPU utilization needs a Maxwell or newer GPU
  • Process-specific GPU memory tracking may not be available on all driver versions