btQuickprof.h 4.7 KB

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