From 71190654bc926fb137e829fb8e2f9efa1e4344ab Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 23 Oct 2015 13:46:21 -0200 Subject: Calculate CPU averages on Darwin (See #295). --- darwin/DarwinProcess.c | 10 +++++----- darwin/Platform.c | 25 +++++++++++++++++++++---- 2 files changed, 26 insertions(+), 9 deletions(-) (limited to 'darwin') diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index 0b4ec3fe..83e9d828 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -220,11 +220,11 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) { /* Convert previous '\0'. */ *np = ' '; } - /* Note location of current '\0'. */ - np = cp; - } - } - } + /* Note location of current '\0'. */ + np = cp; + } + } + } #endif /* diff --git a/darwin/Platform.c b/darwin/Platform.c index f56d27f3..f8108e78 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -21,6 +21,7 @@ in the source distribution for its full text. /*{ #include "Action.h" +#include "CPUMeter.h" #include "BatteryMeter.h" #include "DarwinProcess.h" }*/ @@ -163,11 +164,27 @@ ProcessPidColumn Process_pidColumns[] = { { .id = 0, .label = NULL }, }; +static double Platform_setCPUAverageValues(Meter* mtr) { + DarwinProcessList *dpl = (DarwinProcessList *)mtr->pl; + int cpus = dpl->super.cpuCount; + double sumNice, sumNormal, sumKernel, sumPercent; + for (int i = 1; i <= cpus; i++) { + sumPercent += Platform_setCPUValues(mtr, i); + sumNice += mtr->values[CPU_METER_NICE]; + sumNormal += mtr->values[CPU_METER_NORMAL]; + sumKernel += mtr->values[CPU_METER_KERNEL]; + } + mtr->values[CPU_METER_NICE] = sumNice / cpus; + mtr->values[CPU_METER_NORMAL] = sumNormal / cpus; + mtr->values[CPU_METER_KERNEL] = sumKernel / cpus; + return sumPercent / cpus; +} + double Platform_setCPUValues(Meter* mtr, int cpu) { - /* All just from CPUMeter.c */ - static const int CPU_METER_NICE = 0; - static const int CPU_METER_NORMAL = 1; - static const int CPU_METER_KERNEL = 2; + + if (cpu == 0) { + return Platform_setCPUAverageValues(mtr); + } DarwinProcessList *dpl = (DarwinProcessList *)mtr->pl; processor_cpu_load_info_t prev = &dpl->prev_load[cpu-1]; -- cgit v1.2.3