Skip to content

Commit e79de02

Browse files
committed
fix for watts overflows
prevents a rare bad sensor reading for cpu watts max cpu watts value is now 9,999 watts use `fmt::format_to` for output fix potential one character overflow for gpu pwr usage watts
1 parent 633b4ba commit e79de02

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/btop_draw.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ namespace Cpu {
522522
int b_columns, b_column_size;
523523
int b_x, b_y, b_width, b_height;
524524
float max_observed_pwr = 1.0f;
525+
float last_valid_cpu_watts = 0.0f;
525526

526527
int graph_up_height, graph_low_height;
527528
int graph_up_width, graph_low_width;
@@ -846,11 +847,16 @@ namespace Cpu {
846847
}
847848

848849
if (show_watts) {
849-
string cwatts = fmt::format(" {:>4.{}f}", cpu.usage_watts, cpu.usage_watts < 10.0f ? 2 : cpu.usage_watts < 100.0f ? 1 : 0);
850-
string cwatts_post = "W";
851-
852-
max_observed_pwr = max(max_observed_pwr, cpu.usage_watts);
853-
out += Theme::g("cached").at(clamp(cpu.usage_watts / max_observed_pwr * 100.0f, 0.0f, 100.0f)) + cwatts + Theme::c("main_fg") + cwatts_post;
850+
// Check for good cpu watts reading. It is rare but there has been at least one system_uptime
851+
// that has given a bad reading for one cycle every once in while.
852+
const auto watts_error = cpu.usage_watts < 0.0f or cpu.usage_watts > 9'999.0f;
853+
if (not watts_error) last_valid_cpu_watts = cpu.usage_watts;
854+
max_observed_pwr = max(max_observed_pwr, last_valid_cpu_watts);
855+
fmt::format_to(std::back_inserter(out), " {watts_color}{cwatts:>4.{precision}f}{main_fg}W",
856+
"watts_color"_a = Theme::g("cached").at(clamp(last_valid_cpu_watts / max_observed_pwr * 100.0f, 0.0f, 100.0f)),
857+
"cwatts"_a = last_valid_cpu_watts,
858+
"precision"_a = last_valid_cpu_watts < 9.995f ? 2 : last_valid_cpu_watts < 99.95f ? 1 : 0,
859+
"main_fg"_a = Theme::c("main_fg"));
854860
}
855861

856862
out += Theme::c("div_line") + Symbols::v_line;
@@ -954,8 +960,11 @@ namespace Cpu {
954960
out += rjust(to_string(temp), 3) + Theme::c("main_fg") + unit;
955961
}
956962
if (gpus[i].supported_functions.pwr_usage) {
957-
out += ' ' + Theme::g("cached").at(clamp(safeVal(gpus[i].gpu_percent, "gpu-pwr-totals"s).back(), 0ll, 100ll))
958-
+ fmt::format("{:>4.{}f}", gpus[i].pwr_usage / 1000.0, gpus[i].pwr_usage < 10'000 ? 2 : gpus[i].pwr_usage < 100'000 ? 1 : 0) + Theme::c("main_fg") + 'W';
963+
fmt::format_to(std::back_inserter(out), " {watts_color}{gpu_watts:>4.{precision}f}{main_fg}W",
964+
"watts_color"_a = Theme::g("cached").at(clamp(safeVal(gpus[i].gpu_percent, "gpu-pwr-totals"s).back(), 0ll, 100ll)),
965+
"gpu_watts"_a = gpus[i].pwr_usage / 1000.0,
966+
"precision"_a = gpus[i].pwr_usage < 9'995 ? 2 : gpus[i].pwr_usage < 99'950 ? 1 : 0,
967+
"main_fg"_a = Theme::c("main_fg"));
959968
}
960969

961970
if (cy > b_height - 1) break;
@@ -1081,9 +1090,12 @@ namespace Gpu {
10811090

10821091
//? Power usage meter, power state
10831092
if (gpu.supported_functions.pwr_usage) {
1084-
out += Mv::to(b_y + rows_used, b_x + 1) + Theme::c("main_fg") + Fx::b + "PWR " + pwr_meter(safeVal(gpu.gpu_percent, "gpu-pwr-totals"s).back())
1085-
+ Theme::g("cached").at(clamp(safeVal(gpu.gpu_percent, "gpu-pwr-totals"s).back(), 0ll, 100ll))
1086-
+ fmt::format("{:>5.{}f}", gpu.pwr_usage / 1000.0, gpu.pwr_usage < 10'000 ? 2 : gpu.pwr_usage < 100'000 ? 1 : 0) + Theme::c("main_fg") + 'W';
1093+
fmt::format_to(std::back_inserter(out), "{move}{main_fg}{bold}PWR {pwr_meter}{watts_color}{gpu_watts:>5.{precision}f}{main_fg}W",
1094+
"move"_a = Mv::to(b_y + rows_used, b_x + 1), "bold"_a = Fx::b, "main_fg"_a = Theme::c("main_fg"),
1095+
"pwr_meter"_a = pwr_meter(safeVal(gpu.gpu_percent, "gpu-pwr-totals"s).back()),
1096+
"watts_color"_a = Theme::g("cached").at(clamp(safeVal(gpu.gpu_percent, "gpu-pwr-totals"s).back(), 0ll, 100ll)),
1097+
"gpu_watts"_a = gpu.pwr_usage / 1000.0,
1098+
"precision"_a = gpu.pwr_usage < 9'995 ? 2 : gpu.pwr_usage < 99'950 ? 1 : 0);
10871099
if (gpu.supported_functions.pwr_state and gpu.pwr_state != 32) // NVML_PSTATE_UNKNOWN; unsupported or non-nvidia card
10881100
out += std::string(" P-state: ") + (gpu.pwr_state > 9 ? "" : " ") + 'P' + Theme::g("cached").at(clamp(gpu.pwr_state, 0ll, 100ll)) + to_string(gpu.pwr_state);
10891101
rows_used++;

0 commit comments

Comments
 (0)