Add NVML support for PCIE Lanes
[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 nvml->lib = hc_dlopen ("libnvidia-ml.so", RTLD_NOW);
17
18 if (!nvml->lib)
19 {
20 //if (data.quiet == 0)
21 // log_info ("WARNING: load NVML library failed, proceed without NVML HWMon enabled.");
22
23 return (-1);
24 }
25
26 HC_LOAD_FUNC(nvml, nvmlErrorString, NVML_ERROR_STRING, NVML, 0)
27 HC_LOAD_FUNC(nvml, nvmlInit, NVML_INIT, NVML, 0)
28 HC_LOAD_FUNC(nvml, nvmlShutdown, NVML_SHUTDOWN, NVML, 0)
29 HC_LOAD_FUNC(nvml, nvmlDeviceGetName, NVML_DEVICE_GET_NAME, NVML, 0)
30 HC_LOAD_FUNC(nvml, nvmlDeviceGetHandleByIndex, NVML_DEVICE_GET_HANDLE_BY_INDEX, NVML, 0)
31 HC_LOAD_FUNC(nvml, nvmlDeviceGetTemperature, NVML_DEVICE_GET_TEMPERATURE, NVML, 0)
32 HC_LOAD_FUNC(nvml, nvmlDeviceGetFanSpeed, NVML_DEVICE_GET_FAN_SPEED, NVML, 0)
33 HC_LOAD_FUNC(nvml, nvmlDeviceGetPowerUsage, NVML_DEVICE_GET_POWER_USAGE, NVML, 0)
34 HC_LOAD_FUNC(nvml, nvmlDeviceGetUtilizationRates, NVML_DEVICE_GET_UTILIZATION_RATES, NVML, 0)
35 HC_LOAD_FUNC(nvml, nvmlDeviceGetClockInfo, NVML_DEVICE_GET_CLOCKINFO, NVML, 0)
36 HC_LOAD_FUNC(nvml, nvmlDeviceGetTemperatureThreshold, NVML_DEVICE_GET_THRESHOLD, NVML, 0)
37 HC_LOAD_FUNC(nvml, nvmlDeviceGetCurrPcieLinkGeneration, NVML_DEVICE_GET_CURRPCIELINKGENERATION, NVML, 0)
38 HC_LOAD_FUNC(nvml, nvmlDeviceGetCurrPcieLinkWidth, NVML_DEVICE_GET_CURRPCIELINKWIDTH, NVML, 0)
39
40 return 0;
41 }
42
43 void nvml_close (NVML_PTR *nvml)
44 {
45 if (nvml)
46 {
47 if (nvml->lib)
48 hc_dlclose (nvml->lib);
49
50 myfree (nvml);
51 }
52 }
53
54 const char *hm_NVML_nvmlErrorString (NVML_PTR *nvml, nvmlReturn_t nvml_rc)
55 {
56 if (!nvml) return NULL;
57
58 return nvml->nvmlErrorString (nvml_rc);
59 }
60
61 nvmlReturn_t hm_NVML_nvmlInit (NVML_PTR *nvml)
62 {
63 if (!nvml) return -1;
64
65 nvmlReturn_t nvml_rc = nvml->nvmlInit ();
66
67 if (nvml_rc != NVML_SUCCESS)
68 {
69 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
70
71 log_info ("WARN: %s %d %s\n", "nvmlInit()", nvml_rc, string);
72 }
73
74 return nvml_rc;
75 }
76
77 nvmlReturn_t hm_NVML_nvmlShutdown (NVML_PTR *nvml)
78 {
79 if (!nvml) return -1;
80
81 nvmlReturn_t nvml_rc = nvml->nvmlShutdown ();
82
83 if (nvml_rc != NVML_SUCCESS)
84 {
85 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
86
87 log_info ("WARN: %s %d %s\n", "nvmlShutdown()", nvml_rc, string);
88 }
89
90 return nvml_rc;
91 }
92
93 nvmlReturn_t hm_NVML_nvmlDeviceGetName (NVML_PTR *nvml, nvmlDevice_t device, char *name, unsigned int length)
94 {
95 if (!nvml) return -1;
96
97 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetName (device, name, length);
98
99 if (nvml_rc != NVML_SUCCESS)
100 {
101 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
102
103 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetName()", nvml_rc, string);
104 }
105
106 return nvml_rc;
107 }
108
109 nvmlReturn_t hm_NVML_nvmlDeviceGetHandleByIndex (NVML_PTR *nvml, int skip_warnings, unsigned int index, nvmlDevice_t *device)
110 {
111 if (!nvml) return -1;
112
113 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetHandleByIndex (index, device);
114
115 if (nvml_rc != NVML_SUCCESS)
116 {
117 if (skip_warnings == 0)
118 {
119 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
120
121 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetHandleByIndex()", nvml_rc, string);
122 }
123 }
124
125 return nvml_rc;
126 }
127
128 nvmlReturn_t hm_NVML_nvmlDeviceGetTemperature (NVML_PTR *nvml, nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp)
129 {
130 if (!nvml) return -1;
131
132 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperature (device, sensorType, temp);
133
134 if (nvml_rc != NVML_SUCCESS)
135 {
136 *temp = -1;
137
138 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
139
140 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetTemperature()", nvml_rc, string);
141 }
142
143 return nvml_rc;
144 }
145
146 nvmlReturn_t hm_NVML_nvmlDeviceGetFanSpeed (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *speed)
147 {
148 if (!nvml) return -1;
149
150 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetFanSpeed (device, speed);
151
152 if (nvml_rc != NVML_SUCCESS)
153 {
154 *speed = -1;
155
156 if (skip_warnings == 0)
157 {
158 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
159
160 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetFanSpeed()", nvml_rc, string);
161 }
162 }
163
164 return nvml_rc;
165 }
166
167 /* only tesla following */
168
169 nvmlReturn_t hm_NVML_nvmlDeviceGetPowerUsage (NVML_PTR *nvml, nvmlDevice_t device, unsigned int *power)
170 {
171 if (!nvml) return -1;
172
173 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetPowerUsage (device, power);
174
175 if (nvml_rc != NVML_SUCCESS)
176 {
177 *power = -1;
178
179 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
180
181 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerUsage()", nvml_rc, string);
182 }
183
184 return nvml_rc;
185 }
186
187 nvmlReturn_t hm_NVML_nvmlDeviceGetUtilizationRates (NVML_PTR *nvml, nvmlDevice_t device, nvmlUtilization_t *utilization)
188 {
189 if (!nvml) return -1;
190
191 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetUtilizationRates (device, utilization);
192
193 if (nvml_rc != NVML_SUCCESS)
194 {
195 utilization->gpu = -1;
196 utilization->memory = -1;
197
198 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
199
200 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
201 }
202
203 return nvml_rc;
204 }
205
206 nvmlReturn_t hm_NVML_nvmlDeviceGetClockInfo (NVML_PTR *nvml, nvmlDevice_t device, nvmlClockType_t type, unsigned int *clock)
207 {
208 if (!nvml) return -1;
209
210 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetClockInfo (device, type, clock);
211
212 if (nvml_rc != NVML_SUCCESS)
213 {
214 *clock = -1;
215
216 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
217
218 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
219 }
220
221 return nvml_rc;
222 }
223
224 nvmlReturn_t hm_NVML_nvmlDeviceGetTemperatureThreshold (NVML_PTR *nvml, nvmlDevice_t device, nvmlTemperatureThresholds_t thresholdType, unsigned int *temp)
225 {
226 if (!nvml) return -1;
227
228 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperatureThreshold (device, thresholdType, temp);
229
230 if (nvml_rc != NVML_SUCCESS)
231 {
232 *temp = -1;
233
234 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
235
236 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
237 }
238
239 return nvml_rc;
240 }
241
242 nvmlReturn_t hm_NVML_nvmlDeviceGetCurrPcieLinkGeneration (NVML_PTR *nvml, nvmlDevice_t device, unsigned int *currLinkGen)
243 {
244 if (!nvml) return -1;
245
246 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrPcieLinkGeneration (device, currLinkGen);
247
248 if (nvml_rc != NVML_SUCCESS)
249 {
250 *currLinkGen = -1;
251
252 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
253
254 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
255 }
256
257 return nvml_rc;
258 }
259
260 nvmlReturn_t hm_NVML_nvmlDeviceGetCurrPcieLinkWidth (NVML_PTR *nvml, nvmlDevice_t device, unsigned int *currLinkWidth)
261 {
262 if (!nvml) return -1;
263
264 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetCurrPcieLinkWidth (device, currLinkWidth);
265
266 if (nvml_rc != NVML_SUCCESS)
267 {
268 *currLinkWidth = -1;
269
270 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
271
272 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
273 }
274
275 return nvml_rc;
276 }