From 0bdade1b6cb40c5bd374a93ac0489058a7421bb5 Mon Sep 17 00:00:00 2001 From: Nathan Scott Date: Tue, 2 May 2023 09:02:22 +1000 Subject: 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. --- openbsd/OpenBSDProcess.c | 4 +-- openbsd/OpenBSDProcess.h | 4 +-- openbsd/OpenBSDProcessList.c | 60 +++++++++++++++++++++++++++----------------- openbsd/OpenBSDProcessList.h | 8 ++++-- openbsd/Platform.c | 21 ++++++++-------- 5 files changed, 58 insertions(+), 39 deletions(-) (limited to 'openbsd') diff --git a/openbsd/OpenBSDProcess.c b/openbsd/OpenBSDProcess.c index c2f2ed4c..0875dc00 100644 --- a/openbsd/OpenBSDProcess.c +++ b/openbsd/OpenBSDProcess.c @@ -204,10 +204,10 @@ const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { }; -Process* OpenBSDProcess_new(const Settings* settings) { +Process* OpenBSDProcess_new(const Machine* host) { OpenBSDProcess* this = xCalloc(1, sizeof(OpenBSDProcess)); Object_setClass(this, Class(OpenBSDProcess)); - Process_init(&this->super, settings); + Process_init(&this->super, host); return &this->super; } diff --git a/openbsd/OpenBSDProcess.h b/openbsd/OpenBSDProcess.h index 898c5377..aac4b318 100644 --- a/openbsd/OpenBSDProcess.h +++ b/openbsd/OpenBSDProcess.h @@ -10,9 +10,9 @@ in the source distribution for its full text. #include +#include "Machine.h" #include "Object.h" #include "Process.h" -#include "Settings.h" typedef struct OpenBSDProcess_ { @@ -26,7 +26,7 @@ extern const ProcessClass OpenBSDProcess_class; extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD]; -Process* OpenBSDProcess_new(const Settings* settings); +Process* OpenBSDProcess_new(const Machine* host); void Process_delete(Object* cast); diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index 6ad3a760..c83eb543 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -38,6 +38,7 @@ static int pageSizeKB; static void OpenBSDProcessList_updateCPUcount(ProcessList* super) { OpenBSDProcessList* opl = (OpenBSDProcessList*) super; + Machine* host = super->host; const int nmib[] = { CTL_HW, HW_NCPU }; const int mib[] = { CTL_HW, HW_NCPUONLINE }; int r; @@ -51,20 +52,20 @@ static void OpenBSDProcessList_updateCPUcount(ProcessList* super) { value = 1; } - if (value != super->activeCPUs) { - super->activeCPUs = value; + if (value != host->activeCPUs) { + host->activeCPUs = value; change = true; } size = sizeof(value); r = sysctl(nmib, 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; } @@ -75,7 +76,7 @@ static void OpenBSDProcessList_updateCPUcount(ProcessList* super) { dAvg->totalPeriod = 1; dAvg->online = true; - 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; @@ -94,14 +95,14 @@ static void OpenBSDProcessList_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]; OpenBSDProcessList* opl = xCalloc(1, sizeof(OpenBSDProcessList)); ProcessList* pl = (ProcessList*) opl; - ProcessList_init(pl, Class(OpenBSDProcess), usersTable, pidMatchList, userId); + ProcessList_init(pl, Class(OpenBSDProcess), host, pidMatchList); OpenBSDProcessList_updateCPUcount(pl); @@ -138,6 +139,7 @@ void ProcessList_delete(ProcessList* this) { } static void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { + Machine* host = pl->host; const int uvmexp_mib[] = { CTL_VM, VM_UVMEXP }; struct uvmexp uvmexp; size_t size_uvmexp = sizeof(uvmexp); @@ -146,8 +148,8 @@ static void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { CRT_fatalError("uvmexp sysctl call failed"); } - pl->totalMem = uvmexp.npages * pageSizeKB; - pl->usedMem = (uvmexp.npages - uvmexp.free - uvmexp.paging) * pageSizeKB; + host->totalMem = uvmexp.npages * pageSizeKB; + host->usedMem = (uvmexp.npages - uvmexp.free - uvmexp.paging) * pageSizeKB; // Taken from OpenBSD systat/iostat.c, top/machine.c and uvm_sysctl(9) const int bcache_mib[] = { CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT }; @@ -158,7 +160,7 @@ static void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { CRT_fatalError("cannot get vfs.bcachestat"); } - pl->cachedMem = bcstats.numbufpages * pageSizeKB; + host->cachedMem = bcstats.numbufpages * pageSizeKB; /* * Copyright (c) 1994 Thorsten Lockert @@ -183,10 +185,10 @@ static void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { } } - pl->totalSwap = total; - pl->usedSwap = used; + host->totalSwap = total; + host->usedSwap = used; } else { - pl->totalSwap = pl->usedSwap = 0; + host->totalSwap = host->usedSwap = 0; } } @@ -270,7 +272,8 @@ static double getpcpu(const struct kinfo_proc* kp) { } static void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { - const Settings* settings = this->super.settings; + Machine* host = this->super.host; + const Settings* settings = host->settings; const bool hideKernelThreads = settings->hideKernelThreads; const bool hideUserlandThreads = settings->hideUserlandThreads; int count = 0; @@ -331,8 +334,8 @@ static void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { proc->m_virt = kproc->p_vm_dsize * pageSizeKB; proc->m_resident = kproc->p_vm_rssize * pageSizeKB; - proc->percent_mem = proc->m_resident / (float)this->super.totalMem * 100.0F; - proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0F, this->super.activeCPUs * 100.0F); + proc->percent_mem = proc->m_resident / (float)host->totalMem * 100.0F; + proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0F, host->activeCPUs * 100.0F); Process_updateCPUFieldWidths(proc->percent_cpu); proc->nice = kproc->p_nice - 20; @@ -345,7 +348,7 @@ static void OpenBSDProcessList_scanProcs(OpenBSDProcessList* 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); } /* Taken from: https://github.com/openbsd/src/blob/6a38af0976a6870911f4b2de19d8da28723a5778/sys/sys/proc.h#L420 */ @@ -422,10 +425,11 @@ static void kernelCPUTimesToHtop(const u_int64_t* times, CPUData* cpu) { } static void OpenBSDProcessList_scanCPUTime(OpenBSDProcessList* this) { + 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++) { CPUData* cpu = &this->cpuData[i + 1]; if (!cpu->online) { @@ -446,7 +450,7 @@ static void OpenBSDProcessList_scanCPUTime(OpenBSDProcessList* this) { } for (int i = 0; i < CPUSTATES; i++) { - avg[i] /= this->super.activeCPUs; + avg[i] /= host->activeCPUs; } kernelCPUTimesToHtop(avg, &this->cpuData[0]); @@ -478,9 +482,19 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { OpenBSDProcessList_scanProcs(opl); } -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); - const OpenBSDProcessList* opl = (const OpenBSDProcessList*) super; + const OpenBSDProcessList* opl = (const OpenBSDProcessList*) host->pl; return opl->cpuData[id + 1].online; } diff --git a/openbsd/OpenBSDProcessList.h b/openbsd/OpenBSDProcessList.h index 99de96cd..6ca2ba2c 100644 --- a/openbsd/OpenBSDProcessList.h +++ b/openbsd/OpenBSDProcessList.h @@ -49,12 +49,16 @@ typedef struct OpenBSDProcessList_ { } OpenBSDProcessList; -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); -bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id); +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/openbsd/Platform.c b/openbsd/Platform.c index 707d2106..03c36856 100644 --- a/openbsd/Platform.c +++ b/openbsd/Platform.c @@ -181,7 +181,8 @@ int Platform_getMaxPid(void) { } double Platform_setCPUValues(Meter* this, unsigned int cpu) { - const OpenBSDProcessList* pl = (const OpenBSDProcessList*) this->pl; + const Machine* host = this->host; + const OpenBSDProcessList* pl = (const OpenBSDProcessList*) host->pl; const CPUData* cpuData = &(pl->cpuData[cpu]); double total; double totalPercent; @@ -196,7 +197,7 @@ double Platform_setCPUValues(Meter* this, unsigned 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; @@ -222,12 +223,12 @@ double Platform_setCPUValues(Meter* this, unsigned int cpu) { } void Platform_setMemoryValues(Meter* this) { - const ProcessList* pl = this->pl; - long int usedMem = pl->usedMem; - long int buffersMem = pl->buffersMem; - long int cachedMem = pl->cachedMem; + const Machine* host = this->host; + long int usedMem = host->usedMem; + long int buffersMem = host->buffersMem; + long int cachedMem = host->cachedMem; usedMem -= buffersMem + cachedMem; - this->total = pl->totalMem; + this->total = host->totalMem; this->values[MEMORY_METER_USED] = usedMem; this->values[MEMORY_METER_BUFFERS] = buffersMem; // this->values[MEMORY_METER_SHARED] = "shared memory, like tmpfs and shm" @@ -237,9 +238,9 @@ void Platform_setMemoryValues(Meter* this) { } 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" } -- cgit v1.2.3