Profiling.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #ifndef SE_INCL_PROFILING_H
  13. #define SE_INCL_PROFILING_H
  14. #ifdef PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. #if ENGINE_INTERNAL
  18. #include <Engine/Base/CTString.h>
  19. #include <Engine/Base/Timer.h>
  20. #include <Engine/Base/Timer.inl>
  21. #include <Engine/Templates/StaticArray.h>
  22. /*
  23. * Profiling counter.
  24. */
  25. class CProfileCounter {
  26. friend class CProfileForm;
  27. private:
  28. CTString pc_strName; // name of this counter
  29. INDEX pc_ctCount; // the counter itself
  30. /* Print one counter in report. */
  31. void Report(char *&strBuffer, INDEX ctAveragingCount);
  32. };
  33. /*
  34. * Profiling timer.
  35. */
  36. class CProfileTimer {
  37. friend class CProfileForm;
  38. private:
  39. CTString pt_strName; // name of this timer
  40. CTimerValue pt_tvStarted; // time when the timer was started last time
  41. CTimerValue pt_tvElapsed; // total elapsed time of the timer
  42. CTString pt_strAveragingName; // name of averaging counter
  43. INDEX pt_ctAveraging; // averaging counter for this timer
  44. /* Print one timer in report. */
  45. void Report(char *&strBuffer, INDEX ctAveragingCount,
  46. CTimerValue tvAppElapsed, CTimerValue tvModElapsed);
  47. };
  48. // this file just defines TIMER_PROFILING as 1 or 0
  49. #include <Engine/Base/ProfilingEnabled.h>
  50. #endif ENGINE_INTERNAL
  51. /*
  52. * Class for gathering and reporting profiling information.
  53. */
  54. class CProfileForm {
  55. public:
  56. #if ENGINE_INTERNAL
  57. // implementation:
  58. CTString pf_strTitle; // main title of the profiling form
  59. CTString pf_strAveragingUnits; // name for averaging units
  60. CStaticArray<CProfileCounter> pf_apcCounters; // profiling counters
  61. CStaticArray<CProfileTimer> pf_aptTimers; // profiling timers
  62. CTimerValue pf_tvOverAllStarted; // timer when overall timer was started last time
  63. CTimerValue pf_tvOverAllElapsed; // total elapsed time of the overall timer
  64. INDEX pf_ctRunningTimers; // counter of currently running timers
  65. INDEX pf_ctAveragingCounter; // counter for calculating average results
  66. // override to provide external averaging
  67. virtual INDEX GetAveragingCounter(void);
  68. CTimerValue pf_tvLastReset; // time when profile form was last reset
  69. /* Start a timer. */
  70. void StartTimer_internal(INDEX iTimer);
  71. /* Stop a timer. */
  72. void StopTimer_internal(INDEX iTimer);
  73. // interface:
  74. /* Constructor for profile form with given number of counters and timers.
  75. * NOTE: Reset() must be called on a profile form before using it!
  76. */
  77. CProfileForm(const CTString &strTitle, const CTString &strAveragingUnits,
  78. INDEX ctCounters, INDEX ctTimers);
  79. void Clear(void);
  80. // set/test profiling activation flag
  81. static void SetProfilingActive(BOOL bActive);
  82. static BOOL GetProfilingActive(void);
  83. // Measure profiling errors and set epsilon corrections.
  84. static void CalibrateProfilingTimers(void);
  85. /* Increment averaging counter by given count. */
  86. inline void IncrementAveragingCounter(INDEX ctAdd=1) {
  87. pf_ctAveragingCounter += ctAdd;
  88. };
  89. /* Increment counter by given count. */
  90. inline void IncrementCounter(INDEX iCounter, INDEX ctAdd=1) {
  91. pf_apcCounters[iCounter].pc_ctCount += ctAdd;
  92. };
  93. /* Get current value of a counter. */
  94. INDEX GetCounterCount(INDEX iCounter);
  95. #if TIMER_PROFILING
  96. /* Start a timer. */
  97. inline void StartTimer(INDEX iTimer) {
  98. StartTimer_internal(iTimer);
  99. };
  100. /* Stop a timer. */
  101. inline void StopTimer(INDEX iTimer) {
  102. StopTimer_internal(iTimer);
  103. };
  104. /* Increment averaging counter for a timer by given count. */
  105. inline void IncrementTimerAveragingCounter(INDEX iTimer, INDEX ctAdd=1) {
  106. pf_aptTimers[iTimer].pt_ctAveraging += ctAdd;
  107. };
  108. /* Set name of a counter. */
  109. void SetCounterName_internal(INDEX iCounter, const CTString &strName)
  110. {
  111. pf_apcCounters[iCounter].pc_strName = strName;
  112. }
  113. /* Set name of a timer. */
  114. void SetTimerName_internal(INDEX iTimer, const CTString &strName, const CTString &strAveragingName)
  115. {
  116. pf_aptTimers[iTimer].pt_strName = strName;
  117. pf_aptTimers[iTimer].pt_strAveragingName = strAveragingName;
  118. }
  119. #define SETCOUNTERNAME(a,b) SetCounterName_internal(a,b)
  120. #define SETTIMERNAME(a,b,c) SetTimerName_internal(a,b,c)
  121. #else //TIMER_PROFILING
  122. inline void StartTimer(INDEX iTimer) {};
  123. inline void StopTimer(INDEX iTimer) {};
  124. inline void IncrementTimerAveragingCounter(INDEX iTimer, INDEX ctAdd=1) {};
  125. inline void SetCounterName_internal(INDEX iCounter, const CTString &strName) {};
  126. inline void SetTimerName_internal(INDEX iTimer, const CTString &strName, const CTString &strAveragingName) {};
  127. #define SETCOUNTERNAME(a,b) SetCounterName_internal(a,"")
  128. #define SETTIMERNAME(a,b,c) SetTimerName_internal(a,"","")
  129. #endif
  130. /* Get current value of a timer in seconds or in percentage of module time. */
  131. double GetTimerPercentageOfModule(INDEX iTimer);
  132. double GetTimerAverageTime(INDEX iTimer);
  133. /* Get name of a counter. */
  134. const CTString &GetCounterName(INDEX iCounter);
  135. /* Get name of a timer. */
  136. const CTString &GetTimerName(INDEX iTimer);
  137. /* Get percentage of module time in application time. */
  138. double GetModulePercentage(void);
  139. #endif // ENGINE_INTERNAL
  140. /* Reset all profiling values. */
  141. ENGINE_API void Reset(void);
  142. /* Report profiling results. */
  143. ENGINE_API void Report(CTString &strReport);
  144. };
  145. // profile form for profiling gfx
  146. ENGINE_API extern CProfileForm &_pfGfxProfile;
  147. // profile form for profiling model rendering
  148. ENGINE_API extern CProfileForm &_pfModelProfile;
  149. // profile form for profiling sound
  150. ENGINE_API extern CProfileForm &_pfSoundProfile;
  151. // profile form for profiling network
  152. ENGINE_API extern CProfileForm &_pfNetworkProfile;
  153. // profile form for profiling rendering
  154. ENGINE_API extern CProfileForm &_pfRenderProfile;
  155. // profile form for profiling world editing
  156. ENGINE_API extern CProfileForm &_pfWorldEditingProfile;
  157. // profile form for profiling phisics
  158. ENGINE_API extern CProfileForm &_pfPhysicsProfile;
  159. #endif /* include-once check. */