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