llfasttimer.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @file llfasttimer.cpp
  3. * @brief Implementation of the fast timer.
  4. *
  5. * $LicenseInfo:firstyear=2004&license=viewergpl$
  6. *
  7. * Copyright (c) 2004-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llfasttimer.h"
  34. #if LL_FAST_TIMERS_ENABLED
  35. #include "llsys.h"
  36. // Statics member variables
  37. LLFastTimer::EFastTimerType LLFastTimer::sCurType = LLFastTimer::FTM_OTHER;
  38. LLFastTimer::EFastTimerType LLFastTimer::sType[FTM_MAX_DEPTH];
  39. S32 LLFastTimer::sCurDepth = 0;
  40. U64 LLFastTimer::sStart[FTM_MAX_DEPTH];
  41. U64 LLFastTimer::sCounter[LLFastTimer::FTM_NUM_TYPES];
  42. U64 LLFastTimer::sCountHistory[FTM_HISTORY_NUM][LLFastTimer::FTM_NUM_TYPES];
  43. U64 LLFastTimer::sCountAverage[LLFastTimer::FTM_NUM_TYPES];
  44. U64 LLFastTimer::sCalls[LLFastTimer::FTM_NUM_TYPES];
  45. U64 LLFastTimer::sCallHistory[FTM_HISTORY_NUM][LLFastTimer::FTM_NUM_TYPES];
  46. U64 LLFastTimer::sCallAverage[LLFastTimer::FTM_NUM_TYPES];
  47. S32 LLFastTimer::sCurFrameIndex = -1;
  48. S32 LLFastTimer::sLastFrameIndex = -1;
  49. bool LLFastTimer::sPauseHistory = false;
  50. bool LLFastTimer::sResetHistory = false;
  51. bool LLFastTimer::sFastTimersEnabled = true;
  52. U64 LLFastTimer::sClockResolution = 0;
  53. //static
  54. void LLFastTimer::reset()
  55. {
  56. // Good place to calculate the clock frequency
  57. if (sClockResolution == 0)
  58. {
  59. #if LL_FASTTIMER_USE_RDTSC
  60. // getCPUFrequency returns MHz and sClockResolution must be in Hz
  61. sClockResolution =
  62. U64(LLCPUInfo::getInstance()->getMHz() * 1000000.0);
  63. #else
  64. sClockResolution = SEC_TO_MICROSEC_U64;
  65. #endif
  66. }
  67. if (!sFastTimersEnabled)
  68. {
  69. return;
  70. }
  71. if (sCurDepth != 0)
  72. {
  73. llerrs << "Reset when sCurDepth != 0 (" << sCurDepth
  74. << ") with type number " << sCurType << llendl;
  75. }
  76. if (sPauseHistory)
  77. {
  78. sResetHistory = true;
  79. }
  80. else if (sResetHistory)
  81. {
  82. sCurFrameIndex = -1;
  83. sResetHistory = false;
  84. }
  85. else if (sCurFrameIndex >= 0)
  86. {
  87. S32 hidx = sCurFrameIndex % FTM_HISTORY_NUM;
  88. for (S32 i = 0; i < FTM_NUM_TYPES; ++i)
  89. {
  90. sCountHistory[hidx][i] = sCounter[i];
  91. sCountAverage[i] = (sCountAverage[i] * sCurFrameIndex +
  92. sCounter[i]) / (sCurFrameIndex + 1);
  93. sCallHistory[hidx][i] = sCalls[i];
  94. sCallAverage[i] = (sCallAverage[i] * sCurFrameIndex +
  95. sCalls[i]) / (sCurFrameIndex + 1);
  96. }
  97. sLastFrameIndex = sCurFrameIndex;
  98. }
  99. else
  100. {
  101. for (S32 i = 0; i < FTM_NUM_TYPES; ++i)
  102. {
  103. sCountAverage[i] = 0;
  104. sCallAverage[i] = 0;
  105. }
  106. }
  107. ++sCurFrameIndex;
  108. for (S32 i = 0; i < FTM_NUM_TYPES; ++i)
  109. {
  110. sCounter[i] = 0;
  111. sCalls[i] = 0;
  112. }
  113. sCurDepth = 0;
  114. }
  115. //static
  116. void LLFastTimer::enabledFastTimers(bool enable)
  117. {
  118. sFastTimersEnabled = enable;
  119. if (!enable)
  120. {
  121. sCurType = FTM_OTHER;
  122. }
  123. }
  124. #if LL_FAST_TIMERS_CHECK_MAX_DEPTH
  125. S32 LLFastTimer::sMaxDepth = 0;
  126. //static
  127. void LLFastTimer::checkMaxDepth()
  128. {
  129. if (sCurDepth > sMaxDepth)
  130. {
  131. sMaxDepth = sCurDepth;
  132. if (sMaxDepth > FTM_MAX_DEPTH)
  133. {
  134. llwarns << "Fast timers configured max depth is too small !"
  135. << llendl;
  136. }
  137. llinfos << "Fast timers new max depth = " << sMaxDepth << llendl;
  138. }
  139. }
  140. #endif
  141. #else // LL_FAST_TIMERS_ENABLED
  142. // To avoid the LNK4221 warning under Windows while compiling...
  143. # if LL_WINDOWS && !LL_CLANG
  144. namespace
  145. {
  146. void* dummy;
  147. }
  148. # endif
  149. #endif // LL_FAST_TIMERS_ENABLED