forked from 0ad/0ad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfile.h
194 lines (156 loc) · 5.67 KB
/
Profile.h
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
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* GPG3-style hierarchical profiler
*/
#ifndef INCLUDED_PROFILE
#define INCLUDED_PROFILE
#include <vector>
#include "lib/adts/ring_buf.h"
#include "lib/posix/posix_pthread.h"
#include "ps/Profiler2.h"
#include "ps/Singleton.h"
#include <boost/flyweight.hpp>
#include <boost/flyweight/key_value.hpp>
#include <boost/flyweight/no_locking.hpp>
#include <boost/flyweight/no_tracking.hpp>
#define PROFILE_AMORTIZE_FRAMES 30
#define PROFILE_AMORTIZE_TURNS 1
class CProfileManager;
class CProfileNodeTable;
class CStr8;
class CStrW;
// To profile scripts usefully, we use a call hook that's called on every enter/exit,
// and need to find the function name. But most functions are anonymous so we make do
// with filename plus line number instead.
// Computing the names is fairly expensive, and we need to return an interned char*
// for the profiler to hold a copy of, so we use boost::flyweight to construct interned
// strings per call location.
//
// TODO: Check again how much the overhead for getting filename and line really is and if
// it has increased with the new approach after the SpiderMonkey 31 upgrade.
//
// Flyweight types (with no_locking because the call hooks are only used in the
// main thread, and no_tracking because we mustn't delete values the profiler is
// using and it's not going to waste much memory)
typedef boost::flyweight<
std::string,
boost::flyweights::no_tracking,
boost::flyweights::no_locking
> StringFlyweight;
class CProfileNode
{
NONCOPYABLE(CProfileNode);
public:
typedef std::vector<CProfileNode*>::iterator profile_iterator;
typedef std::vector<CProfileNode*>::const_iterator const_profile_iterator;
CProfileNode( const char* name, CProfileNode* parent );
~CProfileNode();
const char* GetName() const { return name; }
double GetFrameCalls() const;
double GetFrameTime() const;
double GetTurnCalls() const;
double GetTurnTime() const;
double GetFrameMallocs() const;
double GetTurnMallocs() const;
const CProfileNode* GetChild( const char* name ) const;
const CProfileNode* GetScriptChild( const char* name ) const;
const std::vector<CProfileNode*>* GetChildren() const { return( &children ); }
const std::vector<CProfileNode*>* GetScriptChildren() const { return( &script_children ); }
bool CanExpand();
CProfileNode* GetChild( const char* name );
CProfileNode* GetScriptChild( const char* name );
CProfileNode* GetParent() const { return( parent ); }
// Resets timing information for this node and all its children
void Reset();
// Resets frame timings for this node and all its children
void Frame();
// Resets turn timings for this node and all its children
void Turn();
// Enters the node
void Call();
// Leaves the node. Returns true if the node has actually been left
bool Return();
private:
friend class CProfileManager;
friend class CProfileNodeTable;
const char* name;
int calls_frame_current;
int calls_turn_current;
RingBuf<int, PROFILE_AMORTIZE_FRAMES> calls_per_frame;
RingBuf<int, PROFILE_AMORTIZE_TURNS> calls_per_turn;
double time_frame_current;
double time_turn_current;
RingBuf<double, PROFILE_AMORTIZE_FRAMES> time_per_frame;
RingBuf<double, PROFILE_AMORTIZE_TURNS> time_per_turn;
long mallocs_frame_current;
long mallocs_turn_current;
RingBuf<long, PROFILE_AMORTIZE_FRAMES> mallocs_per_frame;
RingBuf<long, PROFILE_AMORTIZE_TURNS> mallocs_per_turn;
double start;
long start_mallocs;
int recursion;
CProfileNode* parent;
std::vector<CProfileNode*> children;
std::vector<CProfileNode*> script_children;
CProfileNodeTable* display_table;
};
class CProfileManager : public Singleton<CProfileManager>
{
public:
CProfileManager();
~CProfileManager();
// Begins timing for a named subsection
void Start( const char* name );
void StartScript( const char* name );
// Ends timing for the current subsection
void Stop();
// Resets all timing information
void Reset();
// Resets frame timing information
void Frame();
// Resets turn timing information
// (Must not be called before Frame)
void Turn();
// Resets absolutely everything, at the end of this frame
void StructuralReset();
inline const CProfileNode* GetCurrent() { return( current ); }
inline const CProfileNode* GetRoot() { return( root ); }
private:
CProfileNode* root;
CProfileNode* current;
bool needs_structural_reset;
void PerformStructuralReset();
};
#define g_Profiler CProfileManager::GetSingleton()
class CProfileSample
{
public:
CProfileSample(const char* name);
~CProfileSample();
};
// Put a PROFILE("xyz") block at the start of all code to be profiled.
// Profile blocks last until the end of the containing scope.
#define PROFILE(name) CProfileSample __profile(name)
// Cheat a bit to make things slightly easier on the user
#define PROFILE_START(name) { CProfileSample __profile(name)
#define PROFILE_END(name) }
// Do both old and new profilers simultaneously (1+2=3), for convenience.
#define PROFILE3(name) PROFILE(name); PROFILE2(name)
// Also do GPU
#define PROFILE3_GPU(name) PROFILE(name); PROFILE2(name); PROFILE2_GPU(name)
#endif // INCLUDED_PROFILE