From c693bac6e7ee70171c0d71bc036b9451cfe35784 Mon Sep 17 00:00:00 2001 From: "mflodman@webrtc.org" Date: Wed, 19 Oct 2011 13:40:58 +0000 Subject: [PATCH] Only start ViEPerformanceMonitor when needed. Tested by taking the added part in base extended test and running in Standard test with cpu threashold in ViEPeroformanceMonitor manually changed to 0. Review URL: http://webrtc-codereview.appspot.com/240005 git-svn-id: http://webrtc.googlecode.com/svn/trunk@772 4adac7df-926f-26a2-2b94-8c16560cd09d --- src/video_engine/main/source/vie_base_impl.cc | 19 +-- .../main/source/vie_performance_monitor.cc | 142 +++++++----------- .../main/source/vie_performance_monitor.h | 9 +- .../test/AutoTest/source/vie_autotest_base.cc | 29 ++++ 4 files changed, 94 insertions(+), 105 deletions(-) diff --git a/src/video_engine/main/source/vie_base_impl.cc b/src/video_engine/main/source/vie_base_impl.cc index 703ef82e9..58a5a2b72 100644 --- a/src/video_engine/main/source/vie_base_impl.cc +++ b/src/video_engine/main/source/vie_base_impl.cc @@ -97,8 +97,6 @@ ViEBaseImpl::~ViEBaseImpl() { WEBRTC_TRACE(webrtc::kTraceMemory, webrtc::kTraceVideo, _instanceId, "ViEBaseImpl::ViEBaseImpl() Dtor"); - - _viePerformanceMonitor.Terminate(); } // ---------------------------------------------------------------------------- @@ -119,9 +117,6 @@ int ViEBaseImpl::Init() } SetInitialized(); - - _viePerformanceMonitor.Init(); - return 0; } @@ -556,15 +551,14 @@ int ViEBaseImpl::StopReceive(const int videoChannel) int ViEBaseImpl::RegisterObserver(ViEBaseObserver& observer) { - WEBRTC_TRACE(webrtc::kTraceApiCall, webrtc::kTraceVideo, ViEId(_instanceId), "%s", - __FUNCTION__); + WEBRTC_TRACE(webrtc::kTraceApiCall, webrtc::kTraceVideo, ViEId(_instanceId), + "%s", __FUNCTION__); if (_viePerformanceMonitor.ViEBaseObserverRegistered()) { SetLastError(kViEBaseObserverAlreadyRegistered); return -1; } - - return _viePerformanceMonitor.RegisterViEBaseObserver(&observer); + return _viePerformanceMonitor.Init(&observer); } // ---------------------------------------------------------------------------- @@ -575,8 +569,8 @@ int ViEBaseImpl::RegisterObserver(ViEBaseObserver& observer) int ViEBaseImpl::DeregisterObserver() { - WEBRTC_TRACE(webrtc::kTraceApiCall, webrtc::kTraceVideo, ViEId(_instanceId), "%s", - __FUNCTION__); + WEBRTC_TRACE(webrtc::kTraceApiCall, webrtc::kTraceVideo, ViEId(_instanceId), + "%s", __FUNCTION__); if (!_viePerformanceMonitor.ViEBaseObserverRegistered()) { @@ -585,7 +579,8 @@ int ViEBaseImpl::DeregisterObserver() "%s No observer registered.", __FUNCTION__); return -1; } - return _viePerformanceMonitor.RegisterViEBaseObserver(NULL); + _viePerformanceMonitor.Terminate(); + return 0; } // ============================================================================ diff --git a/src/video_engine/main/source/vie_performance_monitor.cc b/src/video_engine/main/source/vie_performance_monitor.cc index eb1483e03..3fe706732 100644 --- a/src/video_engine/main/source/vie_performance_monitor.cc +++ b/src/video_engine/main/source/vie_performance_monitor.cc @@ -35,17 +35,10 @@ ViEPerformanceMonitor::ViEPerformanceMonitor(int engineId) _ptrViEMonitorThread(NULL), _monitorkEvent(*EventWrapper::Create()), _averageApplicationCPU(kViECpuStartValue), - _averageSystemCPU(kViECpuStartValue), _cpu(NULL), _vieBaseObserver(NULL) + _averageSystemCPU(kViECpuStartValue), + _cpu(NULL), + _vieBaseObserver(NULL) { - _cpu = CpuWrapper::CreateCpu(); - if (_cpu) - { - _cpu->CpuUsage(); // to initialize - } else - { - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Could not create CpuWrapper", __FUNCTION__); - } } ViEPerformanceMonitor::~ViEPerformanceMonitor() @@ -53,24 +46,31 @@ ViEPerformanceMonitor::~ViEPerformanceMonitor() Terminate(); delete &_pointerCritsect; delete &_monitorkEvent; - if (_cpu) - { - delete _cpu; - _cpu = NULL; - } } -int ViEPerformanceMonitor::Init() +int ViEPerformanceMonitor::Init(ViEBaseObserver* vieBaseObserver) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), + "%s", __FUNCTION__); + + CriticalSectionScoped cs(_pointerCritsect); + if (!vieBaseObserver || _vieBaseObserver) + { + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), + "%s: Bad input argument or observer already set", + __FUNCTION__); + return -1; + } + + _cpu = CpuWrapper::CreateCpu(); if (_cpu == NULL) { // Performance monitoring not supported - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Not supported", __FUNCTION__); + WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideo, + ViEId(_engineId), "%s: Not supported", __FUNCTION__); return 0; } - CriticalSectionScoped cs(_pointerCritsect); if (_ptrViEMonitorThread == NULL) { _monitorkEvent.StartTimer(true, kViEMonitorPeriodMs); @@ -92,74 +92,47 @@ int ViEPerformanceMonitor::Init() return -1; } } + _vieBaseObserver = vieBaseObserver; return 0; } -int ViEPerformanceMonitor::Terminate() +void ViEPerformanceMonitor::Terminate() { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), + "%s", __FUNCTION__); + + _pointerCritsect.Enter(); + if (!_vieBaseObserver) { - _pointerCritsect.Enter(); - _vieBaseObserver = NULL; _pointerCritsect.Leave(); - - _monitorkEvent.StopTimer(); - if (_ptrViEMonitorThread) - { - ThreadWrapper* tmpThread = _ptrViEMonitorThread; - _ptrViEMonitorThread = NULL; - _monitorkEvent.Set(); - if (tmpThread->Stop()) - { - delete tmpThread; - tmpThread = NULL; - } - } + return; } - return 0; + _vieBaseObserver = NULL; + _monitorkEvent.StopTimer(); + if (_ptrViEMonitorThread) + { + ThreadWrapper* tmpThread = _ptrViEMonitorThread; + _ptrViEMonitorThread = NULL; + _monitorkEvent.Set(); + _pointerCritsect.Leave(); + if (tmpThread->Stop()) + { + _pointerCritsect.Enter(); + delete tmpThread; + tmpThread = NULL; + delete _cpu; + } + _cpu = NULL; + } + _pointerCritsect.Leave(); } -int ViEPerformanceMonitor::RegisterViEBaseObserver( - ViEBaseObserver* vieBaseObserver) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), "%s", - __FUNCTION__); - CriticalSectionScoped cs(_pointerCritsect); - if (vieBaseObserver) - { - if (_vieBaseObserver) - { - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Observer already started", __FUNCTION__); - return -1; - } - _vieBaseObserver = vieBaseObserver; - } else - { - _vieBaseObserver = NULL; - } - return 0; -} bool ViEPerformanceMonitor::ViEBaseObserverRegistered() { CriticalSectionScoped cs(_pointerCritsect); return _vieBaseObserver != NULL; } -int ViEPerformanceMonitor::GetAverageApplicationCPU(int& applicationCPU) -{ - // Not supported - return -1; -} - -int ViEPerformanceMonitor::GetAverageSystemCPU(int& systemCPU) -{ - if (_cpu) - { - return _cpu->CpuUsage(); - } - return -1; -} - bool ViEPerformanceMonitor::ViEMonitorThreadFunction(void* obj) { return static_cast (obj)->ViEMonitorProcess(); @@ -169,23 +142,20 @@ bool ViEPerformanceMonitor::ViEMonitorProcess() { // Periodically triggered with time KViEMonitorPeriodMs _monitorkEvent.Wait(kViEMonitorPeriodMs); + if (_ptrViEMonitorThread == NULL) { - if (_ptrViEMonitorThread == NULL) + // Thread removed, exit + return false; + } + CriticalSectionScoped cs(_pointerCritsect); + if (_cpu) + { + int cpuLoad = _cpu->CpuUsage(); + if (cpuLoad > 75) { - // Thread removed, exit - return false; - } - if (_cpu) - { - int cpuLoad = _cpu->CpuUsage(); - if (cpuLoad > 75) + if (_vieBaseObserver) { - _pointerCritsect.Enter(); - if (_vieBaseObserver) - { - _vieBaseObserver->PerformanceAlarm(cpuLoad); - } - _pointerCritsect.Leave(); + _vieBaseObserver->PerformanceAlarm(cpuLoad); } } } diff --git a/src/video_engine/main/source/vie_performance_monitor.h b/src/video_engine/main/source/vie_performance_monitor.h index 6db1367ec..ac5d5665e 100644 --- a/src/video_engine/main/source/vie_performance_monitor.h +++ b/src/video_engine/main/source/vie_performance_monitor.h @@ -33,15 +33,10 @@ public: ViEPerformanceMonitor(int engineId); ~ViEPerformanceMonitor(); - int Init(); - int Terminate(); - int RegisterViEBaseObserver(ViEBaseObserver* vieBaseObserver); + int Init(ViEBaseObserver* vieBaseObserver); + void Terminate(); bool ViEBaseObserverRegistered(); - // ViEBase - int GetAverageApplicationCPU(int& applicationCPU); - int GetAverageSystemCPU(int& systemCPU); - protected: static bool ViEMonitorThreadFunction(void* obj); bool ViEMonitorProcess(); diff --git a/src/video_engine/main/test/AutoTest/source/vie_autotest_base.cc b/src/video_engine/main/test/AutoTest/source/vie_autotest_base.cc index 200d09c5a..cf7c4c124 100644 --- a/src/video_engine/main/test/AutoTest/source/vie_autotest_base.cc +++ b/src/video_engine/main/test/AutoTest/source/vie_autotest_base.cc @@ -18,6 +18,17 @@ #include "engine_configurations.h" #include "video_capture_factory.h" +class BaseObserver : public webrtc::ViEBaseObserver { + public: + BaseObserver() + : cpu_load_(0) {} + + virtual void PerformanceAlarm(const unsigned int cpu_load) { + cpu_load_ = cpu_load; + } + unsigned int cpu_load_; +}; + int ViEAutoTest::ViEBaseStandardTest() { ViETest::Log(" "); ViETest::Log("========================================"); @@ -315,6 +326,24 @@ int ViEAutoTest::ViEBaseExtendedTest() { ViETest::Log("========================================"); ViETest::Log(" ViEBase Extended Test"); + // *************************************************************** + // Test BaseObserver + // *************************************************************** + // TODO(mflodman) Add test for base observer. Cpu load must be over 75%. +// BaseObserver base_observer; +// error = ptrViEBase->RegisterObserver(base_observer); +// numberOfErrors += ViETest::TestError(error == 0, "ERROR: %s at line %d", +// __FUNCTION__, __LINE__); +// +// AutoTestSleep(KAutoTestSleepTimeMs); +// +// error = ptrViEBase->DeregisterObserver(); +// numberOfErrors += ViETest::TestError(error == 0, "ERROR: %s at line %d", +// __FUNCTION__, __LINE__); +// numberOfErrors += ViETest::TestError(base_observer.cpu_load_ > 0, +// "ERROR: %s at line %d", +// __FUNCTION__, __LINE__); + ViETest::Log(" "); ViETest::Log(" ViEBase Extended Test PASSED!"); ViETest::Log("========================================");