Dynamically load nvml library
parent
9527868bb6
commit
8cdd9f19cf
@ -0,0 +1,160 @@
|
||||
// This is generated file. Do not modify directly.
|
||||
// Path to the code generator: /home/crz/git/MangoHud/generate_library_loader.py .
|
||||
|
||||
#include "loader_nvml.h"
|
||||
|
||||
// Put these sanity checks here so that they fire at most once
|
||||
// (to avoid cluttering the build output).
|
||||
#if !defined(LIBRARY_LOADER_NVML_H_DLOPEN) && !defined(LIBRARY_LOADER_NVML_H_DT_NEEDED)
|
||||
#error neither LIBRARY_LOADER_NVML_H_DLOPEN nor LIBRARY_LOADER_NVML_H_DT_NEEDED defined
|
||||
#endif
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DLOPEN) && defined(LIBRARY_LOADER_NVML_H_DT_NEEDED)
|
||||
#error both LIBRARY_LOADER_NVML_H_DLOPEN and LIBRARY_LOADER_NVML_H_DT_NEEDED defined
|
||||
#endif
|
||||
|
||||
libnvml_loader::libnvml_loader() : loaded_(false) {
|
||||
}
|
||||
|
||||
libnvml_loader::~libnvml_loader() {
|
||||
CleanUp(loaded_);
|
||||
}
|
||||
|
||||
bool libnvml_loader::Load(const std::string& library_name) {
|
||||
if (loaded_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DLOPEN)
|
||||
library_ = dlopen(library_name.c_str(), RTLD_LAZY);
|
||||
if (!library_)
|
||||
return false;
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DLOPEN)
|
||||
nvmlInit_v2 =
|
||||
reinterpret_cast<decltype(this->nvmlInit_v2)>(
|
||||
dlsym(library_, "nvmlInit_v2"));
|
||||
#endif
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DT_NEEDED)
|
||||
nvmlInit_v2 = &::nvmlInit_v2;
|
||||
#endif
|
||||
if (!nvmlInit_v2) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DLOPEN)
|
||||
nvmlShutdown =
|
||||
reinterpret_cast<decltype(this->nvmlShutdown)>(
|
||||
dlsym(library_, "nvmlShutdown"));
|
||||
#endif
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DT_NEEDED)
|
||||
nvmlShutdown = &::nvmlShutdown;
|
||||
#endif
|
||||
if (!nvmlShutdown) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DLOPEN)
|
||||
nvmlDeviceGetUtilizationRates =
|
||||
reinterpret_cast<decltype(this->nvmlDeviceGetUtilizationRates)>(
|
||||
dlsym(library_, "nvmlDeviceGetUtilizationRates"));
|
||||
#endif
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DT_NEEDED)
|
||||
nvmlDeviceGetUtilizationRates = &::nvmlDeviceGetUtilizationRates;
|
||||
#endif
|
||||
if (!nvmlDeviceGetUtilizationRates) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DLOPEN)
|
||||
nvmlDeviceGetTemperature =
|
||||
reinterpret_cast<decltype(this->nvmlDeviceGetTemperature)>(
|
||||
dlsym(library_, "nvmlDeviceGetTemperature"));
|
||||
#endif
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DT_NEEDED)
|
||||
nvmlDeviceGetTemperature = &::nvmlDeviceGetTemperature;
|
||||
#endif
|
||||
if (!nvmlDeviceGetTemperature) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DLOPEN)
|
||||
nvmlDeviceGetPciInfo_v3 =
|
||||
reinterpret_cast<decltype(this->nvmlDeviceGetPciInfo_v3)>(
|
||||
dlsym(library_, "nvmlDeviceGetPciInfo_v3"));
|
||||
#endif
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DT_NEEDED)
|
||||
nvmlDeviceGetPciInfo_v3 = &::nvmlDeviceGetPciInfo_v3;
|
||||
#endif
|
||||
if (!nvmlDeviceGetPciInfo_v3) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DLOPEN)
|
||||
nvmlDeviceGetCount_v2 =
|
||||
reinterpret_cast<decltype(this->nvmlDeviceGetCount_v2)>(
|
||||
dlsym(library_, "nvmlDeviceGetCount_v2"));
|
||||
#endif
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DT_NEEDED)
|
||||
nvmlDeviceGetCount_v2 = &::nvmlDeviceGetCount_v2;
|
||||
#endif
|
||||
if (!nvmlDeviceGetCount_v2) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DLOPEN)
|
||||
nvmlDeviceGetHandleByIndex_v2 =
|
||||
reinterpret_cast<decltype(this->nvmlDeviceGetHandleByIndex_v2)>(
|
||||
dlsym(library_, "nvmlDeviceGetHandleByIndex_v2"));
|
||||
#endif
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DT_NEEDED)
|
||||
nvmlDeviceGetHandleByIndex_v2 = &::nvmlDeviceGetHandleByIndex_v2;
|
||||
#endif
|
||||
if (!nvmlDeviceGetHandleByIndex_v2) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DLOPEN)
|
||||
nvmlDeviceGetHandleByPciBusId_v2 =
|
||||
reinterpret_cast<decltype(this->nvmlDeviceGetHandleByPciBusId_v2)>(
|
||||
dlsym(library_, "nvmlDeviceGetHandleByPciBusId_v2"));
|
||||
#endif
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DT_NEEDED)
|
||||
nvmlDeviceGetHandleByPciBusId_v2 = &::nvmlDeviceGetHandleByPciBusId_v2;
|
||||
#endif
|
||||
if (!nvmlDeviceGetHandleByPciBusId_v2) {
|
||||
CleanUp(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
loaded_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void libnvml_loader::CleanUp(bool unload) {
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DLOPEN)
|
||||
if (unload) {
|
||||
dlclose(library_);
|
||||
library_ = NULL;
|
||||
}
|
||||
#endif
|
||||
loaded_ = false;
|
||||
nvmlInit_v2 = NULL;
|
||||
nvmlShutdown = NULL;
|
||||
nvmlDeviceGetUtilizationRates = NULL;
|
||||
nvmlDeviceGetTemperature = NULL;
|
||||
nvmlDeviceGetPciInfo_v3 = NULL;
|
||||
nvmlDeviceGetCount_v2 = NULL;
|
||||
nvmlDeviceGetHandleByIndex_v2 = NULL;
|
||||
nvmlDeviceGetHandleByPciBusId_v2 = NULL;
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
// This is generated file. Do not modify directly.
|
||||
// Path to the code generator: /home/crz/git/MangoHud/generate_library_loader.py .
|
||||
|
||||
#ifndef LIBRARY_LOADER_NVML_H
|
||||
#define LIBRARY_LOADER_NVML_H
|
||||
|
||||
#include "nvml.h"
|
||||
#define LIBRARY_LOADER_NVML_H_DLOPEN
|
||||
|
||||
#include <string>
|
||||
#include <dlfcn.h>
|
||||
|
||||
class libnvml_loader {
|
||||
public:
|
||||
libnvml_loader();
|
||||
libnvml_loader(const std::string& library_name) { Load(library_name); }
|
||||
~libnvml_loader();
|
||||
|
||||
bool Load(const std::string& library_name);
|
||||
bool IsLoaded() { return loaded_; }
|
||||
|
||||
decltype(&::nvmlInit_v2) nvmlInit_v2;
|
||||
decltype(&::nvmlShutdown) nvmlShutdown;
|
||||
decltype(&::nvmlDeviceGetUtilizationRates) nvmlDeviceGetUtilizationRates;
|
||||
decltype(&::nvmlDeviceGetTemperature) nvmlDeviceGetTemperature;
|
||||
decltype(&::nvmlDeviceGetPciInfo_v3) nvmlDeviceGetPciInfo_v3;
|
||||
decltype(&::nvmlDeviceGetCount_v2) nvmlDeviceGetCount_v2;
|
||||
decltype(&::nvmlDeviceGetHandleByIndex_v2) nvmlDeviceGetHandleByIndex_v2;
|
||||
decltype(&::nvmlDeviceGetHandleByPciBusId_v2) nvmlDeviceGetHandleByPciBusId_v2;
|
||||
|
||||
|
||||
private:
|
||||
void CleanUp(bool unload);
|
||||
|
||||
#if defined(LIBRARY_LOADER_NVML_H_DLOPEN)
|
||||
void* library_;
|
||||
#endif
|
||||
|
||||
bool loaded_;
|
||||
|
||||
// Disallow copy constructor and assignment operator.
|
||||
libnvml_loader(const libnvml_loader&);
|
||||
void operator=(const libnvml_loader&);
|
||||
};
|
||||
|
||||
#endif // LIBRARY_LOADER_NVML_H
|
@ -1,17 +0,0 @@
|
||||
#include "nvidia_info.h"
|
||||
#include <nvml.h>
|
||||
|
||||
void checkNvidia(){
|
||||
result = nvmlInit();
|
||||
if (NVML_SUCCESS != result) {
|
||||
printf("MANGOHUD: Nvidia module not loaded\n");
|
||||
} else {
|
||||
nvmlSuccess = true;
|
||||
}
|
||||
}
|
||||
|
||||
void getNvidiaInfo(){
|
||||
nvmlDeviceGetHandleByIndex(0, &nvidiaDevice);
|
||||
nvmlDeviceGetUtilizationRates(nvidiaDevice, &nvidiaUtilization);
|
||||
nvmlDeviceGetTemperature(nvidiaDevice, NVML_TEMPERATURE_GPU, &nvidiaTemp);
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
#include "loaders/loader_nvml.h"
|
||||
#include "nvidia_info.h"
|
||||
|
||||
libnvml_loader nvml("libnvidia-ml.so.1");
|
||||
|
||||
nvmlReturn_t result;
|
||||
nvmlDevice_t nvidiaDevice;
|
||||
bool nvmlSuccess;
|
||||
unsigned int nvidiaTemp;
|
||||
struct nvmlUtilization_st nvidiaUtilization;
|
||||
|
||||
void checkNvidia(){
|
||||
if (nvml.IsLoaded()){
|
||||
result = nvml.nvmlInit();
|
||||
if (NVML_SUCCESS != result) {
|
||||
printf("MANGOHUD: Nvidia module not loaded\n");
|
||||
} else {
|
||||
nvmlSuccess = true;
|
||||
}
|
||||
} else {
|
||||
printf("Failed to load NVML!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void getNvidiaInfo(){
|
||||
nvml.nvmlDeviceGetHandleByIndex(0, &nvidiaDevice);
|
||||
nvml.nvmlDeviceGetUtilizationRates(nvidiaDevice, &nvidiaUtilization);
|
||||
nvml.nvmlDeviceGetTemperature(nvidiaDevice, NVML_TEMPERATURE_GPU, &nvidiaTemp);
|
||||
}
|
Loading…
Reference in New Issue