Refactored ViEPerformanceMonitor.

Only style changes, will follow up with references/ptrs.

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@1045 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
mflodman@webrtc.org
2011-11-29 02:39:28 +00:00
parent a85590d383
commit 611e4c3253
2 changed files with 132 additions and 158 deletions

View File

@@ -8,10 +8,6 @@
* be found in the AUTHORS file in the root of the source tree. * be found in the AUTHORS file in the root of the source tree.
*/ */
/*
* vie_performance_monitor.cc
*/
#include "vie_performance_monitor.h" #include "vie_performance_monitor.h"
#include "cpu_wrapper.h" #include "cpu_wrapper.h"
@@ -25,140 +21,122 @@
namespace webrtc { namespace webrtc {
// ---------------------------------------------------------------------------- enum { kVieMonitorPeriodMs = 975 };
// Constructor enum { kVieCpuStartValue = 75 };
// ----------------------------------------------------------------------------
ViEPerformanceMonitor::ViEPerformanceMonitor(int engineId) ViEPerformanceMonitor::ViEPerformanceMonitor(int engine_id)
: _engineId(engineId), : engine_id_(engine_id),
_pointerCritsect(*CriticalSectionWrapper::CreateCriticalSection()), pointer_critsect_(*CriticalSectionWrapper::CreateCriticalSection()),
_ptrViEMonitorThread(NULL), monitor_thread_(NULL),
_monitorkEvent(*EventWrapper::Create()), monitor_event_(*EventWrapper::Create()),
_averageApplicationCPU(kViECpuStartValue), average_application_cpu_(kVieCpuStartValue),
_averageSystemCPU(kViECpuStartValue), average_system_cpu_(kVieCpuStartValue),
_cpu(NULL), cpu_(NULL),
_vieBaseObserver(NULL) vie_base_observer_(NULL) {
{
} }
ViEPerformanceMonitor::~ViEPerformanceMonitor() ViEPerformanceMonitor::~ViEPerformanceMonitor() {
{ Terminate();
Terminate(); delete &pointer_critsect_;
delete &_pointerCritsect; delete &monitor_event_;
delete &_monitorkEvent;
} }
int ViEPerformanceMonitor::Init(ViEBaseObserver* vieBaseObserver) int ViEPerformanceMonitor::Init(ViEBaseObserver* vie_base_observer) {
{ WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_),
WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), "%s", __FUNCTION__);
"%s", __FUNCTION__);
CriticalSectionScoped cs(_pointerCritsect); CriticalSectionScoped cs(pointer_critsect_);
if (!vieBaseObserver || _vieBaseObserver) if (!vie_base_observer || vie_base_observer_) {
{ WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_),
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), "%s: Bad input argument or observer already set",
"%s: Bad input argument or observer already set", __FUNCTION__);
__FUNCTION__); return -1;
return -1; }
}
_cpu = CpuWrapper::CreateCpu(); cpu_ = CpuWrapper::CreateCpu();
if (_cpu == NULL) if (cpu_ == NULL) {
{ // Performance monitoring not supported
// Performance monitoring not supported WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideo,
WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideo, ViEId(engine_id_), "%s: Not supported", __FUNCTION__);
ViEId(_engineId), "%s: Not supported", __FUNCTION__);
return 0;
}
if (_ptrViEMonitorThread == NULL)
{
_monitorkEvent.StartTimer(true, kViEMonitorPeriodMs);
_ptrViEMonitorThread
= ThreadWrapper::CreateThread(ViEMonitorThreadFunction, this,
kNormalPriority,
"ViEPerformanceMonitor");
unsigned tId = 0;
if (_ptrViEMonitorThread->Start(tId))
{
WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId),
"%s: Performance monitor thread started %u",
__FUNCTION__, tId);
} else
{
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId),
"%s: Could not start performance monitor", __FUNCTION__);
_monitorkEvent.StopTimer();
return -1;
}
}
_vieBaseObserver = vieBaseObserver;
return 0; return 0;
} }
void ViEPerformanceMonitor::Terminate() if (monitor_thread_ == NULL) {
{ monitor_event_.StartTimer(true, kVieMonitorPeriodMs);
WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), monitor_thread_ = ThreadWrapper::CreateThread(ViEMonitorThreadFunction,
"%s", __FUNCTION__); this, kNormalPriority,
"ViEPerformanceMonitor");
_pointerCritsect.Enter(); unsigned int t_id = 0;
if (!_vieBaseObserver) if (monitor_thread_->Start(t_id)) {
{ WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_),
_pointerCritsect.Leave(); "%s: Performance monitor thread started %u",
return; __FUNCTION__, t_id);
} else {
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_),
"%s: Could not start performance monitor", __FUNCTION__);
monitor_event_.StopTimer();
return -1;
} }
_vieBaseObserver = NULL; }
_monitorkEvent.StopTimer(); vie_base_observer_ = vie_base_observer;
if (_ptrViEMonitorThread) return 0;
{
ThreadWrapper* tmpThread = _ptrViEMonitorThread;
_ptrViEMonitorThread = NULL;
_monitorkEvent.Set();
_pointerCritsect.Leave();
if (tmpThread->Stop())
{
_pointerCritsect.Enter();
delete tmpThread;
tmpThread = NULL;
delete _cpu;
}
_cpu = NULL;
}
_pointerCritsect.Leave();
} }
bool ViEPerformanceMonitor::ViEBaseObserverRegistered() void ViEPerformanceMonitor::Terminate() {
{ WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_),
CriticalSectionScoped cs(_pointerCritsect); "%s", __FUNCTION__);
return _vieBaseObserver != NULL;
pointer_critsect_.Enter();
if (!vie_base_observer_) {
pointer_critsect_.Leave();
return;
}
vie_base_observer_ = NULL;
monitor_event_.StopTimer();
if (monitor_thread_) {
ThreadWrapper* tmp_thread = monitor_thread_;
monitor_thread_ = NULL;
monitor_event_.Set();
pointer_critsect_.Leave();
if (tmp_thread->Stop()) {
pointer_critsect_.Enter();
delete tmp_thread;
tmp_thread = NULL;
delete cpu_;
}
cpu_ = NULL;
}
pointer_critsect_.Leave();
} }
bool ViEPerformanceMonitor::ViEMonitorThreadFunction(void* obj) bool ViEPerformanceMonitor::ViEBaseObserverRegistered() {
{ CriticalSectionScoped cs(pointer_critsect_);
return static_cast<ViEPerformanceMonitor*> (obj)->ViEMonitorProcess(); return vie_base_observer_ != NULL;
} }
bool ViEPerformanceMonitor::ViEMonitorProcess() bool ViEPerformanceMonitor::ViEMonitorThreadFunction(void* obj) {
{ return static_cast<ViEPerformanceMonitor*>(obj)->ViEMonitorProcess();
// Periodically triggered with time KViEMonitorPeriodMs
_monitorkEvent.Wait(kViEMonitorPeriodMs);
if (_ptrViEMonitorThread == NULL)
{
// Thread removed, exit
return false;
}
CriticalSectionScoped cs(_pointerCritsect);
if (_cpu)
{
int cpuLoad = _cpu->CpuUsage();
if (cpuLoad > 75)
{
if (_vieBaseObserver)
{
_vieBaseObserver->PerformanceAlarm(cpuLoad);
}
}
}
return true;
} }
} //namespace webrtc
bool ViEPerformanceMonitor::ViEMonitorProcess() {
// Periodically triggered with time KViEMonitorPeriodMs
monitor_event_.Wait(kVieMonitorPeriodMs);
if (monitor_thread_ == NULL) {
// Thread removed, exit
return false;
}
CriticalSectionScoped cs(pointer_critsect_);
if (cpu_) {
int cpu_load = cpu_->CpuUsage();
if (cpu_load > 75) {
if (vie_base_observer_) {
vie_base_observer_->PerformanceAlarm(cpu_load);
}
}
}
return true;
}
} // namespace webrtc

