Fix CPU usage on OpenBSD

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.
This commit is contained in:
Antoine Motet
2018-12-16 09:25:54 +01:00
parent b7b4200f85
commit 9197adf57e
6 changed files with 163 additions and 117 deletions

View File

@ -38,6 +38,7 @@ in the source distribution for its full text.
#include <fcntl.h>
#include <kvm.h>
#include <limits.h>
#include <math.h>
/*{
#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) {