@@ -32,50 +32,57 @@ void GPU_fdinfo::find_fd()
32
32
33
33
for (const auto & fd : fds_to_open) {
34
34
fdinfo.push_back (std::ifstream (fd));
35
+ fdinfo_data.push_back ({});
35
36
36
37
if (module == " xe" )
37
38
xe_fdinfo_last_cycles.push_back (0 );
38
39
}
39
40
}
40
41
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
+
41
55
uint64_t GPU_fdinfo::get_gpu_time ()
42
56
{
43
- uint64_t total_val = 0 ;
57
+ uint64_t total = 0 ;
44
58
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];
48
61
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 ;
52
64
53
- auto start = (drm_engine_type + " : " ).length ();
54
- total_val += std::stoull (line.substr (start));
55
- }
65
+ total += std::stoull (time );
56
66
}
57
67
58
- return total_val ;
68
+ return total ;
59
69
}
60
70
61
71
float GPU_fdinfo::get_memory_used ()
62
72
{
63
- uint64_t total_val = 0 ;
73
+ uint64_t total = 0 ;
64
74
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];
68
77
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 ;
72
80
73
- auto start = (drm_memory_type + " : " ).length ();
74
- total_val += std::stoull (line.substr (start));
75
- }
81
+ total += std::stoull (mem);
76
82
}
77
83
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 ;
79
86
}
80
87
81
88
void GPU_fdinfo::find_intel_hwmon ()
@@ -149,40 +156,30 @@ std::pair<uint64_t, uint64_t> GPU_fdinfo::get_gpu_time_xe()
149
156
{
150
157
uint64_t total_cycles = 0 , total_total_cycles = 0 ;
151
158
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++;
163
162
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" ];
165
165
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 ;
168
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;
169
+ auto cur_cycles = std::stoull (cur_cycles_str);
170
+ auto cur_total_cycles = std::stoull (cur_total_cycles_str);
173
171
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 ;
177
178
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;
183
181
184
- xe_fdinfo_last_cycles[i] = current_cycles;
185
- }
182
+ xe_fdinfo_last_cycles[idx] = cur_cycles;
186
183
}
187
184
188
185
return { total_cycles, total_total_cycles };
@@ -204,6 +201,9 @@ int GPU_fdinfo::get_xe_load()
204
201
205
202
double load = (double )delta_cycles / delta_total_cycles * 100 ;
206
203
204
+ if (load > 100 .f )
205
+ load = 100 .f ;
206
+
207
207
previous_cycles = cycles;
208
208
previous_total_cycles = total_cycles;
209
209
@@ -221,14 +221,8 @@ int GPU_fdinfo::get_gpu_load()
221
221
{
222
222
static uint64_t previous_gpu_time, previous_time;
223
223
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 ();
232
226
233
227
uint64_t now = os_time_get_nano ();
234
228
uint64_t gpu_time_now = get_gpu_time ();
@@ -253,6 +247,8 @@ void GPU_fdinfo::main_thread()
253
247
std::unique_lock<std::mutex> lock (metrics_mutex);
254
248
cond_var.wait (lock, [this ]() { return !paused || stop_thread; });
255
249
250
+ gather_fdinfo_data ();
251
+
256
252
metrics.load = get_gpu_load ();
257
253
metrics.memoryUsed = get_memory_used ();
258
254
metrics.powerUsage = get_power_usage ();
0 commit comments