Skip to content

Commit

Permalink
Add ROCm SMI backend for AMD GPU support
Browse files Browse the repository at this point in the history
  • Loading branch information
romner-set committed May 15, 2023
1 parent 917d568 commit c352bf2
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 79 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ override REQFLAGS := -std=c++20
WARNFLAGS := -Wall -Wextra -pedantic
OPTFLAGS := -O2 -ftree-vectorize -flto=$(LTO)
LDCXXFLAGS := -pthread -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS -D_FILE_OFFSET_BITS=64 $(GOODFLAGS) $(ADDFLAGS)
GPUCXXFLAGS := -I/opt/cuda/include # TODO: there has to be a better way to link NVML than hardcoded dirs
GPULDFLAGS := -L/usr/lib64 -lnvidia-ml
GPUCXXFLAGS := -I/opt/cuda/include -I/opt/rocm/include # TODO: there has to be a better way to link these libs than hardcoded dirs
GPULDFLAGS := -L/opt/cuda/lib -L/opt/rocm/lib -lnvidia-ml -lrocm_smi64
override CXXFLAGS += $(REQFLAGS) $(LDCXXFLAGS) $(OPTFLAGS) $(WARNFLAGS) $(GPUCXXFLAGS)
override LDFLAGS += $(LDCXXFLAGS) $(OPTFLAGS) $(WARNFLAGS) $(GPULDFLAGS)
INC := $(foreach incdir,$(INCDIRS),-isystem $(incdir)) -I$(SRCDIR)
Expand Down
3 changes: 2 additions & 1 deletion src/btop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ void clean_quit(int sig) {
}

Gpu::Nvml::shutdown();
Gpu::Rsmi::shutdown();

Config::write();

