From a9bc0874d48dfef157a95d075e5e81ad248faa87 Mon Sep 17 00:00:00 2001 From: aristocratos Date: Sun, 27 Aug 2023 00:31:07 +0200 Subject: [PATCH] Added show_gpu_info setting and Auto options for cpu graphs --- src/btop.cpp | 22 +++++++++++++--------- src/btop_config.cpp | 15 +++++++++++++-- src/btop_config.hpp | 1 + src/btop_draw.cpp | 26 ++++++++++++++++---------- src/btop_menu.cpp | 19 +++++++++++++++---- src/freebsd/btop_collect.cpp | 2 +- src/linux/btop_collect.cpp | 12 ++---------- src/osx/btop_collect.cpp | 2 +- 8 files changed, 62 insertions(+), 37 deletions(-) diff --git a/src/btop.cpp b/src/btop.cpp index 5b58a10..2598321 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -222,12 +222,14 @@ void term_resize(bool force) { auto key = Input::get(); if (key == "q") clean_quit(0); - else if (std::isdigit(*key.c_str())) { - Config::current_preset = -1; - auto box = all_boxes.at(*key.c_str() - '0'); - if (box.rfind("gpu", 0) == 0 && (box[3] - '0' + 2) > (int)Gpu::gpu_names.size()) return; - Config::toggle_box(box); - boxes = Config::getS("shown_boxes"); + 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))) { + Config::current_preset = -1; + Config::toggle_box(box); + boxes = Config::getS("shown_boxes"); + } } } min_size = Term::get_min_size(boxes); @@ -506,12 +508,14 @@ namespace Runner { //* Run collection and draw functions for all boxes try { //? GPU data collection - bool gpu_in_cpu_panel = Config::getS("cpu_graph_lower").rfind("gpu-", 0) == 0 - or Config::getS("cpu_graph_upper").rfind("gpu-", 0) == 0; + 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-") + or (Gpu::shown == 0 and Config::getS("show_gpu_info") != "Off") + ); vector gpu_panels = {}; for (auto& box : conf.boxes) - if (box.rfind("gpu", 0) == 0) + if (box.starts_with("gpu")) gpu_panels.push_back(box.back()-'0'); vector gpus; diff --git a/src/btop_config.cpp b/src/btop_config.cpp index 34ae1d5..a2864f1 100644 --- a/src/btop_config.cpp +++ b/src/btop_config.cpp @@ -115,6 +115,8 @@ 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."}, + {"show_gpu_info", "#* If gpu info should be shown in the cpu box. Available values = \"Auto\", \"On\" and \"Off\"."}, + {"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."}, @@ -220,8 +222,8 @@ namespace Config { {"graph_symbol_net", "default"}, {"graph_symbol_proc", "default"}, {"proc_sorting", "cpu lazy"}, - {"cpu_graph_upper", "default"}, // set to "total" in btop_collect.cpp - {"cpu_graph_lower", "default"}, // set to "total" or "gpu-totals" in btop_collect.cpp + {"cpu_graph_upper", "Auto"}, + {"cpu_graph_lower", "Auto"}, {"cpu_sensor", "Auto"}, {"selected_battery", "Auto"}, {"cpu_core_map", ""}, @@ -241,6 +243,7 @@ namespace Config { {"custom_gpu_name3", ""}, {"custom_gpu_name4", ""}, {"custom_gpu_name5", ""}, + {"show_gpu_info", "Auto"} }; unordered_flat_map stringsTmp; @@ -437,6 +440,9 @@ namespace Config { else if (name == "shown_boxes" and not value.empty() and not check_boxes(value)) validError = "Invalid box name(s) in shown_boxes!"; + else if (name == "show_gpu_info" and not v_contains(show_gpu_values, value)) + validError = "Invalid value for show_gpu_info: " + value; + else if (name == "presets" and not presetsValid(value)) return false; @@ -539,6 +545,11 @@ namespace Config { auto new_boxes = ssplit(boxes); for (auto& box : new_boxes) { if (not v_contains(valid_boxes, box)) return false; + 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; + } } current_boxes = std::move(new_boxes); return true; diff --git a/src/btop_config.hpp b/src/btop_config.hpp index c7fc993..82b232d 100644 --- a/src/btop_config.hpp +++ b/src/btop_config.hpp @@ -45,6 +45,7 @@ namespace Config { const vector valid_graph_symbols_def = { "default", "braille", "block", "tty" }; const vector valid_boxes = { "cpu", "mem", "net", "proc", "gpu0", "gpu1", "gpu2", "gpu3", "gpu4", "gpu5" }; const vector temp_scales = { "celsius", "fahrenheit", "kelvin", "rankine" }; + const vector show_gpu_values = { "Auto", "On", "Off" }; extern vector current_boxes; extern vector preset_list; diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index 5aa5b28..921fedd 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -533,13 +533,21 @@ namespace Cpu { auto single_graph = Config::getB("cpu_single_graph"); bool hide_cores = show_temps and (cpu_temp_only or not Config::getB("show_coretemp")); const int extra_width = (hide_cores ? max(6, 6 * b_column_size) : 0); - auto& graph_up_field = Config::getS("cpu_graph_upper"); - auto& graph_lo_field = Config::getS("cpu_graph_lower"); + const auto& show_gpu_info = Config::getS("show_gpu_info"); + bool show_gpu = (gpus.size() > 0 and (show_gpu_info == "On" or (show_gpu_info == "Auto" and Gpu::shown == 0))); + auto graph_up_field = Config::getS("cpu_graph_upper"); + if (graph_up_field == "Auto" or not v_contains(Cpu::available_fields, graph_up_field)) + graph_up_field = "total"; + auto graph_lo_field = Config::getS("cpu_graph_lower"); + if (graph_lo_field == "Auto" or not v_contains(Cpu::available_fields, graph_lo_field)) { + graph_lo_field = show_gpu ? "gpu-totals" : "total"; + } auto tty_mode = Config::getB("tty_mode"); auto& graph_symbol = (tty_mode ? "tty" : Config::getS("graph_symbol_cpu")); auto& graph_bg = Symbols::graph_symbols.at((graph_symbol == "default" ? Config::getS("graph_symbol") + "_up" : graph_symbol + "_up")).at(6); auto& temp_scale = Config::getS("temp_scale"); auto cpu_bottom = Config::getB("cpu_bottom"); + const string& title_left = Theme::c("cpu_box") + (cpu_bottom ? Symbols::title_left_down : Symbols::title_left); const string& title_right = Theme::c("cpu_box") + (cpu_bottom ? Symbols::title_right_down : Symbols::title_right); static int bat_pos = 0, bat_len = 0; @@ -573,7 +581,7 @@ namespace Cpu { const int graph_default_width = x + width - b_width - 3; auto init_graphs = [&](vector& graphs, const int graph_height, int& graph_width, const string& graph_field, bool invert) { - if (graph_field.rfind("gpu", 0) == 0) { + if (graph_field.starts_with("gpu")) { if (graph_field.find("totals") != string::npos) { graphs.resize(gpus.size()); gpu_temp_graphs.resize(gpus.size()); @@ -721,7 +729,7 @@ namespace Cpu { //? Cpu/Gpu graphs out += Fx::ub + Mv::to(y + 1, x + 1); auto draw_graphs = [&](vector& graphs, const int graph_height, const int graph_width, const string& graph_field) { - if (graph_field.rfind("gpu", 0) == 0) + if (graph_field.starts_with("gpu")) if (graph_field.find("totals") != string::npos) for (unsigned long i = 0;;) { out += graphs[i](gpus[i].gpu_percent.at(graph_field), (data_same or redraw)); @@ -806,7 +814,6 @@ namespace Cpu { } //? Load average - bool show_gpu = (graph_lo_field.rfind("gpu-", 0) == 0) or (graph_up_field.rfind("gpu-", 0) == 0); if (cy < b_height - 1 and cc <= b_columns) { string lavg_pre; int sep = 1; @@ -1949,11 +1956,10 @@ namespace Draw { //* Calculate and draw cpu box outlines if (Cpu::shown) { using namespace Cpu; - const bool gpus_shown_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 - ) and Gpu::gpu_names.size() != (unsigned long)Gpu::shown; + const bool gpus_shown_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-") + or (Config::getS("cpu_graph_lower") == "Auto" and Gpu::shown == 0) + ); const int gpus_height_offset = (Gpu::gpu_names.size() - Gpu::shown)*gpus_shown_in_cpu_panel; const bool show_temp = (Config::getB("check_temp") and got_sensors); width = round((double)Term::width * width_p / 100); diff --git a/src/btop_menu.cpp b/src/btop_menu.cpp index 873b86a..b217cd1 100644 --- a/src/btop_menu.cpp +++ b/src/btop_menu.cpp @@ -320,7 +320,7 @@ namespace Menu { "the CPU graph.", "", "CPU:", - "\"total\" = Total cpu usage.", + "\"total\" = Total cpu usage. (Auto)", "\"user\" = User mode cpu usage.", "\"system\" = Kernel mode cpu usage.", "+ more depending on kernel.", @@ -346,7 +346,7 @@ namespace Menu { "+ more depending on kernel.", "", "GPU:", - "\"gpu-totals\" = GPU usage split by device.", + "\"gpu-totals\" = GPU usage split/device. (Auto)", "\"gpu-vram-totals\" = VRAM usage split by GPU.", "\"gpu-pwr-totals\" = Power usage split by GPU.", "\"gpu-average\" = Avg usage of all GPUs.", @@ -364,6 +364,16 @@ namespace Menu { "to fit to box height.", "", "True or False."}, + {"show_gpu_info", + "Show gpu info in cpu box.", + "", + "Toggles gpu stats in cpu box and the", + "gpu graph (if \"cpu_graph_lower\" is set to", + "\"Auto\").", + "", + "\"Auto\" to show when no gpu box is shown.", + "\"On\" to always show.", + "\"Off\" to never show."}, {"check_temp", "Enable cpu temperature reporting.", "", @@ -1094,6 +1104,7 @@ namespace Menu { {"cpu_graph_lower", std::cref(Cpu::available_fields)}, {"cpu_sensor", std::cref(Cpu::available_sensors)}, {"selected_battery", std::cref(Config::available_batteries)}, + {"show_gpu_info", std::cref(Config::show_gpu_values)} }; auto tty_mode = Config::getB("tty_mode"); auto vim_keys = Config::getB("vim_keys"); @@ -1145,7 +1156,7 @@ namespace Menu { const auto& option = categories[selected_cat][item_height * page + selected][0]; if (selPred.test(isString) and Config::stringValid(option, editor.text)) { Config::set(option, editor.text); - if (option == "custom_cpu_name" or option.rfind("custom_gpu_name", 0) == 0) + if (option == "custom_cpu_name" or option.starts_with("custom_gpu_name")) screen_redraw = true; else if (is_in(option, "shown_boxes", "presets")) { screen_redraw = true; @@ -1279,7 +1290,7 @@ namespace Menu { Logger::set(optList.at(i)); Logger::info("Logger set to " + optList.at(i)); } - else if (is_in(option, "proc_sorting", "cpu_sensor") or option.starts_with("graph_symbol") or option.starts_with("cpu_graph_")) + else if (is_in(option, "proc_sorting", "cpu_sensor", "show_gpu_info") or option.starts_with("graph_symbol") or option.starts_with("cpu_graph_")) screen_redraw = true; } else diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp index 47598f0..7f93322 100644 --- a/src/freebsd/btop_collect.cpp +++ b/src/freebsd/btop_collect.cpp @@ -74,7 +74,7 @@ using namespace Tools; namespace Cpu { vector core_old_totals; vector core_old_idles; - vector available_fields = {"total"}; + vector available_fields = {"Auto", "total"}; vector available_sensors = {"Auto"}; cpu_info current_cpu; bool got_sensors = false, cpu_temp_only = false; diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 370d744..91e376c 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -64,7 +64,7 @@ using namespace std::literals; // for operator""s namespace Cpu { vector core_old_totals; vector core_old_idles; - vector available_fields; + vector available_fields = {"Auto", "total"}; vector available_sensors = {"Auto"}; cpu_info current_cpu; fs::path freq_path = "/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq"; @@ -251,7 +251,7 @@ namespace Shared { Cpu::collect(); if (Runner::coreNum_reset) Runner::coreNum_reset = false; for (auto& [field, vec] : Cpu::current_cpu.cpu_percent) { - if (not vec.empty()) Cpu::available_fields.push_back(field); + if (not vec.empty() and not v_contains(Cpu::available_fields, field)) Cpu::available_fields.push_back(field); } Cpu::cpuName = Cpu::get_cpuName(); Cpu::got_sensors = Cpu::get_sensors(); @@ -268,8 +268,6 @@ namespace Shared { Cpu::available_fields.push_back(key); for (auto const& [key, _] : Gpu::shared_gpu_percent) Cpu::available_fields.push_back(key); - if (Config::strings.at("cpu_graph_lower") == "default") - Config::strings.at("cpu_graph_lower") = "gpu-totals"; using namespace Gpu; gpu_b_height_offsets.resize(gpus.size()); @@ -284,12 +282,6 @@ namespace Shared { Mem::old_uptime = system_uptime(); Mem::collect(); - //? Init for CPU graphs - if (Config::strings.at("cpu_graph_upper") == "default" or not v_contains(Cpu::available_fields, Config::strings.at("cpu_graph_upper"))) - Config::strings.at("cpu_graph_upper") = "total"; - if (Config::strings.at("cpu_graph_lower") == "default" or not v_contains(Cpu::available_fields, Config::strings.at("cpu_graph_lower"))) - Config::strings.at("cpu_graph_lower") = "total"; - Logger::debug("Shared::init() : Initialized."); } } diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp index 26a93d3..0a1f338 100644 --- a/src/osx/btop_collect.cpp +++ b/src/osx/btop_collect.cpp @@ -70,7 +70,7 @@ using namespace Tools; namespace Cpu { vector core_old_totals; vector core_old_idles; - vector available_fields = {"total"}; + vector available_fields = {"Auto", "total"}; vector available_sensors = {"Auto"}; cpu_info current_cpu; bool got_sensors = false, cpu_temp_only = false;