Skip to content

Commit a81d9e0

Browse files
committed
gpu_fdinfo: add gpu clock support for i915
1 parent ffd9c19 commit a81d9e0

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/gpu_fdinfo.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,55 @@ int GPU_fdinfo::get_gpu_load()
244244
return result;
245245
}
246246

247+
void GPU_fdinfo::find_intel_gt_dir()
248+
{
249+
std::string device = "/sys/bus/pci/devices/" + pci_dev + "/drm";
250+
251+
auto dir_iterator = fs::directory_iterator(device);
252+
253+
// Find first dir which starts with name "card"
254+
for (const auto& entry : fs::directory_iterator(device)) {
255+
auto path = entry.path().string();
256+
if (path.substr(device.size() + 1, 4) == "card") {
257+
device = path;
258+
break;
259+
}
260+
}
261+
262+
device += "/gt_cur_freq_mhz";
263+
264+
if (!fs::exists(device)) {
265+
SPDLOG_WARN(
266+
"Intel gt file ({}) not found. GPU clock will not be available.",
267+
device
268+
);
269+
return;
270+
}
271+
272+
gpu_clock_stream.open(device);
273+
274+
if (!gpu_clock_stream.good())
275+
SPDLOG_WARN("Intel gt dir: failed to open {}", device);
276+
}
277+
278+
int GPU_fdinfo::get_gpu_clock()
279+
{
280+
// Only i915 currently supported
281+
if (module != "i915" || !gpu_clock_stream.is_open())
282+
return 0;
283+
284+
std::string clock_str;
285+
286+
gpu_clock_stream.seekg(0);
287+
288+
std::getline(gpu_clock_stream, clock_str);
289+
290+
if (clock_str.empty())
291+
return 0;
292+
293+
return std::stoi(clock_str);
294+
}
295+
247296
void GPU_fdinfo::main_thread()
248297
{
249298
while (!stop_thread) {
@@ -255,6 +304,7 @@ void GPU_fdinfo::main_thread()
255304
metrics.load = get_gpu_load();
256305
metrics.memoryUsed = get_memory_used();
257306
metrics.powerUsage = get_power_usage();
307+
metrics.CoreClock = get_gpu_clock();
258308

259309
SPDLOG_DEBUG(
260310
"pci_dev = {}, pid = {}, module = {}, load = {}, mem = {}, power = {}",

src/gpu_fdinfo.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <spdlog/spdlog.h>
1515
#include <map>
1616
#include <set>
17-
#include <unistd.h>
1817

1918
class GPU_fdinfo {
2019
private:
@@ -46,7 +45,6 @@ class GPU_fdinfo {
4645

4746
void find_fd();
4847
void open_fdinfo_fd(std::string path);
49-
void find_intel_hwmon();
5048

5149
int get_gpu_load();
5250
uint64_t get_gpu_time();
@@ -58,10 +56,15 @@ class GPU_fdinfo {
5856

5957
float get_memory_used();
6058

59+
void find_intel_hwmon();
6160
float get_current_power();
6261
float get_power_usage();
6362
float last_power = 0;
6463

64+
std::ifstream gpu_clock_stream;
65+
void find_intel_gt_dir();
66+
int get_gpu_clock();
67+
6568
public:
6669
GPU_fdinfo(const std::string module, const std::string pci_dev)
6770
: module(module)
@@ -105,6 +108,9 @@ class GPU_fdinfo {
105108
if (module == "i915" || module == "xe")
106109
find_intel_hwmon();
107110

111+
if (module == "i915")
112+
find_intel_gt_dir();
113+
108114
std::thread thread(&GPU_fdinfo::main_thread, this);
109115
thread.detach();
110116
}

0 commit comments

Comments
 (0)