intel: rework into c++ class

This allows us to properly clean up the thread and popen when exiting
This commit is contained in:
flightlessmango 2023-11-04 17:23:13 +01:00
parent 0849ae42b8
commit 9411963ad9
3 changed files with 78 additions and 52 deletions

View File

@ -1,21 +1,8 @@
#include <thread> #include "intel.h"
#include "overlay.h" std::unique_ptr<Intel> intel;
#include "gpu.h"
#include "spdlog/spdlog.h"
#include <nlohmann/json.hpp>
#include <sys/stat.h>
#include <filesystem.h>
#include <inttypes.h>
using json = nlohmann::json; void Intel::intel_gpu_thread(){
namespace fs = ghc::filesystem; init = true;
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,18 +113,7 @@ 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){
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; static uint64_t previous_gpu_time, previous_time, now, gpu_time_now;
gpu_time_now = get_gpu_time(); gpu_time_now = get_gpu_time();
now = os_time_get_nano(); now = os_time_get_nano();
@ -155,6 +133,3 @@ void getIntelGpuInfo(){
previous_time = now; previous_time = now;
} }
} }
gpu_info = gpu_info_intel;
}

49
src/intel.h Normal file
View File

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

View File

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