Skip to content

Commit

Permalink
Added definition GPU_SUPPORT to toggle GPU related code
Browse files Browse the repository at this point in the history
  • Loading branch information
aristocratos committed Nov 25, 2023
1 parent 975525d commit b877726
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 34 deletions.
14 changes: 13 additions & 1 deletion Makefile
Expand Up @@ -36,6 +36,17 @@ endif

override PLATFORM_LC := $(shell echo $(PLATFORM) | tr '[:upper:]' '[:lower:]')

#? GPU Support
ifeq ($(PLATFORM_LC)$(ARCH),linuxx86_64)
GPU_SUPPORT := true
else
GPU_SUPPORT := false
endif

ifeq ($(GPU_SUPPORT),true)
override ADDFLAGS += -DGPU_SUPPORT
endif

#? Compiler and Linker
ifeq ($(shell $(CXX) --version | grep clang >/dev/null 2>&1; echo $$?),0)
override CXX_IS_CLANG := true
Expand Down Expand Up @@ -218,6 +229,7 @@ info:
@printf " $(BANNER)\n"
@printf "\033[1;92mPLATFORM \033[1;93m?| \033[0m$(PLATFORM)\n"
@printf "\033[1;96mARCH \033[1;93m?| \033[0m$(ARCH)\n"
@printf "\033[1;95mGPU_SUPPORT \033[1;94m:| \033[0m$(GPU_SUPPORT)\n"
@printf "\033[1;93mCXX \033[1;93m?| \033[0m$(CXX) \033[1;93m(\033[97m$(CXX_VERSION)\033[93m)\n"
@printf "\033[1;94mTHREADS \033[1;94m:| \033[0m$(THREADS)\n"
@printf "\033[1;92mREQFLAGS \033[1;91m!| \033[0m$(REQFLAGS)\n"
Expand Down Expand Up @@ -310,7 +322,7 @@ uninstall:
-include $(OBJECTS:.$(OBJEXT)=.$(DEPEXT))

#? Compile rocm_smi
ifeq ($(RSMI_STATIC)$(PLATFORM_LC),truelinux)
ifeq ($(GPU_SUPPORT)$(RSMI_STATIC),truetrue)
.ONESHELL:
rocm_smi:
@printf "\n\033[1;92mBuilding ROCm SMI static library\033[37m...\033[0m\n"
Expand Down
31 changes: 24 additions & 7 deletions src/btop.cpp
Expand Up @@ -183,8 +183,11 @@ void term_resize(bool force) {
if (force and refreshed) force = false;
}
else return;

