/** * @file lltracerecording.h * @brief Sampling object for collecting runtime statistics originating from lltrace. * * $LicenseInfo:firstyear=2001&license=viewerlgpl$ * Second Life Viewer Source Code * Copyright (C) 2012, Linden Research, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License only. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA * $/LicenseInfo$ */ #ifndef LL_LLTRACERECORDING_H #define LL_LLTRACERECORDING_H #include "stdtypes.h" #include "llpreprocessor.h" #include "llpointer.h" #include "lltimer.h" #include "lltraceaccumulators.h" class LLStopWatchControlsMixinCommon { public: virtual ~LLStopWatchControlsMixinCommon() {} enum EPlayState { STOPPED, PAUSED, STARTED }; void start(); void stop(); void pause(); void resume(); void restart(); void reset(); bool isStarted() const { return mPlayState == STARTED; } bool isPaused() const { return mPlayState == PAUSED; } bool isStopped() const { return mPlayState == STOPPED; } EPlayState getPlayState() const { return mPlayState; } // force play state to specific value by calling appropriate handle* methods void setPlayState(EPlayState state); protected: LLStopWatchControlsMixinCommon() : mPlayState(STOPPED) {} private: // trigger active behavior (without reset) virtual void handleStart() = 0; // stop active behavior virtual void handleStop() = 0; // clear accumulated state, can be called while started virtual void handleReset() = 0; EPlayState mPlayState; }; template class LLStopWatchControlsMixin : public LLStopWatchControlsMixinCommon { public: typedef LLStopWatchControlsMixin self_t; virtual void splitTo(DERIVED& other) { EPlayState play_state = getPlayState(); stop(); other.reset(); handleSplitTo(other); other.setPlayState(play_state); } virtual void splitFrom(DERIVED& other) { static_cast(other).handleSplitTo(*static_cast(this)); } private: self_t& operator = (const self_t& other) { // don't do anything, derived class must implement logic } // atomically stop this object while starting the other // no data can be missed in between stop and start virtual void handleSplitTo(DERIVED& other) {}; }; namespace LLTrace { template class TraceType; template class CountStatHandle; template class SampleStatHandle; template class EventStatHandle; template struct RelatedTypes { typedef F64 fractional_t; typedef T sum_t; }; template struct RelatedTypes > { typedef LLUnit::fractional_t, UNIT_T> fractional_t; typedef LLUnit::sum_t, UNIT_T> sum_t; }; template<> struct RelatedTypes { typedef F64 fractional_t; typedef U32 sum_t; }; class Recording : public LLStopWatchControlsMixin { public: Recording(EPlayState state = LLStopWatchControlsMixinCommon::STOPPED); Recording(const Recording& other); ~Recording(); Recording& operator = (const Recording& other); // accumulate data from subsequent, non-overlapping recording void appendRecording(Recording& other); // grab latest recorded data void update(); // ensure that buffers are exclusively owned by this recording void makeUnique() { mBuffers.makeUnique(); } // Timer accessors LLUnit getSum(const TraceType& stat); LLUnit getSum(const TraceType& stat); U32 getSum(const TraceType& stat); LLUnit getPerSec(const TraceType& stat); LLUnit getPerSec(const TraceType& stat); F32 getPerSec(const TraceType& stat); // Memory accessors LLUnit getMin(const TraceType& stat); LLUnit getMean(const TraceType& stat); LLUnit getMax(const TraceType& stat); LLUnit getStandardDeviation(const TraceType& stat); LLUnit getLastValue(const TraceType& stat); LLUnit getMin(const TraceType& stat); LLUnit getMean(const TraceType& stat); LLUnit getMax(const TraceType& stat); LLUnit getStandardDeviation(const TraceType& stat); LLUnit getLastValue(const TraceType& stat); U32 getSum(const TraceType& stat); U32 getSum(const TraceType& stat); // CountStatHandle accessors F64 getSum(const TraceType& stat); template typename RelatedTypes::sum_t getSum(const CountStatHandle& stat) { return (typename RelatedTypes::sum_t)getSum(static_cast&> (stat)); } F64 getPerSec(const TraceType& stat); template typename RelatedTypes::fractional_t getPerSec(const CountStatHandle& stat) { return (typename RelatedTypes::fractional_t)getPerSec(static_cast&> (stat)); } U32 getSampleCount(const TraceType& stat); // SampleStatHandle accessors F64 getMin(const TraceType& stat); template T getMin(const SampleStatHandle& stat) { return (T)getMin(static_cast&> (stat)); } F64 getMean(const TraceType& stat); template typename RelatedTypes::fractional_t getMean(SampleStatHandle& stat) { return (typename RelatedTypes::fractional_t)getMean(static_cast&> (stat)); } F64 getMax(const TraceType& stat); template T getMax(const SampleStatHandle& stat) { return (T)getMax(static_cast&> (stat)); } F64 getStandardDeviation(const TraceType& stat); template typename RelatedTypes::fractional_t getStandardDeviation(const SampleStatHandle& stat) { return (typename RelatedTypes::fractional_t)getStandardDeviation(static_cast&> (stat)); } F64 getLastValue(const TraceType& stat); template T getLastValue(const SampleStatHandle& stat) { return (T)getLastValue(static_cast&> (stat)); } U32 getSampleCount(const TraceType& stat); // EventStatHandle accessors F64 getSum(const TraceType& stat); template typename RelatedTypes::sum_t getSum(const EventStatHandle& stat) { return (typename RelatedTypes::sum_t)getSum(static_cast&> (stat)); } F64 getMin(const TraceType& stat); template T getMin(const EventStatHandle& stat) { return (T)getMin(static_cast&> (stat)); } F64 getMax(const TraceType& stat); template T getMax(const EventStatHandle& stat) { return (T)getMax(static_cast&> (stat)); } F64 getMean(const TraceType& stat); template typename RelatedTypes::fractional_t getMean(EventStatHandle& stat) { return (typename RelatedTypes::fractional_t)getMean(static_cast&> (stat)); } F64 getStandardDeviation(const TraceType& stat); template typename RelatedTypes::fractional_t getStandardDeviation(const EventStatHandle& stat) { return (typename RelatedTypes::fractional_t)getStandardDeviation(static_cast&> (stat)); } F64 getLastValue(const TraceType& stat); template T getLastValue(const EventStatHandle& stat) { return (T)getLastValue(static_cast&> (stat)); } U32 getSampleCount(const TraceType& stat); LLUnit getDuration() const { return mElapsedSeconds; } protected: friend class ThreadRecorder; // implementation for LLStopWatchControlsMixin /*virtual*/ void handleStart(); /*virtual*/ void handleStop(); /*virtual*/ void handleReset(); /*virtual*/ void handleSplitTo(Recording& other); // returns data for current thread class ThreadRecorder* getThreadRecorder(); LLTimer mSamplingTimer; LLUnit mElapsedSeconds; LLCopyOnWritePointer mBuffers; bool mInHandOff; }; class LL_COMMON_API PeriodicRecording : public LLStopWatchControlsMixin { public: PeriodicRecording(U32 num_periods, EPlayState state = STOPPED); void nextPeriod(); size_t getNumRecordedPeriods() { return mNumPeriods; } LLUnit getDuration() const; void appendPeriodicRecording(PeriodicRecording& other); void appendRecording(Recording& recording); Recording& getLastRecording(); const Recording& getLastRecording() const; Recording& getCurRecording(); const Recording& getCurRecording() const; Recording& getPrevRecording(U32 offset); const Recording& getPrevRecording(U32 offset) const; Recording snapshotCurRecording() const; // // PERIODIC MIN // // catch all for stats that have a defined sum template typename T::value_t getPeriodMin(const TraceType& stat, size_t num_periods = U32_MAX) { size_t total_periods = mNumPeriods; num_periods = llmin(num_periods, isStarted() ? total_periods - 1 : total_periods); typename T::value_t min_val = std::numeric_limits::max(); for (S32 i = 1; i <= num_periods; i++) { S32 index = (mCurPeriod + total_periods - i) % total_periods; min_val = llmin(min_val, mRecordingPeriods[index].getSum(stat)); } return min_val; } template T getPeriodMin(const CountStatHandle& stat, size_t num_periods = U32_MAX) { return T(getPeriodMin(static_cast&>(stat), num_periods)); } F64 getPeriodMin(const TraceType& stat, size_t num_periods = U32_MAX); template T getPeriodMin(const SampleStatHandle& stat, size_t num_periods = U32_MAX) { return T(getPeriodMin(static_cast&>(stat), num_periods)); } F64 getPeriodMin(const TraceType& stat, size_t num_periods = U32_MAX); template T getPeriodMin(const EventStatHandle& stat, size_t num_periods = U32_MAX) { return T(getPeriodMin(static_cast&>(stat), num_periods)); } template typename RelatedTypes::fractional_t getPeriodMinPerSec(const TraceType& stat, size_t num_periods = U32_MAX) { size_t total_periods = mNumPeriods; num_periods = llmin(num_periods, isStarted() ? total_periods - 1 : total_periods); F64 min_val = std::numeric_limits::max(); for (S32 i = 1; i <= num_periods; i++) { S32 index = (mCurPeriod + total_periods - i) % total_periods; min_val = llmin(min_val, mRecordingPeriods[index].getPerSec(stat)); } return (typename RelatedTypes::fractional_t) min_val; } template typename RelatedTypes::fractional_t getPeriodMinPerSec(const CountStatHandle& stat, size_t num_periods = U32_MAX) { return typename RelatedTypes::fractional_t(getPeriodMinPerSec(static_cast&>(stat), num_periods)); } // // PERIODIC MAX // // catch all for stats that have a defined sum template typename T::value_t getPeriodMax(const TraceType& stat, size_t num_periods = U32_MAX) { size_t total_periods = mNumPeriods; num_periods = llmin(num_periods, isStarted() ? total_periods - 1 : total_periods); typename T::value_t max_val = std::numeric_limits::min(); for (S32 i = 1; i <= num_periods; i++) { S32 index = (mCurPeriod + total_periods - i) % total_periods; max_val = llmax(max_val, mRecordingPeriods[index].getSum(stat)); } return max_val; } template T getPeriodMax(const CountStatHandle& stat, size_t num_periods = U32_MAX) { return T(getPeriodMax(static_cast&>(stat), num_periods)); } F64 getPeriodMax(const TraceType& stat, size_t num_periods = U32_MAX); template T getPeriodMax(const SampleStatHandle& stat, size_t num_periods = U32_MAX) { return T(getPeriodMax(static_cast&>(stat), num_periods)); } F64 getPeriodMax(const TraceType& stat, size_t num_periods = U32_MAX); template T getPeriodMax(const EventStatHandle& stat, size_t num_periods = U32_MAX) { return T(getPeriodMax(static_cast&>(stat), num_periods)); } template typename RelatedTypes::fractional_t getPeriodMaxPerSec(const TraceType& stat, size_t num_periods = U32_MAX) { size_t total_periods = mNumPeriods; num_periods = llmin(num_periods, isStarted() ? total_periods - 1 : total_periods); F64 max_val = std::numeric_limits::min(); for (S32 i = 1; i <= num_periods; i++) { S32 index = (mCurPeriod + total_periods - i) % total_periods; max_val = llmax(max_val, mRecordingPeriods[index].getPerSec(stat)); } return (typename RelatedTypes::fractional_t)max_val; } template typename RelatedTypes::fractional_t getPeriodMaxPerSec(const CountStatHandle& stat, size_t num_periods = U32_MAX) { return typename RelatedTypes::fractional_t(getPeriodMaxPerSec(static_cast&>(stat), num_periods)); } // // PERIODIC MEAN // // catch all for stats that have a defined sum template typename T::mean_t getPeriodMean(const TraceType& stat, size_t num_periods = U32_MAX) { size_t total_periods = mNumPeriods; num_periods = llmin(num_periods, isStarted() ? total_periods - 1 : total_periods); typename T::mean_t mean = 0; if (num_periods <= 0) { return mean; } for (S32 i = 1; i <= num_periods; i++) { S32 index = (mCurPeriod + total_periods - i) % total_periods; if (mRecordingPeriods[index].getDuration() > 0.f) { mean += mRecordingPeriods[index].getSum(stat); } } mean = mean / num_periods; return mean; } template typename RelatedTypes::fractional_t getPeriodMean(const CountStatHandle& stat, size_t num_periods = U32_MAX) { return typename RelatedTypes::fractional_t(getPeriodMean(static_cast&>(stat), num_periods)); } F64 getPeriodMean(const TraceType& stat, size_t num_periods = U32_MAX); template typename RelatedTypes::fractional_t getPeriodMean(const SampleStatHandle& stat, size_t num_periods = U32_MAX) { return typename RelatedTypes::fractional_t(getPeriodMean(static_cast&>(stat), num_periods)); } F64 getPeriodMean(const TraceType& stat, size_t num_periods = U32_MAX); template typename RelatedTypes::fractional_t getPeriodMean(const EventStatHandle& stat, size_t num_periods = U32_MAX) { return typename RelatedTypes::fractional_t(getPeriodMean(static_cast&>(stat), num_periods)); } template typename RelatedTypes::fractional_t getPeriodMeanPerSec(const TraceType& stat, size_t num_periods = U32_MAX) { size_t total_periods = mNumPeriods; num_periods = llmin(num_periods, isStarted() ? total_periods - 1 : total_periods); typename T::mean_t mean = 0; if (num_periods <= 0) { return mean; } for (S32 i = 1; i <= num_periods; i++) { S32 index = (mCurPeriod + total_periods - i) % total_periods; if (mRecordingPeriods[index].getDuration() > 0.f) { mean += mRecordingPeriods[index].getPerSec(stat); } } mean = mean / num_periods; return (typename RelatedTypes::fractional_t)mean; } template typename RelatedTypes::fractional_t getPeriodMeanPerSec(const CountStatHandle& stat, size_t num_periods = U32_MAX) { return typename RelatedTypes::fractional_t(getPeriodMeanPerSec(static_cast&>(stat), num_periods)); } private: // implementation for LLStopWatchControlsMixin /*virtual*/ void handleStart(); /*virtual*/ void handleStop(); /*virtual*/ void handleReset(); /*virtual*/ void handleSplitTo(PeriodicRecording& other); private: std::vector mRecordingPeriods; const bool mAutoResize; size_t mCurPeriod; size_t mNumPeriods; }; PeriodicRecording& get_frame_recording(); class ExtendableRecording : public LLStopWatchControlsMixin { public: void extend(); Recording& getAcceptedRecording() { return mAcceptedRecording; } const Recording& getAcceptedRecording() const {return mAcceptedRecording;} Recording& getPotentialRecording() { return mPotentialRecording; } const Recording& getPotentialRecording() const { return mPotentialRecording;} private: // implementation for LLStopWatchControlsMixin /*virtual*/ void handleStart(); /*virtual*/ void handleStop(); /*virtual*/ void handleReset(); /*virtual*/ void handleSplitTo(ExtendableRecording& other); private: Recording mAcceptedRecording; Recording mPotentialRecording; }; class ExtendablePeriodicRecording : public LLStopWatchControlsMixin { public: ExtendablePeriodicRecording(); void extend(); PeriodicRecording& getResults() { return mAcceptedRecording; } const PeriodicRecording& getResults() const {return mAcceptedRecording;} void nextPeriod() { mPotentialRecording.nextPeriod(); } private: // implementation for LLStopWatchControlsMixin /*virtual*/ void handleStart(); /*virtual*/ void handleStop(); /*virtual*/ void handleReset(); /*virtual*/ void handleSplitTo(ExtendablePeriodicRecording& other); private: PeriodicRecording mAcceptedRecording; PeriodicRecording mPotentialRecording; }; } #endif // LL_LLTRACERECORDING_H