llstat.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /**
  2. * @file llstat.cpp
  3. *
  4. * $LicenseInfo:firstyear=2001&license=viewergpl$
  5. *
  6. * Copyright (c) 2001-2009, Linden Research, Inc.
  7. *
  8. * Second Life Viewer Source Code
  9. * The source code in this file ("Source Code") is provided by Linden Lab
  10. * to you under the terms of the GNU General Public License, version 2.0
  11. * ("GPL"), unless you have obtained a separate licensing agreement
  12. * ("Other License"), formally executed by you and Linden Lab. Terms of
  13. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  14. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  15. *
  16. * There are special exceptions to the terms and conditions of the GPL as
  17. * it is applied to this Source Code. View the full text of the exception
  18. * in the file doc/FLOSS-exception.txt in this software distribution, or
  19. * online at
  20. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  21. *
  22. * By copying, modifying or distributing this software, you acknowledge
  23. * that you have read and understood your obligations described above,
  24. * and agree to abide by those obligations.
  25. *
  26. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  27. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  28. * COMPLETENESS OR PERFORMANCE.
  29. * $/LicenseInfo$
  30. */
  31. #include "linden_common.h"
  32. #include "llstat.h"
  33. #include "llframetimer.h"
  34. #include "llsdserialize.h"
  35. LLTimer LLStat::sTimer;
  36. LLFrameTimer LLStat::sFrameTimer;
  37. LLStat::LLStat(U32 num_bins, bool use_frame_timer)
  38. : mUseFrameTimer(use_frame_timer),
  39. mNumValues(0),
  40. mLastValue(0.f),
  41. mLastTime(0.f),
  42. mNumBins(num_bins),
  43. mCurBin(num_bins - 1),
  44. mNextBin(0)
  45. {
  46. llassert_always(mNumBins > 0);
  47. init();
  48. }
  49. LLStat::~LLStat()
  50. {
  51. delete[] mBins;
  52. delete[] mBeginTime;
  53. delete[] mTime;
  54. delete[] mDT;
  55. }
  56. void LLStat::init()
  57. {
  58. mBins = new F32[mNumBins];
  59. mBeginTime = new F64[mNumBins];
  60. mTime = new F64[mNumBins];
  61. mDT = new F32[mNumBins];
  62. for (U32 i = 0; i < mNumBins; ++i)
  63. {
  64. mBins[i] = 0.f;
  65. mBeginTime[i] = 0.0;
  66. mTime[i] = 0.0;
  67. mDT[i] = 0.f;
  68. }
  69. }
  70. void LLStat::reset()
  71. {
  72. mNumValues = 0;
  73. mLastValue = 0.f;
  74. mCurBin = mNumBins - 1;
  75. delete[] mBins;
  76. delete[] mBeginTime;
  77. delete[] mTime;
  78. delete[] mDT;
  79. init();
  80. }
  81. void LLStat::addValueTime(F64 time, F32 value)
  82. {
  83. if (mNumValues < mNumBins)
  84. {
  85. ++mNumValues;
  86. }
  87. // Increment the bin counters.
  88. if ((U32)(++mCurBin) == mNumBins)
  89. {
  90. mCurBin = 0;
  91. }
  92. if ((U32)(++mNextBin) == mNumBins)
  93. {
  94. mNextBin = 0;
  95. }
  96. mBins[mCurBin] = value;
  97. mTime[mCurBin] = time;
  98. mDT[mCurBin] = (F32)(mTime[mCurBin] - mBeginTime[mCurBin]);
  99. // This value is used to prime the min/max calls
  100. mLastTime = mTime[mCurBin];
  101. mLastValue = value;
  102. // Set the begin time for the next stat segment.
  103. mBeginTime[mNextBin] = mTime[mCurBin];
  104. mTime[mNextBin] = mTime[mCurBin];
  105. mDT[mNextBin] = 0.f;
  106. }
  107. void LLStat::start()
  108. {
  109. if (mUseFrameTimer)
  110. {
  111. mBeginTime[mNextBin] = sFrameTimer.getElapsedSeconds();
  112. }
  113. else
  114. {
  115. mBeginTime[mNextBin] = sTimer.getElapsedTimeF64();
  116. }
  117. }
  118. void LLStat::addValue(F32 value)
  119. {
  120. if (mNumValues < mNumBins)
  121. {
  122. ++mNumValues;
  123. }
  124. // Increment the bin counters.
  125. if ((U32)(++mCurBin) == mNumBins)
  126. {
  127. mCurBin = 0;
  128. }
  129. if ((U32)(++mNextBin) == mNumBins)
  130. {
  131. mNextBin = 0;
  132. }
  133. mBins[mCurBin] = value;
  134. if (mUseFrameTimer)
  135. {
  136. mTime[mCurBin] = sFrameTimer.getElapsedSeconds();
  137. }
  138. else
  139. {
  140. mTime[mCurBin] = sTimer.getElapsedTimeF64();
  141. }
  142. mDT[mCurBin] = (F32)(mTime[mCurBin] - mBeginTime[mCurBin]);
  143. // This value is used to prime the min/max calls
  144. mLastTime = mTime[mCurBin];
  145. mLastValue = value;
  146. // Set the begin time for the next stat segment.
  147. mBeginTime[mNextBin] = mTime[mCurBin];
  148. mTime[mNextBin] = mTime[mCurBin];
  149. mDT[mNextBin] = 0.f;
  150. }
  151. F32 LLStat::getMax() const
  152. {
  153. F32 current_max = mLastValue;
  154. for (U32 i = 0; i < mNumBins && i < mNumValues; ++i)
  155. {
  156. // Skip the bin we are currently filling.
  157. if (i != (U32)mNextBin && mBins[i] > current_max)
  158. {
  159. current_max = mBins[i];
  160. }
  161. }
  162. return current_max;
  163. }
  164. F32 LLStat::getMean() const
  165. {
  166. F32 current_mean = 0.f;
  167. U32 samples = 0;
  168. for (U32 i = 0; i < mNumBins && i < mNumValues; ++i)
  169. {
  170. // Skip the bin we are currently filling.
  171. if (i != (U32)mNextBin)
  172. {
  173. current_mean += mBins[i];
  174. ++samples;
  175. }
  176. }
  177. return samples != 0 ? current_mean / (F32)samples : 0.f;
  178. }
  179. F32 LLStat::getMin() const
  180. {
  181. F32 current_min = mLastValue;
  182. for (U32 i = 0; i < mNumBins && i < mNumValues; ++i)
  183. {
  184. // Skip the bin we are currently filling.
  185. if (i != (U32)mNextBin && mBins[i] < current_min)
  186. {
  187. current_min = mBins[i];
  188. }
  189. }
  190. return current_min;
  191. }
  192. F32 LLStat::getSum() const
  193. {
  194. F32 sum = 0.f;
  195. for (U32 i = 0; i < mNumBins && i < mNumValues; ++i)
  196. {
  197. // Skip the bin we are currently filling.
  198. if (i != (U32)mNextBin)
  199. {
  200. sum += mBins[i];
  201. }
  202. }
  203. return sum;
  204. }
  205. F32 LLStat::getSumDuration() const
  206. {
  207. F32 sum = 0.f;
  208. for (U32 i = 0; i < mNumBins && i < mNumValues; ++i)
  209. {
  210. // Skip the bin we are currently filling.
  211. if (i != (U32)mNextBin)
  212. {
  213. sum += mDT[i];
  214. }
  215. }
  216. return sum;
  217. }
  218. F32 LLStat::getPrev(S32 age) const
  219. {
  220. S32 bin = mCurBin - age;
  221. while (bin < 0)
  222. {
  223. bin += mNumBins;
  224. }
  225. // Bogus for bin we are currently working on, so return 0 in that case
  226. return bin == mNextBin ? 0.f : mBins[bin];
  227. }
  228. F32 LLStat::getPrevPerSec(S32 age) const
  229. {
  230. S32 bin = mCurBin - age;
  231. while (bin < 0)
  232. {
  233. bin += mNumBins;
  234. }
  235. // Bogus for bin we are currently working on, so return 0 in that case
  236. return bin == mNextBin || mDT[bin] == 0.f ? 0.f : mBins[bin] / mDT[bin];
  237. }
  238. F64 LLStat::getPrevBeginTime(S32 age) const
  239. {
  240. S32 bin = mCurBin - age;
  241. while (bin < 0)
  242. {
  243. bin += mNumBins;
  244. }
  245. // Bogus for bin we are currently working on, so return 0 in that case
  246. return bin == mNextBin ? 0.0 : mBeginTime[bin];
  247. }
  248. F64 LLStat::getPrevTime(S32 age) const
  249. {
  250. S32 bin = mCurBin - age;
  251. while (bin < 0)
  252. {
  253. bin += mNumBins;
  254. }
  255. // Bogus for bin we are currently working on, so return 0 in that case
  256. return bin == mNextBin ? 0.0 : mTime[bin];
  257. }
  258. F32 LLStat::getMeanPerSec() const
  259. {
  260. F32 value = 0.f;
  261. F32 dt = 0.f;
  262. for (U32 i = 0; i < mNumBins && i < mNumValues; ++i)
  263. {
  264. // Skip the bin we are currently filling.
  265. if (i != (U32)mNextBin)
  266. {
  267. value += mBins[i];
  268. dt += mDT[i];
  269. }
  270. }
  271. return dt > 0.f ? value / dt : 0.f;
  272. }
  273. F32 LLStat::getMeanDuration() const
  274. {
  275. F32 dur = 0.f;
  276. U32 count = 0;
  277. for (U32 i = 0; i < mNumBins && i < mNumValues; ++i)
  278. {
  279. if (i == (U32)mNextBin)
  280. {
  281. continue;
  282. }
  283. dur += mDT[i];
  284. ++count;
  285. }
  286. return count > 0 ? dur / (F32)count : 0.f;
  287. }
  288. F32 LLStat::getMaxPerSec() const
  289. {
  290. F32 value;
  291. if (mNextBin != 0 && mDT[0] != 0.f)
  292. {
  293. value = mBins[0] / mDT[0];
  294. }
  295. else if (mNumValues > 0 && mDT[1] != 0.f)
  296. {
  297. value = mBins[1] / mDT[1];
  298. }
  299. else
  300. {
  301. value = 0.f;
  302. }
  303. for (U32 i = 0; i < mNumBins && i < mNumValues; ++i)
  304. {
  305. F32 dt = mDT[i];
  306. // Skip the bin we are currently filling.
  307. if (i != (U32)mNextBin && dt > 0.f)
  308. {
  309. value = llmax(value, mBins[i] / dt);
  310. }
  311. }
  312. return value;
  313. }
  314. F32 LLStat::getMinPerSec() const
  315. {
  316. F32 value;
  317. if (mNextBin != 0 && mDT[0] != 0.f)
  318. {
  319. value = mBins[0] / mDT[0];
  320. }
  321. else if (mNumValues > 0 && mDT[1] != 0.f)
  322. {
  323. value = mBins[1] / mDT[1];
  324. }
  325. else
  326. {
  327. value = 0.f;
  328. }
  329. for (U32 i = 0; i < mNumBins && i < mNumValues; ++i)
  330. {
  331. F32 dt = mDT[i];
  332. // Skip the bin we are currently filling.
  333. if (i != (U32)mNextBin && dt > 0.f)
  334. {
  335. value = llmin(value, mBins[i] / dt);
  336. }
  337. }
  338. return value;
  339. }
  340. F32 LLStat::getMinDuration() const
  341. {
  342. F32 dur = 0.f;
  343. for (U32 i = 0; i < mNumBins && i < mNumValues; ++i)
  344. {
  345. dur = llmin(dur, mDT[i]);
  346. }
  347. return dur;
  348. }