From 9197adf57e04875fe7fd5b768bc5201d5def2548 Mon Sep 17 00:00:00 2001 From: Antoine Motet Date: Sun, 16 Dec 2018 09:25:54 +0100 Subject: [PATCH] Fix CPU usage on OpenBSD MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current OpenBSD-specific CPU usage code is broken. The `cpu` parameter of `Platform_setCPUValues` is an integer in the interval [0, cpuCount], not [0, cpuCount-1]: Actual CPUs are numbered from 1, the “zero” CPU is a “virtual” one which represents the average of actual CPUs (I guess it’s inherited from Linux’s `/proc/stats`). This off-by-one error leads to random crashes. Moreover, the displayed CPU usage is more detailed with system, user and nice times. I made the OpenBSD CPU code more similar to the Linux CPU code, removing a few old bits from OpenBSD’s top(1). I think it will be easier to understand, maintain and evolve. I’d love some feedback from experienced OpenBSD people. --- openbsd/Battery.c | 1 + openbsd/Battery.h | 1 - openbsd/OpenBSDProcessList.c | 142 +++++++++++++++++++++++++++++------ openbsd/OpenBSDProcessList.h | 18 ++++- openbsd/Platform.c | 103 ++++++------------------- openbsd/Platform.h | 15 ---- 6 files changed, 163 insertions(+), 117 deletions(-) diff --git a/openbsd/Battery.c b/openbsd/Battery.c index 3a0bae14..80511dc7 100644 --- a/openbsd/Battery.c +++ b/openbsd/Battery.c @@ -10,6 +10,7 @@ in the source distribution for its full text. #include #include #include +#include static bool findDevice(const char* name, int* mib, struct sensordev* snsrdev, size_t* sdlen) { for (int devn = 0;; devn++) { diff --git a/openbsd/Battery.h b/openbsd/Battery.h index b1a4982e..0f05af3a 100644 --- a/openbsd/Battery.h +++ b/openbsd/Battery.h @@ -12,5 +12,4 @@ in the source distribution for its full text. void Battery_getData(double* level, ACPresence* isOnAC); - #endif diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index e49cbd71..2d8951c3 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -6,6 +6,7 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ +#include "CRT.h" #include "ProcessList.h" #include "OpenBSDProcessList.h" #include "OpenBSDProcess.h" @@ -17,6 +18,7 @@ in the source distribution for its full text. #include #include #include +#include #include #include #include @@ -31,7 +33,22 @@ in the source distribution for its full text. typedef struct CPUData_ { unsigned long long int totalTime; + unsigned long long int userTime; + unsigned long long int niceTime; + unsigned long long int sysTime; + unsigned long long int sysAllTime; + unsigned long long int spinTime; + unsigned long long int intrTime; + unsigned long long int idleTime; + unsigned long long int totalPeriod; + unsigned long long int userPeriod; + unsigned long long int nicePeriod; + unsigned long long int sysPeriod; + unsigned long long int sysAllPeriod; + unsigned long long int spinPeriod; + unsigned long long int intrPeriod; + unsigned long long int idlePeriod; } CPUData; typedef struct OpenBSDProcessList_ { @@ -79,16 +96,17 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui if (e == -1 || pl->cpuCount < 1) { pl->cpuCount = 1; } - opl->cpus = xRealloc(opl->cpus, pl->cpuCount * sizeof(CPUData)); + opl->cpus = xCalloc(pl->cpuCount + 1, sizeof(CPUData)); size = sizeof(fscale); if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) { err(1, "fscale sysctl call failed"); } - for (i = 0; i < pl->cpuCount; i++) { - opl->cpus[i].totalTime = 1; - opl->cpus[i].totalPeriod = 1; + for (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); @@ -205,7 +223,7 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in /* * Taken from OpenBSD's ps(1). */ -double getpcpu(const struct kinfo_proc *kp) { +static double getpcpu(const struct kinfo_proc *kp) { if (fscale == 0) return (0.0); @@ -214,9 +232,8 @@ double getpcpu(const struct kinfo_proc *kp) { return (100.0 * fxtofl(kp->p_pctcpu)); } -void ProcessList_goThroughEntries(ProcessList* this) { - OpenBSDProcessList* opl = (OpenBSDProcessList*) this; - Settings* settings = this->settings; +static inline void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { + Settings* settings = this->super.settings; bool hideKernelThreads = settings->hideKernelThreads; bool hideUserlandThreads = settings->hideUserlandThreads; struct kinfo_proc* kproc; @@ -228,10 +245,8 @@ void ProcessList_goThroughEntries(ProcessList* this) { int count = 0; int i; - OpenBSDProcessList_scanMemoryInfo(this); - // use KERN_PROC_KTHREAD to also include kernel threads - struct kinfo_proc* kprocs = kvm_getprocs(opl->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count); + struct kinfo_proc* kprocs = kvm_getprocs(this->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count); //struct kinfo_proc* kprocs = getprocs(KERN_PROC_ALL, 0, &count); gettimeofday(&tv, NULL); @@ -240,7 +255,7 @@ void ProcessList_goThroughEntries(ProcessList* this) { kproc = &kprocs[i]; preExisting = false; - proc = ProcessList_getProcess(this, kproc->p_pid, &preExisting, (Process_New) OpenBSDProcess_new); + proc = ProcessList_getProcess(&this->super, kproc->p_pid, &preExisting, (Process_New) OpenBSDProcess_new); fp = (OpenBSDProcess*) proc; proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) @@ -255,22 +270,22 @@ void ProcessList_goThroughEntries(ProcessList* this) { proc->pgrp = kproc->p__pgid; proc->st_uid = kproc->p_uid; proc->starttime_ctime = kproc->p_ustart_sec; - proc->user = UsersTable_getRef(this->usersTable, proc->st_uid); - ProcessList_add((ProcessList*)this, proc); - proc->comm = OpenBSDProcessList_readProcessName(opl->kd, kproc, &proc->basenameOffset); + proc->user = UsersTable_getRef(this->super.usersTable, proc->st_uid); + ProcessList_add(&this->super, proc); + proc->comm = OpenBSDProcessList_readProcessName(this->kd, kproc, &proc->basenameOffset); (void) localtime_r((time_t*) &kproc->p_ustart_sec, &date); strftime(proc->starttime_show, 7, ((proc->starttime_ctime > tv.tv_sec - 86400) ? "%R " : "%b%d "), &date); } else { if (settings->updateProcessNames) { free(proc->comm); - proc->comm = OpenBSDProcessList_readProcessName(opl->kd, kproc, &proc->basenameOffset); + proc->comm = OpenBSDProcessList_readProcessName(this->kd, kproc, &proc->basenameOffset); } } proc->m_size = kproc->p_vm_dsize; proc->m_resident = kproc->p_vm_rssize; - proc->percent_mem = (proc->m_resident * PAGE_SIZE_KB) / (double)(this->totalMem) * 100.0; - proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0, this->cpuCount*100.0); + proc->percent_mem = (proc->m_resident * PAGE_SIZE_KB) / (double)(this->super.totalMem) * 100.0; + proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0, this->super.cpuCount*100.0); //proc->nlwp = kproc->p_numthreads; //proc->time = kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 10); proc->nice = kproc->p_nice - 20; @@ -290,14 +305,99 @@ void ProcessList_goThroughEntries(ProcessList* this) { } if (Process_isKernelThread(proc)) { - this->kernelThreads++; + this->super.kernelThreads++; } - this->totalTasks++; + this->super.totalTasks++; // SRUN ('R') means runnable, not running if (proc->state == 'P') { - this->runningTasks++; + this->super.runningTasks++; } proc->updated = true; } } + +static unsigned long long saturatingSub(unsigned long long a, unsigned long long b) { + return a > b ? a - b : 0; +} + +static void getKernelCPUTimes(int cpuId, u_int64_t* times) { + int mib[] = { CTL_KERN, KERN_CPTIME2, cpuId }; + size_t length = sizeof(u_int64_t) * CPUSTATES; + if (sysctl(mib, 3, times, &length, NULL, 0) == -1 || + length != sizeof(u_int64_t) * CPUSTATES) { + CRT_fatalError("sysctl kern.cp_time2 failed"); + } +} + +static void kernelCPUTimesToHtop(const u_int64_t* times, CPUData* cpu) { + unsigned long long totalTime = 0; + for (int i = 0; i < CPUSTATES; i++) { + totalTime += times[i]; + } + + unsigned long long sysAllTime = times[CP_INTR] + times[CP_SYS]; + + // XXX Not sure if CP_SPIN should be added to sysAllTime. + // See https://github.com/openbsd/src/commit/531d8034253fb82282f0f353c086e9ad827e031c + #ifdef CP_SPIN + sysAllTime += times[CP_SPIN]; + #endif + + cpu->totalPeriod = saturatingSub(totalTime, cpu->totalTime); + cpu->userPeriod = saturatingSub(times[CP_USER], cpu->userTime); + cpu->nicePeriod = saturatingSub(times[CP_NICE], cpu->niceTime); + cpu->sysPeriod = saturatingSub(times[CP_SYS], cpu->sysTime); + cpu->sysAllPeriod = saturatingSub(sysAllTime, cpu->sysAllTime); + #ifdef CP_SPIN + cpu->spinPeriod = saturatingSub(times[CP_SPIN], cpu->spinTime); + #endif + cpu->intrPeriod = saturatingSub(times[CP_INTR], cpu->intrTime); + cpu->idlePeriod = saturatingSub(times[CP_IDLE], cpu->idleTime); + + cpu->totalTime = totalTime; + cpu->userTime = times[CP_USER]; + cpu->niceTime = times[CP_NICE]; + cpu->sysTime = times[CP_SYS]; + cpu->sysAllTime = sysAllTime; + #ifdef CP_SPIN + cpu->spinTime = times[CP_SPIN]; + #endif + cpu->intrTime = times[CP_INTR]; + cpu->idleTime = times[CP_IDLE]; +} + +static void OpenBSDProcessList_scanCPUTime(OpenBSDProcessList* this) { + u_int64_t kernelTimes[CPUSTATES] = {0}; + u_int64_t avg[CPUSTATES] = {0}; + + for (int i = 0; i < this->super.cpuCount; i++) { + getKernelCPUTimes(i, kernelTimes); + CPUData* cpu = this->cpus + i + 1; + kernelCPUTimesToHtop(kernelTimes, cpu); + + avg[CP_USER] += cpu->userTime; + avg[CP_NICE] += cpu->niceTime; + avg[CP_SYS] += cpu->sysTime; + #ifdef CP_SPIN + avg[CP_SPIN] += cpu->spinTime; + #endif + avg[CP_INTR] += cpu->intrTime; + avg[CP_IDLE] += cpu->idleTime; + } + + for (int i = 0; i < CPUSTATES; i++) { + avg[i] /= this->super.cpuCount; + } + + kernelCPUTimesToHtop(avg, this->cpus); +} + +void ProcessList_goThroughEntries(ProcessList* this) { + OpenBSDProcessList* opl = (OpenBSDProcessList*) this; + + OpenBSDProcessList_scanMemoryInfo(this); + OpenBSDProcessList_scanProcs(opl); + OpenBSDProcessList_scanCPUTime(opl); +} + diff --git a/openbsd/OpenBSDProcessList.h b/openbsd/OpenBSDProcessList.h index ba9e6d14..ec9fab27 100644 --- a/openbsd/OpenBSDProcessList.h +++ b/openbsd/OpenBSDProcessList.h @@ -15,7 +15,22 @@ in the source distribution for its full text. typedef struct CPUData_ { unsigned long long int totalTime; + unsigned long long int userTime; + unsigned long long int niceTime; + unsigned long long int sysTime; + unsigned long long int sysAllTime; + unsigned long long int spinTime; + unsigned long long int intrTime; + unsigned long long int idleTime; + unsigned long long int totalPeriod; + unsigned long long int userPeriod; + unsigned long long int nicePeriod; + unsigned long long int sysPeriod; + unsigned long long int sysAllPeriod; + unsigned long long int spinPeriod; + unsigned long long int intrPeriod; + unsigned long long int idlePeriod; } CPUData; typedef struct OpenBSDProcessList_ { @@ -51,8 +66,7 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in /* * Taken from OpenBSD's ps(1). */ -double getpcpu(const struct kinfo_proc *kp); - void ProcessList_goThroughEntries(ProcessList* this); + #endif diff --git a/openbsd/Platform.c b/openbsd/Platform.c index 4bb2e35e..7e117a76 100644 --- a/openbsd/Platform.c +++ b/openbsd/Platform.c @@ -38,6 +38,7 @@ in the source distribution for its full text. #include #include #include +#include /*{ #include "Action.h" @@ -48,54 +49,6 @@ extern ProcessFieldData Process_fields[]; }*/ -#define MAXCPU 256 -// XXX: probably should be a struct member -static int64_t old_v[MAXCPU][5]; - -/* - * Copyright (c) 1984, 1989, William LeFebvre, Rice University - * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University - * - * Taken directly from OpenBSD's top(1). - * - * percentages(cnt, out, new, old, diffs) - calculate percentage change - * between array "old" and "new", putting the percentages in "out". - * "cnt" is size of each array and "diffs" is used for scratch space. - * The array "old" is updated on each call. - * The routine assumes modulo arithmetic. This function is especially - * useful on BSD machines for calculating cpu state percentages. - */ -static int percentages(int cnt, int64_t *out, int64_t *new, int64_t *old, int64_t *diffs) { - int64_t change, total_change, *dp, half_total; - int i; - - /* initialization */ - total_change = 0; - dp = diffs; - - /* calculate changes for each state and the overall change */ - for (i = 0; i < cnt; i++) { - if ((change = *new - *old) < 0) { - /* this only happens when the counter wraps */ - change = INT64_MAX - *old + *new; - } - total_change += (*dp++ = change); - *old++ = *new++; - } - - /* avoid divide by zero potential */ - if (total_change == 0) - total_change = 1; - - /* calculate percentages based on overall change, rounding up */ - half_total = total_change / 2l; - for (i = 0; i < cnt; i++) - *out++ = ((*diffs++ * 1000 + half_total) / total_change); - - /* return the total in case the caller wants to use it */ - return (total_change); -} - ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; int Platform_numberOfFields = LAST_PROCESSFIELD; @@ -201,43 +154,37 @@ void Platform_getLoadAverage(double* one, double* five, double* fifteen) { int Platform_getMaxPid() { // this is hard-coded in sys/sys/proc.h - no sysctl exists - return 32766; + return 32766; // XXX Seems changed to 99999, what about using PID_MAX? } double Platform_setCPUValues(Meter* this, int cpu) { - int i; - double perc; - - OpenBSDProcessList* pl = (OpenBSDProcessList*) this->pl; - CPUData* cpuData = &(pl->cpus[cpu]); - int64_t new_v[CPUSTATES], diff_v[CPUSTATES], scratch_v[CPUSTATES]; + const OpenBSDProcessList* pl = (OpenBSDProcessList*) this->pl; + const CPUData* cpuData = &(pl->cpus[cpu]); + double total = cpuData->totalPeriod == 0 ? 1 : cpuData->totalPeriod; + double totalPercent; double *v = this->values; - size_t size = sizeof(double) * CPUSTATES; - int mib[] = { CTL_KERN, KERN_CPTIME2, cpu-1 }; - if (sysctl(mib, 3, new_v, &size, NULL, 0) == -1) { - return 0.; - } - // XXX: why? - cpuData->totalPeriod = 1; - - percentages(CPUSTATES, diff_v, new_v, - (int64_t *)old_v[cpu-1], scratch_v); - - for (i = 0; i < CPUSTATES; i++) { - old_v[cpu-1][i] = new_v[i]; - v[i] = diff_v[i] / 10.; - } - - Meter_setItems(this, 4); - - perc = v[0] + v[1] + v[2] + v[3]; - - if (perc <= 100. && perc >= 0.) { - return perc; + v[CPU_METER_NICE] = cpuData->nicePeriod / total * 100.0; + v[CPU_METER_NORMAL] = cpuData->userPeriod / total * 100.0; + if (this->pl->settings->detailedCPUTime) { + v[CPU_METER_KERNEL] = cpuData->sysPeriod / total * 100.0; + v[CPU_METER_IRQ] = cpuData->intrPeriod / total * 100.0; + v[CPU_METER_SOFTIRQ] = 0.0; + v[CPU_METER_STEAL] = 0.0; + v[CPU_METER_GUEST] = 0.0; + v[CPU_METER_IOWAIT] = 0.0; + Meter_setItems(this, 8); + totalPercent = v[0]+v[1]+v[2]+v[3]; } else { - return 0.; + v[2] = cpuData->sysAllPeriod / total * 100.0; + v[3] = 0.0; // No steal nor guest on OpenBSD + totalPercent = v[0]+v[1]+v[2]; + Meter_setItems(this, 4); } + + totalPercent = CLAMP(totalPercent, 0.0, 100.0); + if (isnan(totalPercent)) totalPercent = 0.0; + return totalPercent; } void Platform_setMemoryValues(Meter* this) { diff --git a/openbsd/Platform.h b/openbsd/Platform.h index e0da7b9f..56e4c400 100644 --- a/openbsd/Platform.h +++ b/openbsd/Platform.h @@ -17,21 +17,6 @@ in the source distribution for its full text. extern ProcessFieldData Process_fields[]; -#define MAXCPU 256 -// XXX: probably should be a struct member -/* - * Copyright (c) 1984, 1989, William LeFebvre, Rice University - * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University - * - * Taken directly from OpenBSD's top(1). - * - * percentages(cnt, out, new, old, diffs) - calculate percentage change - * between array "old" and "new", putting the percentages in "out". - * "cnt" is size of each array and "diffs" is used for scratch space. - * The array "old" is updated on each call. - * The routine assumes modulo arithmetic. This function is especially - * useful on BSD machines for calculating cpu state percentages. - */ extern ProcessField Platform_defaultFields[]; extern int Platform_numberOfFields;