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

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

mercurial