PerfTimer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // PerfTimer.h ////////////////////////////////////////////////////////////////////////////////////
  24. // John McDonald
  25. // July 2002
  26. #pragma once
  27. #ifndef __PERFTIMER_H__
  28. #define __PERFTIMER_H__
  29. #if defined(_DEBUG) || defined(_INTERNAL)
  30. /*
  31. NOTE NOTE NOTE: never check this in with this enabled, since there is a nonzero time penalty
  32. for running in this mode. Only enable it for local builds for testing purposes! (srj)
  33. */
  34. #define NO_PERF_TIMERS
  35. #else
  36. #define NO_PERF_TIMERS
  37. #endif
  38. #include "Common/GameCommon.h" // ensure we get DUMP_PERF_STATS, or not
  39. #ifdef PERF_TIMERS
  40. #include "GameLogic/GameLogic.h"
  41. #include "Common/PerfMetrics.h"
  42. #include "Common/GlobalData.h"
  43. #endif
  44. // Forward Declarations
  45. class DebugDisplayInterface;
  46. //-------------------------------------------------------------------------------------------------
  47. //-------------------------------------------------------------------------------------------------
  48. //-------------------------------------------------------------------------------------------------
  49. #define NO_USE_QPF // non-QPF is much faster.
  50. #if defined(PERF_TIMERS) || defined(DUMP_PERF_STATS)
  51. //-------------------------------------------------------------------------------------------------
  52. void InitPrecisionTimer();
  53. //-------------------------------------------------------------------------------------------------
  54. void GetPrecisionTimerTicksPerSec(Int64* t);
  55. //-------------------------------------------------------------------------------------------------
  56. __forceinline void GetPrecisionTimer(Int64* t)
  57. {
  58. #ifdef USE_QPF
  59. QueryPerformanceCounter((LARGE_INTEGER*)t);
  60. #else
  61. // CPUID is needed to force serialization of any previous instructions.
  62. __asm
  63. {
  64. // for now, I am commenting this out. It throws the timings off a bit more (up to .001%) jkmcd
  65. // CPUID
  66. RDTSC
  67. MOV ECX,[t]
  68. MOV [ECX], EAX
  69. MOV [ECX+4], EDX
  70. }
  71. #endif
  72. }
  73. #endif
  74. //-------------------------------------------------------------------------------------------------
  75. //-------------------------------------------------------------------------------------------------
  76. //-------------------------------------------------------------------------------------------------
  77. #ifdef PERF_TIMERS
  78. //-------------------------------------------------------------------------------------------------
  79. class PerfGather
  80. {
  81. public:
  82. // If net only (default), subtract perf timers running inside. [8/12/2003]
  83. PerfGather( const char *identifier, Bool netOnly=true );
  84. virtual ~PerfGather( );
  85. __forceinline void startTimer();
  86. __forceinline void stopTimer();
  87. enum
  88. {
  89. PERF_GROSSTIME = 0x01,
  90. PERF_NETTIME = 0x02,
  91. PERF_CALLCOUNT = 0x04
  92. };
  93. static void resetAll();
  94. static void initPerfDump(const char* fname, Int options);
  95. static void termPerfDump();
  96. static void dumpAll(UnsignedInt frame);
  97. static void displayGraph(UnsignedInt frame);
  98. void reset();
  99. private:
  100. enum { MAX_ACTIVE_STACK = 256 };
  101. static PerfGather* m_active[MAX_ACTIVE_STACK];
  102. static PerfGather** m_activeHead;
  103. static Int64 s_stopStartOverhead; // overhead for stop+start a timer
  104. static PerfGather*& getHeadPtr();
  105. void addToList();
  106. void removeFromList();
  107. const char* m_identifier;
  108. Int64 m_startTime;
  109. Int64 m_runningTimeGross;
  110. Int64 m_runningTimeNet;
  111. Int m_callCount;
  112. PerfGather* m_next;
  113. PerfGather* m_prev;
  114. Bool m_ignore;
  115. Bool m_netTimeOnly;
  116. };
  117. //-------------------------------------------------------------------------------------------------
  118. void PerfGather::startTimer()
  119. {
  120. *++m_activeHead = this;
  121. GetPrecisionTimer(&m_startTime);
  122. }
  123. //-------------------------------------------------------------------------------------------------
  124. void PerfGather::stopTimer()
  125. {
  126. DEBUG_ASSERTCRASH(this != NULL, ("I am null, uh oh"));
  127. Int64 runTime;
  128. GetPrecisionTimer(&runTime);
  129. runTime -= m_startTime;
  130. m_runningTimeGross += runTime;
  131. m_runningTimeNet += runTime;
  132. ++m_callCount;
  133. #ifdef _DEBUG
  134. DEBUG_ASSERTCRASH(*m_activeHead != NULL, ("m_activeHead is null, uh oh"));
  135. DEBUG_ASSERTCRASH(*m_activeHead == this, ("I am not the active timer, uh oh"));
  136. DEBUG_ASSERTCRASH(m_activeHead >= &m_active[0] && m_activeHead <= &m_active[MAX_ACTIVE_STACK-1], ("active under/over flow"));
  137. #endif
  138. --m_activeHead;
  139. if (*m_activeHead)
  140. {
  141. // don't add the time it took for us to actually get the ticks (in startTimer) to our parent...
  142. (*m_activeHead)->m_runningTimeGross -= (s_stopStartOverhead);
  143. if ((*m_activeHead)->m_netTimeOnly) {
  144. (*m_activeHead)->m_runningTimeNet -= (runTime + s_stopStartOverhead);
  145. }
  146. }
  147. }
  148. //-------------------------------------------------------------------------------------------------
  149. //-------------------------------------------------------------------------------------------------
  150. //-------------------------------------------------------------------------------------------------
  151. //-------------------------------------------------------------------------------------------------
  152. class AutoPerfGather
  153. {
  154. private:
  155. PerfGather& m_g;
  156. public:
  157. __forceinline AutoPerfGather(PerfGather& g);
  158. __forceinline ~AutoPerfGather();
  159. };
  160. //-------------------------------------------------------------------------------------------------
  161. AutoPerfGather::AutoPerfGather(PerfGather& g) : m_g(g)
  162. {
  163. m_g.startTimer();
  164. }
  165. //-------------------------------------------------------------------------------------------------
  166. AutoPerfGather::~AutoPerfGather()
  167. {
  168. m_g.stopTimer();
  169. }
  170. //-------------------------------------------------------------------------------------------------
  171. class AutoPerfGatherIgnore
  172. {
  173. private:
  174. static Bool s_ignoring;
  175. PerfGather& m_g;
  176. Bool m_oldIgnore;
  177. public:
  178. __forceinline AutoPerfGatherIgnore(PerfGather& g);
  179. __forceinline ~AutoPerfGatherIgnore();
  180. };
  181. //-------------------------------------------------------------------------------------------------
  182. AutoPerfGatherIgnore::AutoPerfGatherIgnore(PerfGather& g) : m_g(g)
  183. {
  184. m_oldIgnore = s_ignoring;
  185. s_ignoring = true;
  186. m_g.startTimer();
  187. }
  188. //-------------------------------------------------------------------------------------------------
  189. AutoPerfGatherIgnore::~AutoPerfGatherIgnore()
  190. {
  191. m_g.stopTimer();
  192. if (s_ignoring)
  193. m_g.reset();
  194. s_ignoring = m_oldIgnore;
  195. }
  196. //-------------------------------------------------------------------------------------------------
  197. #define DECLARE_TOTAL_PERF_TIMER(id) static PerfGather s_##id(#id, false);
  198. #define DECLARE_PERF_TIMER(id) static PerfGather s_##id(#id);
  199. #define USE_PERF_TIMER(id) AutoPerfGather a_##id(s_##id);
  200. #define IGNORE_PERF_TIMER(id) AutoPerfGatherIgnore a_##id(s_##id);
  201. //-------------------------------------------------------------------------------------------------
  202. //-------------------------------------------------------------------------------------------------
  203. //-------------------------------------------------------------------------------------------------
  204. //-------------------------------------------------------------------------------------------------
  205. class PerfTimer
  206. {
  207. public:
  208. PerfTimer( const char *identifier, Bool crashWithInfo = true, Int startFrame = 0, Int endFrame = -1);
  209. virtual ~PerfTimer( );
  210. __forceinline void startTimer( void );
  211. __forceinline void stopTimer( void );
  212. protected:
  213. Int64 m_startTime;
  214. protected:
  215. void outputInfo( void );
  216. void showMetrics( void );
  217. protected:
  218. const char *m_identifier;
  219. Bool m_crashWithInfo;
  220. UnsignedInt m_startFrame;
  221. UnsignedInt m_endFrame;
  222. UnsignedInt m_lastFrame; // last frame we got data from
  223. Bool m_outputInfo;
  224. // total running time so far.
  225. Int64 m_runningTime;
  226. Int m_callCount;
  227. friend void StatMetricsDisplay( DebugDisplayInterface *dd, void *, FILE *fp );
  228. friend void EndStatMetricsDisplay( DebugDisplayInterface *dd, void *, FILE *fp );
  229. };
  230. //-------------------------------------------------------------------------------------------------
  231. void PerfTimer::startTimer( void )
  232. {
  233. UnsignedInt frm = (TheGameLogic ? TheGameLogic->getFrame() : m_startFrame);
  234. if (frm >= m_startFrame && (m_endFrame == -1 || frm <= m_endFrame))
  235. {
  236. GetPrecisionTimer(&m_startTime);
  237. }
  238. }
  239. //-------------------------------------------------------------------------------------------------
  240. void PerfTimer::stopTimer( void )
  241. {
  242. UnsignedInt frm = (TheGameLogic ? TheGameLogic->getFrame() : m_startFrame);
  243. if (frm >= m_startFrame && (m_endFrame == -1 || frm <= m_endFrame))
  244. {
  245. Int64 tmp;
  246. GetPrecisionTimer(&tmp);
  247. m_runningTime += (tmp - m_startTime);
  248. ++m_callCount;
  249. m_lastFrame = frm;
  250. }
  251. if (TheGlobalData && TheGlobalData->m_showMetrics && m_endFrame > m_startFrame + PERFMETRICS_BETWEEN_METRICS) {
  252. m_endFrame = m_startFrame + PERFMETRICS_BETWEEN_METRICS;
  253. }
  254. if (m_endFrame > 0 && frm >= m_endFrame) {
  255. if (TheGlobalData->m_showMetrics) {
  256. showMetrics();
  257. }
  258. outputInfo();
  259. }
  260. }
  261. //-------------------------------------------------------------------------------------------------
  262. extern void StatMetricsDisplay( DebugDisplayInterface *dd, void *, FILE *fp );
  263. #else // PERF_TIMERS
  264. #define DECLARE_PERF_TIMER(id)
  265. #define DECLARE_TOTAL_PERF_TIMER(id)
  266. #define USE_PERF_TIMER(id)
  267. #define IGNORE_PERF_TIMER(id)
  268. #endif // PERF_TIMERS
  269. #endif /* __PERFTIMER_H__ */