don't include offline CPUs in summary for OpenBSD

By default, OpenBSD disables SMT (hyperthreading) cpu pseudo-cores.
This can be changed at runtime by setting the hw.smt sysctl so they
may become active later, therefore they are still present in cpu
stat structures but are marked as offline.

As done with native top(1), this drops them from the cpu summary
graphs.
This commit is contained in:
Stuart Henderson 2021-03-27 13:26:26 +00:00 committed by Christian Göttsche
parent d63394b5f6
commit feec16cbb5
2 changed files with 30 additions and 2 deletions

View File

@ -37,9 +37,12 @@ static int pageSize;
static int pageSizeKB; static int pageSizeKB;
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) { 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 }; const int fmib[] = { CTL_KERN, KERN_FSCALE };
int r; int r;
unsigned int cpu_index_c = 0;
unsigned int ncpu;
size_t size; size_t size;
char errbuf[_POSIX2_LINE_MAX]; 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)); 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); size = sizeof(fscale);
if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) { if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) {
CRT_fatalError("fscale sysctl call failed"); CRT_fatalError("fscale sysctl call failed");
@ -76,6 +85,23 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, ui
opl->cpuSpeed = -1; 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; return pl;
} }
@ -340,7 +366,7 @@ static void OpenBSDProcessList_scanCPUTime(OpenBSDProcessList* this) {
u_int64_t avg[CPUSTATES] = {0}; u_int64_t avg[CPUSTATES] = {0};
for (unsigned int i = 0; i < this->super.cpuCount; i++) { 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; CPUData* cpu = this->cpus + i + 1;
kernelCPUTimesToHtop(kernelTimes, cpu); kernelCPUTimesToHtop(kernelTimes, cpu);

View File

@ -35,6 +35,8 @@ typedef struct CPUData_ {
unsigned long long int spinPeriod; unsigned long long int spinPeriod;
unsigned long long int intrPeriod; unsigned long long int intrPeriod;
unsigned long long int idlePeriod; unsigned long long int idlePeriod;
int cpuIndex;
} CPUData; } CPUData;
typedef struct OpenBSDProcessList_ { typedef struct OpenBSDProcessList_ {