SuperTuxKart
Loading...
Searching...
No Matches
profiler.hpp
1// SuperTuxKart - a fun racing game with go-kart
2// Copyright (C) 2004-2015 SuperTuxKart-Team
3//
4// This program is free software; you can redistribute it and/or
5// modify it under the terms of the GNU General Public License
6// as published by the Free Software Foundation; either version 3
7// of the License, or (at your option) any later version.
8//
9// This program 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 this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18#ifndef PROFILER_HPP
19#define PROFILER_HPP
20
21#include "utils/synchronised.hpp"
22
23#include <assert.h>
24#include <atomic>
25#include <iostream>
26#include <list>
27#include <map>
28#include <ostream>
29#include <stack>
30#include <streambuf>
31#include <string>
32#include <vector>
33
34#include <vector2d.h>
35#include <SColor.h>
36using namespace irr;
37
38enum QueryPerf
39{
40 Q_SHADOWS_CASCADE0,
41 Q_SHADOWS_CASCADE1,
42 Q_SHADOWS_CASCADE2,
43 Q_SHADOWS_CASCADE3,
44 Q_SOLID_PASS,
45 Q_ENVMAP,
46 Q_SUN,
47 Q_POINTLIGHTS,
48 Q_SSAO,
49 Q_LIGHTSCATTER,
50 Q_GLOW,
51 Q_COMBINE_DIFFUSE_COLOR,
52 Q_SKYBOX,
53 Q_TRANSPARENT,
54 Q_PARTICLES,
55 Q_DOF,
56 Q_GODRAYS,
57 Q_BLOOM,
58 Q_TONEMAP,
59 Q_MOTIONBLUR,
60 Q_LIGHTNING,
61 Q_MLAA,
62 Q_GUI,
63 Q_LAST
64};
65
66class Profiler;
67extern Profiler profiler;
68
69double getTimeMilliseconds();
70
71#define ENABLE_PROFILER
72
73#ifdef ENABLE_PROFILER
74 #define PROFILER_PUSH_CPU_MARKER(name, r, g, b) \
75 profiler.pushCPUMarker(name, video::SColor(0xFF, r, g, b))
76
77 #define PROFILER_POP_CPU_MARKER() \
78 profiler.popCPUMarker()
79
80 #define PROFILER_SYNC_FRAME() \
81 profiler.synchronizeFrame()
82
83 #define PROFILER_DRAW() \
84 profiler.draw()
85#else
86 #define PROFILER_PUSH_CPU_MARKER(name, r, g, b)
87 #define PROFILER_POP_CPU_MARKER()
88 #define PROFILER_SYNC_FRAME()
89 #define PROFILER_DRAW()
90#endif
91
92using namespace irr;
93
94// ============================================================================
100{
101private:
102 // ------------------------------------------------------------------------
103 class Marker
104 {
105 private:
108 double m_start;
109
116 size_t m_layer;
117 public:
118 // --------------------------------------------------------------------
119 Marker() { m_start = 0; m_duration = 0; m_layer = 0; }
120
121 // --------------------------------------------------------------------
122 Marker(double start, size_t layer=0)
123 : m_start(start), m_duration(0), m_layer(layer)
124 {
125 }
126 // --------------------------------------------------------------------
127 Marker(const Marker& ref)
128 : m_start(ref.m_start), m_duration(ref.m_duration),
129 m_layer(ref.m_layer)
130 {
131 }
132 // --------------------------------------------------------------------
134 double getStart() const { return m_start; }
135 // --------------------------------------------------------------------
137 double getEnd() const { return m_start+m_duration; }
138 // --------------------------------------------------------------------
140 double getDuration() const { return m_duration; }
141 // --------------------------------------------------------------------
142 size_t getLayer() const { return m_layer; }
143 // --------------------------------------------------------------------
146 void clear() { m_duration = 0; }
147 // --------------------------------------------------------------------
149 void setStart(double start, size_t layer = 0)
150 {
151 m_start = start; m_layer = layer;
152 } // setStart
153 // --------------------------------------------------------------------
155 void setEnd(double end)
156 {
157 m_duration += (end - m_start);
158 } // setEnd
159
160 }; // class Marker
161
162 // ========================================================================
167 {
168 private:
170 video::SColor m_colour;
171
173 std::vector<Marker> m_all_markers;
174
175 public:
176 EventData() {}
177 EventData(video::SColor colour, int max_size)
178 {
179 m_all_markers.resize(max_size);
180 m_colour = colour;
181 } // EventData
182 // --------------------------------------------------------------------
184 void setStart(size_t frame, double start, int layer)
185 {
186 assert(frame < m_all_markers.capacity());
187 m_all_markers[frame].setStart(start, layer);
188 } // setStart
189 // --------------------------------------------------------------------
191 void setEnd(size_t frame, double end)
192 {
193 assert(frame < m_all_markers.capacity());
194 m_all_markers[frame].setEnd(end);
195 } // setEnd
196 // --------------------------------------------------------------------
197 const Marker& getMarker(int n) const { return m_all_markers[n]; }
198 Marker& getMarker(int n) { return m_all_markers[n]; }
199 // --------------------------------------------------------------------
201 video::SColor getColour() const { return m_colour; }
202 // --------------------------------------------------------------------
203 }; // EventData
204
205 // ========================================================================
207 typedef std::map<std::string, EventData> AllEventData;
208 // ========================================================================
210 {
212 std::vector< std::string > m_event_stack;
213
218 std::vector<std::string> m_ordered_headings;
219
220 AllEventData m_all_event_data;
221 }; // class ThreadData
222
223 // ========================================================================
224
227 std::vector< ThreadData> m_all_threads_data;
228
230 std::vector<int> m_gpu_times;
231
233 std::atomic<int> m_threads_used;
234
237
243
246
250
254
257
261 std::vector<std::string> m_all_event_names;
262
263 // Handling freeze/unfreeze by clicking on the display
264 enum FreezeState
265 {
266 UNFROZEN,
267 WAITING_FOR_FREEZE,
268 FROZEN,
269 WAITING_FOR_UNFREEZE,
270 };
271
272 FreezeState m_freeze_state;
273
274private:
275 int getThreadID();
276 void drawBackground();
277
278public:
279 Profiler();
280 virtual ~Profiler();
281 void init();
282 void pushCPUMarker(const char* name="N/A",
283 const video::SColor& color=video::SColor());
284 void popCPUMarker();
285 void toggleStatus();
286 void synchronizeFrame();
287 void draw();
288 void onClick(const core::vector2di& mouse_pos);
289 void writeToFile();
290
291 // ------------------------------------------------------------------------
292 bool isFrozen() const { return m_freeze_state == FROZEN; }
293
294};
295
296#endif // PROFILER_HPP
The data for one event.
Definition: profiler.hpp:167
void setStart(size_t frame, double start, int layer)
Records the start of an event for a given frame.
Definition: profiler.hpp:184
std::vector< Marker > m_all_markers
Vector of all buffered markers.
Definition: profiler.hpp:173
video::SColor getColour() const
Returns the colour for this event.
Definition: profiler.hpp:201
video::SColor m_colour
Colour to use in the on-screen display.
Definition: profiler.hpp:170
void setEnd(size_t frame, double end)
Records the end of an event for a given frame.
Definition: profiler.hpp:191
Definition: profiler.hpp:104
void setStart(double start, size_t layer=0)
Sets start time and layer for this event.
Definition: profiler.hpp:149
double m_start
An event that is started (pushed) stores the start time in this variable.
Definition: profiler.hpp:108
double m_duration
Duration of the event in this frame (accumulated if this event should be recorded more than once).
Definition: profiler.hpp:113
double getEnd() const
Returns the end time of this event marker.
Definition: profiler.hpp:137
double getDuration() const
Returns the duration of this event.
Definition: profiler.hpp:140
double getStart() const
Returns the start time of this event marker.
Definition: profiler.hpp:134
void setEnd(double end)
Sets the end time of this event.
Definition: profiler.hpp:155
size_t m_layer
Distance of marker from root (for nested events), used to adjust vertical height when drawing.
Definition: profiler.hpp:116
void clear()
Called when an entry in the cyclic buffer is reused.
Definition: profiler.hpp:146
class that allows run-time graphical profiling through the use of markers.
Definition: profiler.hpp:100
std::vector< int > m_gpu_times
Buffer for the GPU times (in ms).
Definition: profiler.hpp:230
void onClick(const core::vector2di &mouse_pos)
Handle freeze/unfreeze.
Definition: profiler.cpp:478
double m_time_between_sync
Time between now and last sync, used to scale the GUI bar.
Definition: profiler.hpp:256
void toggleStatus()
Switches the profiler either on or off.
Definition: profiler.cpp:206
std::vector< ThreadData > m_all_threads_data
Data structure containing all currently buffered markers.
Definition: profiler.hpp:227
std::vector< std::string > m_all_event_names
List of all event names.
Definition: profiler.hpp:261
Synchronised< bool > m_lock
We don't need the bool, but easiest way to get a lock for the whole instance (since we need to avoid ...
Definition: profiler.hpp:242
void synchronizeFrame()
Saves all data for the current frame, and starts the next frame in the circular buffer.
Definition: profiler.cpp:224
int getThreadID()
Returns a unique index for a thread.
Definition: profiler.cpp:121
void drawBackground()
Helper to draw a white background.
Definition: profiler.cpp:513
std::map< std::string, EventData > AllEventData
The mapping of event names to the corresponding EventData.
Definition: profiler.hpp:207
int m_max_frames
The maximum number of frames to be buffered.
Definition: profiler.hpp:249
void draw()
Draw the markers.
Definition: profiler.cpp:290
double m_time_last_sync
Time of last sync.
Definition: profiler.hpp:253
void writeToFile()
Saves the collected profile data to a file.
Definition: profiler.cpp:534
void popCPUMarker()
Stop the last pushed marker.
Definition: profiler.cpp:173
void init()
It is split from the constructor so that it can be avoided allocating unnecessary memory when the pro...
Definition: profiler.cpp:109
std::atomic< int > m_threads_used
Counts the threads used.
Definition: profiler.hpp:233
int m_current_frame
Index of the current frame in the buffer.
Definition: profiler.hpp:236
bool m_has_wrapped_around
True if the circular buffer has wrapped around.
Definition: profiler.hpp:245
void pushCPUMarker(const char *name="N/A", const video::SColor &color=video::SColor())
Push a new marker that starts now.
Definition: profiler.cpp:137
A variable that is automatically synchronised using pthreads mutex.
Definition: synchronised.hpp:28
Definition: profiler.hpp:210
std::vector< std::string > m_ordered_headings
This stores the event names in the order in which they occur.
Definition: profiler.hpp:218
std::vector< std::string > m_event_stack
Stack of events to detect nesting.
Definition: profiler.hpp:212