From c6ce5e7680a629190aa3a2a4e4782e868f62bf07 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 22 May 2015 18:54:40 +0200 Subject: [PATCH 1/2] PerformanceTimer deactivated when not in use --- interface/src/ui/ApplicationOverlay.cpp | 8 ++++ interface/src/ui/Stats.cpp | 9 +++-- interface/src/ui/Stats.h | 2 + libraries/shared/src/PerfStat.cpp | 49 ++++++++++++++++++------- libraries/shared/src/PerfStat.h | 6 ++- 5 files changed, 55 insertions(+), 19 deletions(-) diff --git a/interface/src/ui/ApplicationOverlay.cpp b/interface/src/ui/ApplicationOverlay.cpp index e3f9216c13..c65de2afb0 100644 --- a/interface/src/ui/ApplicationOverlay.cpp +++ b/interface/src/ui/ApplicationOverlay.cpp @@ -979,6 +979,14 @@ void ApplicationOverlay::renderStatsAndLogs() { glLineWidth(1.0f); glPointSize(1.0f); + // Determine whether to compute timing details + bool shouldDisplayTimingDetail = Menu::getInstance()->isOptionChecked(MenuOption::DisplayDebugTimingDetails) && + Menu::getInstance()->isOptionChecked(MenuOption::Stats) && + Stats::getInstance()->isExpanded(); + if (shouldDisplayTimingDetail != PerformanceTimer::isActive()) { + PerformanceTimer::setActive(shouldDisplayTimingDetail); + } + if (Menu::getInstance()->isOptionChecked(MenuOption::Stats)) { // let's set horizontal offset to give stats some margin to mirror int horizontalOffset = MIRROR_VIEW_WIDTH + MIRROR_VIEW_LEFT_PADDING * 2; diff --git a/interface/src/ui/Stats.cpp b/interface/src/ui/Stats.cpp index 161eb06f5f..c49208c835 100644 --- a/interface/src/ui/Stats.cpp +++ b/interface/src/ui/Stats.cpp @@ -224,9 +224,10 @@ void Stats::display( lines = 5; int columnOneWidth = _generalStatsWidth; - PerformanceTimer::tallyAllTimerRecords(); // do this even if we're not displaying them, so they don't stack up - - if (_expanded && Menu::getInstance()->isOptionChecked(MenuOption::DisplayDebugTimingDetails)) { + bool performanceTimerIsActive = PerformanceTimer::isActive(); + bool displayPerf = _expanded && Menu::getInstance()->isOptionChecked(MenuOption::DisplayDebugTimingDetails); + if (displayPerf && performanceTimerIsActive) { + PerformanceTimer::tallyAllTimerRecords(); // do this even if we're not displaying them, so they don't stack up columnOneWidth = _generalStatsWidth + _pingStatsWidth + _geoStatsWidth; // 3 columns wide... // we will also include room for 1 line per timing record and a header of 4 lines @@ -276,7 +277,7 @@ void Stats::display( // TODO: the display of these timing details should all be moved to JavaScript - if (_expanded && Menu::getInstance()->isOptionChecked(MenuOption::DisplayDebugTimingDetails)) { + if (displayPerf && performanceTimerIsActive) { bool onlyDisplayTopTen = Menu::getInstance()->isOptionChecked(MenuOption::OnlyDisplayTopTen); // Timing details... verticalOffset += STATS_PELS_PER_LINE * 4; // skip 3 lines to be under the other columns diff --git a/interface/src/ui/Stats.h b/interface/src/ui/Stats.h index 406496d858..00c1650b71 100644 --- a/interface/src/ui/Stats.h +++ b/interface/src/ui/Stats.h @@ -27,6 +27,8 @@ public: static void drawBackground(unsigned int rgba, int x, int y, int width, int height); void toggleExpanded(); + bool isExpanded() { return _expanded; } + void checkClick(int mouseX, int mouseY, int mouseDragStartedX, int mouseDragStartedY, int horizontalOffset); void resetWidth(int width, int horizontalOffset); void display(const float* color, int horizontalOffset, float fps, int inPacketsPerSecond, int outPacketsPerSecond, diff --git a/libraries/shared/src/PerfStat.cpp b/libraries/shared/src/PerfStat.cpp index 286d257d1f..22cf000c24 100644 --- a/libraries/shared/src/PerfStat.cpp +++ b/libraries/shared/src/PerfStat.cpp @@ -79,29 +79,50 @@ void PerformanceTimerRecord::tallyResult(const quint64& now) { // PerformanceTimer // ---------------------------------------------------------------------------- +std::atomic PerformanceTimer::_isActive(false); QHash PerformanceTimer::_fullNames; QMap PerformanceTimer::_records; -PerformanceTimer::PerformanceTimer(const QString& name) : - _start(0), - _name(name) -{ - QString& fullName = _fullNames[QThread::currentThread()]; - fullName.append("/"); - fullName.append(_name); - _start = usecTimestampNow(); +PerformanceTimer::PerformanceTimer(const QString& name) { + if (_isActive) { + _name = name; + QString& fullName = _fullNames[QThread::currentThread()]; + fullName.append("/"); + fullName.append(_name); + _start = usecTimestampNow(); + } } PerformanceTimer::~PerformanceTimer() { - quint64 elapsedusec = (usecTimestampNow() - _start); - QString& fullName = _fullNames[QThread::currentThread()]; - PerformanceTimerRecord& namedRecord = _records[fullName]; - namedRecord.accumulateResult(elapsedusec); - fullName.resize(fullName.size() - (_name.size() + 1)); + if (_isActive && _start != 0) { + quint64 elapsedusec = (usecTimestampNow() - _start); + QString& fullName = _fullNames[QThread::currentThread()]; + PerformanceTimerRecord& namedRecord = _records[fullName]; + namedRecord.accumulateResult(elapsedusec); + fullName.resize(fullName.size() - (_name.size() + 1)); + } } -// static +// static +bool PerformanceTimer::PerformanceTimer::isActive() { + return _isActive; +} + +// static +void PerformanceTimer::setActive(bool active) { + if (active != _isActive) { + _isActive.store(active); + if (!active) { + _fullNames.clear(); + _records.clear(); + } + + qDebug() << "PerformanceTimer has been turned" << ((active) ? "on" : "off"); + } +} + +// static void PerformanceTimer::tallyAllTimerRecords() { QMap::iterator recordsItr = _records.begin(); QMap::const_iterator recordsEnd = _records.end(); diff --git a/libraries/shared/src/PerfStat.h b/libraries/shared/src/PerfStat.h index 81731abfa9..2d5a54db38 100644 --- a/libraries/shared/src/PerfStat.h +++ b/libraries/shared/src/PerfStat.h @@ -76,14 +76,18 @@ public: PerformanceTimer(const QString& name); ~PerformanceTimer(); + static bool isActive(); + static void setActive(bool active); + static const PerformanceTimerRecord& getTimerRecord(const QString& name) { return _records[name]; }; static const QMap& getAllTimerRecords() { return _records; }; static void tallyAllTimerRecords(); static void dumpAllTimerRecords(); private: - quint64 _start; + quint64 _start = 0; QString _name; + static std::atomic _isActive; static QHash _fullNames; static QMap _records; }; From e2769266089ef2270e83e7974ffcccdf74b7917b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Brisset?= Date: Fri, 22 May 2015 18:23:42 +0100 Subject: [PATCH 2/2] Add include/remove duplicate type --- libraries/shared/src/PerfStat.cpp | 2 +- libraries/shared/src/PerfStat.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/shared/src/PerfStat.cpp b/libraries/shared/src/PerfStat.cpp index 22cf000c24..615330e13f 100644 --- a/libraries/shared/src/PerfStat.cpp +++ b/libraries/shared/src/PerfStat.cpp @@ -105,7 +105,7 @@ PerformanceTimer::~PerformanceTimer() { } // static -bool PerformanceTimer::PerformanceTimer::isActive() { +bool PerformanceTimer::isActive() { return _isActive; } diff --git a/libraries/shared/src/PerfStat.h b/libraries/shared/src/PerfStat.h index 2d5a54db38..523bee0808 100644 --- a/libraries/shared/src/PerfStat.h +++ b/libraries/shared/src/PerfStat.h @@ -19,6 +19,7 @@ #include "SharedUtil.h" #include "SimpleMovingAverage.h" +#include #include #include #include