Return cached data instead of sleeping in CpuWrapperMac (shaves 2s off WebrtcMediaEngine creation time on Mac).

Review URL: http://webrtc-codereview.appspot.com/226005

git-svn-id: http://webrtc.googlecode.com/svn/trunk@804 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
henrike@webrtc.org 2011-10-24 15:48:14 +00:00
parent 0a9c318c9f
commit 0d0037c2fd
2 changed files with 36 additions and 21 deletions

View File

@ -17,7 +17,12 @@
#include "tick_util.h" #include "tick_util.h"
namespace webrtc { namespace webrtc {
CpuWrapperMac::CpuWrapperMac() : _cpuUsage(NULL) CpuWrapperMac::CpuWrapperMac()
: _cpuCount(0),
_cpuUsage(NULL),
_totalCpuUsage(0),
_lastTickCount(NULL),
_lastTime(0)
{ {
natural_t cpuCount; natural_t cpuCount;
processor_info_array_t infoArray; processor_info_array_t infoArray;
@ -33,6 +38,7 @@ CpuWrapperMac::CpuWrapperMac() : _cpuUsage(NULL)
return; return;
} }
_cpuCount = cpuCount;
_cpuUsage = new WebRtc_UWord32[cpuCount]; _cpuUsage = new WebRtc_UWord32[cpuCount];
_lastTickCount = new WebRtc_Word64[cpuCount]; _lastTickCount = new WebRtc_Word64[cpuCount];
_lastTime = TickTime::MillisecondTimestamp(); _lastTime = TickTime::MillisecondTimestamp();
@ -47,14 +53,15 @@ CpuWrapperMac::CpuWrapperMac() : _cpuUsage(NULL)
ticks += cpuLoadInfo[cpu].cpu_ticks[state]; ticks += cpuLoadInfo[cpu].cpu_ticks[state];
} }
_lastTickCount[cpu] = ticks; _lastTickCount[cpu] = ticks;
_cpuUsage[cpu] = 0;
} }
vm_deallocate(mach_task_self(), (vm_address_t)infoArray, infoCount); vm_deallocate(mach_task_self(), (vm_address_t)infoArray, infoCount);
} }
CpuWrapperMac::~CpuWrapperMac() CpuWrapperMac::~CpuWrapperMac()
{ {
delete _cpuUsage; delete[] _cpuUsage;
delete _lastTickCount; delete[] _lastTickCount;
} }
WebRtc_Word32 CpuWrapperMac::CpuUsage() WebRtc_Word32 CpuWrapperMac::CpuUsage()
@ -68,29 +75,35 @@ WebRtc_Word32
CpuWrapperMac::CpuUsageMultiCore(WebRtc_UWord32& numCores, CpuWrapperMac::CpuUsageMultiCore(WebRtc_UWord32& numCores,
WebRtc_UWord32*& array) WebRtc_UWord32*& array)
{ {
natural_t cpuCount;
processor_info_array_t infoArray;
mach_msg_type_number_t infoCount;
// sanity check // sanity check
if(_cpuUsage == NULL) if(_cpuUsage == NULL)
{ {
return -1; return -1;
} }
WebRtc_Word64 now = TickTime::MillisecondTimestamp(); WebRtc_Word64 now = TickTime::MillisecondTimestamp();
WebRtc_Word64 timeDiffMS = now - _lastTime; WebRtc_Word64 timeDiffMS = now - _lastTime;
// TODO(hellner) why block here? Why not just return the old if(timeDiffMS >= 500)
// value? Is this behavior consistent across all
// platforms?
// Make sure that at least 500 ms pass between calls.
if(timeDiffMS < 500)
{ {
usleep((500-timeDiffMS)*1000); if(Update(timeDiffMS) != 0)
return CpuUsageMultiCore(numCores, array); {
return -1;
}
_lastTime = now;
} }
_lastTime = now;
numCores = _cpuCount;
array = _cpuUsage;
return _totalCpuUsage / _cpuCount;
}
kern_return_t error = host_processor_info(mach_host_self(), WebRtc_Word32 CpuWrapperMac::Update(WebRtc_Word64 timeDiffMS)
{
natural_t cpuCount;
processor_info_array_t infoArray;
mach_msg_type_number_t infoCount;
kern_return_t error = host_processor_info(mach_host_self(),
PROCESSOR_CPU_LOAD_INFO, PROCESSOR_CPU_LOAD_INFO,
&cpuCount, &cpuCount,
&infoArray, &infoArray,
@ -103,7 +116,7 @@ CpuWrapperMac::CpuUsageMultiCore(WebRtc_UWord32& numCores,
processor_cpu_load_info_data_t* cpuLoadInfo = processor_cpu_load_info_data_t* cpuLoadInfo =
(processor_cpu_load_info_data_t*) infoArray; (processor_cpu_load_info_data_t*) infoArray;
WebRtc_Word32 totalCpuUsage = 0; _totalCpuUsage = 0;
for (unsigned int cpu = 0; cpu < cpuCount; cpu++) for (unsigned int cpu = 0; cpu < cpuCount; cpu++)
{ {
WebRtc_Word64 ticks = 0; WebRtc_Word64 ticks = 0;
@ -120,13 +133,11 @@ CpuWrapperMac::CpuUsageMultiCore(WebRtc_UWord32& numCores,
timeDiffMS); timeDiffMS);
} }
_lastTickCount[cpu] = ticks; _lastTickCount[cpu] = ticks;
totalCpuUsage += _cpuUsage[cpu]; _totalCpuUsage += _cpuUsage[cpu];
} }
vm_deallocate(mach_task_self(), (vm_address_t)infoArray, infoCount); vm_deallocate(mach_task_self(), (vm_address_t)infoArray, infoCount);
numCores = cpuCount; return 0;
array = _cpuUsage;
return totalCpuUsage/cpuCount;
} }
} // namespace webrtc } // namespace webrtc

View File

@ -35,7 +35,11 @@ public:
virtual void Stop() {} virtual void Stop() {}
private: private:
WebRtc_Word32 Update(WebRtc_Word64 timeDiffMS);
WebRtc_UWord32 _cpuCount;
WebRtc_UWord32* _cpuUsage; WebRtc_UWord32* _cpuUsage;
WebRtc_Word32 _totalCpuUsage;
WebRtc_Word64* _lastTickCount; WebRtc_Word64* _lastTickCount;
WebRtc_Word64 _lastTime; WebRtc_Word64 _lastTime;
}; };