btQuickprof.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. private:
  38. struct btClockData* m_data;
  39. };
  40. #endif //USE_BT_CLOCK
  41. ///A node in the Profile Hierarchy Tree
  42. class CProfileNode {
  43. public:
  44. CProfileNode( const char * name, CProfileNode * parent );
  45. ~CProfileNode( void );
  46. CProfileNode * Get_Sub_Node( const char * name );
  47. CProfileNode * Get_Parent( void ) { return Parent; }
  48. CProfileNode * Get_Sibling( void ) { return Sibling; }
  49. CProfileNode * Get_Child( void ) { return Child; }
  50. void CleanupMemory();
  51. void Reset( void );
  52. void Call( void );
  53. bool Return( void );
  54. const char * Get_Name( void ) { return Name; }
  55. int Get_Total_Calls( void ) { return TotalCalls; }
  56. float Get_Total_Time( void ) { return TotalTime; }
  57. void* GetUserPointer() const {return m_userPtr;}
  58. void SetUserPointer(void* ptr) { m_userPtr = ptr;}
  59. protected:
  60. const char * Name;
  61. int TotalCalls;
  62. float TotalTime;
  63. unsigned long int StartTime;
  64. int RecursionCounter;
  65. CProfileNode * Parent;
  66. CProfileNode * Child;
  67. CProfileNode * Sibling;
  68. void* m_userPtr;
  69. };
  70. ///An iterator to navigate through the tree
  71. class CProfileIterator
  72. {
  73. public:
  74. // Access all the children of the current parent
  75. void First(void);
  76. void Next(void);
  77. bool Is_Done(void);
  78. bool Is_Root(void) { return (CurrentParent->Get_Parent() == 0); }
  79. void Enter_Child( int index ); // Make the given child the new parent
  80. void Enter_Largest_Child( void ); // Make the largest child the new parent
  81. void Enter_Parent( void ); // Make the current parent's parent the new parent
  82. // Access the current child
  83. const char * Get_Current_Name( void ) { return CurrentChild->Get_Name(); }
  84. int Get_Current_Total_Calls( void ) { return CurrentChild->Get_Total_Calls(); }
  85. float Get_Current_Total_Time( void ) { return CurrentChild->Get_Total_Time(); }
  86. void* Get_Current_UserPointer( void ) { return CurrentChild->GetUserPointer(); }
  87. void Set_Current_UserPointer(void* ptr) {CurrentChild->SetUserPointer(ptr);}
  88. // Access the current parent
  89. const char * Get_Current_Parent_Name( void ) { return CurrentParent->Get_Name(); }
  90. int Get_Current_Parent_Total_Calls( void ) { return CurrentParent->Get_Total_Calls(); }
  91. float Get_Current_Parent_Total_Time( void ) { return CurrentParent->Get_Total_Time(); }
  92. protected:
  93. CProfileNode * CurrentParent;
  94. CProfileNode * CurrentChild;
  95. CProfileIterator( CProfileNode * start );
  96. friend class CProfileManager;
  97. };
  98. ///The Manager for the Profile system
  99. class CProfileManager {
  100. public:
  101. static void Start_Profile( const char * name );
  102. static void Stop_Profile( void );
  103. static void CleanupMemory(void)
  104. {
  105. Root.CleanupMemory();
  106. }
  107. static void Reset( void );
  108. static void Increment_Frame_Counter( void );
  109. static int Get_Frame_Count_Since_Reset( void ) { return FrameCounter; }
  110. static float Get_Time_Since_Reset( void );
  111. static CProfileIterator * Get_Iterator( void )
  112. {
  113. return new CProfileIterator( &Root );
  114. }
  115. static void Release_Iterator( CProfileIterator * iterator ) { delete ( iterator); }
  116. static void dumpRecursive(CProfileIterator* profileIterator, int spacing);
  117. static void dumpAll();
  118. private:
  119. static CProfileNode Root;
  120. static CProfileNode * CurrentNode;
  121. static int FrameCounter;
  122. static unsigned long int ResetTime;
  123. };
  124. ///ProfileSampleClass is a simple way to profile a function's scope
  125. ///Use the BT_PROFILE macro at the start of scope to time
  126. class CProfileSample {
  127. public:
  128. CProfileSample( const char * name )
  129. {
  130. CProfileManager::Start_Profile( name );
  131. }
  132. ~CProfileSample( void )
  133. {
  134. CProfileManager::Stop_Profile();
  135. }
  136. };
  137. #define BT_PROFILE( name ) CProfileSample __profile( name )
  138. #else
  139. #define BT_PROFILE( name )
  140. #endif //#ifndef BT_NO_PROFILE
  141. #endif //BT_QUICK_PROF_H