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-2013 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 "OgreString.h"
47 #include "OgreHeaderPrefix.h"
48 
49 #if OGRE_PROFILING == 1
50 # define OgreProfile( a ) Ogre::Profile _OgreProfileInstance( (a) )
51 # define OgreProfileBegin( a ) Ogre::Profiler::getSingleton().beginProfile( (a) )
52 # define OgreProfileEnd( a ) Ogre::Profiler::getSingleton().endProfile( (a) )
53 # define OgreProfileGroup( a, g ) Ogre::Profile _OgreProfileInstance( (a), (g) )
54 # define OgreProfileBeginGroup( a, g ) Ogre::Profiler::getSingleton().beginProfile( (a), (g) )
55 # define OgreProfileEndGroup( a, g ) Ogre::Profiler::getSingleton().endProfile( (a), (g) )
56 # define OgreProfileBeginGPUEvent( g ) Ogre::Profiler::getSingleton().beginGPUEvent(g)
57 # define OgreProfileEndGPUEvent( g ) Ogre::Profiler::getSingleton().endGPUEvent(g)
58 # define OgreProfileMarkGPUEvent( e ) Ogre::Profiler::getSingleton().markGPUEvent(e)
59 #else
60 # define OgreProfile( a )
61 # define OgreProfileBegin( a )
62 # define OgreProfileEnd( a )
63 # define OgreProfileGroup( a, g )
64 # define OgreProfileBeginGroup( a, g )
65 # define OgreProfileEndGroup( a, g )
66 # define OgreProfileBeginGPUEvent( e )
67 # define OgreProfileEndGPUEvent( e )
68 # define OgreProfileMarkGPUEvent( e )
69 #endif
70 
71 namespace Ogre {
81  {
83  OGREPROF_USER_DEFAULT = 0x00000001,
85  OGREPROF_ALL = 0xFF000000,
87  OGREPROF_GENERAL = 0x80000000,
89  OGREPROF_CULLING = 0x40000000,
91  OGREPROF_RENDERING = 0x20000000
92  };
93 
105  public ProfilerAlloc
106  {
107 
108  public:
109  Profile(const String& profileName, uint32 groupID = (uint32)OGREPROF_USER_DEFAULT);
110  ~Profile();
111 
112  protected:
113 
118 
119  };
120 
124  struct ProfileFrame
125  {
126 
129 
132 
135 
136  };
137 
140  {
145 
150 
155 
158 
163 
167 
170 
171  };
172 
175  {
176  friend class Profiler;
177  public:
178  ProfileInstance(void);
179  virtual ~ProfileInstance(void);
180 
182 
183  void logResults();
184  void reset();
185 
188  inline bool watchForLimit(Real limit, bool greaterThan = true)
189  {
190  if (greaterThan)
191  return history.currentTimePercent > limit;
192  else
193  return history.currentTimePercent < limit;
194  }
195 
196  bool watchForMax(const String& profileName);
197  bool watchForMin(const String& profileName);
198  bool watchForLimit(const String& profileName, Real limit, bool greaterThan = true);
199 
202 
205 
207 
210 
212 
215 
219 
222  };
223 
230  {
231  public:
233  {
237  DISPLAY_MILLISECONDS
238  };
239 
240  ProfileSessionListener() : mDisplayMode(DISPLAY_MILLISECONDS) {}
242 
244  virtual void initializeSession() = 0;
245 
247  virtual void finializeSession() = 0;
248 
253  virtual void changeEnableState(bool enabled) {};
254 
256  virtual void displayResults(const ProfileInstance& instance, ulong maxTotalFrameTime) {};
257 
259  void setDisplayMode(DisplayMode d) { mDisplayMode = d; }
260 
262  DisplayMode getDisplayMode() const { return mDisplayMode; }
263 
264  protected:
267  };
268 
281  public Singleton<Profiler>,
282  public ProfilerAlloc
283  {
284  public:
285  Profiler();
286  ~Profiler();
287 
289  void setTimer(Timer* t);
290 
292  Timer* getTimer();
293 
307  void beginProfile(const String& profileName, uint32 groupID = (uint32)OGREPROF_USER_DEFAULT);
308 
323  void endProfile(const String& profileName, uint32 groupID = (uint32)OGREPROF_USER_DEFAULT);
324 
328  void beginGPUEvent(const String& event);
329 
333  void endGPUEvent(const String& event);
334 
338  void markGPUEvent(const String& event);
339 
345  void setEnabled(bool enabled);
346 
348  bool getEnabled() const;
349 
353  void enableProfile(const String& profileName);
354 
358  void disableProfile(const String& profileName);
359 
362  void setProfileGroupMask(uint32 mask) { mProfileMask = mask; }
365  uint32 getProfileGroupMask() const { return mProfileMask; }
366 
372  bool watchForMax(const String& profileName);
373 
379  bool watchForMin(const String& profileName);
380 
390  bool watchForLimit(const String& profileName, Real limit, bool greaterThan = true);
391 
393  void logResults();
394 
396  void reset();
397 
399  void setUpdateDisplayFrequency(uint freq);
400 
402  uint getUpdateDisplayFrequency() const;
403 
410  void addListener(ProfileSessionListener* listener);
411 
418  void removeListener(ProfileSessionListener* listener);
419 
435  static Profiler& getSingleton(void);
451  static Profiler* getSingletonPtr(void);
452 
453  protected:
454  friend class ProfileInstance;
455 
458 
460  void initialize();
461 
462  void displayResults();
463 
465  void processFrameStats(void);
467  void processFrameStats(ProfileInstance* instance, Real& maxFrameTime);
468 
470  void changeEnableState();
471 
472  // lol. Uses typedef; put's original container type in name.
475 
479 
482 
485 
489 
492 
495 
498 
500  bool mEnabled;
501 
505 
508 
511 
515 
516 
517  }; // end class
521 } // end namespace
522 
523 #include "OgreHeaderSuffix.h"
524 
525 #endif
OgreHeaderSuffix.h
Ogre::Profile::mName
String mName
The name of this profile.
Definition: OgreProfiler.h:115
Ogre::ProfileInstance::reset
void reset()
Ogre::ProfileHistory::totalTimeMillisecs
Real totalTimeMillisecs
The total frame time this profile has taken in milliseconds.
Definition: OgreProfiler.h:162
Ogre::Profiler::mDisabledProfiles
DisabledProfileMap mDisabledProfiles
Holds the names of disabled profiles.
Definition: OgreProfiler.h:481
Ogre::Profiler::mUpdateDisplayFrequency
uint mUpdateDisplayFrequency
The number of frames that must elapse before the current frame display is updated.
Definition: OgreProfiler.h:488
Ogre::Profile
An individual profile that will be processed by the Profiler.
Definition: OgreProfiler.h:104
Ogre::AllocatedObject
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Definition: OgreMemoryAllocatedObject.h:58
Ogre::ProfileInstance::ProfileChildren
Ogre::map< String, ProfileInstance * >::type ProfileChildren
Definition: OgreProfiler.h:181
Ogre
Definition: OgreAndroidLogListener.h:34
Ogre::ProfileSessionListener::~ProfileSessionListener
virtual ~ProfileSessionListener()
Definition: OgreProfiler.h:241
Ogre::Profiler::ProfileChildren
ProfileInstance::ProfileChildren ProfileChildren
Definition: OgreProfiler.h:474
Ogre::OGREPROF_ALL
All in-built Ogre profiling will match this mask.
Definition: OgreProfiler.h:85
Ogre::ProfileHistory::hierarchicalLvl
uint hierarchicalLvl
The hierarchical level of this profile, 0 being the root profile.
Definition: OgreProfiler.h:169
Ogre::ProfileSessionListener::mDisplayMode
DisplayMode mDisplayMode
How to display the overlay.
Definition: OgreProfiler.h:266
OgreSingleton.h
Ogre::ProfileInstance::history
ProfileHistory history
Definition: OgreProfiler.h:211
Ogre::Profiler::mTimer
Timer * mTimer
The timer used for profiling.
Definition: OgreProfiler.h:494
Ogre::set
Definition: OgrePrerequisites.h:519
Ogre::ProfileSessionListener::getDisplayMode
DisplayMode getDisplayMode() const
Get the display mode for the overlay.
Definition: OgreProfiler.h:262
Ogre::Profiler::mTotalFrameTime
ulong mTotalFrameTime
The total time each frame takes.
Definition: OgreProfiler.h:497
Ogre::Profiler::mRoot
ProfileInstance mRoot
Definition: OgreProfiler.h:478
Ogre::ProfileInstance::watchForMin
bool watchForMin(void)
Definition: OgreProfiler.h:187
Ogre::ProfileSessionListener::ProfileSessionListener
ProfileSessionListener()
Definition: OgreProfiler.h:240
Ogre::OGREPROF_CULLING
Culling.
Definition: OgreProfiler.h:89
Ogre::Profiler::mMaxTotalFrameTime
ulong mMaxTotalFrameTime
The max frame time recorded.
Definition: OgreProfiler.h:510
Ogre::uint32
unsigned int uint32
Definition: OgrePlatform.h:359
Ogre::ProfileSessionListener::setDisplayMode
void setDisplayMode(DisplayMode d)
Set the display mode for the overlay.
Definition: OgreProfiler.h:259
Ogre::String
_StringBase String
Definition: OgrePrerequisites.h:439
Ogre::ProfileFrame::frameTime
ulong frameTime
The total time this profile has taken this frame.
Definition: OgreProfiler.h:128
Ogre::ProfileFrame::calls
uint calls
The number of times this profile was called this frame.
Definition: OgreProfiler.h:131
Ogre::ProfileGroupMask
ProfileGroupMask
List of reserved profiling masks.
Definition: OgreProfiler.h:80
Ogre::Profile::mGroupID
uint32 mGroupID
The group ID.
Definition: OgreProfiler.h:117
Ogre::ProfileHistory::maxTimeMillisecs
Real maxTimeMillisecs
The maximum frame time this profile has taken in milliseconds.
Definition: OgreProfiler.h:149
Ogre::ProfileInstance
Represents an individual profile call.
Definition: OgreProfiler.h:174
Ogre::ProfileInstance::frameNumber
ulong frameNumber
Definition: OgreProfiler.h:209
Ogre::ProfileHistory::currentTimePercent
Real currentTimePercent
The current percentage of frame time this profile has taken.
Definition: OgreProfiler.h:142
Ogre::Timer
Timer class.
Definition: Android/OgreTimerImp.h:37
Ogre::ProfileHistory::totalCalls
ulong totalCalls
The total number of times this profile was called (used to calculate average)
Definition: OgreProfiler.h:166
Ogre::Profiler::DisabledProfileMap
set< String >::type DisabledProfileMap
Definition: OgreProfiler.h:473
Ogre::ProfileInstance::name
String name
The name of the profile.
Definition: OgreProfiler.h:201
Ogre::Profiler::mCurrent
ProfileInstance * mCurrent
Definition: OgreProfiler.h:476
OgreHeaderPrefix.h
Ogre::Profiler::mProfileMask
uint32 mProfileMask
Mask to decide whether a type of profile is enabled or not.
Definition: OgreProfiler.h:507
Ogre::Profiler::TProfileSessionListener
vector< ProfileSessionListener * >::type TProfileSessionListener
Definition: OgreProfiler.h:456
Ogre::ProfileHistory::currentTimeMillisecs
Real currentTimeMillisecs
The current frame time this profile has taken in milliseconds.
Definition: OgreProfiler.h:144
Ogre::ProfileHistory
Represents a history of each profile during the duration of the app.
Definition: OgreProfiler.h:139
Ogre::ProfileInstance::hierarchicalLvl
uint hierarchicalLvl
The hierarchical level of this profile, 0 being the root profile.
Definition: OgreProfiler.h:221
OgrePrerequisites.h
Ogre::Profiler::mListeners
TProfileSessionListener mListeners
Definition: OgreProfiler.h:457
Ogre::ProfileInstance::children
ProfileChildren children
Definition: OgreProfiler.h:206
Ogre::Singleton
Template class for creating single-instance global classes.
Definition: OgreSingleton.h:64
Ogre::ProfileHistory::numCallsThisFrame
uint numCallsThisFrame
The number of times this profile has been called each frame.
Definition: OgreProfiler.h:157
Ogre::ProfileFrame::hierarchicalLvl
uint hierarchicalLvl
The hierarchical level of this profile, 0 being the main loop.
Definition: OgreProfiler.h:134
_OgreExport
#define _OgreExport
Definition: OgrePlatform.h:257
Ogre::Profiler::mAverageFrameTime
Real mAverageFrameTime
Rolling average of millisecs.
Definition: OgreProfiler.h:513
Ogre::ProfileSessionListener
ProfileSessionListener should be used to visualize profile results.
Definition: OgreProfiler.h:229
Ogre::ProfileInstance::~ProfileInstance
virtual ~ProfileInstance(void)
Ogre::ProfileHistory::maxTimePercent
Real maxTimePercent
The maximum percentage of frame time this profile has taken.
Definition: OgreProfiler.h:147
Ogre::ProfileInstance::accum
ulong accum
Represents the total time of all child profiles to subtract from this profile.
Definition: OgreProfiler.h:218
Ogre::ulong
unsigned long ulong
Definition: OgrePrerequisites.h:115
Ogre::ProfileInstance::ProfileInstance
ProfileInstance(void)
Ogre::Profiler::mLast
ProfileInstance * mLast
Definition: OgreProfiler.h:477
Ogre::ProfileSessionListener::DISPLAY_PERCENTAGE
Display % frame usage on the overlay.
Definition: OgreProfiler.h:235
Ogre::ProfileInstance::watchForLimit
bool watchForLimit(Real limit, bool greaterThan=true)
Definition: OgreProfiler.h:188
Ogre::OGREPROF_RENDERING
Rendering.
Definition: OgreProfiler.h:91
Ogre::Profiler
The profiler allows you to measure the performance of your code.
Definition: OgreProfiler.h:280
Ogre::ProfileSessionListener::displayResults
virtual void displayResults(const ProfileInstance &instance, ulong maxTotalFrameTime)
Here we get the real profiling information which we can use.
Definition: OgreProfiler.h:256
Ogre::map::type
std::map< K, V, P, A > type
Definition: OgrePrerequisites.h:536
Ogre::uint
unsigned int uint
Definition: OgrePrerequisites.h:114
Ogre::ProfileInstance::currTime
ulong currTime
The time this profile was started.
Definition: OgreProfiler.h:214
Ogre::Profiler::setProfileGroupMask
void setProfileGroupMask(uint32 mask)
Set the mask which all profiles must pass to be enabled.
Definition: OgreProfiler.h:362
Ogre::ProfileInstance::parent
ProfileInstance * parent
The name of the parent, null if root.
Definition: OgreProfiler.h:204
Ogre::OGREPROF_GENERAL
General processing.
Definition: OgreProfiler.h:87
Ogre::Real
float Real
Software floating point type.
Definition: OgrePrerequisites.h:70
Ogre::ProfileHistory::totalTimePercent
Real totalTimePercent
The total percentage of frame time this profile has taken.
Definition: OgreProfiler.h:160
Ogre::ProfileHistory::minTimePercent
Real minTimePercent
The minimum percentage of frame time this profile has taken.
Definition: OgreProfiler.h:152
Ogre::ProfileSessionListener::DisplayMode
DisplayMode
Definition: OgreProfiler.h:232
Ogre::ProfileFrame
Represents the total timing information of a profile since profiles can be called more than once each...
Definition: OgreProfiler.h:124
Ogre::Profiler::mCurrentFrame
uint mCurrentFrame
The number of elapsed frame, used with mUpdateDisplayFrequency.
Definition: OgreProfiler.h:491
Ogre::Profiler::getProfileGroupMask
uint32 getProfileGroupMask() const
Get the mask which all profiles must pass to be enabled.
Definition: OgreProfiler.h:365
OgreString.h
Ogre::ProfileInstance::logResults
void logResults()
Ogre::vector
Definition: OgrePrerequisites.h:491
Ogre::OGREPROF_USER_DEFAULT
User default profile.
Definition: OgreProfiler.h:83
Ogre::ProfileHistory::minTimeMillisecs
Real minTimeMillisecs
The minimum frame time this profile has taken in milliseconds.
Definition: OgreProfiler.h:154
Ogre::Profiler::mResetExtents
bool mResetExtents
Definition: OgreProfiler.h:514
Ogre::Profiler::mInitialized
bool mInitialized
Whether the GUI elements have been initialized.
Definition: OgreProfiler.h:484
Ogre::ProfileInstance::watchForMax
bool watchForMax(void)
Definition: OgreProfiler.h:186
Ogre::ProfileInstance::frame
ProfileFrame frame
Definition: OgreProfiler.h:208
Ogre::ProfileSessionListener::changeEnableState
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:253
Ogre::Profiler::mNewEnableState
bool mNewEnableState
Keeps track of the new enabled/disabled state that the user has requested which will be applied after...
Definition: OgreProfiler.h:504
Ogre::Profiler::mEnabled
bool mEnabled
Whether this profiler is enabled.
Definition: OgreProfiler.h:500

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.