diff options
author | kwesolowski <krzysztof.wesolowski@rainlabs.pl> | 2014-07-24 22:05:31 (GMT) |
---|---|---|
committer | kwesolowski <krzysztof.wesolowski@rainlabs.pl> | 2014-07-25 07:35:20 (GMT) |
commit | 395f4c8873e784131dab6ee67133f80d03b23e4a (patch) | |
tree | c585f08dd3f16393c23114d277b458f975f1d0a0 /src/util.cc | |
parent | 0917ff862bc54626b68bcdc208551e9ab17a2e4a (diff) | |
download | Ninja-395f4c8873e784131dab6ee67133f80d03b23e4a.zip Ninja-395f4c8873e784131dab6ee67133f80d03b23e4a.tar.gz Ninja-395f4c8873e784131dab6ee67133f80d03b23e4a.tar.bz2 |
Prepared load (-l N) support for windows.
Inspired by:
http://stackoverflow.com/questions/23143693/retrieving-cpu-load-percent-total-in-windows-with-c
Diffstat (limited to 'src/util.cc')
-rw-r--r-- | src/util.cc | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/src/util.cc b/src/util.cc index 484b0c1..9d0282f 100644 --- a/src/util.cc +++ b/src/util.cc @@ -415,10 +415,51 @@ int GetProcessorCount() { } #if defined(_WIN32) || defined(__CYGWIN__) +static double CalculateProcessorLoad(uint64_t idleTicks, uint64_t totalTicks) +{ + static uint64_t previousIdleTicks = 0; + static uint64_t previousTotalTicks = 0; + + uint64_t idleTicksSinceLastTime = idleTicks - previousIdleTicks; + uint64_t totalTicksSinceLastTime = totalTicks - previousTotalTicks; + + double load; + if( previousTotalTicks > 0) { + //return error for first call + load = -0.0; + } else if(totalTicksSinceLastTime == 0) { + //return error when load cannot be calculated + load = -0.0; + } else { + double idleToTotalRatio = ((double)idleTicksSinceLastTime) / totalTicksSinceLastTime; + load = 1.0 - idleToTotalRatio; + } + + previousTotalTicks = totalTicks; + previousIdleTicks = idleTicks; + return load; +} + +static uint64_t FileTimeToTickCount(const FILETIME & ft) +{ + uint64_t high = (((uint64_t)(ft.dwHighDateTime)) << 32); + uint64_t low = ft.dwLowDateTime; + return (high | low); +} + double GetLoadAverage() { - // TODO(nicolas.despres@gmail.com): Find a way to implement it on Windows. - // Remember to also update Usage() when this is fixed. - return -0.0f; + FILETIME idleTime, kernelTime, userTime; + BOOL getSystemTimeSucceeded = GetSystemTimes(&idleTime, &kernelTime, &userTime); + + double result; + if (getSystemTimeSucceeded) { + uint64_t idleTicks = FileTimeToTickCount(idleTime); + uint64_t totalTicks = FileTimeToTickCount(kernelTime) + FileTimeToTickCount(userTime); + result = CalculateProcessorLoad(idleTicks, totalTicks); + } else { + result = -0.0; + } + return result; } #else double GetLoadAverage() { |