Bullet Collision Detection & Physics Library
btQuickprof.h
Go to the documentation of this file.
1 
2 /***************************************************************************************************
3 **
4 ** Real-Time Hierarchical Profiling for Game Programming Gems 3
5 **
6 ** by Greg Hjelstrom & Byon Garrabrant
7 **
8 ***************************************************************************************************/
9 
10 // Credits: The Clock class was inspired by the Timer classes in
11 // Ogre (www.ogre3d.org).
12 
13 
14 
15 #ifndef BT_QUICK_PROF_H
16 #define BT_QUICK_PROF_H
17 
18 #include "btScalar.h"
19 #define USE_BT_CLOCK 1
20 
21 #ifdef USE_BT_CLOCK
22 
24 class btClock
25 {
26 public:
27  btClock();
28 
29  btClock(const btClock& other);
30  btClock& operator=(const btClock& other);
31 
32  ~btClock();
33 
35  void reset();
36 
39  unsigned long int getTimeMilliseconds();
40 
43  unsigned long int getTimeMicroseconds();
44 
48 
49 private:
51 };
52 
53 #endif //USE_BT_CLOCK
54 
55 
56 //To disable built-in profiling, please comment out next line
57 #define BT_NO_PROFILE 1
58 #ifndef BT_NO_PROFILE
59 #include <stdio.h>//@todo remove this, backwards compatibility
60 
61 #include "btAlignedAllocator.h"
62 #include <new>
63 
64 
65 
66 
67 
68 
69 
70 
71 
73 class CProfileNode {
74 
75 public:
76  CProfileNode( const char * name, CProfileNode * parent );
77  ~CProfileNode( void );
78 
79  CProfileNode * Get_Sub_Node( const char * name );
80 
81  CProfileNode * Get_Parent( void ) { return Parent; }
82  CProfileNode * Get_Sibling( void ) { return Sibling; }
83  CProfileNode * Get_Child( void ) { return Child; }
84 
85  void CleanupMemory();
86  void Reset( void );
87  void Call( void );
88  bool Return( void );
89 
90  const char * Get_Name( void ) { return Name; }
91  int Get_Total_Calls( void ) { return TotalCalls; }
92  float Get_Total_Time( void ) { return TotalTime; }
93  void* GetUserPointer() const {return m_userPtr;}
94  void SetUserPointer(void* ptr) { m_userPtr = ptr;}
95 protected:
96 
97  const char * Name;
98  int TotalCalls;
99  float TotalTime;
100  unsigned long int StartTime;
101  int RecursionCounter;
102 
103  CProfileNode * Parent;
104  CProfileNode * Child;
105  CProfileNode * Sibling;
106  void* m_userPtr;
107 };
108 
110 class CProfileIterator
111 {
112 public:
113  // Access all the children of the current parent
114  void First(void);
115  void Next(void);
116  bool Is_Done(void);
117  bool Is_Root(void) { return (CurrentParent->Get_Parent() == 0); }
118 
119  void Enter_Child( int index ); // Make the given child the new parent
120  void Enter_Largest_Child( void ); // Make the largest child the new parent
121  void Enter_Parent( void ); // Make the current parent's parent the new parent
122 
123  // Access the current child
124  const char * Get_Current_Name( void ) { return CurrentChild->Get_Name(); }
125  int Get_Current_Total_Calls( void ) { return CurrentChild->Get_Total_Calls(); }
126  float Get_Current_Total_Time( void ) { return CurrentChild->Get_Total_Time(); }
127 
128  void* Get_Current_UserPointer( void ) { return CurrentChild->GetUserPointer(); }
129  void Set_Current_UserPointer(void* ptr) {CurrentChild->SetUserPointer(ptr);}
130  // Access the current parent
131  const char * Get_Current_Parent_Name( void ) { return CurrentParent->Get_Name(); }
132  int Get_Current_Parent_Total_Calls( void ) { return CurrentParent->Get_Total_Calls(); }
133  float Get_Current_Parent_Total_Time( void ) { return CurrentParent->Get_Total_Time(); }
134 
135 
136 
137 protected:
138 
139  CProfileNode * CurrentParent;
140  CProfileNode * CurrentChild;
141 
142 
143  CProfileIterator( CProfileNode * start );
144  friend class CProfileManager;
145 };
146 
147 
149 class CProfileManager {
150 public:
151  static void Start_Profile( const char * name );
152  static void Stop_Profile( void );
153 
154  static void CleanupMemory(void)
155  {
156  Root.CleanupMemory();
157  }
158 
159  static void Reset( void );
160  static void Increment_Frame_Counter( void );
161  static int Get_Frame_Count_Since_Reset( void ) { return FrameCounter; }
162  static float Get_Time_Since_Reset( void );
163 
164  static CProfileIterator * Get_Iterator( void )
165  {
166 
167  return new CProfileIterator( &Root );
168  }
169  static void Release_Iterator( CProfileIterator * iterator ) { delete ( iterator); }
170 
171  static void dumpRecursive(CProfileIterator* profileIterator, int spacing);
172 
173  static void dumpAll();
174 
175 private:
176  static CProfileNode Root;
177  static CProfileNode * CurrentNode;
178  static int FrameCounter;
179  static unsigned long int ResetTime;
180 };
181 
182 
185 class CProfileSample {
186 public:
187  CProfileSample( const char * name )
188  {
189  CProfileManager::Start_Profile( name );
190  }
191 
192  ~CProfileSample( void )
193  {
194  CProfileManager::Stop_Profile();
195  }
196 };
197 
198 
199 #define BT_PROFILE( name ) CProfileSample __profile( name )
200 
201 #else
202 
203 #define BT_PROFILE( name )
204 
205 #endif //#ifndef BT_NO_PROFILE
206 
207 
208 
209 #endif //BT_QUICK_PROF_H
210 
211 
btScalar getTimeSeconds()
Returns the time in s since the last call to reset or since the Clock was created.
unsigned long int getTimeMicroseconds()
Returns the time in us since the last call to reset or since the Clock was created.
The btClock is a portable basic clock that measures accurate time in seconds, use for profiling...
Definition: btQuickprof.h:24
void reset()
Resets the initial reference time.
unsigned long int getTimeMilliseconds()
Returns the time in ms since the last call to reset or since the btClock was created.
btClock()
The btClock is a portable basic clock that measures accurate time in seconds, use for profiling...
Definition: btQuickprof.cpp:78
struct btClockData * m_data
Definition: btQuickprof.h:50
btClock & operator=(const btClock &other)
Definition: btQuickprof.cpp:98
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:279