diff --git a/darwin/Platform.c b/darwin/Platform.c index 9b7e23791..66ce7a26d 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -18,6 +18,7 @@ in the source distribution for its full text. #include #include #include +#include #include #include #include // After `sys/socket.h` for struct `sockaddr` (for iOS6 SDK) @@ -311,15 +312,22 @@ static double Platform_setCPUAverageValues(Meter* mtr) { double sumNormal = 0.0; double sumKernel = 0.0; double sumPercent = 0.0; + double sumFrequency = 0.0; + unsigned int countFrequency = 0; for (unsigned int i = 1; i <= host->existingCPUs; i++) { sumPercent += Platform_setCPUValues(mtr, i); sumNice += mtr->values[CPU_METER_NICE]; sumNormal += mtr->values[CPU_METER_NORMAL]; sumKernel += mtr->values[CPU_METER_KERNEL]; + if (isNonnegative(mtr->values[CPU_METER_FREQUENCY])) { + sumFrequency += mtr->values[CPU_METER_FREQUENCY]; + countFrequency++; + } } mtr->values[CPU_METER_NICE] = sumNice / activeCPUs; mtr->values[CPU_METER_NORMAL] = sumNormal / activeCPUs; mtr->values[CPU_METER_KERNEL] = sumKernel / activeCPUs; + mtr->values[CPU_METER_FREQUENCY] = countFrequency ? (sumFrequency / countFrequency) : NAN; return sumPercent / activeCPUs; } @@ -357,7 +365,18 @@ double Platform_setCPUValues(Meter* mtr, unsigned int cpu) { /* Convert to percent and return */ total = mtr->values[CPU_METER_NICE] + mtr->values[CPU_METER_NORMAL] + mtr->values[CPU_METER_KERNEL]; - mtr->values[CPU_METER_FREQUENCY] = NAN; + double frequency = NAN; + if (mtr->host->settings->showCPUFrequency) { + uint64_t cpufreq = 0; + size_t len = sizeof(cpufreq); + if (sysctlbyname("hw.cpufrequency", &cpufreq, &len, NULL, 0) == 0 && cpufreq > 0) { + frequency = (double)cpufreq / 1E6; + } else if (sysctlbyname("hw.cpufrequency_max", &cpufreq, &len, NULL, 0) == 0 && cpufreq > 0) { + frequency = (double)cpufreq / 1E6; + } + } + + mtr->values[CPU_METER_FREQUENCY] = frequency; mtr->values[CPU_METER_TEMPERATURE] = NAN; return CLAMP(total, 0.0, 100.0);