Expand Down Expand Up @@ -537,7 +538,7 @@ namespace Runner {
if (Global::debug) debug_timer("gpu", draw_begin);

//? Draw box
if (not pause_output and Gpu::Nvml::initialized) output += Gpu::draw(gpus, conf.force_redraw, conf.no_update);
if (not pause_output and not Gpu::gpu_names.empty()) output += Gpu::draw(gpus, conf.force_redraw, conf.no_update);

if (Global::debug) debug_timer("gpu", draw_done);
}
Expand Down
15 changes: 8 additions & 7 deletions src/btop_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ namespace Gpu {
string out;
out.reserve(width * height);

auto gpu = gpus[0]; // TODO: mutli-gpu support
auto gpu = gpus[0]; // TODO: multi-gpu support

//* Redraw elements not needed to be updated every cycle
if (redraw) {
Expand All @@ -781,7 +781,7 @@ namespace Gpu {

pwr_meter = Draw::Meter{b_width - 24, "cached"};

mem_util_graph = Draw::Graph{b_width/2 - 1, 2, "free", gpu.mem_utilization_percent, graph_symbol, 0, 0, 100, 4}; // offset so the graph isn't empty at 0-5% I/O
mem_util_graph = Draw::Graph{b_width/2 - 1, 2, "free", gpu.mem_utilization_percent, graph_symbol, 0, 0, 100, 4}; // offset so the graph isn't empty at 0-5% utilization
mem_used_graph = Draw::Graph{b_width/2 - 2, 4, "used", gpu.mem_used_percent, graph_symbol};
}

Expand Down Expand Up @@ -813,8 +813,9 @@ namespace Gpu {

//? Power usage meter, power state
out += Mv::to(b_y + 2, b_x + 1) + Theme::c("main_fg") + Fx::b + "PWR " + pwr_meter(gpu.pwr_percent.back())
+ Theme::g("cached").at(gpu.pwr_percent.back()) + rjust(to_string(gpu.pwr_usage/1000), 4) + Theme::c("main_fg") + 'W'
+ " P-state: " + (gpu.pwr_state > 9 ? "" : " ") + 'P' + Theme::g("cached").at(gpu.pwr_state) + to_string(gpu.pwr_state);
+ Theme::g("cached").at(gpu.pwr_percent.back()) + rjust(to_string(gpu.pwr_usage/1000), 4) + Theme::c("main_fg") + 'W';
if (gpu.pwr_state != 32) // NVML_PSTATE_UNKNOWN; unsupported or non-nvidia card
out += std::string(" P-state: ") + (gpu.pwr_state > 9 ? "" : " ") + 'P' + Theme::g("cached").at(gpu.pwr_state) + to_string(gpu.pwr_state);

//? Memory section header & clock speed
string used_memory_string = floating_humanizer(gpu.mem_used);
Expand All @@ -828,7 +829,7 @@ namespace Gpu {
}

//? Memory usage borders
out += Mv::to(b_y + 5, b_x) + Theme::c("div_line") + Symbols::div_left+Symbols::h_line + Theme::c("title") + "I/O:" + Theme::c("div_line") + Symbols::h_line*(b_width/2-6)
out += Mv::to(b_y + 5, b_x) + Theme::c("div_line") + Symbols::div_left+Symbols::h_line + Theme::c("title") + "Utilization:" + Theme::c("div_line") + Symbols::h_line*(b_width/2-14)
+ Symbols::div_right + Mv::u(1)+Mv::l(1) + Symbols::v_line + Mv::l(1)+Mv::d(2) + (Symbols::v_line + Mv::l(1)+Mv::d(1))*2;

//? Total memory usage
Expand Down Expand Up @@ -1743,7 +1744,7 @@ namespace Draw {
Cpu::redraw = Gpu::redraw = Mem::redraw = Net::redraw = Proc::redraw = true;

Cpu::shown = s_contains(boxes, "cpu");
Gpu::shown = s_contains(boxes, "gpu") and Gpu::Nvml::initialized;
Gpu::shown = s_contains(boxes, "gpu") and not Gpu::gpu_names.empty();
Mem::shown = s_contains(boxes, "mem");
Net::shown = s_contains(boxes, "net");
Proc::shown = s_contains(boxes, "proc");
Expand All @@ -1753,7 +1754,7 @@ namespace Draw {
using namespace Cpu;
bool show_temp = (Config::getB("check_temp") and got_sensors);
width = round((double)Term::width * width_p / 100);
if (Gpu::shown and !(Mem::shown or Net::shown or Proc::shown)) {
if (Gpu::shown and not (Mem::shown or Net::shown or Proc::shown)) {
height = Term::height/2;
} else {
height = max(8, (int)ceil((double)Term::height * (trim(boxes) == "cpu" ? 100 : height_p/(Gpu::shown+1) + Gpu::shown*5) / 100));
Expand Down
29 changes: 15 additions & 14 deletions src/btop_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,33 +330,34 @@ namespace Gpu {

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

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

deque<long long> temp = {};
long long temp_max = 100;
deque<long long> temp = {0};
long long temp_max = 110;

long long mem_total = 0;
long long mem_used = 0;
deque<long long> mem_used_percent = {};
long long mem_utilization = 0;
deque<long long> mem_utilization_percent = {};
unsigned int mem_clock_speed = 0; // MHz
deque<long long> mem_used_percent = {0};
deque<long long> mem_utilization_percent = {0}; // TODO: properly handle GPUs that can't report some stats
long long mem_clock_speed = 0; // MHz

unsigned int pcie_tx = 0; // KB/s
unsigned int pcie_rx = 0;
long long pcie_tx = 0; // KB/s
long long pcie_rx = 0;

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

namespace Nvml {
extern bool initialized;
extern bool shutdown();
}
namespace Rsmi {
extern bool shutdown();
}

Expand Down
2 changes: 1 addition & 1 deletion src/btop_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace Term {
bool mem = boxes.find("mem") != string::npos;
bool net = boxes.find("net") != string::npos;
bool proc = boxes.find("proc") != string::npos;
bool gpu = boxes.find("gpu") != string::npos and Gpu::Nvml::initialized;
bool gpu = boxes.find("gpu") != string::npos and not Gpu::gpu_names.empty();
int width = 0;
if (mem) width = Mem::min_width;
else if (net) width = Mem::min_width;
Expand Down
Loading

0 comments on commit c352bf2

Please sign in to comment.