From f3147ea2d1598914c2db53e8cfb34c8ff81e2ff4 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Thu, 27 Aug 2020 07:48:10 +0200 Subject: New upstream version 3.0.0 --- openbsd/Battery.c | 7 +- openbsd/Battery.h | 1 - openbsd/OpenBSDCRT.c | 1 - openbsd/OpenBSDProcess.c | 2 +- openbsd/OpenBSDProcessList.c | 147 ++++++++++++++++++++++++++++++++++------ openbsd/OpenBSDProcessList.h | 18 ++++- openbsd/Platform.c | 157 ++++++++++++++++++++----------------------- openbsd/Platform.h | 15 ----- 8 files changed, 221 insertions(+), 127 deletions(-) (limited to 'openbsd') diff --git a/openbsd/Battery.c b/openbsd/Battery.c index 3a0bae1..c215e41 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++) { @@ -34,7 +35,7 @@ void Battery_getData(double* level, ACPresence* isOnAC) { size_t sdlen = sizeof(struct sensordev); bool found = findDevice("acpibat0", mib, &snsrdev, &sdlen); - + *level = -1; if (found) { /* last full capacity */ @@ -57,9 +58,9 @@ void Battery_getData(double* level, ACPresence* isOnAC) { } } } - + found = findDevice("acpiac0", mib, &snsrdev, &sdlen); - + *isOnAC = AC_ERROR; if (found) { mib[3] = 9; diff --git a/openbsd/Battery.h b/openbsd/Battery.h index b1a4982..0f05af3 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/OpenBSDCRT.c b/openbsd/OpenBSDCRT.c index c5dcec4..233d30c 100644 --- a/openbsd/OpenBSDCRT.c +++ b/openbsd/OpenBSDCRT.c @@ -19,4 +19,3 @@ void CRT_handleSIGSEGV(int sgn) { fprintf(stderr, "\nPlease contact your platform package maintainer!\n\n"); abort(); } - diff --git a/openbsd/OpenBSDProcess.c b/openbsd/OpenBSDProcess.c index 70f9653..f54c911 100644 --- a/openbsd/OpenBSDProcess.c +++ b/openbsd/OpenBSDProcess.c @@ -135,7 +135,7 @@ ProcessFieldData Process_fields[] = { .flags = 0, }, [ST_UID] = { .name = "ST_UID", - .title = " UID ", + .title = " UID ", .description = "User ID of the process owner", .flags = 0, }, [PERCENT_CPU] = { diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index 14d9c2d..ea40fe1 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,29 +232,30 @@ 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; bool preExisting; Process* proc; OpenBSDProcess* fp; + struct tm date; + struct timeval tv; 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); + for (i = 0; i < count; i++) { 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)) @@ -251,20 +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; @@ -284,14 +305,98 @@ 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 ba9e6d1..ec9fab2 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 01b6c47..0f5279e 100644 --- a/openbsd/Platform.c +++ b/openbsd/Platform.c @@ -20,9 +20,8 @@ in the source distribution for its full text. #include "OpenBSDProcess.h" #include "OpenBSDProcessList.h" -#include -#include #include +#include #include #include @@ -30,10 +29,13 @@ in the source distribution for its full text. #include #include #include -#include #include #include #include +#include +#include +#include +#include /*{ #include "Action.h" @@ -44,54 +46,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; @@ -197,43 +151,38 @@ 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 99999; } 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; + v[CPU_METER_FREQUENCY] = -1; + 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) { @@ -294,6 +243,48 @@ void Platform_setTasksValues(Meter* this) { } char* Platform_getProcessEnv(pid_t pid) { - // TODO - return NULL; + char errbuf[_POSIX2_LINE_MAX]; + char *env; + char **ptr; + int count; + kvm_t *kt; + struct kinfo_proc *kproc; + size_t capacity = 4096, size = 0; + + if ((kt = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf)) == NULL) + return NULL; + + if ((kproc = kvm_getprocs(kt, KERN_PROC_PID, pid, + sizeof(struct kinfo_proc), &count)) == NULL) {\ + (void) kvm_close(kt); + return NULL; + } + + if ((ptr = kvm_getenvv(kt, kproc, 0)) == NULL) { + (void) kvm_close(kt); + return NULL; + } + + env = xMalloc(capacity); + for (char **p = ptr; *p; p++) { + size_t len = strlen(*p) + 1; + + if (size + len > capacity) { + capacity *= 2; + env = xRealloc(env, capacity); + } + + strlcpy(env + size, *p, len); + size += len; + } + + if (size < 2 || env[size - 1] || env[size - 2]) { + if (size + 2 < capacity) + env = xRealloc(env, capacity + 2); + env[size] = 0; + env[size+1] = 0; + } + + (void) kvm_close(kt); + return env; } diff --git a/openbsd/Platform.h b/openbsd/Platform.h index e0da7b9..56e4c40 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; -- cgit v1.2.3