1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
public class NvmlStateHelper { const string NVML_SHARED_LIBRARY_STRING = "nvml.dll";
const int NVML_DEVICE_NAME_BUFFER_SIZE = 64;
const int NVML_DEVICE_UUID_BUFFER_SIZE = 80;
[DllImport(NVML_SHARED_LIBRARY_STRING, EntryPoint = "nvmlInit_v2")] internal static extern NvmlReturn NvmlInitV2();
[DllImport(NVML_SHARED_LIBRARY_STRING, CharSet = CharSet.Ansi, EntryPoint = "nvmlDeviceGetCount_v2")] internal static extern NvmlReturn NvmlDeviceGetCount_v2(out uint deviceCount);
[DllImport(NVML_SHARED_LIBRARY_STRING, EntryPoint = "nvmlDeviceGetHandleByIndex")] internal static extern NvmlReturn NvmlDeviceGetHandleByIndex(uint index, out IntPtr device);
[DllImport(NVML_SHARED_LIBRARY_STRING, CharSet = CharSet.Ansi, EntryPoint = "nvmlDeviceGetName")] internal static extern NvmlReturn NvmlDeviceGetName(IntPtr device, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] name, uint length);
[DllImport(NVML_SHARED_LIBRARY_STRING, CharSet = CharSet.Ansi, EntryPoint = "nvmlDeviceGetUUID")] internal static extern NvmlReturn NvmlDeviceGetUUID(IntPtr device, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] uuid, uint length);
[DllImport(NVML_SHARED_LIBRARY_STRING, CharSet = CharSet.Ansi, EntryPoint = "nvmlDeviceGetUtilizationRates")] internal static extern NvmlReturn NvmlDeviceGetUtilizationRates(IntPtr device, out NvmlUtilization utilization);
[DllImport(NVML_SHARED_LIBRARY_STRING, EntryPoint = "nvmlShutdown")] internal static extern NvmlReturn NvmlShutdown();
public NvmlStateHelper(uint gpuCount = 0) { Task.Run(() => { NvmlReturn res = NvmlInitV2(); if (NvmlReturn.NVML_SUCCESS != res) { return; }
var device = IntPtr.Zero; res = NvmlDeviceGetHandleByIndex(gpuCount, out device); if (NvmlReturn.NVML_SUCCESS != res) { return; }
byte[] bufferName = new byte[NVML_DEVICE_NAME_BUFFER_SIZE]; res = NvmlDeviceGetName(device, bufferName, NVML_DEVICE_NAME_BUFFER_SIZE); if (NvmlReturn.NVML_SUCCESS == res) { var gpuName = Encoding.Default.GetString(bufferName).Replace("\0", ""); }
byte[] bufferUUID = new byte[NVML_DEVICE_UUID_BUFFER_SIZE]; res = NvmlDeviceGetUUID(device, bufferUUID, NVML_DEVICE_UUID_BUFFER_SIZE); if (NvmlReturn.NVML_SUCCESS == res) { var gpuUUID = Encoding.Default.GetString(bufferUUID).Replace("\0", ""); }
while (true) { try { NvmlUtilization nvmlUtilization; res = NvmlDeviceGetUtilizationRates(device, out nvmlUtilization); if (NvmlReturn.NVML_SUCCESS != res) { return; }
GpuChange?.Invoke(nvmlUtilization.gpu); MemoryChange?.Invoke(nvmlUtilization.memory); } catch (Exception) { }
Thread.Sleep(1000); } }); }
public event Action<uint> GpuChange;
public event Action<uint> MemoryChange; }
public enum NvmlReturn { NVML_SUCCESS = 0, NVML_ERROR_UNINITIALIZED, NVML_ERROR_INVALID_ARGUMENT, NVML_ERROR_NOT_SUPPORTED, NVML_ERROR_NO_PERMISSION, NVML_ERROR_ALREADY_INITIALIZED, NVML_ERROR_NOT_FOUND, NVML_ERROR_INSUFFICIENT_SIZE, NVML_ERROR_INSUFFICIENT_POWER, NVML_ERROR_DRIVER_NOT_LOADED, NVML_ERROR_TIMEOUT, NVML_ERROR_IRQ_ISSUE, NVML_ERROR_LIBRARY_NOT_FOUND, NVML_ERROR_FUNCTION_NOT_FOUND, NVML_ERROR_CORRUPTED_INFOROM, NVML_ERROR_GPU_IS_LOST, NVML_ERROR_RESET_REQUIRED, NVML_ERROR_OPERATING_SYSTEM, NVML_ERROR_LIB_RM_VERSION_MISMATCH, NVML_ERROR_IN_USE, NVML_ERROR_MEMORY, NVML_ERROR_NO_DATA, NVML_ERROR_VGPU_ECC_NOT_SUPPORTED, NVML_ERROR_INSUFFICIENT_RESOURCES, NVML_ERROR_UNKNOWN = 999 }
public struct NvmlUtilization { public uint gpu { get; } public uint memory { get; } }
|