31c078ab0bf646c054a9c43c1948542224ee9f34
[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
38 return 0;
39 }
40
41 void nvml_close (NVML_PTR *nvml)
42 {
43 if (nvml)
44 {
45 if (nvml->lib)
46 hc_dlclose (nvml->lib);
47
48 myfree (nvml);
49 }
50 }
51
52 const char *hm_NVML_nvmlErrorString (NVML_PTR *nvml, nvmlReturn_t nvml_rc)
53 {
54 if (!nvml) return NULL;
55
56 return nvml->nvmlErrorString (nvml_rc);
57 }
58
59 nvmlReturn_t hm_NVML_nvmlInit (NVML_PTR *nvml)
60 {
61 if (!nvml) return -1;
62
63 nvmlReturn_t nvml_rc = nvml->nvmlInit ();
64
65 if (nvml_rc != NVML_SUCCESS)
66 {
67 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
68
69 log_info ("WARN: %s %d %s\n", "nvmlInit()", nvml_rc, string);
70 }
71
72 return nvml_rc;
73 }
74
75 nvmlReturn_t hm_NVML_nvmlShutdown (NVML_PTR *nvml)
76 {
77 if (!nvml) return -1;
78
79 nvmlReturn_t nvml_rc = nvml->nvmlShutdown ();
80
81 if (nvml_rc != NVML_SUCCESS)
82 {
83 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
84
85 log_info ("WARN: %s %d %s\n", "nvmlShutdown()", nvml_rc, string);
86 }
87
88 return nvml_rc;
89 }
90
91 nvmlReturn_t hm_NVML_nvmlDeviceGetName (NVML_PTR *nvml, nvmlDevice_t device, char *name, unsigned int length)
92 {
93 if (!nvml) return -1;
94
95 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetName (device, name, length);
96
97 if (nvml_rc != NVML_SUCCESS)
98 {
99 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
100
101 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetName()", nvml_rc, string);
102 }
103
104 return nvml_rc;
105 }
106
107 nvmlReturn_t hm_NVML_nvmlDeviceGetHandleByIndex (NVML_PTR *nvml, int skip_warnings, unsigned int index, nvmlDevice_t *device)
108 {
109 if (!nvml) return -1;
110
111 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetHandleByIndex (index, device);
112
113 if (nvml_rc != NVML_SUCCESS)
114 {
115 if (skip_warnings == 0)
116 {
117 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
118
119 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetHandleByIndex()", nvml_rc, string);
120 }
121 }
122
123 return nvml_rc;
124 }
125
126 nvmlReturn_t hm_NVML_nvmlDeviceGetTemperature (NVML_PTR *nvml, nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp)
127 {
128 if (!nvml) return -1;
129
130 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperature (device, sensorType, temp);
131
132 if (nvml_rc != NVML_SUCCESS)
133 {
134 *temp = -1;
135
136 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
137
138 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetTemperature()", nvml_rc, string);
139 }
140
141 return nvml_rc;
142 }
143
144 nvmlReturn_t hm_NVML_nvmlDeviceGetFanSpeed (NVML_PTR *nvml, int skip_warnings, nvmlDevice_t device, unsigned int *speed)
145 {
146 if (!nvml) return -1;
147
148 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetFanSpeed (device, speed);
149
150 if (nvml_rc != NVML_SUCCESS)
151 {
152 *speed = -1;
153
154 if (skip_warnings == 0)
155 {
156 const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
157
158 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetFanSpeed()", nvml_rc, string);
159 }
160 }
161
162 return nvml_rc;
163 }
164
165 /* only tesla following */
166
167 nvmlReturn_t hm_NVML_nvmlDeviceGetPowerUsage (NVML_PTR *nvml, nvmlDevice_t device, unsigned int *power)
168 {
169 if (!nvml) return -1;
170
171 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetPowerUsage (device, power);
172
173 if (nvml_rc != NVML_SUCCESS)
174 {
175 *power = -1;
176
177 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
178
179 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerUsage()", nvml_rc, string);
180 }
181
182 return nvml_rc;
183 }
184
185 nvmlReturn_t hm_NVML_nvmlDeviceGetUtilizationRates (NVML_PTR *nvml, nvmlDevice_t device, nvmlUtilization_t *utilization)
186 {
187 if (!nvml) return -1;
188
189 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetUtilizationRates (device, utilization);
190
191 if (nvml_rc != NVML_SUCCESS)
192 {
193 utilization->gpu = -1;
194 utilization->memory = -1;
195
196 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
197
198 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
199 }
200
201 return nvml_rc;
202 }
203
204 nvmlReturn_t hm_NVML_nvmlDeviceGetClockInfo (NVML_PTR *nvml, nvmlDevice_t device, nvmlClockType_t type, unsigned int *clock)
205 {
206 if (!nvml) return -1;
207
208 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetClockInfo (device, type, clock);
209
210 if (nvml_rc != NVML_SUCCESS)
211 {
212 *clock = -1;
213
214 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
215
216 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
217 }
218
219 return nvml_rc;
220 }
221
222 nvmlReturn_t hm_NVML_nvmlDeviceGetTemperatureThreshold (NVML_PTR *nvml, nvmlDevice_t device, nvmlTemperatureThresholds_t thresholdType, unsigned int *temp)
223 {
224 if (!nvml) return -1;
225
226 nvmlReturn_t nvml_rc = nvml->nvmlDeviceGetTemperatureThreshold (device, thresholdType, temp);
227
228 if (nvml_rc != NVML_SUCCESS)
229 {
230 *temp = -1;
231
232 //const char *string = hm_NVML_nvmlErrorString (nvml, nvml_rc);
233
234 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
235 }
236
237 return nvml_rc;
238 }