forked from nicehash/sgminer
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathnvml.c
342 lines (293 loc) · 10.7 KB
/
nvml.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright 2014 Andre Vehreschild
* Copyright 2016 John Doering
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See LICENCE for more details.
*/
#include "config.h"
#ifdef HAVE_NVML
/* NVML is available for Linux and Windows only */
#if defined(__linux__) || defined(_WIN32)
#include "miner.h"
#ifdef __linux__
#include <stdlib.h>
#include <unistd.h>
#include <dlfcn.h>
void *hDLL;
#else
#include <windows.h>
typedef unsigned int uint;
#define dlsym (void *) GetProcAddress
#define dlclose FreeLibrary
static HMODULE hDLL;
#endif
static char * (*NVML_nvmlErrorString)(nvmlReturn_t);
static nvmlReturn_t (*NVML_nvmlInit)();
static nvmlReturn_t (*NVML_nvmlDeviceGetCount)(uint *);
static nvmlReturn_t (*NVML_nvmlDeviceGetHandleByIndex)(uint, nvmlDevice_t *);
static nvmlReturn_t (*NVML_nvmlDeviceGetName)(nvmlDevice_t, char *, uint);
static nvmlReturn_t (*NVML_nvmlDeviceGetPciInfo)(nvmlDevice_t, nvmlPciInfo_t *);
static nvmlReturn_t (*NVML_nvmlDeviceGetTemperature)(nvmlDevice_t, nvmlTemperatureSensors_t, uint *);
static nvmlReturn_t (*NVML_nvmlDeviceGetFanSpeed)(nvmlDevice_t, uint *);
static nvmlReturn_t (*NVML_nvmlDeviceGetClockInfo)(nvmlDevice_t, nvmlClockType_t, unsigned int *);
static nvmlReturn_t (*NVML_nvmlDeviceGetDefaultApplicationsClock)(nvmlDevice_t, nvmlClockType_t, unsigned int *);
static nvmlReturn_t (*NVML_nvmlDeviceGetPowerManagementLimit)(nvmlDevice_t, uint *);
static nvmlReturn_t (*NVML_nvmlDeviceGetPowerUsage)(nvmlDevice_t, uint *);
static nvmlReturn_t (*NVML_nvmlShutdown)();
void nvml_init() {
nvmlReturn_t ret;
#ifdef WIN32
/* Not in system path, but could be local */
hDLL = LoadLibrary("nvml.dll");
if(!hDLL) {
char path[512];
ExpandEnvironmentStringsA("%ProgramFiles%\\NVIDIA Corporation\\NVSMI\\nvml.dll", path, sizeof(path));
hDLL = LoadLibrary(path);
}
#else
hDLL = dlopen("libnvidia-ml.so", RTLD_LAZY | RTLD_GLOBAL);
#endif
if(!hDLL) {
applog(LOG_INFO, "Unable to load the NVIDIA Management Library");
opt_nonvml = true;
return;
}
NVML_nvmlInit = (nvmlReturn_t (*)()) dlsym(hDLL, "nvmlInit_v2");
if(!NVML_nvmlInit) {
/* Try an older interface */
NVML_nvmlInit = (nvmlReturn_t (*)()) dlsym(hDLL, "nvmlInit");
if(!NVML_nvmlInit) {
applog(LOG_ERR, "NVML: Unable to initialise");
opt_nonvml = true;
return;
} else {
NVML_nvmlDeviceGetCount = (nvmlReturn_t (*)(uint *)) \
dlsym(hDLL, "nvmlDeviceGetCount");
NVML_nvmlDeviceGetHandleByIndex = (nvmlReturn_t (*)(uint, nvmlDevice_t *)) \
dlsym(hDLL, "nvmlDeviceGetHandleByIndex");
NVML_nvmlDeviceGetPciInfo = (nvmlReturn_t (*)(nvmlDevice_t, nvmlPciInfo_t *)) \
dlsym(hDLL, "nvmlDeviceGetPciInfo");
}
} else {
NVML_nvmlDeviceGetCount = (nvmlReturn_t (*)(uint *)) \
dlsym(hDLL, "nvmlDeviceGetCount_v2");
NVML_nvmlDeviceGetHandleByIndex = (nvmlReturn_t (*)(uint, nvmlDevice_t *)) \
dlsym(hDLL, "nvmlDeviceGetHandleByIndex_v2");
NVML_nvmlDeviceGetPciInfo = (nvmlReturn_t (*)(nvmlDevice_t, nvmlPciInfo_t *)) \
dlsym(hDLL, "nvmlDeviceGetPciInfo_v2");
}
NVML_nvmlErrorString = (char * (*)(nvmlReturn_t)) \
dlsym(hDLL, "nvmlErrorString");
NVML_nvmlDeviceGetName = (nvmlReturn_t (*)(nvmlDevice_t, char *, uint)) \
dlsym(hDLL, "nvmlDeviceGetName");
NVML_nvmlDeviceGetTemperature = (nvmlReturn_t (*)(nvmlDevice_t, nvmlTemperatureSensors_t, uint *)) \
dlsym(hDLL, "nvmlDeviceGetTemperature");
NVML_nvmlDeviceGetFanSpeed = (nvmlReturn_t (*)(nvmlDevice_t, uint *)) \
dlsym(hDLL, "nvmlDeviceGetFanSpeed");
NVML_nvmlDeviceGetClockInfo = (nvmlReturn_t (*)(nvmlDevice_t, nvmlClockType_t, uint *)) \
dlsym(hDLL, "nvmlDeviceGetClockInfo");
NVML_nvmlDeviceGetDefaultApplicationsClock = (nvmlReturn_t (*)(nvmlDevice_t, nvmlClockType_t, uint *)) \
dlsym(hDLL, "nvmlDeviceGetDefaultApplicationsClock");
NVML_nvmlDeviceGetPowerManagementLimit = (nvmlReturn_t (*)(nvmlDevice_t, uint *)) \
dlsym(hDLL, "nvmlDeviceGetPowerManagementLimit");
NVML_nvmlDeviceGetPowerUsage = (nvmlReturn_t (*)(nvmlDevice_t, uint *)) \
dlsym(hDLL, "nvmlDeviceGetPowerUsage");
NVML_nvmlShutdown = (nvmlReturn_t (*)()) \
dlsym(hDLL, "nvmlShutdown");
ret = NVML_nvmlInit();
if(ret != NVML_SUCCESS) {
applog(LOG_ERR, "NVML: Init failed %s", NVML_nvmlErrorString(ret));
}
}
// todo: cache mapping in an array (or cgpu)
unsigned int nvml_gpu_id(const unsigned int busid)
{
uint dev, devnum = 0;
bool matched = false;
nvmlReturn_t ret = NVML_nvmlDeviceGetCount(&devnum);
if (ret != NVML_SUCCESS) {
applog(LOG_ERR, "NVML: unable to query devices %s", NVML_nvmlErrorString(ret));
return UINT_MAX;
}
for (dev=0; dev < devnum; dev++) {
nvmlPciInfo_t pci;
nvmlDevice_t gpu = NULL;
ret = NVML_nvmlDeviceGetHandleByIndex(dev, &gpu);
if (ret != NVML_SUCCESS)
continue;
ret = NVML_nvmlDeviceGetPciInfo(gpu, &pci);
if (ret != NVML_SUCCESS)
continue;
if (pci.bus == busid) {
matched = true;
break;
}
}
if (!matched) {
return UINT_MAX;
}
return dev;
}
void nvml_gpu_temp_and_fanspeed(const unsigned int busid, float *temp, int *fanspeed)
{
nvmlReturn_t ret;
nvmlDevice_t gpu = NULL;
uint nTemp, nSpeed;
uint dev = nvml_gpu_id(busid);
if (dev == UINT_MAX) {
*temp = -1.0f;
*fanspeed = -1;
return;
}
ret = NVML_nvmlDeviceGetHandleByIndex(dev, &gpu);
if (ret != NVML_SUCCESS || !gpu) {
*temp = -1.0f;
*fanspeed = -1;
return;
}
ret = NVML_nvmlDeviceGetTemperature(gpu, NVML_TEMPERATURE_GPU, &nTemp);
*temp = (ret != NVML_SUCCESS) ? -1.0f : (float)nTemp;
ret = NVML_nvmlDeviceGetFanSpeed(gpu, &nSpeed);
*fanspeed = (ret != NVML_SUCCESS) ? -1 : (int)nSpeed;
}
void nvml_gpu_clocks(const unsigned int busid, unsigned int *gpuClock, unsigned int *memClock)
{
nvmlReturn_t ret;
nvmlDevice_t gpu = NULL;
unsigned int clock = 0;
*gpuClock = 0; *memClock = 0;
uint dev = nvml_gpu_id(busid);
if (dev == UINT_MAX) {
return;
}
ret = NVML_nvmlDeviceGetHandleByIndex(dev, &gpu);
if (ret != NVML_SUCCESS || !gpu) {
return;
}
ret = NVML_nvmlDeviceGetClockInfo(gpu, NVML_CLOCK_SM, &clock);
*gpuClock = (ret != NVML_SUCCESS) ? 0 : clock;
ret = NVML_nvmlDeviceGetClockInfo(gpu, NVML_CLOCK_MEM, &clock);
*memClock = (ret != NVML_SUCCESS) ? 0 : clock;
}
void nvml_gpu_defclocks(const unsigned int busid, unsigned int *gpuClock, unsigned int *memClock)
{
nvmlReturn_t ret;
nvmlDevice_t gpu = NULL;
unsigned int clock = 0;
*gpuClock = 0; *memClock = 0;
uint dev = nvml_gpu_id(busid);
if (dev == UINT_MAX || !NVML_nvmlDeviceGetDefaultApplicationsClock) {
return;
}
ret = NVML_nvmlDeviceGetHandleByIndex(dev, &gpu);
if (ret != NVML_SUCCESS || !gpu) {
return;
}
ret = NVML_nvmlDeviceGetDefaultApplicationsClock(gpu, NVML_CLOCK_GRAPHICS, &clock);
*gpuClock = (ret != NVML_SUCCESS) ? 0 : clock;
ret = NVML_nvmlDeviceGetDefaultApplicationsClock(gpu, NVML_CLOCK_MEM, &clock);
*memClock = (ret != NVML_SUCCESS) ? 0 : clock;
}
void nvml_gpu_usage(const unsigned int busid, unsigned int *watts, unsigned int *limit)
{
nvmlReturn_t ret;
nvmlDevice_t gpu = NULL;
unsigned int mwatts = 0, plimit = 0;
*watts = 0; *limit = 0;
uint dev = nvml_gpu_id(busid);
if (dev == UINT_MAX) {
return;
}
ret = NVML_nvmlDeviceGetHandleByIndex(dev, &gpu);
if (ret != NVML_SUCCESS || !gpu) {
return;
}
ret = NVML_nvmlDeviceGetPowerUsage(gpu, &mwatts);
*watts = (ret != NVML_SUCCESS) ? 0 : mwatts/1000;
ret = NVML_nvmlDeviceGetPowerManagementLimit(gpu, &plimit);
*limit = (ret != NVML_SUCCESS) ? 0 : plimit/1000;
}
void nvml_gpu_ids(const unsigned int busid, int *vid, int *pid, int *svid, int *spid)
{
nvmlReturn_t ret;
nvmlDevice_t gpu = NULL;
nvmlPciInfo_t pci = { 0 };
*vid = *pid = 0;
if (svid ) *svid = *spid = 0;
uint dev = nvml_gpu_id(busid);
if (dev == UINT_MAX) {
return;
}
ret = NVML_nvmlDeviceGetHandleByIndex(dev, &gpu);
if (ret != NVML_SUCCESS || !gpu) return;
ret = NVML_nvmlDeviceGetPciInfo(gpu, &pci);
if (ret != NVML_SUCCESS) return;
if (pci.bus != busid) return;
*vid = pci.pciDeviceId & 0xFFFF;
*pid = pci.pciDeviceId >> 16;
if (svid) {
*svid = pci.pciSubSystemId & 0xFFFF;
*spid = pci.pciSubSystemId >> 16;
}
}
void nvml_print_devices()
{
uint dev, devnum = 0;
nvmlReturn_t ret = NVML_nvmlDeviceGetCount(&devnum);
if(ret != NVML_SUCCESS) {
applog(LOG_ERR, "NVML: Device number query failed with code %s",
NVML_nvmlErrorString(ret));
return;
}
applog(LOG_INFO, "Number of NVML devices: %d", devnum);
if(!devnum) return;
for(dev = 0; dev < devnum; dev++) {
char name[NVML_DEVICE_NAME_BUFFER_SIZE];
nvmlDevice_t gpu;
nvmlPciInfo_t pci;
ret = NVML_nvmlDeviceGetHandleByIndex(dev, &gpu);
if(ret != NVML_SUCCESS) {
applog(LOG_ERR, "NVML: GPU %u handle failed with code %s",
dev, NVML_nvmlErrorString(ret));
return;
}
ret = NVML_nvmlDeviceGetName(gpu, name, NVML_DEVICE_NAME_BUFFER_SIZE);
if(ret != NVML_SUCCESS) {
applog(LOG_ERR, "NVML: GPU %u name query failed with code %s",
dev, NVML_nvmlErrorString(ret));
return;
}
ret = NVML_nvmlDeviceGetPciInfo(gpu, &pci);
if(ret != NVML_SUCCESS) {
applog(LOG_ERR, "NVML: GPU %u PCI ID query failed with code %s",
dev, NVML_nvmlErrorString(ret));
return;
}
applog(LOG_INFO, "GPU %u: %s [%s]", dev, name, pci.busId);
}
}
void nvml_shutdown()
{
nvmlReturn_t ret = NVML_nvmlShutdown();
if(ret != NVML_SUCCESS) {
applog(LOG_ERR, "NVML: Unable to shut down");
return;
}
if(hDLL) dlclose(hDLL);
}
#else /* !(defined(__linux__) || defined(_WIN32)) */
/* Unsupported platform */
void nvml_init() {
opt_nonvml = true;
}
void nvml_gpu_temp_and_fanspeed(const unsigned int __unused, float *temp, int *fanspeed) {
*temp = -1.0f;
*fanspeed = -1;
}
void nvml_print_devices() {}
void nvml_shutdown() {}
#endif /* defined(__linux__) || defined(_WIN32) */
#endif /* HAVE_NVML */