Skip to content

Commit

Permalink
intel: rework into c++ class
Browse files Browse the repository at this point in the history
This allows us to properly clean up the thread and popen when exiting
  • Loading branch information
flightlessmango committed Nov 4, 2023
1 parent 0849ae4 commit 9411963
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 54 deletions.
81 changes: 28 additions & 53 deletions src/intel.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
#include <thread>
#include "overlay.h"
#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;
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> intel;

void Intel::intel_gpu_thread(){
init = true;
static char stdout_buffer[1024];
static FILE* intel_gpu_top;
if (runtime)
Expand Down Expand Up @@ -63,6 +50,8 @@ static void intelGpuThread(bool runtime){
num_line = 0;
}
num_iterations++;
if (stop)
break;
}

int exitcode = pclose(intel_gpu_top) / 256;
Expand All @@ -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];
Expand All @@ -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");
Expand Down Expand Up @@ -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;
}
49 changes: 49 additions & 0 deletions src/intel.h
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 3 additions & 1 deletion src/overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "iostats.h"
#include "amdgpu.h"
#include "fps_metrics.h"
#include "intel.h"

#ifdef __linux__
#include <libgen.h>
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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<Intel>();
break;
}
}
Expand Down

0 comments on commit 9411963

Please sign in to comment.