Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Profile.h
Go to the documentation of this file.
1 /* Copyright (C) 2013 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * GPG3-style hierarchical profiler
20  */
21 
22 #ifndef INCLUDED_PROFILE
23 #define INCLUDED_PROFILE
24 
25 #include <vector>
26 
27 #include "lib/adts/ring_buf.h"
28 #include "ps/Profiler2.h"
29 #include "ps/Singleton.h"
30 #include "ps/ThreadUtil.h"
31 
32 #define PROFILE_AMORTIZE_FRAMES 30
33 #define PROFILE_AMORTIZE_TURNS 1
34 
35 class CProfileManager;
36 class CProfileNodeTable;
37 
38 class CStr8;
39 class CStrW;
40 
42 {
44 
45  friend class CProfileManager;
46  friend class CProfileNodeTable;
47 
48  const char* name;
49 
54 
59 
64 
65  double start;
67  int recursion;
68 
70  std::vector<CProfileNode*> children;
71  std::vector<CProfileNode*> script_children;
73 
74 public:
75  typedef std::vector<CProfileNode*>::iterator profile_iterator;
76  typedef std::vector<CProfileNode*>::const_iterator const_profile_iterator;
77 
78  CProfileNode( const char* name, CProfileNode* parent );
79  ~CProfileNode();
80 
81  const char* GetName() const { return name; }
82 
83  double GetFrameCalls() const;
84  double GetFrameTime() const;
85  double GetTurnCalls() const;
86  double GetTurnTime() const;
87  double GetFrameMallocs() const;
88  double GetTurnMallocs() const;
89 
90  const CProfileNode* GetChild( const char* name ) const;
91  const CProfileNode* GetScriptChild( const char* name ) const;
92  const std::vector<CProfileNode*>* GetChildren() const { return( &children ); }
93  const std::vector<CProfileNode*>* GetScriptChildren() const { return( &script_children ); }
94 
95  bool CanExpand();
96 
97  CProfileNode* GetChild( const char* name );
98  CProfileNode* GetScriptChild( const char* name );
99  CProfileNode* GetParent() const { return( parent ); }
100 
101  // Resets timing information for this node and all its children
102  void Reset();
103  // Resets frame timings for this node and all its children
104  void Frame();
105  // Resets turn timings for this node and all its children
106  void Turn();
107  // Enters the node
108  void Call();
109  // Leaves the node. Returns true if the node has actually been left
110  bool Return();
111 };
112 
113 class CProfileManager : public Singleton<CProfileManager>
114 {
117 
119 
120  void PerformStructuralReset();
121 
122 public:
123  CProfileManager();
125 
126  // Begins timing for a named subsection
127  void Start( const char* name );
128  void StartScript( const char* name );
129 
130  // Ends timing for the current subsection
131  void Stop();
132 
133  // Resets all timing information
134  void Reset();
135  // Resets frame timing information
136  void Frame();
137  // Resets turn timing information
138  // (Must not be called before Frame)
139  void Turn();
140  // Resets absolutely everything, at the end of this frame
141  void StructuralReset();
142 
143  inline const CProfileNode* GetCurrent() { return( current ); }
144  inline const CProfileNode* GetRoot() { return( root ); }
145 };
146 
147 #define g_Profiler CProfileManager::GetSingleton()
148 
150 {
151 public:
152  CProfileSample(const char* name)
153  {
155  {
156  // The profiler is only safe to use on the main thread
158 
159  g_Profiler.Start(name);
160  }
161  }
163  {
165  g_Profiler.Stop();
166  }
167 };
168 
170 {
171 public:
172  CProfileSampleScript( const char* name )
173  {
175  {
176  // The profiler is only safe to use on the main thread,
177  // but scripts get run on other threads too so we need to
178  // conditionally enable the profiler.
179  // (This usually only gets used in debug mode so performance
180  // doesn't matter much.)
182  g_Profiler.StartScript( name );
183  }
184  }
186  {
189  g_Profiler.Stop();
190  }
191 };
192 
193 // Put a PROFILE("xyz") block at the start of all code to be profiled.
194 // Profile blocks last until the end of the containing scope.
195 #define PROFILE(name) CProfileSample __profile(name)
196 // Cheat a bit to make things slightly easier on the user
197 #define PROFILE_START(name) { CProfileSample __profile(name)
198 #define PROFILE_END(name) }
199 
200 // Do both old and new profilers simultaneously (1+2=3), for convenience.
201 #define PROFILE3(name) PROFILE(name); PROFILE2(name)
202 
203 // Also do GPU
204 #define PROFILE3_GPU(name) PROFILE(name); PROFILE2(name); PROFILE2_GPU(name)
205 
206 #endif // INCLUDED_PROFILE
RingBuf< double, PROFILE_AMORTIZE_TURNS > time_per_turn
Definition: Profile.h:58
const CProfileNode * GetChild(const char *name) const
Definition: Profile.cpp:323
RingBuf< int, PROFILE_AMORTIZE_TURNS > calls_per_turn
Definition: Profile.h:53
void Turn()
Definition: Profile.cpp:413
CProfileSample(const char *name)
Definition: Profile.h:152
double GetFrameCalls() const
Definition: Profile.cpp:293
const char * GetName() const
Definition: Profile.h:81
bool Return()
Definition: Profile.cpp:632
const std::vector< CProfileNode * > * GetScriptChildren() const
Definition: Profile.h:93
const CProfileNode * GetCurrent()
Definition: Profile.h:143
RingBuf< int, PROFILE_AMORTIZE_FRAMES > calls_per_frame
Definition: Profile.h:52
void StartScript(const char *name)
Definition: Profile.cpp:664
CProfileNode * parent
Definition: Profile.h:69
std::vector< CProfileNode * > children
Definition: Profile.h:70
const CProfileNode * GetRoot()
Definition: Profile.h:144
double time_turn_current
Definition: Profile.h:56
long mallocs_turn_current
Definition: Profile.h:61
double start
Definition: Profile.h:65
int calls_turn_current
Definition: Profile.h:51
CProfileNode * GetParent() const
Definition: Profile.h:99
Class CProfileNodeTable: Implement ProfileViewer&#39;s AbstractProfileTable interface in order to display...
Definition: Profile.cpp:51
void Frame()
Definition: Profile.cpp:396
NONCOPYABLE(CProfileNode)
CProfileNodeTable * display_table
Definition: Profile.h:72
CProfileNode * root
Definition: Profile.h:115
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
int recursion
Definition: Profile.h:67
CProfileSampleScript(const char *name)
Definition: Profile.h:172
std::vector< CProfileNode * >::const_iterator const_profile_iterator
Definition: Profile.h:76
void PerformStructuralReset()
Definition: Profile.cpp:716
#define g_Profiler
Definition: Profile.h:147
bool needs_structural_reset
Definition: Profile.h:118
New profiler (complementing the older CProfileManager)
void StructuralReset()
Definition: Profile.cpp:706
CProfileNode * current
Definition: Profile.h:116
static bool IsInitialised()
Definition: Singleton.h:63
double GetFrameMallocs() const
Definition: Profile.cpp:313
const std::vector< CProfileNode * > * GetChildren() const
Definition: Profile.h:92
long mallocs_frame_current
Definition: Profile.h:60
void Call()
Definition: Profile.cpp:621
const CProfileNode * GetScriptChild(const char *name) const
Definition: Profile.cpp:333
double GetTurnTime() const
Definition: Profile.cpp:308
double time_frame_current
Definition: Profile.h:55
RingBuf< double, PROFILE_AMORTIZE_FRAMES > time_per_frame
Definition: Profile.h:57
CProfileNode(const char *name, CProfileNode *parent)
Definition: Profile.cpp:262
void Reset()
Definition: Profile.cpp:372
int calls_frame_current
Definition: Profile.h:50
double GetFrameTime() const
Definition: Profile.cpp:298
bool IsMainThread()
Returns whether the current thread is the &#39;main&#39; thread (i.e.
Definition: ThreadUtil.cpp:25
const char * name
Definition: Profile.h:48
long start_mallocs
Definition: Profile.h:66
RingBuf< long, PROFILE_AMORTIZE_FRAMES > mallocs_per_frame
Definition: Profile.h:62
RingBuf< long, PROFILE_AMORTIZE_TURNS > mallocs_per_turn
Definition: Profile.h:63
std::vector< CProfileNode * > script_children
Definition: Profile.h:71
double GetTurnMallocs() const
Definition: Profile.cpp:318
bool CanExpand()
Definition: Profile.cpp:367
std::vector< CProfileNode * >::iterator profile_iterator
Definition: Profile.h:75
double GetTurnCalls() const
Definition: Profile.cpp:303
void Start(const char *name)
Definition: Profile.cpp:657