From 2b595fb329df6cc4cda6b420f545e4b2249f86b3 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Thu, 24 Mar 2016 16:26:28 -0700 Subject: [PATCH] break out thread safe moving average to get around unix build hell --- interface/src/Application.cpp | 2 +- libraries/gpu/src/gpu/Query.cpp | 2 +- libraries/shared/src/SimpleMovingAverage.h | 29 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 57fbe22577..4a815e453b 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -322,7 +322,7 @@ public: static std::atomic _maxElapsed; static std::atomic _maxElapsedAverage; bool _quit { false }; - MovingAverage _movingAverage; + ThreadSafeMovingAverage _movingAverage; }; std::atomic DeadlockWatchdogThread::_heartbeat; diff --git a/libraries/gpu/src/gpu/Query.cpp b/libraries/gpu/src/gpu/Query.cpp index 51bd16a979..2e28dcd061 100644 --- a/libraries/gpu/src/gpu/Query.cpp +++ b/libraries/gpu/src/gpu/Query.cpp @@ -67,5 +67,5 @@ void RangeTimer::end(gpu::Batch& batch) { } double RangeTimer::getAverage() const { - return _movingAverage.getAverage(); + return _movingAverage.average; } \ No newline at end of file diff --git a/libraries/shared/src/SimpleMovingAverage.h b/libraries/shared/src/SimpleMovingAverage.h index 44b1daa2e1..debe58da52 100644 --- a/libraries/shared/src/SimpleMovingAverage.h +++ b/libraries/shared/src/SimpleMovingAverage.h @@ -44,6 +44,11 @@ private: template class MovingAverage { public: + const float WEIGHTING = 1.0f / (float)MAX_NUM_SAMPLES; + const float ONE_MINUS_WEIGHTING = 1.0f - WEIGHTING; + int numSamples{ 0 }; + T average; + void clear() { numSamples = 0; } @@ -60,6 +65,29 @@ public: numSamples++; } + T getAverage() const { return average; } + T getNumSamples() const { return numSamples; } +}; + +template class ThreadSafeMovingAverage { +public: + void clear() { + numSamples = 0; + } + + bool isAverageValid() const { return (numSamples > 0); } + + void addSample(T sample) { + if (numSamples > 0) { + T lastAverage = average; + average = (sample * WEIGHTING) + (lastAverage * ONE_MINUS_WEIGHTING); + } + else { + average = sample; + } + numSamples++; + } + T getAverage() const { return average; } T getNumSamples() const { return numSamples; } @@ -70,4 +98,5 @@ private: std::atomic average; }; + #endif // hifi_SimpleMovingAverage_h