summaryrefslogtreecommitdiffstats
path: root/netbsd/NetBSDProcessList.c
diff options
context:
space:
mode:
authorNathan Scott <nathans@redhat.com>2023-05-02 16:56:18 +1000
committerNathan Scott <nathans@redhat.com>2023-05-08 13:06:38 +1000
commit72235d8e098d9d79029dca65122605741e1aafad (patch)
tree96593b8bd9dc95dc5ab321bd363d36351cbd0a99 /netbsd/NetBSDProcessList.c
parent0bdade1b6cb40c5bd374a93ac0489058a7421bb5 (diff)
Adapt platform code for the new Machine base class
Move host-centric data to new derived <Platform>Machine classes, separate from process-list-centric data.
Diffstat (limited to 'netbsd/NetBSDProcessList.c')
-rw-r--r--netbsd/NetBSDProcessList.c280
1 files changed, 17 insertions, 263 deletions
diff --git a/netbsd/NetBSDProcessList.c b/netbsd/NetBSDProcessList.c
index b7b6915a..9e5a9be4 100644
--- a/netbsd/NetBSDProcessList.c
+++ b/netbsd/NetBSDProcessList.c
@@ -20,7 +20,6 @@ in the source distribution for its full text.
#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>
@@ -32,137 +31,23 @@ in the source distribution for its full text.
#include "ProcessList.h"
#include "Settings.h"
#include "XUtils.h"
+#include "netbsd/NetBSDMachine.h"
#include "netbsd/NetBSDProcess.h"
-static long fscale;
-static int pageSize;
-static int pageSizeKB;
-
-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 },
-};
-
-static void NetBSDProcessList_updateCPUcount(ProcessList* super) {
- NetBSDProcessList* opl = (NetBSDProcessList*) super;
- Machine* host = super->host;
-
- // 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 != host->activeCPUs) {
- host->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 = host->activeCPUs;
- }
-
- if (value != host->existingCPUs) {
- opl->cpuData = xReallocArray(opl->cpuData, value + 1, sizeof(CPUData));
- host->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 < host->existingCPUs; i++) {
- CPUData* d = &opl->cpuData[i + 1];
- memset(d, '\0', sizeof(CPUData));
- d->totalTime = 1;
- d->totalPeriod = 1;
- }
- }
-}
-
ProcessList* ProcessList_new(Machine* host, Hashtable* pidMatchList) {
- const int fmib[] = { CTL_KERN, KERN_FSCALE };
- size_t size;
- char errbuf[_POSIX2_LINE_MAX];
-
- NetBSDProcessList* npl = xCalloc(1, sizeof(NetBSDProcessList));
- ProcessList* pl = (ProcessList*) npl;
- ProcessList_init(pl, Class(NetBSDProcess), host, pidMatchList);
+ NetBSDProcessList* this = xCalloc(1, sizeof(NetBSDProcessList));
+ ProcessList* super = (ProcessList*) this;
- NetBSDProcessList_updateCPUcount(pl);
-
- size = sizeof(fscale);
- if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) {
- CRT_fatalError("fscale sysctl call failed");
- }
+ ProcessList_init(super, Class(NetBSDProcess), host, pidMatchList);
- if ((pageSize = sysconf(_SC_PAGESIZE)) == -1)
- CRT_fatalError("pagesize sysconf call failed");
- pageSizeKB = pageSize / ONE_K;
-
- npl->kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
- if (npl->kd == NULL) {
- CRT_fatalError("kvm_openfiles() failed");
- }
-
- return pl;
+ return super;
}
void ProcessList_delete(ProcessList* this) {
NetBSDProcessList* npl = (NetBSDProcessList*) this;
-
- if (npl->kd) {
- kvm_close(npl->kd);
- }
-
- free(npl->cpuData);
-
ProcessList_done(this);
- free(this);
-}
-
-static void NetBSDProcessList_scanMemoryInfo(Machine* host) {
- 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");
- }
-
- host->totalMem = uvmexp.npages * pageSizeKB;
- host->buffersMem = 0;
- host->cachedMem = (uvmexp.filepages + uvmexp.execpages) * pageSizeKB;
- host->usedMem = (uvmexp.active + uvmexp.wired) * pageSizeKB;
- host->totalSwap = uvmexp.swpages * pageSizeKB;
- host->usedSwap = uvmexp.swpginuse * pageSizeKB;
+ free(npl);
}
static void NetBSDProcessList_updateExe(const struct kinfo_proc2* kproc, Process* proc) {
@@ -255,21 +140,22 @@ static void NetBSDProcessList_updateProcessName(kvm_t* kd, const struct kinfo_pr
/*
* Borrowed with modifications from NetBSD's top(1).
*/
-static double getpcpu(const struct kinfo_proc2* kp) {
- if (fscale == 0)
+static double getpcpu(const NetBSDMachine* nhost, const struct kinfo_proc2* kp) {
+ if (nhost->fscale == 0)
return 0.0;
- return 100.0 * (double)kp->p_pctcpu / fscale;
+ return 100.0 * (double)kp->p_pctcpu / nhost->fscale;
}
static void NetBSDProcessList_scanProcs(NetBSDProcessList* this) {
const Machine* host = this->super.host;
+ const NetBSDMachine* nhost = (const NetBSDMachine*) host;
const Settings* settings = host->settings;
bool hideKernelThreads = settings->hideKernelThreads;
bool hideUserlandThreads = settings->hideUserlandThreads;
int count = 0;
- const struct kinfo_proc2* kprocs = kvm_getproc2(this->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &count);
+ const struct kinfo_proc2* kprocs = kvm_getproc2(nhost->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &count);
for (int i = 0; i < count; i++) {
const struct kinfo_proc2* kproc = &kprocs[i];
@@ -302,10 +188,10 @@ static void NetBSDProcessList_scanProcs(NetBSDProcessList* this) {
}
NetBSDProcessList_updateExe(kproc, proc);
- NetBSDProcessList_updateProcessName(this->kd, kproc, proc);
+ NetBSDProcessList_updateProcessName(nhost->kd, kproc, proc);
} else {
if (settings->updateProcessNames) {
- NetBSDProcessList_updateProcessName(this->kd, kproc, proc);
+ NetBSDProcessList_updateProcessName(nhost->kd, kproc, proc);
}
}
@@ -321,8 +207,8 @@ static void NetBSDProcessList_scanProcs(NetBSDProcessList* this) {
proc->m_virt = kproc->p_vm_vsize;
proc->m_resident = kproc->p_vm_rssize;
- proc->percent_mem = (proc->m_resident * pageSizeKB) / (double)(host->totalMem) * 100.0;
- proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0, host->activeCPUs * 100.0);
+ proc->percent_mem = (proc->m_resident * nhost->pageSizeKB) / (double)(host->totalMem) * 100.0;
+ proc->percent_cpu = CLAMP(getpcpu(nhost, kproc), 0.0, host->activeCPUs * 100.0);
Process_updateCPUFieldWidths(proc->percent_cpu);
proc->nlwp = kproc->p_nlwps;
@@ -334,7 +220,7 @@ static void NetBSDProcessList_scanProcs(NetBSDProcessList* this) {
proc->majflt = kproc->p_uru_majflt;
int nlwps = 0;
- const struct kinfo_lwp* klwps = kvm_getlwps(this->kd, kproc->p_pid, kproc->p_paddr, sizeof(struct kinfo_lwp), &nlwps);
+ const struct kinfo_lwp* klwps = kvm_getlwps(nhost->kd, kproc->p_pid, kproc->p_paddr, sizeof(struct kinfo_lwp), &nlwps);
/* TODO: According to the link below, SDYING should be a regarded state */
/* Taken from: https://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/sys/sys/proc.h */
@@ -379,140 +265,8 @@ static void NetBSDProcessList_scanProcs(NetBSDProcessList* this) {
}
}
-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) {
- const Machine* host = this->super.host;
- u_int64_t kernelTimes[CPUSTATES] = {0};
- u_int64_t avg[CPUSTATES] = {0};
-
- for (unsigned int i = 0; i < host->existingCPUs; i++) {
- getKernelCPUTimes(i, kernelTimes);
- CPUData* cpu = &this->cpuData[i + 1];
- 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++) {
- avg[i] /= host->activeCPUs;
- }
-
- kernelCPUTimesToHtop(avg, &this->cpuData[0]);
-}
-
-static void NetBSDProcessList_scanCPUFrequency(NetBSDProcessList* this) {
- const Machine* host = this->super.host;
- unsigned int cpus = host->existingCPUs;
- bool match = false;
- char name[64];
- long int freq = 0;
- size_t freqSize;
-
- for (unsigned int i = 0; i < cpus; i++) {
- this->cpuData[i + 1].frequency = NAN;
- }
-
- /* newer hardware supports per-core frequency, for e.g. ARM big.LITTLE */
- for (unsigned int i = 0; i < cpus; i++) {
- xSnprintf(name, sizeof(name), "machdep.cpufreq.cpu%u.current", i);
- freqSize = sizeof(freq);
- if (sysctlbyname(name, &freq, &freqSize, NULL, 0) != -1) {
- this->cpuData[i + 1].frequency = freq; /* already in MHz */
- match = true;
- }
- }
-
- if (match) {
- return;
- }
-
- /*
- * Iterate through legacy sysctl nodes for single-core frequency until
- * we find a match...
- */
- for (size_t i = 0; i < ARRAYSIZE(freqSysctls); i++) {
- freqSize = sizeof(freq);
- if (sysctlbyname(freqSysctls[i].name, &freq, &freqSize, NULL, 0) != -1) {
- freq /= freqSysctls[i].scale; /* scale to MHz */
- match = true;
- break;
- }
- }
-
- if (match) {
- for (unsigned int i = 0; i < cpus; i++) {
- this->cpuData[i + 1].frequency = freq;
- }
- }
-}
-
-void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
+void ProcessList_goThroughEntries(ProcessList* super) {
NetBSDProcessList* npl = (NetBSDProcessList*) super;
- NetBSDProcessList_scanMemoryInfo(super->host);
- NetBSDProcessList_scanCPUTime(npl);
-
- if (super->settings->showCPUFrequency) {
- NetBSDProcessList_scanCPUFrequency(npl);
- }
-
- // in pause mode only gather global data for meters (CPU/memory/...)
- if (pauseProcessUpdate) {
- return;
- }
-
NetBSDProcessList_scanProcs(npl);
}
-
-Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
- Machine* this = xCalloc(1, sizeof(Machine));
- Machine_init(this, usersTable, userId);
- return this;
-}
-
-void Machine_delete(Machine* host) {
- free(host);
-}
-
-bool Machine_isCPUonline(const Machine* host, unsigned int id) {
- assert(id < host->existingCPUs);
-
- // TODO: Support detecting online / offline CPUs.
- return true;
-}

© 2014-2024 Faster IT GmbH | imprint | privacy policy