Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion darwin/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ in the source distribution for its full text.
#include <net/if_types.h>
#include <net/route.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <mach/port.h>
#include <net/if.h> // After `sys/socket.h` for struct `sockaddr` (for iOS6 SDK)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the per-core frequency? Because I don't see, where it follow's up on the cpu argument provided to this function.

frequency = (double)cpufreq / 1E6;
} else if (sysctlbyname("hw.cpufrequency_max", &cpufreq, &len, NULL, 0) == 0 && cpufreq > 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is len guaranteed to be unchanged when the first of these two sysctls fails?

frequency = (double)cpufreq / 1E6;
}
}

mtr->values[CPU_METER_FREQUENCY] = frequency;
mtr->values[CPU_METER_TEMPERATURE] = NAN;

return CLAMP(total, 0.0, 100.0);
Expand Down
Loading