2022-11-01 11:01:50 +00:00
|
|
|
#include <thread>
|
|
|
|
#include "overlay.h"
|
|
|
|
#include "gpu.h"
|
|
|
|
#include "spdlog/spdlog.h"
|
2022-11-29 05:18:37 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2023-06-08 15:08:52 +00:00
|
|
|
#include <sys/stat.h>
|
2022-11-29 05:18:37 +00:00
|
|
|
using json = nlohmann::json;
|
2022-11-01 11:01:50 +00:00
|
|
|
|
|
|
|
static bool init_intel = false;
|
|
|
|
struct gpuInfo gpu_info_intel {};
|
2022-11-29 05:18:37 +00:00
|
|
|
|
2023-06-07 14:51:48 +00:00
|
|
|
static void intelGpuThread(bool runtime){
|
2022-11-01 11:01:50 +00:00
|
|
|
init_intel = true;
|
|
|
|
static char stdout_buffer[1024];
|
2023-06-07 14:51:48 +00:00
|
|
|
static FILE* intel_gpu_top;
|
|
|
|
if (runtime)
|
|
|
|
intel_gpu_top = popen("steam-runtime-launch-client --alongside-steam --host -- intel_gpu_top -J -s 500", "r");
|
|
|
|
else
|
|
|
|
intel_gpu_top = popen("intel_gpu_top -J -s 500", "r");
|
|
|
|
|
2022-11-29 05:18:37 +00:00
|
|
|
int num_line = 0;
|
|
|
|
std::string buf;
|
|
|
|
int num_iterations = 0;
|
2022-11-01 11:01:50 +00:00
|
|
|
while (fgets(stdout_buffer, sizeof(stdout_buffer), intel_gpu_top)) {
|
2022-11-29 05:18:37 +00:00
|
|
|
if (num_line > 0)
|
|
|
|
buf += stdout_buffer;
|
|
|
|
|
|
|
|
num_line++;
|
|
|
|
if (strlen(stdout_buffer) < 4 && !strchr(stdout_buffer, '{') && !strchr(stdout_buffer, ',')) {
|
|
|
|
if (buf[0] != '{')
|
|
|
|
buf = "{\n" + buf;
|
|
|
|
|
|
|
|
if (num_iterations > 0){
|
|
|
|
buf += "\n}";
|
|
|
|
json j = json::parse(buf);
|
|
|
|
if (j.contains("engines"))
|
|
|
|
if (j["engines"].contains("Render/3D/0"))
|
|
|
|
if (j["engines"]["Render/3D/0"].contains("busy"))
|
|
|
|
gpu_info_intel.load = j["engines"]["Render/3D/0"]["busy"].get<int>();
|
|
|
|
|
|
|
|
if (j.contains("frequency"))
|
|
|
|
if (j["frequency"].contains("actual"))
|
|
|
|
gpu_info_intel.CoreClock = j["frequency"]["actual"].get<int>();
|
|
|
|
if (j.contains("power")){
|
|
|
|
if (j["power"].contains("GPU"))
|
|
|
|
gpu_info_intel.powerUsage = j["power"]["GPU"].get<float>();
|
|
|
|
if (j["power"].contains("Package"))
|
|
|
|
gpu_info_intel.apu_cpu_power = j["power"]["Package"].get<float>();
|
|
|
|
}
|
|
|
|
|
2022-11-01 11:01:50 +00:00
|
|
|
}
|
2022-11-29 05:18:37 +00:00
|
|
|
buf = "";
|
|
|
|
num_line = 0;
|
2022-11-01 11:01:50 +00:00
|
|
|
}
|
2022-11-29 05:18:37 +00:00
|
|
|
num_iterations++;
|
2022-11-01 11:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int exitcode = pclose(intel_gpu_top) / 256;
|
|
|
|
if (exitcode > 0){
|
|
|
|
if (exitcode == 127)
|
|
|
|
SPDLOG_INFO("Failed to open '{}'", "intel_gpu_top");
|
|
|
|
|
|
|
|
if (exitcode == 1)
|
|
|
|
SPDLOG_INFO("Missing permissions for '{}'", "intel_gpu_top");
|
2023-03-03 10:43:59 +00:00
|
|
|
|
2022-11-01 11:01:50 +00:00
|
|
|
SPDLOG_INFO("Disabling gpu_stats");
|
|
|
|
_params->enabled[OVERLAY_PARAM_ENABLED_gpu_stats] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void getIntelGpuInfo(){
|
2023-06-07 14:51:48 +00:00
|
|
|
if (!init_intel){
|
|
|
|
static bool runtime = false;
|
2023-06-08 15:08:52 +00:00
|
|
|
static struct stat buffer;
|
|
|
|
if (stat("/run/pressure-vessel", &buffer) == 0)
|
|
|
|
runtime = true;
|
2023-06-07 14:51:48 +00:00
|
|
|
|
|
|
|
std::thread(intelGpuThread, runtime).detach();
|
|
|
|
}
|
2022-11-01 11:01:50 +00:00
|
|
|
|
|
|
|
gpu_info = gpu_info_intel;
|
2023-03-03 10:43:59 +00:00
|
|
|
}
|