2015-09-18 04:46:48 +00:00
|
|
|
|
/*
|
|
|
|
|
htop - OpenBSDProcessList.c
|
|
|
|
|
(C) 2014 Hisham H. Muhammad
|
|
|
|
|
(C) 2015 Michael McConville
|
|
|
|
|
Released under the GNU GPL, see the COPYING file
|
|
|
|
|
in the source distribution for its full text.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-12-16 08:25:54 +00:00
|
|
|
|
#include "CRT.h"
|
2015-09-18 04:46:48 +00:00
|
|
|
|
#include "ProcessList.h"
|
|
|
|
|
#include "OpenBSDProcessList.h"
|
|
|
|
|
#include "OpenBSDProcess.h"
|
|
|
|
|
|
2016-01-02 16:57:53 +00:00
|
|
|
|
#include <err.h>
|
2015-09-18 04:46:48 +00:00
|
|
|
|
#include <errno.h>
|
2016-02-12 00:28:22 +00:00
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/mount.h>
|
2015-09-18 04:46:48 +00:00
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
#include <sys/proc.h>
|
2016-02-12 00:28:22 +00:00
|
|
|
|
#include <sys/resource.h>
|
2018-12-16 08:25:54 +00:00
|
|
|
|
#include <sys/sched.h>
|
2015-09-18 04:46:48 +00:00
|
|
|
|
#include <sys/sysctl.h>
|
2016-02-12 00:28:22 +00:00
|
|
|
|
#include <sys/types.h>
|
2015-09-18 04:46:48 +00:00
|
|
|
|
#include <sys/user.h>
|
2016-03-06 04:23:29 +00:00
|
|
|
|
#include <limits.h>
|
2016-02-12 00:28:22 +00:00
|
|
|
|
#include <stdlib.h>
|
2015-09-18 04:46:48 +00:00
|
|
|
|
#include <string.h>
|
2016-02-12 00:28:22 +00:00
|
|
|
|
#include <unistd.h>
|
2015-09-18 04:46:48 +00:00
|
|
|
|
|
2016-03-06 04:23:29 +00:00
|
|
|
|
/*
|
|
|
|
|
* avoid relying on or conflicting with MIN() and MAX() in sys/param.h
|
|
|
|
|
*/
|
|
|
|
|
#ifndef MINIMUM
|
|
|
|
|
#define MINIMUM(x, y) ((x) > (y) ? (y) : (x))
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef MAXIMUM
|
|
|
|
|
#define MAXIMUM(x, y) ((x) > (y) ? (x) : (y))
|
|
|
|
|
#endif
|
|
|
|
|
|
Introduce CLAMP macro. Unify all MIN(MAX(a,b),c) uses.
With the CLAMP macro replacing the combination of MIN and MAX, we will
have at least two advantages:
1. It's more obvious semantically.
2. There are no more mixes of confusing uses like MIN(MAX(a,b),c) and
MAX(MIN(a,b),c) and MIN(a,MAX(b,c)) appearing everywhere. We unify
the 'clamping' with a single macro.
Note that the behavior of this CLAMP macro is different from
the combination `MAX(low,MIN(x,high))`.
* This CLAMP macro expands to two comparisons instead of three from
MAX and MIN combination. In theory, this makes the code slightly
smaller, in case that (low) or (high) or both are computed at
runtime, so that compilers cannot optimize them. (The third
comparison will matter if (low)>(high); see below.)
* CLAMP has a side effect, that if (low)>(high) it will produce weird
results. Unlike MIN & MAX which will force either (low) or (high) to
win. No assertion of ((low)<=(high)) is done in this macro, for now.
This CLAMP macro is implemented like described in glib
<http://developer.gnome.org/glib/stable/glib-Standard-Macros.html>
and does not handle weird uses like CLAMP(a++, low++, high--) .
2016-01-15 12:26:01 +00:00
|
|
|
|
#ifndef CLAMP
|
2016-03-06 04:23:29 +00:00
|
|
|
|
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : MAXIMUM(x, low))
|
Introduce CLAMP macro. Unify all MIN(MAX(a,b),c) uses.
With the CLAMP macro replacing the combination of MIN and MAX, we will
have at least two advantages:
1. It's more obvious semantically.
2. There are no more mixes of confusing uses like MIN(MAX(a,b),c) and
MAX(MIN(a,b),c) and MIN(a,MAX(b,c)) appearing everywhere. We unify
the 'clamping' with a single macro.
Note that the behavior of this CLAMP macro is different from
the combination `MAX(low,MIN(x,high))`.
* This CLAMP macro expands to two comparisons instead of three from
MAX and MIN combination. In theory, this makes the code slightly
smaller, in case that (low) or (high) or both are computed at
runtime, so that compilers cannot optimize them. (The third
comparison will matter if (low)>(high); see below.)
* CLAMP has a side effect, that if (low)>(high) it will produce weird
results. Unlike MIN & MAX which will force either (low) or (high) to
win. No assertion of ((low)<=(high)) is done in this macro, for now.
This CLAMP macro is implemented like described in glib
<http://developer.gnome.org/glib/stable/glib-Standard-Macros.html>
and does not handle weird uses like CLAMP(a++, low++, high--) .
2016-01-15 12:26:01 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2015-09-18 04:46:48 +00:00
|
|
|
|
static long fscale;
|
|
|
|
|
|
|
|
|
|
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
|
|
|
|
|
int mib[] = { CTL_HW, HW_NCPU };
|
|
|
|
|
int fmib[] = { CTL_KERN, KERN_FSCALE };
|
2016-01-03 21:56:33 +00:00
|
|
|
|
int i, e;
|
2016-03-06 04:23:29 +00:00
|
|
|
|
OpenBSDProcessList* opl;
|
|
|
|
|
ProcessList* pl;
|
|
|
|
|
size_t size;
|
|
|
|
|
char errbuf[_POSIX2_LINE_MAX];
|
|
|
|
|
|
|
|
|
|
opl = xCalloc(1, sizeof(OpenBSDProcessList));
|
|
|
|
|
pl = (ProcessList*) opl;
|
|
|
|
|
size = sizeof(pl->cpuCount);
|
2015-09-18 04:46:48 +00:00
|
|
|
|
ProcessList_init(pl, Class(OpenBSDProcess), usersTable, pidWhiteList, userId);
|
2016-03-06 04:23:29 +00:00
|
|
|
|
|
2016-01-03 21:56:33 +00:00
|
|
|
|
e = sysctl(mib, 2, &pl->cpuCount, &size, NULL, 0);
|
|
|
|
|
if (e == -1 || pl->cpuCount < 1) {
|
|
|
|
|
pl->cpuCount = 1;
|
|
|
|
|
}
|
2018-12-16 08:25:54 +00:00
|
|
|
|
opl->cpus = xCalloc(pl->cpuCount + 1, sizeof(CPUData));
|
2015-09-18 04:46:48 +00:00
|
|
|
|
|
|
|
|
|
size = sizeof(fscale);
|
2016-03-06 04:23:29 +00:00
|
|
|
|
if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) {
|
2016-01-03 21:56:33 +00:00
|
|
|
|
err(1, "fscale sysctl call failed");
|
2016-03-06 04:23:29 +00:00
|
|
|
|
}
|
2015-09-18 04:46:48 +00:00
|
|
|
|
|
2018-12-16 08:25:54 +00:00
|
|
|
|
for (i = 0; i <= pl->cpuCount; i++) {
|
|
|
|
|
CPUData *d = opl->cpus + i;
|
|
|
|
|
d->totalTime = 1;
|
|
|
|
|
d->totalPeriod = 1;
|
2015-09-18 04:46:48 +00:00
|
|
|
|
}
|
2015-10-13 15:05:52 +00:00
|
|
|
|
|
2016-03-06 04:23:29 +00:00
|
|
|
|
opl->kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
|
|
|
|
|
if (opl->kd == NULL) {
|
|
|
|
|
errx(1, "kvm_open: %s", errbuf);
|
|
|
|
|
}
|
2015-09-18 04:46:48 +00:00
|
|
|
|
|
|
|
|
|
return pl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProcessList_delete(ProcessList* this) {
|
2016-01-04 21:20:51 +00:00
|
|
|
|
const OpenBSDProcessList* opl = (OpenBSDProcessList*) this;
|
2016-03-06 04:23:29 +00:00
|
|
|
|
|
|
|
|
|
if (opl->kd) {
|
|
|
|
|
kvm_close(opl->kd);
|
|
|
|
|
}
|
2015-10-13 15:05:52 +00:00
|
|
|
|
|
2016-01-04 21:20:51 +00:00
|
|
|
|
free(opl->cpus);
|
2016-01-03 21:56:33 +00:00
|
|
|
|
|
2015-09-18 04:46:48 +00:00
|
|
|
|
ProcessList_done(this);
|
|
|
|
|
free(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) {
|
|
|
|
|
static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP};
|
|
|
|
|
struct uvmexp uvmexp;
|
2016-02-12 00:28:22 +00:00
|
|
|
|
size_t size_uvmexp = sizeof(uvmexp);
|
2015-09-18 04:46:48 +00:00
|
|
|
|
|
2016-02-12 00:28:22 +00:00
|
|
|
|
if (sysctl(uvmexp_mib, 2, &uvmexp, &size_uvmexp, NULL, 0) < 0) {
|
2016-01-02 17:17:35 +00:00
|
|
|
|
err(1, "uvmexp sysctl call failed");
|
2015-09-18 04:46:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-06 04:23:29 +00:00
|
|
|
|
pl->totalMem = uvmexp.npages * PAGE_SIZE_KB;
|
2015-09-18 04:46:48 +00:00
|
|
|
|
|
2016-02-12 00:28:22 +00:00
|
|
|
|
// Taken from OpenBSD systat/iostat.c, top/machine.c and uvm_sysctl(9)
|
|
|
|
|
static int bcache_mib[] = {CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT};
|
|
|
|
|
struct bcachestats bcstats;
|
|
|
|
|
size_t size_bcstats = sizeof(bcstats);
|
|
|
|
|
|
|
|
|
|
if (sysctl(bcache_mib, 3, &bcstats, &size_bcstats, NULL, 0) < 0) {
|
|
|
|
|
err(1, "cannot get vfs.bcachestat");
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 18:03:18 +00:00
|
|
|
|
pl->cachedMem = bcstats.numbufpages * PAGE_SIZE_KB;
|
|
|
|
|
pl->freeMem = uvmexp.free * PAGE_SIZE_KB;
|
|
|
|
|
pl->usedMem = (uvmexp.npages - uvmexp.free - uvmexp.paging) * PAGE_SIZE_KB;
|
2016-02-12 00:28:22 +00:00
|
|
|
|
|
2015-09-19 16:08:34 +00:00
|
|
|
|
/*
|
2016-01-04 21:20:51 +00:00
|
|
|
|
const OpenBSDProcessList* opl = (OpenBSDProcessList*) pl;
|
2015-10-13 15:05:52 +00:00
|
|
|
|
|
2015-09-18 04:46:48 +00:00
|
|
|
|
size_t len = sizeof(pl->totalMem);
|
|
|
|
|
sysctl(MIB_hw_physmem, 2, &(pl->totalMem), &len, NULL, 0);
|
|
|
|
|
pl->totalMem /= 1024;
|
|
|
|
|
sysctl(MIB_vm_stats_vm_v_wire_count, 4, &(pl->usedMem), &len, NULL, 0);
|
2016-03-06 04:23:29 +00:00
|
|
|
|
pl->usedMem *= PAGE_SIZE_KB;
|
2015-09-18 04:46:48 +00:00
|
|
|
|
pl->freeMem = pl->totalMem - pl->usedMem;
|
|
|
|
|
sysctl(MIB_vm_stats_vm_v_cache_count, 4, &(pl->cachedMem), &len, NULL, 0);
|
2016-03-06 04:23:29 +00:00
|
|
|
|
pl->cachedMem *= PAGE_SIZE_KB;
|
2015-10-13 15:05:52 +00:00
|
|
|
|
|
2015-09-18 04:46:48 +00:00
|
|
|
|
struct kvm_swap swap[16];
|
2016-01-04 21:20:51 +00:00
|
|
|
|
int nswap = kvm_getswapinfo(opl->kd, swap, sizeof(swap)/sizeof(swap[0]), 0);
|
2015-09-18 04:46:48 +00:00
|
|
|
|
pl->totalSwap = 0;
|
|
|
|
|
pl->usedSwap = 0;
|
|
|
|
|
for (int i = 0; i < nswap; i++) {
|
|
|
|
|
pl->totalSwap += swap[i].ksw_total;
|
|
|
|
|
pl->usedSwap += swap[i].ksw_used;
|
|
|
|
|
}
|
2016-03-06 04:23:29 +00:00
|
|
|
|
pl->totalSwap *= PAGE_SIZE_KB;
|
|
|
|
|
pl->usedSwap *= PAGE_SIZE_KB;
|
2015-10-13 15:05:52 +00:00
|
|
|
|
|
2015-09-18 04:46:48 +00:00
|
|
|
|
pl->sharedMem = 0; // currently unused
|
|
|
|
|
pl->buffersMem = 0; // not exposed to userspace
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd) {
|
2016-03-06 04:23:29 +00:00
|
|
|
|
char *s, **arg;
|
|
|
|
|
size_t len = 0, n;
|
2016-01-02 16:57:53 +00:00
|
|
|
|
int i;
|
2015-09-18 04:46:48 +00:00
|
|
|
|
|
2016-01-03 03:05:20 +00:00
|
|
|
|
/*
|
2016-03-06 04:23:29 +00:00
|
|
|
|
* Like OpenBSD's top(1), we try to fall back to the command name
|
|
|
|
|
* (argv[0]) if we fail to construct the full command.
|
2016-01-03 03:05:20 +00:00
|
|
|
|
*/
|
2016-01-02 16:57:53 +00:00
|
|
|
|
arg = kvm_getargv(kd, kproc, 500);
|
2016-03-06 04:38:12 +00:00
|
|
|
|
if (arg == NULL || *arg == NULL) {
|
2016-03-06 04:23:29 +00:00
|
|
|
|
*basenameEnd = strlen(kproc->p_comm);
|
|
|
|
|
return xStrdup(kproc->p_comm);
|
2016-01-02 16:57:53 +00:00
|
|
|
|
}
|
|
|
|
|
for (i = 0; arg[i] != NULL; i++) {
|
2016-03-06 04:23:29 +00:00
|
|
|
|
len += strlen(arg[i]) + 1; /* room for arg and trailing space or NUL */
|
2016-01-02 16:57:53 +00:00
|
|
|
|
}
|
2016-03-06 04:23:29 +00:00
|
|
|
|
/* don't use xMalloc here - we want to handle huge argv's gracefully */
|
2016-03-06 04:38:12 +00:00
|
|
|
|
if ((s = malloc(len)) == NULL) {
|
2016-03-06 04:23:29 +00:00
|
|
|
|
*basenameEnd = strlen(kproc->p_comm);
|
|
|
|
|
return xStrdup(kproc->p_comm);
|
2016-01-03 03:05:20 +00:00
|
|
|
|
}
|
2016-03-06 04:38:12 +00:00
|
|
|
|
|
|
|
|
|
*s = '\0';
|
|
|
|
|
|
2016-01-02 16:57:53 +00:00
|
|
|
|
for (i = 0; arg[i] != NULL; i++) {
|
2016-03-06 04:23:29 +00:00
|
|
|
|
n = strlcat(s, arg[i], len);
|
2016-01-02 16:57:53 +00:00
|
|
|
|
if (i == 0) {
|
2016-03-06 04:23:29 +00:00
|
|
|
|
/* TODO: rename all basenameEnd to basenameLen, make size_t */
|
|
|
|
|
*basenameEnd = MINIMUM(n, len-1);
|
2016-01-02 16:57:53 +00:00
|
|
|
|
}
|
2016-03-06 04:38:12 +00:00
|
|
|
|
/* the trailing space should get truncated anyway */
|
2016-03-06 04:23:29 +00:00
|
|
|
|
strlcat(s, " ", len);
|
2016-01-02 16:57:53 +00:00
|
|
|
|
}
|
2016-03-06 04:38:12 +00:00
|
|
|
|
|
2016-01-02 16:57:53 +00:00
|
|
|
|
return s;
|
2015-09-18 04:46:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Taken from OpenBSD's ps(1).
|
|
|
|
|
*/
|
2018-12-16 08:25:54 +00:00
|
|
|
|
static double getpcpu(const struct kinfo_proc *kp) {
|
2015-09-19 16:08:34 +00:00
|
|
|
|
if (fscale == 0)
|
|
|
|
|
return (0.0);
|
2015-09-18 04:46:48 +00:00
|
|
|
|
|
2015-09-19 16:08:34 +00:00
|
|
|
|
#define fxtofl(fixpt) ((double)(fixpt) / fscale)
|
2015-09-18 04:46:48 +00:00
|
|
|
|
|
2015-09-19 16:08:34 +00:00
|
|
|
|
return (100.0 * fxtofl(kp->p_pctcpu));
|
2015-09-18 04:46:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-16 08:25:54 +00:00
|
|
|
|
static inline void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) {
|
|
|
|
|
Settings* settings = this->super.settings;
|
2015-09-18 04:46:48 +00:00
|
|
|
|
bool hideKernelThreads = settings->hideKernelThreads;
|
|
|
|
|
bool hideUserlandThreads = settings->hideUserlandThreads;
|
|
|
|
|
struct kinfo_proc* kproc;
|
|
|
|
|
bool preExisting;
|
|
|
|
|
Process* proc;
|
|
|
|
|
OpenBSDProcess* fp;
|
2018-08-19 04:09:08 +00:00
|
|
|
|
struct tm date;
|
|
|
|
|
struct timeval tv;
|
2015-09-18 04:46:48 +00:00
|
|
|
|
int count = 0;
|
|
|
|
|
int i;
|
2015-10-13 15:05:52 +00:00
|
|
|
|
|
2015-09-18 04:46:48 +00:00
|
|
|
|
// use KERN_PROC_KTHREAD to also include kernel threads
|
2018-12-16 08:25:54 +00:00
|
|
|
|
struct kinfo_proc* kprocs = kvm_getprocs(this->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
|
2015-09-18 04:46:48 +00:00
|
|
|
|
//struct kinfo_proc* kprocs = getprocs(KERN_PROC_ALL, 0, &count);
|
2015-10-13 15:05:52 +00:00
|
|
|
|
|
2018-08-19 04:09:08 +00:00
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
|
2015-09-18 04:46:48 +00:00
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
|
kproc = &kprocs[i];
|
2015-10-13 15:05:52 +00:00
|
|
|
|
|
2015-09-18 04:46:48 +00:00
|
|
|
|
preExisting = false;
|
2018-12-16 08:25:54 +00:00
|
|
|
|
proc = ProcessList_getProcess(&this->super, kproc->p_pid, &preExisting, (Process_New) OpenBSDProcess_new);
|
2015-09-18 04:46:48 +00:00
|
|
|
|
fp = (OpenBSDProcess*) proc;
|
|
|
|
|
|
|
|
|
|
proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc))
|
2015-09-19 16:08:34 +00:00
|
|
|
|
|| (hideUserlandThreads && Process_isUserlandThread(proc)));
|
2015-10-13 15:05:52 +00:00
|
|
|
|
|
2015-09-18 04:46:48 +00:00
|
|
|
|
if (!preExisting) {
|
|
|
|
|
proc->ppid = kproc->p_ppid;
|
|
|
|
|
proc->tpgid = kproc->p_tpgid;
|
|
|
|
|
proc->tgid = kproc->p_pid;
|
|
|
|
|
proc->session = kproc->p_sid;
|
|
|
|
|
proc->tty_nr = kproc->p_tdev;
|
|
|
|
|
proc->pgrp = kproc->p__pgid;
|
|
|
|
|
proc->st_uid = kproc->p_uid;
|
|
|
|
|
proc->starttime_ctime = kproc->p_ustart_sec;
|
2018-12-16 08:25:54 +00:00
|
|
|
|
proc->user = UsersTable_getRef(this->super.usersTable, proc->st_uid);
|
|
|
|
|
ProcessList_add(&this->super, proc);
|
|
|
|
|
proc->comm = OpenBSDProcessList_readProcessName(this->kd, kproc, &proc->basenameOffset);
|
2018-08-19 04:09:08 +00:00
|
|
|
|
(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);
|
2015-09-18 04:46:48 +00:00
|
|
|
|
} else {
|
|
|
|
|
if (settings->updateProcessNames) {
|
|
|
|
|
free(proc->comm);
|
2018-12-16 08:25:54 +00:00
|
|
|
|
proc->comm = OpenBSDProcessList_readProcessName(this->kd, kproc, &proc->basenameOffset);
|
2015-09-18 04:46:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
proc->m_size = kproc->p_vm_dsize;
|
|
|
|
|
proc->m_resident = kproc->p_vm_rssize;
|
2018-12-16 08:25:54 +00:00
|
|
|
|
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);
|
2015-09-18 04:46:48 +00:00
|
|
|
|
//proc->nlwp = kproc->p_numthreads;
|
|
|
|
|
//proc->time = kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 10);
|
2015-09-19 16:08:34 +00:00
|
|
|
|
proc->nice = kproc->p_nice - 20;
|
|
|
|
|
proc->time = kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 1000000);
|
|
|
|
|
proc->time *= 100;
|
2015-09-18 04:46:48 +00:00
|
|
|
|
proc->priority = kproc->p_priority - PZERO;
|
|
|
|
|
|
|
|
|
|
switch (kproc->p_stat) {
|
2015-09-19 16:08:34 +00:00
|
|
|
|
case SIDL: proc->state = 'I'; break;
|
|
|
|
|
case SRUN: proc->state = 'R'; break;
|
|
|
|
|
case SSLEEP: proc->state = 'S'; break;
|
|
|
|
|
case SSTOP: proc->state = 'T'; break;
|
|
|
|
|
case SZOMB: proc->state = 'Z'; break;
|
|
|
|
|
case SDEAD: proc->state = 'D'; break;
|
|
|
|
|
case SONPROC: proc->state = 'P'; break;
|
|
|
|
|
default: proc->state = '?';
|
2015-09-18 04:46:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Process_isKernelThread(proc)) {
|
2018-12-16 08:25:54 +00:00
|
|
|
|
this->super.kernelThreads++;
|
2015-09-18 04:46:48 +00:00
|
|
|
|
}
|
2015-10-13 15:05:52 +00:00
|
|
|
|
|
2018-12-16 08:25:54 +00:00
|
|
|
|
this->super.totalTasks++;
|
2015-09-19 16:45:22 +00:00
|
|
|
|
// SRUN ('R') means runnable, not running
|
|
|
|
|
if (proc->state == 'P') {
|
2018-12-16 08:25:54 +00:00
|
|
|
|
this->super.runningTasks++;
|
2015-09-19 16:21:22 +00:00
|
|
|
|
}
|
2015-09-18 04:46:48 +00:00
|
|
|
|
proc->updated = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-16 08:25:54 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|