@@ -16,6 +16,21 @@ std::string GPU_fdinfo::get_drm_engine_type() {
16
16
return drm_type;
17
17
}
18
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
+
19
34
void GPU_fdinfo::find_fd () {
20
35
#ifdef __linux__
21
36
DIR* dir = opendir (" /proc/self/fdinfo" );
@@ -70,24 +85,51 @@ uint64_t GPU_fdinfo::get_gpu_time() {
70
85
return total_val;
71
86
}
72
87
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
+
73
111
void GPU_fdinfo::get_load () {
74
112
while (!stop_thread) {
75
113
std::unique_lock<std::mutex> lock (metrics_mutex);
76
114
cond_var.wait (lock, [this ]() { return !paused || stop_thread; });
77
115
78
116
static uint64_t previous_gpu_time, previous_time, now, gpu_time_now;
117
+
79
118
gpu_time_now = get_gpu_time ();
80
119
now = os_time_get_nano ();
81
120
82
121
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 ) {
84
123
float time_since_last = now - previous_time;
85
124
float gpu_since_last = gpu_time_now - previous_gpu_time;
125
+
86
126
auto result = int ((gpu_since_last / time_since_last) * 100 );
87
127
if (result > 100 )
88
128
result = 100 ;
89
129
90
130
metrics.load = result;
131
+ metrics.memoryUsed = get_vram_usage ();
132
+
91
133
previous_gpu_time = gpu_time_now;
92
134
previous_time = now;
93
135
}
0 commit comments