c864391fa5360f555929947bd7b5750237abe50c
[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 HC_LOAD_FUNC(nvml, nvmlDeviceGetPowerManagementLimit, NVML_DEVICE_GET_POWERMANAGEMENTLIMIT, NVML, 0)
66
67 return 0;
68 }
69
70 void nvml_close (NVML_PTR *nvml)
71 {
72 if (nvml)
73 {
74 if (nvml->lib)
75 hc_dlclose (nvml->lib);
76
77 myfree (nvml);
78 }
79 }
80
81 const char *hm_NVML_nvmlErrorString (NVML_PTR *nvml, nvmlReturn_t nvml_rc)
82 {
83 if (!nvml) return NULL;
84
85 return nvml->nvmlErrorString (nvml_rc);
86 }
87
88 nvmlReturn_t hm_NVML_nvmlInit (NVML_PTR *nvml)
89 {
90 if (!nvml) return -1;
91
92 nvmlReturn_t nvml_rc = nvml->nvmlInit ();
93
94 if (nvml_rc != NVML_SUCCESS)
95 {
96 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
97
98 log_info ("WARN: %s %d %s\n", "nvmlInit()", nvml_rc, string);
99 }
100
101 return nvml_rc;
102 }
103
104 nvmlReturn_t hm_NVML_nvmlShutdown (NVML_PTR *nvml)
105 {
106 if (!nvml) return -1;
107
108 nvmlReturn_t nvml_rc = nvml->nvmlShutdown ();
109
110 if (nvml_rc != NVML_SUCCESS)
111 {
112 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
113
114 log_info ("WARN: %s %d %s\n", "nvmlShutdown()", nvml_rc, string);
115 }
116
117 return nvml_rc;
118 }
119
120 nvmlReturn_t hm_NVML_nvmlDeviceGetName (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, char *name, unsigned int length)
121 {
122 if (!nvml) return -1;
123
124 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetName (device, name, length);
125
126 if (nvml_rc != NVML_SUCCESS)
127 {
128 if (skip_warnings == 0)
129 {
130 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
131
132 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetName()", nvml_rc, string);
133 }
134 }
135
136 return nvml_rc;
137 }
138
139 nvmlReturn_t hm_NVML_nvmlDeviceGetHandleByIndex (NVML_PTR *nvml, int skip_warnings, unsigned int index, nvmlDevice_t *device)
140 {
141 if (!nvml) return -1;
142
143 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetHandleByIndex (index, device);
144
145 if (nvml_rc != NVML_SUCCESS)
146 {
147 if (skip_warnings == 0)
148 {
149 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
150
151 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetHandleByIndex()", nvml_rc, string);
152 }
153 }
154
155 return nvml_rc;
156 }
157
158 nvmlReturn_t hm_NVML_nvmlDeviceGetTemperature (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp)
159 {
160 if (!nvml) return -1;
161
162 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperature (device, sensorType, temp);
163
164 if (nvml_rc != NVML_SUCCESS)
165 {
166 if (skip_warnings == 0)
167 {
168 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
169
170 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetTemperature()", nvml_rc, string);
171 }
172 }
173
174 return nvml_rc;
175 }
176
177 nvmlReturn_t hm_NVML_nvmlDeviceGetFanSpeed (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *speed)
178 {
179 if (!nvml) return -1;
180
181 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetFanSpeed (device, speed);
182
183 if (nvml_rc != NVML_SUCCESS)
184 {
185 if (skip_warnings == 0)
186 {
187 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
188
189 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetFanSpeed()", nvml_rc, string);
190 }
191 }
192
193 return nvml_rc;
194 }
195
196 nvmlReturn_t hm_NVML_nvmlDeviceGetPowerUsage (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *power)
197 {
198 if (!nvml) return -1;
199
200 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetPowerUsage (device, power);
201
202 if (nvml_rc != NVML_SUCCESS)
203 {
204 if (skip_warnings == 0)
205 {
206 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
207
208 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerUsage()", nvml_rc, string);
209 }
210 }
211
212 return nvml_rc;
213 }
214
215 nvmlReturn_t hm_NVML_nvmlDeviceGetUtilizationRates (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlUtilization_t *utilization)
216 {
217 if (!nvml) return -1;
218
219 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetUtilizationRates (device, utilization);
220
221 if (nvml_rc != NVML_SUCCESS)
222 {
223 if (skip_warnings == 0)
224 {
225 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
226
227 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
228 }
229 }
230
231 return nvml_rc;
232 }
233
234 nvmlReturn_t hm_NVML_nvmlDeviceGetClockInfo (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlClockType_t type, unsigned int *clock)
235 {
236 if (!nvml) return -1;
237
238 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetClockInfo (device, type, clock);
239
240 if (nvml_rc != NVML_SUCCESS)
241 {
242 if (skip_warnings == 0)
243 {
244 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
245
246 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetClockInfo()", nvml_rc, string);
247 }
248 }
249
250 return nvml_rc;
251 }
252
253 nvmlReturn_t hm_NVML_nvmlDeviceGetTemperatureThreshold (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlTemperatureThresholds_t thresholdType, unsigned int *temp)
254 {
255 if (!nvml) return -1;
256
257 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperatureThreshold (device, thresholdType, temp);
258
259 if (nvml_rc != NVML_SUCCESS)
260 {
261 if (skip_warnings == 0)
262 {
263 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
264
265 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetTemperatureThreshold()", nvml_rc, string);
266 }
267 }
268
269 return nvml_rc;
270 }
271
272 nvmlReturn_t hm_NVML_nvmlDeviceGetCurrPcieLinkGeneration (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *currLinkGen)
273 {
274 if (!nvml) return -1;
275
276 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrPcieLinkGeneration (device, currLinkGen);
277
278 if (nvml_rc != NVML_SUCCESS)
279 {
280 if (skip_warnings == 0)
281 {
282 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
283
284 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetCurrPcieLinkGeneration()", nvml_rc, string);
285 }
286 }
287
288 return nvml_rc;
289 }
290
291 nvmlReturn_t hm_NVML_nvmlDeviceGetCurrPcieLinkWidth (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *currLinkWidth)
292 {
293 if (!nvml) return -1;
294
295 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrPcieLinkWidth (device, currLinkWidth);
296
297 if (nvml_rc != NVML_SUCCESS)
298 {
299 if (skip_warnings == 0)
300 {
301 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
302
303 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetCurrPcieLinkWidth()", nvml_rc, string);
304 }
305 }
306
307 return nvml_rc;
308 }
309
310 nvmlReturn_t hm_NVML_nvmlDeviceGetCurrentClocksThrottleReasons (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned long long *clocksThrottleReasons)
311 {
312 if (!nvml) return -1;
313
314 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrentClocksThrottleReasons (device, clocksThrottleReasons);
315
316 if (nvml_rc != NVML_SUCCESS)
317 {
318 if (skip_warnings == 0)
319 {
320 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
321
322 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetCurrentClocksThrottleReasons()", nvml_rc, string);
323 }
324 }
325
326 return nvml_rc;
327 }
328
329 nvmlReturn_t hm_NVML_nvmlDeviceGetSupportedClocksThrottleReasons (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned long long *supportedClocksThrottleReasons)
330 {
331 if (!nvml) return -1;
332
333 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetSupportedClocksThrottleReasons (device, supportedClocksThrottleReasons);
334
335 if (nvml_rc != NVML_SUCCESS)
336 {
337 if (skip_warnings == 0)
338 {
339 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
340
341 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetSupportedClocksThrottleReasons()", nvml_rc, string);
342 }
343 }
344
345 return nvml_rc;
346 }
347
348 nvmlReturn_t hm_NVML_nvmlDeviceSetComputeMode (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlComputeMode_t mode)
349 {
350 if (!nvml) return -1;
351
352 nvmlReturn_t nvml_rc = nvml->nvmlDeviceSetComputeMode (device, mode);
353
354 if (nvml_rc != NVML_SUCCESS)
355 {
356 if (skip_warnings == 0)
357 {
358 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
359
360 log_info ("WARN: %s %d %s\n", "nvmlDeviceSetComputeMode()", nvml_rc, string);
361 }
362 }
363
364 return nvml_rc;
365 }
366
367 nvmlReturn_t hm_NVML_nvmlDeviceSetGpuOperationMode (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, nvmlGpuOperationMode_t mode)
368 {
369 if (!nvml) return -1;
370
371 nvmlReturn_t nvml_rc = nvml->nvmlDeviceSetGpuOperationMode (device, mode);
372
373 if (nvml_rc != NVML_SUCCESS)
374 {
375 if (skip_warnings == 0)
376 {
377 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
378
379 log_info ("WARN: %s %d %s\n", "nvmlDeviceSetGpuOperationMode()", nvml_rc, string);
380 }
381 }
382
383 return nvml_rc;
384 }
385
386 nvmlReturn_t hm_NVML_nvmlDeviceGetPowerManagementLimitConstraints (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *minLimit, unsigned int *maxLimit)
387 {
388 if (!nvml) return -1;
389
390 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetPowerManagementLimitConstraints (device, minLimit, maxLimit);
391
392 if (nvml_rc != NVML_SUCCESS)
393 {
394 if (skip_warnings == 0)
395 {
396 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
397
398 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerManagementLimitConstraints()", nvml_rc, string);
399 }
400 }
401
402 return nvml_rc;
403 }
404
405 nvmlReturn_t hm_NVML_nvmlDeviceSetPowerManagementLimit (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int limit)
406 {
407 if (!nvml) return -1;
408
409 nvmlReturn_t nvml_rc = nvml->nvmlDeviceSetPowerManagementLimit (device, limit);
410
411 if (nvml_rc != NVML_SUCCESS)
412 {
413 if (skip_warnings == 0)
414 {
415 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
416
417 log_info ("WARN: %s %d %s\n", "nvmlDeviceSetPowerManagementLimit()", nvml_rc, string);
418 }
419 }
420
421 return nvml_rc;
422 }
423
424 nvmlReturn_t hm_NVML_nvmlDeviceGetPowerManagementLimit (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *limit)
425 {
426 if (!nvml) return -1;
427
428 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetPowerManagementLimit (device, limit);
429
430 if (nvml_rc != NVML_SUCCESS)
431 {
432 if (skip_warnings == 0)
433 {
434 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
435
436 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerManagementLimit()", nvml_rc, string);
437 }
438 }
439
440 return nvml_rc;
441 }