#ifdef GPU_SUPPORT
static const array<string, 10> all_boxes = {"gpu5", "cpu", "mem", "net", "proc", "gpu0", "gpu1", "gpu2", "gpu3", "gpu4"};
#else
static const array<string, 5> all_boxes = {"", "cpu", "mem", "net", "proc"};
#endif
Global::resized = true;
if (Runner::active) Runner::stop();
Term::refresh();
Expand Down Expand Up @@ -224,8 +227,12 @@ void term_resize(bool force) {
clean_quit(0);
else if (key.size() == 1 and isint(key)) {
auto intKey = stoi(key);
auto box = all_boxes.at(intKey);
if (not box.starts_with("gpu") or (intKey == 0 and Gpu::gpu_names.size() >= 5) or (intKey >= 5 and std::cmp_greater_equal(Gpu::gpu_names.size(), intKey - 4))) {
#ifdef GPU_SUPPORT
if ((intKey == 0 and Gpu::gpu_names.size() >= 5) or (intKey >= 5 and std::cmp_greater_equal(Gpu::gpu_names.size(), intKey - 4))) {
#else
if (intKey > 0 and intKey < 5) {
#endif
auto box = all_boxes.at(intKey);
Config::current_preset = -1;
Config::toggle_box(box);
boxes = Config::getS("shown_boxes");
Expand Down Expand Up @@ -262,7 +269,7 @@ void clean_quit(int sig) {
#endif
}

#ifdef __linux__
#ifdef GPU_SUPPORT
Gpu::Nvml::shutdown();
Gpu::Rsmi::shutdown();
#endif
Expand Down Expand Up @@ -499,7 +506,13 @@ namespace Runner {
//! DEBUG stats
if (Global::debug) {
if (debug_bg.empty() or redraw)
Runner::debug_bg = Draw::createBox(2, 2, 33, 9, "", true, "μs");
Runner::debug_bg = Draw::createBox(2, 2, 33,
#ifdef GPU_SUPPORT
9,
#else
8,
#endif
"", true, "μs");

debug_times.clear();
debug_times["total"] = {0, 0};
Expand All @@ -509,7 +522,7 @@ namespace Runner {

//* Run collection and draw functions for all boxes
try {
#ifdef __linux__
#ifdef GPU_SUPPORT
//? GPU data collection
const bool gpu_in_cpu_panel = Gpu::gpu_names.size() > 0 and (
Config::getS("cpu_graph_lower").starts_with("gpu-") or Config::getS("cpu_graph_upper").starts_with("gpu-")
Expand Down Expand Up @@ -559,7 +572,7 @@ namespace Runner {
throw std::runtime_error("Cpu:: -> " + string{e.what()});
}
}
#ifdef __linux__
#ifdef GPU_SUPPORT
//? GPU
if (not gpu_panels.empty() and not gpus_ref.empty()) {
try {
Expand Down Expand Up @@ -693,7 +706,11 @@ namespace Runner {
"post"_a = Theme::c("main_fg") + Fx::ub
);
static auto loc = std::locale(std::locale::classic(), new MyNumPunct);
#ifdef GPU_SUPPORT
for (const string name : {"cpu", "mem", "net", "proc", "gpu", "total"}) {
#else
for (const string name : {"cpu", "mem", "net", "proc", "total"}) {
#endif
if (not debug_times.contains(name)) debug_times[name] = {0,0};
const auto& [time_collect, time_draw] = debug_times.at(name);
if (name == "total") output += Fx::b;
Expand Down
18 changes: 14 additions & 4 deletions src/btop_config.cpp
Expand Up @@ -73,9 +73,9 @@ namespace Config {
"#* Note that \"tty\" only has half the horizontal resolution of the other two, so will show a shorter historical view."},

{"graph_symbol_cpu", "# Graph symbol to use for graphs in cpu box, \"default\", \"braille\", \"block\" or \"tty\"."},

#ifdef GPU_SUPPORT
{"graph_symbol_gpu", "# Graph symbol to use for graphs in gpu box, \"default\", \"braille\", \"block\" or \"tty\"."},

#endif
{"graph_symbol_mem", "# Graph symbol to use for graphs in cpu box, \"default\", \"braille\", \"block\" or \"tty\"."},

{"graph_symbol_net", "# Graph symbol to use for graphs in cpu box, \"default\", \"braille\", \"block\" or \"tty\"."},
Expand Down Expand Up @@ -114,9 +114,9 @@ namespace Config {

{"cpu_graph_lower", "#* Sets the CPU stat shown in lower half of the CPU graph, \"total\" is always available.\n"
"#* Select from a list of detected attributes from the options menu."},

#ifdef GPU_SUPPORT
{"show_gpu_info", "#* If gpu info should be shown in the cpu box. Available values = \"Auto\", \"On\" and \"Off\"."},

#endif
{"cpu_invert_lower", "#* Toggles if the lower CPU graph should be inverted."},

{"cpu_single_graph", "#* Set to True to completely disable the lower CPU graph."},
Expand Down Expand Up @@ -197,6 +197,7 @@ namespace Config {

{"log_level", "#* Set loglevel for \"~/.config/btop/btop.log\" levels are: \"ERROR\" \"WARNING\" \"INFO\" \"DEBUG\".\n"
"#* The level set includes all lower levels, i.e. \"DEBUG\" will show all logging info."},
#ifdef GPU_SUPPORT

{"nvml_measure_pcie_speeds",
"#* Measure PCIe throughput on NVIDIA cards, may impact performance on certain cards."},
Expand All @@ -209,6 +210,7 @@ namespace Config {
{"custom_gpu_name3", "#* Custom gpu3 model name, empty string to disable."},
{"custom_gpu_name4", "#* Custom gpu4 model name, empty string to disable."},
{"custom_gpu_name5", "#* Custom gpu5 model name, empty string to disable."},
#endif
};

unordered_flat_map<std::string_view, string> strings = {
Expand Down Expand Up @@ -237,13 +239,15 @@ namespace Config {
{"proc_filter", ""},
{"proc_command", ""},
{"selected_name", ""},
#ifdef GPU_SUPPORT
{"custom_gpu_name0", ""},
{"custom_gpu_name1", ""},
{"custom_gpu_name2", ""},
{"custom_gpu_name3", ""},
{"custom_gpu_name4", ""},
{"custom_gpu_name5", ""},
{"show_gpu_info", "Auto"}
#endif
};
unordered_flat_map<std::string_view, string> stringsTmp;

Expand Down Expand Up @@ -292,8 +296,10 @@ namespace Config {
{"lowcolor", false},
{"show_detailed", false},
{"proc_filtering", false},
#ifdef GPU_SUPPORT
{"nvml_measure_pcie_speeds", true},
{"gpu_mirror_graph", true},
#endif
};
unordered_flat_map<std::string_view, bool> boolsTmp;

Expand Down Expand Up @@ -440,8 +446,10 @@ namespace Config {
else if (name == "shown_boxes" and not value.empty() and not check_boxes(value))
validError = "Invalid box name(s) in shown_boxes!";

#ifdef GPU_SUPPORT
else if (name == "show_gpu_info" and not v_contains(show_gpu_values, value))
validError = "Invalid value for show_gpu_info: " + value;
#endif

else if (name == "presets" and not presetsValid(value))
return false;
Expand Down Expand Up @@ -545,11 +553,13 @@ namespace Config {
auto new_boxes = ssplit(boxes);
for (auto& box : new_boxes) {
if (not v_contains(valid_boxes, box)) return false;
#ifdef GPU_SUPPORT
if (box.starts_with("gpu")) {
size_t gpu_num = stoi(box.substr(3));
if (gpu_num == 0) gpu_num = 5;
if (std::cmp_greater(gpu_num, Gpu::gpu_names.size())) return false;
}
#endif
}
current_boxes = std::move(new_boxes);
return true;
Expand Down
10 changes: 8 additions & 2 deletions src/btop_config.hpp
Expand Up @@ -43,10 +43,16 @@ namespace Config {

const vector<string> valid_graph_symbols = { "braille", "block", "tty" };
const vector<string> valid_graph_symbols_def = { "default", "braille", "block", "tty" };
const vector<string> valid_boxes = { "cpu", "mem", "net", "proc", "gpu0", "gpu1", "gpu2", "gpu3", "gpu4", "gpu5" };
const vector<string> valid_boxes = {
"cpu", "mem", "net", "proc"
#ifdef GPU_SUPPORT
,"gpu0", "gpu1", "gpu2", "gpu3", "gpu4", "gpu5"
#endif
};
const vector<string> temp_scales = { "celsius", "fahrenheit", "kelvin", "rankine" };
#ifdef GPU_SUPPORT
const vector<string> show_gpu_values = { "Auto", "On", "Off" };

#endif
extern vector<string> current_boxes;
extern vector<string> preset_list;
extern vector<string> available_batteries;
Expand Down

0 comments on commit b877726

Please sign in to comment.