From e7372d18a1a661d8c3dba9f51e1f17b5f94171a7 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Wed, 10 Jan 2024 11:17:08 +0100 Subject: New upstream version 3.3.0 --- netbsd/NetBSDMachine.c | 285 +++++++++++++++++++++++++ netbsd/NetBSDMachine.h | 54 +++++ netbsd/NetBSDProcess.c | 31 ++- netbsd/NetBSDProcess.h | 4 +- netbsd/NetBSDProcessList.c | 504 -------------------------------------------- netbsd/NetBSDProcessList.h | 58 ----- netbsd/NetBSDProcessTable.c | 273 ++++++++++++++++++++++++ netbsd/NetBSDProcessTable.h | 24 +++ netbsd/Platform.c | 63 +++--- netbsd/Platform.h | 34 ++- 10 files changed, 720 insertions(+), 610 deletions(-) create mode 100644 netbsd/NetBSDMachine.c create mode 100644 netbsd/NetBSDMachine.h delete mode 100644 netbsd/NetBSDProcessList.c delete mode 100644 netbsd/NetBSDProcessList.h create mode 100644 netbsd/NetBSDProcessTable.c create mode 100644 netbsd/NetBSDProcessTable.h (limited to 'netbsd') diff --git a/netbsd/NetBSDMachine.c b/netbsd/NetBSDMachine.c new file mode 100644 index 0000000..79c50c1 --- /dev/null +++ b/netbsd/NetBSDMachine.c @@ -0,0 +1,285 @@ +/* +htop - NetBSDMachine.c +(C) 2014 Hisham H. Muhammad +(C) 2015 Michael McConville +(C) 2021 Santhosh Raju +(C) 2021 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" // IWYU pragma: keep + +#include "netbsd/NetBSDMachine.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "CRT.h" +#include "Machine.h" +#include "Macros.h" +#include "Object.h" +#include "Settings.h" +#include "XUtils.h" + + +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 NetBSDMachine_updateCPUcount(NetBSDMachine* this) { + Machine* super = &this->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) { + this->cpuData = xReallocArray(this->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 = &this->cpuData[0]; + memset(dAvg, '\0', sizeof(CPUData)); + dAvg->totalTime = 1; + dAvg->totalPeriod = 1; + + for (unsigned int i = 0; i < super->existingCPUs; i++) { + CPUData* d = &this->cpuData[i + 1]; + memset(d, '\0', sizeof(CPUData)); + d->totalTime = 1; + d->totalPeriod = 1; + } + } +} + +Machine* Machine_new(UsersTable* usersTable, uid_t userId) { + const int fmib[] = { CTL_KERN, KERN_FSCALE }; + size_t size; + char errbuf[_POSIX2_LINE_MAX]; + + NetBSDMachine* this = xCalloc(1, sizeof(NetBSDMachine)); + Machine* super = &this->super; + Machine_init(super, usersTable, userId); + + NetBSDMachine_updateCPUcount(this); + + size = sizeof(this->fscale); + if (sysctl(fmib, 2, &this->fscale, &size, NULL, 0) < 0 || this->fscale <= 0) { + CRT_fatalError("fscale sysctl call failed"); + } + + if ((this->pageSize = sysconf(_SC_PAGESIZE)) == -1) + CRT_fatalError("pagesize sysconf call failed"); + this->pageSizeKB = this->pageSize / ONE_K; + + this->kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); + if (this->kd == NULL) { + CRT_fatalError("kvm_openfiles() failed"); + } + + return super; +} + +void Machine_delete(Machine* super) { + NetBSDMachine* this = (NetBSDMachine*) super; + + Machine_done(super); + + if (this->kd) { + kvm_close(this->kd); + } + free(this->cpuData); + free(this); +} + +static void NetBSDMachine_scanMemoryInfo(NetBSDMachine* this) { + Machine* super = &this->super; + + 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"); + } + + super->totalMem = uvmexp.npages * this->pageSizeKB; + super->buffersMem = 0; + super->cachedMem = (uvmexp.filepages + uvmexp.execpages) * this->pageSizeKB; + super->usedMem = (uvmexp.active + uvmexp.wired) * this->pageSizeKB; + super->totalSwap = uvmexp.swpages * this->pageSizeKB; + super->usedSwap = uvmexp.swpginuse * this->pageSizeKB; +} + +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 NetBSDMachine_scanCPUTime(NetBSDMachine* this) { + const Machine* super = &this->super; + + u_int64_t kernelTimes[CPUSTATES] = {0}; + u_int64_t avg[CPUSTATES] = {0}; + + for (unsigned int i = 0; i < super->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] /= super->activeCPUs; + } + + kernelCPUTimesToHtop(avg, &this->cpuData[0]); +} + +static void NetBSDMachine_scanCPUFrequency(NetBSDMachine* this) { + const Machine* super = &this->super; + unsigned int cpus = super->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 Machine_scan(Machine* super) { + NetBSDMachine* this = (NetBSDMachine*) super; + + NetBSDMachine_scanMemoryInfo(this); + NetBSDMachine_scanCPUTime(this); + + if (super->settings->showCPUFrequency) { + NetBSDMachine_scanCPUFrequency(this); + } +} + +bool Machine_isCPUonline(const Machine* host, unsigned int id) { + assert(id < host->existingCPUs); + (void)host; (void)id; + + // TODO: Support detecting online / offline CPUs. + return true; +} diff --git a/netbsd/NetBSDMachine.h b/netbsd/NetBSDMachine.h new file mode 100644 index 0000000..9d3aa0b --- /dev/null +++ b/netbsd/NetBSDMachine.h @@ -0,0 +1,54 @@ +#ifndef HEADER_NetBSDMachine +#define HEADER_NetBSDMachine +/* +htop - NetBSDMachine.h +(C) 2014 Hisham H. Muhammad +(C) 2015 Michael McConville +(C) 2021 Santhosh Raju +(C) 2021 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include +#include +#include + +#include "Machine.h" +#include "ProcessTable.h" + + +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; + + double frequency; +} CPUData; + +typedef struct NetBSDMachine_ { + Machine super; + kvm_t* kd; + + long fscale; + int pageSize; + int pageSizeKB; + + CPUData* cpuData; +} NetBSDMachine; + +#endif diff --git a/netbsd/NetBSDProcess.c b/netbsd/NetBSDProcess.c index 4d4ac4e..f58cdf2 100644 --- a/netbsd/NetBSDProcess.c +++ b/netbsd/NetBSDProcess.c @@ -8,6 +8,8 @@ Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ +#include "config.h" // IWYU pragma: keep + #include "netbsd/NetBSDProcess.h" #include @@ -212,10 +214,10 @@ const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { }; -Process* NetBSDProcess_new(const Settings* settings) { +Process* NetBSDProcess_new(const Machine* host) { NetBSDProcess* this = xCalloc(1, sizeof(NetBSDProcess)); Object_setClass(this, Class(NetBSDProcess)); - Process_init(&this->super, settings); + Process_init(&this->super, host); return &this->super; } @@ -225,16 +227,20 @@ void Process_delete(Object* cast) { free(this); } -static void NetBSDProcess_writeField(const Process* this, RichString* str, ProcessField field) { +static void NetBSDProcess_rowWriteField(const Row* super, RichString* str, ProcessField field) { + const NetBSDProcess* np = (const NetBSDProcess*) super; + char buffer[256]; buffer[255] = '\0'; int attr = CRT_colors[DEFAULT_COLOR]; + //size_t n = sizeof(buffer) - 1; switch (field) { // add NetBSD-specific fields here default: - Process_writeField(this, str, field); + Process_writeField(&np->super, str, field); return; } + RichString_appendWide(str, attr, buffer); } @@ -254,11 +260,18 @@ static int NetBSDProcess_compareByKey(const Process* v1, const Process* v2, Proc const ProcessClass NetBSDProcess_class = { .super = { - .extends = Class(Process), - .display = Process_display, - .delete = Process_delete, - .compare = Process_compare + .super = { + .extends = Class(Process), + .display = Row_display, + .delete = Process_delete, + .compare = Process_compare + }, + .isHighlighted = Process_rowIsHighlighted, + .isVisible = Process_rowIsVisible, + .matchesFilter = Process_rowMatchesFilter, + .compareByParent = Process_compareByParent, + .sortKeyString = Process_rowGetSortKey, + .writeField = NetBSDProcess_rowWriteField }, - .writeField = NetBSDProcess_writeField, .compareByKey = NetBSDProcess_compareByKey }; diff --git a/netbsd/NetBSDProcess.h b/netbsd/NetBSDProcess.h index b9e6b26..1c068a4 100644 --- a/netbsd/NetBSDProcess.h +++ b/netbsd/NetBSDProcess.h @@ -12,9 +12,9 @@ in the source distribution for its full text. #include +#include "Machine.h" #include "Object.h" #include "Process.h" -#include "Settings.h" typedef struct NetBSDProcess_ { @@ -25,7 +25,7 @@ extern const ProcessClass NetBSDProcess_class; extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD]; -Process* NetBSDProcess_new(const Settings* settings); +Process* NetBSDProcess_new(const Machine* host); void Process_delete(Object* cast); diff --git a/netbsd/NetBSDProcessList.c b/netbsd/NetBSDProcessList.c deleted file mode 100644 index 197a150..0000000 --- a/netbsd/NetBSDProcessList.c +++ /dev/null @@ -1,504 +0,0 @@ -/* -htop - NetBSDProcessList.c -(C) 2014 Hisham H. Muhammad -(C) 2015 Michael McConville -(C) 2021 Santhosh Raju -(C) 2021 htop dev team -Released under the GNU GPLv2+, see the COPYING file -in the source distribution for its full text. -*/ - -#include "netbsd/NetBSDProcessList.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "CRT.h" -#include "Macros.h" -#include "Object.h" -#include "Process.h" -#include "ProcessList.h" -#include "Settings.h" -#include "XUtils.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; - - // 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; - } - } -} - -ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* pidMatchList, uid_t userId) { - 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), usersTable, dynamicMeters, dynamicColumns, pidMatchList, userId); - - NetBSDProcessList_updateCPUcount(pl); - - 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; - - npl->kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); - if (npl->kd == NULL) { - CRT_fatalError("kvm_openfiles() failed"); - } - - return pl; -} - -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(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; - pl->buffersMem = 0; - pl->cachedMem = (uvmexp.filepages + uvmexp.execpages) * pageSizeKB; - pl->usedMem = (uvmexp.active + uvmexp.wired) * pageSizeKB; - pl->totalSwap = uvmexp.swpages * pageSizeKB; - pl->usedSwap = uvmexp.swpginuse * pageSizeKB; -} - -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; - } - - /* 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); -} - -static void NetBSDProcessList_updateProcessName(kvm_t* kd, const struct kinfo_proc2* kproc, Process* proc) { - Process_updateComm(proc, kproc->p_comm); - - /* - * Like NetBSD's top(1), we try to fall back to the command name - * (argv[0]) if we fail to construct the full command. - */ - char** arg = kvm_getargv2(kd, kproc, 500); - if (arg == NULL || *arg == NULL) { - Process_updateCmdline(proc, kproc->p_comm, 0, strlen(kproc->p_comm)); - return; - } - - 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) { - Process_updateCmdline(proc, kproc->p_comm, 0, strlen(kproc->p_comm)); - return; - } - - *s = '\0'; - - int start = 0; - int end = 0; - for (int i = 0; arg[i] != NULL; i++) { - size_t n = strlcat(s, arg[i], len); - if (i == 0) { - end = MINIMUM(n, len - 1); - /* check if cmdline ended earlier, e.g 'kdeinit5: Running...' */ - for (int j = end; j > 0; j--) { - if (arg[0][j] == ' ' && arg[0][j - 1] != '\\') { - end = (arg[0][j - 1] == ':') ? (j - 1) : j; - } - } - } - /* the trailing space should get truncated anyway */ - strlcat(s, " ", len); - } - - Process_updateCmdline(proc, s, start, end); - - free(s); -} - -/* - * Borrowed with modifications from NetBSD's top(1). - */ -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; - - 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) { - proc->pid = kproc->p_pid; - 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; - proc->isKernelThread = !!(kproc->p_flag & P_SYSTEM); - proc->isUserlandThread = proc->pid != proc->tgid; - proc->starttime_ctime = kproc->p_ustart_sec; - Process_fillStarttimeBuffer(proc); - ProcessList_add(&this->super, proc); - - 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); - } - - NetBSDProcessList_updateExe(kproc, proc); - NetBSDProcessList_updateProcessName(this->kd, kproc, proc); - } else { - if (settings->updateProcessNames) { - NetBSDProcessList_updateProcessName(this->kd, kproc, proc); - } - } - - if (settings->ss->flags & PROCESS_FLAG_CWD) { - NetBSDProcessList_updateCwd(kproc, proc); - } - - if (proc->st_uid != kproc->p_uid) { - proc->st_uid = kproc->p_uid; - proc->user = UsersTable_getRef(this->super.usersTable, proc->st_uid); - } - - 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; - proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0, this->super.activeCPUs * 100.0); - Process_updateCPUFieldWidths(proc->percent_cpu); - - proc->nlwp = kproc->p_nlwps; - 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; - proc->processor = kproc->p_cpuid; - proc->minflt = kproc->p_uru_minflt; - 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); - - /* 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 */ - switch (kproc->p_realstat) { - case SIDL: proc->state = IDLE; break; - case SACTIVE: - // 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 = RUNNING; break; - case LSRUN: proc->state = RUNNABLE; break; - case LSSLEEP: proc->state = SLEEPING; break; - case LSSTOP: proc->state = STOPPED; break; - default: proc->state = UNKNOWN; - } - if (proc->state != UNKNOWN) - break; - } else { - proc->state = UNKNOWN; - break; - } - } - break; - case SSTOP: proc->state = STOPPED; break; - case SZOMB: proc->state = ZOMBIE; break; - case SDEAD: proc->state = DEFUNCT; break; - default: proc->state = UNKNOWN; - } - - if (Process_isKernelThread(proc)) { - this->super.kernelThreads++; - } else if (Process_isUserlandThread(proc)) { - this->super.userlandThreads++; - } - - this->super.totalTasks++; - if (proc->state == RUNNING) { - 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}; - - for (unsigned int i = 0; i < this->super.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] /= this->super.activeCPUs; - } - - kernelCPUTimesToHtop(avg, &this->cpuData[0]); -} - -static void NetBSDProcessList_scanCPUFrequency(NetBSDProcessList* this) { - unsigned int cpus = this->super.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) { - NetBSDProcessList* npl = (NetBSDProcessList*) super; - - NetBSDProcessList_scanMemoryInfo(super); - 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); -} - -bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id) { - assert(id < super->existingCPUs); - - // TODO: Support detecting online / offline CPUs. - return true; -} diff --git a/netbsd/NetBSDProcessList.h b/netbsd/NetBSDProcessList.h deleted file mode 100644 index d228f48..0000000 --- a/netbsd/NetBSDProcessList.h +++ /dev/null @@ -1,58 +0,0 @@ -#ifndef HEADER_NetBSDProcessList -#define HEADER_NetBSDProcessList -/* -htop - NetBSDProcessList.h -(C) 2014 Hisham H. Muhammad -(C) 2015 Michael McConville -(C) 2021 Santhosh Raju -(C) 2021 htop dev team -Released under the GNU GPLv2+, see the COPYING file -in the source distribution for its full text. -*/ - -#include -#include -#include - -#include "Hashtable.h" -#include "ProcessList.h" -#include "UsersTable.h" - - -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; - - double frequency; -} CPUData; - -typedef struct NetBSDProcessList_ { - ProcessList super; - kvm_t* kd; - - CPUData* cpuData; -} NetBSDProcessList; - - -ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* pidMatchList, uid_t userId); - -void ProcessList_delete(ProcessList* this); - -void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate); - -#endif diff --git a/netbsd/NetBSDProcessTable.c b/netbsd/NetBSDProcessTable.c new file mode 100644 index 0000000..71ae53e --- /dev/null +++ b/netbsd/NetBSDProcessTable.c @@ -0,0 +1,273 @@ +/* +htop - NetBSDProcessTable.c +(C) 2014 Hisham H. Muhammad +(C) 2015 Michael McConville +(C) 2021 Santhosh Raju +(C) 2021 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" // IWYU pragma: keep + +#include "netbsd/NetBSDProcessTable.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "CRT.h" +#include "Macros.h" +#include "Object.h" +#include "Process.h" +#include "ProcessTable.h" +#include "Settings.h" +#include "XUtils.h" +#include "netbsd/NetBSDMachine.h" +#include "netbsd/NetBSDProcess.h" + + +ProcessTable* ProcessTable_new(Machine* host, Hashtable* pidMatchList) { + NetBSDProcessTable* this = xCalloc(1, sizeof(NetBSDProcessTable)); + Object_setClass(this, Class(ProcessTable)); + + ProcessTable* super = (ProcessTable*) this; + ProcessTable_init(super, Class(NetBSDProcess), host, pidMatchList); + + return super; +} + +void ProcessTable_delete(Object* cast) { + NetBSDProcessTable* this = (NetBSDProcessTable*) cast; + ProcessTable_done(&this->super); + free(this); +} + +static void NetBSDProcessTable_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; + } + + /* Kernel threads return an empty buffer */ + if (buffer[0] == '\0') { + Process_updateExe(proc, NULL); + return; + } + + Process_updateExe(proc, buffer); +} + +static void NetBSDProcessTable_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); +} + +static void NetBSDProcessTable_updateProcessName(kvm_t* kd, const struct kinfo_proc2* kproc, Process* proc) { + Process_updateComm(proc, kproc->p_comm); + + /* + * Like NetBSD's top(1), we try to fall back to the command name + * (argv[0]) if we fail to construct the full command. + */ + char** arg = kvm_getargv2(kd, kproc, 500); + if (arg == NULL || *arg == NULL) { + Process_updateCmdline(proc, kproc->p_comm, 0, strlen(kproc->p_comm)); + return; + } + + 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) { + Process_updateCmdline(proc, kproc->p_comm, 0, strlen(kproc->p_comm)); + return; + } + + *s = '\0'; + + int start = 0; + int end = 0; + for (int i = 0; arg[i] != NULL; i++) { + size_t n = strlcat(s, arg[i], len); + if (i == 0) { + end = MINIMUM(n, len - 1); + /* check if cmdline ended earlier, e.g 'kdeinit5: Running...' */ + for (int j = end; j > 0; j--) { + if (arg[0][j] == ' ' && arg[0][j - 1] != '\\') { + end = (arg[0][j - 1] == ':') ? (j - 1) : j; + } + } + } + /* the trailing space should get truncated anyway */ + strlcat(s, " ", len); + } + + Process_updateCmdline(proc, s, start, end); + + free(s); +} + +/* + * Borrowed with modifications from NetBSD's top(1). + */ +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 / nhost->fscale; +} + +void ProcessTable_goThroughEntries(ProcessTable* super) { + const Machine* host = super->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(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]; + + bool preExisting = false; + Process* proc = ProcessTable_getProcess(super, kproc->p_pid, &preExisting, NetBSDProcess_new); + + proc->super.show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc))); + + if (!preExisting) { + Process_setPid(proc, kproc->p_pid); + Process_setParent(proc, kproc->p_ppid); + Process_setThreadGroup(proc, kproc->p_pid); + proc->tpgid = kproc->p_tpgid; + proc->session = kproc->p_sid; + proc->pgrp = kproc->p__pgid; + proc->isKernelThread = !!(kproc->p_flag & P_SYSTEM); + proc->isUserlandThread = Process_getPid(proc) != Process_getThreadGroup(proc); // eh? + proc->starttime_ctime = kproc->p_ustart_sec; + Process_fillStarttimeBuffer(proc); + ProcessTable_add(super, proc); + + proc->tty_nr = kproc->p_tdev; + // KERN_PROC_TTY_NODEV is a negative constant but the type of + // kproc->p_tdev may be unsigned. + const char* name = ((dev_t)~kproc->p_tdev != (dev_t)~(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); + } + + NetBSDProcessTable_updateExe(kproc, proc); + NetBSDProcessTable_updateProcessName(nhost->kd, kproc, proc); + } else { + if (settings->updateProcessNames) { + NetBSDProcessTable_updateProcessName(nhost->kd, kproc, proc); + } + } + + if (settings->ss->flags & PROCESS_FLAG_CWD) { + NetBSDProcessTable_updateCwd(kproc, proc); + } + + if (proc->st_uid != kproc->p_uid) { + proc->st_uid = kproc->p_uid; + proc->user = UsersTable_getRef(host->usersTable, proc->st_uid); + } + + proc->m_virt = kproc->p_vm_vsize; + proc->m_resident = kproc->p_vm_rssize; + + 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; + 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; + proc->processor = kproc->p_cpuid; + proc->minflt = kproc->p_uru_minflt; + proc->majflt = kproc->p_uru_majflt; + + int nlwps = 0; + 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 */ + switch (kproc->p_realstat) { + case SIDL: proc->state = IDLE; break; + case SACTIVE: + // 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 = RUNNING; break; + case LSRUN: proc->state = RUNNABLE; break; + case LSSLEEP: proc->state = SLEEPING; break; + case LSSTOP: proc->state = STOPPED; break; + default: proc->state = UNKNOWN; + } + + if (proc->state != UNKNOWN) { + break; + } + } else { + proc->state = UNKNOWN; + break; + } + } + break; + case SSTOP: proc->state = STOPPED; break; + case SZOMB: proc->state = ZOMBIE; break; + case SDEAD: proc->state = DEFUNCT; break; + default: proc->state = UNKNOWN; + } + + if (Process_isKernelThread(proc)) { + super->kernelThreads++; + } else if (Process_isUserlandThread(proc)) { + super->userlandThreads++; + } + + super->totalTasks++; + if (proc->state == RUNNING) { + super->runningTasks++; + } + proc->super.updated = true; + } +} diff --git a/netbsd/NetBSDProcessTable.h b/netbsd/NetBSDProcessTable.h new file mode 100644 index 0000000..1bcfa98 --- /dev/null +++ b/netbsd/NetBSDProcessTable.h @@ -0,0 +1,24 @@ +#ifndef HEADER_NetBSDProcessTable +#define HEADER_NetBSDProcessTable +/* +htop - NetBSDProcessTable.h +(C) 2014 Hisham H. Muhammad +(C) 2015 Michael McConville +(C) 2021 Santhosh Raju +(C) 2021 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include +#include + +#include "Hashtable.h" +#include "ProcessTable.h" + + +typedef struct NetBSDProcessTable_ { + ProcessTable super; +} NetBSDProcessTable; + +#endif diff --git a/netbsd/Platform.c b/netbsd/Platform.c index cf6079d..f458c23 100644 --- a/netbsd/Platform.c +++ b/netbsd/Platform.c @@ -9,6 +9,8 @@ Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ +#include "config.h" // IWYU pragma: keep + #include "netbsd/Platform.h" #include @@ -38,13 +40,13 @@ in the source distribution for its full text. #include "ClockMeter.h" #include "DateMeter.h" #include "DateTimeMeter.h" +#include "FileDescriptorMeter.h" #include "HostnameMeter.h" #include "LoadAverageMeter.h" #include "Macros.h" #include "MemoryMeter.h" #include "MemorySwapMeter.h" #include "Meter.h" -#include "ProcessList.h" #include "Settings.h" #include "SignalsPanel.h" #include "SwapMeter.h" @@ -52,8 +54,9 @@ in the source distribution for its full text. #include "TasksMeter.h" #include "UptimeMeter.h" #include "XUtils.h" +#include "generic/fdstat_sysctl.h" +#include "netbsd/NetBSDMachine.h" #include "netbsd/NetBSDProcess.h" -#include "netbsd/NetBSDProcessList.h" /* * The older proplib APIs will be deprecated in NetBSD 10, but we still @@ -179,6 +182,7 @@ const MeterClass* const Platform_meterTypes[] = { &BlankMeter_class, &DiskIOMeter_class, &NetworkIOMeter_class, + &FileDescriptorMeter_class, NULL }; @@ -196,7 +200,7 @@ void Platform_setBindings(Htop_Action* keys) { (void) keys; } -int Platform_getUptime() { +int Platform_getUptime(void) { struct timeval bootTime, currTime; const int mib[2] = { CTL_KERN, KERN_BOOTTIME }; size_t size = sizeof(bootTime); @@ -227,22 +231,23 @@ void Platform_getLoadAverage(double* one, double* five, double* fifteen) { *fifteen = (double) loadAverage.ldavg[2] / loadAverage.fscale; } -int Platform_getMaxPid() { +pid_t Platform_getMaxPid(void) { // https://nxr.netbsd.org/xref/src/sys/sys/ansi.h#__pid_t // pid is assigned as a 32bit Integer. return INT32_MAX; } double Platform_setCPUValues(Meter* this, int cpu) { - const NetBSDProcessList* npl = (const NetBSDProcessList*) this->pl; - const CPUData* cpuData = &npl->cpuData[cpu]; + const Machine* host = this->host; + const NetBSDMachine* nhost = (const NetBSDMachine*) host; + const CPUData* cpuData = &nhost->cpuData[cpu]; double total = cpuData->totalPeriod == 0 ? 1 : cpuData->totalPeriod; double totalPercent; double* v = this->values; v[CPU_METER_NICE] = cpuData->nicePeriod / total * 100.0; v[CPU_METER_NORMAL] = cpuData->userPeriod / total * 100.0; - if (this->pl->settings->detailedCPUTime) { + if (host->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; @@ -251,14 +256,12 @@ double Platform_setCPUValues(Meter* this, int cpu) { v[CPU_METER_IOWAIT] = 0.0; v[CPU_METER_FREQUENCY] = NAN; this->curItems = 8; - totalPercent = v[0] + v[1] + v[2] + v[3]; } else { - v[2] = cpuData->sysAllPeriod / total * 100.0; - v[3] = 0.0; // No steal nor guest on NetBSD - totalPercent = v[0] + v[1] + v[2]; + v[CPU_METER_KERNEL] = cpuData->sysAllPeriod / total * 100.0; + v[CPU_METER_IRQ] = 0.0; // No steal nor guest on NetBSD this->curItems = 4; } - + totalPercent = v[CPU_METER_NICE] + v[CPU_METER_NORMAL] + v[CPU_METER_KERNEL] + v[CPU_METER_IRQ]; totalPercent = CLAMP(totalPercent, 0.0, 100.0); v[CPU_METER_FREQUENCY] = cpuData->frequency; @@ -268,20 +271,22 @@ double Platform_setCPUValues(Meter* this, int cpu) { } void Platform_setMemoryValues(Meter* this) { - const ProcessList* pl = this->pl; - this->total = pl->totalMem; - this->values[0] = pl->usedMem; - this->values[1] = pl->buffersMem; - // this->values[2] = "shared memory, like tmpfs and shm" - this->values[3] = pl->cachedMem; - // this->values[4] = "available memory" + const Machine* host = this->host; + this->total = host->totalMem; + this->values[MEMORY_METER_USED] = host->usedMem; + // this->values[MEMORY_METER_SHARED] = "shared memory, like tmpfs and shm" + // this->values[MEMORY_METER_COMPRESSED] = "compressed memory, like zswap on linux" + this->values[MEMORY_METER_BUFFERS] = host->buffersMem; + this->values[MEMORY_METER_CACHE] = host->cachedMem; + // this->values[MEMORY_METER_AVAILABLE] = "available memory" } void Platform_setSwapValues(Meter* this) { - const ProcessList* pl = this->pl; - this->total = pl->totalSwap; - this->values[0] = pl->usedSwap; - this->values[1] = NAN; + const Machine* host = this->host; + this->total = host->totalSwap; + this->values[SWAP_METER_USED] = host->usedSwap; + // this->values[SWAP_METER_CACHE] = "pages that are both in swap and RAM, like SwapCached on linux" + // this->values[SWAP_METER_FRONTSWAP] = "pages that are accounted to swap but stored elsewhere, like frontswap on linux" } char* Platform_getProcessEnv(pid_t pid) { @@ -338,20 +343,18 @@ end: return env; } -char* Platform_getInodeFilename(pid_t pid, ino_t inode) { +FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) { (void)pid; - (void)inode; return NULL; } -FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) { - (void)pid; - return NULL; +void Platform_getFileDescriptors(double* used, double* max) { + Generic_getFileDescriptors_sysctl(used, max); } bool Platform_getDiskIO(DiskIOData* data) { const int mib[] = { CTL_HW, HW_IOSTATS, sizeof(struct io_sysctl) }; - struct io_sysctl *iostats = NULL; + struct io_sysctl* iostats = NULL; size_t size = 0; for (int retry = 3; retry > 0; retry--) { @@ -414,7 +417,7 @@ bool Platform_getNetworkIO(NetworkIOData* data) { if (ifa->ifa_flags & IFF_LOOPBACK) continue; - const struct if_data* ifd = (const struct if_data *)ifa->ifa_data; + const struct if_data* ifd = (const struct if_data*)ifa->ifa_data; data->bytesReceived += ifd->ifi_ibytes; data->packetsReceived += ifd->ifi_ipackets; diff --git a/netbsd/Platform.h b/netbsd/Platform.h index 3ad51e2..a543f52 100644 --- a/netbsd/Platform.h +++ b/netbsd/Platform.h @@ -55,7 +55,7 @@ int Platform_getUptime(void); void Platform_getLoadAverage(double* one, double* five, double* fifteen); -int Platform_getMaxPid(void); +pid_t Platform_getMaxPid(void); double Platform_setCPUValues(Meter* this, int cpu); @@ -65,10 +65,10 @@ void Platform_setSwapValues(Meter* this); char* Platform_getProcessEnv(pid_t pid); -char* Platform_getInodeFilename(pid_t pid, ino_t inode); - FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid); +void Platform_getFileDescriptors(double* used, double* max); + bool Platform_getDiskIO(DiskIOData* data); bool Platform_getNetworkIO(NetworkIOData* data); @@ -97,7 +97,9 @@ static inline void Platform_gettime_monotonic(uint64_t* msec) { Generic_gettime_monotonic(msec); } -static inline Hashtable* Platform_dynamicMeters(void) { return NULL; } +static inline Hashtable* Platform_dynamicMeters(void) { + return NULL; +} static inline void Platform_dynamicMetersDone(ATTR_UNUSED Hashtable* table) { } @@ -107,12 +109,30 @@ static inline void Platform_dynamicMeterUpdateValues(ATTR_UNUSED Meter* meter) { static inline void Platform_dynamicMeterDisplay(ATTR_UNUSED const Meter* meter, ATTR_UNUSED RichString* out) { } -static inline Hashtable* Platform_dynamicColumns(void) { return NULL; } +static inline Hashtable* Platform_dynamicColumns(void) { + return NULL; +} static inline void Platform_dynamicColumnsDone(ATTR_UNUSED Hashtable* table) { } -static inline const char* Platform_dynamicColumnInit(ATTR_UNUSED unsigned int key) { return NULL; } +static inline const char* Platform_dynamicColumnName(ATTR_UNUSED unsigned int key) { + return NULL; +} + +static inline bool Platform_dynamicColumnWriteField(ATTR_UNUSED const Process* proc, ATTR_UNUSED RichString* str, ATTR_UNUSED unsigned int key) { + return false; +} + +static inline Hashtable* Platform_dynamicScreens(void) { + return NULL; +} + +static inline void Platform_defaultDynamicScreens(ATTR_UNUSED Settings* settings) { } + +static inline void Platform_addDynamicScreen(ATTR_UNUSED ScreenSettings* ss) { } + +static inline void Platform_addDynamicScreenAvailableColumns(ATTR_UNUSED Panel* availableColumns, ATTR_UNUSED const char* screen) { } -static inline bool Platform_dynamicColumnWriteField(ATTR_UNUSED const Process* proc, ATTR_UNUSED RichString* str, ATTR_UNUSED unsigned int key) { return false; } +static inline void Platform_dynamicScreensDone(ATTR_UNUSED Hashtable* screens) { } #endif -- cgit v1.2.3