diff options
author | Nathan Scott <nathans@redhat.com> | 2023-05-02 16:56:18 +1000 |
---|---|---|
committer | Nathan Scott <nathans@redhat.com> | 2023-05-08 13:06:38 +1000 |
commit | 72235d8e098d9d79029dca65122605741e1aafad (patch) | |
tree | 96593b8bd9dc95dc5ab321bd363d36351cbd0a99 | |
parent | 0bdade1b6cb40c5bd374a93ac0489058a7421bb5 (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.
55 files changed, 3672 insertions, 3266 deletions
diff --git a/CommandLine.c b/CommandLine.c index 9e2018ef..8095fa8f 100644 --- a/CommandLine.c +++ b/CommandLine.c @@ -383,9 +383,11 @@ int CommandLine_run(int argc, char** argv) { ScreenManager* scr = ScreenManager_new(header, host, &state, true); ScreenManager_add(scr, (Panel*) panel, -1); - ProcessList_scan(pl, false); + Machine_scan(host); + ProcessList_scan(pl); CommandLine_delay(host, 75); - ProcessList_scan(pl, false); + Machine_scan(host); + ProcessList_scan(pl); if (settings->ss->allBranchesCollapsed) ProcessList_collapseAllBranches(pl); @@ -86,4 +86,6 @@ bool Machine_isCPUonline(const Machine* this, unsigned int id); void Machine_addList(Machine* this, struct ProcessList_ *pl); +void Machine_scan(Machine* this); + #endif diff --git a/Makefile.am b/Makefile.am index 94db4d77..b25d1cb8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -166,6 +166,7 @@ linux_platform_headers = \ linux/IOPriority.h \ linux/IOPriorityPanel.h \ linux/LibSensors.h \ + linux/LinuxMachine.h \ linux/LinuxProcess.h \ linux/LinuxProcessList.h \ linux/Platform.h \ @@ -188,6 +189,7 @@ linux_platform_sources = \ linux/HugePageMeter.c \ linux/IOPriorityPanel.c \ linux/LibSensors.c \ + linux/LinuxMachine.c \ linux/LinuxProcess.c \ linux/LinuxProcessList.c \ linux/Platform.c \ @@ -208,6 +210,7 @@ endif # ------- freebsd_platform_headers = \ + freebsd/FreeBSDMachine.h \ freebsd/FreeBSDProcessList.h \ freebsd/FreeBSDProcess.h \ freebsd/Platform.h \ @@ -223,6 +226,7 @@ freebsd_platform_headers = \ freebsd_platform_sources = \ freebsd/Platform.c \ + freebsd/FreeBSDMachine.c \ freebsd/FreeBSDProcessList.c \ freebsd/FreeBSDProcess.c \ generic/fdstat_sysctl.c \ @@ -242,6 +246,7 @@ endif # ------------ dragonflybsd_platform_headers = \ + dragonflybsd/DragonFlyBSDMachine.h \ dragonflybsd/DragonFlyBSDProcessList.h \ dragonflybsd/DragonFlyBSDProcess.h \ dragonflybsd/Platform.h \ @@ -252,6 +257,7 @@ dragonflybsd_platform_headers = \ generic/uname.h dragonflybsd_platform_sources = \ + dragonflybsd/DragonFlyBSDMachine.c \ dragonflybsd/DragonFlyBSDProcessList.c \ dragonflybsd/DragonFlyBSDProcess.c \ dragonflybsd/Platform.c \ @@ -275,6 +281,7 @@ netbsd_platform_headers = \ generic/uname.h \ netbsd/Platform.h \ netbsd/ProcessField.h \ + netbsd/NetBSDMachine.h \ netbsd/NetBSDProcess.h \ netbsd/NetBSDProcessList.h @@ -284,6 +291,7 @@ netbsd_platform_sources = \ generic/hostname.c \ generic/uname.c \ netbsd/Platform.c \ + netbsd/NetBSDMachine.c \ netbsd/NetBSDProcess.c \ netbsd/NetBSDProcessList.c @@ -299,6 +307,7 @@ openbsd_platform_headers = \ generic/gettime.h \ generic/hostname.h \ generic/uname.h \ + openbsd/OpenBSDMachine.h \ openbsd/OpenBSDProcessList.h \ openbsd/OpenBSDProcess.h \ openbsd/Platform.h \ @@ -308,6 +317,7 @@ openbsd_platform_sources = \ generic/gettime.c \ generic/hostname.c \ generic/uname.c \ + openbsd/OpenBSDMachine.c \ openbsd/OpenBSDProcessList.c \ openbsd/OpenBSDProcess.c \ openbsd/Platform.c @@ -321,6 +331,7 @@ endif # ------ darwin_platform_headers = \ + darwin/DarwinMachine.h \ darwin/DarwinProcess.h \ darwin/DarwinProcessList.h \ darwin/Platform.h \ @@ -338,6 +349,7 @@ darwin_platform_headers = \ darwin_platform_sources = \ darwin/Platform.c \ darwin/PlatformHelpers.c \ + darwin/DarwinMachine.c \ darwin/DarwinProcess.c \ darwin/DarwinProcessList.c \ generic/fdstat_sysctl.c \ @@ -363,6 +375,7 @@ solaris_platform_headers = \ generic/uname.h \ solaris/ProcessField.h \ solaris/Platform.h \ + solaris/SolarisMachine.h \ solaris/SolarisProcess.h \ solaris/SolarisProcessList.h \ zfs/ZfsArcMeter.h \ @@ -374,6 +387,7 @@ solaris_platform_sources = \ generic/hostname.c \ generic/uname.c \ solaris/Platform.c \ + solaris/SolarisMachine.c \ solaris/SolarisProcess.c \ solaris/SolarisProcessList.c \ zfs/ZfsArcMeter.c \ @@ -393,6 +407,7 @@ pcp_platform_headers = \ linux/ZramStats.h \ pcp/PCPDynamicColumn.h \ pcp/PCPDynamicMeter.h \ + pcp/PCPMachine.h \ pcp/PCPMetric.h \ pcp/PCPProcess.h \ pcp/PCPProcessList.h \ @@ -407,6 +422,7 @@ pcp_platform_sources = \ linux/ZramMeter.c \ pcp/PCPDynamicColumn.c \ pcp/PCPDynamicMeter.c \ + pcp/PCPMachine.c \ pcp/PCPMetric.c \ pcp/PCPProcess.c \ pcp/PCPProcessList.c \ @@ -427,12 +443,14 @@ unsupported_platform_headers = \ generic/gettime.h \ unsupported/Platform.h \ unsupported/ProcessField.h \ + unsupported/UnsupportedMachine.h \ unsupported/UnsupportedProcess.h \ unsupported/UnsupportedProcessList.h unsupported_platform_sources = \ generic/gettime.c \ unsupported/Platform.c \ + unsupported/UnsupportedMachine.c \ unsupported/UnsupportedProcess.c \ unsupported/UnsupportedProcessList.c diff --git a/ProcessList.c b/ProcessList.c index 49217b8c..58e63d03 100644 --- a/ProcessList.c +++ b/ProcessList.c @@ -410,13 +410,7 @@ Process* ProcessList_getProcess(ProcessList* this, pid_t pid, bool* preExisting, return proc; } -void ProcessList_scan(ProcessList* this, bool pauseProcessUpdate) { - // in pause mode only gather global data for meters (CPU/memory/...) - if (pauseProcessUpdate) { - ProcessList_goThroughEntries(this, true); - return; - } - +void ProcessList_scan(ProcessList* this) { // mark all process as "dirty" for (int i = 0; i < Vector_size(this->processes); i++) { Process* p = (Process*) Vector_get(this->processes, i); @@ -442,7 +436,7 @@ void ProcessList_scan(ProcessList* this, bool pauseProcessUpdate) { firstScanDone = true; } - ProcessList_goThroughEntries(this, false); + ProcessList_goThroughEntries(this); uid_t maxUid = 0; const Settings* settings = host->settings; diff --git a/ProcessList.h b/ProcessList.h index d09cc072..0f0f7d51 100644 --- a/ProcessList.h +++ b/ProcessList.h @@ -49,8 +49,8 @@ typedef struct ProcessList_ { /* Implemented by platforms */ ProcessList* ProcessList_new(Machine* host, Hashtable* pidMatchList); -void ProcessList_delete(ProcessList* pl); -void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate); +void ProcessList_delete(ProcessList* this); +void ProcessList_goThroughEntries(ProcessList* this); void ProcessList_init(ProcessList* this, const ObjectClass* klass, Machine* host, Hashtable* pidMatchList); @@ -74,7 +74,7 @@ void ProcessList_rebuildPanel(ProcessList* this); Process* ProcessList_getProcess(ProcessList* this, pid_t pid, bool* preExisting, Process_New constructor); -void ProcessList_scan(ProcessList* this, bool pauseProcessUpdate); +void ProcessList_scan(ProcessList* this); static inline Process* ProcessList_findProcess(ProcessList* this, pid_t pid) { return (Process*) Hashtable_get(this->processTable, pid); diff --git a/ScreenManager.c b/ScreenManager.c index f1897893..18e09343 100644 --- a/ScreenManager.c +++ b/ScreenManager.c @@ -16,6 +16,7 @@ in the source distribution for its full text. #include "CRT.h" #include "FunctionBar.h" +#include "Machine.h" #include "Macros.h" #include "Object.h" #include "Platform.h" @@ -135,8 +136,10 @@ static void checkRecalculation(ScreenManager* this, double* oldTime, int* sortTi host->pl->needsSort = true; *sortTimeout = 1; } - // scan processes first - some header values are calculated there - ProcessList_scan(host->pl, this->state->pauseUpdate); + // sample current values for system metrics and processes if not paused + Machine_scan(host); + if (!this->state->pauseUpdate) + ProcessList_scan(host->pl); // always update header, especially to avoid gaps in graph meters Header_updateData(this->header); // force redraw if the number of UID digits was changed diff --git a/darwin/DarwinMachine.c b/darwin/DarwinMachine.c new file mode 100644 index 00000000..6bf52b76 --- /dev/null +++ b/darwin/DarwinMachine.c @@ -0,0 +1,113 @@ +/* +htop - DarwinMachine.c +(C) 2014 Hisham H. Muhammad +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "darwin/DarwinMachine.h" + +#include <errno.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/sysctl.h> + +#include "CRT.h" +#include "Machine.h" +#include "darwin/Platform.h" +#include "darwin/PlatformHelpers.h" +#include "generic/openzfs_sysctl.h" +#include "zfs/ZfsArcStats.h" + + +static void DarwinMachine_getHostInfo(host_basic_info_data_t* p) { + mach_msg_type_number_t info_size = HOST_BASIC_INFO_COUNT; + + if (0 != host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)p, &info_size)) { + CRT_fatalError("Unable to retrieve host info"); + } +} + +static void DarwinMachine_freeCPULoadInfo(processor_cpu_load_info_t* p) { + if (NULL != p && NULL != *p) { + if (0 != munmap(*p, vm_page_size)) { + CRT_fatalError("Unable to free old CPU load information"); + } + *p = NULL; + } +} + +static unsigned DarwinMachine_allocateCPULoadInfo(processor_cpu_load_info_t* p) { + mach_msg_type_number_t info_size = sizeof(processor_cpu_load_info_t); + unsigned cpu_count; + + // TODO Improving the accuracy of the load counts would help a lot. + if (0 != host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, (processor_info_array_t*)p, &info_size)) { + CRT_fatalError("Unable to retrieve CPU info"); + } + + return cpu_count; +} + +static void DarwinMachine_getVMStats(vm_statistics_t p) { + mach_msg_type_number_t info_size = HOST_VM_INFO_COUNT; + + if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)p, &info_size) != 0) { + CRT_fatalError("Unable to retrieve VM statistics"); + } +} + +void Machine_scan(Machine* super) { + DarwinMachine* host = (DarwinMachine*) super; + + /* Update the global data (CPU times and VM stats) */ + DarwinMachine_freeCPULoadInfo(&host->prev_load); + host->prev_load = host->curr_load; + DarwinMachine_allocateCPULoadInfo(&host->curr_load); + DarwinMachine_getVMStats(&host->vm_stats); + openzfs_sysctl_updateArcStats(&host->zfs); +} + +Machine* Machine_new(UsersTable* usersTable, uid_t userId) { + DarwinMachine* this = xCalloc(1, sizeof(DarwinMachine)); + Machine* super = &this->super; + + Machine_init(super, usersTable, userId); + + /* Initialize the CPU information */ + super->activeCPUs = DarwinMachine_allocateCPULoadInfo(&this->prev_load); + super->existingCPUs = super->activeCPUs; + DarwinMachine_getHostInfo(&this->host_info); + DarwinMachine_allocateCPULoadInfo(&this->curr_load); + + /* Initialize the VM statistics */ + DarwinMachine_getVMStats(&this->vm_stats); + + /* Initialize the ZFS kstats, if zfs.kext loaded */ + openzfs_sysctl_init(&this->zfs); + openzfs_sysctl_updateArcStats(&this->zfs); + + return super; +} + +void Machine_delete(Machine* super) { + DarwinMachine* host = (DarwinMachine*) super; + + DarwinMachine_freeCPULoadInfo(&host->prev_load); + + Machine_done(super); + free(super); +} + +bool Machine_isCPUonline(const Machine* host, unsigned int id) { + assert(id < host->existingCPUs); + + // TODO: support offline CPUs and hot swapping + (void) host; (void) id; + + return true; +} diff --git a/darwin/DarwinMachine.h b/darwin/DarwinMachine.h new file mode 100644 index 00000000..3135b589 --- /dev/null +++ b/darwin/DarwinMachine.h @@ -0,0 +1,28 @@ +#ifndef HEADER_DarwinMachine +#define HEADER_DarwinMachine +/* +htop - DarwinMachine.h +(C) 2014 Hisham H. Muhammad +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include <mach/mach_host.h> +#include <sys/sysctl.h> + +#include "Machine.h" +#include "zfs/ZfsArcStats.h" + + +typedef struct DarwinMachine_ { + Machine super; + + host_basic_info_data_t host_info; + vm_statistics_data_t vm_stats; + processor_cpu_load_info_t prev_load; + processor_cpu_load_info_t curr_load; + + ZfsArcStats zfs; +} DarwinMachine; + +#endif diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index 22004e36..e6366d70 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -16,6 +16,7 @@ in the source distribution for its full text. #include "CRT.h" #include "Process.h" +#include "darwin/DarwinMachine.h" #include "darwin/Platform.h" @@ -365,6 +366,8 @@ void DarwinProcess_setFromLibprocPidinfo(DarwinProcess* proc, DarwinProcessList* struct proc_taskinfo pti; if (sizeof(pti) == proc_pidinfo(proc->super.pid, PROC_PIDTASKINFO, 0, &pti, sizeof(pti))) { + const DarwinMachine* dhost = (const DarwinMachine*) proc->super.host; + uint64_t total_existing_time_ns = proc->stime + proc->utime; uint64_t user_time_ns = Platform_machTicksToNanoseconds(pti.pti_total_user); @@ -386,7 +389,7 @@ void DarwinProcess_setFromLibprocPidinfo(DarwinProcess* proc, DarwinProcessList* proc->super.m_resident = pti.pti_resident_size / ONE_K; proc->super.majflt = pti.pti_faults; proc->super.percent_mem = (double)pti.pti_resident_size * 100.0 - / (double)dpl->host_info.max_mem; + / (double)dhost->host_info.max_mem; proc->stime = system_time_ns; proc->utime = user_time_ns; diff --git a/darwin/DarwinProcessList.c b/darwin/DarwinProcessList.c index 16c44d2c..bf311dc7 100644 --- a/darwin/DarwinProcessList.c +++ b/darwin/DarwinProcessList.c @@ -20,6 +20,7 @@ in the source distribution for its full text. #include "CRT.h" #include "ProcessList.h" +#include "darwin/DarwinMachine.h" #include "darwin/DarwinProcess.h" #include "darwin/Platform.h" #include "darwin/PlatformHelpers.h" @@ -27,43 +28,6 @@ in the source distribution for its full text. #include "zfs/ZfsArcStats.h" -static void ProcessList_getHostInfo(host_basic_info_data_t* p) { - mach_msg_type_number_t info_size = HOST_BASIC_INFO_COUNT; - - if (0 != host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)p, &info_size)) { - CRT_fatalError("Unable to retrieve host info"); - } -} - -static void ProcessList_freeCPULoadInfo(processor_cpu_load_info_t* p) { - if (NULL != p && NULL != *p) { - if (0 != munmap(*p, vm_page_size)) { - CRT_fatalError("Unable to free old CPU load information"); - } - *p = NULL; - } -} - -static unsigned ProcessList_allocateCPULoadInfo(processor_cpu_load_info_t* p) { - mach_msg_type_number_t info_size = sizeof(processor_cpu_load_info_t); - unsigned cpu_count; - - // TODO Improving the accuracy of the load counts woule help a lot. - if (0 != host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, (processor_info_array_t*)p, &info_size)) { - CRT_fatalError("Unable to retrieve CPU info"); - } - - return cpu_count; -} - -static void ProcessList_getVMStats(vm_statistics_t p) { - mach_msg_type_number_t info_size = HOST_VM_INFO_COUNT; - - if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)p, &info_size) != 0) { - CRT_fatalError("Unable to retrieve VM statistics"); - } -} - static struct kinfo_proc* ProcessList_getKInfoProcs(size_t* count) { int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; struct kinfo_proc* processes = NULL; @@ -91,29 +55,11 @@ static struct kinfo_proc* ProcessList_getKInfoProcs(size_t* count) { ProcessList* ProcessList_new(Machine* host, Hashtable* pidMatchList) { DarwinProcessList* this = xCalloc(1, sizeof(DarwinProcessList)); + ProcessList* super = &this->super; - ProcessList_init(&this->super, Class(DarwinProcess), host, pidMatchList); - - /* Initialize the CPU information */ - host->activeCPUs = ProcessList_allocateCPULoadInfo(&this->prev_load); - // TODO: support offline CPUs and hot swapping - host->existingCPUs = host->activeCPUs; - ProcessList_getHostInfo(&this->host_info); - ProcessList_allocateCPULoadInfo(&this->curr_load); - - /* Initialize the VM statistics */ - ProcessList_getVMStats(&this->vm_stats); - - /* Initialize the ZFS kstats, if zfs.kext loaded */ - openzfs_sysctl_init(&this->zfs); - openzfs_sysctl_updateArcStats(&this->zfs); + ProcessList_init(super, Class(DarwinProcess), host, pidMatchList); - this->super.kernelThreads = 0; - this->super.userlandThreads = 0; - this->super.totalTasks = 0; - this->super.runningTasks = 0; - - return &this->super; + return super; } void ProcessList_delete(ProcessList* this) { @@ -121,31 +67,20 @@ void ProcessList_delete(ProcessList* this) { free(this); } -void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { - DarwinProcessList* dpl = (DarwinProcessList*)super; +void ProcessList_goThroughEntries(ProcessList* super) { const Machine* host = super->host; + const DarwinMachine* dhost = (const DarwinMachine*) host; + DarwinProcessList* dpl = (DarwinProcessList*) super; bool preExisting = true; struct kinfo_proc* ps; size_t count; DarwinProcess* proc; - /* Update the global data (CPU times and VM stats) */ - ProcessList_freeCPULoadInfo(&dpl->prev_load); - dpl->prev_load = dpl->curr_load; - ProcessList_allocateCPULoadInfo(&dpl->curr_load); - ProcessList_getVMStats(&dpl->vm_stats); - openzfs_sysctl_updateArcStats(&dpl->zfs); - - // in pause mode only gather global data for meters (CPU/memory/...) - if (pauseProcessUpdate) { - return; - } - /* Get the time difference */ dpl->global_diff = 0; 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]; + dpl->global_diff += dhost->curr_load[i].cpu_ticks[j] - dhost->prev_load[i].cpu_ticks[j]; } } @@ -178,7 +113,7 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { } // Disabled for High Sierra due to bug in macOS High Sierra - bool isScanThreadSupported = !Platform_KernelVersionIsBetween((KernelVersion) {17, 0, 0}, (KernelVersion) {17, 5, 0}); + bool isScanThreadSupported = !Platform_KernelVersionIsBetween((KernelVersion) {17, 0, 0}, (KernelVersion) {17, 5, 0}); if (isScanThreadSupported) { DarwinProcess_scanThreads(proc); @@ -193,22 +128,3 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { free(ps); } - -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) host; (void) id; - - return true; -} diff --git a/darwin/DarwinProcessList.h b/darwin/DarwinProcessList.h index 128896ae..56d6c1b5 100644 --- a/darwin/DarwinProcessList.h +++ b/darwin/DarwinProcessList.h @@ -11,33 +11,12 @@ in the source distribution for its full text. #include <sys/sysctl.h> #include "ProcessList.h" -#include "zfs/ZfsArcStats.h" typedef struct DarwinProcessList_ { ProcessList super; - host_basic_info_data_t host_info; - vm_statistics_data_t vm_stats; - processor_cpu_load_info_t prev_load; - processor_cpu_load_info_t curr_load; - uint64_t kernel_threads; - uint64_t user_threads; uint64_t global_diff; - - ZfsArcStats zfs; } DarwinProcessList; -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/darwin/Platform.c b/darwin/Platform.c index 71d08247..bb1ae92f 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -44,7 +44,7 @@ in the source distribution for its full text. #include "SysArchMeter.h" #include "TasksMeter.h" #include "UptimeMeter.h" -#include "darwin/DarwinProcessList.h" +#include "darwin/DarwinMachine.h" #include "darwin/PlatformHelpers.h" #include "generic/fdstat_sysctl.h" #include "zfs/ZfsArcMeter.h" @@ -257,9 +257,9 @@ double Platform_setCPUValues(Meter* mtr, unsigned int cpu) { return Platform_setCPUAverageValues(mtr); } - const DarwinPro |