aboutsummaryrefslogtreecommitdiff
path: root/sp/src/public/measure_section.h
blob: 4fb2362e18387666737a2b4d9f756c48636c47c7 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $Workfile:     $
// $Date:         $
// $NoKeywords: $
//=============================================================================//
#if !defined( MEASURE_SECTION_H )
#define MEASURE_SECTION_H
#ifdef _WIN32
#pragma once
#endif


#include "tier0/fasttimer.h"
#include "convar.h"


// This is the macro to use in your code to measure until the code goes
//  out of scope
#if defined( _DEBUG ) || defined( FORCE_MEASURE )
#define MEASURECODE( description ) \
	static CMeasureSection	_xxx_ms( description ); \
	CMeasureSectionInstance _xxx_ms_inst( &_xxx_ms );
#else
#define MEASURECODE( description )
#endif


// ------------------------------------------------------------------------------------ //
// These things must exist in the executable for the CMeasureSection code to work.
// ------------------------------------------------------------------------------------ //
float GetRealTime();	// Get the clock's time.

extern ConVar	game_speeds;
extern ConVar	measure_resort;
// ------------------------------------------------------------------------------------ //



// Called once per frame to allow any necessary measurements to latch
void ResetTimeMeasurements( void );

//-----------------------------------------------------------------------------
// Purpose: Accumulates time for the named section
//-----------------------------------------------------------------------------
class CMeasureSection
{
public:
	// Allows for measuring named section
						CMeasureSection( const char *name );
	virtual				~CMeasureSection( void );


	// Update max value hit
	void				UpdateMax( void );
	// Reset totals
	void				Reset( void );
	// Reset sortable totals
	void				SortReset( void );
	// Get static name of section
	const char			*GetName( void );
	
	// Get accumulated time
	CCycleCount const&	GetTotalTime( void );

	CCycleCount const&	GetTime();

	CCycleCount const&	GetMaxTime();
	
	// Add in some time
	void				AddTime( CCycleCount const &rCount );

	// Get next section in chain
	CMeasureSection		*GetNext( void );

	// Get head of list of all sections
	static CMeasureSection *GetList( void );
	// Sort all sections by most time consuming
	static void			SortSections( void );

public:
	// Time when list should be sorted again
	static double		m_dNextResort;

private:
	// Accumulated time for section
	CCycleCount			m_dAccumulatedTime;
	
	// Max time for section
	CCycleCount			m_dMaxTime;

	// Elapsed time for section
	CCycleCount			m_dTotalTime;
	
	// Name of section
	const char			*m_pszName;
	// Next section in chain
	CMeasureSection		*m_pNext;
	// Head of section list
	static CMeasureSection *s_pSections;
	// Quick total for doing sorts faster
	static int			s_nCount;
};

//-----------------------------------------------------------------------------
// Purpose: On construction marks time and on destruction adds time to 
//  parent CMeasureSection object
//-----------------------------------------------------------------------------
class CMeasureSectionInstance
{
public:
	// Constructor:  Points to object to accumulate time into
						CMeasureSectionInstance( CMeasureSection *ms );
	// Destructor:  Latches accumulated time
	virtual				~CMeasureSectionInstance( void );

private:
	// Time of construction
	CFastTimer			m_Timer;

	// Where to place elapsed time
	CMeasureSection		*m_pMS;
};

#endif // MEASURE_SECTION_H