15 #ifndef hifi_PerfStat_h
16 #define hifi_PerfStat_h
19 #include "SharedUtil.h"
20 #include "SimpleMovingAverage.h"
27 using AtomicUIntStat = std::atomic<uintmax_t>;
29 class PerformanceWarning {
33 bool _renderWarningsOn;
35 AtomicUIntStat* _runningTotal;
36 AtomicUIntStat* _totalCalls;
37 static bool _suppressShortTimings;
40 PerformanceWarning(
bool renderWarnings,
const char* message,
bool alwaysDisplay =
false,
41 AtomicUIntStat* runningTotal = NULL, AtomicUIntStat* totalCalls = NULL) :
42 _start(usecTimestampNow()),
44 _renderWarningsOn(renderWarnings),
45 _alwaysDisplay(alwaysDisplay),
46 _runningTotal(runningTotal),
47 _totalCalls(totalCalls) { }
49 quint64 elapsed()
const {
return (usecTimestampNow() - _start); };
51 ~PerformanceWarning();
53 static void setSuppressShortTimings(
bool suppressShortTimings) { _suppressShortTimings = suppressShortTimings; }
56 class PerformanceTimerRecord {
58 PerformanceTimerRecord() : _runningTotal(0), _lastTotal(0), _numAccumulations(0), _numTallies(0), _expiry(0) {}
60 void accumulateResult(
const quint64& elapsed) { _runningTotal += elapsed; ++_numAccumulations; }
61 void tallyResult(
const quint64& now);
62 bool isStale(
const quint64& now)
const {
return now > _expiry; }
63 quint64 getAverage()
const {
return (_numTallies == 0) ? 0 : _runningTotal / _numTallies; }
64 quint64 getMovingAverage()
const {
return (_numTallies == 0) ? 0 : _movingAverage.getAverage(); }
65 quint64 getCount()
const {
return _numTallies; }
68 quint64 _runningTotal;
70 quint64 _numAccumulations;
73 SimpleMovingAverage _movingAverage;
76 class PerformanceTimer {
79 PerformanceTimer(
const QString& name);
82 static bool isActive();
83 static void setActive(
bool active);
85 static QString getContextName();
86 static void addTimerRecord(
const QString& fullName, quint64 elapsedUsec);
87 static QMap<QString, PerformanceTimerRecord> getAllTimerRecords();
88 static void tallyAllTimerRecords();
89 static void dumpAllTimerRecords();
94 static std::atomic<bool> _isActive;
96 static std::mutex _mutex;
97 static QHash<QThread*, QString> _fullNames;
98 static QMap<QString, PerformanceTimerRecord> _records;
103 #ifdef WANT_DETAILED_PERFORMANCE_TIMERS
104 #define DETAILED_PERFORMANCE_TIMER(name) PerformanceTimer detailedPerformanceTimer(name);
106 #define DETAILED_PERFORMANCE_TIMER(name) ;