1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webrtc/LoadManager.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,139 @@ 1.4 +/* -*- Mode: C++; tab-width: 50; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "LoadManager.h" 1.10 +#include "LoadMonitor.h" 1.11 +#include "nsString.h" 1.12 +#include "prlog.h" 1.13 +#include "prtime.h" 1.14 +#include "prinrval.h" 1.15 +#include "prsystem.h" 1.16 + 1.17 +#include "nsString.h" 1.18 +#include "nsThreadUtils.h" 1.19 +#include "nsReadableUtils.h" 1.20 +#include "nsNetUtil.h" 1.21 + 1.22 +// NSPR_LOG_MODULES=LoadManager:5 1.23 +PRLogModuleInfo *gLoadManagerLog = nullptr; 1.24 +#undef LOG 1.25 +#undef LOG_ENABLED 1.26 +#if defined(PR_LOGGING) 1.27 +#define LOG(args) PR_LOG(gLoadManagerLog, PR_LOG_DEBUG, args) 1.28 +#define LOG_ENABLED() PR_LOG_TEST(gLoadManagerLog, 5) 1.29 +#else 1.30 +#define LOG(args) 1.31 +#define LOG_ENABLED() (false) 1.32 +#endif 1.33 + 1.34 +namespace mozilla { 1.35 + 1.36 +LoadManager::LoadManager(int aLoadMeasurementInterval, 1.37 + int aAveragingMeasurements, 1.38 + float aHighLoadThreshold, 1.39 + float aLowLoadThreshold) 1.40 + : mLoadSum(0.0f), 1.41 + mLoadSumMeasurements(0), 1.42 + mOveruseActive(false), 1.43 + mLoadMeasurementInterval(aLoadMeasurementInterval), 1.44 + mAveragingMeasurements(aAveragingMeasurements), 1.45 + mHighLoadThreshold(aHighLoadThreshold), 1.46 + mLowLoadThreshold(aLowLoadThreshold), 1.47 + mCurrentState(webrtc::kLoadNormal) 1.48 +{ 1.49 +#if defined(PR_LOGGING) 1.50 + if (!gLoadManagerLog) 1.51 + gLoadManagerLog = PR_NewLogModule("LoadManager"); 1.52 +#endif 1.53 + LOG(("LoadManager - Initializing (%dms x %d, %f, %f)", 1.54 + mLoadMeasurementInterval, mAveragingMeasurements, 1.55 + mHighLoadThreshold, mLowLoadThreshold)); 1.56 + MOZ_ASSERT(mHighLoadThreshold > mLowLoadThreshold); 1.57 + mLoadMonitor = new LoadMonitor(mLoadMeasurementInterval); 1.58 + mLoadMonitor->Init(mLoadMonitor); 1.59 + mLoadMonitor->SetLoadChangeCallback(this); 1.60 +} 1.61 + 1.62 +LoadManager::~LoadManager() 1.63 +{ 1.64 + mLoadMonitor->Shutdown(); 1.65 +} 1.66 + 1.67 +void 1.68 +LoadManager::LoadChanged(float aSystemLoad, float aProcesLoad) 1.69 +{ 1.70 + // Update total load, and total amount of measured seconds. 1.71 + mLoadSum += aSystemLoad; 1.72 + mLoadSumMeasurements++; 1.73 + 1.74 + if (mLoadSumMeasurements >= mAveragingMeasurements) { 1.75 + double averagedLoad = mLoadSum / (float)mLoadSumMeasurements; 1.76 + 1.77 + webrtc::CPULoadState oldState = mCurrentState; 1.78 + 1.79 + if (mOveruseActive || averagedLoad > mHighLoadThreshold) { 1.80 + LOG(("LoadManager - LoadStressed")); 1.81 + mCurrentState = webrtc::kLoadStressed; 1.82 + } else if (averagedLoad < mLowLoadThreshold) { 1.83 + LOG(("LoadManager - LoadRelaxed")); 1.84 + mCurrentState = webrtc::kLoadRelaxed; 1.85 + } else { 1.86 + LOG(("LoadManager - LoadNormal")); 1.87 + mCurrentState = webrtc::kLoadNormal; 1.88 + } 1.89 + 1.90 + if (oldState != mCurrentState) 1.91 + LoadHasChanged(); 1.92 + 1.93 + mLoadSum = 0; 1.94 + mLoadSumMeasurements = 0; 1.95 + } 1.96 +} 1.97 + 1.98 +void 1.99 +LoadManager::OveruseDetected() 1.100 +{ 1.101 + LOG(("LoadManager - Overuse Detected")); 1.102 + mOveruseActive = true; 1.103 + if (mCurrentState != webrtc::kLoadStressed) { 1.104 + mCurrentState = webrtc::kLoadStressed; 1.105 + LoadHasChanged(); 1.106 + } 1.107 +} 1.108 + 1.109 +void 1.110 +LoadManager::NormalUsage() 1.111 +{ 1.112 + LOG(("LoadManager - Overuse finished")); 1.113 + mOveruseActive = false; 1.114 +} 1.115 + 1.116 +void 1.117 +LoadManager::LoadHasChanged() 1.118 +{ 1.119 + LOG(("LoadManager - Signaling LoadHasChanged to %d listeners", mObservers.Length())); 1.120 + for (size_t i = 0; i < mObservers.Length(); i++) { 1.121 + mObservers.ElementAt(i)->onLoadStateChanged(mCurrentState); 1.122 + } 1.123 +} 1.124 + 1.125 +void 1.126 +LoadManager::AddObserver(webrtc::CPULoadStateObserver * aObserver) 1.127 +{ 1.128 + LOG(("LoadManager - Adding Observer")); 1.129 + mObservers.AppendElement(aObserver); 1.130 +} 1.131 + 1.132 +void 1.133 +LoadManager::RemoveObserver(webrtc::CPULoadStateObserver * aObserver) 1.134 +{ 1.135 + LOG(("LoadManager - Removing Observer")); 1.136 + if (!mObservers.RemoveElement(aObserver)) { 1.137 + LOG(("LOadManager - Element to remove not found")); 1.138 + } 1.139 +} 1.140 + 1.141 + 1.142 +}