diff --git a/src/intel.cpp b/src/intel.cpp index 1c9b428e..d61fa56f 100644 --- a/src/intel.cpp +++ b/src/intel.cpp @@ -1,21 +1,8 @@ -#include -#include "overlay.h" -#include "gpu.h" -#include "spdlog/spdlog.h" -#include -#include -#include -#include - -using json = nlohmann::json; -namespace fs = ghc::filesystem; - -static bool init_intel = false; -struct gpuInfo gpu_info_intel {}; -FILE* fdinfo; - -static void intelGpuThread(bool runtime){ - init_intel = true; +#include "intel.h" +std::unique_ptr intel; + +void Intel::intel_gpu_thread(){ + init = true; static char stdout_buffer[1024]; static FILE* intel_gpu_top; if (runtime) @@ -63,6 +50,8 @@ static void intelGpuThread(bool runtime){ num_line = 0; } num_iterations++; + if (stop) + break; } int exitcode = pclose(intel_gpu_top) / 256; @@ -74,11 +63,11 @@ static void intelGpuThread(bool runtime){ SPDLOG_INFO("Missing permissions for '{}'", "intel_gpu_top"); SPDLOG_INFO("Disabling gpu_stats"); - _params->enabled[OVERLAY_PARAM_ENABLED_gpu_stats] = false; + HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_gpu_stats] = false; } } -static uint64_t get_gpu_time() { +uint64_t Intel::get_gpu_time() { rewind(fdinfo); fflush(fdinfo); char line[256]; @@ -91,7 +80,7 @@ static uint64_t get_gpu_time() { return val; } -static FILE* find_fd() { +FILE* Intel::find_fd() { DIR* dir = opendir("/proc/self/fdinfo"); if (!dir) { perror("Failed to open directory"); @@ -124,37 +113,23 @@ static FILE* find_fd() { return NULL; // Return NULL if no matching file is found } -void getIntelGpuInfo(){ - if (!init_intel){ - fdinfo = find_fd(); - static bool runtime = false; - static struct stat buffer; - if (stat("/run/pressure-vessel", &buffer) == 0) - runtime = true; - - std::thread(intelGpuThread, runtime).detach(); - } - - if (fdinfo){ - static uint64_t previous_gpu_time, previous_time, now, gpu_time_now; - gpu_time_now = get_gpu_time(); - now = os_time_get_nano(); - - if (previous_time && previous_gpu_time && gpu_time_now > previous_gpu_time){ - float time_since_last = now - previous_time; - float gpu_since_last = gpu_time_now - previous_gpu_time; - auto result = int((gpu_since_last / time_since_last) * 100); - if (result > 100) - result = 100; - - gpu_info_intel.load = result; - previous_gpu_time = gpu_time_now; - previous_time = now; - } else { - previous_gpu_time = gpu_time_now; - previous_time = now; - } +void Intel::get_fdinfo(){ + static uint64_t previous_gpu_time, previous_time, now, gpu_time_now; + gpu_time_now = get_gpu_time(); + now = os_time_get_nano(); + + if (previous_time && previous_gpu_time && gpu_time_now > previous_gpu_time){ + float time_since_last = now - previous_time; + float gpu_since_last = gpu_time_now - previous_gpu_time; + auto result = int((gpu_since_last / time_since_last) * 100); + if (result > 100) + result = 100; + + gpu_info_intel.load = result; + previous_gpu_time = gpu_time_now; + previous_time = now; + } else { + previous_gpu_time = gpu_time_now; + previous_time = now; } - - gpu_info = gpu_info_intel; } diff --git a/src/intel.h b/src/intel.h new file mode 100644 index 00000000..4b080986 --- /dev/null +++ b/src/intel.h @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include +#include "gpu.h" +#include "hud_elements.h" + +using json = nlohmann::json; +namespace fs = ghc::filesystem; + +class Intel { + private: + bool init = false; + bool runtime = false; + bool stop = false; + struct gpuInfo gpu_info_intel {}; + FILE* fdinfo; + struct stat stat_buffer; + std::thread thread; + + FILE* find_fd(); + void intel_gpu_thread(); + uint64_t get_gpu_time(); + void get_fdinfo(); + + public: + Intel() { + if (stat("/run/pressure-vessel", &stat_buffer) == 0) + runtime = true; + + fdinfo = find_fd(); + thread = std::thread(&Intel::intel_gpu_thread, this); + } + + void update() { + get_fdinfo(); + gpu_info = gpu_info_intel; + } + + ~Intel(){ + stop = true; + thread.join(); + } +}; + +extern std::unique_ptr intel; diff --git a/src/overlay.cpp b/src/overlay.cpp index c528531f..ec192bd8 100644 --- a/src/overlay.cpp +++ b/src/overlay.cpp @@ -24,6 +24,7 @@ #include "iostats.h" #include "amdgpu.h" #include "fps_metrics.h" +#include "intel.h" #ifdef __linux__ #include @@ -135,7 +136,7 @@ void update_hw_info(const struct overlay_params& params, uint32_t vendorID) getNvidiaGpuInfo(params); #ifdef __linux__ if (vendorID== 0x8086) - getIntelGpuInfo(); + if (intel) intel->update(); #endif } @@ -823,6 +824,7 @@ void init_gpu_stats(uint32_t& vendorID, uint32_t reported_deviceID, overlay_para path = drm + dir; drm_dev = dir; SPDLOG_DEBUG("Intel: using drm device {}", drm_dev); + intel = std::make_unique(); break; } }