Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Profile.h
Go to the documentation of this file.
1/* Copyright (C) 2025 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 "lib/adts/ring_buf.h"
26#include "lib/code_annotation.h"
27#include "ps/Profiler2.h"
28#include "ps/Singleton.h"
29
30#include <vector>
31
33
34#define PROFILE_AMORTIZE_FRAMES 30
35#define PROFILE_AMORTIZE_TURNS 5
36
37// To profile scripts usefully, we use a call hook that's called on every enter/exit,
38// and need to find the function name. But most functions are anonymous so we make do
39// with filename plus line number instead.
40// Computing the names is fairly expensive, and we need to return an interned char*
41// for the profiler to hold a copy of, so we use boost::flyweight to construct interned
42// strings per call location.
43//
44// TODO: Check again how much the overhead for getting filename and line really is and if
45// it has increased with the new approach after the SpiderMonkey 31 upgrade.
46
48{
50public:
51 typedef std::vector<CProfileNode*>::iterator profile_iterator;
52 typedef std::vector<CProfileNode*>::const_iterator const_profile_iterator;
53
54 CProfileNode( const char* name, CProfileNode* parent );
56
57 const char* GetName() const { return name; }
58
59 double GetFrameCalls() const;
60 double GetFrameTime() const;
61 double GetTurnCalls() const;
62 double GetTurnTime() const;
63
64 const CProfileNode* GetChild( const char* name ) const;
65 const CProfileNode* GetScriptChild( const char* name ) const;
66 const std::vector<CProfileNode*>* GetChildren() const { return( &children ); }
67 const std::vector<CProfileNode*>* GetScriptChildren() const { return( &script_children ); }
68
69 bool CanExpand();
70
71 CProfileNode* GetChild( const char* name );
72 CProfileNode* GetScriptChild( const char* name );
73 CProfileNode* GetParent() const { return( parent ); }
74
75 // Resets timing information for this node and all its children
76 void Reset();
77 // Resets frame timings for this node and all its children
78 void Frame();
79 // Resets turn timings for this node and all its children
80 void Turn();
81 // Enters the node
82 void Call();
83 // Leaves the node. Returns true if the node has actually been left
84 bool Return();
85
86private:
87 friend class CProfileManager;
88 friend class CProfileNodeTable;
89
90 const char* name;
91
96
101
102 double start;
104
106 std::vector<CProfileNode*> children;
107 std::vector<CProfileNode*> script_children;
109};
110
111class CProfileManager : public Singleton<CProfileManager>
112{
113public:
116
117 // Begins timing for a named subsection
118 void Start( const char* name );
119 void StartScript( const char* name );
120
121 // Ends timing for the current subsection
122 void Stop();
123
124 // Resets all timing information
125 void Reset();
126 // Resets frame timing information
127 void Frame();
128 // Resets turn timing information
129 // (Must not be called before Frame)
130 void Turn();
131 // Resets absolutely everything, at the end of this frame
132 void StructuralReset();
133
134 inline const CProfileNode* GetCurrent() { return( current ); }
135 inline const CProfileNode* GetRoot() { return( root ); }
136
137private:
140
142
144};
145
146#define g_Profiler CProfileManager::GetSingleton()
147
149{
150public:
151 CProfileSample(const char* name);
153};
154
155// Put a PROFILE("xyz") block at the start of all code to be profiled.
156// Profile blocks last until the end of the containing scope.
157#define PROFILE(name) CProfileSample __profile(name)
158// Cheat a bit to make things slightly easier on the user
159#define PROFILE_START(name) { CProfileSample __profile(name)
160#define PROFILE_END(name) }
161
162// Do both old and new profilers simultaneously (1+2=3), for convenience.
163#define PROFILE3(name) PROFILE(name); PROFILE2(name)
164
165// Also do GPU
166#define PROFILE3_GPU(deviceCommandContext, name) PROFILE(name); PROFILE2(name); PROFILE2_GPU(deviceCommandContext, name)
167
168#endif // INCLUDED_PROFILE
New profiler (complementing the older CProfileManager)
Definition: Profile.h:112
const CProfileNode * GetCurrent()
Definition: Profile.h:134
void Turn()
Definition: Profile.cpp:452
const CProfileNode * GetRoot()
Definition: Profile.h:135
void Start(const char *name)
Definition: Profile.cpp:412
void PerformStructuralReset()
Definition: Profile.cpp:467
void Stop()
Definition: Profile.cpp:426
CProfileNode * root
Definition: Profile.h:138
CProfileManager()
Definition: Profile.cpp:401
~CProfileManager()
Definition: Profile.cpp:407
CProfileNode * current
Definition: Profile.h:139
void Reset()
Definition: Profile.cpp:432
void StructuralReset()
Definition: Profile.cpp:457
void StartScript(const char *name)
Definition: Profile.cpp:419
void Frame()
Definition: Profile.cpp:437
bool needs_structural_reset
Definition: Profile.h:141
Class CProfileNodeTable: Implement ProfileViewer's AbstractProfileTable interface in order to display...
Definition: Profile.cpp:45
Definition: Profile.h:48
std::vector< CProfileNode * >::const_iterator const_profile_iterator
Definition: Profile.h:52
void Reset()
Definition: Profile.cpp:331
CProfileNodeTable * display_table
Definition: Profile.h:108
const char * GetName() const
Definition: Profile.h:57
void Call()
Definition: Profile.cpp:380
RingBuf< double, PROFILE_AMORTIZE_TURNS > time_per_turn
Definition: Profile.h:100
const CProfileNode * GetChild(const char *name) const
Definition: Profile.cpp:282
std::vector< CProfileNode * > script_children
Definition: Profile.h:107
int recursion
Definition: Profile.h:103
double time_frame_current
Definition: Profile.h:97
RingBuf< int, PROFILE_AMORTIZE_TURNS > calls_per_turn
Definition: Profile.h:95
bool CanExpand()
Definition: Profile.cpp:326
double GetTurnTime() const
Definition: Profile.cpp:277
RingBuf< double, PROFILE_AMORTIZE_FRAMES > time_per_frame
Definition: Profile.h:99
double GetTurnCalls() const
Definition: Profile.cpp:272
std::vector< CProfileNode * > children
Definition: Profile.h:106
void Frame()
Definition: Profile.cpp:350
CProfileNode * GetParent() const
Definition: Profile.h:73
NONCOPYABLE(CProfileNode)
const std::vector< CProfileNode * > * GetScriptChildren() const
Definition: Profile.h:67
RingBuf< int, PROFILE_AMORTIZE_FRAMES > calls_per_frame
Definition: Profile.h:94
bool Return()
Definition: Profile.cpp:390
const char * name
Definition: Profile.h:90
double start
Definition: Profile.h:102
double GetFrameTime() const
Definition: Profile.cpp:267
double GetFrameCalls() const
Definition: Profile.cpp:262
CProfileNode(const char *name, CProfileNode *parent)
Definition: Profile.cpp:231
CProfileNode * parent
Definition: Profile.h:105
const CProfileNode * GetScriptChild(const char *name) const
Definition: Profile.cpp:292
const std::vector< CProfileNode * > * GetChildren() const
Definition: Profile.h:66
int calls_turn_current
Definition: Profile.h:93
std::vector< CProfileNode * >::iterator profile_iterator
Definition: Profile.h:51
int calls_frame_current
Definition: Profile.h:92
double time_turn_current
Definition: Profile.h:98
~CProfileNode()
Definition: Profile.cpp:243
void Turn()
Definition: Profile.cpp:365
Definition: Profile.h:149
~CProfileSample()
Definition: Profile.cpp:486
CProfileSample(const char *name)
Definition: Profile.cpp:476
Template base class for singletons.
Definition: Singleton.h:35