Skip to content

Commit

Permalink
Handle GPUs which cannot report certain stats in btop_collect.cpp and…
Browse files Browse the repository at this point in the history
… CPU panel
  • Loading branch information
romner-set committed May 21, 2023
1 parent 005de97 commit 414d7eb
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 172 deletions.
8 changes: 3 additions & 5 deletions src/btop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,17 +507,15 @@ namespace Runner {
//* Run collection and draw functions for all boxes
try {
//? GPU data collection
vector<Gpu::gpu_info> gpus;
bool gpu_in_cpu_panel =
Config::getS("cpu_graph_lower") == "default"
or Config::getS("cpu_graph_lower").rfind("gpu-", 0) == 0
or Config::getS("cpu_graph_upper").rfind("gpu-", 0) == 0;
bool gpu_in_cpu_panel = Config::getS("cpu_graph_lower").rfind("gpu-", 0) == 0
or Config::getS("cpu_graph_upper").rfind("gpu-", 0) == 0;

vector<unsigned int> gpu_panels = {};
for (auto& box : conf.boxes)
if (box.rfind("gpu", 0) == 0)
gpu_panels.push_back(box.back()-'0');

vector<Gpu::gpu_info> gpus;
if (gpu_in_cpu_panel or not gpu_panels.empty()) {
if (Global::debug) debug_timer("gpu", collect_begin);
gpus = Gpu::collect(conf.no_update);
Expand Down
77 changes: 52 additions & 25 deletions src/btop_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ namespace Cpu {
long unsigned int lavg_str_len = 0;
int graph_up_height, graph_low_height;
int graph_up_width, graph_low_width;
int gpu_meter_width;
bool shown = true, redraw = true, mid_line = false;
string box;
vector<Draw::Graph> graphs_upper;
Expand Down Expand Up @@ -566,16 +567,25 @@ namespace Cpu {
auto& gpu = gpus[i]; auto& graph = graphs[i];

//? GPU graphs/meters
gpu_temp_graphs[i] = Draw::Graph{ 5, 1, "temp", gpu.temp, graph_symbol, false, false, gpu.temp_max, -23 };
gpu_mem_graphs[i] = Draw::Graph{ 5, 1, "used", gpu.mem_used_percent, graph_symbol };
gpu_meters[i] = Draw::Meter{ b_width - 12 - (int)floating_humanizer(gpu.mem_total, true).size() - (show_temps ? 24 : 12) - (int)to_string(i).size(), "cpu" };
if (++i < gpus.size())
graph = Draw::Graph{graph_width, graph_height, "cpu", gpu.gpu_percent, graph_symbol, invert, true};
else {
graph = Draw::Graph{
graph_width + graph_default_width%graph_width - (int)gpus.size() + 1,
graph_height, "cpu", gpu.gpu_percent, graph_symbol, invert, true
};
if (gpu.supported_functions.temp_info)
gpu_temp_graphs[i] = Draw::Graph{ 5, 1, "temp", gpu.temp, graph_symbol, false, false, gpu.temp_max, -23 };
if (gpu.supported_functions.mem_used and gpu.supported_functions.mem_total)
gpu_mem_graphs[i] = Draw::Graph{ 5, 1, "used", gpu.mem_used_percent, graph_symbol };
if (gpu.supported_functions.gpu_utilization) {
gpu_meter_width = b_width - 12 - (int)floating_humanizer(gpu.mem_total, true).size() - (show_temps ? 24 : 12) - (int)to_string(i).size() + (gpus.size() == 1)*2 - (gpus.size() > 9 and i <= 9);
gpu_meters[i] = Draw::Meter{gpu_meter_width, "cpu" };
}

bool utilization_support = gpu.supported_functions.gpu_utilization;
if (++i < gpus.size()) {
if (utilization_support)
graph = Draw::Graph{graph_width, graph_height, "cpu", gpu.gpu_percent, graph_symbol, invert, true};
} else {
if (utilization_support)
graph = Draw::Graph{
graph_width + graph_default_width%graph_width - (int)gpus.size() + 1,
graph_height, "cpu", gpu.gpu_percent, graph_symbol, invert, true
};
break;
}
}
Expand All @@ -587,9 +597,14 @@ namespace Cpu {
gpu_mem_graphs.resize(gpus.size());
gpu_meters.resize(gpus.size());
for (unsigned long i = 0; i < gpus.size(); ++i) {
gpu_temp_graphs[i] = Draw::Graph{ 5, 1, "temp", gpus[i].temp, graph_symbol, false, false, gpus[i].temp_max, -23 };
gpu_mem_graphs[i] = Draw::Graph{ 5, 1, "used", gpus[i].mem_used_percent, graph_symbol };
gpu_meters[i] = Draw::Meter{ b_width - 12 - (int)floating_humanizer(gpus[i].mem_total, true).size() - (show_temps ? 24 : 12) - (int)to_string(i).size(), "cpu" };
if (gpus[i].supported_functions.temp_info)
gpu_temp_graphs[i] = Draw::Graph{ 5, 1, "temp", gpus[i].temp, graph_symbol, false, false, gpus[i].temp_max, -23 };
if (gpus[i].supported_functions.mem_used and gpus[i].supported_functions.mem_total)
gpu_mem_graphs[i] = Draw::Graph{ 5, 1, "used", gpus[i].mem_used_percent, graph_symbol };
if (gpus[i].supported_functions.gpu_utilization) {
gpu_meter_width = b_width - 12 - (int)floating_humanizer(gpus[i].mem_total, true).size() - (show_temps ? 24 : 12) - (int)to_string(i).size() + (gpus.size() == 1)*2 - (gpus.size() > 9 and i <= 9);
gpu_meters[i] = Draw::Meter{gpu_meter_width, "cpu" };
}
}
} else {
graphs.resize(1);
Expand Down Expand Up @@ -675,12 +690,15 @@ namespace Cpu {
auto draw_graphs = [&](vector<Draw::Graph>& graphs, const int graph_height, const int graph_width, const string& graph_field) {
if (graph_field == "gpu-totals")
for (unsigned long i = 0;;) {
out += graphs[i](gpus[i].gpu_percent, (data_same or redraw));
if (gpus.size() > 1) {
auto i_str = to_string(i);
out += Mv::l(graph_width-1) + Mv::u(graph_height/2) + (graph_width > 5 ? "GPU " : "") + i_str
+ Mv::d(graph_height/2) + Mv::r(graph_width - 1 - (graph_width > 5)*4 - i_str.size());
}
if (gpus[i].supported_functions.gpu_utilization) {
out += graphs[i](gpus[i].gpu_percent, (data_same or redraw));
if (gpus.size() > 1) {
auto i_str = to_string(i);
out += Mv::l(graph_width-1) + Mv::u(graph_height/2) + (graph_width > 5 ? "GPU " : "") + i_str
+ Mv::d(graph_height/2) + Mv::r(graph_width - 1 - (graph_width > 5)*4 - i_str.size());
}
} else out += Mv::d(graph_height/2) + Mv::r(graph_width/2 - 6) + "UNSUPPORTED" + Mv::r(graph_width/2 - 5);

if (++i < graphs.size())
out += Theme::c("div_line") + (Symbols::v_line + Mv::l(1) + Mv::u(1))*graph_height + Mv::r(1) + Mv::d(1);
else break;
Expand Down Expand Up @@ -790,12 +808,21 @@ namespace Cpu {
for (unsigned long i = 0; i < gpus.size(); ++i) {
if (not v_contains(Gpu::shown_panels, i)) {
out += Mv::to(b_y + b_height - 1 - gpus.size() + ++shown_panels_count - (Gpu::shown == 0), b_x + 1)
+ Theme::c("main_fg") + Fx::b + "GPU " + to_string(i) + ' ' + gpu_meters[i](gpus[i].gpu_percent.back())
+ Theme::g("cpu").at(gpus[i].gpu_percent.back()) + rjust(to_string(gpus[i].gpu_percent.back()), 4) + Theme::c("main_fg") + '%';
out += ' ' + Theme::c("inactive_fg") + graph_bg * 6 + Mv::l(6) + Theme::g("used").at(gpus[i].mem_used_percent.back())
+ gpu_mem_graphs[i](gpus[i].mem_used_percent, data_same or redraw) + Theme::c("main_fg")
+ rjust(floating_humanizer(gpus[i].mem_used, true), 5) + Theme::c("inactive_fg") + '/' + Theme::c("main_fg") + floating_humanizer(gpus[i].mem_total, true);
if (show_temps) {
+ Theme::c("main_fg") + Fx::b + "GPU " + (gpus.size() > 1 ? rjust(to_string(i) + ' ', 1 + gpus.size() > 9) : "");
if (gpus[i].supported_functions.gpu_utilization)
out += gpu_meters[i](gpus[i].gpu_percent.back())
+ Theme::g("cpu").at(gpus[i].gpu_percent.back()) + rjust(to_string(gpus[i].gpu_percent.back()), 4) + Theme::c("main_fg") + '%';
else out += Mv::r(gpu_meter_width);

if (gpus[i].supported_functions.mem_used) {
out += ' ' + Theme::c("inactive_fg") + graph_bg * 6 + Mv::l(6) + Theme::g("used").at(gpus[i].mem_used_percent.back())
+ gpu_mem_graphs[i](gpus[i].mem_used_percent, data_same or redraw) + Theme::c("main_fg")
+ rjust(floating_humanizer(gpus[i].mem_used, true), 5);
if (gpus[i].supported_functions.mem_total)
out += Theme::c("inactive_fg") + '/' + Theme::c("main_fg") + floating_humanizer(gpus[i].mem_total, true);
else out += Mv::r(5);
} else out += Mv::r(17);
if (show_temps and gpus[i].supported_functions.temp_info) {
const auto [temp, unit] = celsius_to(gpus[i].temp.back(), temp_scale);
out += ' ' + Theme::c("inactive_fg") + graph_bg * 6 + Mv::l(6) + Theme::g("temp").at(clamp(gpus[i].temp.back() * 100 / gpus[i].temp_max, 0ll, 100ll))
+ gpu_temp_graphs[i](gpus[i].temp, data_same or redraw)
Expand Down
26 changes: 21 additions & 5 deletions src/btop_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,29 @@ namespace Gpu {
unsigned long long mem;
};*/

//* Container for supported Gpu::*::collect() functions
struct gpu_info_supported {
bool gpu_utilization = true,
mem_utilization = true,
gpu_clock = true,
mem_clock = true,
pwr_usage = true,
pwr_state = true,
temp_info = true,
mem_total = true,
mem_used = true,
pcie_txrx = true;
};

//* Per-device container for GPU info
struct gpu_info {
deque<long long> gpu_percent = {0};
unsigned int gpu_clock_speed = 0; // MHz
deque<long long> gpu_percent = {};
unsigned int gpu_clock_speed; // MHz

deque<long long> pwr_percent = {0};
long long pwr_usage = 0; // mW
deque<long long> pwr_percent = {};
long long pwr_usage; // mW
long long pwr_max_usage = 255000;
long long pwr_state = 32;
long long pwr_state;

deque<long long> temp = {0};
long long temp_max = 110;
Expand All @@ -126,6 +140,8 @@ namespace Gpu {
long long pcie_tx = 0; // KB/s
long long pcie_rx = 0;

gpu_info_supported supported_functions;

// vector<proc_info> graphics_processes = {}; // TODO
// vector<proc_info> compute_processes = {};
};
Expand Down
Loading

0 comments on commit 414d7eb

Please sign in to comment.