|
1 | 1 | #include "gpu_fdinfo.h"
|
2 | 2 | namespace fs = ghc::filesystem;
|
3 | 3 |
|
4 |
| -std::string GPU_fdinfo::get_drm_engine_type() { |
5 |
| - std::string drm_type = "drm-engine-"; |
6 |
| - |
7 |
| - if (strstr(module, "amdgpu")) |
8 |
| - drm_type += "gfx"; |
9 |
| - else if (strstr(module, "i915")) |
10 |
| - drm_type += "render"; |
11 |
| - else if (strstr(module, "msm")) |
12 |
| - drm_type += "gpu"; |
13 |
| - else |
14 |
| - drm_type += "none"; |
15 |
| - |
16 |
| - return drm_type; |
17 |
| -} |
18 |
| - |
19 |
| -std::string GPU_fdinfo::get_drm_memory_type() { |
20 |
| - std::string drm_type = "drm-"; |
21 |
| - |
22 |
| - // msm driver does not report vram usage |
23 |
| - |
24 |
| - if (strstr(module, "amdgpu")) |
25 |
| - drm_type += "memory-vram"; |
26 |
| - else if (strstr(module, "i915")) |
27 |
| - drm_type += "total-local0"; |
28 |
| - else |
29 |
| - drm_type += "memory-none"; |
30 |
| - |
31 |
| - return drm_type; |
32 |
| -} |
33 |
| - |
34 | 4 | void GPU_fdinfo::find_fd() {
|
35 |
| -#ifdef __linux__ |
36 |
| - DIR* dir = opendir("/proc/self/fdinfo"); |
37 |
| - if (!dir) { |
38 |
| - perror("Failed to open directory"); |
| 5 | + auto path = fs::path("/proc/self/fdinfo"); |
| 6 | + |
| 7 | + if (!fs::exists(path)) { |
| 8 | + SPDLOG_DEBUG("{} does not exist", path.string()); |
| 9 | + return; |
39 | 10 | }
|
40 | 11 |
|
41 |
| - for (const auto& entry : fs::directory_iterator("/proc/self/fdinfo")){ |
42 |
| - FILE* file = fopen(entry.path().string().c_str(), "r"); |
| 12 | + std::vector<std::string> fds_to_open; |
| 13 | + for (const auto& entry : fs::directory_iterator(path)) { |
| 14 | + auto fd_path = entry.path().string(); |
| 15 | + auto file = std::ifstream(fd_path); |
43 | 16 |
|
44 |
| - if (!file) continue; |
| 17 | + if (!file.is_open()) |
| 18 | + continue; |
45 | 19 |
|
46 |
| - char line[256]; |
47 | 20 | bool found_driver = false;
|
48 |
| - while (fgets(line, sizeof(line), file)) { |
49 |
| - if (strstr(line, module) != NULL) |
| 21 | + for (std::string line; std::getline(file, line);) { |
| 22 | + if (line.find(module) != std::string::npos) |
50 | 23 | found_driver = true;
|
51 | 24 |
|
52 |
| - if (found_driver) { |
53 |
| - if(strstr(line, get_drm_engine_type().c_str())) { |
54 |
| - fdinfo.push_back(file); |
55 |
| - break; |
56 |
| - } |
| 25 | + if (found_driver && line.find(drm_engine_type) != std::string::npos) { |
| 26 | + fds_to_open.push_back(fd_path); |
| 27 | + break; |
57 | 28 | }
|
58 | 29 | }
|
59 |
| - |
60 |
| - if (!found_driver) |
61 |
| - fclose(file); |
62 | 30 | }
|
63 | 31 |
|
64 |
| - closedir(dir); |
65 |
| -#endif |
| 32 | + for (const auto& fd : fds_to_open) |
| 33 | + fdinfo.push_back(std::ifstream(fd)); |
66 | 34 | }
|
67 | 35 |
|
68 | 36 | uint64_t GPU_fdinfo::get_gpu_time() {
|
69 |
| - char line[256]; |
70 | 37 | uint64_t total_val = 0;
|
71 |
| - for (auto fd : fdinfo) { |
72 |
| - rewind(fd); |
73 |
| - fflush(fd); |
74 |
| - uint64_t val = 0; |
75 |
| - while (fgets(line, sizeof(line), fd)){ |
76 |
| - std::string scan_str = get_drm_engine_type() + ": %" SCNu64 " ns"; |
77 |
| - |
78 |
| - if (sscanf(line, scan_str.c_str(), &val) == 1) { |
79 |
| - total_val += val; |
80 |
| - break; |
81 |
| - } |
| 38 | + |
| 39 | + for (auto& fd : fdinfo) { |
| 40 | + fd.clear(); |
| 41 | + fd.seekg(0); |
| 42 | + |
| 43 | + for (std::string line; std::getline(fd, line);) { |
| 44 | + if (line.find(drm_engine_type) == std::string::npos) |
| 45 | + continue; |
| 46 | + |
| 47 | + auto start = (drm_engine_type + ": ").length(); |
| 48 | + total_val += std::stoull(line.substr(start)); |
82 | 49 | }
|
83 | 50 | }
|
84 | 51 |
|
85 | 52 | return total_val;
|
86 | 53 | }
|
87 | 54 |
|
88 | 55 | float GPU_fdinfo::get_vram_usage() {
|
89 |
| - char line[256]; |
90 | 56 | uint64_t total_val = 0;
|
91 | 57 |
|
92 |
| - for (auto fd : fdinfo) { |
93 |
| - rewind(fd); |
94 |
| - fflush(fd); |
95 |
| - |
96 |
| - uint64_t val = 0; |
| 58 | + for (auto& fd : fdinfo) { |
| 59 | + fd.clear(); |
| 60 | + fd.seekg(0); |
97 | 61 |
|
98 |
| - while (fgets(line, sizeof(line), fd)) { |
99 |
| - std::string scan_str = get_drm_memory_type() + ": %llu KiB"; |
| 62 | + for (std::string line; std::getline(fd, line);) { |
| 63 | + if (line.find(drm_memory_type) == std::string::npos) |
| 64 | + continue; |
100 | 65 |
|
101 |
| - if (sscanf(line, scan_str.c_str(), &val) == 1) { |
102 |
| - total_val += val; |
103 |
| - break; |
104 |
| - } |
| 66 | + auto start = (drm_memory_type + ": ").length(); |
| 67 | + total_val += std::stoull(line.substr(start)); |
105 | 68 | }
|
106 | 69 | }
|
107 | 70 |
|
@@ -188,7 +151,6 @@ void GPU_fdinfo::get_load() {
|
188 | 151 | metrics.powerUsage = power_usage_since_last;
|
189 | 152 |
|
190 | 153 | previous_gpu_time = gpu_time_now;
|
191 |
| - previous_time = now; |
192 | 154 | previous_power_usage = power_usage_now;
|
193 | 155 | }
|
194 | 156 |
|
|
0 commit comments