Skip to content

Commit

Permalink
Improved cpu idle ticks calculation
Browse files Browse the repository at this point in the history
Removes the minimum of 1 idle cpu tick calculated and adds a check when there were no changes since last update.
  • Loading branch information
TerenceTux committed Jan 8, 2025
1 parent 5535f50 commit 2c004d8
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 20 deletions.
18 changes: 14 additions & 4 deletions src/freebsd/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,13 @@ namespace Cpu {

//? Calculate cpu total for each core
if (i > Shared::coreCount) break;
const long long calc_totals = max(0ll, totals - core_old_totals.at(i));
const long long calc_idles = max(0ll, idles - core_old_idles.at(i));
long long calc_totals = max(0ll, totals - core_old_totals.at(i));
long long calc_idles = max(0ll, idles - core_old_idles.at(i));
if (calc_totals == 0) {
//? No changes for this core, simulate 1 idle
calc_totals = 1;
calc_idles = 1;
}
core_old_totals.at(i) = totals;
core_old_idles.at(i) = idles;

Expand All @@ -453,8 +458,13 @@ namespace Cpu {

}

const long long calc_totals = max(1ll, global_totals - cpu_old.at("totals"));
const long long calc_idles = max(1ll, global_idles - cpu_old.at("idles"));
long long calc_totals = max(0ll, global_totals - cpu_old.at("totals"));
long long calc_idles = max(0ll, global_idles - cpu_old.at("idles"));
if (calc_totals == 0) {
//? No changes, simulate 1 idle
calc_totals = 1;
calc_idles = 1;
}

//? Populate cpu.cpu_percent with all fields from syscall
for (int ii = 0; const auto &val : times_summed) {
Expand Down
18 changes: 14 additions & 4 deletions src/linux/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,8 +951,13 @@ namespace Cpu {

//? Calculate values for totals from first line of stat
if (i == 0) {
const long long calc_totals = max(1ll, totals - cpu_old.at("totals"));
const long long calc_idles = max(1ll, idles - cpu_old.at("idles"));
long long calc_totals = max(0ll, totals - cpu_old.at("totals"));
long long calc_idles = max(0ll, idles - cpu_old.at("idles"));
if (calc_totals == 0) {
//? No changes in stat, simulate 1 idle
calc_totals = 1;
calc_idles = 1;
}
cpu_old.at("totals") = totals;
cpu_old.at("idles") = idles;

Expand Down Expand Up @@ -982,8 +987,13 @@ namespace Cpu {
core_old_idles.push_back(0);
cpu.core_percent.emplace_back();
}
const long long calc_totals = max(0ll, totals - core_old_totals.at(i-1));
const long long calc_idles = max(0ll, idles - core_old_idles.at(i-1));
long long calc_totals = max(0ll, totals - core_old_totals.at(i-1));
long long calc_idles = max(0ll, idles - core_old_idles.at(i-1));
if (calc_totals == 0) {
//? No changes in stat for this core, simulate 1 idle
calc_totals = 1;
calc_idles = 1;
}
core_old_totals.at(i-1) = totals;
core_old_idles.at(i-1) = idles;

Expand Down
18 changes: 14 additions & 4 deletions src/netbsd/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,13 @@ namespace Cpu {

//? Calculate cpu total for each core
if (i > Shared::coreCount) break;
const long long calc_totals = max(0ll, totals - core_old_totals.at(i));
const long long calc_idles = max(0ll, idles - core_old_idles.at(i));
long long calc_totals = max(0ll, totals - core_old_totals.at(i));
long long calc_idles = max(0ll, idles - core_old_idles.at(i));
if (calc_totals == 0) {
//? No changes for this core, simulate 1 idle
calc_totals = 1;
calc_idles = 1;
}
core_old_totals.at(i) = totals;
core_old_idles.at(i) = idles;

Expand All @@ -618,8 +623,13 @@ namespace Cpu {

}

const long long calc_totals = max(1ll, global_totals - cpu_old.at("totals"));
const long long calc_idles = max(1ll, global_idles - cpu_old.at("idles"));
long long calc_totals = max(0ll, global_totals - cpu_old.at("totals"));
long long calc_idles = max(0ll, global_idles - cpu_old.at("idles"));
if (calc_totals == 0) {
//? No changes, simulate 1 idle
calc_totals = 1;
calc_idles = 1;
}

//? Populate cpu.cpu_percent with all fields from syscall
for (int ii = 0; const auto &val : times_summed) {
Expand Down
18 changes: 14 additions & 4 deletions src/openbsd/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,13 @@ namespace Cpu {

//? Calculate cpu total for each core
if (i > Shared::coreCount) break;
const long long calc_totals = max(0ll, totals - core_old_totals.at(i));
const long long calc_idles = max(0ll, idles - core_old_idles.at(i));
long long calc_totals = max(0ll, totals - core_old_totals.at(i));
long long calc_idles = max(0ll, idles - core_old_idles.at(i));
if (calc_totals == 0) {
//? No changes for this core, simulate 1 idle
calc_totals = 1;
calc_idles = 1;
}
core_old_totals.at(i) = totals;
core_old_idles.at(i) = idles;

Expand All @@ -469,8 +474,13 @@ namespace Cpu {

}

const long long calc_totals = max(1ll, global_totals - cpu_old.at("totals"));
const long long calc_idles = max(1ll, global_idles - cpu_old.at("idles"));
long long calc_totals = max(0ll, global_totals - cpu_old.at("totals"));
long long calc_idles = max(0ll, global_idles - cpu_old.at("idles"));
if (calc_totals == 0) {
//? No changes, simulate 1 idle
calc_totals = 1;
calc_idles = 1;
}

//? Populate cpu.cpu_percent with all fields from syscall
for (int ii = 0; const auto &val : times_summed) {
Expand Down
18 changes: 14 additions & 4 deletions src/osx/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,13 @@ namespace Cpu {

//? Calculate cpu total for each core
if (i > Shared::coreCount) break;
const long long calc_totals = max(0ll, totals - core_old_totals.at(i));
const long long calc_idles = max(0ll, idles - core_old_idles.at(i));
long long calc_totals = max(0ll, totals - core_old_totals.at(i));
long long calc_idles = max(0ll, idles - core_old_idles.at(i));
if (calc_totals == 0) {
//? No changes for this core, simulate 1 idle
calc_totals = 1;
calc_idles = 1;
}
core_old_totals.at(i) = totals;
core_old_idles.at(i) = idles;

Expand All @@ -510,8 +515,13 @@ namespace Cpu {
}
}

const long long calc_totals = max(1ll, global_totals - cpu_old.at("totals"));
const long long calc_idles = max(1ll, global_idles - cpu_old.at("idles"));
long long calc_totals = max(0ll, global_totals - cpu_old.at("totals"));
long long calc_idles = max(0ll, global_idles - cpu_old.at("idles"));
if (calc_totals == 0) {
//? No changes, simulate 1 idle
calc_totals = 1;
calc_idles = 1;
}

//? Populate cpu.cpu_percent with all fields from syscall
for (int ii = 0; const auto &val : times_summed) {
Expand Down

0 comments on commit 2c004d8

Please sign in to comment.