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).
13 #ifndef BT_QUICK_PROF_H
14 #define BT_QUICK_PROF_H
17 #define USE_BT_CLOCK 1
21 ///The btClock is a portable basic clock that measures accurate time in seconds, use for profiling.
27 btClock(const btClock& other);
28 btClock& operator=(const btClock& other);
32 /// Resets the initial reference time.
35 /// Returns the time in ms since the last call to reset or since
36 /// the btClock was created.
37 unsigned long long int getTimeMilliseconds();
39 /// Returns the time in us since the last call to reset or since
40 /// the Clock was created.
41 unsigned long long int getTimeMicroseconds();
43 unsigned long long int getTimeNanoseconds();
45 /// Returns the time in s since the last call to reset or since
46 /// the Clock was created.
47 btScalar getTimeSeconds();
50 struct btClockData* m_data;
55 typedef void(btEnterProfileZoneFunc)(const char* msg);
56 typedef void(btLeaveProfileZoneFunc)();
58 btEnterProfileZoneFunc* btGetCurrentEnterProfileZoneFunc();
59 btLeaveProfileZoneFunc* btGetCurrentLeaveProfileZoneFunc();
61 void btSetCustomEnterProfileZoneFunc(btEnterProfileZoneFunc* enterFunc);
62 void btSetCustomLeaveProfileZoneFunc(btLeaveProfileZoneFunc* leaveFunc);
64 #ifndef BT_ENABLE_PROFILE
65 #define BT_NO_PROFILE 1
66 #endif //BT_NO_PROFILE
68 const unsigned int BT_QUICKPROF_MAX_THREAD_COUNT = 64;
70 //btQuickprofGetCurrentThreadIndex will return -1 if thread index cannot be determined,
71 //otherwise returns thread index in range [0..maxThreads]
72 unsigned int btQuickprofGetCurrentThreadIndex2();
77 #include <stdio.h> //@todo remove this, backwards compatibility
79 #include "btAlignedAllocator.h"
82 ///A node in the Profile Hierarchy Tree
86 CProfileNode(const char* name, CProfileNode* parent);
89 CProfileNode* Get_Sub_Node(const char* name);
91 CProfileNode* Get_Parent(void) { return Parent; }
92 CProfileNode* Get_Sibling(void) { return Sibling; }
93 CProfileNode* Get_Child(void) { return Child; }
100 const char* Get_Name(void) { return Name; }
101 int Get_Total_Calls(void) { return TotalCalls; }
102 float Get_Total_Time(void) { return TotalTime; }
103 void* GetUserPointer() const { return m_userPtr; }
104 void SetUserPointer(void* ptr) { m_userPtr = ptr; }
110 unsigned long int StartTime;
111 int RecursionCounter;
113 CProfileNode* Parent;
115 CProfileNode* Sibling;
119 ///An iterator to navigate through the tree
120 class CProfileIterator
123 // Access all the children of the current parent
127 bool Is_Root(void) { return (CurrentParent->Get_Parent() == 0); }
129 void Enter_Child(int index); // Make the given child the new parent
130 void Enter_Largest_Child(void); // Make the largest child the new parent
131 void Enter_Parent(void); // Make the current parent's parent the new parent
133 // Access the current child
134 const char* Get_Current_Name(void) { return CurrentChild->Get_Name(); }
135 int Get_Current_Total_Calls(void) { return CurrentChild->Get_Total_Calls(); }
136 float Get_Current_Total_Time(void) { return CurrentChild->Get_Total_Time(); }
138 void* Get_Current_UserPointer(void) { return CurrentChild->GetUserPointer(); }
139 void Set_Current_UserPointer(void* ptr) { CurrentChild->SetUserPointer(ptr); }
140 // Access the current parent
141 const char* Get_Current_Parent_Name(void) { return CurrentParent->Get_Name(); }
142 int Get_Current_Parent_Total_Calls(void) { return CurrentParent->Get_Total_Calls(); }
143 float Get_Current_Parent_Total_Time(void) { return CurrentParent->Get_Total_Time(); }
146 CProfileNode* CurrentParent;
147 CProfileNode* CurrentChild;
149 CProfileIterator(CProfileNode* start);
150 friend class CProfileManager;
153 ///The Manager for the Profile system
154 class CProfileManager
157 static void Start_Profile(const char* name);
158 static void Stop_Profile(void);
160 static void CleanupMemory(void);
162 // Root.CleanupMemory();
165 static void Reset(void);
166 static void Increment_Frame_Counter(void);
167 static int Get_Frame_Count_Since_Reset(void) { return FrameCounter; }
168 static float Get_Time_Since_Reset(void);
170 static CProfileIterator* Get_Iterator(void);
173 // return new CProfileIterator( &Root );
175 static void Release_Iterator(CProfileIterator* iterator) { delete (iterator); }
177 static void dumpRecursive(CProfileIterator* profileIterator, int spacing);
179 static void dumpAll();
182 static int FrameCounter;
183 static unsigned long int ResetTime;
186 #endif //#ifndef BT_NO_PROFILE
188 ///ProfileSampleClass is a simple way to profile a function's scope
189 ///Use the BT_PROFILE macro at the start of scope to time
193 CProfileSample(const char* name);
195 ~CProfileSample(void);
198 #define BT_PROFILE(name) CProfileSample __profile(name)
200 #endif //BT_QUICK_PROF_H