2 /***************************************************************************************************
4 ** Real-Time Hierarchical Profiling for Game Programming Gems 3
6 ** by Greg Hjelstrom & Byon Garrabrant
8 ***************************************************************************************************/
10 // Credits: The Clock class was inspired by the Timer classes in
11 // Ogre (www.ogre3d.org).
15 #ifndef BT_QUICK_PROF_H
16 #define BT_QUICK_PROF_H
18 //To disable built-in profiling, please comment out next line
19 //#define BT_NO_PROFILE 1
21 #include <stdio.h>//@todo remove this, backwards compatibility
23 #include "btAlignedAllocator.h"
30 #define USE_BT_CLOCK 1
34 ///The btClock is a portable basic clock that measures accurate time in seconds, use for profiling.
40 btClock(const btClock& other);
41 btClock& operator=(const btClock& other);
45 /// Resets the initial reference time.
48 /// Returns the time in ms since the last call to reset or since
49 /// the btClock was created.
50 unsigned long int getTimeMilliseconds();
52 /// Returns the time in us since the last call to reset or since
53 /// the Clock was created.
54 unsigned long int getTimeMicroseconds();
56 struct btClockData* m_data;
64 ///A node in the Profile Hierarchy Tree
68 CProfileNode( const char * name, CProfileNode * parent );
69 ~CProfileNode( void );
71 CProfileNode * Get_Sub_Node( const char * name );
73 CProfileNode * Get_Parent( void ) { return Parent; }
74 CProfileNode * Get_Sibling( void ) { return Sibling; }
75 CProfileNode * Get_Child( void ) { return Child; }
82 const char * Get_Name( void ) { return Name; }
83 int Get_Total_Calls( void ) { return TotalCalls; }
84 float Get_Total_Time( void ) { return TotalTime; }
85 void* GetUserPointer() const {return m_userPtr;}
86 void SetUserPointer(void* ptr) { m_userPtr = ptr;}
92 unsigned long int StartTime;
95 CProfileNode * Parent;
97 CProfileNode * Sibling;
101 ///An iterator to navigate through the tree
102 class CProfileIterator
105 // Access all the children of the current parent
109 bool Is_Root(void) { return (CurrentParent->Get_Parent() == 0); }
111 void Enter_Child( int index ); // Make the given child the new parent
112 void Enter_Largest_Child( void ); // Make the largest child the new parent
113 void Enter_Parent( void ); // Make the current parent's parent the new parent
115 // Access the current child
116 const char * Get_Current_Name( void ) { return CurrentChild->Get_Name(); }
117 int Get_Current_Total_Calls( void ) { return CurrentChild->Get_Total_Calls(); }
118 float Get_Current_Total_Time( void ) { return CurrentChild->Get_Total_Time(); }
120 void* Get_Current_UserPointer( void ) { return CurrentChild->GetUserPointer(); }
121 void Set_Current_UserPointer(void* ptr) {CurrentChild->SetUserPointer(ptr);}
122 // Access the current parent
123 const char * Get_Current_Parent_Name( void ) { return CurrentParent->Get_Name(); }
124 int Get_Current_Parent_Total_Calls( void ) { return CurrentParent->Get_Total_Calls(); }
125 float Get_Current_Parent_Total_Time( void ) { return CurrentParent->Get_Total_Time(); }
131 CProfileNode * CurrentParent;
132 CProfileNode * CurrentChild;
135 CProfileIterator( CProfileNode * start );
136 friend class CProfileManager;
140 ///The Manager for the Profile system
141 class CProfileManager {
143 static void Start_Profile( const char * name );
144 static void Stop_Profile( void );
146 static void CleanupMemory(void)
148 Root.CleanupMemory();
151 static void Reset( void );
152 static void Increment_Frame_Counter( void );
153 static int Get_Frame_Count_Since_Reset( void ) { return FrameCounter; }
154 static float Get_Time_Since_Reset( void );
156 static CProfileIterator * Get_Iterator( void )
159 return new CProfileIterator( &Root );
161 static void Release_Iterator( CProfileIterator * iterator ) { delete ( iterator); }
163 static void dumpRecursive(CProfileIterator* profileIterator, int spacing);
165 static void dumpAll();
168 static CProfileNode Root;
169 static CProfileNode * CurrentNode;
170 static int FrameCounter;
171 static unsigned long int ResetTime;
175 ///ProfileSampleClass is a simple way to profile a function's scope
176 ///Use the BT_PROFILE macro at the start of scope to time
177 class CProfileSample {
179 CProfileSample( const char * name )
181 CProfileManager::Start_Profile( name );
184 ~CProfileSample( void )
186 CProfileManager::Stop_Profile();
191 #define BT_PROFILE( name ) CProfileSample __profile( name )
195 #define BT_PROFILE( name )
197 #endif //#ifndef BT_NO_PROFILE
201 #endif //BT_QUICK_PROF_H