From 41af31be7ffbd34518b27aad56a4f54af6a8adf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sat, 12 Jun 2021 18:17:28 +0200 Subject: Rework CPU counting Currently htop does not support offline CPUs and hot-swapping, e.g. via echo 0 > /sys/devices/system/cpu/cpu2/online Split the current single cpuCount variable into activeCPUs and existingCPUs. Supersedes: #650 Related: #580 --- darwin/DarwinProcessList.c | 8 +++++--- darwin/Platform.c | 12 ++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) (limited to 'darwin') diff --git a/darwin/DarwinProcessList.c b/darwin/DarwinProcessList.c index 91d62c4a..e58c3496 100644 --- a/darwin/DarwinProcessList.c +++ b/darwin/DarwinProcessList.c @@ -134,7 +134,9 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, H ProcessList_init(&this->super, Class(DarwinProcess), usersTable, dynamicMeters, pidMatchList, userId); /* Initialize the CPU information */ - this->super.cpuCount = ProcessList_allocateCPULoadInfo(&this->prev_load); + this->super.activeCPUs = ProcessList_allocateCPULoadInfo(&this->prev_load); + // TODO: support offline CPUs and hot swapping + this->super.existingCPUs = this->super.activeCPUs; ProcessList_getHostInfo(&this->host_info); ProcessList_allocateCPULoadInfo(&this->curr_load); @@ -184,13 +186,13 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { /* Get the time difference */ dpl->global_diff = 0; - for (unsigned int i = 0; i < dpl->super.cpuCount; ++i) { + for (unsigned int i = 0; i < dpl->super.existingCPUs; ++i) { for (size_t j = 0; j < CPU_STATE_MAX; ++j) { dpl->global_diff += dpl->curr_load[i].cpu_ticks[j] - dpl->prev_load[i].cpu_ticks[j]; } } - const double time_interval = ticksToNanoseconds(dpl->global_diff) / (double) dpl->super.cpuCount; + const double time_interval = ticksToNanoseconds(dpl->global_diff) / (double) dpl->super.activeCPUs; /* Clear the thread counts */ super->kernelThreads = 0; diff --git a/darwin/Platform.c b/darwin/Platform.c index 3fcf314d..e110f72b 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -186,21 +186,21 @@ int Platform_getMaxPid() { static double Platform_setCPUAverageValues(Meter* mtr) { const ProcessList* dpl = mtr->pl; - unsigned int cpus = dpl->cpuCount; + unsigned int activeCPUs = dpl->activeCPUs; double sumNice = 0.0; double sumNormal = 0.0; double sumKernel = 0.0; double sumPercent = 0.0; - for (unsigned int i = 1; i <= cpus; i++) { + for (unsigned int i = 1; i <= dpl->existingCPUs; i++) { sumPercent += Platform_setCPUValues(mtr, i); sumNice += mtr->values[CPU_METER_NICE]; sumNormal += mtr->values[CPU_METER_NORMAL]; sumKernel += mtr->values[CPU_METER_KERNEL]; } - mtr->values[CPU_METER_NICE] = sumNice / cpus; - mtr->values[CPU_METER_NORMAL] = sumNormal / cpus; - mtr->values[CPU_METER_KERNEL] = sumKernel / cpus; - return sumPercent / cpus; + mtr->values[CPU_METER_NICE] = sumNice / activeCPUs; + mtr->values[CPU_METER_NORMAL] = sumNormal / activeCPUs; + mtr->values[CPU_METER_KERNEL] = sumKernel / activeCPUs; + return sumPercent / activeCPUs; } double Platform_setCPUValues(Meter* mtr, unsigned int cpu) { -- cgit v1.2.3 From 11d2206f40dd1680923ccae6e421a494c2af0992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sat, 12 Jun 2021 22:04:37 +0200 Subject: Add ProcessList_isCPUonline --- darwin/DarwinProcessList.c | 9 +++++++++ darwin/DarwinProcessList.h | 2 ++ 2 files changed, 11 insertions(+) (limited to 'darwin') diff --git a/darwin/DarwinProcessList.c b/darwin/DarwinProcessList.c index e58c3496..8d14efe1 100644 --- a/darwin/DarwinProcessList.c +++ b/darwin/DarwinProcessList.c @@ -236,3 +236,12 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { free(ps); } + +bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id) { + assert(id < super->existingCPUs); + + // TODO: support offline CPUs and hot swapping + (void) super; (void) id; + + return true; +} diff --git a/darwin/DarwinProcessList.h b/darwin/DarwinProcessList.h index bd39ab0e..24259d3e 100644 --- a/darwin/DarwinProcessList.h +++ b/darwin/DarwinProcessList.h @@ -34,4 +34,6 @@ void ProcessList_delete(ProcessList* this); void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate); +bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id); + #endif -- cgit v1.2.3