Skip to content

Commit

Permalink
gpu_fdinfo: add gpu clock support for i915
Browse files Browse the repository at this point in the history
  • Loading branch information
17314642 committed Dec 8, 2024
1 parent ffd9c19 commit a81d9e0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
50 changes: 50 additions & 0 deletions src/gpu_fdinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,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) {
Expand All @@ -255,6 +304,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 = {}",
Expand Down
10 changes: 8 additions & 2 deletions src/gpu_fdinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <spdlog/spdlog.h>
#include <map>
#include <set>
#include <unistd.h>

class GPU_fdinfo {
private:
Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit a81d9e0

Please sign in to comment.