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 &_pointerCritsect; delete &pointer_critsect_;
delete &_monitorkEvent; delete &monitor_event_;
} }
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(_engineId), "%s: Not supported", __FUNCTION__); ViEId(engine_id_), "%s: Not supported", __FUNCTION__);
return 0; return 0;
} }
if (_ptrViEMonitorThread == NULL) if (monitor_thread_ == NULL) {
{ monitor_event_.StartTimer(true, kVieMonitorPeriodMs);
_monitorkEvent.StartTimer(true, kViEMonitorPeriodMs); monitor_thread_ = ThreadWrapper::CreateThread(ViEMonitorThreadFunction,
_ptrViEMonitorThread this, kNormalPriority,
= ThreadWrapper::CreateThread(ViEMonitorThreadFunction, this,
kNormalPriority,
"ViEPerformanceMonitor"); "ViEPerformanceMonitor");
unsigned tId = 0; unsigned int t_id = 0;
if (_ptrViEMonitorThread->Start(tId)) if (monitor_thread_->Start(t_id)) {
{ WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_),
WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId),
"%s: Performance monitor thread started %u", "%s: Performance monitor thread started %u",
__FUNCTION__, tId); __FUNCTION__, t_id);
} else } else {
{ WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_),
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId),
"%s: Could not start performance monitor", __FUNCTION__); "%s: Could not start performance monitor", __FUNCTION__);
_monitorkEvent.StopTimer(); monitor_event_.StopTimer();
return -1; return -1;
} }
} }
_vieBaseObserver = vieBaseObserver; vie_base_observer_ = vie_base_observer;
return 0; return 0;
} }
void ViEPerformanceMonitor::Terminate() void ViEPerformanceMonitor::Terminate() {
{ WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_),
WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId),
"%s", __FUNCTION__); "%s", __FUNCTION__);
_pointerCritsect.Enter(); pointer_critsect_.Enter();
if (!_vieBaseObserver) if (!vie_base_observer_) {
{ pointer_critsect_.Leave();
_pointerCritsect.Leave();
return; return;
} }
_vieBaseObserver = NULL;
_monitorkEvent.StopTimer(); vie_base_observer_ = NULL;
if (_ptrViEMonitorThread) monitor_event_.StopTimer();
{ if (monitor_thread_) {
ThreadWrapper* tmpThread = _ptrViEMonitorThread; ThreadWrapper* tmp_thread = monitor_thread_;
_ptrViEMonitorThread = NULL; monitor_thread_ = NULL;
_monitorkEvent.Set(); monitor_event_.Set();
_pointerCritsect.Leave(); pointer_critsect_.Leave();
if (tmpThread->Stop()) if (tmp_thread->Stop()) {
{ pointer_critsect_.Enter();
_pointerCritsect.Enter(); delete tmp_thread;
delete tmpThread; tmp_thread = NULL;
tmpThread = NULL; delete cpu_;
delete _cpu;
} }
_cpu = NULL; cpu_ = NULL;
} }
_pointerCritsect.Leave(); pointer_critsect_.Leave();
} }
bool ViEPerformanceMonitor::ViEBaseObserverRegistered() bool ViEPerformanceMonitor::ViEBaseObserverRegistered() {
{ CriticalSectionScoped cs(pointer_critsect_);
CriticalSectionScoped cs(_pointerCritsect); return vie_base_observer_ != NULL;
return _vieBaseObserver != NULL;
} }
bool ViEPerformanceMonitor::ViEMonitorThreadFunction(void* obj) bool ViEPerformanceMonitor::ViEMonitorThreadFunction(void* obj) {
{
return static_cast<ViEPerformanceMonitor*>(obj)->ViEMonitorProcess(); return static_cast<ViEPerformanceMonitor*>(obj)->ViEMonitorProcess();
} }
bool ViEPerformanceMonitor::ViEMonitorProcess() bool ViEPerformanceMonitor::ViEMonitorProcess() {
{
// Periodically triggered with time KViEMonitorPeriodMs // Periodically triggered with time KViEMonitorPeriodMs
_monitorkEvent.Wait(kViEMonitorPeriodMs); monitor_event_.Wait(kVieMonitorPeriodMs);
if (_ptrViEMonitorThread == NULL) if (monitor_thread_ == NULL) {
{
// Thread removed, exit // Thread removed, exit
return false; return false;
} }
CriticalSectionScoped cs(_pointerCritsect);
if (_cpu) CriticalSectionScoped cs(pointer_critsect_);
{ if (cpu_) {
int cpuLoad = _cpu->CpuUsage(); int cpu_load = cpu_->CpuUsage();
if (cpuLoad > 75) if (cpu_load > 75) {
{ if (vie_base_observer_) {
if (_vieBaseObserver) vie_base_observer_->PerformanceAlarm(cpu_load);
{
_vieBaseObserver->PerformanceAlarm(cpuLoad);
} }
} }
} }
return true; return true;
} }
} // namespace webrtc } // namespace webrtc

View File

@@ -8,32 +8,29 @@
* 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:
ViEPerformanceMonitor(int engineId); explicit ViEPerformanceMonitor(int engine_id);
~ViEPerformanceMonitor(); ~ViEPerformanceMonitor();
int Init(ViEBaseObserver* vieBaseObserver); int Init(ViEBaseObserver* vie_base_observer);
void Terminate(); void Terminate();
bool ViEBaseObserverRegistered(); bool ViEBaseObserverRegistered();
@@ -42,17 +39,16 @@ protected:
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 } // namespace webrtc
#endif // WEBRTC_VIDEO_ENGINE_MAIN_SOURCE_VIE_PERFORMANCE_MONITOR_H_
#endif // WEBRTC_VIDEO_ENGINE_VIE_PERFORMANCE_MONITOR_H_