Skip to content

Commit 975815c

Browse files
17314642flightlessmango
authored andcommitted
gpu_fdinfo: remove duplicate fdinfo enumeration
1 parent ae4c411 commit 975815c

File tree

2 files changed

+58
-58
lines changed

2 files changed

+58
-58
lines changed

src/gpu_fdinfo.cpp

+54-58
Original file line numberDiff line numberDiff line change
@@ -32,50 +32,57 @@ void GPU_fdinfo::find_fd()
3232

3333
for (const auto& fd : fds_to_open) {
3434
fdinfo.push_back(std::ifstream(fd));
35+
fdinfo_data.push_back({});
3536

3637
if (module == "xe")
3738
xe_fdinfo_last_cycles.push_back(0);
3839
}
3940
}
4041

42+
void GPU_fdinfo::gather_fdinfo_data() {
43+
for (size_t i = 0; i < fdinfo.size(); i++) {
44+
fdinfo[i].clear();
45+
fdinfo[i].seekg(0);
46+
47+
for (std::string line; std::getline(fdinfo[i], line);) {
48+
auto key = line.substr(0, line.find(":"));
49+
auto val = line.substr(key.length() + 2);
50+
fdinfo_data[i][key] = val;
51+
}
52+
}
53+
}
54+
4155
uint64_t GPU_fdinfo::get_gpu_time()
4256
{
43-
uint64_t total_val = 0;
57+
uint64_t total = 0;
4458

45-
for (auto& fd : fdinfo) {
46-
fd.clear();
47-
fd.seekg(0);
59+
for (auto& fd : fdinfo_data) {
60+
auto time = fd[drm_engine_type];
4861

49-
for (std::string line; std::getline(fd, line);) {
50-
if (line.find(drm_engine_type) == std::string::npos)
51-
continue;
62+
if (time.empty())
63+
continue;
5264

53-
auto start = (drm_engine_type + ": ").length();
54-
total_val += std::stoull(line.substr(start));
55-
}
65+
total += std::stoull(time);
5666
}
5767

58-
return total_val;
68+
return total;
5969
}
6070

6171
float GPU_fdinfo::get_memory_used()
6272
{
63-
uint64_t total_val = 0;
73+
uint64_t total = 0;
6474

65-
for (auto& fd : fdinfo) {
66-
fd.clear();
67-
fd.seekg(0);
75+
for (auto& fd : fdinfo_data) {
76+
auto mem = fd[drm_memory_type];
6877

69-
for (std::string line; std::getline(fd, line);) {
70-
if (line.find(drm_memory_type) == std::string::npos)
71-
continue;
78+
if (mem.empty())
79+
continue;
7280

73-
auto start = (drm_memory_type + ": ").length();
74-
total_val += std::stoull(line.substr(start));
75-
}
81+
total += std::stoull(mem);
7682
}
7783

78-
return (float)total_val / 1024 / 1024;
84+
// TODO: sometimes it's not KB, so add a check for that.
85+
return (float)total / 1024 / 1024;
7986
}
8087

8188
void GPU_fdinfo::find_intel_hwmon()
@@ -149,40 +156,30 @@ std::pair<uint64_t, uint64_t> GPU_fdinfo::get_gpu_time_xe()
149156
{
150157
uint64_t total_cycles = 0, total_total_cycles = 0;
151158

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;
159+
size_t idx = -1;
160+
for (auto& fd : fdinfo_data) {
161+
idx++;
163162

164-
auto drm_type = line.substr(0, line.find(":"));
163+
auto cur_cycles_str = fd["drm-cycles-rcs"];
164+
auto cur_total_cycles_str = fd["drm-total-cycles-rcs"];
165165

166-
auto start = (drm_type + ": ").length();
167-
auto val = std::stoull(line.substr(start));
166+
if (cur_cycles_str.empty() || cur_total_cycles_str.empty())
167+
continue;
168168

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;
169+
auto cur_cycles = std::stoull(cur_cycles_str);
170+
auto cur_total_cycles = std::stoull(cur_total_cycles_str);
173171

174-
if (current_cycles > 0 && current_total_cycles > 0)
175-
break;
176-
}
172+
if (
173+
cur_cycles <= 0 ||
174+
cur_cycles == xe_fdinfo_last_cycles[idx] ||
175+
cur_total_cycles <= 0
176+
)
177+
continue;
177178

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;
179+
total_cycles += cur_cycles;
180+
total_total_cycles += cur_total_cycles;
183181

184-
xe_fdinfo_last_cycles[i] = current_cycles;
185-
}
182+
xe_fdinfo_last_cycles[idx] = cur_cycles;
186183
}
187184

188185
return { total_cycles, total_total_cycles };
@@ -204,6 +201,9 @@ int GPU_fdinfo::get_xe_load()
204201

205202
double load = (double)delta_cycles / delta_total_cycles * 100;
206203

204+
if (load > 100.f)
205+
load = 100.f;
206+
207207
previous_cycles = cycles;
208208
previous_total_cycles = total_cycles;
209209

@@ -221,14 +221,8 @@ int GPU_fdinfo::get_gpu_load()
221221
{
222222
static uint64_t previous_gpu_time, previous_time;
223223

224-
if (module == "xe") {
225-
int result = get_xe_load();
226-
227-
if (result > 100)
228-
result = 100;
229-
230-
return result;
231-
}
224+
if (module == "xe")
225+
return get_xe_load();
232226

233227
uint64_t now = os_time_get_nano();
234228
uint64_t gpu_time_now = get_gpu_time();
@@ -253,6 +247,8 @@ void GPU_fdinfo::main_thread()
253247
std::unique_lock<std::mutex> lock(metrics_mutex);
254248
cond_var.wait(lock, [this]() { return !paused || stop_thread; });
255249

250+
gather_fdinfo_data();
251+
256252
metrics.load = get_gpu_load();
257253
metrics.memoryUsed = get_memory_used();
258254
metrics.powerUsage = get_power_usage();

src/gpu_fdinfo.h

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "gpu_metrics_util.h"
1212
#include <atomic>
1313
#include <spdlog/spdlog.h>
14+
#include <map>
1415

1516
class GPU_fdinfo {
1617
private:
@@ -34,6 +35,9 @@ class GPU_fdinfo {
3435
std::string drm_engine_type = "EMPTY";
3536
std::string drm_memory_type = "EMPTY";
3637

38+
std::vector<std::map<std::string, std::string>> fdinfo_data;
39+
void gather_fdinfo_data();
40+
3741
void main_thread();
3842

3943
void find_fd();

0 commit comments

Comments
 (0)