Skip to content

Commit 26c0f0a

Browse files
17314642flightlessmango
authored andcommitted
gpu_fdinfo: replace c code with c++ and minor fixes
1 parent d9e312f commit 26c0f0a

File tree

2 files changed

+53
-89
lines changed

2 files changed

+53
-89
lines changed

src/gpu_fdinfo.cpp

+37-75
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,70 @@
11
#include "gpu_fdinfo.h"
22
namespace fs = ghc::filesystem;
33

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-
344
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;
3910
}
4011

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);
4316

44-
if (!file) continue;
17+
if (!file.is_open())
18+
continue;
4519

46-
char line[256];
4720
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)
5023
found_driver = true;
5124

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;
5728
}
5829
}
59-
60-
if (!found_driver)
61-
fclose(file);
6230
}
6331

64-
closedir(dir);
65-
#endif
32+
for (const auto& fd : fds_to_open)
33+
fdinfo.push_back(std::ifstream(fd));
6634
}
6735

6836
uint64_t GPU_fdinfo::get_gpu_time() {
69-
char line[256];
7037
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));
8249
}
8350
}
8451

8552
return total_val;
8653
}
8754

8855
float GPU_fdinfo::get_vram_usage() {
89-
char line[256];
9056
uint64_t total_val = 0;
9157

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);
9761

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;
10065

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));
10568
}
10669
}
10770

@@ -188,7 +151,6 @@ void GPU_fdinfo::get_load() {
188151
metrics.powerUsage = power_usage_since_last;
189152

190153
previous_gpu_time = gpu_time_now;
191-
previous_time = now;
192154
previous_power_usage = power_usage_now;
193155
}
194156

src/gpu_fdinfo.h

+16-14
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class GPU_fdinfo {
1616
private:
1717
bool init = false;
1818
struct gpu_metrics metrics;
19-
std::vector<FILE*> fdinfo;
20-
const char* module;
21-
const char* pci_dev;
19+
std::vector<std::ifstream> fdinfo;
20+
const std::string module;
21+
const std::string pci_dev;
2222
void find_fd();
2323
void find_intel_hwmon();
2424
std::ifstream energy_stream;
@@ -30,17 +30,26 @@ class GPU_fdinfo {
3030

3131
uint64_t get_gpu_time();
3232
void get_load();
33-
std::string get_drm_engine_type();
34-
std::string get_drm_memory_type();
33+
std::string drm_engine_type = "EMPTY";
34+
std::string drm_memory_type = "EMPTY";
3535
float get_vram_usage();
3636
float get_power_usage();
3737

3838
public:
39-
GPU_fdinfo(const char* module, const char* pci_dev) : module(module), pci_dev(pci_dev) {
39+
GPU_fdinfo(const std::string module, const std::string pci_dev) : module(module), pci_dev(pci_dev) {
4040
find_fd();
4141

42-
if (strstr(module, "i915"))
42+
if (module == "i915") {
43+
drm_engine_type = "drm-engine-render";
44+
drm_memory_type = "drm-total-local0";
4345
find_intel_hwmon();
46+
} else if (module == "amdgpu") {
47+
drm_engine_type = "drm-engine-gfx";
48+
drm_memory_type = "drm-memory-vram";
49+
} else if (module == "msm") {
50+
// msm driver does not report vram usage
51+
drm_engine_type = "drm-engine-gpu";
52+
}
4453

4554
std::thread thread(&GPU_fdinfo::get_load, this);
4655
thread.detach();
@@ -59,11 +68,4 @@ class GPU_fdinfo {
5968
paused = false;
6069
cond_var.notify_one();
6170
}
62-
63-
~GPU_fdinfo() {
64-
for (size_t i = 0; i < fdinfo.size(); i++) {
65-
fclose(fdinfo[i]);
66-
}
67-
fdinfo.clear();
68-
}
6971
};

0 commit comments

Comments
 (0)