Added a ton of new NVML stuff
[hashcat.git] / src / ext_nvml.c
1 /**
2 * Authors.....: Jens Steube <jens.steube@gmail.com>
3 * Gabriele Gristina <matrix@hashcat.net>
4 *
5 * License.....: MIT
6 */
7
8 #include <ext_nvml.h>
9
10 int nvml_init (NVML_PTR *nvml)
11 {
12 if (!nvml) return (-1);
13
14 memset (nvml, 0, sizeof (NVML_PTR));
15
16 #ifdef _WIN
17 nvml->lib = hc_dlopen ("nvml.dll");
18
19 if (!nvml->lib)
20 {
21 DWORD BufferSize = 1024;
22
23 char *Buffer = (char *) mymalloc (BufferSize);
24
25 RegGetValue (HKEY_LOCAL_MACHINE, "SOFTWARE\\NVIDIA Corporation\\Global\\NVSMI", "NVSMIPATH", RRF_RT_ANY, NULL, (PVOID) Buffer, &BufferSize);
26
27 strcat (Buffer, "\\nvml.dll");
28
29 nvml->lib = hc_dlopen (Buffer);
30
31 myfree (Buffer);
32 }
33
34 #elif _POSIX
35 nvml->lib = hc_dlopen ("libnvidia-ml.so", RTLD_NOW);
36 #endif
37
38 if (!nvml->lib)
39 {
40 if (data.quiet == 0)
41 log_info ("WARNING: load NVML library failed, proceed without NVML HWMon enabled.");
42
43 return (-1);
44 }
45
46 HC_LOAD_FUNC(nvml, nvmlErrorString, NVML_ERROR_STRING, NVML, 0)
47 HC_LOAD_FUNC(nvml, nvmlInit, NVML_INIT, NVML, 0)
48 HC_LOAD_FUNC(nvml, nvmlShutdown, NVML_SHUTDOWN, NVML, 0)
49 HC_LOAD_FUNC(nvml, nvmlDeviceGetName, NVML_DEVICE_GET_NAME, NVML, 0)
50 HC_LOAD_FUNC(nvml, nvmlDeviceGetHandleByIndex, NVML_DEVICE_GET_HANDLE_BY_INDEX, NVML, 0)
51 HC_LOAD_FUNC(nvml, nvmlDeviceGetTemperature, NVML_DEVICE_GET_TEMPERATURE, NVML, 0)
52 HC_LOAD_FUNC(nvml, nvmlDeviceGetFanSpeed, NVML_DEVICE_GET_FAN_SPEED, NVML, 0)
53 HC_LOAD_FUNC(nvml, nvmlDeviceGetPowerUsage, NVML_DEVICE_GET_POWER_USAGE, NVML, 0)
54 HC_LOAD_FUNC(nvml, nvmlDeviceGetUtilizationRates, NVML_DEVICE_GET_UTILIZATION_RATES, NVML, 0)
55 HC_LOAD_FUNC(nvml, nvmlDeviceGetClockInfo, NVML_DEVICE_GET_CLOCKINFO, NVML, 0)
56 HC_LOAD_FUNC(nvml, nvmlDeviceGetTemperatureThreshold, NVML_DEVICE_GET_THRESHOLD, NVML, 0)
57 HC_LOAD_FUNC(nvml, nvmlDeviceGetCurrPcieLinkGeneration, NVML_DEVICE_GET_CURRPCIELINKGENERATION, NVML, 0)
58 HC_LOAD_FUNC(nvml, nvmlDeviceGetCurrPcieLinkWidth, NVML_DEVICE_GET_CURRPCIELINKWIDTH, NVML, 0)
59 HC_LOAD_FUNC(nvml, nvmlDeviceGetCurrentClocksThrottleReasons, NVML_DEVICE_GET_CURRENTCLOCKSTHROTTLEREASONS, NVML, 0)
60 HC_LOAD_FUNC(nvml, nvmlDeviceGetSupportedClocksThrottleReasons, NVML_DEVICE_GET_SUPPORTEDCLOCKSTHROTTLEREASONS, NVML, 0)
61 HC_LOAD_FUNC(nvml, nvmlDeviceSetComputeMode, NVML_DEVICE_SET_COMPUTEMODE, NVML, 0)
62 HC_LOAD_FUNC(nvml, nvmlDeviceSetGpuOperationMode, NVML_DEVICE_SET_OPERATIONMODE, NVML, 0)
63 HC_LOAD_FUNC(nvml, nvmlDeviceGetPowerManagementLimitConstraints, NVML_DEVICE_GET_POWERMANAGEMENTLIMITCONSTRAINTS, NVML, 0)
64 HC_LOAD_FUNC(nvml, nvmlDeviceSetPowerManagementLimit, NVML_DEVICE_SET_POWERMANAGEMENTLIMIT, NVML, 0)
65
66 return 0;
67 }
68
69 void nvml_close (NVML_PTR *nvml)
70 {
71 if (nvml)
72 {
73 if (nvml->lib)
74 hc_dlclose (nvml->lib);
75
76 myfree (nvml);
77 }
78 }
79
80 const char *hm_NVML_nvmlErrorString (NVML_PTR *nvml, nvmlReturn_t nvml_rc)
81 {
82 if (!nvml) return NULL;
83
84 return nvml->nvmlErrorString (nvml_rc);
85 }
86
87 nvmlReturn_t hm_NVML_nvmlInit (NVML_PTR *nvml)
88 {
89 if (!nvml) return -1;
90
91 nvmlReturn_t nvml_rc = nvml->nvmlInit ();
92
93 if (nvml_rc != NVML_SUCCESS)
94 {
95 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
96
97 log_info ("WARN: %s %d %s\n", "nvmlInit()", nvml_rc, string);
98 }
99
100 return nvml_rc;
101 }
102
103 nvmlReturn_t hm_NVML_nvmlShutdown (NVML_PTR *nvml)
104 {
105 if (!nvml) return -1;
106
107 nvmlReturn_t nvml_rc = nvml->nvmlShutdown ();
108
109 if (nvml_rc != NVML_SUCCESS)
110 {
111 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
112
113 log_info ("WARN: %s %d %s\n", "nvmlShutdown()", nvml_rc, string);
114 }
115
116 return nvml_rc;
117 }
118
119 nvmlReturn_t hm_NVML_nvmlDeviceGetName (NVML_PTR *nvml, nvmlDevice_t device, char *name, unsigned int length)
120 {
121 if (!nvml) return -1;
122
123 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetName (device, name, length);
124
125 if (nvml_rc != NVML_SUCCESS)
126 {
127 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
128
129 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetName()", nvml_rc, string);
130 }
131
132 return nvml_rc;
133 }
134
135 nvmlReturn_t hm_NVML_nvmlDeviceGetHandleByIndex (NVML_PTR *nvml, int skip_warnings, unsigned int index, nvmlDevice_t *device)
136 {
137 if (!nvml) return -1;
138
139 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetHandleByIndex (index, device);
140
141 if (nvml_rc != NVML_SUCCESS)
142 {
143 if (skip_warnings == 0)
144 {
145 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
146
147 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetHandleByIndex()", nvml_rc, string);
148 }
149 }
150
151 return nvml_rc;
152 }
153
154 nvmlReturn_t hm_NVML_nvmlDeviceGetTemperature (NVML_PTR *nvml, nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp)
155 {
156 if (!nvml) return -1;
157
158 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperature (device, sensorType, temp);
159
160 if (nvml_rc != NVML_SUCCESS)
161 {
162 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
163
164 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetTemperature()", nvml_rc, string);
165 }
166
167 return nvml_rc;
168 }
169
170 nvmlReturn_t hm_NVML_nvmlDeviceGetFanSpeed (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *speed)
171 {
172 if (!nvml) return -1;
173
174 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetFanSpeed (device, speed);
175
176 if (nvml_rc != NVML_SUCCESS)
177 {
178 if (skip_warnings == 0)
179 {
180 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
181
182 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetFanSpeed()", nvml_rc, string);
183 }
184 }
185
186 return nvml_rc;
187 }
188
189 nvmlReturn_t hm_NVML_nvmlDeviceGetPowerUsage (NVML_PTR *nvml, nvmlDevice_t device, unsigned int *power)
190 {
191 if (!nvml) return -1;
192
193 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetPowerUsage (device, power);
194
195 if (nvml_rc != NVML_SUCCESS)
196 {
197 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
198
199 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerUsage()", nvml_rc, string);
200 }
201
202 return nvml_rc;
203 }
204
205 nvmlReturn_t hm_NVML_nvmlDeviceGetUtilizationRates (NVML_PTR *nvml, nvmlDevice_t device, nvmlUtilization_t *utilization)
206 {
207 if (!nvml) return -1;
208
209 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetUtilizationRates (device, utilization);
210
211 if (nvml_rc != NVML_SUCCESS)
212 {
213 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
214
215 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
216 }
217
218 return nvml_rc;
219 }
220
221 nvmlReturn_t hm_NVML_nvmlDeviceGetClockInfo (NVML_PTR *nvml, nvmlDevice_t device, nvmlClockType_t type, unsigned int *clock)
222 {
223 if (!nvml) return -1;
224
225 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetClockInfo (device, type, clock);
226
227 if (nvml_rc != NVML_SUCCESS)
228 {
229 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
230
231 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
232 }
233
234 return nvml_rc;
235 }
236
237 nvmlReturn_t hm_NVML_nvmlDeviceGetTemperatureThreshold (NVML_PTR *nvml, nvmlDevice_t device, nvmlTemperatureThresholds_t thresholdType, unsigned int *temp)
238 {
239 if (!nvml) return -1;
240
241 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperatureThreshold (device, thresholdType, temp);
242
243 if (nvml_rc != NVML_SUCCESS)
244 {
245 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
246
247 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetTemperatureThreshold()", nvml_rc, string);
248 }
249
250 return nvml_rc;
251 }
252
253 nvmlReturn_t hm_NVML_nvmlDeviceGetCurrPcieLinkGeneration (NVML_PTR *nvml, nvmlDevice_t device, unsigned int *currLinkGen)
254 {
255 if (!nvml) return -1;
256
257 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrPcieLinkGeneration (device, currLinkGen);
258
259 if (nvml_rc != NVML_SUCCESS)
260 {
261 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
262
263 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
264 }
265
266 return nvml_rc;
267 }
268
269 nvmlReturn_t hm_NVML_nvmlDeviceGetCurrPcieLinkWidth (NVML_PTR *nvml, nvmlDevice_t device, unsigned int *currLinkWidth)
270 {
271 if (!nvml) return -1;
272
273 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrPcieLinkWidth (device, currLinkWidth);
274
275 if (nvml_rc != NVML_SUCCESS)
276 {
277 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
278
279 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
280 }
281
282 return nvml_rc;
283 }
284
285 nvmlReturn_t hm_NVML_nvmlDeviceGetCurrentClocksThrottleReasons (NVML_PTR *nvml, nvmlDevice_t device, unsigned long long *clocksThrottleReasons)
286 {
287 if (!nvml) return -1;
288
289 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrentClocksThrottleReasons (device, clocksThrottleReasons);
290
291 if (nvml_rc != NVML_SUCCESS)
292 {
293 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
294
295 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
296 }
297
298 return nvml_rc;
299 }
300
301 nvmlReturn_t hm_NVML_nvmlDeviceGetSupportedClocksThrottleReasons (NVML_PTR *nvml, nvmlDevice_t device, unsigned long long *supportedClocksThrottleReasons)
302 {
303 if (!nvml) return -1;
304
305 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetSupportedClocksThrottleReasons (device, supportedClocksThrottleReasons);
306
307 if (nvml_rc != NVML_SUCCESS)
308 {
309 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
310
311 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetSupportedClocksThrottleReasons()", nvml_rc, string);
312 }
313
314 return nvml_rc;
315 }
316
317 nvmlReturn_t hm_NVML_nvmlDeviceSetComputeMode (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlComputeMode_t mode)
318 {
319 if (!nvml) return -1;
320
321 nvmlReturn_t nvml_rc = nvml->nvmlDeviceSetComputeMode (device, mode);
322
323 if (nvml_rc != NVML_SUCCESS)
324 {
325 if (skip_warnings == 0)
326 {
327 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
328
329 log_info ("WARN: %s %d %s\n", "nvmlDeviceSetComputeMode()", nvml_rc, string);
330 }
331 }
332
333 return nvml_rc;
334 }
335
336 nvmlReturn_t hm_NVML_nvmlDeviceSetGpuOperationMode (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlGpuOperationMode_t mode)
337 {
338 if (!nvml) return -1;
339
340 nvmlReturn_t nvml_rc = nvml->nvmlDeviceSetGpuOperationMode (device, mode);
341
342 if (nvml_rc != NVML_SUCCESS)
343 {
344 if (skip_warnings == 0)
345 {
346 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
347
348 log_info ("WARN: %s %d %s\n", "nvmlDeviceSetGpuOperationMode()", nvml_rc, string);
349 }
350 }
351
352 return nvml_rc;
353 }
354
355 nvmlReturn_t hm_NVML_nvmlDeviceGetPowerManagementLimitConstraints (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *minLimit, unsigned int *maxLimit)
356 {
357 if (!nvml) return -1;
358
359 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetPowerManagementLimitConstraints (device, minLimit, maxLimit);
360
361 if (nvml_rc != NVML_SUCCESS)
362 {
363 if (skip_warnings == 0)
364 {
365 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
366
367 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerManagementLimitConstraints()", nvml_rc, string);
368 }
369 }
370
371 return nvml_rc;
372 }
373
374 nvmlReturn_t hm_NVML_nvmlDeviceSetPowerManagementLimit (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int limit)
375 {
376 if (!nvml) return -1;
377
378 nvmlReturn_t nvml_rc = nvml->nvmlDeviceSetPowerManagementLimit (device, limit);
379
380 if (nvml_rc != NVML_SUCCESS)
381 {
382 if (skip_warnings == 0)
383 {
384 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
385
386 log_info ("WARN: %s %d %s\n", "nvmlDeviceSetPowerManagementLimit()", nvml_rc, string);
387 }
388 }
389
390 return nvml_rc;
391 }