Dynamically load nvml library

pull/20/head
FlightlessMango 4 years ago
parent 9527868bb6
commit 8cdd9f19cf

@ -13,10 +13,8 @@
#include <string>
#include <sstream>
#include <regex>
extern "C"
{
#include "nvidia_info.h"
}
#include "nvidia_info.h"
using namespace std;
int gpuLoad, gpuTemp, cpuTemp;
@ -191,7 +189,6 @@ void *cpuInfo(void *){
}
void *getNvidiaGpuInfo(void *){
#ifdef HAVE_NVML
if (!nvmlSuccess)
checkNvidia();
@ -201,7 +198,6 @@ void *getNvidiaGpuInfo(void *){
gpuLoadDisplay = gpuLoad;
gpuTemp = nvidiaTemp;
}
#endif
pthread_detach(nvidiaSmiThread);
return NULL;

@ -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

@ -35,14 +35,10 @@ vklayer_files = files(
'overlay.cpp',
'overlay_params.c',
'font_unispace.c',
'loaders/loader_nvml.cpp',
'nvml.cpp',
)
dep_nvml = cc.find_library('nvidia-ml', required: false)
if dep_nvml.found()
vklayer_files += 'nvml.c'
pre_args += '-DHAVE_NVML'
endif
vklayer_mesa_overlay = shared_library(
'MangoHud',
util_files,
@ -65,8 +61,7 @@ vklayer_mesa_overlay = shared_library(
vulkan_wsi_deps,
libimgui_core_dep,
dep_dl,
dep_pthread,
dep_nvml],
dep_pthread],
include_directories : inc_common,
link_args : cc.get_supported_link_arguments(['-Wl,-Bsymbolic-functions', '-Wl,-z,relro']),
install : true

@ -2,12 +2,12 @@
#include <stdio.h>
#include <nvml.h>
nvmlReturn_t result;
unsigned int nvidiaTemp, processSamplesCount, lastSeenTimeStamp, *vgpuInstanceSamplesCount;
nvmlValueType_t *sampleValType;
nvmlDevice_t nvidiaDevice;
struct nvmlUtilization_st nvidiaUtilization;
bool nvmlSuccess;
extern nvmlReturn_t result;
extern unsigned int nvidiaTemp, processSamplesCount, lastSeenTimeStamp, *vgpuInstanceSamplesCount;
extern nvmlValueType_t *sampleValType;
extern nvmlDevice_t nvidiaDevice;
extern struct nvmlUtilization_st nvidiaUtilization;
extern bool nvmlSuccess;
void checkNvidia(void);
void getNvidiaInfo(void);

@ -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…
Cancel
Save