diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index fbe7c103..1c6c0e2f 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -37,9 +37,12 @@ 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 r; + unsigned int cpu_index_c = 0; + unsigned int ncpu; size_t size; char errbuf[_POSIX2_LINE_MAX]; @@ -54,6 +57,12 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, ui } opl->cpus = xCalloc(pl->cpuCount + 1, sizeof(CPUData)); + 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 +85,23 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, ui opl->cpuSpeed = -1; + for (unsigned int i = 0; i < ncpu; i++) { + const int ncmib[] = { CTL_KERN, KERN_CPUSTATS, i }; + struct cpustats cpu_stats; + + size = sizeof(cpu_stats); + if (sysctl(ncmib, 3, &cpu_stats, &size, NULL, 0) < 0) { + CRT_fatalError("ncmib sysctl call failed"); + } + if (cpu_stats.cs_flags & CPUSTATS_ONLINE) { + opl->cpus[cpu_index_c].cpuIndex = i; + cpu_index_c++; + } + + if (cpu_index_c == pl->cpuCount) + break; + } + return pl; } @@ -340,7 +366,7 @@ static void OpenBSDProcessList_scanCPUTime(OpenBSDProcessList* this) { u_int64_t avg[CPUSTATES] = {0}; for (unsigned int i = 0; i < this->super.cpuCount; i++) { - getKernelCPUTimes(i, kernelTimes); + getKernelCPUTimes(this->cpus[i].cpuIndex, kernelTimes); CPUData* cpu = this->cpus + i + 1; kernelCPUTimesToHtop(kernelTimes, cpu); diff --git a/openbsd/OpenBSDProcessList.h b/openbsd/OpenBSDProcessList.h index 46f53505..0ef6a894 100644 --- a/openbsd/OpenBSDProcessList.h +++ b/openbsd/OpenBSDProcessList.h @@ -35,6 +35,8 @@ typedef struct CPUData_ { unsigned long long int spinPeriod; unsigned long long int intrPeriod; unsigned long long int idlePeriod; + + int cpuIndex; } CPUData; typedef struct OpenBSDProcessList_ {