break out thread safe moving average to get around unix build hell

This commit is contained in:
Brad Hefta-Gaub 2016-03-24 16:26:28 -07:00
parent 8691aa6905
commit 2b595fb329
3 changed files with 31 additions and 2 deletions
interface/src
libraries
gpu/src/gpu
shared/src

View file

@ -322,7 +322,7 @@ public:
static std::atomic<int> _maxElapsed;
static std::atomic<int> _maxElapsedAverage;
bool _quit { false };
MovingAverage<int, HEARTBEAT_SAMPLES> _movingAverage;
ThreadSafeMovingAverage<int, HEARTBEAT_SAMPLES> _movingAverage;
};
std::atomic<uint64_t> DeadlockWatchdogThread::_heartbeat;

View file

@ -67,5 +67,5 @@ void RangeTimer::end(gpu::Batch& batch) {
}
double RangeTimer::getAverage() const {
return _movingAverage.getAverage();
return _movingAverage.average;
}

View file

@ -44,6 +44,11 @@ private:
template <class T, int MAX_NUM_SAMPLES> 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 T, int MAX_NUM_SAMPLES> 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<T> average;
};
#endif // hifi_SimpleMovingAverage_h