OgreProfiler.h
Go to the documentation of this file.
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2013 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
/*
Although the code is original, many of the ideas for the profiler were borrowed from
"Real-Time In-Game Profiling" by Steve Rabin which can be found in Game Programming
Gems 1.
This code can easily be adapted to your own non-Ogre project. The only code that is
Ogre-dependent is in the visualization/logging routines and the use of the Timer class.
Enjoy!
*/
#ifndef __Profiler_H__
#define __Profiler_H__
#include "OgrePrerequisites.h"
#include "OgreSingleton.h"
#include "OgreString.h"
#include "OgreHeaderPrefix.h"
#if OGRE_PROFILING == 1
# define OgreProfile( a ) Ogre::Profile _OgreProfileInstance( (a) )
# define OgreProfileBegin( a ) Ogre::Profiler::getSingleton().beginProfile( (a) )
# define OgreProfileEnd( a ) Ogre::Profiler::getSingleton().endProfile( (a) )
# define OgreProfileGroup( a, g ) Ogre::Profile _OgreProfileInstance( (a), (g) )
# define OgreProfileBeginGroup( a, g ) Ogre::Profiler::getSingleton().beginProfile( (a), (g) )
# define OgreProfileEndGroup( a, g ) Ogre::Profiler::getSingleton().endProfile( (a), (g) )
# define OgreProfileBeginGPUEvent( g ) Ogre::Profiler::getSingleton().beginGPUEvent(g)
# define OgreProfileEndGPUEvent( g ) Ogre::Profiler::getSingleton().endGPUEvent(g)
# define OgreProfileMarkGPUEvent( e ) Ogre::Profiler::getSingleton().markGPUEvent(e)
#else
# define OgreProfile( a )
# define OgreProfileBegin( a )
# define OgreProfileEnd( a )
# define OgreProfileGroup( a, g )
# define OgreProfileBeginGroup( a, g )
# define OgreProfileEndGroup( a, g )
# define OgreProfileBeginGPUEvent( e )
# define OgreProfileEndGPUEvent( e )
# define OgreProfileMarkGPUEvent( e )
#endif
namespace Ogre {
enum ProfileGroupMask
{
OGREPROF_USER_DEFAULT = 0x00000001,
OGREPROF_ALL = 0xFF000000,
OGREPROF_GENERAL = 0x80000000,
OGREPROF_CULLING = 0x40000000,
OGREPROF_RENDERING = 0x20000000
};
class _OgreExport Profile :
public ProfilerAlloc
{
public:
Profile(const String& profileName, uint32 groupID = (uint32)OGREPROF_USER_DEFAULT);
~Profile();
protected:
String mName;
uint32 mGroupID;
};
struct ProfileFrame
{
ulong frameTime;
uint calls;
uint hierarchicalLvl;
};
struct ProfileHistory
{
Real currentTimePercent;
Real currentTimeMillisecs;
Real maxTimePercent;
Real maxTimeMillisecs;
Real minTimePercent;
Real minTimeMillisecs;
uint numCallsThisFrame;
Real totalTimePercent;
Real totalTimeMillisecs;
ulong totalCalls;
uint hierarchicalLvl;
};
class ProfileInstance : public ProfilerAlloc
{
friend class Profiler;
public:
ProfileInstance(void);
virtual ~ProfileInstance(void);
typedef Ogre::map<String,ProfileInstance*>::type ProfileChildren;
void logResults();
void reset();
inline bool watchForMax(void) { return history.currentTimePercent == history.maxTimePercent; }
inline bool watchForMin(void) { return history.currentTimePercent == history.minTimePercent; }
inline bool watchForLimit(Real limit, bool greaterThan = true)
{
if (greaterThan)
return history.currentTimePercent > limit;
else
return history.currentTimePercent < limit;
}
bool watchForMax(const String& profileName);
bool watchForMin(const String& profileName);
bool watchForLimit(const String& profileName, Real limit, bool greaterThan = true);
String name;
ProfileInstance* parent;
ProfileChildren children;
ProfileFrame frame;
ulong frameNumber;
ProfileHistory history;
ulong currTime;
ulong accum;
uint hierarchicalLvl;
};
class _OgreExport ProfileSessionListener
{
public:
enum DisplayMode
{
DISPLAY_PERCENTAGE,
DISPLAY_MILLISECONDS
};
ProfileSessionListener() : mDisplayMode(DISPLAY_MILLISECONDS) {}
virtual ~ProfileSessionListener() {}
virtual void initializeSession() = 0;
virtual void finializeSession() = 0;
virtual void changeEnableState(bool enabled) {};
virtual void displayResults(const ProfileInstance& instance, ulong maxTotalFrameTime) {};
void setDisplayMode(DisplayMode d) { mDisplayMode = d; }
DisplayMode getDisplayMode() const { return mDisplayMode; }
protected:
DisplayMode mDisplayMode;
};
class _OgreExport Profiler :
public Singleton<Profiler>,
public ProfilerAlloc
{
public:
Profiler();
~Profiler();
void setTimer(Timer* t);
Timer* getTimer();
void beginProfile(const String& profileName, uint32 groupID = (uint32)OGREPROF_USER_DEFAULT);
void endProfile(const String& profileName, uint32 groupID = (uint32)OGREPROF_USER_DEFAULT);
void beginGPUEvent(const String& event);
void endGPUEvent(const String& event);
void markGPUEvent(const String& event);
void setEnabled(bool enabled);
bool getEnabled() const;
void enableProfile(const String& profileName);
void disableProfile(const String& profileName);
void setProfileGroupMask(uint32 mask) { mProfileMask = mask; }
uint32 getProfileGroupMask() const { return mProfileMask; }
bool watchForMax(const String& profileName);
bool watchForMin(const String& profileName);
bool watchForLimit(const String& profileName, Real limit, bool greaterThan = true);
void logResults();
void reset();
void setUpdateDisplayFrequency(uint freq);
uint getUpdateDisplayFrequency() const;
void addListener(ProfileSessionListener* listener);
void removeListener(ProfileSessionListener* listener);
static Profiler& getSingleton(void);
static Profiler* getSingletonPtr(void);
protected:
friend class ProfileInstance;
typedef vector<ProfileSessionListener*>::type TProfileSessionListener;
TProfileSessionListener mListeners;
void initialize();
void displayResults();
void processFrameStats(void);
void processFrameStats(ProfileInstance* instance, Real& maxFrameTime);
void changeEnableState();
// lol. Uses typedef; put's original container type in name.
typedef set<String>::type DisabledProfileMap;
typedef ProfileInstance::ProfileChildren ProfileChildren;
ProfileInstance* mCurrent;
ProfileInstance* mLast;
ProfileInstance mRoot;
DisabledProfileMap mDisabledProfiles;
bool mInitialized;
uint mUpdateDisplayFrequency;
uint mCurrentFrame;
Timer* mTimer;
ulong mTotalFrameTime;
bool mEnabled;
bool mNewEnableState;
uint32 mProfileMask;
ulong mMaxTotalFrameTime;
Real mAverageFrameTime;
bool mResetExtents;
}; // end class
} // end namespace
#include "OgreHeaderSuffix.h"
#endif
Ogre::OGREPROF_GENERAL
General processing.
Definition: OgreProfiler.h:87
Ogre::Profiler::mRoot
ProfileInstance mRoot
Definition: OgreProfiler.h:478
Ogre::Profiler::mLast
ProfileInstance * mLast
Definition: OgreProfiler.h:477
Ogre::ProfileHistory::hierarchicalLvl
uint hierarchicalLvl
The hierarchical level of this profile, 0 being the root profile.
Definition: OgreProfiler.h:169
Ogre::ProfileFrame::frameTime
ulong frameTime
The total time this profile has taken this frame.
Definition: OgreProfiler.h:128
Ogre::ProfileInstance::watchForMin
bool watchForMin(void)
Definition: OgreProfiler.h:187
Ogre::ProfileHistory
Represents a history of each profile during the duration of the app.
Definition: OgreProfiler.h:139
Ogre::Profiler::mProfileMask
uint32 mProfileMask
Mask to decide whether a type of profile is enabled or not.
Definition: OgreProfiler.h:507
Ogre::Profiler::setProfileGroupMask
void setProfileGroupMask(uint32 mask)
Set the mask which all profiles must pass to be enabled.
Definition: OgreProfiler.h:362
_OgreExport
#define _OgreExport
Definition: OgrePlatform.h:257
Ogre::Profiler::TProfileSessionListener
vector< ProfileSessionListener * >::type TProfileSessionListener
Definition: OgreProfiler.h:456
Ogre::uint
unsigned int uint
Definition: OgrePrerequisites.h:114
Ogre::Profiler::mListeners
TProfileSessionListener mListeners
Definition: OgreProfiler.h:457
Ogre::ProfileSessionListener::DisplayMode
DisplayMode
Definition: OgreProfiler.h:232
Ogre::Profiler::DisabledProfileMap
set< String >::type DisabledProfileMap
Definition: OgreProfiler.h:473
Ogre::Profiler::mDisabledProfiles
DisabledProfileMap mDisabledProfiles
Holds the names of disabled profiles.
Definition: OgreProfiler.h:481
Ogre::ProfileSessionListener::setDisplayMode
void setDisplayMode(DisplayMode d)
Set the display mode for the overlay.
Definition: OgreProfiler.h:259
Ogre::ProfileInstance::frameNumber
ulong frameNumber
Definition: OgreProfiler.h:209
Ogre::Profiler::mInitialized
bool mInitialized
Whether the GUI elements have been initialized.
Definition: OgreProfiler.h:484
Ogre::ProfileInstance::frame
ProfileFrame frame
Definition: OgreProfiler.h:208
Ogre::Real
float Real
Software floating point type.
Definition: OgrePrerequisites.h:70
Ogre::ProfileSessionListener::ProfileSessionListener
ProfileSessionListener()
Definition: OgreProfiler.h:240
OgreString.h
Ogre::ProfileHistory::totalTimeMillisecs
Real totalTimeMillisecs
The total frame time this profile has taken in milliseconds.
Definition: OgreProfiler.h:162
Ogre::Profiler::mCurrentFrame
uint mCurrentFrame
The number of elapsed frame, used with mUpdateDisplayFrequency.
Definition: OgreProfiler.h:491
Ogre::ProfileInstance
Represents an individual profile call.
Definition: OgreProfiler.h:174
Ogre::ProfileHistory::maxTimeMillisecs
Real maxTimeMillisecs
The maximum frame time this profile has taken in milliseconds.
Definition: OgreProfiler.h:149
Ogre::ProfileSessionListener::~ProfileSessionListener
virtual ~ProfileSessionListener()
Definition: OgreProfiler.h:241
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::mCurrent
ProfileInstance * mCurrent
Definition: OgreProfiler.h:476
Ogre::ProfileInstance::history
ProfileHistory history
Definition: OgreProfiler.h:211
Ogre::ProfileInstance::hierarchicalLvl
uint hierarchicalLvl
The hierarchical level of this profile, 0 being the root profile.
Definition: OgreProfiler.h:221
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::ProfileInstance::ProfileChildren
Ogre::map< String, ProfileInstance * >::type ProfileChildren
Definition: OgreProfiler.h:181
Ogre::Timer
Timer class.
Definition: Android/OgreTimerImp.h:37
Ogre::ProfileInstance::accum
ulong accum
Represents the total time of all child profiles to subtract from this profile.
Definition: OgreProfiler.h:218
Ogre::ProfileHistory::currentTimePercent
Real currentTimePercent
The current percentage of frame time this profile has taken.
Definition: OgreProfiler.h:142
Ogre::Profiler::mTotalFrameTime
ulong mTotalFrameTime
The total time each frame takes.
Definition: OgreProfiler.h:497
Ogre::OGREPROF_USER_DEFAULT
User default profile.
Definition: OgreProfiler.h:83
OgreSingleton.h
Ogre::Profiler
The profiler allows you to measure the performance of your code.
Definition: OgreProfiler.h:280
Ogre::Profiler::ProfileChildren
ProfileInstance::ProfileChildren ProfileChildren
Definition: OgreProfiler.h:474
OgrePrerequisites.h
Ogre::ProfileSessionListener::mDisplayMode
DisplayMode mDisplayMode
How to display the overlay.
Definition: OgreProfiler.h:266
Ogre::map::type
std::map< K, V, P, A > type
Definition: OgrePrerequisites.h:536
Ogre::ProfileFrame::calls
uint calls
The number of times this profile was called this frame.
Definition: OgreProfiler.h:131
Ogre::Profiler::mTimer
Timer * mTimer
The timer used for profiling.
Definition: OgreProfiler.h:494
Ogre::ProfileFrame::hierarchicalLvl
uint hierarchicalLvl
The hierarchical level of this profile, 0 being the main loop.
Definition: OgreProfiler.h:134
Ogre::Profiler::getProfileGroupMask
uint32 getProfileGroupMask() const
Get the mask which all profiles must pass to be enabled.
Definition: OgreProfiler.h:365
Ogre::ProfileGroupMask
ProfileGroupMask
List of reserved profiling masks.
Definition: OgreProfiler.h:80
Ogre::String
_StringBase String
Definition: OgrePrerequisites.h:439
Ogre::OGREPROF_ALL
All in-built Ogre profiling will match this mask.
Definition: OgreProfiler.h:85
Ogre::ProfileSessionListener::DISPLAY_PERCENTAGE
Display % frame usage on the overlay.
Definition: OgreProfiler.h:235
Ogre::ProfileSessionListener::getDisplayMode
DisplayMode getDisplayMode() const
Get the display mode for the overlay.
Definition: OgreProfiler.h:262
Ogre::set
Definition: OgrePrerequisites.h:519
Ogre::ProfileHistory::totalCalls
ulong totalCalls
The total number of times this profile was called (used to calculate average)
Definition: OgreProfiler.h:166
Ogre::ProfileHistory::currentTimeMillisecs
Real currentTimeMillisecs
The current frame time this profile has taken in milliseconds.
Definition: OgreProfiler.h:144
OgreHeaderSuffix.h
Ogre::ProfileInstance::name
String name
The name of the profile.
Definition: OgreProfiler.h:201
Ogre::ProfileHistory::maxTimePercent
Real maxTimePercent
The maximum percentage of frame time this profile has taken.
Definition: OgreProfiler.h:147
Ogre::uint32
unsigned int uint32
Definition: OgrePlatform.h:359
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::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
Definition: OgreAndroidLogListener.h:34
Ogre::ProfileHistory::minTimePercent
Real minTimePercent
The minimum percentage of frame time this profile has taken.
Definition: OgreProfiler.h:152
Ogre::ProfileHistory::numCallsThisFrame
uint numCallsThisFrame
The number of times this profile has been called each frame.
Definition: OgreProfiler.h:157
Ogre::ProfileInstance::children
ProfileChildren children
Definition: OgreProfiler.h:206
Ogre::OGREPROF_RENDERING
Rendering.
Definition: OgreProfiler.h:91
Ogre::Profile::mName
String mName
The name of this profile.
Definition: OgreProfiler.h:115
Ogre::vector
Definition: OgrePrerequisites.h:491
Ogre::ulong
unsigned long ulong
Definition: OgrePrerequisites.h:115
Ogre::ProfileInstance::watchForMax
bool watchForMax(void)
Definition: OgreProfiler.h:186
OgreHeaderPrefix.h
Ogre::OGREPROF_CULLING
Culling.
Definition: OgreProfiler.h:89
Ogre::Singleton
Template class for creating single-instance global classes.
Definition: OgreSingleton.h:64
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::ProfileInstance::watchForLimit
bool watchForLimit(Real limit, bool greaterThan=true)
Definition: OgreProfiler.h:188
Ogre::Profiler::mResetExtents
bool mResetExtents
Definition: OgreProfiler.h:514
Ogre::ProfileInstance::currTime
ulong currTime
The time this profile was started.
Definition: OgreProfiler.h:214
Ogre::ProfileHistory::minTimeMillisecs
Real minTimeMillisecs
The minimum frame time this profile has taken in milliseconds.
Definition: OgreProfiler.h:154
Ogre::Profiler::mMaxTotalFrameTime
ulong mMaxTotalFrameTime
The max frame time recorded.
Definition: OgreProfiler.h:510
Ogre::ProfileHistory::totalTimePercent
Real totalTimePercent
The total percentage of frame time this profile has taken.
Definition: OgreProfiler.h:160
Ogre::Profiler::mEnabled
bool mEnabled
Whether this profiler is enabled.
Definition: OgreProfiler.h:500
Ogre::ProfileInstance::parent
ProfileInstance * parent
The name of the parent, null if root.
Definition: OgreProfiler.h:204
Ogre::AllocatedObject
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Definition: OgreMemoryAllocatedObject.h:58
Ogre::ProfileFrame
Represents the total timing information of a profile since profiles can be called more than once each...
Definition: OgreProfiler.h:124
Ogre::Profile::mGroupID
uint32 mGroupID
The group ID.
Definition: OgreProfiler.h:117

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