Skip to content

Commit af0d80c

Browse files
17314642flightlessmango
authored andcommitted
gpu_fdinfo: divide logic in separate sections
1 parent 6c49103 commit af0d80c

File tree

2 files changed

+60
-37
lines changed

2 files changed

+60
-37
lines changed

src/gpu_fdinfo.cpp

+39-28
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ uint64_t GPU_fdinfo::get_gpu_time()
5454
return total_val;
5555
}
5656

57-
float GPU_fdinfo::get_vram_usage()
57+
float GPU_fdinfo::get_memory_used()
5858
{
5959
uint64_t total_val = 0;
6060

@@ -108,7 +108,7 @@ void GPU_fdinfo::find_intel_hwmon()
108108
SPDLOG_DEBUG("Intel hwmon: failed to open {}", hwmon);
109109
}
110110

111-
float GPU_fdinfo::get_power_usage()
111+
float GPU_fdinfo::get_current_power()
112112
{
113113
if (!energy_stream.is_open())
114114
return 0.f;
@@ -128,40 +128,51 @@ float GPU_fdinfo::get_power_usage()
128128
return (float)energy_input / 1'000'000;
129129
}
130130

131-
void GPU_fdinfo::get_load()
131+
float GPU_fdinfo::get_power_usage()
132132
{
133-
while (!stop_thread) {
134-
std::unique_lock<std::mutex> lock(metrics_mutex);
135-
cond_var.wait(lock, [this]() { return !paused || stop_thread; });
133+
static float last;
134+
float now = get_current_power();
136135

137-
static uint64_t previous_gpu_time, previous_time, now, gpu_time_now;
138-
static float power_usage_now, previous_power_usage;
136+
float delta = now - last;
137+
delta /= (float)METRICS_UPDATE_PERIOD_MS / 1000;
139138

140-
now = os_time_get_nano();
141-
gpu_time_now = get_gpu_time();
142-
power_usage_now = get_power_usage();
139+
last = now;
143140

144-
if (gpu_time_now > previous_gpu_time) {
145-
float time_since_last = now - previous_time;
146-
float gpu_since_last = gpu_time_now - previous_gpu_time;
147-
float power_usage_since_last =
148-
(power_usage_now - previous_power_usage) /
149-
((float)METRICS_UPDATE_PERIOD_MS / 1000);
141+
return delta;
142+
}
150143

151-
auto result = int((gpu_since_last / time_since_last) * 100);
152-
if (result > 100)
153-
result = 100;
144+
int GPU_fdinfo::get_gpu_load()
145+
{
146+
static uint64_t previous_gpu_time, previous_time;
154147

155-
metrics.load = result;
156-
metrics.memoryUsed = get_vram_usage();
157-
metrics.powerUsage = power_usage_since_last;
148+
uint64_t now = os_time_get_nano();
149+
uint64_t gpu_time_now = get_gpu_time();
158150

159-
previous_gpu_time = gpu_time_now;
160-
previous_power_usage = power_usage_now;
161-
}
151+
float delta_time = now - previous_time;
152+
float delta_gpu_time = gpu_time_now - previous_gpu_time;
153+
154+
int result = std::lround(delta_gpu_time / delta_time * 100);
155+
156+
if (result > 100)
157+
result = 100;
158+
159+
previous_gpu_time = gpu_time_now;
160+
previous_time = now;
161+
162+
return result;
163+
}
164+
165+
void GPU_fdinfo::main_thread()
166+
{
167+
while (!stop_thread) {
168+
std::unique_lock<std::mutex> lock(metrics_mutex);
169+
cond_var.wait(lock, [this]() { return !paused || stop_thread; });
170+
171+
metrics.load = get_gpu_load();
172+
metrics.memoryUsed = get_memory_used();
173+
metrics.powerUsage = get_power_usage();
162174

163175
std::this_thread::sleep_for(
164-
std::chrono::milliseconds(METRICS_UPDATE_PERIOD_MS)
165-
);
176+
std::chrono::milliseconds(METRICS_UPDATE_PERIOD_MS));
166177
}
167178
}

src/gpu_fdinfo.h

+21-9
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,36 @@
1515
class GPU_fdinfo {
1616
private:
1717
bool init = false;
18-
struct gpu_metrics metrics;
19-
std::vector<std::ifstream> fdinfo;
18+
2019
const std::string module;
2120
const std::string pci_dev;
22-
void find_fd();
23-
void find_intel_hwmon();
24-
std::ifstream energy_stream;
21+
2522
std::thread thread;
2623
std::condition_variable cond_var;
24+
2725
std::atomic<bool> stop_thread { false };
2826
std::atomic<bool> paused { false };
27+
28+
struct gpu_metrics metrics;
2929
mutable std::mutex metrics_mutex;
3030

31-
uint64_t get_gpu_time();
32-
void get_load();
31+
std::vector<std::ifstream> fdinfo;
32+
std::ifstream energy_stream;
33+
3334
std::string drm_engine_type = "EMPTY";
3435
std::string drm_memory_type = "EMPTY";
35-
float get_vram_usage();
36+
37+
void main_thread();
38+
39+
void find_fd();
40+
void find_intel_hwmon();
41+
42+
int get_gpu_load();
43+
uint64_t get_gpu_time();
44+
45+
float get_memory_used();
46+
47+
float get_current_power();
3648
float get_power_usage();
3749

3850
public:
@@ -54,7 +66,7 @@ class GPU_fdinfo {
5466

5567
find_fd();
5668

57-
std::thread thread(&GPU_fdinfo::get_load, this);
69+
std::thread thread(&GPU_fdinfo::main_thread, this);
5870
thread.detach();
5971
}
6072

0 commit comments

Comments
 (0)