master ohmu/fastfetch-openbsd / src / detection / gpu / nvml.h
ohmu  ·  2026-10-15
  1#pragma once
  2
  3// DISCLAIMER:
  4// THIS FILE IS CREATED FROM SCRATCH, BY READING THE OFFICIAL NVML API
  5// DOCUMENTATION REFERENCED BELOW, IN ORDER TO MAKE FASTFETCH MIT COMPLIANT.
  6
  7// https://docs.nvidia.com/deploy/pdf/NVML_API_Reference_Guide.pdf
  8
  9// https://docs.nvidia.com/deploy/nvml-api/group__nvmlDeviceStructs.html
 10#define NVML_DEVICE_PCI_BUS_ID_BUFFER_SIZE 32
 11#define NVML_DEVICE_PCI_BUS_ID_BUFFER_V2_SIZE 16
 12#define NVML_DEVICE_NAME_V2_BUFFER_SIZE 96
 13
 14typedef enum { NVML_SUCCESS = 0 } nvmlReturn_t;
 15typedef struct nvmlDevice_t* nvmlDevice_t;
 16#define NVML_STRUCT_VERSION(data, ver) (unsigned int)(sizeof(nvml##data##_v##ver##_t) | (ver << 24U))
 17
 18// https://docs.nvidia.com/deploy/nvml-api/structnvmlPciInfo__t.html
 19// PCI information about a GPU device
 20typedef struct {
 21    // The legacy tuple domain:bus:device.function PCI identifier (& nullptr terminator)
 22    char busIdLegacy[NVML_DEVICE_PCI_BUS_ID_BUFFER_V2_SIZE];
 23    // The PCI domain on which the device's bus resides, 0 to 0xffffffff
 24    unsigned int domain;
 25    // The bus on which the device resides, 0 to 0xff
 26    unsigned int bus;
 27    // The device's id on the bus, 0 to 31
 28    unsigned int device;
 29    // The combined 16-bit device id and 16-bit vendor id
 30    unsigned int pciDeviceId;
 31    // The 32-bit Sub System Device ID
 32    unsigned int pciSubSystemId;
 33    // The tuple domain:bus:device.function PCI identifier (& nullptr terminator)
 34    char busId[NVML_DEVICE_PCI_BUS_ID_BUFFER_SIZE];
 35} nvmlPciInfo_t;
 36
 37// https://docs.nvidia.com/deploy/nvml-api/group__nvmlDeviceEnumvs.html#group__nvmlDeviceEnumvs_1g2650b526841fa38b8f293c2d509a1de0
 38// Temperature sensors
 39typedef enum {
 40    // Temperature sensor for the GPU die
 41    NVML_TEMPERATURE_GPU = 0,
 42    NVML_TEMPERATURE_COUNT,
 43} nvmlTemperatureSensors_t;
 44
 45// https://docs.nvidia.com/deploy/nvml-api/structnvmlMemory__v2__t.html#structnvmlMemory__v2__t
 46// Memory allocation information for a device (v2)
 47typedef struct {
 48    // Structure format version (must be 2)
 49    unsigned int version;
 50    // Total physical device memory (in bytes)
 51    unsigned long long total;
 52    // Device memory (in bytes) reserved for system use (driver or firmware)
 53    unsigned long long reserved;
 54    // Unallocated device memory (in bytes)
 55    unsigned long long free;
 56    // Allocated device memory (in bytes)
 57    unsigned long long used;
 58} nvmlMemory_v2_t;
 59// https://github.com/NVIDIA/nvidia-settings/issues/78#issuecomment-1012837988
 60
 61// https://docs.nvidia.com/deploy/nvml-api/structnvmlMemory__t.html#structnvmlMemory__t
 62// Memory allocation information for a device (v1)
 63typedef struct
 64{
 65    // Total physical device memory (in bytes)
 66    unsigned long long total;
 67    // Unallocated device memory (in bytes)
 68    unsigned long long free;
 69    // Sum of Reserved and Allocated device memory (in bytes)
 70    unsigned long long used;
 71} nvmlMemory_t;
 72
 73// https://docs.nvidia.com/deploy/nvml-api/group__nvmlDeviceEnumvs.html#group__nvmlDeviceEnumvs_1g805c0647be9996589fc5e3f6ff680c64
 74// Clock types
 75typedef enum {
 76    // Graphics clock domain
 77    NVML_CLOCK_GRAPHICS = 0,
 78    // SM clock domain
 79    NVML_CLOCK_SM = 1,
 80    // Memory clock domain
 81    NVML_CLOCK_MEM = 2,
 82    // Video encoder/decoder clock domain
 83    NVML_CLOCK_VIDEO = 3,
 84    // Count of clock types
 85    NVML_CLOCK_COUNT,
 86} nvmlClockType_t;
 87
 88// https://docs.nvidia.com/deploy/nvml-api/group__nvmlDeviceEnumvs.html#group__nvmlDeviceEnumvs_1gfa6b01990b212f7b49089b7158eafd2b
 89// The Brand of the GPU
 90typedef enum {
 91    NVML_BRAND_UNKNOWN = 0,
 92    NVML_BRAND_QUADRO = 1,
 93    NVML_BRAND_TESLA = 2,
 94    NVML_BRAND_NVS = 3,
 95    NVML_BRAND_GRID = 4,
 96    NVML_BRAND_GEFORCE = 5,
 97    NVML_BRAND_TITAN = 6,
 98    NVML_BRAND_NVIDIA_VAPPS = 7,
 99    NVML_BRAND_NVIDIA_VPC = 8,
100    NVML_BRAND_NVIDIA_VCS = 9,
101    NVML_BRAND_NVIDIA_VWS = 10,
102    NVML_BRAND_NVIDIA_CLOUD_GAMING = 11,
103    NVML_BRAND_NVIDIA_VGAMING = NVML_BRAND_NVIDIA_CLOUD_GAMING,
104    NVML_BRAND_QUADRO_RTX = 12,
105    NVML_BRAND_NVIDIA_RTX = 13,
106    NVML_BRAND_NVIDIA = 14,
107    NVML_BRAND_GEFORCE_RTX = 15,
108    NVML_BRAND_TITAN_RTX = 16,
109    NVML_BRAND_COUNT,
110} nvmlBrandType_t;
111
112// https://docs.nvidia.com/deploy/nvml-api/structnvmlUtilization__t.html#structnvmlUtilization__t
113// Utilization information for a device.
114typedef struct
115{
116    // Percent of time over the past second during which one or more kernels was executing on the GPU
117    unsigned int gpu;
118    // Percent of time over the past second during which global (device) memory was being read or written
119    unsigned int memory;
120} nvmlUtilization_t;
121
122typedef struct {
123    // v1
124    unsigned int version;
125    // input
126    nvmlTemperatureSensors_t sensorType;
127    // output
128    int temperature;
129} nvmlTemperature_v1_t;
130
131// https://docs.nvidia.com/deploy/nvml-api/group__nvmlInitializationAndCleanup.html#group__nvmlInitializationAndCleanup
132// Initialize NVML, but don't initialize any GPUs yet
133extern nvmlReturn_t nvmlInit_v2(void);
134// Shut down NVML by releasing all GPU resources previously allocated with nvmlInit_v2()
135extern nvmlReturn_t nvmlShutdown(void);
136
137// https://docs.nvidia.com/deploy/nvml-api/group__nvmlDeviceQueries.html
138
139// v545
140// Retrieves the PCI attributes of this device
141extern nvmlReturn_t nvmlDeviceGetPciInfo_v3(nvmlDevice_t device, nvmlPciInfo_t* pci);
142
143// v510
144// Gets the device's core count
145extern nvmlReturn_t nvmlDeviceGetNumGpuCores(nvmlDevice_t device, unsigned int* numCores);
146// Retrieves the amount of used, free, reserved and total memory available on the device, in bytes. The reserved amount is supported on version 2 only
147extern nvmlReturn_t nvmlDeviceGetMemoryInfo_v2(nvmlDevice_t device, nvmlMemory_v2_t* memory);
148
149// v361
150// Retrieves the maximum clock speeds for the device
151extern nvmlReturn_t nvmlDeviceGetMaxClockInfo(nvmlDevice_t device, nvmlClockType_t type, unsigned int* clock);
152
153// v340
154// Retrieves the brand of this device
155extern nvmlReturn_t nvmlDeviceGetBrand(nvmlDevice_t device, nvmlBrandType_t* type);
156
157// v5.319 RC
158// Retrieves the number of compute devices in the system. A compute device is a single GPU
159extern nvmlReturn_t nvmlDeviceGetCount_v2(unsigned int* deviceCount);
160// Acquire the handle for a particular device, based on its index
161extern nvmlReturn_t nvmlDeviceGetHandleByIndex_v2(unsigned int index, nvmlDevice_t* device);
162// Retrieves the globally unique immutable UUID associated with this device, as a 5 part hexadecimal string, that augments the immutable, board serial identifier.
163extern nvmlReturn_t nvmlDeviceGetIndex(nvmlDevice_t device, unsigned int* index);
164
165// v3.295
166// Retrieves the maximum PCIe link generation possible with this device and system
167extern nvmlReturn_t nvmlDeviceGetMaxPcieLinkGeneration(nvmlDevice_t device, unsigned int* maxLinkGen);
168// Retrieves the maximum PCIe link width possible with this device and system
169extern nvmlReturn_t nvmlDeviceGetMaxPcieLinkWidth(nvmlDevice_t device, unsigned int* maxLinkWidth);
170// Retrieves the current PCIe link generation
171extern nvmlReturn_t nvmlDeviceGetCurrPcieLinkGeneration(nvmlDevice_t device, unsigned int* currLinkGen);
172// Retrieves the current PCIe link width
173extern nvmlReturn_t nvmlDeviceGetCurrPcieLinkWidth(nvmlDevice_t device, unsigned int* currLinkWidth);
174
175// v0
176// Acquire the handle for a particular device, based on its PCI bus id
177extern nvmlReturn_t nvmlDeviceGetHandleByPciBusId_v2(const char* pciBusId, nvmlDevice_t* device);
178// Retrieves the amount of used, free, total memory available on the device, in bytes.
179extern nvmlReturn_t nvmlDeviceGetMemoryInfo(nvmlDevice_t device, nvmlMemory_t* memory);
180// Retrieves the current utilization rates for the device
181extern nvmlReturn_t nvmlDeviceGetUtilizationRates(nvmlDevice_t device, nvmlUtilization_t* utilization);
182// Retrieves the name of this device.
183extern nvmlReturn_t nvmlDeviceGetName(nvmlDevice_t device, char* name, unsigned int length);
184// Retrieves the current temperature readings (in degrees C) for the given device
185extern nvmlReturn_t nvmlDeviceGetTemperatureV(nvmlDevice_t device, nvmlTemperature_v1_t* temperature);
186
187
188enum {
189    nvmlMemory_v2 = NVML_STRUCT_VERSION(Memory, 2),
190    nvmlTemperature_v1 = NVML_STRUCT_VERSION(Temperature, 1),
191};