Class LinuxGpuStats

java.lang.Object
oshi.hardware.common.platform.linux.LinuxGpuStats
All Implemented Interfaces:
AutoCloseable, GpuStats

@ThreadSafe public abstract class LinuxGpuStats extends Object implements GpuStats
Linux GpuStats session. Dynamic metrics are sourced in priority order: NVML (NVIDIA GPUs), then sysfs DRM driver files under /sys/class/drm/cardN/device/. The hwmon path and driver-specific sysfs paths are resolved once at construction time.

GPU ticks are not available on Linux and always return (0L, 0L). Shared memory is not available and always returns -1.

Subclasses provide the NVML integration via JNA or FFM by implementing the nvml* methods.

  • Constructor Details

    • LinuxGpuStats

      protected LinuxGpuStats(String drmDevicePath, String driverName, String pciBusId, String cardName)
      Constructor.
      Parameters:
      drmDevicePath - sysfs device path
      driverName - driver name
      pciBusId - PCI bus ID for NVML correlation
      cardName - card name for NVML fallback lookup
  • Method Details

    • getPciBusId

      protected String getPciBusId()
      Returns the PCI bus ID.
      Returns:
      PCI bus ID string
    • getCardName

      protected String getCardName()
      Returns the card name.
      Returns:
      card name string
    • nvmlIsAvailable

      protected abstract boolean nvmlIsAvailable()
      Returns whether NVML is available.
      Returns:
      true if NVML can be used
    • nvmlFindDevice

      protected abstract String nvmlFindDevice(String busId)
      Finds the NVML device by PCI bus ID.
      Parameters:
      busId - PCI bus ID
      Returns:
      device identifier string, or null
    • nvmlFindDeviceByName

      protected abstract String nvmlFindDeviceByName(String name)
      Finds the NVML device by GPU name.
      Parameters:
      name - GPU name
      Returns:
      device identifier string, or null
    • nvmlGetVramUsed

      protected abstract long nvmlGetVramUsed(String deviceId)
      Returns VRAM used in bytes via NVML, or -1.
      Parameters:
      deviceId - NVML device identifier
      Returns:
      bytes used or -1
    • nvmlGetTemperature

      protected abstract double nvmlGetTemperature(String deviceId)
      Returns GPU temperature via NVML, or -1.
      Parameters:
      deviceId - NVML device identifier
      Returns:
      temperature in °C or -1
    • nvmlGetPowerDraw

      protected abstract double nvmlGetPowerDraw(String deviceId)
      Returns GPU power draw via NVML, or -1.
      Parameters:
      deviceId - NVML device identifier
      Returns:
      power in watts or -1
    • nvmlGetCoreClockMhz

      protected abstract long nvmlGetCoreClockMhz(String deviceId)
      Returns GPU core clock via NVML, or -1.
      Parameters:
      deviceId - NVML device identifier
      Returns:
      core clock in MHz or -1
    • nvmlGetMemoryClockMhz

      protected abstract long nvmlGetMemoryClockMhz(String deviceId)
      Returns GPU memory clock via NVML, or -1.
      Parameters:
      deviceId - NVML device identifier
      Returns:
      memory clock in MHz or -1
    • nvmlGetFanSpeedPercent

      protected abstract double nvmlGetFanSpeedPercent(String deviceId)
      Returns GPU fan speed via NVML, or -1.
      Parameters:
      deviceId - NVML device identifier
      Returns:
      fan speed percentage or -1
    • findNvmlDevice

      protected String findNvmlDevice()
      Resolves the NVML device identifier, caching the result.
      Returns:
      device identifier, or null if unavailable
    • close

      public void close()
      Description copied from interface: GpuStats
      Releases any native resources held by this session. Safe to call multiple times; subsequent calls after the first are no-ops. Does not throw checked exceptions.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface GpuStats
    • isClosed

      public boolean isClosed()
      Description copied from interface: GpuStats
      Returns true if GpuStats.close() has been called on this session. Does not throw.
      Specified by:
      isClosed in interface GpuStats
      Returns:
      true if this session is closed
    • getGpuTicks

      public GpuTicks getGpuTicks()
      Description copied from interface: GpuStats
      Returns a snapshot of cumulative GPU active and idle ticks in opaque, platform-native units. The counters are monotonically increasing; diff two snapshots to compute utilization:
      long dActive = curr.getActiveTicks() - prev.getActiveTicks();
      long dIdle = curr.getIdleTicks() - prev.getIdleTicks();
      long dTotal = dActive + dIdle;
      double utilPct = dTotal > 0 ? dActive * 100.0 / dTotal : -1d;
      

      Both counters are 0 on platforms where tick-level GPU metrics are not available (see GpuTicks for the sentinel semantics). Use GpuStats.getGpuUtilization() as an alternative.

      Specified by:
      getGpuTicks in interface GpuStats
      Returns:
      a GpuTicks snapshot; never null
    • getGpuUtilization

      public double getGpuUtilization()
      Description copied from interface: GpuStats
      Returns the instantaneous GPU core utilization as a percentage, computed internally as a delta between the current sample and the previous one recorded by this session.

      Behaviour on the first call is implementation-dependent. Backends that derive utilization from an energy or residency counter record an initial baseline on the first call and return -1; subsequent calls return the utilization computed over the elapsed interval. Backends that read an instantaneous hardware register may return a valid value immediately. To ensure the first polling iteration returns a valid value on delta-based backends, call this method once as a priming step before the polling loop begins:

      stats.getGpuUtilization(); // prime — may return -1 on delta-based backends
      Thread.sleep(intervalMs);
      double util = stats.getGpuUtilization(); // valid on all backends
      
      Specified by:
      getGpuUtilization in interface GpuStats
      Returns:
      utilization in the range 0.0 to 100.0, or -1 if not available or not yet primed
    • getVramUsed

      public long getVramUsed()
      Description copied from interface: GpuStats
      Returns the amount of dedicated VRAM currently in use.
      Specified by:
      getVramUsed in interface GpuStats
      Returns:
      bytes of VRAM in use, or -1 if unavailable
    • getSharedMemoryUsed

      public long getSharedMemoryUsed()
      Description copied from interface: GpuStats
      Returns the amount of shared system memory currently used by this GPU.
      Specified by:
      getSharedMemoryUsed in interface GpuStats
      Returns:
      bytes of shared memory in use, or -1 if unavailable
    • getTemperature

      public double getTemperature()
      Description copied from interface: GpuStats
      Returns the GPU temperature.
      Specified by:
      getTemperature in interface GpuStats
      Returns:
      temperature in degrees Celsius, or -1 if unavailable
    • getPowerDraw

      public double getPowerDraw()
      Description copied from interface: GpuStats
      Returns the GPU power consumption. On backends that derive power from an energy counter (e.g. macOS Apple Silicon via IOReport), this is computed as a delta between the current sample and the previous one; the first call records the initial baseline and returns -1. On backends that read an instantaneous hardware sensor (e.g. Windows via NVML/ADL/LHM), a valid value may be returned immediately.

      To ensure the first polling iteration returns a valid value on delta-based backends, call this method once as a priming step before the polling loop begins:

      stats.getPowerDraw(); // prime — may return -1 on delta-based backends
      Thread.sleep(intervalMs);
      double watts = stats.getPowerDraw(); // valid on all backends
      
      Specified by:
      getPowerDraw in interface GpuStats
      Returns:
      power draw in watts, or -1 if unavailable or not yet primed
    • getCoreClockMhz

      public long getCoreClockMhz()
      Description copied from interface: GpuStats
      Returns the current GPU core clock speed.
      Specified by:
      getCoreClockMhz in interface GpuStats
      Returns:
      core clock in MHz, or -1 if unavailable
    • getMemoryClockMhz

      public long getMemoryClockMhz()
      Description copied from interface: GpuStats
      Returns the current GPU memory clock speed.
      Specified by:
      getMemoryClockMhz in interface GpuStats
      Returns:
      memory clock in MHz, or -1 if unavailable
    • getFanSpeedPercent

      public double getFanSpeedPercent()
      Description copied from interface: GpuStats
      Returns the GPU fan speed as a percentage of maximum.
      Specified by:
      getFanSpeedPercent in interface GpuStats
      Returns:
      fan speed in the range 0.0 to 100.0, or -1 if unavailable