aboutsummaryrefslogtreecommitdiff
path: root/sp/src/public/measure_section.cpp
blob: e43a16ac7186104549d0edb2bcae761d84dfc29b (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $Workfile:     $
// $Date:         $
//
//-----------------------------------------------------------------------------
// $Log: $
//
// $NoKeywords: $
//=============================================================================//
#include <string.h>
#include <stdlib.h>
#include "basetypes.h"
#include "measure_section.h"
#include "convar.h"


// Static members
CMeasureSection *CMeasureSection::s_pSections = 0;
int	CMeasureSection::s_nCount = 0;
double CMeasureSection::m_dNextResort = 0.0;

ConVar	measure_resort( "measure_resort", "1.0", 0, "How often to re-sort profiling sections\n" );
ConVar	game_speeds( "game_speeds","0" );
extern ConVar host_speeds;

//-----------------------------------------------------------------------------
// Purpose: Creates a profiling section
// Input  : *name - name of the section ( allocated on stack hopefully )
//-----------------------------------------------------------------------------
CMeasureSection::CMeasureSection( const char *name )
{
	// Just point at name since it's static
	m_pszName		= name;
	
	// Clear accumulators
	Reset();
	SortReset();
	m_dMaxTime.Init();

	// Link into master list
	m_pNext			= s_pSections;
	s_pSections		= this;
	// Update count
	s_nCount++;
}

//-----------------------------------------------------------------------------
// Purpose: Destroys the object
//-----------------------------------------------------------------------------
CMeasureSection::~CMeasureSection( void )
{
	m_pNext = NULL;
	s_nCount--;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CMeasureSection::UpdateMax( void )
{
	if (m_dMaxTime.IsLessThan(m_dAccumulatedTime))
	{
		m_dMaxTime.Init();
		CCycleCount::Add(m_dMaxTime,m_dAccumulatedTime,m_dMaxTime);
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CMeasureSection::Reset( void )
{
	m_dAccumulatedTime.Init();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CMeasureSection::SortReset( void )
{
	m_dTotalTime.Init();
}

//-----------------------------------------------------------------------------
// Purpose: 
// Output : const char
//-----------------------------------------------------------------------------
const char *CMeasureSection::GetName( void )
{
	return m_pszName;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Output : 
//-----------------------------------------------------------------------------
CCycleCount const& CMeasureSection::GetTotalTime( void )
{
	return m_dTotalTime;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Output : 
//-----------------------------------------------------------------------------
CCycleCount const& CMeasureSection::GetTime( void )
{
	return m_dAccumulatedTime;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Output : 
//-----------------------------------------------------------------------------
CCycleCount const& CMeasureSection::GetMaxTime( void )
{
	return m_dMaxTime;
}

//-----------------------------------------------------------------------------
// Purpose: Accumulates a timeslice
// Input  : time - 
//-----------------------------------------------------------------------------
void CMeasureSection::AddTime( CCycleCount const &rCount )
{
	CCycleCount::Add(m_dAccumulatedTime, rCount, m_dAccumulatedTime);
	CCycleCount::Add(m_dTotalTime, rCount, m_dTotalTime);
}

//-----------------------------------------------------------------------------
// Purpose: 
// Output : CMeasureSection
//-----------------------------------------------------------------------------
CMeasureSection *CMeasureSection::GetNext( void )
{
	return m_pNext;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Output : CMeasureSection
//-----------------------------------------------------------------------------
CMeasureSection *CMeasureSection::GetList( void )
{
	return s_pSections;
}

//-----------------------------------------------------------------------------
// Purpose: Compares accumulated time for two sections
// Input  : ppms1 - 
//			ppms2 - 
// Output : static int
//-----------------------------------------------------------------------------
static int SectionCompare( const void* ppms1,const void* ppms2 )
{
	CMeasureSection* pms1 = *(CMeasureSection**)ppms1;
	CMeasureSection* pms2 = *(CMeasureSection**)ppms2;

	if ( pms1->GetTotalTime().IsLessThan(pms2->GetTotalTime()) )
		return 1;
	else
		return -1;
}

//-----------------------------------------------------------------------------
// Purpose: Sorts sections by time usage
//-----------------------------------------------------------------------------
void CMeasureSection::SortSections( void )
{
	// Not enough to be sortable
	if ( s_nCount <= 1 )
		return;

	CMeasureSection *sortarray[ 128 ];
	CMeasureSection *ms;

	memset(sortarray,sizeof(CMeasureSection*)*128,0);
	
	ms = GetList();
	int i;
	int c = 0;
	while ( ms )
	{
		sortarray[ c++ ] = ms;
		ms = ms->GetNext();
	}

	// Sort the array alphabetically
	qsort( sortarray, c , sizeof( CMeasureSection * ), SectionCompare );

	// Fix next pointers
	for ( i = 0; i < c-1; i++ )
	{
		sortarray[ i ]->m_pNext = sortarray[ i + 1 ];
	}
	sortarray[i]->m_pNext = NULL;

	// Point head of list at it
	s_pSections = sortarray[ 0 ];
}

//-----------------------------------------------------------------------------
// Purpose: Creates an instance for timing a section
// Input  : *ms - 
//-----------------------------------------------------------------------------
CMeasureSectionInstance::CMeasureSectionInstance( CMeasureSection *ms )
{
	// Remember where to put accumulated time
	m_pMS		= ms;

	if ( host_speeds.GetInt() < 3 && !game_speeds.GetInt())
		return;
	
	// Get initial timestamp
	m_Timer.Start();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CMeasureSectionInstance::~CMeasureSectionInstance( void )
{
	if ( host_speeds.GetInt() < 3 && !game_speeds.GetInt())
		return;

	// Get final timestamp
	m_Timer.End();

	// Add time to section
	m_pMS->AddTime( m_Timer.GetDuration() );
}

//-----------------------------------------------------------------------------
// Purpose: Re-sort all data and determine whether sort keys should be reset too ( after
//  re-doing sort if needed ).
//-----------------------------------------------------------------------------
void ResetTimeMeasurements( void )
{
#if defined( _DEBUG ) || defined( FORCE_MEASURE )
	bool sort_reset = false;

	// Time to redo sort?
	if ( measure_resort.GetFloat() > 0.0 &&
		GetRealTime() >= CMeasureSection::m_dNextResort )
	{
		// Redo it
		CMeasureSection::SortSections();
		// Set next time
		CMeasureSection::m_dNextResort = GetRealTime() + measure_resort.GetFloat();
		// Flag to reset sort accumulator, too
		sort_reset = true;
	}

	// Iterate through the sections now
	CMeasureSection *p = CMeasureSection::GetList();
	while ( p )
	{
		// Reset regular accum.
		p->Reset();
		// Reset sort accum less often
		if ( sort_reset )
		{
			p->SortReset();
		}
		p = p->GetNext();
	}
#endif
}