overte-HifiExperiments/libraries/shared/src/PerfStat.h
Anthony J. Thibault c4c3581285 Fix memory corruption in PerformanceTimer when Stats are active
PerformanceTimer was neither thread safe nor re-entrant.  Because it was being used increasingly on render and worker threads it has increased the likelihood of heap corruption.
I was able to identify this by enabling full page heap verification using the gflags app.

https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/enable-page-heap
2018-08-08 09:53:26 -07:00

109 lines
3.5 KiB
C++

//
// PerfStat.h
// libraries/shared/src
//
// Created by Brad Hefta-Gaub on 3/29/13.
// Copyright 2013 High Fidelity, Inc.
//
// Poor-man's performance stats collector class. Useful for collecting timing
// details from various portions of the code.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_PerfStat_h
#define hifi_PerfStat_h
#include <stdint.h>
#include "SharedUtil.h"
#include "SimpleMovingAverage.h"
#include <atomic>
#include <cstring>
#include <string>
#include <map>
using AtomicUIntStat = std::atomic<uintmax_t>;
class PerformanceWarning {
private:
quint64 _start;
const char* _message;
bool _renderWarningsOn;
bool _alwaysDisplay;
AtomicUIntStat* _runningTotal;
AtomicUIntStat* _totalCalls;
static bool _suppressShortTimings;
public:
PerformanceWarning(bool renderWarnings, const char* message, bool alwaysDisplay = false,
AtomicUIntStat* runningTotal = NULL, AtomicUIntStat* totalCalls = NULL) :
_start(usecTimestampNow()),
_message(message),
_renderWarningsOn(renderWarnings),
_alwaysDisplay(alwaysDisplay),
_runningTotal(runningTotal),
_totalCalls(totalCalls) { }
quint64 elapsed() const { return (usecTimestampNow() - _start); };
~PerformanceWarning();
static void setSuppressShortTimings(bool suppressShortTimings) { _suppressShortTimings = suppressShortTimings; }
};
class PerformanceTimerRecord {
public:
PerformanceTimerRecord() : _runningTotal(0), _lastTotal(0), _numAccumulations(0), _numTallies(0), _expiry(0) {}
void accumulateResult(const quint64& elapsed) { _runningTotal += elapsed; ++_numAccumulations; }
void tallyResult(const quint64& now);
bool isStale(const quint64& now) const { return now > _expiry; }
quint64 getAverage() const { return (_numTallies == 0) ? 0 : _runningTotal / _numTallies; }
quint64 getMovingAverage() const { return (_numTallies == 0) ? 0 : _movingAverage.getAverage(); }
quint64 getCount() const { return _numTallies; }
private:
quint64 _runningTotal;
quint64 _lastTotal;
quint64 _numAccumulations;
quint64 _numTallies;
quint64 _expiry;
SimpleMovingAverage _movingAverage;
};
class PerformanceTimer {
public:
PerformanceTimer(const QString& name);
~PerformanceTimer();
static bool isActive();
static void setActive(bool active);
static QString getContextName();
static void addTimerRecord(const QString& fullName, quint64 elapsedUsec);
static QMap<QString, PerformanceTimerRecord> getAllTimerRecords();
static void tallyAllTimerRecords();
static void dumpAllTimerRecords();
private:
quint64 _start = 0;
QString _name;
static std::atomic<bool> _isActive;
static std::mutex _mutex; // used to guard multi-threaded access to _fullNames and _records
static QHash<QThread*, QString> _fullNames;
static QMap<QString, PerformanceTimerRecord> _records;
};
// uncomment WANT_DETAILED_PERFORMANCE_TIMERS definition to enable performance timers in high-frequency contexts
//#define WANT_DETAILED_PERFORMANCE_TIMERS
#ifdef WANT_DETAILED_PERFORMANCE_TIMERS
#define DETAILED_PERFORMANCE_TIMER(name) PerformanceTimer detailedPerformanceTimer(name);
#else // WANT_DETAILED_PERFORMANCE_TIMERS
#define DETAILED_PERFORMANCE_TIMER(name) ; // no-op
#endif // WANT_DETAILED_PERFORMANCE_TIMERS
#endif // hifi_PerfStat_h