Implemented NvmlLib.

This commit is contained in:
XMRig 2019-10-30 20:26:21 +07:00
parent 175a7b06b7
commit 83f437f979
8 changed files with 203 additions and 12 deletions

View file

@ -39,6 +39,14 @@ namespace xmrig {
static uv_lib_t nvmlLib;
static const char *kNvmlDeviceGetClockInfo = "nvmlDeviceGetClockInfo";
static const char *kNvmlDeviceGetCount = "nvmlDeviceGetCount_v2";
static const char *kNvmlDeviceGetFanSpeed = "nvmlDeviceGetFanSpeed";
static const char *kNvmlDeviceGetFanSpeed_v2 = "nvmlDeviceGetFanSpeed_v2";
static const char *kNvmlDeviceGetHandleByIndex = "nvmlDeviceGetHandleByIndex_v2";
static const char *kNvmlDeviceGetPciInfo = "nvmlDeviceGetPciInfo_v2";
static const char *kNvmlDeviceGetPowerUsage = "nvmlDeviceGetPowerUsage";
static const char *kNvmlDeviceGetTemperature = "nvmlDeviceGetTemperature";
static const char *kNvmlInit = "nvmlInit_v2";
static const char *kNvmlShutdown = "nvmlShutdown";
static const char *kNvmlSystemGetDriverVersion = "nvmlSystemGetDriverVersion";
@ -46,10 +54,18 @@ static const char *kNvmlSystemGetNVMLVersion = "nvmlSyste
static const char *kSymbolNotFound = "symbol not found";
static nvmlReturn_t (*pNvmlInit)() = nullptr;
static nvmlReturn_t (*pNvmlShutdown)() = nullptr;
static nvmlReturn_t (*pNvmlSystemGetDriverVersion)(char *version, unsigned int length) = nullptr;
static nvmlReturn_t (*pNvmlSystemGetNVMLVersion)(char *version, unsigned int length) = nullptr;
static nvmlReturn_t (*pNvmlDeviceGetClockInfo)(nvmlDevice_t device, uint32_t type, uint32_t *clock) = nullptr;
static nvmlReturn_t (*pNvmlDeviceGetCount)(uint32_t *deviceCount) = nullptr;
static nvmlReturn_t (*pNvmlDeviceGetFanSpeed_v2)(nvmlDevice_t device, uint32_t fan, uint32_t *speed) = nullptr;
static nvmlReturn_t (*pNvmlDeviceGetFanSpeed)(nvmlDevice_t device, uint32_t *speed) = nullptr;
static nvmlReturn_t (*pNvmlDeviceGetHandleByIndex)(uint32_t index, nvmlDevice_t *device) = nullptr;
static nvmlReturn_t (*pNvmlDeviceGetPciInfo)(nvmlDevice_t device, nvmlPciInfo_t *pci) = nullptr;
static nvmlReturn_t (*pNvmlDeviceGetPowerUsage)(nvmlDevice_t device, uint32_t *power) = nullptr;
static nvmlReturn_t (*pNvmlDeviceGetTemperature)(nvmlDevice_t device, uint32_t sensorType, uint32_t *temp) = nullptr;
static nvmlReturn_t (*pNvmlInit)() = nullptr;
static nvmlReturn_t (*pNvmlShutdown)() = nullptr;
static nvmlReturn_t (*pNvmlSystemGetDriverVersion)(char *version, uint32_t length) = nullptr;
static nvmlReturn_t (*pNvmlSystemGetNVMLVersion)(char *version, uint32_t length) = nullptr;
#define DLSYM(x) if (uv_dlsym(&nvmlLib, k##x, reinterpret_cast<void**>(&p##x)) == -1) { throw std::runtime_error(kSymbolNotFound); }
@ -93,6 +109,72 @@ void xmrig::NvmlLib::close()
}
bool xmrig::NvmlLib::assign(std::vector<CudaDevice> &devices)
{
uint32_t count = 0;
if (pNvmlDeviceGetCount(&count) != NVML_SUCCESS) {
return false;
}
for (uint32_t i = 0; i < count; i++) {
nvmlDevice_t nvmlDevice;
if (pNvmlDeviceGetHandleByIndex(i, &nvmlDevice) != NVML_SUCCESS) {
continue;
}
nvmlPciInfo_t pci;
if (pNvmlDeviceGetPciInfo(nvmlDevice, &pci) != NVML_SUCCESS) {
continue;
}
for (auto &device : devices) {
if (device.topology().bus() == pci.bus && device.topology().device() == pci.device) {
device.setNvmlDevice(nvmlDevice);
}
}
}
return true;
}
NvmlHealth xmrig::NvmlLib::health(nvmlDevice_t device)
{
if (!device) {
return {};
}
NvmlHealth health;
pNvmlDeviceGetTemperature(device, NVML_TEMPERATURE_GPU, &health.temperature);
pNvmlDeviceGetPowerUsage(device, &health.power);
pNvmlDeviceGetClockInfo(device, NVML_CLOCK_SM, &health.clock);
pNvmlDeviceGetClockInfo(device, NVML_CLOCK_MEM, &health.memClock);
if (health.power) {
health.power /= 1000;
}
uint32_t speed = 0;
if (pNvmlDeviceGetFanSpeed_v2) {
uint32_t i = 0;
while (pNvmlDeviceGetFanSpeed_v2(device, i, &speed) == NVML_SUCCESS) {
health.fanSpeed.push_back(speed);
++i;
}
}
else {
pNvmlDeviceGetFanSpeed(device, &speed);
health.fanSpeed.push_back(speed);
}
return health;
}
bool xmrig::NvmlLib::dlopen()
{
if (!m_loader.isNull()) {
@ -117,6 +199,13 @@ bool xmrig::NvmlLib::dlopen()
bool xmrig::NvmlLib::load()
{
try {
DLSYM(NvmlDeviceGetClockInfo);
DLSYM(NvmlDeviceGetCount);
DLSYM(NvmlDeviceGetFanSpeed);
DLSYM(NvmlDeviceGetHandleByIndex);
DLSYM(NvmlDeviceGetPciInfo);
DLSYM(NvmlDeviceGetPowerUsage);
DLSYM(NvmlDeviceGetTemperature);
DLSYM(NvmlInit);
DLSYM(NvmlShutdown);
DLSYM(NvmlSystemGetDriverVersion);
@ -125,6 +214,8 @@ bool xmrig::NvmlLib::load()
return false;
}
uv_dlsym(&nvmlLib, kNvmlDeviceGetFanSpeed_v2, reinterpret_cast<void**>(&pNvmlDeviceGetFanSpeed_v2));
if (pNvmlInit() != NVML_SUCCESS) {
return false;
}