diff options
author | Daniel Lange <DLange@git.local> | 2021-08-02 15:21:07 +0200 |
---|---|---|
committer | Daniel Lange <DLange@git.local> | 2021-08-02 15:21:07 +0200 |
commit | ed82ce6456f0f904a0ab2b346b85d7cf46df109c (patch) | |
tree | 1b02b20548da6dbcdf53872c3c99a3c013588fd1 /openbsd | |
parent | e341217fea844e1c45b7079146b9de7cb68e8c2a (diff) | |
parent | edf236f9fcf257c5266cead0c3bc45087d937e65 (diff) |
Merge branch 'cpu_count' of cgzones/htop
Diffstat (limited to 'openbsd')
-rw-r--r-- | openbsd/OpenBSDProcessList.c | 132 | ||||
-rw-r--r-- | openbsd/OpenBSDProcessList.h | 6 | ||||
-rw-r--r-- | openbsd/Platform.c | 11 |
3 files changed, 95 insertions, 54 deletions
diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index 76b2aeb7..089ca21d 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -36,13 +36,66 @@ static long fscale; static int pageSize; static int pageSizeKB; -ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, Hashtable* pidMatchList, uid_t userId) { +static void OpenBSDProcessList_updateCPUcount(ProcessList* super) { + OpenBSDProcessList* opl = (OpenBSDProcessList*) super; 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; + unsigned int value; + size_t size; + bool change = false; + + size = sizeof(value); + r = sysctl(mib, 2, &value, &size, NULL, 0); + if (r < 0 || value < 1) { + value = 1; + } + + if (value != super->activeCPUs) { + super->activeCPUs = value; + change = true; + } + + size = sizeof(value); + r = sysctl(nmib, 2, &value, &size, NULL, 0); + if (r < 0 || value < 1) { + value = super->activeCPUs; + } + + if (value != super->existingCPUs) { + opl->cpuData = xReallocArray(opl->cpuData, value + 1, sizeof(CPUData)); + super->existingCPUs = value; + change = true; + } + + if (change) { + CPUData* dAvg = &opl->cpuData[0]; + memset(dAvg, '\0', sizeof(CPUData)); + dAvg->totalTime = 1; + dAvg->totalPeriod = 1; + dAvg->online = true; + + for (unsigned int i = 0; i < super->existingCPUs; i++) { + CPUData* d = &opl->cpuData[i + 1]; + memset(d, '\0', sizeof(CPUData)); + d->totalTime = 1; + d->totalPeriod = 1; + + 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"); + } + d->online = (cpu_stats.cs_flags & CPUSTATS_ONLINE); + } + } +} + + +ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, Hashtable* pidMatchList, uid_t userId) { + const int fmib[] = { CTL_KERN, KERN_FSCALE }; size_t size; char errbuf[_POSIX2_LINE_MAX]; @@ -50,18 +103,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, H ProcessList* pl = (ProcessList*) opl; ProcessList_init(pl, Class(OpenBSDProcess), usersTable, dynamicMeters, pidMatchList, userId); - size = sizeof(pl->cpuCount); - r = sysctl(mib, 2, &pl->cpuCount, &size, NULL, 0); - if (r < 0 || pl->cpuCount < 1) { - pl->cpuCount = 1; - } - 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; - } + OpenBSDProcessList_updateCPUcount(pl); size = sizeof(fscale); if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) { @@ -72,12 +114,6 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, H CRT_fatalError("pagesize sysconf call failed"); pageSizeKB = pageSize / ONE_K; - for (unsigned int i = 0; i <= pl->cpuCount; i++) { - CPUData* d = opl->cpus + i; - d->totalTime = 1; - d->totalPeriod = 1; - } - opl->kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); if (opl->kd == NULL) { CRT_fatalError("kvm_openfiles() failed"); @@ -85,23 +121,6 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, H 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; } @@ -112,7 +131,7 @@ void ProcessList_delete(ProcessList* this) { kvm_close(opl->kd); } - free(opl->cpus); + free(opl->cpuData); ProcessList_done(this); free(this); @@ -172,7 +191,7 @@ static void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { } static void OpenBSDProcessList_updateCwd(const struct kinfo_proc* kproc, Process* proc) { - const int mib[] = { CTL_KERN, KERN_PROC_CWD, kproc->ki_pid }; + const int mib[] = { CTL_KERN, KERN_PROC_CWD, kproc->p_pid }; char buffer[2048]; size_t size = sizeof(buffer); if (sysctl(mib, 3, buffer, &size, NULL, 0) != 0) { @@ -274,8 +293,6 @@ static void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { Process* proc = ProcessList_getProcess(&this->super, (kproc->p_tid == -1) ? kproc->p_pid : kproc->p_tid, &preExisting, OpenBSDProcess_new); OpenBSDProcess* fp = (OpenBSDProcess*) proc; - proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc))); - if (!preExisting) { proc->ppid = kproc->p_ppid; proc->tpgid = kproc->p_tpgid; @@ -312,7 +329,7 @@ static void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { proc->m_virt = kproc->p_vm_dsize * pageSizeKB; proc->m_resident = kproc->p_vm_rssize * pageSizeKB; proc->percent_mem = proc->m_resident / (float)this->super.totalMem * 100.0F; - proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0F, this->super.cpuCount * 100.0F); + proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0F, this->super.activeCPUs * 100.0F); proc->nice = kproc->p_nice - 20; proc->time = 100 * (kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 1000000)); proc->priority = kproc->p_priority - PZERO; @@ -347,11 +364,13 @@ static void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { if (proc->state == 'R') { this->super.runningTasks++; } + + proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc))); proc->updated = true; } } -static void getKernelCPUTimes(int cpuId, u_int64_t* times) { +static void getKernelCPUTimes(unsigned int cpuId, u_int64_t* times) { const int mib[] = { CTL_KERN, KERN_CPTIME2, cpuId }; size_t length = sizeof(*times) * CPUSTATES; if (sysctl(mib, 3, times, &length, NULL, 0) == -1 || length != sizeof(*times) * CPUSTATES) { @@ -400,9 +419,14 @@ static void OpenBSDProcessList_scanCPUTime(OpenBSDProcessList* this) { u_int64_t kernelTimes[CPUSTATES] = {0}; u_int64_t avg[CPUSTATES] = {0}; - for (unsigned int i = 0; i < this->super.cpuCount; i++) { - getKernelCPUTimes(this->cpus[i].cpuIndex, kernelTimes); - CPUData* cpu = this->cpus + i + 1; + for (unsigned int i = 0; i < this->super.existingCPUs; i++) { + CPUData* cpu = &this->cpuData[i + 1]; + + if (!cpu->online) { + continue; + } + + getKernelCPUTimes(i, kernelTimes); kernelCPUTimesToHtop(kernelTimes, cpu); avg[CP_USER] += cpu->userTime; @@ -416,10 +440,10 @@ static void OpenBSDProcessList_scanCPUTime(OpenBSDProcessList* this) { } for (int i = 0; i < CPUSTATES; i++) { - avg[i] /= this->super.cpuCount; + avg[i] /= this->super.activeCPUs; } - kernelCPUTimesToHtop(avg, this->cpus); + kernelCPUTimesToHtop(avg, &this->cpuData[0]); { const int mib[] = { CTL_HW, HW_CPUSPEED }; @@ -436,6 +460,7 @@ static void OpenBSDProcessList_scanCPUTime(OpenBSDProcessList* this) { void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { OpenBSDProcessList* opl = (OpenBSDProcessList*) super; + OpenBSDProcessList_updateCPUcount(super); OpenBSDProcessList_scanMemoryInfo(super); OpenBSDProcessList_scanCPUTime(opl); @@ -446,3 +471,10 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { OpenBSDProcessList_scanProcs(opl); } + +bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id) { + assert(id < super->existingCPUs); + + const OpenBSDProcessList* opl = (const OpenBSDProcessList*) super; + return opl->cpuData[id + 1].online; +} diff --git a/openbsd/OpenBSDProcessList.h b/openbsd/OpenBSDProcessList.h index 5734d3ab..0a47773f 100644 --- a/openbsd/OpenBSDProcessList.h +++ b/openbsd/OpenBSDProcessList.h @@ -36,14 +36,14 @@ typedef struct CPUData_ { unsigned long long int intrPeriod; unsigned long long int idlePeriod; - int cpuIndex; + bool online; } CPUData; typedef struct OpenBSDProcessList_ { ProcessList super; kvm_t* kd; - CPUData* cpus; + CPUData* cpuData; int cpuSpeed; } OpenBSDProcessList; @@ -55,4 +55,6 @@ void ProcessList_delete(ProcessList* this); void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate); +bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id); + #endif diff --git a/openbsd/Platform.c b/openbsd/Platform.c index 1afbdfde..65133122 100644 --- a/openbsd/Platform.c +++ b/openbsd/Platform.c @@ -169,11 +169,18 @@ int Platform_getMaxPid() { double Platform_setCPUValues(Meter* this, unsigned int cpu) { const OpenBSDProcessList* pl = (const OpenBSDProcessList*) this->pl; - const CPUData* cpuData = &(pl->cpus[cpu]); - double total = cpuData->totalPeriod == 0 ? 1 : cpuData->totalPeriod; + const CPUData* cpuData = &(pl->cpuData[cpu]); + double total; double totalPercent; double* v = this->values; + if (!cpuData->online) { + this->curItems = 0; + return NAN; + } + + total = cpuData->totalPeriod == 0 ? 1 : cpuData->totalPeriod; + v[CPU_METER_NICE] = cpuData->nicePeriod / total * 100.0; v[CPU_METER_NORMAL] = cpuData->userPeriod / total * 100.0; if (this->pl->settings->detailedCPUTime) { |