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. --- darwin/DarwinProcess.c | 9 +++++---- darwin/DarwinProcess.h | 4 ++-- darwin/DarwinProcessList.c | 31 +++++++++++++++++++++---------- darwin/DarwinProcessList.h | 8 ++++++-- darwin/Platform.c | 15 ++++++++------- 5 files changed, 42 insertions(+), 25 deletions(-) (limited to 'darwin') diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index 6027c25b..22004e36 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -51,10 +51,10 @@ const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [TRANSLATED] = { .name = "TRANSLATED", .title = "T ", .description = "Translation info (T translated, N native)", .flags = 0, }, }; -Process* DarwinProcess_new(const Settings* settings) { +Process* DarwinProcess_new(const Machine* host) { DarwinProcess* this = xCalloc(1, sizeof(DarwinProcess)); Object_setClass(this, Class(DarwinProcess)); - Process_init(&this->super, settings); + Process_init(&this->super, host); this->utime = 0; this->stime = 0; @@ -291,6 +291,7 @@ static char* DarwinProcess_getDevname(dev_t dev) { void DarwinProcess_setFromKInfoProc(Process* proc, const struct kinfo_proc* ps, bool exists) { DarwinProcess* dp = (DarwinProcess*)proc; + const Settings* settings = proc->host->settings; const struct extern_proc* ep = &ps->kp_proc; @@ -328,7 +329,7 @@ void DarwinProcess_setFromKInfoProc(Process* proc, const struct kinfo_proc* ps, DarwinProcess_updateExe(ep->p_pid, proc); DarwinProcess_updateCmdLine(ps, proc); - if (proc->settings->ss->flags & PROCESS_FLAG_CWD) { + if (settings->ss->flags & PROCESS_FLAG_CWD) { DarwinProcess_updateCwd(ep->p_pid, proc); } } @@ -341,7 +342,7 @@ void DarwinProcess_setFromKInfoProc(Process* proc, const struct kinfo_proc* ps, * To mitigate this we only fetch TTY information if the TTY * field is enabled in the settings. */ - if (proc->settings->ss->flags & PROCESS_FLAG_TTY) { + if (settings->ss->flags & PROCESS_FLAG_TTY) { proc->tty_name = DarwinProcess_getDevname(proc->tty_nr); if (!proc->tty_name) { /* devname failed: prevent us from calling it again */ diff --git a/darwin/DarwinProcess.h b/darwin/DarwinProcess.h index bd179746..89a0576d 100644 --- a/darwin/DarwinProcess.h +++ b/darwin/DarwinProcess.h @@ -9,7 +9,7 @@ in the source distribution for its full text. #include -#include "Settings.h" +#include "Machine.h" #include "darwin/DarwinProcessList.h" @@ -28,7 +28,7 @@ extern const ProcessClass DarwinProcess_class; extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD]; -Process* DarwinProcess_new(const Settings* settings); +Process* DarwinProcess_new(const Machine* settings); void Process_delete(Object* cast); diff --git a/darwin/DarwinProcessList.c b/darwin/DarwinProcessList.c index 6e9eecf7..16c44d2c 100644 --- a/darwin/DarwinProcessList.c +++ b/darwin/DarwinProcessList.c @@ -89,15 +89,15 @@ static struct kinfo_proc* ProcessList_getKInfoProcs(size_t* count) { CRT_fatalError("Unable to get kinfo_procs"); } -ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) { +ProcessList* ProcessList_new(Machine* host, Hashtable* pidMatchList) { DarwinProcessList* this = xCalloc(1, sizeof(DarwinProcessList)); - ProcessList_init(&this->super, Class(DarwinProcess), usersTable, pidMatchList, userId); + ProcessList_init(&this->super, Class(DarwinProcess), host, pidMatchList); /* Initialize the CPU information */ - this->super.activeCPUs = ProcessList_allocateCPULoadInfo(&this->prev_load); + host->activeCPUs = ProcessList_allocateCPULoadInfo(&this->prev_load); // TODO: support offline CPUs and hot swapping - this->super.existingCPUs = this->super.activeCPUs; + host->existingCPUs = host->activeCPUs; ProcessList_getHostInfo(&this->host_info); ProcessList_allocateCPULoadInfo(&this->curr_load); @@ -123,6 +123,7 @@ void ProcessList_delete(ProcessList* this) { void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { DarwinProcessList* dpl = (DarwinProcessList*)super; + const Machine* host = super->host; bool preExisting = true; struct kinfo_proc* ps; size_t count; @@ -142,13 +143,13 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { /* Get the time difference */ dpl->global_diff = 0; - for (unsigned int i = 0; i < dpl->super.existingCPUs; ++i) { + for (unsigned int i = 0; i < host->existingCPUs; ++i) { for (size_t j = 0; j < CPU_STATE_MAX; ++j) { dpl->global_diff += dpl->curr_load[i].cpu_ticks[j] - dpl->prev_load[i].cpu_ticks[j]; } } - const double time_interval_ns = Platform_schedulerTicksToNanoseconds(dpl->global_diff) / (double) dpl->super.activeCPUs; + const double time_interval_ns = Platform_schedulerTicksToNanoseconds(dpl->global_diff) / (double) host->activeCPUs; /* Clear the thread counts */ super->kernelThreads = 0; @@ -173,7 +174,7 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { if (proc->super.st_uid != ps[i].kp_eproc.e_ucred.cr_uid) { proc->super.st_uid = ps[i].kp_eproc.e_ucred.cr_uid; - proc->super.user = UsersTable_getRef(super->usersTable, proc->super.st_uid); + proc->super.user = UsersTable_getRef(host->usersTable, proc->super.st_uid); } // Disabled for High Sierra due to bug in macOS High Sierra @@ -193,11 +194,21 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { free(ps); } -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 offline CPUs and hot swapping - (void) super; (void) id; + (void) host; (void) id; return true; } diff --git a/darwin/DarwinProcessList.h b/darwin/DarwinProcessList.h index ec504bcf..128896ae 100644 --- a/darwin/DarwinProcessList.h +++ b/darwin/DarwinProcessList.h @@ -28,12 +28,16 @@ typedef struct DarwinProcessList_ { ZfsArcStats zfs; } DarwinProcessList; -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/darwin/Platform.c b/darwin/Platform.c index 20bfec26..71d08247 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -233,13 +233,13 @@ int Platform_getMaxPid(void) { } static double Platform_setCPUAverageValues(Meter* mtr) { - const ProcessList* dpl = mtr->pl; - unsigned int activeCPUs = dpl->activeCPUs; + const Machine* host = mtr->host; + unsigned int activeCPUs = host->activeCPUs; double sumNice = 0.0; double sumNormal = 0.0; double sumKernel = 0.0; double sumPercent = 0.0; - for (unsigned int i = 1; i <= dpl->existingCPUs; i++) { + for (unsigned int i = 1; i <= host->existingCPUs; i++) { sumPercent += Platform_setCPUValues(mtr, i); sumNice += mtr->values[CPU_METER_NICE]; sumNormal += mtr->values[CPU_METER_NORMAL]; @@ -257,7 +257,7 @@ double Platform_setCPUValues(Meter* mtr, unsigned int cpu) { return Platform_setCPUAverageValues(mtr); } - const DarwinProcessList* dpl = (const DarwinProcessList*)mtr->pl; + const DarwinProcessList* dpl = (const DarwinProcessList*)mtr->host->pl; const processor_cpu_load_info_t prev = &dpl->prev_load[cpu - 1]; const processor_cpu_load_info_t curr = &dpl->curr_load[cpu - 1]; double total = 0; @@ -286,7 +286,8 @@ double Platform_setCPUValues(Meter* mtr, unsigned int cpu) { } void Platform_setMemoryValues(Meter* mtr) { - const DarwinProcessList* dpl = (const DarwinProcessList*)mtr->pl; + const Machine* host = mtr->host; + const DarwinProcessList* dpl = (const DarwinProcessList*) host->pl; const struct vm_statistics* vm = &dpl->vm_stats; double page_K = (double)vm_page_size / (double)1024; @@ -312,13 +313,13 @@ void Platform_setSwapValues(Meter* mtr) { } void Platform_setZfsArcValues(Meter* this) { - const DarwinProcessList* dpl = (const DarwinProcessList*) this->pl; + const DarwinProcessList* dpl = (const DarwinProcessList*) this->host->pl; ZfsArcMeter_readStats(this, &(dpl->zfs)); } void Platform_setZfsCompressedArcValues(Meter* this) { - const DarwinProcessList* dpl = (const DarwinProcessList*) this->pl; + const DarwinProcessList* dpl = (const DarwinProcessList*) this->host->pl; ZfsCompressedArcMeter_readStats(this, &(dpl->zfs)); } -- cgit v1.2.3