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