From 4cc641320bbe6630104e119b8346646c42682956 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Fri, 2 Jan 2015 15:04:48 -0800 Subject: [PATCH] fix PerformanceTimer to handle timer stacks on multiple threads --- libraries/shared/src/PerfStat.cpp | 20 ++++++++++++++++---- libraries/shared/src/PerfStat.h | 11 ++--------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/libraries/shared/src/PerfStat.cpp b/libraries/shared/src/PerfStat.cpp index b811a719bc..f5f6964b6d 100644 --- a/libraries/shared/src/PerfStat.cpp +++ b/libraries/shared/src/PerfStat.cpp @@ -13,7 +13,8 @@ #include #include -#include +#include +#include #include "PerfStat.h" @@ -77,15 +78,26 @@ void PerformanceTimerRecord::tallyResult(const quint64& now) { // PerformanceTimer // ---------------------------------------------------------------------------- -QString PerformanceTimer::_fullName; +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() { quint64 elapsedusec = (usecTimestampNow() - _start); - PerformanceTimerRecord& namedRecord = _records[_fullName]; + QString& fullName = _fullNames[QThread::currentThread()]; + PerformanceTimerRecord& namedRecord = _records[fullName]; namedRecord.accumulateResult(elapsedusec); - _fullName.resize(_fullName.size() - (_name.size() + 1)); + fullName.resize(fullName.size() - (_name.size() + 1)); } // static diff --git a/libraries/shared/src/PerfStat.h b/libraries/shared/src/PerfStat.h index 4f94be73b1..81731abfa9 100644 --- a/libraries/shared/src/PerfStat.h +++ b/libraries/shared/src/PerfStat.h @@ -73,14 +73,7 @@ private: class PerformanceTimer { public: - PerformanceTimer(const QString& name) : - _start(0), - _name(name) { - _fullName.append("/"); - _fullName.append(_name); - _start = usecTimestampNow(); - } - + PerformanceTimer(const QString& name); ~PerformanceTimer(); static const PerformanceTimerRecord& getTimerRecord(const QString& name) { return _records[name]; }; @@ -91,7 +84,7 @@ public: private: quint64 _start; QString _name; - static QString _fullName; + static QHash _fullNames; static QMap _records; };