-
Notifications
You must be signed in to change notification settings - Fork 2
/
Profile.cpp
379 lines (305 loc) · 11.2 KB
/
Profile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//-----------------------------------------------------------------------------
// File: Profile.h
//
// Desc: This software is provided "as is" without express or implied
// warranties. You may freely copy and compile this source into
// applications you distribute provided that the copyright text
// below is included in the resulting source code, for example:
// "Portions Copyright (C) Steve Rabin, 2000"
//
// Copyright (C) Steve Rabin, 2000. | Modified: Dan 2002
//-----------------------------------------------------------------------------
#include "profile.h"
#include "custom_time.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "main.h"
#include "OpenGL.h"
//-----------------------------------------------------------------------------
// Name: CProfile()
// Desc:
//-----------------------------------------------------------------------------
CProfile::CProfile()
{
Log( "CProfile::CProfile()\n" );
unsigned int i;
for( i = 0; i < NUM_PROFILE_SAMPLES; i++ )
{
m_samples[i].bValid = false;
m_history[i].bValid = false;
}
m_startProfile = GetExactTime();
m_endProfile = 0.0f;
// m_strBuffer = "";
}
//-----------------------------------------------------------------------------
// Name: Begin()
// Desc:
//-----------------------------------------------------------------------------
void CProfile::Begin( char* name )
{
unsigned int i = 0;
while( i < NUM_PROFILE_SAMPLES && m_samples[i].bValid == true )
{
if( strcmp( m_samples[i].szName, name ) == 0 )
{
//Found the sample
m_samples[i].iOpenProfiles++;
m_samples[i].iProfileInstances++;
m_samples[i].fStartTime = GetExactTime();
assert( m_samples[i].iOpenProfiles == 1 ); //max 1 open at once
return;
}
i++;
}
if( i >= NUM_PROFILE_SAMPLES )
{
assert( !"Exceeded Max Available Profile Samples" );
return;
}
strcpy( m_samples[i].szName, name );
m_samples[i].bValid = true;
m_samples[i].iOpenProfiles = 1;
m_samples[i].iProfileInstances = 1;
m_samples[i].fAccumulator = 0.0f;
m_samples[i].fStartTime = GetExactTime();
m_samples[i].fChildrenSampleTime = 0.0f;
}
//-----------------------------------------------------------------------------
// Name: End()
// Desc:
//-----------------------------------------------------------------------------
void CProfile::End( char* name )
{
unsigned int i = 0;
unsigned int numParents = 0;
while( i < NUM_PROFILE_SAMPLES && m_samples[i].bValid == true )
{
if( strcmp( m_samples[i].szName, name ) == 0 )
{
//Found the sample
unsigned int inner = 0;
int parent = -1;
float fEndTime = GetExactTime();
m_samples[i].iOpenProfiles--;
//Count all parents and find the immediate parent
while( m_samples[inner].bValid == true )
{
if( m_samples[inner].iOpenProfiles > 0 )
{
//Found a parent (any open profiles are parents)
numParents++;
if( parent < 0 )
{
//Replace invalid parent (index)
parent = inner;
}
else if( m_samples[inner].fStartTime >= m_samples[parent].fStartTime )
{
//Replace with more immediate parent
parent = inner;
}
}
inner++;
}
//Remember the current number of parents of the sample
m_samples[i].iNumParents = numParents;
if( parent >= 0 )
{
//Record this time in fChildrenSampleTime (add it in)
m_samples[parent].fChildrenSampleTime += fEndTime -
m_samples[i].fStartTime;
}
//Save sample time in accumulator
m_samples[i].fAccumulator += fEndTime - m_samples[i].fStartTime;
return;
}
i++;
}
}
//-----------------------------------------------------------------------------
// Name: DumpOutputToBuffer()
// Desc:
//-----------------------------------------------------------------------------
void CProfile::DumpOutputToFile()
{
unsigned int i = 0;
m_endProfile = GetExactTime();
Log( " Ave : Min : Max : # : Profile Name\n" );
Log( "--------------------------------------------\n" );
while( i < NUM_PROFILE_SAMPLES && m_samples[i].bValid == true )
{
unsigned int indent = 0;
float sampleTime, percentTime, aveTime, minTime, maxTime;
char line[256], name[256], indentedName[256];
char ave[16], min[16], max[16], num[16];
if( m_samples[i].iOpenProfiles < 0 )
{
assert( !"ProfileEnd() called without a ProfileBegin()" );
}
else if( m_samples[i].iOpenProfiles > 0 )
{
assert( !"ProfileBegin() called without a ProfileEnd()" );
}
sampleTime = m_samples[i].fAccumulator - m_samples[i].fChildrenSampleTime;
percentTime = ( sampleTime / (m_endProfile - m_startProfile ) ) * 100.0f;
aveTime = minTime = maxTime = percentTime;
//Add new measurement into the history and get the ave, min, and max
StoreInHistory( m_samples[i].szName, percentTime );
GetFromHistory( m_samples[i].szName, &aveTime, &minTime, &maxTime );
//Format the data
sprintf( ave, "%3.1f", aveTime );
sprintf( min, "%3.1f", minTime );
sprintf( max, "%3.1f", maxTime );
sprintf( num, "%3d", m_samples[i].iProfileInstances );
strcpy( indentedName, m_samples[i].szName );
for( indent=0; indent < m_samples[i].iNumParents; indent++ )
{
sprintf( name, " %s", indentedName );
strcpy( indentedName, name );
}
sprintf( line,"%5s : %5s : %5s : %3s : %s\n", ave, min, max, num, indentedName );
Log( line ); //Send the line to text buffer
i++;
}
{
//Reset samples for next frame
unsigned int i;
for( i = 0; i < NUM_PROFILE_SAMPLES; i++ )
m_samples[i].bValid = false;
m_startProfile = GetExactTime();
}
}
//-----------------------------------------------------------------------------
// Name: StoreInHistory()
// Desc:
//-----------------------------------------------------------------------------
void CProfile::StoreInHistory( char* name, float percent )
{
unsigned int i = 0;
float oldRatio;
float newRatio = 0.8f * GetElapsedTime();
if( newRatio > 1.0f )
newRatio = 1.0f;
oldRatio = 1.0f - newRatio;
while( i < NUM_PROFILE_SAMPLES && m_history[i].bValid == true )
{
if( strcmp( m_history[i].szName, name ) == 0 )
{
//Found the sample
m_history[i].fAve = ( m_history[i].fAve * oldRatio ) + ( percent * newRatio );
if( percent < m_history[i].fMin )
{
m_history[i].fMin = percent;
}
else
{
m_history[i].fMin = ( m_history[i].fMin * oldRatio ) + ( percent * newRatio );
}
if( m_history[i].fMin < 0.0f )
m_history[i].fMin = 0.0f;
if( percent > m_history[i].fMax )
m_history[i].fMax = percent;
else
m_history[i].fMax = ( m_history[i].fMax * oldRatio ) + ( percent * newRatio );
return;
}
i++;
}
if( i < NUM_PROFILE_SAMPLES )
{
//Add to history
strcpy( m_history[i].szName, name );
m_history[i].bValid = true;
m_history[i].fAve = m_history[i].fMin = m_history[i].fMax = percent;
}
else
{
assert( !"Exceeded Max Available Profile Samples!");
}
}
//-----------------------------------------------------------------------------
// Name: GetFromHistory()
// Desc:
//-----------------------------------------------------------------------------
void CProfile::GetFromHistory( char* name, float* ave, float* min, float* max )
{
unsigned int i = 0;
while( i < NUM_PROFILE_SAMPLES && m_history[i].bValid == true )
{
if( strcmp( m_history[i].szName, name ) == 0 )
{
//Found the sample
*ave = m_history[i].fAve;
*min = m_history[i].fMin;
*max = m_history[i].fMax;
return;
}
i++;
}
*ave = *min = *max = 0.0f;
}
//-----------------------------------------------------------------------------
// Name: Draw()
// Desc:
//-----------------------------------------------------------------------------
void CProfile::CopyBufferToScreen()
{
OpenGL->DrawGLText( 50, 50, " Ave : Min : Max : # : Profile Name" );
OpenGL->DrawGLText( 50, 75, "--------------------------------------------" );
OpenGL->DrawGLText( 50, 100, m_strBuffer );
Log( m_strBuffer );
Log( "\n" );
}
//-----------------------------------------------------------------------------
// Name: DumpOutputToBuffer()
// Desc:
//-----------------------------------------------------------------------------
void CProfile::DumpOutputToBuffer()
{
unsigned int i = 0;
m_endProfile = GetExactTime();
while( i < NUM_PROFILE_SAMPLES && m_samples[i].bValid == true )
{
unsigned int indent = 0;
float sampleTime, percentTime, aveTime, minTime, maxTime;
char line[256], name[256], indentedName[256];
char ave[16], min[16], max[16], num[16];
if( m_samples[i].iOpenProfiles < 0 )
{
assert( !"ProfileEnd() called without a ProfileBegin()" );
}
else if( m_samples[i].iOpenProfiles > 0 )
{
assert( !"ProfileBegin() called without a ProfileEnd()" );
}
sampleTime = m_samples[i].fAccumulator - m_samples[i].fChildrenSampleTime;
percentTime = ( sampleTime / (m_endProfile - m_startProfile ) ) * 100.0f;
aveTime = minTime = maxTime = percentTime;
//Add new measurement into the history and get the ave, min, and max
StoreInHistory( m_samples[i].szName, percentTime );
GetFromHistory( m_samples[i].szName, &aveTime, &minTime, &maxTime );
//Format the data
sprintf( ave, "%3.1f", aveTime );
sprintf( min, "%3.1f", minTime );
sprintf( max, "%3.1f", maxTime );
sprintf( num, "%3d", m_samples[i].iProfileInstances );
strcpy( indentedName, m_samples[i].szName );
for( indent=0; indent < m_samples[i].iNumParents; indent++ )
{
sprintf( name, " %s", indentedName );
strcpy( indentedName, name );
}
sprintf( line,"%5s : %5s : %5s : %3s : %s\n", ave, min, max, num, indentedName );
strcpy( m_strBuffer, line );
i++;
}
{
//Reset samples for next frame
unsigned int i;
for( i = 0; i < NUM_PROFILE_SAMPLES; i++ )
m_samples[i].bValid = false;
m_startProfile = GetExactTime();
}
}