diff options
author | Nathan Scott <nathans@redhat.com> | 2023-05-02 09:02:22 +1000 |
---|---|---|
committer | Nathan Scott <nathans@redhat.com> | 2023-05-08 13:06:07 +1000 |
commit | 0bdade1b6cb40c5bd374a93ac0489058a7421bb5 (patch) | |
tree | 0e0225f7dbf6867402c5ed3481a705d01941f42e /netbsd | |
parent | e4ebe18b67c366d367231a1123b057c82018cf5b (diff) |
Introduce Machine class for host-specific info (split from ProcessList)
First stage in sanitizing the process list structure so that htop
can support other types of lists too (cgroups, filesystems, ...),
in the not-too-distant future.
This introduces struct Machine for system-wide information while
keeping process-list information in ProcessList (now much less).
Next step is to propogate this separation into each platform, to
match these core changes.
Diffstat (limited to 'netbsd')
-rw-r--r-- | netbsd/NetBSDProcess.c | 4 | ||||
-rw-r--r-- | netbsd/NetBSDProcess.h | 4 | ||||
-rw-r--r-- | netbsd/NetBSDProcessList.c | 64 | ||||
-rw-r--r-- | netbsd/NetBSDProcessList.h | 8 | ||||
-rw-r--r-- | netbsd/Platform.c | 21 |
5 files changed, 61 insertions, 40 deletions
diff --git a/netbsd/NetBSDProcess.c b/netbsd/NetBSDProcess.c index 4d4ac4ee..bdb0f50c 100644 --- a/netbsd/NetBSDProcess.c +++ b/netbsd/NetBSDProcess.c @@ -212,10 +212,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; } diff --git a/netbsd/NetBSDProcess.h b/netbsd/NetBSDProcess.h index b9e6b26a..1c068a4a 100644 --- a/netbsd/NetBSDProcess.h +++ b/netbsd/NetBSDProcess.h @@ -12,9 +12,9 @@ in the source distribution for its full text. #include <stdbool.h> +#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 index 381672d6..b7b6915a 100644 --- a/netbsd/NetBSDProcessList.c +++ b/netbsd/NetBSDProcessList.c @@ -54,6 +54,7 @@ static const struct { 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 @@ -72,8 +73,8 @@ static void NetBSDProcessList_updateCPUcount(ProcessList* super) { value = 1; } - if (value != super->activeCPUs) { - super->activeCPUs = value; + if (value != host->activeCPUs) { + host->activeCPUs = value; change = true; } @@ -81,12 +82,12 @@ static void NetBSDProcessList_updateCPUcount(ProcessList* super) { size = sizeof(value); r = sysctl(mib_ncpu_existing, 2, &value, &size, NULL, 0); if (r < 0 || value < 1) { - value = super->activeCPUs; + value = host->activeCPUs; } - if (value != super->existingCPUs) { + if (value != host->existingCPUs) { opl->cpuData = xReallocArray(opl->cpuData, value + 1, sizeof(CPUData)); - super->existingCPUs = value; + host->existingCPUs = value; change = true; } @@ -97,7 +98,7 @@ static void NetBSDProcessList_updateCPUcount(ProcessList* super) { dAvg->totalTime = 1; dAvg->totalPeriod = 1; - for (unsigned int i = 0; i < super->existingCPUs; i++) { + for (unsigned int i = 0; i < host->existingCPUs; i++) { CPUData* d = &opl->cpuData[i + 1]; memset(d, '\0', sizeof(CPUData)); d->totalTime = 1; @@ -106,14 +107,14 @@ static void NetBSDProcessList_updateCPUcount(ProcessList* super) { } } -ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) { +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), usersTable, pidMatchList, userId); + ProcessList_init(pl, Class(NetBSDProcess), host, pidMatchList); NetBSDProcessList_updateCPUcount(pl); @@ -147,7 +148,7 @@ void ProcessList_delete(ProcessList* this) { free(this); } -static void NetBSDProcessList_scanMemoryInfo(ProcessList* pl) { +static void NetBSDProcessList_scanMemoryInfo(Machine* host) { static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP2}; struct uvmexp_sysctl uvmexp; size_t size_uvmexp = sizeof(uvmexp); @@ -156,12 +157,12 @@ static void NetBSDProcessList_scanMemoryInfo(ProcessList* pl) { 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; + 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; } static void NetBSDProcessList_updateExe(const struct kinfo_proc2* kproc, Process* proc) { @@ -262,7 +263,8 @@ static double getpcpu(const struct kinfo_proc2* kp) { } static void NetBSDProcessList_scanProcs(NetBSDProcessList* this) { - const Settings* settings = this->super.settings; + const Machine* host = this->super.host; + const Settings* settings = host->settings; bool hideKernelThreads = settings->hideKernelThreads; bool hideUserlandThreads = settings->hideUserlandThreads; int count = 0; @@ -313,14 +315,14 @@ static void NetBSDProcessList_scanProcs(NetBSDProcessList* this) { if (proc->st_uid != kproc->p_uid) { proc->st_uid = kproc->p_uid; - proc->user = UsersTable_getRef(this->super.usersTable, proc->st_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 * pageSizeKB) / (double)(this->super.totalMem) * 100.0; - proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0, this->super.activeCPUs * 100.0); + proc->percent_mem = (proc->m_resident * pageSizeKB) / (double)(host->totalMem) * 100.0; + proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0, host->activeCPUs * 100.0); Process_updateCPUFieldWidths(proc->percent_cpu); proc->nlwp = kproc->p_nlwps; @@ -411,10 +413,11 @@ static void kernelCPUTimesToHtop(const u_int64_t* times, CPUData* cpu) { } 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 < this->super.existingCPUs; i++) { + for (unsigned int i = 0; i < host->existingCPUs; i++) { getKernelCPUTimes(i, kernelTimes); CPUData* cpu = &this->cpuData[i + 1]; kernelCPUTimesToHtop(kernelTimes, cpu); @@ -427,14 +430,15 @@ static void NetBSDProcessList_scanCPUTime(NetBSDProcessList* this) { } for (int i = 0; i < CPUSTATES; i++) { - avg[i] /= this->super.activeCPUs; + avg[i] /= host->activeCPUs; } kernelCPUTimesToHtop(avg, &this->cpuData[0]); } static void NetBSDProcessList_scanCPUFrequency(NetBSDProcessList* this) { - unsigned int cpus = this->super.existingCPUs; + const Machine* host = this->super.host; + unsigned int cpus = host->existingCPUs; bool match = false; char name[64]; long int freq = 0; @@ -481,7 +485,7 @@ static void NetBSDProcessList_scanCPUFrequency(NetBSDProcessList* this) { void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { NetBSDProcessList* npl = (NetBSDProcessList*) super; - NetBSDProcessList_scanMemoryInfo(super); + NetBSDProcessList_scanMemoryInfo(super->host); NetBSDProcessList_scanCPUTime(npl); if (super->settings->showCPUFrequency) { @@ -496,8 +500,18 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { NetBSDProcessList_scanProcs(npl); } -bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id) { - assert(id < super->existingCPUs); +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; diff --git a/netbsd/NetBSDProcessList.h b/netbsd/NetBSDProcessList.h index d5a757fe..0fe18b03 100644 --- a/netbsd/NetBSDProcessList.h +++ b/netbsd/NetBSDProcessList.h @@ -49,10 +49,16 @@ typedef struct NetBSDProcessList_ { } NetBSDProcessList; -ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId); +ProcessList* ProcessList_new(Machine* host, Hashtable* pidMatchList); void ProcessList_delete(ProcessList* this); void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate); +Machine* Machine_new(UsersTable* usersTable, uid_t userId); + +bool Machine_isCPUonline(const Machine* host, unsigned int id); + +void Machine_delete(Machine* host); + #endif diff --git a/netbsd/Platform.c b/netbsd/Platform.c index 1cf16067..ea81a26a 100644 --- a/netbsd/Platform.c +++ b/netbsd/Platform.c @@ -237,7 +237,8 @@ int Platform_getMaxPid(void) { } double Platform_setCPUValues(Meter* this, int cpu) { - const NetBSDProcessList* npl = (const NetBSDProcessList*) this->pl; + const Machine* host = this->host; + const NetBSDProcessList* npl = (const NetBSDProcessList*) host->pl; const CPUData* cpuData = &npl->cpuData[cpu]; double total = cpuData->totalPeriod == 0 ? 1 : cpuData->totalPeriod; double totalPercent; @@ -245,7 +246,7 @@ double Platform_setCPUValues(Meter* this, int cpu) { 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; @@ -269,20 +270,20 @@ double Platform_setCPUValues(Meter* this, int cpu) { } void Platform_setMemoryValues(Meter* this) { - const ProcessList* pl = this->pl; - this->total = pl->totalMem; - this->values[MEMORY_METER_USED] = pl->usedMem; - this->values[MEMORY_METER_BUFFERS] = pl->buffersMem; + const Machine* host = this->host; + this->total = host->totalMem; + this->values[MEMORY_METER_USED] = host->usedMem; + this->values[MEMORY_METER_BUFFERS] = host->buffersMem; // 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_CACHE] = pl->cachedMem; + 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[SWAP_METER_USED] = pl->usedSwap; + 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" } |