Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/bullet/LinearMath/btQuickprof.cpp @ 1963

Last change on this file since 1963 was 1963, checked in by rgrieder, 16 years ago

Added Bullet physics engine.

  • Property svn:eol-style set to native
File size: 8.7 KB
Line 
1/*
2
3/***************************************************************************************************
4**
5** profile.cpp
6**
7** Real-Time Hierarchical Profiling for Game Programming Gems 3
8**
9** by Greg Hjelstrom & Byon Garrabrant
10**
11***************************************************************************************************/
12
13// Credits: The Clock class was inspired by the Timer classes in
14// Ogre (www.ogre3d.org).
15
16#include "LinearMath/btQuickprof.h"
17
18
19#ifdef USE_BT_CLOCK
20
21static btClock gProfileClock;
22
23inline void Profile_Get_Ticks(unsigned long int * ticks)
24{
25        *ticks = gProfileClock.getTimeMicroseconds();
26}
27
28inline float Profile_Get_Tick_Rate(void)
29{
30//      return 1000000.f;
31        return 1000.f;
32
33}
34
35
36
37/***************************************************************************************************
38**
39** CProfileNode
40**
41***************************************************************************************************/
42
43/***********************************************************************************************
44 * INPUT:                                                                                      *
45 * name - pointer to a static string which is the name of this profile node                    *
46 * parent - parent pointer                                                                     *
47 *                                                                                             *
48 * WARNINGS:                                                                                   *
49 * The name is assumed to be a static pointer, only the pointer is stored and compared for     *
50 * efficiency reasons.                                                                         *
51 *=============================================================================================*/
52CProfileNode::CProfileNode( const char * name, CProfileNode * parent ) :
53        Name( name ),
54        TotalCalls( 0 ),
55        TotalTime( 0 ),
56        StartTime( 0 ),
57        RecursionCounter( 0 ),
58        Parent( parent ),
59        Child( NULL ),
60        Sibling( NULL )
61{
62        Reset();
63}
64
65
66void    CProfileNode::CleanupMemory()
67{
68        delete ( Child);
69        Child = NULL;
70        delete ( Sibling);
71        Sibling = NULL;
72}
73
74CProfileNode::~CProfileNode( void )
75{
76        delete ( Child);
77        delete ( Sibling);
78}
79
80
81/***********************************************************************************************
82 * INPUT:                                                                                      *
83 * name - static string pointer to the name of the node we are searching for                   *
84 *                                                                                             *
85 * WARNINGS:                                                                                   *
86 * All profile names are assumed to be static strings so this function uses pointer compares   *
87 * to find the named node.                                                                     *
88 *=============================================================================================*/
89CProfileNode * CProfileNode::Get_Sub_Node( const char * name )
90{
91        // Try to find this sub node
92        CProfileNode * child = Child;
93        while ( child ) {
94                if ( child->Name == name ) {
95                        return child;
96                }
97                child = child->Sibling;
98        }
99
100        // We didn't find it, so add it
101       
102        CProfileNode * node = new CProfileNode( name, this );
103        node->Sibling = Child;
104        Child = node;
105        return node;
106}
107
108
109void    CProfileNode::Reset( void )
110{
111        TotalCalls = 0;
112        TotalTime = 0.0f;
113        gProfileClock.reset();
114
115        if ( Child ) {
116                Child->Reset();
117        }
118        if ( Sibling ) {
119                Sibling->Reset();
120        }
121}
122
123
124void    CProfileNode::Call( void )
125{
126        TotalCalls++;
127        if (RecursionCounter++ == 0) {
128                Profile_Get_Ticks(&StartTime);
129        }
130}
131
132
133bool    CProfileNode::Return( void )
134{
135        if ( --RecursionCounter == 0 && TotalCalls != 0 ) { 
136                unsigned long int time;
137                Profile_Get_Ticks(&time);
138                time-=StartTime;
139                TotalTime += (float)time / Profile_Get_Tick_Rate();
140        }
141        return ( RecursionCounter == 0 );
142}
143
144
145/***************************************************************************************************
146**
147** CProfileIterator
148**
149***************************************************************************************************/
150CProfileIterator::CProfileIterator( CProfileNode * start )
151{
152        CurrentParent = start;
153        CurrentChild = CurrentParent->Get_Child();
154}
155
156
157void    CProfileIterator::First(void)
158{
159        CurrentChild = CurrentParent->Get_Child();
160}
161
162
163void    CProfileIterator::Next(void)
164{
165        CurrentChild = CurrentChild->Get_Sibling();
166}
167
168
169bool    CProfileIterator::Is_Done(void)
170{
171        return CurrentChild == NULL;
172}
173
174
175void    CProfileIterator::Enter_Child( int index )
176{
177        CurrentChild = CurrentParent->Get_Child();
178        while ( (CurrentChild != NULL) && (index != 0) ) {
179                index--;
180                CurrentChild = CurrentChild->Get_Sibling();
181        }
182
183        if ( CurrentChild != NULL ) {
184                CurrentParent = CurrentChild;
185                CurrentChild = CurrentParent->Get_Child();
186        }
187}
188
189
190void    CProfileIterator::Enter_Parent( void )
191{
192        if ( CurrentParent->Get_Parent() != NULL ) {
193                CurrentParent = CurrentParent->Get_Parent();
194        }
195        CurrentChild = CurrentParent->Get_Child();
196}
197
198
199/***************************************************************************************************
200**
201** CProfileManager
202**
203***************************************************************************************************/
204
205CProfileNode    CProfileManager::Root( "Root", NULL );
206CProfileNode *  CProfileManager::CurrentNode = &CProfileManager::Root;
207int                             CProfileManager::FrameCounter = 0;
208unsigned long int                       CProfileManager::ResetTime = 0;
209
210
211/***********************************************************************************************
212 * CProfileManager::Start_Profile -- Begin a named profile                                    *
213 *                                                                                             *
214 * Steps one level deeper into the tree, if a child already exists with the specified name     *
215 * then it accumulates the profiling; otherwise a new child node is added to the profile tree. *
216 *                                                                                             *
217 * INPUT:                                                                                      *
218 * name - name of this profiling record                                                        *
219 *                                                                                             *
220 * WARNINGS:                                                                                   *
221 * The string used is assumed to be a static string; pointer compares are used throughout      *
222 * the profiling code for efficiency.                                                          *
223 *=============================================================================================*/
224void    CProfileManager::Start_Profile( const char * name )
225{
226        if (name != CurrentNode->Get_Name()) {
227                CurrentNode = CurrentNode->Get_Sub_Node( name );
228        } 
229       
230        CurrentNode->Call();
231}
232
233
234/***********************************************************************************************
235 * CProfileManager::Stop_Profile -- Stop timing and record the results.                       *
236 *=============================================================================================*/
237void    CProfileManager::Stop_Profile( void )
238{
239        // Return will indicate whether we should back up to our parent (we may
240        // be profiling a recursive function)
241        if (CurrentNode->Return()) {
242                CurrentNode = CurrentNode->Get_Parent();
243        }
244}
245
246
247/***********************************************************************************************
248 * CProfileManager::Reset -- Reset the contents of the profiling system                       *
249 *                                                                                             *
250 *    This resets everything except for the tree structure.  All of the timing data is reset.  *
251 *=============================================================================================*/
252void    CProfileManager::Reset( void )
253{ 
254        Root.Reset();
255    Root.Call();
256        FrameCounter = 0;
257        Profile_Get_Ticks(&ResetTime);
258}
259
260
261/***********************************************************************************************
262 * CProfileManager::Increment_Frame_Counter -- Increment the frame counter                    *
263 *=============================================================================================*/
264void CProfileManager::Increment_Frame_Counter( void )
265{
266        FrameCounter++;
267}
268
269
270/***********************************************************************************************
271 * CProfileManager::Get_Time_Since_Reset -- returns the elapsed time since last reset         *
272 *=============================================================================================*/
273float CProfileManager::Get_Time_Since_Reset( void )
274{
275        unsigned long int time;
276        Profile_Get_Ticks(&time);
277        time -= ResetTime;
278        return (float)time / Profile_Get_Tick_Rate();
279}
280
281#endif //USE_BT_CLOCK
282
Note: See TracBrowser for help on using the repository browser.