From ddbb4f5be1dba235ebfd770f622c5fe8be46987e Mon Sep 17 00:00:00 2001 From: FlightlessMango Date: Tue, 29 Nov 2022 06:18:37 +0100 Subject: [PATCH] intel: use json for parsing intel_gpu_top output --- meson.build | 2 +- src/intel.cpp | 59 ++++++++++++++++++++++++++++++------------------- src/meson.build | 3 ++- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/meson.build b/meson.build index ecc12bcd..9cdeff02 100644 --- a/meson.build +++ b/meson.build @@ -277,9 +277,9 @@ endif if get_option('mangoapp') or get_option('mangoapp_layer') glfw3_dep = dependency('glfw3') - json_dep = dependency('nlohmann_json') endif +json_dep = dependency('nlohmann_json') subdir('src') if get_option('include_doc') diff --git a/src/intel.cpp b/src/intel.cpp index abba8b0c..8ae1de3d 100644 --- a/src/intel.cpp +++ b/src/intel.cpp @@ -2,38 +2,51 @@ #include "overlay.h" #include "gpu.h" #include "spdlog/spdlog.h" +#include +using json = nlohmann::json; static bool init_intel = false; struct gpuInfo gpu_info_intel {}; + void intelGpuThread(){ init_intel = true; static char stdout_buffer[1024]; - FILE* intel_gpu_top = popen("intel_gpu_top -l -s 500", "r"); + FILE* intel_gpu_top = popen("intel_gpu_top -J -s 500", "r"); + int num_line = 0; + std::string buf; + int num_iterations = 0; while (fgets(stdout_buffer, sizeof(stdout_buffer), intel_gpu_top)) { - if (strstr(stdout_buffer, "Freq") == NULL && - strstr(stdout_buffer, "req") == NULL) { - char * pch; - pch = strtok(stdout_buffer, " "); - int i = 0; - while (pch != NULL){ - switch (i){ - case 0: - gpu_info_intel.CoreClock = atoi(pch); - break; - case 4: - gpu_info_intel.powerUsage = atof(pch); - break; - case 5: - gpu_info_intel.apu_cpu_power = atof(pch); - break; - case 8: - gpu_info_intel.load = atoi(pch); - break; + 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(); + + if (j.contains("frequency")) + if (j["frequency"].contains("actual")) + gpu_info_intel.CoreClock = j["frequency"]["actual"].get(); + if (j.contains("power")){ + if (j["power"].contains("GPU")) + gpu_info_intel.powerUsage = j["power"]["GPU"].get(); + if (j["power"].contains("Package")) + gpu_info_intel.apu_cpu_power = j["power"]["Package"].get(); + } + } - pch = strtok(NULL, " "); - i++; - } + buf = ""; + num_line = 0; } + num_iterations++; } int exitcode = pclose(intel_gpu_top) / 256; diff --git a/src/meson.build b/src/meson.build index 732632b8..d08607f9 100644 --- a/src/meson.build +++ b/src/meson.build @@ -186,7 +186,8 @@ vklayer_mesa_overlay = shared_library( dep_rt, dep_pthread, dep_vulkan, - windows_deps], + windows_deps, + json_dep], include_directories : [inc_common], link_args : link_args, install_dir : libdir_mangohud,