Fixed CPU updating

This commit is contained in:
David Hunt 2015-07-13 17:09:18 -05:00 committed by Hisham Muhammad
parent 7f3faa276a
commit 6463ea2956
3 changed files with 30 additions and 16 deletions

View File

@ -25,7 +25,8 @@ typedef struct DarwinProcessList_ {
ProcessList super; ProcessList super;
host_basic_info_data_t host_info; 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; } DarwinProcessList;
}*/ }*/
@ -39,17 +40,21 @@ 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) {
mach_msg_type_number_t info_size = sizeof(processor_cpu_load_info_t); if(NULL != p && NULL != *p) {
unsigned cpu_count;
if(NULL != p) {
if(0 != munmap(*p, vm_page_size)) { if(0 != munmap(*p, vm_page_size)) {
fprintf(stderr, "Unable to free old CPU load information\n"); fprintf(stderr, "Unable to free old CPU load information\n");
exit(8); 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)) { 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"); fprintf(stderr, "Unable to retrieve CPU info\n");
exit(4); exit(4);
@ -95,9 +100,9 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
ProcessList_init(&this->super, Class(Process), usersTable, pidWhiteList, userId); ProcessList_init(&this->super, Class(Process), usersTable, pidWhiteList, userId);
/* Initialize the previous information */ /* Initialize the previous information */
this->cpu_load = NULL; this->super.cpuCount = ProcessList_allocateCPULoadInfo(&this->prev_load);
this->super.cpuCount = ProcessList_updateCPULoadInfo(&this->cpu_load);
ProcessList_getHostInfo(&this->host_info); ProcessList_getHostInfo(&this->host_info);
ProcessList_allocateCPULoadInfo(&this->curr_load);
return &this->super; return &this->super;
} }
@ -118,7 +123,9 @@ void ProcessList_goThroughEntries(ProcessList* super) {
gettimeofday(&tv, NULL); /* Start processing time */ gettimeofday(&tv, NULL); /* Start processing time */
/* Update the global data (CPU times) */ /* 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 : /* We use kinfo_procs for initial data since :
* *

View File

@ -16,13 +16,16 @@ typedef struct DarwinProcessList_ {
ProcessList super; ProcessList super;
host_basic_info_data_t host_info; 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; } DarwinProcessList;
void ProcessList_getHostInfo(host_basic_info_data_t *p); 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); struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count);

View File

@ -139,17 +139,21 @@ double Platform_setCPUValues(Meter* mtr, int cpu) {
static const int CPU_METER_KERNEL = 2; static const int CPU_METER_KERNEL = 2;
DarwinProcessList *dpl = (DarwinProcessList *)mtr->pl; 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; double total = 0;
/* Take the sums */ /* Take the sums */
for(size_t i = 0; i < CPU_STATE_MAX; ++i) { 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_NICE]
mtr->values[CPU_METER_NORMAL] = (double)ticks->cpu_ticks[CPU_STATE_USER] * 100.0 / total; = ((double)curr->cpu_ticks[CPU_STATE_NICE] - (double)prev->cpu_ticks[CPU_STATE_NICE])* 100.0 / total;
mtr->values[CPU_METER_KERNEL] = (double)ticks->cpu_ticks[CPU_STATE_SYSTEM] * 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); Meter_setItems(mtr, 3);