b3Quickprof.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Copyright (c) 2003-2013 Erwin Coumans http://bulletphysics.org
  3. This software is provided 'as-is', without any express or implied warranty.
  4. In no event will the authors be held liable for any damages arising from the use of this software.
  5. Permission is granted to anyone to use this software for any purpose,
  6. including commercial applications, and to alter it and redistribute it freely,
  7. subject to the following restrictions:
  8. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  9. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. 3. This notice may not be removed or altered from any source distribution.
  11. */
  12. /***************************************************************************************************
  13. **
  14. ** Real-Time Hierarchical Profiling for Game Programming Gems 3
  15. **
  16. ** by Greg Hjelstrom & Byon Garrabrant
  17. **
  18. ***************************************************************************************************/
  19. // Credits: The Clock class was inspired by the Timer classes in
  20. // Ogre (www.ogre3d.org).
  21. #ifndef B3_QUICK_PROF_H
  22. #define B3_QUICK_PROF_H
  23. //To disable built-in profiling, please comment out next line
  24. //#define B3_NO_PROFILE 1
  25. #ifndef B3_NO_PROFILE
  26. #include <stdio.h>//@todo remove this, backwards compatibility
  27. #include "Bullet3Common/b3Scalar.h"
  28. #include "Bullet3Common/b3AlignedAllocator.h"
  29. #include <new>
  30. #include "b3Clock.h"
  31. ///A node in the Profile Hierarchy Tree
  32. class b3ProfileNode {
  33. public:
  34. b3ProfileNode( const char * name, b3ProfileNode * parent );
  35. ~b3ProfileNode( void );
  36. b3ProfileNode * Get_Sub_Node( const char * name );
  37. b3ProfileNode * Get_Parent( void ) { return Parent; }
  38. b3ProfileNode * Get_Sibling( void ) { return Sibling; }
  39. b3ProfileNode * Get_Child( void ) { return Child; }
  40. void CleanupMemory();
  41. void Reset( void );
  42. void Call( void );
  43. bool Return( void );
  44. const char * Get_Name( void ) { return Name; }
  45. int Get_Total_Calls( void ) { return TotalCalls; }
  46. float Get_Total_Time( void ) { return TotalTime; }
  47. void* GetUserPointer() const {return m_userPtr;}
  48. void SetUserPointer(void* ptr) { m_userPtr = ptr;}
  49. protected:
  50. const char * Name;
  51. int TotalCalls;
  52. float TotalTime;
  53. unsigned long int StartTime;
  54. int RecursionCounter;
  55. b3ProfileNode * Parent;
  56. b3ProfileNode * Child;
  57. b3ProfileNode * Sibling;
  58. void* m_userPtr;
  59. };
  60. ///An iterator to navigate through the tree
  61. class b3ProfileIterator
  62. {
  63. public:
  64. // Access all the children of the current parent
  65. void First(void);
  66. void Next(void);
  67. bool Is_Done(void);
  68. bool Is_Root(void) { return (CurrentParent->Get_Parent() == 0); }
  69. void Enter_Child( int index ); // Make the given child the new parent
  70. void Enter_Largest_Child( void ); // Make the largest child the new parent
  71. void Enter_Parent( void ); // Make the current parent's parent the new parent
  72. // Access the current child
  73. const char * Get_Current_Name( void ) { return CurrentChild->Get_Name(); }
  74. int Get_Current_Total_Calls( void ) { return CurrentChild->Get_Total_Calls(); }
  75. float Get_Current_Total_Time( void ) { return CurrentChild->Get_Total_Time(); }
  76. void* Get_Current_UserPointer( void ) { return CurrentChild->GetUserPointer(); }
  77. void Set_Current_UserPointer(void* ptr) {CurrentChild->SetUserPointer(ptr);}
  78. // Access the current parent
  79. const char * Get_Current_Parent_Name( void ) { return CurrentParent->Get_Name(); }
  80. int Get_Current_Parent_Total_Calls( void ) { return CurrentParent->Get_Total_Calls(); }
  81. float Get_Current_Parent_Total_Time( void ) { return CurrentParent->Get_Total_Time(); }
  82. protected:
  83. b3ProfileNode * CurrentParent;
  84. b3ProfileNode * CurrentChild;
  85. b3ProfileIterator( b3ProfileNode * start );
  86. friend class b3ProfileManager;
  87. };
  88. ///The Manager for the Profile system
  89. class b3ProfileManager {
  90. public:
  91. static void Start_Profile( const char * name );
  92. static void Stop_Profile( void );
  93. static void CleanupMemory(void)
  94. {
  95. Root.CleanupMemory();
  96. }
  97. static void Reset( void );
  98. static void Increment_Frame_Counter( void );
  99. static int Get_Frame_Count_Since_Reset( void ) { return FrameCounter; }
  100. static float Get_Time_Since_Reset( void );
  101. static b3ProfileIterator * Get_Iterator( void )
  102. {
  103. return new b3ProfileIterator( &Root );
  104. }
  105. static void Release_Iterator( b3ProfileIterator * iterator ) { delete ( iterator); }
  106. static void dumpRecursive(b3ProfileIterator* profileIterator, int spacing);
  107. static void dumpAll();
  108. static void dumpRecursive(FILE* f, b3ProfileIterator* profileIterator, int spacing);
  109. static void dumpAll(FILE* f);
  110. private:
  111. static b3ProfileNode Root;
  112. static b3ProfileNode * CurrentNode;
  113. static int FrameCounter;
  114. static unsigned long int ResetTime;
  115. };
  116. #else
  117. #endif //#ifndef B3_NO_PROFILE
  118. #endif //B3_QUICK_PROF_H