00001 00032 #ifndef LL_LLSTAT_H 00033 #define LL_LLSTAT_H 00034 00035 #include <deque> 00036 00037 #include "lltimer.h" 00038 #include "llframetimer.h" 00039 00040 // 00041 // Accumulates statistics for an arbitrary length of time. 00042 // Does this by maintaining a chain of accumulators, each one 00043 // accumulation the results of the parent. Can scale to arbitrary 00044 // amounts of time with very low memory cost. 00045 // 00046 00047 class LLStatAccum 00048 { 00049 protected: 00050 LLStatAccum(bool use_frame_timer); 00051 virtual ~LLStatAccum(); 00052 00053 public: 00054 enum TimeScale { 00055 SCALE_SECOND, 00056 SCALE_MINUTE, 00057 SCALE_TWO_MINUTE, 00058 SCALE_HOUR, 00059 SCALE_DAY, 00060 SCALE_WEEK, 00061 00062 NUM_SCALES 00063 }; 00064 00065 F32 meanValue(TimeScale scale) const; 00066 // see the subclasses for the specific meaning of value 00067 00068 F32 meanValueOverLastSecond() const { return meanValue(SCALE_SECOND); } 00069 F32 meanValueOverLastMinute() const { return meanValue(SCALE_MINUTE); } 00070 00071 protected: 00072 class impl; 00073 impl& m; 00074 }; 00075 00076 class LLStatMeasure : public LLStatAccum 00077 // gathers statistics about things that are measured 00078 // ex.: tempature, time dilation 00079 { 00080 public: 00081 LLStatMeasure(bool use_frame_timer = true); 00082 00083 void sample(F64); 00084 void sample(S32 v) { sample((F64)v); } 00085 void sample(U32 v) { sample((F64)v); } 00086 void sample(S64 v) { sample((F64)v); } 00087 void sample(U64 v) { sample((F64)v); } 00088 }; 00089 00090 00091 class LLStatRate : public LLStatAccum 00092 // gathers statistics about things that can be counted over time 00093 // ex.: LSL instructions executed, messages sent, simulator frames completed 00094 // renders it in terms of rate of thing per second 00095 { 00096 public: 00097 LLStatRate(bool use_frame_timer = true); 00098 00099 void count(U32); 00100 // used to note that n items have occured 00101 00102 void mark() { count(1); } 00103 // used for counting the rate thorugh a point in the code 00104 }; 00105 00106 00107 class LLTimeBlock; 00108 00109 class LLStatTime : public LLStatAccum 00110 // gathers statistics about time spent in a block of code 00111 // measure average duration per second in the block 00112 { 00113 public: 00114 LLStatTime(bool use_frame_timer = false); 00115 00116 private: 00117 void start(); 00118 void stop(); 00119 friend class LLTimeBlock; 00120 }; 00121 00122 class LLTimeBlock 00123 { 00124 public: 00125 LLTimeBlock(LLStatTime& stat) : mStat(stat) { mStat.start(); } 00126 ~LLTimeBlock() { mStat.stop(); } 00127 private: 00128 LLStatTime& mStat; 00129 }; 00130 00131 00132 00133 00134 00135 class LLStat 00136 { 00137 public: 00138 LLStat(const U32 num_bins = 32, BOOL use_frame_timer = FALSE); 00139 ~LLStat(); 00140 00141 void reset(); 00142 00143 void start(); // Start the timer for the current "frame", otherwise uses the time tracked from 00144 // the last addValue 00145 void addValue(const F32 value = 1.f); // Adds the current value being tracked, and tracks the DT. 00146 void addValue(const S32 value) { addValue((F32)value); } 00147 void addValue(const U32 value) { addValue((F32)value); } 00148 00149 void setBeginTime(const F64 time); 00150 void addValueTime(const F64 time, const F32 value = 1.f); 00151 00152 S32 getCurBin() const; 00153 S32 getNextBin() const; 00154 00155 F32 getCurrent() const; 00156 F32 getCurrentPerSec() const; 00157 F64 getCurrentBeginTime() const; 00158 F64 getCurrentTime() const; 00159 F32 getCurrentDuration() const; 00160 00161 F32 getPrev(S32 age) const; // Age is how many "addValues" previously - zero is current 00162 F32 getPrevPerSec(S32 age) const; // Age is how many "addValues" previously - zero is current 00163 F64 getPrevBeginTime(S32 age) const; 00164 F64 getPrevTime(S32 age) const; 00165 00166 F32 getBin(S32 bin) const; 00167 F32 getBinPerSec(S32 bin) const; 00168 F64 getBinBeginTime(S32 bin) const; 00169 F64 getBinTime(S32 bin) const; 00170 00171 F32 getMax() const; 00172 F32 getMaxPerSec() const; 00173 00174 F32 getMean() const; 00175 F32 getMeanPerSec() const; 00176 F32 getMeanDuration() const; 00177 00178 F32 getMin() const; 00179 F32 getMinPerSec() const; 00180 F32 getMinDuration() const; 00181 00182 F32 getSum() const; 00183 F32 getSumDuration() const; 00184 00185 U32 getNumValues() const; 00186 S32 getNumBins() const; 00187 00188 F64 getLastTime() const; 00189 private: 00190 BOOL mUseFrameTimer; 00191 U32 mNumValues; 00192 U32 mNumBins; 00193 F32 mLastValue; 00194 F64 mLastTime; 00195 F32 *mBins; 00196 F64 *mBeginTime; 00197 F64 *mTime; 00198 F32 *mDT; 00199 S32 mCurBin; 00200 S32 mNextBin; 00201 static LLTimer sTimer; 00202 static LLFrameTimer sFrameTimer; 00203 }; 00204 00205 #endif // LL_STAT_