Skip to content

Commit ae4c411

Browse files
17314642flightlessmango
authored andcommitted
gpu_fdinfo: add support for intel xe driver
1 parent af0d80c commit ae4c411

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

src/gpu.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ std::string GPU::is_i915_or_xe() {
8686
return "";
8787
}
8888

89-
std::string driver = fs::read_symlink(path);
89+
std::string driver = fs::read_symlink(path).string();
9090
driver = driver.substr(driver.rfind("/") + 1);
9191

9292
return driver;

src/gpu_fdinfo.cpp

+88-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ void GPU_fdinfo::find_fd()
3030
}
3131
}
3232

33-
for (const auto& fd : fds_to_open)
33+
for (const auto& fd : fds_to_open) {
3434
fdinfo.push_back(std::ifstream(fd));
35+
36+
if (module == "xe")
37+
xe_fdinfo_last_cycles.push_back(0);
38+
}
3539
}
3640

3741
uint64_t GPU_fdinfo::get_gpu_time()
@@ -93,7 +97,7 @@ void GPU_fdinfo::find_intel_hwmon()
9397
return;
9498
}
9599

96-
hwmon += "/energy1_input";
100+
hwmon += module == "i915" ? "/energy1_input" : "/energy2_input";
97101

98102
if (!fs::exists(hwmon)) {
99103
SPDLOG_DEBUG("Intel hwmon: file {} doesn't exist.", hwmon);
@@ -141,17 +145,98 @@ float GPU_fdinfo::get_power_usage()
141145
return delta;
142146
}
143147

148+
std::pair<uint64_t, uint64_t> GPU_fdinfo::get_gpu_time_xe()
149+
{
150+
uint64_t total_cycles = 0, total_total_cycles = 0;
151+
152+
for (size_t i = 0; i < fdinfo.size(); i++) {
153+
fdinfo[i].clear();
154+
fdinfo[i].seekg(0);
155+
156+
uint64_t current_cycles = 0, current_total_cycles = 0;
157+
158+
for (std::string line; std::getline(fdinfo[i], line);) {
159+
if (line.find("drm-cycles-rcs") == std::string::npos &&
160+
line.find("drm-total-cycles-rcs") == std::string::npos
161+
)
162+
continue;
163+
164+
auto drm_type = line.substr(0, line.find(":"));
165+
166+
auto start = (drm_type + ": ").length();
167+
auto val = std::stoull(line.substr(start));
168+
169+
if (drm_type == "drm-cycles-rcs")
170+
current_cycles = val;
171+
else if (drm_type == "drm-total-cycles-rcs")
172+
current_total_cycles = val;
173+
174+
if (current_cycles > 0 && current_total_cycles > 0)
175+
break;
176+
}
177+
178+
if (current_cycles > 0 && current_cycles != xe_fdinfo_last_cycles[i] &&
179+
current_total_cycles > 0)
180+
{
181+
total_cycles += current_cycles;
182+
total_total_cycles += current_total_cycles;
183+
184+
xe_fdinfo_last_cycles[i] = current_cycles;
185+
}
186+
}
187+
188+
return { total_cycles, total_total_cycles };
189+
}
190+
191+
int GPU_fdinfo::get_xe_load()
192+
{
193+
static uint64_t previous_cycles, previous_total_cycles;
194+
195+
auto gpu_time = get_gpu_time_xe();
196+
uint64_t cycles = gpu_time.first;
197+
uint64_t total_cycles = gpu_time.second;
198+
199+
uint64_t delta_cycles = cycles - previous_cycles;
200+
uint64_t delta_total_cycles = total_cycles - previous_total_cycles;
201+
202+
if (delta_cycles == 0 || delta_total_cycles == 0)
203+
return 0;
204+
205+
double load = (double)delta_cycles / delta_total_cycles * 100;
206+
207+
previous_cycles = cycles;
208+
previous_total_cycles = total_cycles;
209+
210+
// SPDLOG_DEBUG("cycles = {}", cycles);
211+
// SPDLOG_DEBUG("total_cycles = {}", total_cycles);
212+
// SPDLOG_DEBUG("delta_cycles = {}", delta_cycles);
213+
// SPDLOG_DEBUG("delta_total_cycles = {}", delta_total_cycles);
214+
// SPDLOG_DEBUG("{} / {} * 100 = {}", delta_cycles, delta_total_cycles, load);
215+
// SPDLOG_DEBUG("load = {}\n", std::lround(load));
216+
217+
return std::lround(load);
218+
}
219+
144220
int GPU_fdinfo::get_gpu_load()
145221
{
146222
static uint64_t previous_gpu_time, previous_time;
147223

224+
if (module == "xe") {
225+
int result = get_xe_load();
226+
227+
if (result > 100)
228+
result = 100;
229+
230+
return result;
231+
}
232+
148233
uint64_t now = os_time_get_nano();
149234
uint64_t gpu_time_now = get_gpu_time();
150235

151236
float delta_time = now - previous_time;
152237
float delta_gpu_time = gpu_time_now - previous_gpu_time;
153238

154-
int result = std::lround(delta_gpu_time / delta_time * 100);
239+
int result = delta_gpu_time / delta_time * 100;
155240

156241
if (result > 100)
157242
result = 100;

src/gpu_fdinfo.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class GPU_fdinfo {
4242
int get_gpu_load();
4343
uint64_t get_gpu_time();
4444

45+
std::vector<uint64_t> xe_fdinfo_last_cycles;
46+
int get_xe_load();
47+
std::pair<uint64_t, uint64_t> get_gpu_time_xe();
48+
4549
float get_memory_used();
4650

4751
float get_current_power();
@@ -52,10 +56,14 @@ class GPU_fdinfo {
5256
: module(module)
5357
, pci_dev(pci_dev)
5458
{
59+
SPDLOG_DEBUG("GPU driver is \"{}\"", module);
60+
5561
if (module == "i915") {
5662
drm_engine_type = "drm-engine-render";
5763
drm_memory_type = "drm-total-local0";
58-
find_intel_hwmon();
64+
} else if (module == "xe") {
65+
drm_engine_type = "drm-total-cycles-rcs";
66+
drm_memory_type = "drm-total-vram0";
5967
} else if (module == "amdgpu") {
6068
drm_engine_type = "drm-engine-gfx";
6169
drm_memory_type = "drm-memory-vram";
@@ -64,6 +72,9 @@ class GPU_fdinfo {
6472
drm_engine_type = "drm-engine-gpu";
6573
}
6674

75+
if (module == "i915" || module == "xe")
76+
find_intel_hwmon();
77+
6778
find_fd();
6879

6980
std::thread thread(&GPU_fdinfo::main_thread, this);

0 commit comments

Comments
 (0)