-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathgpu_fdinfo.cpp
More file actions
774 lines (582 loc) · 20.3 KB
/
Copy pathgpu_fdinfo.cpp
File metadata and controls
774 lines (582 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#include "gpu_fdinfo.h"
#ifndef TEST_ONLY
#include "hud_elements.h"
#endif
#include <fstream>
namespace fs = ghc::filesystem;
void GPU_fdinfo::find_fd()
{
fdinfo.clear();
fdinfo_data.clear();
auto dir = std::string("/proc/") + std::to_string(pid) + "/fdinfo";
auto path = fs::path(dir);
SPDLOG_DEBUG("fdinfo_dir = {}", dir);
if (!fs::exists(path)) {
SPDLOG_DEBUG("{} does not exist", path.string());
return;
}
// Here we store client-ids, if ids match, we dont open this file,
// because it will have same readings and it becomes a duplicate
std::set<std::string> client_ids;
int total = 0;
for (const auto& entry : fs::directory_iterator(path)) {
auto fd_path = entry.path().string();
auto file = std::ifstream(fd_path);
if (!file.is_open())
continue;
std::string driver, pdev, client_id;
for (std::string line; std::getline(file, line);) {
size_t colon = line.find(":");
if (line[0] == ' ' || line[0] == '\t')
continue;
if (colon == std::string::npos || colon + 2 >= line.length())
continue;
auto key = line.substr(0, colon);
auto val = line.substr(key.length() + 2);
if (key == "drm-driver")
driver = val;
else if (key == "drm-pdev")
pdev = val;
else if (key == "drm-client-id")
client_id = val;
}
if (!driver.empty() && driver == module) {
total++;
SPDLOG_DEBUG(
"driver = \"{}\", pdev = \"{}\", "
"client_id = \"{}\", client_id_exists = \"{}\"",
driver, pdev,
client_id, client_ids.find(client_id) != client_ids.end()
);
}
if (
driver.empty() || client_id.empty() ||
driver != module || pdev != pci_dev ||
client_ids.find(client_id) != client_ids.end()
)
continue;
client_ids.insert(client_id);
open_fdinfo_fd(fd_path);
}
SPDLOG_DEBUG(
"Found {} total fds. Opened {} unique fds.",
total,
fdinfo.size()
);
}
void GPU_fdinfo::open_fdinfo_fd(std::string path) {
fdinfo.push_back(std::ifstream(path));
fdinfo_data.push_back({});
}
void GPU_fdinfo::gather_fdinfo_data() {
for (size_t i = 0; i < fdinfo.size(); i++) {
fdinfo[i].clear();
fdinfo[i].seekg(0);
for (std::string line; std::getline(fdinfo[i], line);) {
size_t colon = line.find(":");
if (line[0] == ' ' || line[0] == '\t')
continue;
if (colon == std::string::npos || colon + 2 >= line.length())
continue;
auto key = line.substr(0, line.find(":"));
auto val = line.substr(key.length() + 2);
fdinfo_data[i][key] = val;
}
}
}
uint64_t GPU_fdinfo::get_gpu_time()
{
uint64_t total = 0;
if (module == "panfrost")
return get_gpu_time_panfrost();
for (auto& fd : fdinfo_data) {
auto time = fd[drm_engine_type];
if (time.empty())
continue;
total += std::stoull(time);
}
return total;
}
uint64_t GPU_fdinfo::get_gpu_time_panfrost() {
uint64_t total = 0;
for (auto& fd : fdinfo_data) {
auto frag = fd["drm-engine-fragment"];
auto vert = fd["drm-engine-vertex-tiler"];
if (!frag.empty())
total += std::stoull(frag);
if (!vert.empty())
total += std::stoull(vert);
}
return total;
}
float GPU_fdinfo::get_memory_used()
{
uint64_t total = 0;
for (auto& fd : fdinfo_data) {
auto mem = fd[drm_memory_type];
if (mem.empty())
continue;
total += std::stoull(mem);
}
// TODO: sometimes it's not KB, so add a check for that.
return static_cast<float>(total) / 1024 / 1024;
}
void GPU_fdinfo::find_hwmon_sensors()
{
std::string hwmon;
if (module == "msm")
hwmon = find_hwmon_sensor_dir("gpu");
else if (module == "panfrost" || module == "panthor")
hwmon = find_hwmon_sensor_dir("gpu_thermal");
else
hwmon = find_hwmon_dir();
if (hwmon.empty()) {
SPDLOG_DEBUG("hwmon: failed to find hwmon directory");
return;
}
SPDLOG_DEBUG("hwmon: checking \"{}\" directory", hwmon);
for (const auto &entry : fs::directory_iterator(hwmon)) {
auto filename = entry.path().filename().string();
for (auto& hs : hwmon_sensors) {
auto key = hs.first;
auto sensor = &hs.second;
std::smatch matches;
if (
!std::regex_match(filename, matches, sensor->rx) ||
matches.size() != 2
)
continue;
auto cur_id = std::stoull(matches[1].str());
if (sensor->filename.empty() || cur_id < sensor->id) {
sensor->filename = entry.path().string();
sensor->id = cur_id;
}
}
}
for (auto& hs : hwmon_sensors) {
auto key = hs.first;
auto sensor = &hs.second;
if (sensor->filename.empty()) {
SPDLOG_DEBUG("hwmon: {} reading not found at {}", key, hwmon);
continue;
}
SPDLOG_DEBUG("hwmon: {} reading found at {}", key, sensor->filename);
sensor->stream.open(sensor->filename);
if (!sensor->stream.good()) {
SPDLOG_DEBUG(
"hwmon: failed to open {} reading {}",
key, sensor->filename
);
continue;
}
}
}
std::string GPU_fdinfo::find_hwmon_dir() {
std::string d = "/sys/class/drm/" + drm_node + "/device/hwmon";
if (!fs::exists(d)) {
SPDLOG_DEBUG("hwmon: hwmon directory \"{}\" doesn't exist", d);
return "";
}
auto dir_iterator = fs::directory_iterator(d);
auto hwmon = dir_iterator->path().string();
if (hwmon.empty()) {
SPDLOG_DEBUG("hwmon: hwmon directory \"{}\" is empty.", d);
return "";
}
return hwmon;
}
std::string GPU_fdinfo::find_hwmon_sensor_dir(std::string name) {
std::string d = "/sys/class/hwmon/";
if (!fs::exists(d))
return "";
for (const auto &entry : fs::directory_iterator(d)) {
auto hwmon_dir = entry.path().string();
auto hwmon_name = hwmon_dir + "/name";
std::ifstream name_stream(hwmon_name);
std::string name_content;
if (!name_stream.is_open())
continue;
std::getline(name_stream, name_content);
if (name_content.find(name) == std::string::npos)
continue;
// return the first gpu sensor
return hwmon_dir;
}
return "";
}
void GPU_fdinfo::get_current_hwmon_readings()
{
for (auto& hs : hwmon_sensors) {
auto key = hs.first;
auto sensor = &hs.second;
if (!sensor->stream.is_open())
continue;
sensor->stream.seekg(0);
std::stringstream ss;
ss << sensor->stream.rdbuf();
if (ss.str().empty())
continue;
sensor->val = std::stoull(ss.str());
}
}
float GPU_fdinfo::get_power_usage()
{
if (!hwmon_sensors["power"].filename.empty())
return static_cast<float>(hwmon_sensors["power"].val) / 1'000'000;
float now = hwmon_sensors["energy"].val;
// Initialize value for the first time, otherwise delta will be very large
// and your gpu power usage will be like 1 million watts for a second.
if (this->last_power == 0.f)
this->last_power = now;
float delta = now - this->last_power;
delta /= METRICS_UPDATE_PERIOD_MS / 1000.f;
this->last_power = now;
return delta / 1'000'000;
}
int GPU_fdinfo::get_xe_load()
{
double load = 0;
for (auto& fd : fdinfo_data) {
std::string client_id = fd["drm-client-id"];
std::string cur_cycles_str = fd["drm-cycles-rcs"];
std::string cur_total_cycles_str = fd["drm-total-cycles-rcs"];
if (
client_id.empty() || cur_cycles_str.empty() ||
cur_total_cycles_str.empty()
)
continue;
auto cur_cycles = std::stoull(cur_cycles_str);
auto cur_total_cycles = std::stoull(cur_total_cycles_str);
if (prev_xe_cycles.find(client_id) == prev_xe_cycles.end()) {
prev_xe_cycles[client_id] = { cur_cycles, cur_total_cycles };
continue;
}
auto prev_cycles = prev_xe_cycles[client_id].first;
auto prev_total_cycles = prev_xe_cycles[client_id].second;
auto delta_cycles = cur_cycles - prev_cycles;
auto delta_total_cycles = cur_total_cycles - prev_total_cycles;
prev_xe_cycles[client_id] = { cur_cycles, cur_total_cycles };
if (delta_cycles <= 0 || delta_total_cycles <= 0)
continue;
auto fd_load = static_cast<double>(delta_cycles) / delta_total_cycles * 100;
load += fd_load;
}
if (load > 100.f)
load = 100.f;
return std::lround(load);
}
int GPU_fdinfo::get_gpu_load()
{
if (module == "xe")
return get_xe_load();
else if (module == "msm_drm")
return get_kgsl_load();
uint64_t now = os_time_get_nano();
uint64_t gpu_time_now = get_gpu_time();
if (previous_time == 0) {
previous_gpu_time = gpu_time_now;
previous_time = now;
return 0;
}
float delta_time = now - previous_time;
float delta_gpu_time = gpu_time_now - previous_gpu_time;
int result = delta_gpu_time / delta_time * 100;
if (result > 100)
result = 100;
previous_gpu_time = gpu_time_now;
previous_time = now;
return std::round(result);
}
void GPU_fdinfo::find_i915_gt_dir()
{
std::string device = "/sys/bus/pci/devices/" + pci_dev + "/drm";
// Find first dir which starts with name "card"
for (const auto& entry : fs::directory_iterator(device)) {
auto path = entry.path().string();
if (path.substr(device.size() + 1, 4) == "card") {
device = path;
break;
}
}
auto gpu_clock_path = device + "/gt_act_freq_mhz";
gpu_clock_stream.open(gpu_clock_path);
if (!gpu_clock_stream.good())
SPDLOG_WARN("Intel i915 gt dir: failed to open {}", device);
// Assuming gt0 since all recent GPUs have the RCS engine on gt0,
// and latest GPUs need Xe anyway
auto throttle_folder = device + "/gt/gt0/throttle_";
auto throttle_status_path = throttle_folder + "reason_status";
throttle_status_stream.open(throttle_status_path);
if (!throttle_status_stream.good()) {
SPDLOG_WARN("Intel i915 gt dir: failed to open {}", throttle_status_path);
} else {
load_xe_i915_throttle_reasons(throttle_folder,
intel_throttle_power,
throttle_power_streams);
load_xe_i915_throttle_reasons(throttle_folder,
intel_throttle_current,
throttle_current_streams);
load_xe_i915_throttle_reasons(throttle_folder,
intel_throttle_temp,
throttle_temp_streams);
}
}
void GPU_fdinfo::find_xe_gt_dir()
{
std::string device = "/sys/bus/pci/devices/" + pci_dev + "/tile0";
if (!fs::exists(device)) {
SPDLOG_WARN(
"\"{}\" doesn't exist. GPU clock will be unavailable.",
device
);
return;
}
bool has_rcs = true;
// Check every "gt" dir if it has "engines/rcs" inside
for (const auto& entry : fs::directory_iterator(device)) {
auto path = entry.path().string();
if (path.substr(device.size() + 1, 2) != "gt")
continue;
SPDLOG_DEBUG("Checking \"{}\" for rcs.", path);
if (!fs::exists(path + "/engines/rcs")) {
SPDLOG_DEBUG("Skipping \"{}\" because rcs doesn't exist.", path);
continue;
}
SPDLOG_DEBUG("Found rcs in \"{}\"", path);
has_rcs = true;
device = path;
break;
}
if (!has_rcs) {
SPDLOG_WARN(
"rcs not found inside \"{}\". GPU clock will not be available.",
device
);
return;
}
auto gpu_clock_path = device + "/freq0/act_freq";
gpu_clock_stream.open(gpu_clock_path);
if (!gpu_clock_stream.good())
SPDLOG_WARN("Intel xe gt dir: failed to open {}", gpu_clock_path);
auto throttle_folder = device + "/freq0/throttle/";
auto throttle_status_path = throttle_folder + "status";
throttle_status_stream.open(throttle_status_path);
if (!throttle_status_stream.good()) {
SPDLOG_WARN("Intel xe gt dir: failed to open {}", throttle_status_path);
} else {
load_xe_i915_throttle_reasons(throttle_folder,
intel_throttle_power,
throttle_power_streams);
load_xe_i915_throttle_reasons(throttle_folder,
intel_throttle_current,
throttle_current_streams);
load_xe_i915_throttle_reasons(throttle_folder,
intel_throttle_temp,
throttle_temp_streams);
}
}
void GPU_fdinfo::load_xe_i915_throttle_reasons(
std::string throttle_folder,
std::vector<std::string> throttle_reasons,
std::vector<std::ifstream>& throttle_reason_streams
) {
for (const auto& throttle_reason : throttle_reasons) {
std::string throttle_path = throttle_folder + throttle_reason;
if (!fs::exists(throttle_path)) {
SPDLOG_WARN(
"Intel xe/i915 gt dir: Throttle file {} not found",
throttle_path
);
continue;
}
auto throttle_stream = std::ifstream(throttle_path);
if (!throttle_stream.good()) {
SPDLOG_WARN("Intel xe/i915 gt dir: failed to open {}", throttle_path);
continue;
}
throttle_reason_streams.push_back(std::move(throttle_stream));
}
}
int GPU_fdinfo::get_gpu_clock()
{
if (module == "panfrost")
return get_gpu_clock_panfrost();
if (module == "panthor")
return get_gpu_clock_panthor();
if (!gpu_clock_stream.is_open())
return 0;
std::string clock_str;
gpu_clock_stream.seekg(0);
std::getline(gpu_clock_stream, clock_str);
if (clock_str.empty())
return 0;
return std::stoi(clock_str);
}
int GPU_fdinfo::get_gpu_clock_panthor() {
if (fdinfo_data.empty())
return 0;
auto freq_str = fdinfo_data[0]["drm-curfreq-panthor"];
if (freq_str.empty())
return 0;
float freq = std::stoull(freq_str) / 1'000'000;
return std::round(freq);
}
int GPU_fdinfo::get_gpu_clock_panfrost() {
if (fdinfo_data.empty())
return 0;
auto freq_str = fdinfo_data[0]["drm-curfreq-fragment"];
if (freq_str.empty())
return 0;
float freq = std::stoull(freq_str) / 1'000'000;
return std::round(freq);
}
bool GPU_fdinfo::check_throttle_reasons(
std::vector<std::ifstream>& throttle_reason_streams)
{
for (auto& throttle_reason_stream : throttle_reason_streams) {
std::string throttle_reason_str;
throttle_reason_stream.seekg(0);
std::getline(throttle_reason_stream, throttle_reason_str);
if (throttle_reason_str == "1")
return true;
}
return false;
}
int GPU_fdinfo::get_throttling_status()
{
if (!throttle_status_stream.is_open())
return 0;
std::string throttle_status_str;
throttle_status_stream.seekg(0);
std::getline(throttle_status_stream, throttle_status_str);
if (throttle_status_str != "1")
return 0;
int reasons =
check_throttle_reasons(throttle_power_streams) * GPU_throttle_status::POWER +
check_throttle_reasons(throttle_current_streams) * GPU_throttle_status::CURRENT +
check_throttle_reasons(throttle_temp_streams) * GPU_throttle_status::TEMP;
// No throttle reasons for OTHER currently
if (reasons == 0)
reasons |= GPU_throttle_status::OTHER;
return reasons;
}
float GPU_fdinfo::amdgpu_helper_get_proc_vram() {
#ifndef TEST_ONLY
if (HUDElements.g_gamescopePid > 0 && HUDElements.g_gamescopePid != pid)
{
pid = HUDElements.g_gamescopePid;
find_fd();
}
#endif
// Recheck fds every 10secs, fixes Mass Effect 1, maybe some others too
{
auto t = os_time_get_nano() / 1'000'000;
if (t - fdinfo_last_update_ms >= 10'000) {
find_fd();
fdinfo_last_update_ms = t;
}
}
gather_fdinfo_data();
return get_memory_used();
}
void GPU_fdinfo::init_kgsl() {
const std::string sys_path = "/sys/class/kgsl/kgsl-3d0";
try {
if (!fs::exists(sys_path)) {
SPDLOG_WARN("kgsl: {} is not found. kgsl stats will not work!", sys_path);
return;
}
} catch (fs::filesystem_error& ex) {
SPDLOG_WARN("kgsl: {}", ex.what());
return;
}
for (std::string metric : {"gpu_busy_percentage", "temp", "clock_mhz" }) {
std::string p = sys_path + "/" + metric;
if (!fs::exists(p)) {
SPDLOG_WARN("kgsl: {} is not found", p);
continue;
}
SPDLOG_DEBUG("kgsl: {} found", p);
if (metric == "clock_mhz")
gpu_clock_stream.open(p);
else
kgsl_streams[metric].open(p);
}
}
int GPU_fdinfo::get_kgsl_load() {
std::ifstream* s = &kgsl_streams["gpu_busy_percentage"];
if (!s->is_open())
return 0;
std::string usage_str;
s->seekg(0);
std::getline(*s, usage_str);
if (usage_str.empty())
return 0;
return std::stoi(usage_str);
}
int GPU_fdinfo::get_kgsl_temp() {
std::ifstream* s = &kgsl_streams["temp"];
if (!s->is_open())
return 0;
std::string temp_str;
s->seekg(0);
std::getline(*s, temp_str);
if (temp_str.empty())
return 0;
return std::round(std::stoi(temp_str) / 1'000.f);
}
void GPU_fdinfo::main_thread()
{
while (!stop_thread) {
std::unique_lock<std::mutex> lock(metrics_mutex);
cond_var.wait(lock, [this]() { return !paused || stop_thread; });
#ifndef TEST_ONLY
if (HUDElements.g_gamescopePid > 0 && HUDElements.g_gamescopePid != pid)
{
pid = HUDElements.g_gamescopePid;
find_fd();
}
#endif
// Recheck fds every 10secs, fixes Mass Effect 1, maybe some others too
{
auto t = os_time_get_nano() / 1'000'000;
if (t - fdinfo_last_update_ms >= 10'000) {
find_fd();
fdinfo_last_update_ms = t;
}
}
gather_fdinfo_data();
get_current_hwmon_readings();
metrics.load = get_gpu_load();
metrics.proc_vram_used = get_memory_used();
metrics.powerUsage = get_power_usage();
metrics.powerLimit = static_cast<float>(hwmon_sensors["power_limit"].val) / 1'000'000;
metrics.CoreClock = get_gpu_clock();
metrics.voltage = hwmon_sensors["voltage"].val;
if (module == "msm_drm")
metrics.temp = get_kgsl_temp();
else
metrics.temp = hwmon_sensors["temp"].val / 1000.f;
metrics.memory_temp = hwmon_sensors["vram_temp"].val / 1000.f;
metrics.fan_speed = hwmon_sensors["fan_speed"].val;
metrics.fan_rpm = true; // Fan data is pulled from hwmon
int throttling = get_throttling_status();
metrics.is_power_throttled = throttling & GPU_throttle_status::POWER;
metrics.is_current_throttled = throttling & GPU_throttle_status::CURRENT;
metrics.is_temp_throttled = throttling & GPU_throttle_status::TEMP;
metrics.is_other_throttled = throttling & GPU_throttle_status::OTHER;
SPDLOG_DEBUG(
"pci_dev = {}, pid = {}, module = {}, "
"load = {}, proc_vram = {}, power = {}, "
"core = {}, temp = {}, fan = {}, "
"voltage = {}",
pci_dev, pid, module,
metrics.load, metrics.proc_vram_used, metrics.powerUsage,
metrics.CoreClock, metrics.temp, metrics.fan_speed,
metrics.voltage
);
std::this_thread::sleep_for(
std::chrono::milliseconds(METRICS_UPDATE_PERIOD_MS)
);
}
}