View File

@@ -8,51 +8,47 @@
* be found in the AUTHORS file in the root of the source tree. * be found in the AUTHORS file in the root of the source tree.
*/ */
/* // ViEPerformanceMonitor is used to check the current CPU usage and triggers a
* vie_performance_monitor.h // callback when getting over a specified threshold.
*/
#ifndef WEBRTC_VIDEO_ENGINE_MAIN_SOURCE_VIE_PERFORMANCE_MONITOR_H_ #ifndef WEBRTC_VIDEO_ENGINE_VIE_PERFORMANCE_MONITOR_H_
#define WEBRTC_VIDEO_ENGINE_MAIN_SOURCE_VIE_PERFORMANCE_MONITOR_H_ #define WEBRTC_VIDEO_ENGINE_VIE_PERFORMANCE_MONITOR_H_
// Defines
#include "vie_defines.h"
#include "typedefs.h" #include "typedefs.h"
#include "vie_defines.h"
namespace webrtc {
namespace webrtc
{
class CriticalSectionWrapper; class CriticalSectionWrapper;
class CpuWrapper; class CpuWrapper;
class EventWrapper; class EventWrapper;
class ThreadWrapper; class ThreadWrapper;
class ViEBaseObserver; class ViEBaseObserver;
class ViEPerformanceMonitor class ViEPerformanceMonitor {
{ public:
public: explicit ViEPerformanceMonitor(int engine_id);
ViEPerformanceMonitor(int engineId); ~ViEPerformanceMonitor();
~ViEPerformanceMonitor();
int Init(ViEBaseObserver* vieBaseObserver); int Init(ViEBaseObserver* vie_base_observer);
void Terminate(); void Terminate();
bool ViEBaseObserverRegistered(); bool ViEBaseObserverRegistered();
protected: protected:
static bool ViEMonitorThreadFunction(void* obj); static bool ViEMonitorThreadFunction(void* obj);
bool ViEMonitorProcess(); bool ViEMonitorProcess();
private: private:
enum { kViEMonitorPeriodMs = 975 }; const int engine_id_;
enum { kViECpuStartValue = 75 }; CriticalSectionWrapper& pointer_critsect_;
ThreadWrapper* monitor_thread_;
const int _engineId; EventWrapper& monitor_event_;
CriticalSectionWrapper& _pointerCritsect; int average_application_cpu_;
ThreadWrapper* _ptrViEMonitorThread; int average_system_cpu_;
EventWrapper& _monitorkEvent; CpuWrapper* cpu_;
int _averageApplicationCPU; ViEBaseObserver* vie_base_observer_;
int _averageSystemCPU;
CpuWrapper* _cpu;
ViEBaseObserver* _vieBaseObserver;
}; };
} // namespace webrtc
#endif // WEBRTC_VIDEO_ENGINE_MAIN_SOURCE_VIE_PERFORMANCE_MONITOR_H_ } // namespace webrtc
#endif // WEBRTC_VIDEO_ENGINE_VIE_PERFORMANCE_MONITOR_H_