btQuickprof.h 5.1 KB

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