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 #ifndef BT_QUICK_PROF_H
14 #define BT_QUICK_PROF_H
15 
16 #include "btScalar.h"
17 #define USE_BT_CLOCK 1
18 
19 #ifdef USE_BT_CLOCK
20 
22 class btClock
23 {
24 public:
25  btClock();
26 
27  btClock(const btClock& other);
28  btClock& operator=(const btClock& other);
29 
30  ~btClock();
31 
33  void reset();
34 
37  unsigned long long int getTimeMilliseconds();
38 
41  unsigned long long int getTimeMicroseconds();
42 
43  unsigned long long int getTimeNanoseconds();
44 
48 
49 private:
51 };
52 
53 #endif //USE_BT_CLOCK
54 
55 typedef void(btEnterProfileZoneFunc)(const char* msg);
56 typedef void(btLeaveProfileZoneFunc)();
57 
60 
63 
64 #ifndef BT_ENABLE_PROFILE
65 #define BT_NO_PROFILE 1
66 #endif //BT_NO_PROFILE
67 
68 const unsigned int BT_QUICKPROF_MAX_THREAD_COUNT = 64;
69 
70 //btQuickprofGetCurrentThreadIndex will return -1 if thread index cannot be determined,
71 //otherwise returns thread index in range [0..maxThreads]
73 
74 #ifndef BT_NO_PROFILE
75 
76 
77 #include <stdio.h> //@todo remove this, backwards compatibility
78 
79 #include "btAlignedAllocator.h"
80 #include <new>
81 
83 class CProfileNode
84 {
85 public:
86  CProfileNode(const char* name, CProfileNode* parent);
87  ~CProfileNode(void);
88 
89  CProfileNode* Get_Sub_Node(const char* name);
90 
91  CProfileNode* Get_Parent(void) { return Parent; }
92  CProfileNode* Get_Sibling(void) { return Sibling; }
93  CProfileNode* Get_Child(void) { return Child; }
94 
95  void CleanupMemory();
96  void Reset(void);
97  void Call(void);
98  bool Return(void);
99 
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; }
105 
106 protected:
107  const char* Name;
108  int TotalCalls;
109  float TotalTime;
110  unsigned long int StartTime;
111  int RecursionCounter;
112 
113  CProfileNode* Parent;
114  CProfileNode* Child;
115  CProfileNode* Sibling;
116  void* m_userPtr;
117 };
118 
120 class CProfileIterator
121 {
122 public:
123  // Access all the children of the current parent
124  void First(void);
125  void Next(void);
126  bool Is_Done(void);
127  bool Is_Root(void) { return (CurrentParent->Get_Parent() == 0); }
128 
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
132 
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(); }
137 
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(); }
144 
145 protected:
146  CProfileNode* CurrentParent;
147  CProfileNode* CurrentChild;
148 
149  CProfileIterator(CProfileNode* start);
150  friend class CProfileManager;
151 };
152 
154 class CProfileManager
155 {
156 public:
157  static void Start_Profile(const char* name);
158  static void Stop_Profile(void);
159 
160  static void CleanupMemory(void);
161  // {
162  // Root.CleanupMemory();
163  // }
164 
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);
169 
170  static CProfileIterator* Get_Iterator(void);
171  // {
172  //
173  // return new CProfileIterator( &Root );
174  // }
175  static void Release_Iterator(CProfileIterator* iterator) { delete (iterator); }
176 
177  static void dumpRecursive(CProfileIterator* profileIterator, int spacing);
178 
179  static void dumpAll();
180 
181 private:
182  static int FrameCounter;
183  static unsigned long int ResetTime;
184 };
185 
186 #endif //#ifndef BT_NO_PROFILE
187 
191 {
192 public:
193  CProfileSample(const char* name);
194 
195  ~CProfileSample(void);
196 };
197 
198 #define BT_PROFILE(name) CProfileSample __profile(name)
199 
200 #endif //BT_QUICK_PROF_H
void() btLeaveProfileZoneFunc()
Definition: btQuickprof.h:56
void btSetCustomEnterProfileZoneFunc(btEnterProfileZoneFunc *enterFunc)
btLeaveProfileZoneFunc * btGetCurrentLeaveProfileZoneFunc()
void btSetCustomLeaveProfileZoneFunc(btLeaveProfileZoneFunc *leaveFunc)
const unsigned int BT_QUICKPROF_MAX_THREAD_COUNT
Definition: btQuickprof.h:68
unsigned int btQuickprofGetCurrentThreadIndex2()
void() btEnterProfileZoneFunc(const char *msg)
Definition: btQuickprof.h:55
btEnterProfileZoneFunc * btGetCurrentEnterProfileZoneFunc()
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
ProfileSampleClass is a simple way to profile a function's scope Use the BT_PROFILE macro at the star...
Definition: btQuickprof.h:191
CProfileSample(const char *name)
The btClock is a portable basic clock that measures accurate time in seconds, use for profiling.
Definition: btQuickprof.h:23
btScalar getTimeSeconds()
Returns the time in s since the last call to reset or since the Clock was created.
unsigned long long int getTimeNanoseconds()
void reset()
Resets the initial reference time.
struct btClockData * m_data
Definition: btQuickprof.h:50
unsigned long long int getTimeMilliseconds()
Returns the time in ms since the last call to reset or since the btClock was created.
unsigned long long int getTimeMicroseconds()
Returns the time in us since the last call to reset or since the Clock was created.
btClock()
The btClock is a portable basic clock that measures accurate time in seconds, use for profiling.
Definition: btQuickprof.cpp:85
btClock & operator=(const btClock &other)