content/media/webrtc/LoadManager.cpp

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

michael@0 1 /* -*- Mode: C++; tab-width: 50; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "LoadManager.h"
michael@0 7 #include "LoadMonitor.h"
michael@0 8 #include "nsString.h"
michael@0 9 #include "prlog.h"
michael@0 10 #include "prtime.h"
michael@0 11 #include "prinrval.h"
michael@0 12 #include "prsystem.h"
michael@0 13
michael@0 14 #include "nsString.h"
michael@0 15 #include "nsThreadUtils.h"
michael@0 16 #include "nsReadableUtils.h"
michael@0 17 #include "nsNetUtil.h"
michael@0 18
michael@0 19 // NSPR_LOG_MODULES=LoadManager:5
michael@0 20 PRLogModuleInfo *gLoadManagerLog = nullptr;
michael@0 21 #undef LOG
michael@0 22 #undef LOG_ENABLED
michael@0 23 #if defined(PR_LOGGING)
michael@0 24 #define LOG(args) PR_LOG(gLoadManagerLog, PR_LOG_DEBUG, args)
michael@0 25 #define LOG_ENABLED() PR_LOG_TEST(gLoadManagerLog, 5)
michael@0 26 #else
michael@0 27 #define LOG(args)
michael@0 28 #define LOG_ENABLED() (false)
michael@0 29 #endif
michael@0 30
michael@0 31 namespace mozilla {
michael@0 32
michael@0 33 LoadManager::LoadManager(int aLoadMeasurementInterval,
michael@0 34 int aAveragingMeasurements,
michael@0 35 float aHighLoadThreshold,
michael@0 36 float aLowLoadThreshold)
michael@0 37 : mLoadSum(0.0f),
michael@0 38 mLoadSumMeasurements(0),
michael@0 39 mOveruseActive(false),
michael@0 40 mLoadMeasurementInterval(aLoadMeasurementInterval),
michael@0 41 mAveragingMeasurements(aAveragingMeasurements),
michael@0 42 mHighLoadThreshold(aHighLoadThreshold),
michael@0 43 mLowLoadThreshold(aLowLoadThreshold),
michael@0 44 mCurrentState(webrtc::kLoadNormal)
michael@0 45 {
michael@0 46 #if defined(PR_LOGGING)
michael@0 47 if (!gLoadManagerLog)
michael@0 48 gLoadManagerLog = PR_NewLogModule("LoadManager");
michael@0 49 #endif
michael@0 50 LOG(("LoadManager - Initializing (%dms x %d, %f, %f)",
michael@0 51 mLoadMeasurementInterval, mAveragingMeasurements,
michael@0 52 mHighLoadThreshold, mLowLoadThreshold));
michael@0 53 MOZ_ASSERT(mHighLoadThreshold > mLowLoadThreshold);
michael@0 54 mLoadMonitor = new LoadMonitor(mLoadMeasurementInterval);
michael@0 55 mLoadMonitor->Init(mLoadMonitor);
michael@0 56 mLoadMonitor->SetLoadChangeCallback(this);
michael@0 57 }
michael@0 58
michael@0 59 LoadManager::~LoadManager()
michael@0 60 {
michael@0 61 mLoadMonitor->Shutdown();
michael@0 62 }
michael@0 63
michael@0 64 void
michael@0 65 LoadManager::LoadChanged(float aSystemLoad, float aProcesLoad)
michael@0 66 {
michael@0 67 // Update total load, and total amount of measured seconds.
michael@0 68 mLoadSum += aSystemLoad;
michael@0 69 mLoadSumMeasurements++;
michael@0 70
michael@0 71 if (mLoadSumMeasurements >= mAveragingMeasurements) {
michael@0 72 double averagedLoad = mLoadSum / (float)mLoadSumMeasurements;
michael@0 73
michael@0 74 webrtc::CPULoadState oldState = mCurrentState;
michael@0 75
michael@0 76 if (mOveruseActive || averagedLoad > mHighLoadThreshold) {
michael@0 77 LOG(("LoadManager - LoadStressed"));
michael@0 78 mCurrentState = webrtc::kLoadStressed;
michael@0 79 } else if (averagedLoad < mLowLoadThreshold) {
michael@0 80 LOG(("LoadManager - LoadRelaxed"));
michael@0 81 mCurrentState = webrtc::kLoadRelaxed;
michael@0 82 } else {
michael@0 83 LOG(("LoadManager - LoadNormal"));
michael@0 84 mCurrentState = webrtc::kLoadNormal;
michael@0 85 }
michael@0 86
michael@0 87 if (oldState != mCurrentState)
michael@0 88 LoadHasChanged();
michael@0 89
michael@0 90 mLoadSum = 0;
michael@0 91 mLoadSumMeasurements = 0;
michael@0 92 }
michael@0 93 }
michael@0 94
michael@0 95 void
michael@0 96 LoadManager::OveruseDetected()
michael@0 97 {
michael@0 98 LOG(("LoadManager - Overuse Detected"));
michael@0 99 mOveruseActive = true;
michael@0 100 if (mCurrentState != webrtc::kLoadStressed) {
michael@0 101 mCurrentState = webrtc::kLoadStressed;
michael@0 102 LoadHasChanged();
michael@0 103 }
michael@0 104 }
michael@0 105
michael@0 106 void
michael@0 107 LoadManager::NormalUsage()
michael@0 108 {
michael@0 109 LOG(("LoadManager - Overuse finished"));
michael@0 110 mOveruseActive = false;
michael@0 111 }
michael@0 112
michael@0 113 void
michael@0 114 LoadManager::LoadHasChanged()
michael@0 115 {
michael@0 116 LOG(("LoadManager - Signaling LoadHasChanged to %d listeners", mObservers.Length()));
michael@0 117 for (size_t i = 0; i < mObservers.Length(); i++) {
michael@0 118 mObservers.ElementAt(i)->onLoadStateChanged(mCurrentState);
michael@0 119 }
michael@0 120 }
michael@0 121
michael@0 122 void
michael@0 123 LoadManager::AddObserver(webrtc::CPULoadStateObserver * aObserver)
michael@0 124 {
michael@0 125 LOG(("LoadManager - Adding Observer"));
michael@0 126 mObservers.AppendElement(aObserver);
michael@0 127 }
michael@0 128
michael@0 129 void
michael@0 130 LoadManager::RemoveObserver(webrtc::CPULoadStateObserver * aObserver)
michael@0 131 {
michael@0 132 LOG(("LoadManager - Removing Observer"));
michael@0 133 if (!mObservers.RemoveElement(aObserver)) {
michael@0 134 LOG(("LOadManager - Element to remove not found"));
michael@0 135 }
michael@0 136 }
michael@0 137
michael@0 138
michael@0 139 }

mercurial