$OpenBSD: patch-openbsd_OpenBSDProcessList_c,v 1.6 2021/04/03 11:13:48 sthen Exp $

Don't include "offline" CPUs in the stats (i.e. CPUs which have been
disabled by sysctl hw.smt=0).

https://github.com/htop-dev/htop/pull/580

Index: openbsd/OpenBSDProcessList.c
--- openbsd/OpenBSDProcessList.c.orig
+++ openbsd/OpenBSDProcessList.c
@@ -37,11 +37,15 @@ static int pageSize;
 static int pageSizeKB;
 
 ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) {
-   const int mib[] = { CTL_HW, HW_NCPU };
+   const int nmib[] = { CTL_HW, HW_NCPU };
+   const int mib[] = { CTL_HW, HW_NCPUONLINE };
    const int fmib[] = { CTL_KERN, KERN_FSCALE };
+   int ncmib[] = { CTL_KERN, KERN_CPUSTATS, 0 };
    int r;
+   unsigned int cpu_index_c = 0, ncpu;
    size_t size;
    char errbuf[_POSIX2_LINE_MAX];
+   struct cpustats cpu_stats;
 
    OpenBSDProcessList* opl = xCalloc(1, sizeof(OpenBSDProcessList));
    ProcessList* pl = (ProcessList*) opl;
@@ -53,7 +57,14 @@ ProcessList* ProcessList_new(UsersTable* usersTable, H
       pl->cpuCount = 1;
    }
    opl->cpus = xCalloc(pl->cpuCount + 1, sizeof(CPUData));
+   opl->cpuIndex = xRealloc(opl->cpuIndex, pl->cpuCount * sizeof(int));
 
+   size = sizeof(int);
+   r = sysctl(nmib, 2, &ncpu, &size, NULL, 0);
+   if (r < 0) {
+      ncpu = pl->cpuCount;
+   }
+
    size = sizeof(fscale);
    if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) {
       CRT_fatalError("fscale sysctl call failed");
@@ -76,6 +87,19 @@ ProcessList* ProcessList_new(UsersTable* usersTable, H
 
    opl->cpuSpeed = -1;
 
+   size = sizeof(cpu_stats);
+   for (unsigned int i = 0; i < ncpu; i++) {
+      ncmib[2] = i;
+      sysctl(ncmib, 3, &cpu_stats, &size, NULL, 0);
+      if (cpu_stats.cs_flags & CPUSTATS_ONLINE) {
+         opl->cpuIndex[cpu_index_c] = i;
+         cpu_index_c++;
+      }
+
+      if (cpu_index_c == pl->cpuCount)
+         break;
+   }
+
    return pl;
 }
 
@@ -332,7 +356,7 @@ static void OpenBSDProcessList_scanCPUTime(OpenBSDProc
    u_int64_t avg[CPUSTATES] = {0};
 
    for (unsigned int i = 0; i < this->super.cpuCount; i++) {
-      getKernelCPUTimes(i, kernelTimes);
+      getKernelCPUTimes(this->cpuIndex[i], kernelTimes);
       CPUData* cpu = this->cpus + i + 1;
       kernelCPUTimesToHtop(kernelTimes, cpu);
 
