Load libnvidia-ml and libXNVCtrl on demand

sdf
jackun 4 years ago
parent 24b731f78c
commit fcefc07966
No known key found for this signature in database
GPG Key ID: 119DB3F1D05A9ED3

@ -1,5 +1,6 @@
#include "loader_nvctrl.h"
#include <iostream>
#include <memory>
// Put these sanity checks here so that they fire at most once
// (to avoid cluttering the build output).
@ -10,6 +11,15 @@
#error both LIBRARY_LOADER_NVCTRL_H_DLOPEN and LIBRARY_LOADER_NVCTRL_H_DT_NEEDED defined
#endif
static std::unique_ptr<libnvctrl_loader> libnvctrl_;
libnvctrl_loader& get_libnvctrl_loader()
{
if (!libnvctrl_)
libnvctrl_ = std::make_unique<libnvctrl_loader>("libXNVCtrl.so.0");
return *libnvctrl_.get();
}
libnvctrl_loader::libnvctrl_loader() : loaded_(false) {
}

@ -1,6 +1,3 @@
// This is generated file. Do not modify directly.
// Path to the code generator: /home/crz/git/MangoHud/generate_library_loader.py .
#ifndef LIBRARY_LOADER_NVCTRL_H
#define LIBRARY_LOADER_NVCTRL_H
#define Bool bool
@ -8,7 +5,6 @@
#include "NVCtrl/NVCtrlLib.h"
#define LIBRARY_LOADER_NVCTRL_H_DLOPEN
#include <string>
#include <dlfcn.h>
@ -43,4 +39,5 @@ class libnvctrl_loader {
void operator=(const libnvctrl_loader&);
};
libnvctrl_loader& get_libnvctrl_loader();
#endif // LIBRARY_LOADER_NVCTRL_H

@ -3,6 +3,7 @@
#include "loader_nvml.h"
#include <iostream>
#include <memory>
// Put these sanity checks here so that they fire at most once
// (to avoid cluttering the build output).
@ -13,6 +14,15 @@
#error both LIBRARY_LOADER_NVML_H_DLOPEN and LIBRARY_LOADER_NVML_H_DT_NEEDED defined
#endif
static std::unique_ptr<libnvml_loader> libnvml_;
libnvml_loader& get_libnvml_loader()
{
if (!libnvml_)
libnvml_ = std::make_unique<libnvml_loader>("libnvidia-ml.so.1");
return *libnvml_.get();
}
libnvml_loader::libnvml_loader() : loaded_(false) {
}

@ -52,4 +52,5 @@ class libnvml_loader {
void operator=(const libnvml_loader&);
};
libnvml_loader& get_libnvml_loader();
#endif // LIBRARY_LOADER_NVML_H

@ -13,11 +13,10 @@
typedef std::unordered_map<std::string, std::string> string_map;
static std::unique_ptr<Display, std::function<void(Display*)>> display;
libnvctrl_loader nvctrl("libXNVCtrl.so.0");
struct nvctrlInfo nvctrl_info;
bool nvctrlSuccess = false;
static bool find_nv_x11(Display*& dpy)
static bool find_nv_x11(libnvctrl_loader& nvctrl, Display*& dpy)
{
char buf[8] {};
for (int i = 0; i < 16; i++) {
@ -42,13 +41,14 @@ bool checkXNVCtrl()
return false;
}
auto& nvctrl = get_libnvctrl_loader();
if (!nvctrl.IsLoaded()) {
std::cerr << "MANGOHUD: XNVCtrl loader failed to load\n";
return false;
}
Display *dpy;
nvctrlSuccess = find_nv_x11(dpy);
nvctrlSuccess = find_nv_x11(nvctrl, dpy);
if (!nvctrlSuccess) {
std::cerr << "MANGOHUD: XNVCtrl didn't find the correct display" << std::endl;
@ -91,7 +91,7 @@ static void parse_token(std::string token, string_map& options) {
options[param] = value;
}
char* get_attr_target_string(int attr, int target_type, int target_id) {
char* get_attr_target_string(libnvctrl_loader& nvctrl, int attr, int target_type, int target_id) {
char* c = nullptr;
if (!nvctrl.XNVCTRLQueryTargetStringAttribute(display.get(), target_type, target_id, 0, attr, &c)) {
std::cerr << "Failed to query attribute '" << attr << "'.\n";
@ -102,6 +102,7 @@ char* get_attr_target_string(int attr, int target_type, int target_id) {
void getNvctrlInfo(){
string_map params;
std::string token;
auto& nvctrl = get_libnvctrl_loader();
if (!display)
return;
@ -113,7 +114,7 @@ void getNvctrlInfo(){
};
for (size_t i=0; enums[i]; i++) {
char* str = get_attr_target_string(enums[i], NV_CTRL_TARGET_TYPE_GPU, 0);
char* str = get_attr_target_string(nvctrl, enums[i], NV_CTRL_TARGET_TYPE_GPU, 0);
if (!str)
continue;

@ -3,8 +3,6 @@
#include <iostream>
#include "overlay.h"
libnvml_loader nvml("libnvidia-ml.so.1");
nvmlReturn_t result;
nvmlDevice_t nvidiaDevice;
nvmlPciInfo_t nvidiaPciInfo;
@ -14,6 +12,7 @@ struct nvmlUtilization_st nvidiaUtilization;
struct nvmlMemory_st nvidiaMemory {};
bool checkNVML(const char* pciBusId){
auto& nvml = get_libnvml_loader();
if (nvml.IsLoaded()){
result = nvml.nvmlInit();
if (NVML_SUCCESS != result) {
@ -42,6 +41,7 @@ bool checkNVML(const char* pciBusId){
bool getNVMLInfo(){
nvmlReturn_t response;
auto& nvml = get_libnvml_loader();
response = nvml.nvmlDeviceGetUtilizationRates(nvidiaDevice, &nvidiaUtilization);
nvml.nvmlDeviceGetTemperature(nvidiaDevice, NVML_TEMPERATURE_GPU, &nvidiaTemp);
nvml.nvmlDeviceGetMemoryInfo(nvidiaDevice, &nvidiaMemory);

Loading…
Cancel
Save