Merge pull request #4939 from Atlante45/perf_timer_tweaks

Deactivate PerformanceTimer when not in use
This commit is contained in:
Brad Hefta-Gaub 2015-05-22 10:46:13 -07:00
commit 7ed3a97bf2
5 changed files with 56 additions and 19 deletions

View file

@ -979,6 +979,14 @@ void ApplicationOverlay::renderStatsAndLogs() {
glLineWidth(1.0f); glLineWidth(1.0f);
glPointSize(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)) { if (Menu::getInstance()->isOptionChecked(MenuOption::Stats)) {
// let's set horizontal offset to give stats some margin to mirror // let's set horizontal offset to give stats some margin to mirror
int horizontalOffset = MIRROR_VIEW_WIDTH + MIRROR_VIEW_LEFT_PADDING * 2; int horizontalOffset = MIRROR_VIEW_WIDTH + MIRROR_VIEW_LEFT_PADDING * 2;

View file

@ -224,9 +224,10 @@ void Stats::display(
lines = 5; lines = 5;
int columnOneWidth = _generalStatsWidth; int columnOneWidth = _generalStatsWidth;
PerformanceTimer::tallyAllTimerRecords(); // do this even if we're not displaying them, so they don't stack up bool performanceTimerIsActive = PerformanceTimer::isActive();
bool displayPerf = _expanded && Menu::getInstance()->isOptionChecked(MenuOption::DisplayDebugTimingDetails);
if (_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... columnOneWidth = _generalStatsWidth + _pingStatsWidth + _geoStatsWidth; // 3 columns wide...
// we will also include room for 1 line per timing record and a header of 4 lines // 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 // 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); bool onlyDisplayTopTen = Menu::getInstance()->isOptionChecked(MenuOption::OnlyDisplayTopTen);
// Timing details... // Timing details...
verticalOffset += STATS_PELS_PER_LINE * 4; // skip 3 lines to be under the other columns verticalOffset += STATS_PELS_PER_LINE * 4; // skip 3 lines to be under the other columns

View file

@ -27,6 +27,8 @@ public:
static void drawBackground(unsigned int rgba, int x, int y, int width, int height); static void drawBackground(unsigned int rgba, int x, int y, int width, int height);
void toggleExpanded(); void toggleExpanded();
bool isExpanded() { return _expanded; }
void checkClick(int mouseX, int mouseY, int mouseDragStartedX, int mouseDragStartedY, int horizontalOffset); void checkClick(int mouseX, int mouseY, int mouseDragStartedX, int mouseDragStartedY, int horizontalOffset);
void resetWidth(int width, int horizontalOffset); void resetWidth(int width, int horizontalOffset);
void display(const float* color, int horizontalOffset, float fps, int inPacketsPerSecond, int outPacketsPerSecond, void display(const float* color, int horizontalOffset, float fps, int inPacketsPerSecond, int outPacketsPerSecond,

View file

@ -79,29 +79,50 @@ void PerformanceTimerRecord::tallyResult(const quint64& now) {
// PerformanceTimer // PerformanceTimer
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
std::atomic<bool> PerformanceTimer::_isActive(false);
QHash<QThread*, QString> PerformanceTimer::_fullNames; QHash<QThread*, QString> PerformanceTimer::_fullNames;
QMap<QString, PerformanceTimerRecord> PerformanceTimer::_records; QMap<QString, PerformanceTimerRecord> PerformanceTimer::_records;
PerformanceTimer::PerformanceTimer(const QString& name) : PerformanceTimer::PerformanceTimer(const QString& name) {
_start(0), if (_isActive) {
_name(name) _name = name;
{ QString& fullName = _fullNames[QThread::currentThread()];
QString& fullName = _fullNames[QThread::currentThread()]; fullName.append("/");
fullName.append("/"); fullName.append(_name);
fullName.append(_name); _start = usecTimestampNow();
_start = usecTimestampNow(); }
} }
PerformanceTimer::~PerformanceTimer() { PerformanceTimer::~PerformanceTimer() {
quint64 elapsedusec = (usecTimestampNow() - _start); if (_isActive && _start != 0) {
QString& fullName = _fullNames[QThread::currentThread()]; quint64 elapsedusec = (usecTimestampNow() - _start);
PerformanceTimerRecord& namedRecord = _records[fullName]; QString& fullName = _fullNames[QThread::currentThread()];
namedRecord.accumulateResult(elapsedusec); PerformanceTimerRecord& namedRecord = _records[fullName];
fullName.resize(fullName.size() - (_name.size() + 1)); namedRecord.accumulateResult(elapsedusec);
fullName.resize(fullName.size() - (_name.size() + 1));
}
} }
// static // static
bool 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() { void PerformanceTimer::tallyAllTimerRecords() {
QMap<QString, PerformanceTimerRecord>::iterator recordsItr = _records.begin(); QMap<QString, PerformanceTimerRecord>::iterator recordsItr = _records.begin();
QMap<QString, PerformanceTimerRecord>::const_iterator recordsEnd = _records.end(); QMap<QString, PerformanceTimerRecord>::const_iterator recordsEnd = _records.end();

View file

@ -19,6 +19,7 @@
#include "SharedUtil.h" #include "SharedUtil.h"
#include "SimpleMovingAverage.h" #include "SimpleMovingAverage.h"
#include <atomic>
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <map> #include <map>
@ -76,14 +77,18 @@ public:
PerformanceTimer(const QString& name); PerformanceTimer(const QString& name);
~PerformanceTimer(); ~PerformanceTimer();
static bool isActive();
static void setActive(bool active);
static const PerformanceTimerRecord& getTimerRecord(const QString& name) { return _records[name]; }; static const PerformanceTimerRecord& getTimerRecord(const QString& name) { return _records[name]; };
static const QMap<QString, PerformanceTimerRecord>& getAllTimerRecords() { return _records; }; static const QMap<QString, PerformanceTimerRecord>& getAllTimerRecords() { return _records; };
static void tallyAllTimerRecords(); static void tallyAllTimerRecords();
static void dumpAllTimerRecords(); static void dumpAllTimerRecords();
private: private:
quint64 _start; quint64 _start = 0;
QString _name; QString _name;
static std::atomic<bool> _isActive;
static QHash<QThread*, QString> _fullNames; static QHash<QThread*, QString> _fullNames;
static QMap<QString, PerformanceTimerRecord> _records; static QMap<QString, PerformanceTimerRecord> _records;
}; };