Finally got rid of NvAPI on Windows, replace with NVML
[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
60 return 0;
61 }
62
63 void nvml_close (NVML_PTR *nvml)
64 {
65 if (nvml)
66 {
67 if (nvml->lib)
68 hc_dlclose (nvml->lib);
69
70 myfree (nvml);
71 }
72 }
73
74 const char *hm_NVML_nvmlErrorString (NVML_PTR *nvml, nvmlReturn_t nvml_rc)
75 {
76 if (!nvml) return NULL;
77
78 return nvml->nvmlErrorString (nvml_rc);
79 }
80
81 nvmlReturn_t hm_NVML_nvmlInit (NVML_PTR *nvml)
82 {
83 if (!nvml) return -1;
84
85 nvmlReturn_t nvml_rc = nvml->nvmlInit ();
86
87 if (nvml_rc != NVML_SUCCESS)
88 {
89 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
90
91 log_info ("WARN: %s %d %s\n", "nvmlInit()", nvml_rc, string);
92 }
93
94 return nvml_rc;
95 }
96
97 nvmlReturn_t hm_NVML_nvmlShutdown (NVML_PTR *nvml)
98 {
99 if (!nvml) return -1;
100
101 nvmlReturn_t nvml_rc = nvml->nvmlShutdown ();
102
103 if (nvml_rc != NVML_SUCCESS)
104 {
105 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
106
107 log_info ("WARN: %s %d %s\n", "nvmlShutdown()", nvml_rc, string);
108 }
109
110 return nvml_rc;
111 }
112
113 nvmlReturn_t hm_NVML_nvmlDeviceGetName (NVML_PTR *nvml, nvmlDevice_t device, char *name, unsigned int length)
114 {
115 if (!nvml) return -1;
116
117 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetName (device, name, length);
118
119 if (nvml_rc != NVML_SUCCESS)
120 {
121 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
122
123 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetName()", nvml_rc, string);
124 }
125
126 return nvml_rc;
127 }
128
129 nvmlReturn_t hm_NVML_nvmlDeviceGetHandleByIndex (NVML_PTR *nvml, int skip_warnings, unsigned int index, nvmlDevice_t *device)
130 {
131 if (!nvml) return -1;
132
133 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetHandleByIndex (index, device);
134
135 if (nvml_rc != NVML_SUCCESS)
136 {
137 if (skip_warnings == 0)
138 {
139 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
140
141 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetHandleByIndex()", nvml_rc, string);
142 }
143 }
144
145 return nvml_rc;
146 }
147
148 nvmlReturn_t hm_NVML_nvmlDeviceGetTemperature (NVML_PTR *nvml, nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp)
149 {
150 if (!nvml) return -1;
151
152 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperature (device, sensorType, temp);
153
154 if (nvml_rc != NVML_SUCCESS)
155 {
156 *temp = -1;
157
158 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
159
160 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetTemperature()", nvml_rc, string);
161 }
162
163 return nvml_rc;
164 }
165
166 nvmlReturn_t hm_NVML_nvmlDeviceGetFanSpeed (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *speed)
167 {
168 if (!nvml) return -1;
169
170 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetFanSpeed (device, speed);
171
172 if (nvml_rc != NVML_SUCCESS)
173 {
174 *speed = -1;
175
176 if (skip_warnings == 0)
177 {
178 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
179
180 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetFanSpeed()", nvml_rc, string);
181 }
182 }
183
184 return nvml_rc;
185 }
186
187 /* only tesla following */
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 *power = -1;
198
199 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
200
201 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerUsage()", nvml_rc, string);
202 }
203
204 return nvml_rc;
205 }
206
207 nvmlReturn_t hm_NVML_nvmlDeviceGetUtilizationRates (NVML_PTR *nvml, nvmlDevice_t device, nvmlUtilization_t *utilization)
208 {
209 if (!nvml) return -1;
210
211 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetUtilizationRates (device, utilization);
212
213 if (nvml_rc != NVML_SUCCESS)
214 {
215 utilization->gpu = -1;
216 utilization->memory = -1;
217
218 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
219
220 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
221 }
222
223 return nvml_rc;
224 }
225
226 nvmlReturn_t hm_NVML_nvmlDeviceGetClockInfo (NVML_PTR *nvml, nvmlDevice_t device, nvmlClockType_t type, unsigned int *clock)
227 {
228 if (!nvml) return -1;
229
230 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetClockInfo (device, type, clock);
231
232 if (nvml_rc != NVML_SUCCESS)
233 {
234 *clock = -1;
235
236 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
237
238 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
239 }
240
241 return nvml_rc;
242 }
243
244 nvmlReturn_t hm_NVML_nvmlDeviceGetTemperatureThreshold (NVML_PTR *nvml, nvmlDevice_t device, nvmlTemperatureThresholds_t thresholdType, unsigned int *temp)
245 {
246 if (!nvml) return -1;
247
248 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperatureThreshold (device, thresholdType, temp);
249
250 if (nvml_rc != NVML_SUCCESS)
251 {
252 *temp = -1;
253
254 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
255
256 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
257 }
258
259 return nvml_rc;
260 }
261
262 nvmlReturn_t hm_NVML_nvmlDeviceGetCurrPcieLinkGeneration (NVML_PTR *nvml, nvmlDevice_t device, unsigned int *currLinkGen)
263 {
264 if (!nvml) return -1;
265
266 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrPcieLinkGeneration (device, currLinkGen);
267
268 if (nvml_rc != NVML_SUCCESS)
269 {
270 *currLinkGen = -1;
271
272 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
273
274 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
275 }
276
277 return nvml_rc;
278 }
279
280 nvmlReturn_t hm_NVML_nvmlDeviceGetCurrPcieLinkWidth (NVML_PTR *nvml, nvmlDevice_t device, unsigned int *currLinkWidth)
281 {
282 if (!nvml) return -1;
283
284 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrPcieLinkWidth (device, currLinkWidth);
285
286 if (nvml_rc != NVML_SUCCESS)
287 {
288 *currLinkWidth = -1;
289
290 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
291
292 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
293 }
294
295 return nvml_rc;
296 }