PerfTimer.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. ** Command & Conquer Generals(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. PerfGather( const char *identifier );
  83. virtual ~PerfGather( );
  84. __forceinline void startTimer();
  85. __forceinline void stopTimer();
  86. enum
  87. {
  88. PERF_GROSSTIME = 0x01,
  89. PERF_NETTIME = 0x02,
  90. PERF_CALLCOUNT = 0x04
  91. };
  92. static void resetAll();
  93. static void initPerfDump(const char* fname, Int options);
  94. static void termPerfDump();
  95. static void dumpAll(UnsignedInt frame);
  96. static void displayGraph(UnsignedInt frame);
  97. void reset();
  98. private:
  99. enum { MAX_ACTIVE_STACK = 256 };
  100. static PerfGather* m_active[MAX_ACTIVE_STACK];
  101. static PerfGather** m_activeHead;
  102. static Int64 s_stopStartOverhead; // overhead for stop+start a timer
  103. static PerfGather*& getHeadPtr();
  104. void addToList();
  105. void removeFromList();
  106. const char* m_identifier;
  107. Int64 m_startTime;
  108. Int64 m_runningTimeGross;
  109. Int64 m_runningTimeNet;
  110. Int m_callCount;
  111. PerfGather* m_next;
  112. PerfGather* m_prev;
  113. Bool m_ignore;
  114. };
  115. //-------------------------------------------------------------------------------------------------
  116. void PerfGather::startTimer()
  117. {
  118. *++m_activeHead = this;
  119. GetPrecisionTimer(&m_startTime);
  120. }
  121. //-------------------------------------------------------------------------------------------------
  122. void PerfGather::stopTimer()
  123. {
  124. DEBUG_ASSERTCRASH(this != NULL, ("I am null, uh oh"));
  125. Int64 runTime;
  126. GetPrecisionTimer(&runTime);
  127. runTime -= m_startTime;
  128. m_runningTimeGross += runTime;
  129. m_runningTimeNet += runTime;
  130. ++m_callCount;
  131. #ifdef _DEBUG
  132. DEBUG_ASSERTCRASH(*m_activeHead != NULL, ("m_activeHead is null, uh oh"));
  133. DEBUG_ASSERTCRASH(*m_activeHead == this, ("I am not the active timer, uh oh"));
  134. DEBUG_ASSERTCRASH(m_activeHead >= &m_active[0] && m_activeHead <= &m_active[MAX_ACTIVE_STACK-1], ("active under/over flow"));
  135. #endif
  136. --m_activeHead;
  137. if (*m_activeHead)
  138. {
  139. // don't add the time it took for us to actually get the ticks (in startTimer) to our parent...
  140. (*m_activeHead)->m_runningTimeGross -= (s_stopStartOverhead);
  141. (*m_activeHead)->m_runningTimeNet -= (runTime + s_stopStartOverhead);
  142. }
  143. }
  144. //-------------------------------------------------------------------------------------------------
  145. //-------------------------------------------------------------------------------------------------
  146. //-------------------------------------------------------------------------------------------------
  147. //-------------------------------------------------------------------------------------------------
  148. class AutoPerfGather
  149. {
  150. private:
  151. PerfGather& m_g;
  152. public:
  153. __forceinline AutoPerfGather(PerfGather& g);
  154. __forceinline ~AutoPerfGather();
  155. };
  156. //-------------------------------------------------------------------------------------------------
  157. AutoPerfGather::AutoPerfGather(PerfGather& g) : m_g(g)
  158. {
  159. m_g.startTimer();
  160. }
  161. //-------------------------------------------------------------------------------------------------
  162. AutoPerfGather::~AutoPerfGather()
  163. {
  164. m_g.stopTimer();
  165. }
  166. //-------------------------------------------------------------------------------------------------
  167. class AutoPerfGatherIgnore
  168. {
  169. private:
  170. static Bool s_ignoring;
  171. PerfGather& m_g;
  172. Bool m_oldIgnore;
  173. public:
  174. __forceinline AutoPerfGatherIgnore(PerfGather& g);
  175. __forceinline ~AutoPerfGatherIgnore();
  176. };
  177. //-------------------------------------------------------------------------------------------------
  178. AutoPerfGatherIgnore::AutoPerfGatherIgnore(PerfGather& g) : m_g(g)
  179. {
  180. m_oldIgnore = s_ignoring;
  181. s_ignoring = true;
  182. m_g.startTimer();
  183. }
  184. //-------------------------------------------------------------------------------------------------
  185. AutoPerfGatherIgnore::~AutoPerfGatherIgnore()
  186. {
  187. m_g.stopTimer();
  188. if (s_ignoring)
  189. m_g.reset();
  190. s_ignoring = m_oldIgnore;
  191. }
  192. //-------------------------------------------------------------------------------------------------
  193. #define DECLARE_PERF_TIMER(id) static PerfGather s_##id(#id);
  194. #define USE_PERF_TIMER(id) AutoPerfGather a_##id(s_##id);
  195. #define IGNORE_PERF_TIMER(id) AutoPerfGatherIgnore a_##id(s_##id);
  196. //-------------------------------------------------------------------------------------------------
  197. //-------------------------------------------------------------------------------------------------
  198. //-------------------------------------------------------------------------------------------------
  199. //-------------------------------------------------------------------------------------------------
  200. class PerfTimer
  201. {
  202. public:
  203. PerfTimer( const char *identifier, Bool crashWithInfo = true, Int startFrame = 0, Int endFrame = -1);
  204. virtual ~PerfTimer( );
  205. __forceinline void startTimer( void );
  206. __forceinline void stopTimer( void );
  207. protected:
  208. Int64 m_startTime;
  209. protected:
  210. void outputInfo( void );
  211. void showMetrics( void );
  212. protected:
  213. const char *m_identifier;
  214. Bool m_crashWithInfo;
  215. UnsignedInt m_startFrame;
  216. UnsignedInt m_endFrame;
  217. UnsignedInt m_lastFrame; // last frame we got data from
  218. Bool m_outputInfo;
  219. // total running time so far.
  220. Int64 m_runningTime;
  221. Int m_callCount;
  222. friend void StatMetricsDisplay( DebugDisplayInterface *dd, void *, FILE *fp );
  223. friend void EndStatMetricsDisplay( DebugDisplayInterface *dd, void *, FILE *fp );
  224. };
  225. //-------------------------------------------------------------------------------------------------
  226. void PerfTimer::startTimer( void )
  227. {
  228. UnsignedInt frm = (TheGameLogic ? TheGameLogic->getFrame() : m_startFrame);
  229. if (frm >= m_startFrame && (m_endFrame == -1 || frm <= m_endFrame))
  230. {
  231. GetPrecisionTimer(&m_startTime);
  232. }
  233. }
  234. //-------------------------------------------------------------------------------------------------
  235. void PerfTimer::stopTimer( void )
  236. {
  237. UnsignedInt frm = (TheGameLogic ? TheGameLogic->getFrame() : m_startFrame);
  238. if (frm >= m_startFrame && (m_endFrame == -1 || frm <= m_endFrame))
  239. {
  240. Int64 tmp;
  241. GetPrecisionTimer(&tmp);
  242. m_runningTime += (tmp - m_startTime);
  243. ++m_callCount;
  244. m_lastFrame = frm;
  245. }
  246. if (TheGlobalData && TheGlobalData->m_showMetrics && m_endFrame > m_startFrame + PERFMETRICS_BETWEEN_METRICS) {
  247. m_endFrame = m_startFrame + PERFMETRICS_BETWEEN_METRICS;
  248. }
  249. if (m_endFrame > 0 && frm >= m_endFrame) {
  250. if (TheGlobalData->m_showMetrics) {
  251. showMetrics();
  252. }
  253. outputInfo();
  254. }
  255. }
  256. //-------------------------------------------------------------------------------------------------
  257. extern void StatMetricsDisplay( DebugDisplayInterface *dd, void *, FILE *fp );
  258. #else // PERF_TIMERS
  259. #define DECLARE_PERF_TIMER(id)
  260. #define USE_PERF_TIMER(id)
  261. #define IGNORE_PERF_TIMER(id)
  262. #endif // PERF_TIMERS
  263. #endif /* __PERFTIMER_H__ */