2021-03-15 07:44:39 +00:00
|
|
|
/*
|
|
|
|
htop - NetBSDProcessList.c
|
|
|
|
(C) 2014 Hisham H. Muhammad
|
2021-06-13 14:24:55 +00:00
|
|
|
(C) 2015 Michael McConville
|
2021-03-15 07:44:39 +00:00
|
|
|
(C) 2021 Santhosh Raju
|
2021-06-13 14:24:55 +00:00
|
|
|
(C) 2021 htop dev team
|
2021-03-15 07:44:39 +00:00
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
2021-04-24 04:20:51 +00:00
|
|
|
#include "netbsd/NetBSDProcessList.h"
|
2021-03-15 07:44:39 +00:00
|
|
|
|
|
|
|
#include <kvm.h>
|
2021-07-14 18:53:43 +00:00
|
|
|
#include <math.h>
|
2021-03-15 07:44:39 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/sched.h>
|
|
|
|
#include <sys/swap.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
|
|
|
#include "CRT.h"
|
|
|
|
#include "Macros.h"
|
|
|
|
#include "Object.h"
|
|
|
|
#include "Process.h"
|
|
|
|
#include "ProcessList.h"
|
|
|
|
#include "Settings.h"
|
|
|
|
#include "XUtils.h"
|
2021-05-24 17:27:59 +00:00
|
|
|
#include "netbsd/NetBSDProcess.h"
|
2021-03-15 07:44:39 +00:00
|
|
|
|
2021-04-24 04:20:51 +00:00
|
|
|
|
2021-03-15 07:44:39 +00:00
|
|
|
static long fscale;
|
|
|
|
static int pageSize;
|
|
|
|
static int pageSizeKB;
|
|
|
|
|
2021-08-30 17:26:50 +00:00
|
|
|
static const struct {
|
|
|
|
const char* name;
|
|
|
|
long int scale;
|
|
|
|
} freqSysctls[] = {
|
|
|
|
{ "machdep.est.frequency.current", 1 },
|
|
|
|
{ "machdep.powernow.frequency.current", 1 },
|
|
|
|
{ "machdep.intrepid.frequency.current", 1 },
|
|
|
|
{ "machdep.loongson.frequency.current", 1 },
|
|
|
|
{ "machdep.cpu.frequency.current", 1 },
|
|
|
|
{ "machdep.frequency.current", 1 },
|
|
|
|
{ "machdep.tsc_freq", 1000000 },
|
2021-07-14 18:53:43 +00:00
|
|
|
};
|
|
|
|
|
2021-08-03 03:05:02 +00:00
|
|
|
static void NetBSDProcessList_updateCPUcount(ProcessList* super) {
|
|
|
|
NetBSDProcessList* opl = (NetBSDProcessList*) super;
|
|
|
|
|
|
|
|
// Definitions for sysctl(3), cf. https://nxr.netbsd.org/xref/src/sys/sys/sysctl.h#813
|
|
|
|
const int mib_ncpu_existing[] = { CTL_HW, HW_NCPU }; // Number of existing CPUs
|
|
|
|
const int mib_ncpu_online[] = { CTL_HW, HW_NCPUONLINE }; // Number of online/active CPUs
|
|
|
|
|
|
|
|
int r;
|
|
|
|
unsigned int value;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
bool change = false;
|
|
|
|
|
|
|
|
// Query the number of active/online CPUs.
|
|
|
|
size = sizeof(value);
|
|
|
|
r = sysctl(mib_ncpu_online, 2, &value, &size, NULL, 0);
|
|
|
|
if (r < 0 || value < 1) {
|
|
|
|
value = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value != super->activeCPUs) {
|
|
|
|
super->activeCPUs = value;
|
|
|
|
change = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the total number of CPUs.
|
|
|
|
size = sizeof(value);
|
|
|
|
r = sysctl(mib_ncpu_existing, 2, &value, &size, NULL, 0);
|
|
|
|
if (r < 0 || value < 1) {
|
|
|
|
value = super->activeCPUs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value != super->existingCPUs) {
|
|
|
|
opl->cpuData = xReallocArray(opl->cpuData, value + 1, sizeof(CPUData));
|
|
|
|
super->existingCPUs = value;
|
|
|
|
change = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset CPU stats when number of online/existing CPU cores changed
|
|
|
|
if (change) {
|
|
|
|
CPUData* dAvg = &opl->cpuData[0];
|
|
|
|
memset(dAvg, '\0', sizeof(CPUData));
|
|
|
|
dAvg->totalTime = 1;
|
|
|
|
dAvg->totalPeriod = 1;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < super->existingCPUs; i++) {
|
|
|
|
CPUData* d = &opl->cpuData[i + 1];
|
|
|
|
memset(d, '\0', sizeof(CPUData));
|
|
|
|
d->totalTime = 1;
|
|
|
|
d->totalPeriod = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 05:45:04 +00:00
|
|
|
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* pidMatchList, uid_t userId) {
|
2021-03-15 07:44:39 +00:00
|
|
|
const int fmib[] = { CTL_KERN, KERN_FSCALE };
|
|
|
|
size_t size;
|
|
|
|
char errbuf[_POSIX2_LINE_MAX];
|
|
|
|
|
2021-04-24 04:29:11 +00:00
|
|
|
NetBSDProcessList* npl = xCalloc(1, sizeof(NetBSDProcessList));
|
|
|
|
ProcessList* pl = (ProcessList*) npl;
|
2021-08-16 05:45:04 +00:00
|
|
|
ProcessList_init(pl, Class(NetBSDProcess), usersTable, dynamicMeters, dynamicColumns, pidMatchList, userId);
|
2021-03-15 07:44:39 +00:00
|
|
|
|
2021-08-03 03:05:02 +00:00
|
|
|
NetBSDProcessList_updateCPUcount(pl);
|
2021-03-15 07:44:39 +00:00
|
|
|
|
|
|
|
size = sizeof(fscale);
|
|
|
|
if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) {
|
|
|
|
CRT_fatalError("fscale sysctl call failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pageSize = sysconf(_SC_PAGESIZE)) == -1)
|
|
|
|
CRT_fatalError("pagesize sysconf call failed");
|
|
|
|
pageSizeKB = pageSize / ONE_K;
|
|
|
|
|
2021-04-24 04:29:11 +00:00
|
|
|
npl->kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
|
|
|
|
if (npl->kd == NULL) {
|
2021-03-15 07:44:39 +00:00
|
|
|
CRT_fatalError("kvm_openfiles() failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return pl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessList_delete(ProcessList* this) {
|
2021-04-24 04:29:11 +00:00
|
|
|
NetBSDProcessList* npl = (NetBSDProcessList*) this;
|
2021-03-15 07:44:39 +00:00
|
|
|
|
2021-04-24 04:29:11 +00:00
|
|
|
if (npl->kd) {
|
|
|
|
kvm_close(npl->kd);
|
2021-03-15 07:44:39 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 03:05:02 +00:00
|
|
|
free(npl->cpuData);
|
2021-03-15 07:44:39 +00:00
|
|
|
|
|
|
|
ProcessList_done(this);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void NetBSDProcessList_scanMemoryInfo(ProcessList* pl) {
|
|
|
|
static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP2};
|
|
|
|
struct uvmexp_sysctl uvmexp;
|
|
|
|
size_t size_uvmexp = sizeof(uvmexp);
|
|
|
|
|
|
|
|
if (sysctl(uvmexp_mib, 2, &uvmexp, &size_uvmexp, NULL, 0) < 0) {
|
|
|
|
CRT_fatalError("uvmexp sysctl call failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
pl->totalMem = uvmexp.npages * pageSizeKB;
|
2021-07-13 16:49:35 +00:00
|
|
|
pl->buffersMem = 0;
|
|
|
|
pl->cachedMem = (uvmexp.filepages + uvmexp.execpages) * pageSizeKB;
|
|
|
|
pl->usedMem = (uvmexp.active + uvmexp.wired) * pageSizeKB;
|
2021-03-15 07:44:39 +00:00
|
|
|
pl->totalSwap = uvmexp.swpages * pageSizeKB;
|
|
|
|
pl->usedSwap = uvmexp.swpginuse * pageSizeKB;
|
|
|
|
}
|
|
|
|
|
2021-06-12 11:37:58 +00:00
|
|
|
static void NetBSDProcessList_updateExe(const struct kinfo_proc2* kproc, Process* proc) {
|
|
|
|
const int mib[] = { CTL_KERN, KERN_PROC_ARGS, kproc->p_pid, KERN_PROC_PATHNAME };
|
|
|
|
char buffer[2048];
|
|
|
|
size_t size = sizeof(buffer);
|
|
|
|
if (sysctl(mib, 4, buffer, &size, NULL, 0) != 0) {
|
|
|
|
Process_updateExe(proc, NULL);
|
|
|
|
return;
|
|
|
|
}
|
2021-06-12 23:58:50 +00:00
|
|
|
|
2021-06-12 11:37:58 +00:00
|
|
|
/* Kernel threads return an empty buffer */
|
|
|
|
if (buffer[0] == '\0') {
|
|
|
|
Process_updateExe(proc, NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Process_updateExe(proc, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void NetBSDProcessList_updateCwd(const struct kinfo_proc2* kproc, Process* proc) {
|
|
|
|
const int mib[] = { CTL_KERN, KERN_PROC_ARGS, kproc->p_pid, KERN_PROC_CWD };
|
|
|
|
char buffer[2048];
|
|
|
|
size_t size = sizeof(buffer);
|
|
|
|
if (sysctl(mib, 4, buffer, &size, NULL, 0) != 0) {
|
|
|
|
free(proc->procCwd);
|
|
|
|
proc->procCwd = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Kernel threads return an empty buffer */
|
|
|
|
if (buffer[0] == '\0') {
|
|
|
|
free(proc->procCwd);
|
|
|
|
proc->procCwd = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free_and_xStrdup(&proc->procCwd, buffer);
|
|
|
|
}
|
|
|
|
|
2021-05-23 09:30:48 +00:00
|
|
|
static void NetBSDProcessList_updateProcessName(kvm_t* kd, const struct kinfo_proc2* kproc, Process* proc) {
|
|
|
|
Process_updateComm(proc, kproc->p_comm);
|
|
|
|
|
2021-03-15 07:44:39 +00:00
|
|
|
/*
|
2021-04-17 03:11:50 +00:00
|
|
|
* Like NetBSD's top(1), we try to fall back to the command name
|
2021-03-15 07:44:39 +00:00
|
|
|
* (argv[0]) if we fail to construct the full command.
|
|
|
|
*/
|
|
|
|
char** arg = kvm_getargv2(kd, kproc, 500);
|
|
|
|
if (arg == NULL || *arg == NULL) {
|
2021-05-23 09:30:48 +00:00
|
|
|
Process_updateCmdline(proc, kproc->p_comm, 0, strlen(kproc->p_comm));
|
|
|
|
return;
|
2021-03-15 07:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t len = 0;
|
|
|
|
for (int 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 */
|
|
|
|
char* s;
|
|
|
|
if ((s = malloc(len)) == NULL) {
|
2021-05-23 09:30:48 +00:00
|
|
|
Process_updateCmdline(proc, kproc->p_comm, 0, strlen(kproc->p_comm));
|
|
|
|
return;
|
2021-03-15 07:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*s = '\0';
|
|
|
|
|
2021-05-23 09:30:48 +00:00
|
|
|
int start = 0;
|
|
|
|
int end = 0;
|
2021-03-15 07:44:39 +00:00
|
|
|
for (int i = 0; arg[i] != NULL; i++) {
|
|
|
|
size_t n = strlcat(s, arg[i], len);
|
|
|
|
if (i == 0) {
|
2021-05-23 09:30:48 +00:00
|
|
|
end = MINIMUM(n, len - 1);
|
|
|
|
/* check if cmdline ended earlier, e.g 'kdeinit5: Running...' */
|
|
|
|
for (int j = end; j > 0; j--) {
|
2021-07-14 17:18:27 +00:00
|
|
|
if (arg[0][j] == ' ' && arg[0][j - 1] != '\\') {
|
|
|
|
end = (arg[0][j - 1] == ':') ? (j - 1) : j;
|
2021-05-23 09:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-15 07:44:39 +00:00
|
|
|
}
|
|
|
|
/* the trailing space should get truncated anyway */
|
|
|
|
strlcat(s, " ", len);
|
|
|
|
}
|
|
|
|
|
2021-05-23 09:30:48 +00:00
|
|
|
Process_updateCmdline(proc, s, start, end);
|
2021-03-15 07:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-04-17 03:11:50 +00:00
|
|
|
* Borrowed with modifications from NetBSD's top(1).
|
2021-03-15 07:44:39 +00:00
|
|
|
*/
|
|
|
|
static double getpcpu(const struct kinfo_proc2* kp) {
|
|
|
|
if (fscale == 0)
|
|
|
|
return 0.0;
|
|
|
|
|
|
|
|
return 100.0 * (double)kp->p_pctcpu / fscale;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void NetBSDProcessList_scanProcs(NetBSDProcessList* this) {
|
|
|
|
const Settings* settings = this->super.settings;
|
|
|
|
bool hideKernelThreads = settings->hideKernelThreads;
|
|
|
|
bool hideUserlandThreads = settings->hideUserlandThreads;
|
|
|
|
int count = 0;
|
2021-04-17 03:11:50 +00:00
|
|
|
|
2021-03-15 07:44:39 +00:00
|
|
|
const struct kinfo_proc2* kprocs = kvm_getproc2(this->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &count);
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
const struct kinfo_proc2* kproc = &kprocs[i];
|
|
|
|
|
|
|
|
bool preExisting = false;
|
|
|
|
Process* proc = ProcessList_getProcess(&this->super, kproc->p_pid, &preExisting, NetBSDProcess_new);
|
|
|
|
|
|
|
|
proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc)));
|
|
|
|
|
|
|
|
if (!preExisting) {
|
2021-06-12 23:58:50 +00:00
|
|
|
proc->pid = kproc->p_pid;
|
2021-03-15 07:44:39 +00:00
|
|
|
proc->ppid = kproc->p_ppid;
|
|
|
|
proc->tpgid = kproc->p_tpgid;
|
|
|
|
proc->tgid = kproc->p_pid;
|
|
|
|
proc->session = kproc->p_sid;
|
|
|
|
proc->pgrp = kproc->p__pgid;
|
2021-08-15 08:15:16 +00:00
|
|
|
proc->isKernelThread = !!(kproc->p_flag & P_SYSTEM);
|
2021-05-23 09:30:48 +00:00
|
|
|
proc->isUserlandThread = proc->pid != proc->tgid;
|
2021-03-15 07:44:39 +00:00
|
|
|
proc->starttime_ctime = kproc->p_ustart_sec;
|
|
|
|
Process_fillStarttimeBuffer(proc);
|
|
|
|
ProcessList_add(&this->super, proc);
|
2021-05-23 09:30:48 +00:00
|
|
|
|
2021-08-30 17:28:15 +00:00
|
|
|
proc->tty_nr = kproc->p_tdev;
|
|
|
|
const char* name = ((dev_t)kproc->p_tdev != KERN_PROC_TTY_NODEV) ? devname(kproc->p_tdev, S_IFCHR) : NULL;
|
|
|
|
if (!name) {
|
|
|
|
free(proc->tty_name);
|
|
|
|
proc->tty_name = NULL;
|
|
|
|
} else {
|
|
|
|
free_and_xStrdup(&proc->tty_name, name);
|
|
|
|
}
|
|
|
|
|
2021-06-12 11:37:58 +00:00
|
|
|
NetBSDProcessList_updateExe(kproc, proc);
|
2021-05-23 09:30:48 +00:00
|
|
|
NetBSDProcessList_updateProcessName(this->kd, kproc, proc);
|
2021-03-15 07:44:39 +00:00
|
|
|
} else {
|
|
|
|
if (settings->updateProcessNames) {
|
2021-05-23 09:30:48 +00:00
|
|
|
NetBSDProcessList_updateProcessName(this->kd, kproc, proc);
|
2021-03-15 07:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-17 03:11:50 +00:00
|
|
|
|
2021-06-12 11:37:58 +00:00
|
|
|
if (settings->flags & PROCESS_FLAG_CWD) {
|
|
|
|
NetBSDProcessList_updateCwd(kproc, proc);
|
|
|
|
}
|
|
|
|
|
2021-06-18 10:53:23 +00:00
|
|
|
if (proc->st_uid != kproc->p_uid) {
|
|
|
|
proc->st_uid = kproc->p_uid;
|
|
|
|
proc->user = UsersTable_getRef(this->super.usersTable, proc->st_uid);
|
|
|
|
}
|
|
|
|
|
2021-03-15 07:44:39 +00:00
|
|
|
proc->m_virt = kproc->p_vm_vsize;
|
|
|
|
proc->m_resident = kproc->p_vm_rssize;
|
|
|
|
proc->percent_mem = (proc->m_resident * pageSizeKB) / (double)(this->super.totalMem) * 100.0;
|
2021-08-03 03:05:02 +00:00
|
|
|
proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0, this->super.activeCPUs * 100.0);
|
2021-04-17 03:11:50 +00:00
|
|
|
proc->nlwp = kproc->p_nlwps;
|
2021-03-15 07:44:39 +00:00
|
|
|
proc->nice = kproc->p_nice - 20;
|
|
|
|
proc->time = 100 * (kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 1000000));
|
|
|
|
proc->priority = kproc->p_priority - PZERO;
|
2021-08-30 17:28:15 +00:00
|
|
|
proc->processor = kproc->p_cpuid;
|
|
|
|
proc->minflt = kproc->p_uru_minflt;
|
|
|
|
proc->majflt = kproc->p_uru_majflt;
|
2021-03-15 07:44:39 +00:00
|
|
|
|
2021-08-30 17:28:15 +00:00
|
|
|
int nlwps = 0;
|
|
|
|
const struct kinfo_lwp* klwps = kvm_getlwps(this->kd, kproc->p_pid, kproc->p_paddr, sizeof(struct kinfo_lwp), &nlwps);
|
2021-03-15 07:44:39 +00:00
|
|
|
|
|
|
|
switch (kproc->p_realstat) {
|
|
|
|
case SIDL: proc->state = 'I'; break;
|
|
|
|
case SACTIVE:
|
2021-04-27 11:49:35 +00:00
|
|
|
// We only consider the first LWP with a one of the below states.
|
|
|
|
for (int j = 0; j < nlwps; j++) {
|
|
|
|
if (klwps) {
|
|
|
|
switch (klwps[j].l_stat) {
|
|
|
|
case LSONPROC: proc->state = 'P'; break;
|
|
|
|
case LSRUN: proc->state = 'R'; break;
|
|
|
|
case LSSLEEP: proc->state = 'S'; break;
|
|
|
|
case LSSTOP: proc->state = 'T'; break;
|
|
|
|
default: proc->state = '?';
|
|
|
|
}
|
|
|
|
if (proc->state != '?')
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
proc->state = '?';
|
|
|
|
break;
|
2021-03-15 07:44:39 +00:00
|
|
|
}
|
2021-04-27 11:49:35 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-03-15 07:44:39 +00:00
|
|
|
case SSTOP: proc->state = 'T'; break;
|
|
|
|
case SZOMB: proc->state = 'Z'; break;
|
|
|
|
case SDEAD: proc->state = 'D'; break;
|
|
|
|
default: proc->state = '?';
|
|
|
|
}
|
|
|
|
|
2021-06-12 23:58:50 +00:00
|
|
|
if (Process_isKernelThread(proc)) {
|
|
|
|
this->super.kernelThreads++;
|
|
|
|
} else if (Process_isUserlandThread(proc)) {
|
|
|
|
this->super.userlandThreads++;
|
|
|
|
}
|
|
|
|
|
2021-03-15 07:44:39 +00:00
|
|
|
this->super.totalTasks++;
|
|
|
|
// SRUN ('R') means runnable, not running
|
|
|
|
if (proc->state == 'P') {
|
|
|
|
this->super.runningTasks++;
|
|
|
|
}
|
|
|
|
proc->updated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void getKernelCPUTimes(int cpuId, u_int64_t* times) {
|
|
|
|
const int mib[] = { CTL_KERN, KERN_CP_TIME, cpuId };
|
|
|
|
size_t length = sizeof(*times) * CPUSTATES;
|
|
|
|
if (sysctl(mib, 3, times, &length, NULL, 0) == -1 || length != sizeof(*times) * 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];
|
|
|
|
|
|
|
|
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);
|
|
|
|
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;
|
|
|
|
cpu->intrTime = times[CP_INTR];
|
|
|
|
cpu->idleTime = times[CP_IDLE];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void NetBSDProcessList_scanCPUTime(NetBSDProcessList* this) {
|
|
|
|
u_int64_t kernelTimes[CPUSTATES] = {0};
|
|
|
|
u_int64_t avg[CPUSTATES] = {0};
|
|
|
|
|
2021-08-03 03:05:02 +00:00
|
|
|
for (unsigned int i = 0; i < this->super.existingCPUs; i++) {
|
2021-03-15 07:44:39 +00:00
|
|
|
getKernelCPUTimes(i, kernelTimes);
|
2021-08-03 03:05:02 +00:00
|
|
|
CPUData* cpu = &this->cpuData[i + 1];
|
2021-03-15 07:44:39 +00:00
|
|
|
kernelCPUTimesToHtop(kernelTimes, cpu);
|
|
|
|
|
|
|
|
avg[CP_USER] += cpu->userTime;
|
|
|
|
avg[CP_NICE] += cpu->niceTime;
|
|
|
|
avg[CP_SYS] += cpu->sysTime;
|
|
|
|
avg[CP_INTR] += cpu->intrTime;
|
|
|
|
avg[CP_IDLE] += cpu->idleTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < CPUSTATES; i++) {
|
2021-08-03 03:05:02 +00:00
|
|
|
avg[i] /= this->super.activeCPUs;
|
2021-03-15 07:44:39 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 03:05:02 +00:00
|
|
|
kernelCPUTimesToHtop(avg, &this->cpuData[0]);
|
2021-03-15 07:44:39 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 18:53:43 +00:00
|
|
|
static void NetBSDProcessList_scanCPUFrequency(NetBSDProcessList* this) {
|
2021-08-03 03:05:02 +00:00
|
|
|
unsigned int cpus = this->super.existingCPUs;
|
2021-07-14 18:53:43 +00:00
|
|
|
bool match = false;
|
|
|
|
char name[64];
|
2021-08-30 17:26:50 +00:00
|
|
|
long int freq = 0;
|
2021-07-14 19:35:04 +00:00
|
|
|
size_t freqSize;
|
2021-07-14 18:53:43 +00:00
|
|
|
|
2021-08-03 03:05:02 +00:00
|
|
|
for (unsigned int i = 0; i < cpus; i++) {
|
|
|
|
this->cpuData[i + 1].frequency = NAN;
|
2021-07-14 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* newer hardware supports per-core frequency, for e.g. ARM big.LITTLE */
|
2021-08-03 03:05:02 +00:00
|
|
|
for (unsigned int i = 0; i < cpus; i++) {
|
2021-07-14 19:36:44 +00:00
|
|
|
xSnprintf(name, sizeof(name), "machdep.cpufreq.cpu%u.current", i);
|
2021-07-14 19:35:04 +00:00
|
|
|
freqSize = sizeof(freq);
|
2021-07-14 18:53:43 +00:00
|
|
|
if (sysctlbyname(name, &freq, &freqSize, NULL, 0) != -1) {
|
2021-08-30 17:26:50 +00:00
|
|
|
this->cpuData[i + 1].frequency = freq; /* already in MHz */
|
2021-07-14 18:53:43 +00:00
|
|
|
match = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate through legacy sysctl nodes for single-core frequency until
|
|
|
|
* we find a match...
|
|
|
|
*/
|
2021-08-30 17:26:50 +00:00
|
|
|
for (size_t i = 0; i < ARRAYSIZE(freqSysctls); i++) {
|
2021-07-14 19:35:04 +00:00
|
|
|
freqSize = sizeof(freq);
|
2021-08-30 17:26:50 +00:00
|
|
|
if (sysctlbyname(freqSysctls[i].name, &freq, &freqSize, NULL, 0) != -1) {
|
|
|
|
freq /= freqSysctls[i].scale; /* scale to MHz */
|
2021-07-14 18:53:43 +00:00
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match) {
|
2021-08-03 03:05:02 +00:00
|
|
|
for (unsigned int i = 0; i < cpus; i++) {
|
|
|
|
this->cpuData[i + 1].frequency = freq;
|
2021-07-14 18:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 07:44:39 +00:00
|
|
|
void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
|
2021-04-24 04:29:11 +00:00
|
|
|
NetBSDProcessList* npl = (NetBSDProcessList*) super;
|
2021-03-15 07:44:39 +00:00
|
|
|
|
|
|
|
NetBSDProcessList_scanMemoryInfo(super);
|
2021-04-24 04:29:11 +00:00
|
|
|
NetBSDProcessList_scanCPUTime(npl);
|
2021-03-15 07:44:39 +00:00
|
|
|
|
2021-07-14 18:53:43 +00:00
|
|
|
if (super->settings->showCPUFrequency) {
|
|
|
|
NetBSDProcessList_scanCPUFrequency(npl);
|
|
|
|
}
|
|
|
|
|
2021-03-15 07:44:39 +00:00
|
|
|
// in pause mode only gather global data for meters (CPU/memory/...)
|
|
|
|
if (pauseProcessUpdate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-24 04:29:11 +00:00
|
|
|
NetBSDProcessList_scanProcs(npl);
|
2021-03-15 07:44:39 +00:00
|
|
|
}
|
2021-08-03 03:05:02 +00:00
|
|
|
|
|
|
|
bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id) {
|
|
|
|
assert(id < super->existingCPUs);
|
|
|
|
|
|
|
|
// TODO: Support detecting online / offline CPUs.
|
|
|
|
return true;
|
|
|
|
}
|