btQuickprof.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /***************************************************************************************************
  2. **
  3. ** Real-Time Hierarchical Profiling for Game Programming Gems 3
  4. **
  5. ** by Greg Hjelstrom & Byon Garrabrant
  6. **
  7. ***************************************************************************************************/
  8. // Credits: The Clock class was inspired by the Timer classes in
  9. // Ogre (www.ogre3d.org).
  10. #ifndef BT_QUICK_PROF_H
  11. #define BT_QUICK_PROF_H
  12. #include "btScalar.h"
  13. #define USE_BT_CLOCK 1
  14. #ifdef USE_BT_CLOCK
  15. ///The btClock is a portable basic clock that measures accurate time in seconds, use for profiling.
  16. class btClock
  17. {
  18. public:
  19. btClock();
  20. btClock(const btClock& other);
  21. btClock& operator=(const btClock& other);
  22. ~btClock();
  23. /// Resets the initial reference time.
  24. void reset();
  25. /// Returns the time in ms since the last call to reset or since
  26. /// the btClock was created.
  27. unsigned long long int getTimeMilliseconds();
  28. /// Returns the time in us since the last call to reset or since
  29. /// the Clock was created.
  30. unsigned long long int getTimeMicroseconds();
  31. unsigned long long int getTimeNanoseconds();
  32. /// Returns the time in s since the last call to reset or since
  33. /// the Clock was created.
  34. btScalar getTimeSeconds();
  35. private:
  36. struct btClockData* m_data;
  37. };
  38. #endif //USE_BT_CLOCK
  39. typedef void(btEnterProfileZoneFunc)(const char* msg);
  40. typedef void(btLeaveProfileZoneFunc)();
  41. btEnterProfileZoneFunc* btGetCurrentEnterProfileZoneFunc();
  42. btLeaveProfileZoneFunc* btGetCurrentLeaveProfileZoneFunc();
  43. void btSetCustomEnterProfileZoneFunc(btEnterProfileZoneFunc* enterFunc);
  44. void btSetCustomLeaveProfileZoneFunc(btLeaveProfileZoneFunc* leaveFunc);
  45. #ifndef BT_ENABLE_PROFILE
  46. #define BT_NO_PROFILE 1
  47. #endif //BT_NO_PROFILE
  48. const unsigned int BT_QUICKPROF_MAX_THREAD_COUNT = 64;
  49. //btQuickprofGetCurrentThreadIndex will return -1 if thread index cannot be determined,
  50. //otherwise returns thread index in range [0..maxThreads]
  51. unsigned int btQuickprofGetCurrentThreadIndex2();
  52. #ifndef BT_NO_PROFILE
  53. #include <stdio.h> //@todo remove this, backwards compatibility
  54. #include "btAlignedAllocator.h"
  55. #include <new>
  56. ///A node in the Profile Hierarchy Tree
  57. class CProfileNode
  58. {
  59. public:
  60. CProfileNode(const char* name, CProfileNode* parent);
  61. ~CProfileNode(void);
  62. CProfileNode* Get_Sub_Node(const char* name);
  63. CProfileNode* Get_Parent(void) { return Parent; }
  64. CProfileNode* Get_Sibling(void) { return Sibling; }
  65. CProfileNode* Get_Child(void) { return Child; }
  66. void CleanupMemory();
  67. void Reset(void);
  68. void Call(void);
  69. bool Return(void);
  70. const char* Get_Name(void) { return Name; }
  71. int Get_Total_Calls(void) { return TotalCalls; }
  72. float Get_Total_Time(void) { return TotalTime; }
  73. void* GetUserPointer() const { return m_userPtr; }
  74. void SetUserPointer(void* ptr) { m_userPtr = ptr; }
  75. protected:
  76. const char* Name;
  77. int TotalCalls;
  78. float TotalTime;
  79. unsigned long int StartTime;
  80. int RecursionCounter;
  81. CProfileNode* Parent;
  82. CProfileNode* Child;
  83. CProfileNode* Sibling;
  84. void* m_userPtr;
  85. };
  86. ///An iterator to navigate through the tree
  87. class CProfileIterator
  88. {
  89. public:
  90. // Access all the children of the current parent
  91. void First(void);
  92. void Next(void);
  93. bool Is_Done(void);
  94. bool Is_Root(void) { return (CurrentParent->Get_Parent() == 0); }
  95. void Enter_Child(int index); // Make the given child the new parent
  96. void Enter_Largest_Child(void); // Make the largest child the new parent
  97. void Enter_Parent(void); // Make the current parent's parent the new parent
  98. // Access the current child
  99. const char* Get_Current_Name(void) { return CurrentChild->Get_Name(); }
  100. int Get_Current_Total_Calls(void) { return CurrentChild->Get_Total_Calls(); }
  101. float Get_Current_Total_Time(void) { return CurrentChild->Get_Total_Time(); }
  102. void* Get_Current_UserPointer(void) { return CurrentChild->GetUserPointer(); }
  103. void Set_Current_UserPointer(void* ptr) { CurrentChild->SetUserPointer(ptr); }
  104. // Access the current parent
  105. const char* Get_Current_Parent_Name(void) { return CurrentParent->Get_Name(); }
  106. int Get_Current_Parent_Total_Calls(void) { return CurrentParent->Get_Total_Calls(); }
  107. float Get_Current_Parent_Total_Time(void) { return CurrentParent->Get_Total_Time(); }
  108. protected:
  109. CProfileNode* CurrentParent;
  110. CProfileNode* CurrentChild;
  111. CProfileIterator(CProfileNode* start);
  112. friend class CProfileManager;
  113. };
  114. ///The Manager for the Profile system
  115. class CProfileManager
  116. {
  117. public:
  118. static void Start_Profile(const char* name);
  119. static void Stop_Profile(void);
  120. static void CleanupMemory(void);
  121. // {
  122. // Root.CleanupMemory();
  123. // }
  124. static void Reset(void);
  125. static void Increment_Frame_Counter(void);
  126. static int Get_Frame_Count_Since_Reset(void) { return FrameCounter; }
  127. static float Get_Time_Since_Reset(void);
  128. static CProfileIterator* Get_Iterator(void);
  129. // {
  130. //
  131. // return new CProfileIterator( &Root );
  132. // }
  133. static void Release_Iterator(CProfileIterator* iterator) { delete (iterator); }
  134. static void dumpRecursive(CProfileIterator* profileIterator, int spacing);
  135. static void dumpAll();
  136. private:
  137. static int FrameCounter;
  138. static unsigned long int ResetTime;
  139. };
  140. #endif //#ifndef BT_NO_PROFILE
  141. ///ProfileSampleClass is a simple way to profile a function's scope
  142. ///Use the BT_PROFILE macro at the start of scope to time
  143. class CProfileSample
  144. {
  145. public:
  146. CProfileSample(const char* name);
  147. ~CProfileSample(void);
  148. };
  149. #define BT_PROFILE(name) CProfileSample __profile(name)
  150. #endif //BT_QUICK_PROF_H