OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreProfiler.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4  (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 /*
29 
30  Although the code is original, many of the ideas for the profiler were borrowed from
31 "Real-Time In-Game Profiling" by Steve Rabin which can be found in Game Programming
32 Gems 1.
33 
34  This code can easily be adapted to your own non-Ogre project. The only code that is
35 Ogre-dependent is in the visualization/logging routines and the use of the Timer class.
36 
37  Enjoy!
38 
39 */
40 
41 #ifndef __Profiler_H__
42 #define __Profiler_H__
43 
44 #include "OgrePrerequisites.h"
45 #include "OgreSingleton.h"
46 #include "OgreHeaderPrefix.h"
47 
48 #if OGRE_PROFILING == 1
49 # define OgreProfile( a ) Ogre::Profile _OgreProfileInstance( (a) )
50 # define OgreProfileBegin( a ) Ogre::Profiler::getSingleton().beginProfile( (a) )
51 # define OgreProfileEnd( a ) Ogre::Profiler::getSingleton().endProfile( (a) )
52 # define OgreProfileGroup( a, g ) Ogre::Profile _OgreProfileInstance( (a), (g) )
53 # define OgreProfileBeginGroup( a, g ) Ogre::Profiler::getSingleton().beginProfile( (a), (g) )
54 # define OgreProfileEndGroup( a, g ) Ogre::Profiler::getSingleton().endProfile( (a), (g) )
55 # define OgreProfileBeginGPUEvent( g ) Ogre::Profiler::getSingleton().beginGPUEvent(g)
56 # define OgreProfileEndGPUEvent( g ) Ogre::Profiler::getSingleton().endGPUEvent(g)
57 # define OgreProfileMarkGPUEvent( e ) Ogre::Profiler::getSingleton().markGPUEvent(e)
58 #else
59 # define OgreProfile( a )
60 # define OgreProfileBegin( a )
61 # define OgreProfileEnd( a )
62 # define OgreProfileGroup( a, g )
63 # define OgreProfileBeginGroup( a, g )
64 # define OgreProfileEndGroup( a, g )
65 # define OgreProfileBeginGPUEvent( e )
66 # define OgreProfileEndGPUEvent( e )
67 # define OgreProfileMarkGPUEvent( e )
68 #endif
69 
70 namespace Ogre {
80  {
82  OGREPROF_USER_DEFAULT = 0x00000001,
84  OGREPROF_ALL = 0xFF000000,
86  OGREPROF_GENERAL = 0x80000000,
88  OGREPROF_CULLING = 0x40000000,
90  OGREPROF_RENDERING = 0x20000000
91  };
92 
104  public ProfilerAlloc
105  {
106 
107  public:
108  Profile(const String& profileName, uint32 groupID = (uint32)OGREPROF_USER_DEFAULT);
109  ~Profile();
110 
111  protected:
112 
117 
118  };
119 
123  struct ProfileFrame
124  {
125 
128 
131 
134 
135  };
136 
139  {
144 
149 
154 
157 
162 
166 
169 
170  };
171 
174  {
175  friend class Profiler;
176  public:
177  ProfileInstance(void);
178  virtual ~ProfileInstance(void);
179 
181 
182  void logResults();
183  void reset();
184 
187  inline bool watchForLimit(Real limit, bool greaterThan = true)
188  {
189  if (greaterThan)
190  return history.currentTimePercent > limit;
191  else
192  return history.currentTimePercent < limit;
193  }
194 
195  bool watchForMax(const String& profileName);
196  bool watchForMin(const String& profileName);
197  bool watchForLimit(const String& profileName, Real limit, bool greaterThan = true);
198 
201 
204 
206 
209 
211 
214 
218 
221  };
222 
229  {
230  public:
232  {
236  DISPLAY_MILLISECONDS
237  };
238 
239  ProfileSessionListener() : mDisplayMode(DISPLAY_MILLISECONDS) {}
241 
243  virtual void initializeSession() = 0;
244 
246  virtual void finializeSession() = 0;
247 
252  virtual void changeEnableState(bool enabled) {};
253 
255  virtual void displayResults(const ProfileInstance& instance, ulong maxTotalFrameTime) {};
256 
258  void setDisplayMode(DisplayMode d) { mDisplayMode = d; }
259 
261  DisplayMode getDisplayMode() const { return mDisplayMode; }
262 
263  protected:
266  };
267 
280  public Singleton<Profiler>,
281  public ProfilerAlloc
282  {
283  public:
284  Profiler();
285  ~Profiler();
286 
288  void setTimer(Timer* t);
289 
291  Timer* getTimer();
292 
306  void beginProfile(const String& profileName, uint32 groupID = (uint32)OGREPROF_USER_DEFAULT);
307 
322  void endProfile(const String& profileName, uint32 groupID = (uint32)OGREPROF_USER_DEFAULT);
323 
327  void beginGPUEvent(const String& event);
328 
332  void endGPUEvent(const String& event);
333 
337  void markGPUEvent(const String& event);
338 
344  void setEnabled(bool enabled);
345 
347  bool getEnabled() const;
348 
352  void enableProfile(const String& profileName);
353 
357  void disableProfile(const String& profileName);
358 
361  void setProfileGroupMask(uint32 mask) { mProfileMask = mask; }
364  uint32 getProfileGroupMask() const { return mProfileMask; }
365 
371  bool watchForMax(const String& profileName);
372 
378  bool watchForMin(const String& profileName);
379 
389  bool watchForLimit(const String& profileName, Real limit, bool greaterThan = true);
390 
392  void logResults();
393 
395  void reset();
396 
398  void setUpdateDisplayFrequency(uint freq);
399 
401  uint getUpdateDisplayFrequency() const;
402 
409  void addListener(ProfileSessionListener* listener);
410 
417  void removeListener(ProfileSessionListener* listener);
418 
434  static Profiler& getSingleton(void);
450  static Profiler* getSingletonPtr(void);
451 
452  protected:
453  friend class ProfileInstance;
454 
457 
459  void initialize();
460 
461  void displayResults();
462 
464  void processFrameStats(void);
466  void processFrameStats(ProfileInstance* instance, Real& maxFrameTime);
467 
469  void changeEnableState();
470 
471  // lol. Uses typedef; put's original container type in name.
474 
478 
481 
484 
488 
491 
494 
497 
499  bool mEnabled;
500 
504 
507 
510 
514 
515 
516  }; // end class
520 } // end namespace
521 
522 #include "OgreHeaderSuffix.h"
523 
524 #endif
ProfileSessionListener should be used to visualize profile results.
Definition: OgreProfiler.h:228
String name
The name of the profile.
Definition: OgreProfiler.h:200
Real maxTimeMillisecs
The maximum frame time this profile has taken in milliseconds.
Definition: OgreProfiler.h:148
User default profile.
Definition: OgreProfiler.h:82
virtual void changeEnableState(bool enabled)
If the profiler disables this listener then it should hide its panels (if any exists) or stop sending...
Definition: OgreProfiler.h:252
float Real
Software floating point type.
unsigned int uint32
Definition: OgrePlatform.h:420
#define _OgreExport
Definition: OgrePlatform.h:255
unsigned long ulong
ProfileInstance * mLast
Definition: OgreProfiler.h:476
uint mCurrentFrame
The number of elapsed frame, used with mUpdateDisplayFrequency.
Definition: OgreProfiler.h:490
Template class for creating single-instance global classes.
Definition: OgreSingleton.h:64
uint hierarchicalLvl
The hierarchical level of this profile, 0 being the root profile.
Definition: OgreProfiler.h:220
Real mAverageFrameTime
Rolling average of millisecs.
Definition: OgreProfiler.h:512
virtual ~ProfileInstance(void)
uint32 getProfileGroupMask() const
Get the mask which all profiles must pass to be enabled.
Definition: OgreProfiler.h:364
ulong accum
Represents the total time of all child profiles to subtract from this profile.
Definition: OgreProfiler.h:217
ProfileInstance mRoot
Definition: OgreProfiler.h:477
uint32 mGroupID
The group ID.
Definition: OgreProfiler.h:116
void setDisplayMode(DisplayMode d)
Set the display mode for the overlay.
Definition: OgreProfiler.h:258
Real currentTimePercent
The current percentage of frame time this profile has taken.
Definition: OgreProfiler.h:141
ProfileInstance::ProfileChildren ProfileChildren
Definition: OgreProfiler.h:473
ulong frameTime
The total time this profile has taken this frame.
Definition: OgreProfiler.h:127
String mName
The name of this profile.
Definition: OgreProfiler.h:114
uint mUpdateDisplayFrequency
The number of frames that must elapse before the current frame display is updated.
Definition: OgreProfiler.h:487
uint hierarchicalLvl
The hierarchical level of this profile, 0 being the root profile.
Definition: OgreProfiler.h:168
bool mEnabled
Whether this profiler is enabled.
Definition: OgreProfiler.h:499
TProfileSessionListener mListeners
Definition: OgreProfiler.h:456
Represents an individual profile call.
Definition: OgreProfiler.h:173
ProfileChildren children
Definition: OgreProfiler.h:205
Timer * mTimer
The timer used for profiling.
Definition: OgreProfiler.h:493
The profiler allows you to measure the performance of your code.
Definition: OgreProfiler.h:279
Real minTimeMillisecs
The minimum frame time this profile has taken in milliseconds.
Definition: OgreProfiler.h:153
ProfileHistory history
Definition: OgreProfiler.h:210
Timer class.
uint hierarchicalLvl
The hierarchical level of this profile, 0 being the main loop.
Definition: OgreProfiler.h:133
ProfileGroupMask
List of reserved profiling masks.
Definition: OgreProfiler.h:79
virtual void displayResults(const ProfileInstance &instance, ulong maxTotalFrameTime)
Here we get the real profiling information which we can use.
Definition: OgreProfiler.h:255
Real totalTimeMillisecs
The total frame time this profile has taken in milliseconds.
Definition: OgreProfiler.h:161
Represents a history of each profile during the duration of the app.
Definition: OgreProfiler.h:138
DisplayMode mDisplayMode
How to display the overlay.
Definition: OgreProfiler.h:265
DisabledProfileMap mDisabledProfiles
Holds the names of disabled profiles.
Definition: OgreProfiler.h:480
ulong mMaxTotalFrameTime
The max frame time recorded.
Definition: OgreProfiler.h:509
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
set< String >::type DisabledProfileMap
Definition: OgreProfiler.h:472
ProfileInstance * mCurrent
Definition: OgreProfiler.h:475
ulong totalCalls
The total number of times this profile was called (used to calculate average)
Definition: OgreProfiler.h:165
Display % frame usage on the overlay.
Definition: OgreProfiler.h:234
ulong currTime
The time this profile was started.
Definition: OgreProfiler.h:213
std::map< K, V, P, A > type
DisplayMode getDisplayMode() const
Get the display mode for the overlay.
Definition: OgreProfiler.h:261
General processing.
Definition: OgreProfiler.h:86
uint numCallsThisFrame
The number of times this profile has been called each frame.
Definition: OgreProfiler.h:156
ProfileInstance * parent
The name of the parent, null if root.
Definition: OgreProfiler.h:203
Real currentTimeMillisecs
The current frame time this profile has taken in milliseconds.
Definition: OgreProfiler.h:143
bool mNewEnableState
Keeps track of the new enabled/disabled state that the user has requested which will be applied after...
Definition: OgreProfiler.h:503
vector< ProfileSessionListener * >::type TProfileSessionListener
Definition: OgreProfiler.h:455
void setProfileGroupMask(uint32 mask)
Set the mask which all profiles must pass to be enabled.
Definition: OgreProfiler.h:361
bool mInitialized
Whether the GUI elements have been initialized.
Definition: OgreProfiler.h:483
Represents the total timing information of a profile since profiles can be called more than once each...
Definition: OgreProfiler.h:123
Ogre::map< String, ProfileInstance * >::type ProfileChildren
Definition: OgreProfiler.h:180
_StringBase String
Definition: OgreCommon.h:53
Real maxTimePercent
The maximum percentage of frame time this profile has taken.
Definition: OgreProfiler.h:146
Real totalTimePercent
The total percentage of frame time this profile has taken.
Definition: OgreProfiler.h:159
All in-built Ogre profiling will match this mask.
Definition: OgreProfiler.h:84
ulong mTotalFrameTime
The total time each frame takes.
Definition: OgreProfiler.h:496
uint calls
The number of times this profile was called this frame.
Definition: OgreProfiler.h:130
Real minTimePercent
The minimum percentage of frame time this profile has taken.
Definition: OgreProfiler.h:151
An individual profile that will be processed by the Profiler.
Definition: OgreProfiler.h:103
uint32 mProfileMask
Mask to decide whether a type of profile is enabled or not.
Definition: OgreProfiler.h:506
bool watchForLimit(Real limit, bool greaterThan=true)
Definition: OgreProfiler.h:187
unsigned int uint