Merge pull request #25 from philsmd/GetRidOfCUDA
[hashcat.git] / src / ext_nvml.c
1 /**
2 * Author......: Jens Steube <jens.steube@gmail.com>
3 * License.....: MIT
4 */
5
6 #include <ext_nvml.h>
7
8 //#ifdef _POSIX // implied
9
10 void *GetLibFunction (void *pLibrary, const char *name)
11 {
12 return dlsym (pLibrary, name);
13 }
14
15 const char * hc_NVML_nvmlErrorString (HM_LIB hDLL, nvmlReturn_t nvml_rc)
16 {
17 NVML_ERROR_STRING nvmlErrorString = (NVML_ERROR_STRING) GetLibFunction (hDLL, "nvmlErrorString");
18
19 if (nvmlErrorString == NULL)
20 {
21 log_error ("ERROR: %s\n", "nvmlErrorString() is missing");
22
23 exit (-1);
24 }
25
26 return nvmlErrorString (nvml_rc);
27 }
28
29 nvmlReturn_t hc_NVML_nvmlInit (HM_LIB hDLL)
30 {
31 NVML_INIT nvmlInit = (NVML_INIT) GetLibFunction (hDLL, "nvmlInit");
32
33 if (nvmlInit == NULL)
34 {
35 log_error ("ERROR: %s\n", "nvmlInit() is missing");
36
37 exit (-1);
38 }
39
40 nvmlReturn_t nvml_rc = nvmlInit ();
41
42 if (nvml_rc != NVML_SUCCESS)
43 {
44 const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc);
45
46 log_info ("WARN: %s %d %s\n", "nvmlInit()", nvml_rc, string);
47 }
48
49 return nvml_rc;
50 }
51
52 nvmlReturn_t hc_NVML_nvmlShutdown (HM_LIB hDLL)
53 {
54 NVML_SHUTDOWN nvmlShutdown = (NVML_SHUTDOWN) GetLibFunction (hDLL, "nvmlShutdown");
55
56 if (nvmlShutdown == NULL)
57 {
58 log_error ("ERROR: %s\n", "nvmlShutdown() is missing");
59
60 exit (-1);
61 }
62
63 nvmlReturn_t nvml_rc = nvmlShutdown ();
64
65 if (nvml_rc != NVML_SUCCESS)
66 {
67 const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc);
68
69 log_info ("WARN: %s %d %s\n", "nvmlShutdown()", nvml_rc, string);
70 }
71
72 return nvml_rc;
73 }
74
75 nvmlReturn_t hc_NVML_nvmlDeviceGetName (HM_LIB hDLL, nvmlDevice_t device, char *name, unsigned int length)
76 {
77 NVML_DEVICE_GET_NAME nvmlDeviceGetName = (NVML_DEVICE_GET_NAME) GetLibFunction (hDLL, "nvmlDeviceGetName");
78
79 if (nvmlDeviceGetName == NULL)
80 {
81 log_error ("ERROR: %s\n", "nvmlDeviceGetName() is missing");
82
83 exit (-1);
84 }
85
86 nvmlReturn_t nvml_rc = nvmlDeviceGetName (device, name, length);
87
88 if (nvml_rc != NVML_SUCCESS)
89 {
90 const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc);
91
92 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetName()", nvml_rc, string);
93 }
94
95 return nvml_rc;
96 }
97
98 nvmlReturn_t hc_NVML_nvmlDeviceGetHandleByIndex (HM_LIB hDLL, int skip_warnings, unsigned int index, nvmlDevice_t *device)
99 {
100 NVML_DEVICE_GET_HANDLE_BY_INDEX nvmlDeviceGetHandleByIndex = (NVML_DEVICE_GET_HANDLE_BY_INDEX) GetLibFunction (hDLL, "nvmlDeviceGetHandleByIndex");
101
102 if (nvmlDeviceGetHandleByIndex == NULL)
103 {
104 log_error ("ERROR: %s\n", "nvmlDeviceGetHandleByIndex() is missing");
105
106 exit (-1);
107 }
108
109 nvmlReturn_t nvml_rc = nvmlDeviceGetHandleByIndex (index, device);
110
111 if (nvml_rc != NVML_SUCCESS)
112 {
113 if (skip_warnings == 0)
114 {
115 const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc);
116
117 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetHandleByIndex()", nvml_rc, string);
118 }
119 }
120
121 return nvml_rc;
122 }
123
124 nvmlReturn_t hc_NVML_nvmlDeviceGetTemperature (HM_LIB hDLL, nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp)
125 {
126 NVML_DEVICE_GET_TEMPERATURE nvmlDeviceGetTemperature = (NVML_DEVICE_GET_TEMPERATURE) GetLibFunction (hDLL, "nvmlDeviceGetTemperature");
127
128 if (nvmlDeviceGetTemperature == NULL)
129 {
130 log_error ("ERROR: %s\n", "nvmlDeviceGetTemperature() is missing");
131
132 exit (-1);
133 }
134
135 nvmlReturn_t nvml_rc = nvmlDeviceGetTemperature (device, sensorType, temp);
136
137 if (nvml_rc != NVML_SUCCESS)
138 {
139 *temp = -1;
140
141 //const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc);
142
143 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetTemperature()", nvml_rc, string);
144 }
145
146 return nvml_rc;
147 }
148
149 nvmlReturn_t hc_NVML_nvmlDeviceGetFanSpeed (HM_LIB hDLL, int skip_warnings, nvmlDevice_t device, unsigned int *speed)
150 {
151 NVML_DEVICE_GET_FAN_SPEED nvmlDeviceGetFanSpeed = (NVML_DEVICE_GET_FAN_SPEED) GetLibFunction (hDLL, "nvmlDeviceGetFanSpeed");
152
153 if (nvmlDeviceGetFanSpeed == NULL)
154 {
155 log_error ("ERROR: %s\n", "nvmlDeviceGetFanSpeed() is missing");
156
157 exit (-1);
158 }
159
160 nvmlReturn_t nvml_rc = nvmlDeviceGetFanSpeed (device, speed);
161
162 if (nvml_rc != NVML_SUCCESS)
163 {
164 *speed = -1;
165
166 if (skip_warnings == 0)
167 {
168 const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc);
169
170 log_info ("WARN: %s %d %s\n", "nvmlDeviceGetFanSpeed()", nvml_rc, string);
171 }
172 }
173
174 return nvml_rc;
175 }
176
177 /* only tesla following */
178
179 nvmlReturn_t hc_NVML_nvmlDeviceGetPowerUsage (HM_LIB hDLL, nvmlDevice_t device, unsigned int *power)
180 {
181 NVML_DEVICE_GET_POWER_USAGE nvmlDeviceGetPowerUsage = (NVML_DEVICE_GET_POWER_USAGE) GetLibFunction (hDLL, "nvmlDeviceGetPowerUsage");
182
183 if (nvmlDeviceGetPowerUsage == NULL)
184 {
185 log_error ("ERROR: %s\n", "nvmlDeviceGetPowerUsage() is missing");
186
187 exit (-1);
188 }
189
190 nvmlReturn_t nvml_rc = nvmlDeviceGetPowerUsage (device, power);
191
192 if (nvml_rc != NVML_SUCCESS)
193 {
194 *power = -1;
195
196 //const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc);
197
198 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetPowerUsage()", nvml_rc, string);
199 }
200
201 return nvml_rc;
202 }
203
204 nvmlReturn_t hc_NVML_nvmlDeviceGetUtilizationRates (HM_LIB hDLL, nvmlDevice_t device, nvmlUtilization_t *utilization)
205 {
206 NVML_DEVICE_GET_UTILIZATION_RATES nvmlDeviceGetUtilizationRates = (NVML_DEVICE_GET_UTILIZATION_RATES) GetLibFunction (hDLL, "nvmlDeviceGetUtilizationRates");
207
208 if (nvmlDeviceGetUtilizationRates == NULL)
209 {
210 log_error ("ERROR: %s\n", "nvmlDeviceGetUtilizationRates() is missing");
211
212 exit (-1);
213 }
214
215 nvmlReturn_t nvml_rc = nvmlDeviceGetUtilizationRates (device, utilization);
216
217 if (nvml_rc != NVML_SUCCESS)
218 {
219 utilization->gpu = -1;
220 utilization->memory = -1;
221
222 //const char *string = hc_NVML_nvmlErrorString (hDLL, nvml_rc);
223
224 //log_info ("WARN: %s %d %s\n", "nvmlDeviceGetUtilizationRates()", nvml_rc, string);
225 }
226
227 return nvml_rc;
228 }
229
230 //#endif