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 /darwin | |
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.
Diffstat (limited to 'darwin')
-rw-r--r-- | darwin/DarwinMachine.c | 113 | ||||
-rw-r--r-- | darwin/DarwinMachine.h | 28 | ||||
-rw-r--r-- | darwin/DarwinProcess.c | 5 | ||||
-rw-r--r-- | darwin/DarwinProcessList.c | 102 | ||||
-rw-r--r-- | darwin/DarwinProcessList.h | 21 | ||||
-rw-r--r-- | darwin/Platform.c | 23 |
6 files changed, 165 insertions, 127 deletions
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 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]; + const DarwinMachine* dhost = (const DarwinMachine*) mtr->host; + const processor_cpu_load_info_t prev = &dhost->prev_load[cpu - 1]; + const processor_cpu_load_info_t curr = &dhost->curr_load[cpu - 1]; double total = 0; /* Take the sums */ @@ -286,12 +286,11 @@ double Platform_setCPUValues(Meter* mtr, unsigned int cpu) { } void Platform_setMemoryValues(Meter* mtr) { - const Machine* host = mtr->host; - const DarwinProcessList* dpl = (const DarwinProcessList*) host->pl; - const struct vm_statistics* vm = &dpl->vm_stats; + const DarwinMachine* dhost = (const DarwinMachine*) mtr->host; + const struct vm_statistics* vm = &dhost->vm_stats; double page_K = (double)vm_page_size / (double)1024; - mtr->total = dpl->host_info.max_mem / 1024; + mtr->total = dhost->host_info.max_mem / 1024; mtr->values[MEMORY_METER_USED] = (double)(vm->active_count + vm->wire_count) * page_K; mtr->values[MEMORY_METER_BUFFERS] = (double)vm->purgeable_count * page_K; // mtr->values[MEMORY_METER_SHARED] = "shared memory, like tmpfs and shm" @@ -313,15 +312,15 @@ void Platform_setSwapValues(Meter* mtr) { } void Platform_setZfsArcValues(Meter* this) { - const DarwinProcessList* dpl = (const DarwinProcessList*) this->host->pl; + const DarwinMachine* dhost = (const DarwinMachine*) this->host; - ZfsArcMeter_readStats(this, &(dpl->zfs)); + ZfsArcMeter_readStats(this, &dhost->zfs); } void Platform_setZfsCompressedArcValues(Meter* this) { - const DarwinProcessList* dpl = (const DarwinProcessList*) this->host->pl; + const DarwinMachine* dhost = (const DarwinMachine*) this->host; - ZfsCompressedArcMeter_readStats(this, &(dpl->zfs)); + ZfsCompressedArcMeter_readStats(this, &dhost->zfs); } char* Platform_getProcessEnv(pid_t pid) { |