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 --- pcp/PCPProcessList.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'pcp') diff --git a/pcp/PCPProcessList.c b/pcp/PCPProcessList.c index fccd60d7..a27bd474 100644 --- a/pcp/PCPProcessList.c +++ b/pcp/PCPProcessList.c @@ -33,10 +33,13 @@ static int PCPProcessList_computeCPUcount(void) { static void PCPProcessList_updateCPUcount(PCPProcessList* this) { ProcessList* pl = &(this->super); unsigned int cpus = PCPProcessList_computeCPUcount(); - if (cpus == pl->cpuCount) + if (cpus == pl->existingCPUs) return; - pl->cpuCount = cpus; + pl->existingCPUs = cpus; + // TODO: support offline CPUs and hot swapping + pl->activeCPUs = pl->existingCPUs; + free(this->percpu); free(this->values); @@ -79,7 +82,7 @@ void ProcessList_delete(ProcessList* pl) { PCPProcessList* this = (PCPProcessList*) pl; ProcessList_done(pl); free(this->values); - for (unsigned int i = 0; i < pl->cpuCount; i++) + for (unsigned int i = 0; i < pl->existingCPUs; i++) free(this->percpu[i]); free(this->percpu); free(this->cpu); @@ -371,7 +374,7 @@ static bool PCPProcessList_updateProcesses(PCPProcessList* this, double period, float percent_cpu = (pp->utime + pp->stime - lasttimes) / period * 100.0; proc->percent_cpu = isnan(percent_cpu) ? - 0.0 : CLAMP(percent_cpu, 0.0, pl->cpuCount * 100.0); + 0.0 : CLAMP(percent_cpu, 0.0, pl->activeCPUs * 100.0); proc->percent_mem = proc->m_resident / (double)pl->totalMem * 100.0; PCPProcessList_updateUsername(proc, pid, offset, pl->usersTable); @@ -539,7 +542,7 @@ static void PCPProcessList_updateAllCPUTime(PCPProcessList* this, Metric metric, static void PCPProcessList_updatePerCPUTime(PCPProcessList* this, Metric metric, CPUMetric cpumetric) { - int cpus = this->super.cpuCount; + int cpus = this->super.existingCPUs; if (Metric_values(metric, this->values, cpus, PM_TYPE_U64) == NULL) memset(this->values, 0, cpus * sizeof(pmAtomValue)); for (int i = 0; i < cpus; i++) @@ -548,7 +551,7 @@ static void PCPProcessList_updatePerCPUTime(PCPProcessList* this, Metric metric, static void PCPProcessList_updatePerCPUReal(PCPProcessList* this, Metric metric, CPUMetric cpumetric) { - int cpus = this->super.cpuCount; + int cpus = this->super.existingCPUs; if (Metric_values(metric, this->values, cpus, PM_TYPE_DOUBLE) == NULL) memset(this->values, 0, cpus * sizeof(pmAtomValue)); for (int i = 0; i < cpus; i++) @@ -608,7 +611,7 @@ static void PCPProcessList_updateHeader(ProcessList* super, const Settings* sett PCPProcessList_updateAllCPUTime(this, PCP_CPU_GUEST, CPU_GUEST_TIME); PCPProcessList_deriveCPUTime(this->cpu); - for (unsigned int i = 0; i < super->cpuCount; i++) + for (unsigned int i = 0; i < super->existingCPUs; i++) PCPProcessList_backupCPUTime(this->percpu[i]); PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_USER, CPU_USER_TIME); PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_NICE, CPU_NICE_TIME); @@ -619,7 +622,7 @@ static void PCPProcessList_updateHeader(ProcessList* super, const Settings* sett PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_SOFTIRQ, CPU_SOFTIRQ_TIME); PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_STEAL, CPU_STEAL_TIME); PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_GUEST, CPU_GUEST_TIME); - for (unsigned int i = 0; i < super->cpuCount; i++) + for (unsigned int i = 0; i < super->existingCPUs; i++) PCPProcessList_deriveCPUTime(this->percpu[i]); if (settings->showCPUFrequency) -- 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 --- pcp/PCPProcessList.c | 9 +++++++++ pcp/PCPProcessList.h | 2 ++ 2 files changed, 11 insertions(+) (limited to 'pcp') diff --git a/pcp/PCPProcessList.c b/pcp/PCPProcessList.c index a27bd474..be91ca47 100644 --- a/pcp/PCPProcessList.c +++ b/pcp/PCPProcessList.c @@ -675,3 +675,12 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { double period = (this->timestamp - sample) * 100; PCPProcessList_updateProcesses(this, period, ×tamp); } + +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/pcp/PCPProcessList.h b/pcp/PCPProcessList.h index f1784904..7f0f6fe4 100644 --- a/pcp/PCPProcessList.h +++ b/pcp/PCPProcessList.h @@ -69,4 +69,6 @@ void ProcessList_delete(ProcessList* pl); void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate); +bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id); + #endif -- cgit v1.2.3