From fcefc07966d1373cd56fa940e9e8022d6e7878a6 Mon Sep 17 00:00:00 2001 From: jackun Date: Tue, 4 Aug 2020 21:27:12 +0300 Subject: [PATCH] Load libnvidia-ml and libXNVCtrl on demand --- src/loaders/loader_nvctrl.cpp | 10 ++++++++++ src/loaders/loader_nvctrl.h | 5 +---- src/loaders/loader_nvml.cpp | 10 ++++++++++ src/loaders/loader_nvml.h | 1 + src/nvctrl.cpp | 11 ++++++----- src/nvml.cpp | 4 ++-- 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/loaders/loader_nvctrl.cpp b/src/loaders/loader_nvctrl.cpp index e5fca7c9..915b1e3e 100644 --- a/src/loaders/loader_nvctrl.cpp +++ b/src/loaders/loader_nvctrl.cpp @@ -1,5 +1,6 @@ #include "loader_nvctrl.h" #include +#include // 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_; + +libnvctrl_loader& get_libnvctrl_loader() +{ + if (!libnvctrl_) + libnvctrl_ = std::make_unique("libXNVCtrl.so.0"); + return *libnvctrl_.get(); +} + libnvctrl_loader::libnvctrl_loader() : loaded_(false) { } diff --git a/src/loaders/loader_nvctrl.h b/src/loaders/loader_nvctrl.h index 5aae4393..8ddb532a 100644 --- a/src/loaders/loader_nvctrl.h +++ b/src/loaders/loader_nvctrl.h @@ -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 #include @@ -43,4 +39,5 @@ class libnvctrl_loader { void operator=(const libnvctrl_loader&); }; +libnvctrl_loader& get_libnvctrl_loader(); #endif // LIBRARY_LOADER_NVCTRL_H diff --git a/src/loaders/loader_nvml.cpp b/src/loaders/loader_nvml.cpp index d09fe541..eef5875b 100644 --- a/src/loaders/loader_nvml.cpp +++ b/src/loaders/loader_nvml.cpp @@ -3,6 +3,7 @@ #include "loader_nvml.h" #include +#include // 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_; + +libnvml_loader& get_libnvml_loader() +{ + if (!libnvml_) + libnvml_ = std::make_unique("libnvidia-ml.so.1"); + return *libnvml_.get(); +} + libnvml_loader::libnvml_loader() : loaded_(false) { } diff --git a/src/loaders/loader_nvml.h b/src/loaders/loader_nvml.h index 6cd0aebb..b7ec30e0 100644 --- a/src/loaders/loader_nvml.h +++ b/src/loaders/loader_nvml.h @@ -52,4 +52,5 @@ class libnvml_loader { void operator=(const libnvml_loader&); }; +libnvml_loader& get_libnvml_loader(); #endif // LIBRARY_LOADER_NVML_H diff --git a/src/nvctrl.cpp b/src/nvctrl.cpp index 687bf81e..792c99e7 100644 --- a/src/nvctrl.cpp +++ b/src/nvctrl.cpp @@ -13,11 +13,10 @@ typedef std::unordered_map string_map; static std::unique_ptr> 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; diff --git a/src/nvml.cpp b/src/nvml.cpp index c5669e4c..6a5a5c80 100644 --- a/src/nvml.cpp +++ b/src/nvml.cpp @@ -3,8 +3,6 @@ #include #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);