-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathnvml.c
221 lines (182 loc) · 6.89 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
/*
* Copyright 2014 Andre Vehreschild <vehre@gmx.de>
* Copyright 2016-2017 John Doering <ghostlander@phoenixcoin.org>
*
* 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>
#define dlsym (void *) GetProcAddress
#define dlclose FreeLibrary
HMODULE hDLL;
#endif
extern bool opt_nonvml;
static char * (*NVML_nvmlErrorString)();
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_nvmlShutdown)();
void nvml_init() {
nvmlReturn_t ret;
#ifdef __linux__
hDLL = dlopen("libnvidia-ml.so", RTLD_LAZY | RTLD_GLOBAL);
#else
/* Not in system path, but could be local */
hDLL = LoadLibrary("nvml.dll");
if(!hDLL) {
/* %ProgramW6432% is unsupported by OS prior to year 2009 */
char path[512];
ExpandEnvironmentStringsA("%ProgramFiles%\\NVIDIA Corporation\\NVSMI\\nvml.dll", path, sizeof(path));
hDLL = LoadLibrary(path);
}
#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 * (*)()) \
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_nvmlShutdown = (nvmlReturn_t (*)()) \
dlsym(hDLL, "nvmlShutdown");
ret = NVML_nvmlInit();
if(ret != NVML_SUCCESS) {
applog(LOG_ERR, "NVML: initialisation failed with code %s",
NVML_nvmlErrorString(ret));
opt_nonvml = true;
return;
}
}
void nvml_gpu_temp_and_fanspeed(const uint dev, float *temp, int *fanspeed) {
nvmlReturn_t ret;
nvmlDevice_t gpu;
uint nTemp, nSpeed;
if(gpus[dev].mapped)
ret = NVML_nvmlDeviceGetHandleByIndex(gpus[dev].virtual_adl, &gpu);
else
ret = NVML_nvmlDeviceGetHandleByIndex(dev, &gpu);
if(ret != NVML_SUCCESS) {
applog(LOG_ERR, "NVML: GPU %d handle failed with code %s",
dev, NVML_nvmlErrorString(ret));
*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_print_devices() {
nvmlReturn_t ret;
uint devnum, dev;
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, "NVML found %u device%s%s", \
devnum, devnum != 1 ? "s" : "", devnum ? ":" : "");
if(!devnum) return;
for(dev = 0; dev < nDevs; dev++) {
if(gpus[dev].mapped && (gpus[dev].virtual_adl >= devnum))
applog(LOG_ERR, "Mapped NVML device %u doesn't exist",
gpus[dev].virtual_adl);
applog(LOG_INFO, "Mapping OpenCL device %u to NVML device %u", dev,
gpus[dev].mapped ? gpus[dev].virtual_adl : dev);
}
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;
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 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 */