From 6463ea29564c872bd93278878a2e5245e7d02aa0 Mon Sep 17 00:00:00 2001 From: David Hunt Date: Mon, 13 Jul 2015 17:09:18 -0500 Subject: [PATCH] Fixed CPU updating --- darwin/DarwinProcessList.c | 25 ++++++++++++++++--------- darwin/DarwinProcessList.h | 7 +++++-- darwin/Platform.c | 14 +++++++++----- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/darwin/DarwinProcessList.c b/darwin/DarwinProcessList.c index 1e9bd148..a58fae17 100644 --- a/darwin/DarwinProcessList.c +++ b/darwin/DarwinProcessList.c @@ -25,7 +25,8 @@ typedef struct DarwinProcessList_ { ProcessList super; host_basic_info_data_t host_info; - processor_cpu_load_info_t cpu_load; + processor_cpu_load_info_t prev_load; + processor_cpu_load_info_t curr_load; } DarwinProcessList; }*/ @@ -39,17 +40,21 @@ void ProcessList_getHostInfo(host_basic_info_data_t *p) { } } -unsigned ProcessList_updateCPULoadInfo(processor_cpu_load_info_t *p) { - mach_msg_type_number_t info_size = sizeof(processor_cpu_load_info_t); - unsigned cpu_count; - - if(NULL != p) { +void ProcessList_freeCPULoadInfo(processor_cpu_load_info_t *p) { + if(NULL != p && NULL != *p) { if(0 != munmap(*p, vm_page_size)) { fprintf(stderr, "Unable to free old CPU load information\n"); exit(8); } } + *p = NULL; +} + +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; + if(0 != host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, (processor_info_array_t *)p, &info_size)) { fprintf(stderr, "Unable to retrieve CPU info\n"); exit(4); @@ -95,9 +100,9 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui ProcessList_init(&this->super, Class(Process), usersTable, pidWhiteList, userId); /* Initialize the previous information */ - this->cpu_load = NULL; - this->super.cpuCount = ProcessList_updateCPULoadInfo(&this->cpu_load); + this->super.cpuCount = ProcessList_allocateCPULoadInfo(&this->prev_load); ProcessList_getHostInfo(&this->host_info); + ProcessList_allocateCPULoadInfo(&this->curr_load); return &this->super; } @@ -118,7 +123,9 @@ void ProcessList_goThroughEntries(ProcessList* super) { gettimeofday(&tv, NULL); /* Start processing time */ /* Update the global data (CPU times) */ - ProcessList_updateCPULoadInfo(&dpl->cpu_load); + ProcessList_freeCPULoadInfo(&dpl->prev_load); + dpl->prev_load = dpl->curr_load; + ProcessList_allocateCPULoadInfo(&dpl->curr_load); /* We use kinfo_procs for initial data since : * diff --git a/darwin/DarwinProcessList.h b/darwin/DarwinProcessList.h index 065e8fbf..a893dcfe 100644 --- a/darwin/DarwinProcessList.h +++ b/darwin/DarwinProcessList.h @@ -16,13 +16,16 @@ typedef struct DarwinProcessList_ { ProcessList super; host_basic_info_data_t host_info; - processor_cpu_load_info_t cpu_load; + processor_cpu_load_info_t prev_load; + processor_cpu_load_info_t curr_load; } DarwinProcessList; void ProcessList_getHostInfo(host_basic_info_data_t *p); -unsigned ProcessList_updateCPULoadInfo(processor_cpu_load_info_t *p); +void ProcessList_freeCPULoadInfo(processor_cpu_load_info_t *p); + +unsigned ProcessList_allocateCPULoadInfo(processor_cpu_load_info_t *p); struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count); diff --git a/darwin/Platform.c b/darwin/Platform.c index 1d43eaa8..b3af72fb 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -139,17 +139,21 @@ double Platform_setCPUValues(Meter* mtr, int cpu) { static const int CPU_METER_KERNEL = 2; DarwinProcessList *dpl = (DarwinProcessList *)mtr->pl; - processor_cpu_load_info_t ticks = &dpl->cpu_load[cpu-1]; + processor_cpu_load_info_t prev = &dpl->prev_load[cpu-1]; + processor_cpu_load_info_t curr = &dpl->curr_load[cpu-1]; double total = 0; /* Take the sums */ for(size_t i = 0; i < CPU_STATE_MAX; ++i) { - total += (double)ticks->cpu_ticks[i]; + total += (double)curr->cpu_ticks[i] - (double)prev->cpu_ticks[i]; } - mtr->values[CPU_METER_NICE] = (double)ticks->cpu_ticks[CPU_STATE_NICE] * 100.0 / total; - mtr->values[CPU_METER_NORMAL] = (double)ticks->cpu_ticks[CPU_STATE_USER] * 100.0 / total; - mtr->values[CPU_METER_KERNEL] = (double)ticks->cpu_ticks[CPU_STATE_SYSTEM] * 100.0 / total; + mtr->values[CPU_METER_NICE] + = ((double)curr->cpu_ticks[CPU_STATE_NICE] - (double)prev->cpu_ticks[CPU_STATE_NICE])* 100.0 / total; + mtr->values[CPU_METER_NORMAL] + = ((double)curr->cpu_ticks[CPU_STATE_USER] - (double)prev->cpu_ticks[CPU_STATE_USER])* 100.0 / total; + mtr->values[CPU_METER_KERNEL] + = ((double)curr->cpu_ticks[CPU_STATE_SYSTEM] - (double)prev->cpu_ticks[CPU_STATE_SYSTEM])* 100.0 / total; Meter_setItems(mtr, 3);