michael@0: /* -*- Mode: C++; tab-width: 50; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "LoadManager.h" michael@0: #include "LoadMonitor.h" michael@0: #include "nsString.h" michael@0: #include "prlog.h" michael@0: #include "prtime.h" michael@0: #include "prinrval.h" michael@0: #include "prsystem.h" michael@0: michael@0: #include "nsString.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "nsNetUtil.h" michael@0: michael@0: // NSPR_LOG_MODULES=LoadManager:5 michael@0: PRLogModuleInfo *gLoadManagerLog = nullptr; michael@0: #undef LOG michael@0: #undef LOG_ENABLED michael@0: #if defined(PR_LOGGING) michael@0: #define LOG(args) PR_LOG(gLoadManagerLog, PR_LOG_DEBUG, args) michael@0: #define LOG_ENABLED() PR_LOG_TEST(gLoadManagerLog, 5) michael@0: #else michael@0: #define LOG(args) michael@0: #define LOG_ENABLED() (false) michael@0: #endif michael@0: michael@0: namespace mozilla { michael@0: michael@0: LoadManager::LoadManager(int aLoadMeasurementInterval, michael@0: int aAveragingMeasurements, michael@0: float aHighLoadThreshold, michael@0: float aLowLoadThreshold) michael@0: : mLoadSum(0.0f), michael@0: mLoadSumMeasurements(0), michael@0: mOveruseActive(false), michael@0: mLoadMeasurementInterval(aLoadMeasurementInterval), michael@0: mAveragingMeasurements(aAveragingMeasurements), michael@0: mHighLoadThreshold(aHighLoadThreshold), michael@0: mLowLoadThreshold(aLowLoadThreshold), michael@0: mCurrentState(webrtc::kLoadNormal) michael@0: { michael@0: #if defined(PR_LOGGING) michael@0: if (!gLoadManagerLog) michael@0: gLoadManagerLog = PR_NewLogModule("LoadManager"); michael@0: #endif michael@0: LOG(("LoadManager - Initializing (%dms x %d, %f, %f)", michael@0: mLoadMeasurementInterval, mAveragingMeasurements, michael@0: mHighLoadThreshold, mLowLoadThreshold)); michael@0: MOZ_ASSERT(mHighLoadThreshold > mLowLoadThreshold); michael@0: mLoadMonitor = new LoadMonitor(mLoadMeasurementInterval); michael@0: mLoadMonitor->Init(mLoadMonitor); michael@0: mLoadMonitor->SetLoadChangeCallback(this); michael@0: } michael@0: michael@0: LoadManager::~LoadManager() michael@0: { michael@0: mLoadMonitor->Shutdown(); michael@0: } michael@0: michael@0: void michael@0: LoadManager::LoadChanged(float aSystemLoad, float aProcesLoad) michael@0: { michael@0: // Update total load, and total amount of measured seconds. michael@0: mLoadSum += aSystemLoad; michael@0: mLoadSumMeasurements++; michael@0: michael@0: if (mLoadSumMeasurements >= mAveragingMeasurements) { michael@0: double averagedLoad = mLoadSum / (float)mLoadSumMeasurements; michael@0: michael@0: webrtc::CPULoadState oldState = mCurrentState; michael@0: michael@0: if (mOveruseActive || averagedLoad > mHighLoadThreshold) { michael@0: LOG(("LoadManager - LoadStressed")); michael@0: mCurrentState = webrtc::kLoadStressed; michael@0: } else if (averagedLoad < mLowLoadThreshold) { michael@0: LOG(("LoadManager - LoadRelaxed")); michael@0: mCurrentState = webrtc::kLoadRelaxed; michael@0: } else { michael@0: LOG(("LoadManager - LoadNormal")); michael@0: mCurrentState = webrtc::kLoadNormal; michael@0: } michael@0: michael@0: if (oldState != mCurrentState) michael@0: LoadHasChanged(); michael@0: michael@0: mLoadSum = 0; michael@0: mLoadSumMeasurements = 0; michael@0: } michael@0: } michael@0: michael@0: void michael@0: LoadManager::OveruseDetected() michael@0: { michael@0: LOG(("LoadManager - Overuse Detected")); michael@0: mOveruseActive = true; michael@0: if (mCurrentState != webrtc::kLoadStressed) { michael@0: mCurrentState = webrtc::kLoadStressed; michael@0: LoadHasChanged(); michael@0: } michael@0: } michael@0: michael@0: void michael@0: LoadManager::NormalUsage() michael@0: { michael@0: LOG(("LoadManager - Overuse finished")); michael@0: mOveruseActive = false; michael@0: } michael@0: michael@0: void michael@0: LoadManager::LoadHasChanged() michael@0: { michael@0: LOG(("LoadManager - Signaling LoadHasChanged to %d listeners", mObservers.Length())); michael@0: for (size_t i = 0; i < mObservers.Length(); i++) { michael@0: mObservers.ElementAt(i)->onLoadStateChanged(mCurrentState); michael@0: } michael@0: } michael@0: michael@0: void michael@0: LoadManager::AddObserver(webrtc::CPULoadStateObserver * aObserver) michael@0: { michael@0: LOG(("LoadManager - Adding Observer")); michael@0: mObservers.AppendElement(aObserver); michael@0: } michael@0: michael@0: void michael@0: LoadManager::RemoveObserver(webrtc::CPULoadStateObserver * aObserver) michael@0: { michael@0: LOG(("LoadManager - Removing Observer")); michael@0: if (!mObservers.RemoveElement(aObserver)) { michael@0: LOG(("LOadManager - Element to remove not found")); michael@0: } michael@0: } michael@0: michael@0: michael@0: }