htop/openbsd/OpenBSDProcessList.c

280 lines
7.9 KiB
C
Raw Normal View History

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.
*/
#include "ProcessList.h"
#include "OpenBSDProcessList.h"
#include "OpenBSDProcess.h"
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <err.h>
#include <errno.h>
2015-09-18 04:46:48 +00:00
#include <fcntl.h>
#include <limits.h>
2015-09-18 04:46:48 +00:00
#include <string.h>
#include <sys/resource.h>
/*{
#include <kvm.h>
typedef struct CPUData_ {
unsigned long long int totalTime;
unsigned long long int totalPeriod;
} CPUData;
typedef struct OpenBSDProcessList_ {
ProcessList super;
kvm_t* kd;
CPUData* cpus;
} OpenBSDProcessList;
}*/
/*
* 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
#ifndef CLAMP
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : MAXIMUM(x, low))
#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 };
int i, e;
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);
e = sysctl(mib, 2, &pl->cpuCount, &size, NULL, 0);
if (e == -1 || pl->cpuCount < 1) {
pl->cpuCount = 1;
}
2016-02-02 14:53:02 +00:00
opl->cpus = xRealloc(opl->cpus, pl->cpuCount * sizeof(CPUData));
2015-09-18 04:46:48 +00:00
size = sizeof(fscale);
if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) {
err(1, "fscale sysctl call failed");
}
2015-09-18 04:46:48 +00:00
for (i = 0; i < pl->cpuCount; i++) {
opl->cpus[i].totalTime = 1;
opl->cpus[i].totalPeriod = 1;
2015-09-18 04:46:48 +00:00
}
2015-10-13 15:05:52 +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) {
const OpenBSDProcessList* opl = (OpenBSDProcessList*) this;
if (opl->kd) {
kvm_close(opl->kd);
}
2015-10-13 15:05:52 +00:00
free(opl->cpus);
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;
size_t size = sizeof(uvmexp);
if (sysctl(uvmexp_mib, 2, &uvmexp, &size, NULL, 0) < 0) {
err(1, "uvmexp sysctl call failed");
2015-09-18 04:46:48 +00:00
}
pl->usedMem = uvmexp.active * PAGE_SIZE_KB;
pl->totalMem = uvmexp.npages * PAGE_SIZE_KB;
2015-09-18 04:46:48 +00:00
2015-09-19 16:08:34 +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);
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);
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];
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;
}
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) {
char *s, **arg;
size_t len = 0, n;
int i;
2015-09-18 04:46:48 +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.
*/
arg = kvm_getargv(kd, kproc, 500);
if (arg == NULL) {
*basenameEnd = strlen(kproc->p_comm);
return xStrdup(kproc->p_comm);
}
for (i = 0; arg[i] != NULL; i++) {
len += strlen(arg[i]) + 1; /* room for arg and trailing space or NUL */
}
/* don't use xMalloc here - we want to handle huge argv's gracefully */
if ((s = xCalloc(len, 1)) == NULL) {
*basenameEnd = strlen(kproc->p_comm);
return xStrdup(kproc->p_comm);
}
for (i = 0; arg[i] != NULL; i++) {
n = strlcat(s, arg[i], len);
if (i == 0) {
/* TODO: rename all basenameEnd to basenameLen, make size_t */
*basenameEnd = MINIMUM(n, len-1);
}
strlcat(s, " ", len);
}
return s;
2015-09-18 04:46:48 +00:00
}
/*
* Taken from OpenBSD's ps(1).
*/
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
}
void ProcessList_goThroughEntries(ProcessList* this) {
OpenBSDProcessList* opl = (OpenBSDProcessList*) this;
2015-09-18 04:46:48 +00:00
Settings* settings = this->settings;
bool hideKernelThreads = settings->hideKernelThreads;
bool hideUserlandThreads = settings->hideUserlandThreads;
struct kinfo_proc* kproc;
bool preExisting;
Process* proc;
OpenBSDProcess* fp;
int count = 0;
int i;
2015-10-13 15:05:52 +00:00
2015-09-18 04:46:48 +00:00
OpenBSDProcessList_scanMemoryInfo(this);
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
struct kinfo_proc* kprocs = kvm_getprocs(opl->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
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;
proc = ProcessList_getProcess(this, kproc->p_pid, &preExisting, (Process_New) OpenBSDProcess_new);
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;
proc->user = UsersTable_getRef(this->usersTable, proc->st_uid);
ProcessList_add((ProcessList*)this, proc);
proc->comm = OpenBSDProcessList_readProcessName(opl->kd, kproc, &proc->basenameOffset);
2015-09-18 04:46:48 +00:00
} else {
if (settings->updateProcessNames) {
free(proc->comm);
proc->comm = OpenBSDProcessList_readProcessName(opl->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;
2015-09-19 16:08:34 +00:00
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);
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)) {
this->kernelThreads++;
}
2015-10-13 15:05:52 +00:00
2015-09-18 04:46:48 +00:00
this->totalTasks++;
// SRUN ('R') means runnable, not running
if (proc->state == 'P') {
2015-09-18 04:46:48 +00:00
this->runningTasks++;
2015-09-19 16:21:22 +00:00
}
2015-09-18 04:46:48 +00:00
proc->updated = true;
}
}