@@ -30,8 +30,12 @@ void GPU_fdinfo::find_fd()
30
30
}
31
31
}
32
32
33
- for (const auto & fd : fds_to_open)
33
+ for (const auto & fd : fds_to_open) {
34
34
fdinfo.push_back (std::ifstream (fd));
35
+
36
+ if (module == " xe" )
37
+ xe_fdinfo_last_cycles.push_back (0 );
38
+ }
35
39
}
36
40
37
41
uint64_t GPU_fdinfo::get_gpu_time ()
@@ -93,7 +97,7 @@ void GPU_fdinfo::find_intel_hwmon()
93
97
return ;
94
98
}
95
99
96
- hwmon += " /energy1_input" ;
100
+ hwmon += module == " i915 " ? " /energy1_input" : " /energy2_input " ;
97
101
98
102
if (!fs::exists (hwmon)) {
99
103
SPDLOG_DEBUG (" Intel hwmon: file {} doesn't exist." , hwmon);
@@ -141,17 +145,98 @@ float GPU_fdinfo::get_power_usage()
141
145
return delta;
142
146
}
143
147
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
+
144
220
int GPU_fdinfo::get_gpu_load ()
145
221
{
146
222
static uint64_t previous_gpu_time, previous_time;
147
223
224
+ if (module == " xe" ) {
225
+ int result = get_xe_load ();
226
+
227
+ if (result > 100 )
228
+ result = 100 ;
229
+
230
+ return result;
231
+ }
232
+
148
233
uint64_t now = os_time_get_nano ();
149
234
uint64_t gpu_time_now = get_gpu_time ();
150
235
151
236
float delta_time = now - previous_time;
152
237
float delta_gpu_time = gpu_time_now - previous_gpu_time;
153
238
154
- int result = std::lround ( delta_gpu_time / delta_time * 100 ) ;
239
+ int result = delta_gpu_time / delta_time * 100 ;
155
240
156
241
if (result > 100 )
157
242
result = 100 ;
0 commit comments