Skip to content

Commit c4c7869

Browse files
authored
Merge branch 'main' into fix-non-ascii-proc
2 parents f6c2721 + 6462ebc commit c4c7869

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/openbsd/btop_collect.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ namespace Shared {
127127
//? Shared global variables init
128128
int mib[2];
129129
mib[0] = CTL_HW;
130-
mib[1] = HW_NCPU;
130+
mib[1] = HW_NCPUONLINE;
131131
int ncpu;
132132
size_t len = sizeof(ncpu);
133133
if (sysctl(mib, 2, &ncpu, &len, nullptr, 0) == -1) {
@@ -392,13 +392,22 @@ namespace Cpu {
392392
Logger::error("failed to get load averages");
393393
}
394394

395+
//? Read total physical CPU count for sysctl iteration
396+
//? (may differ from coreCount when SMT is disabled via hw.smt=0).
397+
int ncpu_total = Shared::coreCount;
398+
{
399+
int mib[] = {CTL_HW, HW_NCPU};
400+
size_t len = sizeof(ncpu_total);
401+
sysctl(mib, 2, &ncpu_total, &len, nullptr, 0);
402+
}
403+
395404
auto cp_time = std::unique_ptr<struct cpustats[]>{
396-
new struct cpustats[Shared::coreCount]
405+
new struct cpustats[ncpu_total]
397406
};
398-
size_t size = Shared::coreCount * sizeof(struct cpustats);
407+
size_t size = sizeof(struct cpustats);
399408
static int cpustats_mib[] = {CTL_KERN, KERN_CPUSTATS, /*fillme*/0};
400-
for (int i = 0; i < Shared::coreCount; i++) {
401-
cpustats_mib[2] = i / 2;
409+
for (int i = 0; i < ncpu_total; i++) {
410+
cpustats_mib[2] = i;
402411
if (sysctl(cpustats_mib, 3, &cp_time[i], &size, NULL, 0) == -1) {
403412
Logger::error("sysctl kern.cpustats failed");
404413
}
@@ -407,11 +416,16 @@ namespace Cpu {
407416
long long global_idles = 0;
408417
vector<long long> times_summed = {0, 0, 0, 0};
409418

410-
for (long i = 0; i < Shared::coreCount; i++) {
419+
//? j iterates all physical CPUs; offline ones are skipped
420+
//? i is the display slot index, incremented only for online CPUs
421+
for (long i = 0, j = 0; j < ncpu_total; j++) {
422+
if (!(cp_time[j].cs_flags & CPUSTATS_ONLINE))
423+
continue;
424+
411425
vector<long long> times;
412426
//? 0=user, 1=nice, 2=system, 3=idle
413427
for (int x = 0; const unsigned int c_state : {CP_USER, CP_NICE, CP_SYS, CP_IDLE}) {
414-
auto val = cp_time[i].cs_time[c_state];
428+
auto val = cp_time[j].cs_time[c_state];
415429
times.push_back(val);
416430
times_summed.at(x++) += val;
417431
}
@@ -426,7 +440,6 @@ namespace Cpu {
426440
global_idles += idles;
427441

428442
//? Calculate cpu total for each core
429-
if (i > Shared::coreCount) break;
430443
const long long calc_totals = max(0ll, totals - core_old_totals.at(i));
431444
const long long calc_idles = max(0ll, idles - core_old_idles.at(i));
432445
core_old_totals.at(i) = totals;
@@ -441,7 +454,7 @@ namespace Cpu {
441454
Logger::error("Cpu::collect() : {}", e.what());
442455
throw std::runtime_error(fmt::format("collect() : {}", e.what()));
443456
}
444-
457+
i++;
445458
}
446459

447460
const long long calc_totals = max(1ll, global_totals - cpu_old.at("totals"));
@@ -1222,7 +1235,7 @@ namespace Proc {
12221235
}
12231236
toggle_children = -1;
12241237
}
1225-
1238+
12261239
if (auto find_pid = (collapse != -1 ? collapse : expand); find_pid != -1) {
12271240
auto collapser = rng::find(current_procs, find_pid, &proc_info::pid);
12281241
if (collapser != current_procs.end()) {

0 commit comments

Comments
 (0)