Skip to content

Commit 4247170

Browse files
17314642flightlessmango
authored andcommitted
gpu_fdinfo: add vram usage
1 parent afaa7d1 commit 4247170

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/gpu_fdinfo.cpp

+43-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ std::string GPU_fdinfo::get_drm_engine_type() {
1616
return drm_type;
1717
}
1818

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+
1934
void GPU_fdinfo::find_fd() {
2035
#ifdef __linux__
2136
DIR* dir = opendir("/proc/self/fdinfo");
@@ -70,24 +85,51 @@ uint64_t GPU_fdinfo::get_gpu_time() {
7085
return total_val;
7186
}
7287

88+
float GPU_fdinfo::get_vram_usage() {
89+
char line[256];
90+
uint64_t total_val = 0;
91+
92+
for (auto fd : fdinfo) {
93+
rewind(fd);
94+
fflush(fd);
95+
96+
uint64_t val = 0;
97+
98+
while (fgets(line, sizeof(line), fd)) {
99+
std::string scan_str = get_drm_memory_type() + ": %llu KiB";
100+
101+
if (sscanf(line, scan_str.c_str(), &val) == 1) {
102+
total_val += val;
103+
break;
104+
}
105+
}
106+
}
107+
108+
return (float)total_val / 1024 / 1024;
109+
}
110+
73111
void GPU_fdinfo::get_load() {
74112
while (!stop_thread) {
75113
std::unique_lock<std::mutex> lock(metrics_mutex);
76114
cond_var.wait(lock, [this]() { return !paused || stop_thread; });
77115

78116
static uint64_t previous_gpu_time, previous_time, now, gpu_time_now;
117+
79118
gpu_time_now = get_gpu_time();
80119
now = os_time_get_nano();
81120

82121
if (gpu_time_now > previous_gpu_time &&
83-
now - previous_time > METRICS_UPDATE_PERIOD_MS * 1'000'000){
122+
now - previous_time > METRICS_UPDATE_PERIOD_MS * 1'000'000) {
84123
float time_since_last = now - previous_time;
85124
float gpu_since_last = gpu_time_now - previous_gpu_time;
125+
86126
auto result = int((gpu_since_last / time_since_last) * 100);
87127
if (result > 100)
88128
result = 100;
89129

90130
metrics.load = result;
131+
metrics.memoryUsed = get_vram_usage();
132+
91133
previous_gpu_time = gpu_time_now;
92134
previous_time = now;
93135
}

src/gpu_fdinfo.h

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class GPU_fdinfo {
2828
uint64_t get_gpu_time();
2929
void get_load();
3030
std::string get_drm_engine_type();
31+
std::string get_drm_memory_type();
32+
float get_vram_usage();
3133

3234
public:
3335
GPU_fdinfo(const char* module) : module(module) {

0 commit comments

Comments
 (0)