PerfTimer.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #pragma once
  2. #include "../Common.h"
  3. //#include "boost/unordered/unordered_map.hpp"
  4. #include "BumpAllocator.h"
  5. #include "SLIList.h"
  6. #include <unordered_map>
  7. #include "BumpAllocator.h"
  8. #include "Dictionary.h"
  9. #include <map>
  10. #include <list>
  11. NS_BF_BEGIN;
  12. class PerfEntry;
  13. class PerfEntry
  14. {
  15. public:
  16. String mName;
  17. Dictionary<String, PerfEntry*> mChildren;
  18. PerfEntry* mChild;
  19. int mCount;
  20. uint64 mTimeTotal;
  21. uint64 mCurTimingStart;
  22. public:
  23. PerfEntry();
  24. void DbgPrint(const StringImpl& name, int level, std::map<String, int>& uniqueTimingMap);
  25. };
  26. class PerfFrame : public PerfEntry
  27. {
  28. public:
  29. int mAppFrameNum;
  30. };
  31. typedef std::list<PerfFrame> PerfFrameList;
  32. const int MAX_PERFMANGER_STACKSIZE = 256;
  33. enum PerfLogEntryType
  34. {
  35. PerfLogEntryType_FrameStart,
  36. PerfLogEntryType_Start,
  37. PerfLogEntryType_End,
  38. PerfLogEntryType_Log,
  39. PerfLogEntryType_StopRecording
  40. };
  41. struct PerfLogEntry
  42. {
  43. int mType;
  44. PerfLogEntry* mNext;
  45. };
  46. struct PerfLogEntry_FrameStart : PerfLogEntry
  47. {
  48. uint64 mTimingStart;
  49. };
  50. struct PerfLogEntry_StopRecording : PerfLogEntry
  51. {
  52. uint64 mTimingEnd;
  53. };
  54. // We could use 32-bit timing start and timing end values and still
  55. // record timings up to 71 minutes long
  56. struct PerfLogEntry_Start : PerfLogEntry
  57. {
  58. const char* mName;
  59. uint64 mTimingStart;
  60. };
  61. struct PerfLogEntry_End : PerfLogEntry
  62. {
  63. uint64 mTimingEnd;
  64. };
  65. class PerfManager
  66. {
  67. public:
  68. BumpAllocator mAlloc;
  69. SLIList<PerfLogEntry*> mLogEntries;
  70. PerfFrameList mFrames;
  71. PerfEntry* mCurStack[MAX_PERFMANGER_STACKSIZE];
  72. int mCurStackIdx;
  73. bool mRecording;
  74. BfpThreadId mOwningThreadId;
  75. uint64 mOverhead;
  76. protected:
  77. void Clear();
  78. public:
  79. PerfManager();
  80. void NextFrame();
  81. void ZoneStart(const char* name);
  82. void ZoneEnd(bool allowFrameEnd = false);
  83. void Message(const StringImpl& theString);
  84. void StartRecording();
  85. void StopRecording(bool dbgPrint = false);
  86. bool IsRecording();
  87. void DbgPrint();
  88. };
  89. extern PerfManager* gPerfManager;
  90. class AutoPerf
  91. {
  92. public:
  93. PerfManager* mPerfManager;
  94. int mStartStackIdx;
  95. public:
  96. AutoPerf(const char* name, PerfManager* perfManager = gPerfManager);
  97. ~AutoPerf();
  98. void Leave();
  99. };
  100. class AutoDbgTime
  101. {
  102. public:
  103. uint32 mStartTick;
  104. String mName;
  105. public:
  106. AutoDbgTime(const StringImpl& name);
  107. ~AutoDbgTime();
  108. };
  109. class AutoPerfRecordAndPrint
  110. {
  111. public:
  112. PerfManager* mPerfManager;
  113. bool mDidStart;
  114. public:
  115. AutoPerfRecordAndPrint(PerfManager* perfManager = gPerfManager);
  116. ~AutoPerfRecordAndPrint();
  117. };
  118. class AutoTimer
  119. {
  120. public:
  121. uint32 mStartTick;
  122. int* mTimePtr;
  123. public:
  124. AutoTimer(int& timeRef)
  125. {
  126. mTimePtr = &timeRef;
  127. mStartTick = BFTickCount();
  128. }
  129. ~AutoTimer()
  130. {
  131. *mTimePtr += BFTickCount() - mStartTick;
  132. }
  133. };
  134. class DebugTimeGuard
  135. {
  136. public:
  137. uint32 mStartTick;
  138. int mMaxTicks;
  139. String mName;
  140. public:
  141. DebugTimeGuard(int maxTicks, const StringImpl& name = StringImpl::MakeRef("DebugTimeGuard"))
  142. {
  143. mName = name;
  144. mMaxTicks = maxTicks;
  145. Start();
  146. }
  147. void Start()
  148. {
  149. mStartTick = BFTickCount();
  150. }
  151. void Stop()
  152. {
  153. if (mMaxTicks == 0)
  154. return;
  155. int maxTime = (int)(BFTickCount() - mStartTick);
  156. if (maxTime > mMaxTicks)
  157. OutputDebugStrF("%s took too long: %dms\n", mName.c_str(), maxTime);
  158. mMaxTicks = 0;
  159. }
  160. ~DebugTimeGuard()
  161. {
  162. Stop();
  163. }
  164. };
  165. NS_BF_END;