From 57ab332d5a1f0f46b87049720a614e4ff46ee938 Mon Sep 17 00:00:00 2001 From: David Hunt Date: Mon, 13 Jul 2015 21:02:40 -0500 Subject: [PATCH] Fix the thread counts --- darwin/DarwinProcess.c | 13 +++++++++++-- darwin/DarwinProcess.h | 4 +++- darwin/DarwinProcessList.c | 39 ++++++++++++++++++++++++++++++++++---- darwin/DarwinProcessList.h | 6 ++++++ darwin/Platform.c | 24 ++++++++++++++++++++--- darwin/Platform.h | 2 +- 6 files changed, 77 insertions(+), 11 deletions(-) diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index 64388490..6fe947ea 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -14,6 +14,8 @@ in the source distribution for its full text. /*{ #include "Settings.h" +#include "DarwinProcessList.h" + #include }*/ @@ -285,15 +287,22 @@ void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, time_t proc->updated = true; } -void DarwinProcess_setFromLibprocPidinfo(Process *proc, uint64_t total_memory, bool preExisting) { +void DarwinProcess_setFromLibprocPidinfo(Process *proc, DarwinProcessList *dpl, bool preExisting) { struct proc_taskinfo pti; if(sizeof(pti) == proc_pidinfo(proc->pid, PROC_PIDTASKINFO, 0, &pti, sizeof(pti))) { + proc->time = (pti.pti_total_system + pti.pti_total_user) / 10000000; proc->nlwp = pti.pti_threadnum; proc->m_size = pti.pti_virtual_size / 1024; proc->m_resident = pti.pti_resident_size / 1024; proc->majflt = pti.pti_faults; - proc->percent_mem = (double)pti.pti_resident_size * 100.0 / (double)total_memory; + proc->percent_mem = (double)pti.pti_resident_size * 100.0 + / (double)dpl->host_info.max_mem; + + dpl->super.kernelThreads += 0; /*pti.pti_threads_system;*/ + dpl->super.userlandThreads += pti.pti_threadnum; /*pti.pti_threads_user;*/ + dpl->super.totalTasks += pti.pti_threadnum; + dpl->super.runningTasks += pti.pti_numrunning; } } diff --git a/darwin/DarwinProcess.h b/darwin/DarwinProcess.h index 65fff6b7..3c4f2e0b 100644 --- a/darwin/DarwinProcess.h +++ b/darwin/DarwinProcess.h @@ -10,6 +10,8 @@ in the source distribution for its full text. */ #include "Settings.h" +#include "DarwinProcessList.h" + #include @@ -25,7 +27,7 @@ char *DarwinProcessList_getCmdLine(struct kinfo_proc* k, int show_args ); void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, time_t now, bool exists); -void DarwinProcess_setFromLibprocPidinfo(Process *proc, uint64_t total_memory, bool preExisting); +void DarwinProcess_setFromLibprocPidinfo(Process *proc, DarwinProcessList *dpl, bool preExisting); void DarwinProcess_parseThreads(Process *proc, time_t now, bool preExisting); diff --git a/darwin/DarwinProcessList.c b/darwin/DarwinProcessList.c index a58fae17..ea91ecb7 100644 --- a/darwin/DarwinProcessList.c +++ b/darwin/DarwinProcessList.c @@ -16,8 +16,10 @@ in the source distribution for its full text. #include #include #include +#include /*{ +#include "ProcessList.h" #include #include @@ -25,8 +27,11 @@ typedef struct DarwinProcessList_ { ProcessList super; host_basic_info_data_t host_info; + vm_statistics64_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; } DarwinProcessList; }*/ @@ -63,6 +68,15 @@ unsigned ProcessList_allocateCPULoadInfo(processor_cpu_load_info_t *p) { return cpu_count; } +void ProcessList_getVMStats(vm_statistics64_t p) { + mach_msg_type_number_t info_size = HOST_VM_INFO64_COUNT; + + if(0 != host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info_t)p, &info_size)) { + fprintf(stderr, "Unable to retrieve VM statistics\n"); + exit(9); + } +} + struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) { int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; struct kinfo_proc *processes = NULL; @@ -98,12 +112,20 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui DarwinProcessList* this = calloc(1, sizeof(DarwinProcessList)); ProcessList_init(&this->super, Class(Process), usersTable, pidWhiteList, userId); - - /* Initialize the previous information */ + + /* Initialize the CPU information */ this->super.cpuCount = ProcessList_allocateCPULoadInfo(&this->prev_load); ProcessList_getHostInfo(&this->host_info); ProcessList_allocateCPULoadInfo(&this->curr_load); + /* Initialize the VM statistics */ + ProcessList_getVMStats(&this->vm_stats); + + this->super.kernelThreads = 0; + this->super.userlandThreads = 0; + this->super.totalTasks = 0; + this->super.runningTasks = 0; + return &this->super; } @@ -122,10 +144,17 @@ void ProcessList_goThroughEntries(ProcessList* super) { gettimeofday(&tv, NULL); /* Start processing time */ - /* Update the global data (CPU times) */ + /* 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); + + /* Clear the thread counts */ + super->kernelThreads = 0; + super->userlandThreads = 0; + super->totalTasks = 0; + super->runningTasks = 0; /* We use kinfo_procs for initial data since : * @@ -140,7 +169,9 @@ void ProcessList_goThroughEntries(ProcessList* super) { proc = ProcessList_getProcess(super, ps[i].kp_proc.p_pid, &preExisting, DarwinProcess_new); DarwinProcess_setFromKInfoProc(proc, ps + i, tv.tv_sec, preExisting); - DarwinProcess_setFromLibprocPidinfo(proc, dpl->host_info.max_mem, preExisting); + DarwinProcess_setFromLibprocPidinfo(proc, dpl, preExisting); + + super->totalTasks += 1; if(!preExisting) { proc->user = UsersTable_getRef(super->usersTable, proc->st_uid); diff --git a/darwin/DarwinProcessList.h b/darwin/DarwinProcessList.h index a893dcfe..e02ea2dd 100644 --- a/darwin/DarwinProcessList.h +++ b/darwin/DarwinProcessList.h @@ -9,6 +9,7 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ +#include "ProcessList.h" #include #include @@ -16,8 +17,11 @@ typedef struct DarwinProcessList_ { ProcessList super; host_basic_info_data_t host_info; + vm_statistics64_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; } DarwinProcessList; @@ -27,6 +31,8 @@ void ProcessList_freeCPULoadInfo(processor_cpu_load_info_t *p); unsigned ProcessList_allocateCPULoadInfo(processor_cpu_load_info_t *p); +void ProcessList_getVMStats(vm_statistics64_t p); + struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count); ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId); diff --git a/darwin/Platform.c b/darwin/Platform.c index b3af72fb..94d75da6 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -62,7 +62,6 @@ MeterClass* Platform_meterTypes[] = { &LoadAverageMeter_class, &LoadMeter_class, &MemoryMeter_class, - &SwapMeter_class, &TasksMeter_class, &BatteryMeter_class, &HostnameMeter_class, @@ -86,7 +85,17 @@ char* Process_pidFormat = "%7u "; char* Process_tpgidFormat = "%7u "; int Platform_getUptime() { - return 0; + struct timeval bootTime, currTime; + int mib[2] = { CTL_KERN, KERN_BOOTTIME }; + size_t size = sizeof(bootTime); + + int err = sysctl(mib, 2, &bootTime, &size, NULL, 0); + if (err) { + return -1; + } + gettimeofday(&currTime, NULL); + + return (int) difftime(currTime.tv_sec, bootTime.tv_sec); } void Platform_getLoadAverage(double* one, double* five, double* fifteen) { @@ -163,7 +172,16 @@ double Platform_setCPUValues(Meter* mtr, int cpu) { return MIN(100.0, MAX(0.0, total)); } -void Platform_setMemoryValues(Meter* this) { +void Platform_setMemoryValues(Meter* mtr) { + DarwinProcessList *dpl = (DarwinProcessList *)mtr->pl; + vm_statistics64_t vm = &dpl->vm_stats; + double page_K = (double)vm_page_size / (double)1024; + + mtr->total = dpl->host_info.max_mem / 1024; + mtr->values[0] = (double)(vm->active_count + vm->wire_count) * page_K; + mtr->values[1] = (double)vm->purgeable_count * page_K; + mtr->values[2] = (double)vm->inactive_count * page_K; + } void Platform_setSwapValues(Meter* this) { diff --git a/darwin/Platform.h b/darwin/Platform.h index 31e68685..eaf0cd7f 100644 --- a/darwin/Platform.h +++ b/darwin/Platform.h @@ -36,7 +36,7 @@ void Process_setupColumnWidths(); double Platform_setCPUValues(Meter* mtr, int cpu); -void Platform_setMemoryValues(Meter* this); +void Platform_setMemoryValues(Meter* mtr); void Platform_setSwapValues(Meter* this);