diff --git a/src/gpu_fdinfo.cpp b/src/gpu_fdinfo.cpp index ad2e6cc661..6eedb2dc01 100644 --- a/src/gpu_fdinfo.cpp +++ b/src/gpu_fdinfo.cpp @@ -1,4 +1,6 @@ #include "gpu_fdinfo.h" +#include "filesystem.h" +#include "spdlog/spdlog.h" namespace fs = ghc::filesystem; void GPU_fdinfo::find_fd() @@ -244,6 +246,55 @@ int GPU_fdinfo::get_gpu_load() return result; } +void GPU_fdinfo::find_intel_gt_dir() +{ + std::string device = "/sys/bus/pci/devices/" + pci_dev + "/drm"; + + auto dir_iterator = fs::directory_iterator(device); + + // Find first dir which starts with name "card" + for (const auto& entry : fs::directory_iterator(device)) { + auto path = entry.path().string(); + if (path.substr(device.size() + 1, 4) == "card") { + device = path; + break; + } + } + + device += "/gt_cur_freq_mhz"; + + if (!fs::exists(device)) { + SPDLOG_WARN( + "Intel gt file ({}) not found. GPU clock will not be available.", + device + ); + return; + } + + gpu_clock_stream.open(device); + + if (!gpu_clock_stream.good()) + SPDLOG_WARN("Intel gt dir: failed to open {}", device); +} + +int GPU_fdinfo::get_gpu_clock() +{ + // Only i915 currently supported + if (module != "i915" || !gpu_clock_stream.is_open()) + return 0; + + std::string clock_str; + + gpu_clock_stream.seekg(0); + + std::getline(gpu_clock_stream, clock_str); + + if (clock_str.empty()) + return 0; + + return std::stoi(clock_str); +} + void GPU_fdinfo::main_thread() { while (!stop_thread) { @@ -255,6 +306,7 @@ void GPU_fdinfo::main_thread() metrics.load = get_gpu_load(); metrics.memoryUsed = get_memory_used(); metrics.powerUsage = get_power_usage(); + metrics.CoreClock = get_gpu_clock(); SPDLOG_DEBUG( "pci_dev = {}, pid = {}, module = {}, load = {}, mem = {}, power = {}", diff --git a/src/gpu_fdinfo.h b/src/gpu_fdinfo.h index b477c60e9a..dc2e1f45cc 100644 --- a/src/gpu_fdinfo.h +++ b/src/gpu_fdinfo.h @@ -14,7 +14,6 @@ #include #include #include -#include class GPU_fdinfo { private: @@ -46,7 +45,6 @@ class GPU_fdinfo { void find_fd(); void open_fdinfo_fd(std::string path); - void find_intel_hwmon(); int get_gpu_load(); uint64_t get_gpu_time(); @@ -58,10 +56,15 @@ class GPU_fdinfo { float get_memory_used(); + void find_intel_hwmon(); float get_current_power(); float get_power_usage(); float last_power = 0; + std::ifstream gpu_clock_stream; + void find_intel_gt_dir(); + int get_gpu_clock(); + public: GPU_fdinfo(const std::string module, const std::string pci_dev) : module(module) @@ -105,6 +108,9 @@ class GPU_fdinfo { if (module == "i915" || module == "xe") find_intel_hwmon(); + if (module == "i915") + find_intel_gt_dir(); + std::thread thread(&GPU_fdinfo::main_thread, this); thread.detach(); }