intel: rework into c++ class

This allows us to properly clean up the thread and popen when exiting
winesync
flightlessmango 6 months ago
parent 0849ae42b8
commit 9411963ad9

@ -1,21 +1,8 @@
#include <thread> #include "intel.h"
#include "overlay.h" std::unique_ptr<Intel> intel;
#include "gpu.h"
#include "spdlog/spdlog.h" void Intel::intel_gpu_thread(){
#include <nlohmann/json.hpp> init = true;
#include <sys/stat.h>
#include <filesystem.h>
#include <inttypes.h>
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;
static char stdout_buffer[1024]; static char stdout_buffer[1024];
static FILE* intel_gpu_top; static FILE* intel_gpu_top;
if (runtime) if (runtime)
@ -63,6 +50,8 @@ static void intelGpuThread(bool runtime){
num_line = 0; num_line = 0;
} }
num_iterations++; num_iterations++;
if (stop)
break;
} }
int exitcode = pclose(intel_gpu_top) / 256; 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("Missing permissions for '{}'", "intel_gpu_top");
SPDLOG_INFO("Disabling gpu_stats"); 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); rewind(fdinfo);
fflush(fdinfo); fflush(fdinfo);
char line[256]; char line[256];
@ -91,7 +80,7 @@ static uint64_t get_gpu_time() {
return val; return val;
} }
static FILE* find_fd() { FILE* Intel::find_fd() {
DIR* dir = opendir("/proc/self/fdinfo"); DIR* dir = opendir("/proc/self/fdinfo");
if (!dir) { if (!dir) {
perror("Failed to open directory"); perror("Failed to open directory");
@ -124,37 +113,23 @@ static FILE* find_fd() {
return NULL; // Return NULL if no matching file is found return NULL; // Return NULL if no matching file is found
} }
void getIntelGpuInfo(){ void Intel::get_fdinfo(){
if (!init_intel){ static uint64_t previous_gpu_time, previous_time, now, gpu_time_now;
fdinfo = find_fd(); gpu_time_now = get_gpu_time();
static bool runtime = false; now = os_time_get_nano();
static struct stat buffer;
if (stat("/run/pressure-vessel", &buffer) == 0) if (previous_time && previous_gpu_time && gpu_time_now > previous_gpu_time){
runtime = true; float time_since_last = now - previous_time;
float gpu_since_last = gpu_time_now - previous_gpu_time;
std::thread(intelGpuThread, runtime).detach(); auto result = int((gpu_since_last / time_since_last) * 100);
} if (result > 100)
result = 100;
if (fdinfo){
static uint64_t previous_gpu_time, previous_time, now, gpu_time_now; gpu_info_intel.load = result;
gpu_time_now = get_gpu_time(); previous_gpu_time = gpu_time_now;
now = os_time_get_nano(); previous_time = now;
} else {
if (previous_time && previous_gpu_time && gpu_time_now > previous_gpu_time){ previous_gpu_time = gpu_time_now;
float time_since_last = now - previous_time; previous_time = now;
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;
} }

@ -0,0 +1,49 @@
#include <sys/stat.h>
#include <thread>
#include <nlohmann/json.hpp>
#include <filesystem.h>
#include <inttypes.h>
#include <mesa/util/os_time.h>
#include <spdlog/spdlog.h>
#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> intel;

@ -24,6 +24,7 @@
#include "iostats.h" #include "iostats.h"
#include "amdgpu.h" #include "amdgpu.h"
#include "fps_metrics.h" #include "fps_metrics.h"
#include "intel.h"
#ifdef __linux__ #ifdef __linux__
#include <libgen.h> #include <libgen.h>
@ -135,7 +136,7 @@ void update_hw_info(const struct overlay_params& params, uint32_t vendorID)
getNvidiaGpuInfo(params); getNvidiaGpuInfo(params);
#ifdef __linux__ #ifdef __linux__
if (vendorID== 0x8086) if (vendorID== 0x8086)
getIntelGpuInfo(); if (intel) intel->update();
#endif #endif
} }
@ -823,6 +824,7 @@ void init_gpu_stats(uint32_t& vendorID, uint32_t reported_deviceID, overlay_para
path = drm + dir; path = drm + dir;
drm_dev = dir; drm_dev = dir;
SPDLOG_DEBUG("Intel: using drm device {}", drm_dev); SPDLOG_DEBUG("Intel: using drm device {}", drm_dev);
intel = std::make_unique<Intel>();
break; break;
} }
} }

Loading…
Cancel
Save