True Axis Physics SDK 1.2.0.1 Beta Documentation
www.trueaxis.com

Profiler.h

00001 //---------------------------------------------------------------------------------
00002 // File Name: Profiler.h
00003 // Description: For profiling the running speed of sections of code
00004 //
00005 // Copyright (C) 2004 - 2006 True Axis Pty Ltd, Australia. 
00006 // All Rights Reserved.
00007 //
00008 // History:
00009 //      Created File.
00010 //---------------------------------------------------------------------------------
00011 
00012 #ifndef TA_PROFILER_H
00013 #define TA_PROFILER_H
00014 
00015 #ifndef TA_SINGLETON_H
00016 #include "Singleton.h"
00017 #endif // TA_SINGLETON_H
00018 
00019 #ifndef TA_STRING_H
00020 #include "String.h"
00021 #endif // TA_STRING_H
00022 
00023 #ifndef TA_LIST_H
00024 #include "List.h"
00025 #endif // TA_LIST_H
00026 
00027 
00028 #ifndef TA_PROFILER_ENABLED
00029 //#define TA_PROFILER_ENABLED
00030 #endif // TA_PROFILER_ENABLED
00031 #ifndef TA_TIMER_ENABLED
00032 //#define TA_TIMER_ENABLED
00033 #endif // TA_TIMER_ENABLED
00034 
00035 #ifndef TA_TIMER_H
00036 #include "Timer.h"
00037 #endif // TA_TIMER_H
00038 
00039 namespace TA
00040 {
00041 
00042 #ifdef TA_PROFILER_ENABLED
00043 
00044 // Can be use in a hearer file if a profile section needs to be used across source files.
00045 #define PROFILER_EXTERN_SECTION(name)               \
00046 extern Profiler::Section s_profilerSection
00047 
00048 // Define a profile section in the source file. Only use globally.
00049 #define PROFILER_DEFINE_SECTION(name)               \
00050 Profiler::Section s_profilerSection##name(#name)
00051 
00052 #define PROFILER_ENTER_SECTION(name)                \
00053 s_profilerSection##name.Enter()
00054 
00055 #define PROFILER_EXIT_SECTION(name)                 \
00056 s_profilerSection##name.Exit()
00057 
00058 // Auto section does a enter section where used and does an exit section when the scope is left
00059 // by creating some data on the stack.
00060 #define PROFILER_AUTO_SECTION(name)                 \
00061 Profiler::AutoSection autoSection##name(s_profilerSection##name)
00062 
00063 #else 
00064 
00065 #define PROFILER_EXTERN_SECTION(name)
00066 #define PROFILER_DEFINE_SECTION(name)
00067 #define PROFILER_ENTER_SECTION(name)
00068 #define PROFILER_EXIT_SECTION(name)
00069 #define PROFILER_AUTO_SECTION(name)
00070 
00071 #endif // TA_PROFILER_ENABLED
00072 
00073 #ifdef PROFILER_ENABLED
00074 
00075 class TACOMMON_CLASS Profiler
00076 {
00077     SINGLETON_DEFINE(Profiler)
00078 public:
00079     // Use the macros above rather than this class directly.
00080     class Section
00081     {
00082     public:
00083         // No default construction because it is impractical in this case
00084         // for use with the macros. 
00085         Section(const char* szName);
00086         ~Section();
00087 
00088         void Clear();
00089         void Reset();
00090 
00091         const String& GetName() const { return m_strName; }
00092         s64 GetTime() const { return m_nAverageTime; }//m_nTotalTime; }
00093         s64 GetCount() const { return m_nEnterCount; }
00094 
00095         void Enter();
00096         void Exit();
00097 
00098     private:
00099         s64 m_nEnterTime;
00100         s64 m_nAverageTime;
00101         int m_nTotalTime;
00102         int m_nEnterCount;
00103         int m_nExitCount;
00104         String m_strName;
00105     };
00106 
00107     class AutoSection
00108     {
00109     public:
00110         // No default construction because it is impractical in this case
00111         // for use with the macros.
00112         AutoSection(Section& section);
00113         ~AutoSection();
00114 
00115     private:
00116         Section& m_section;
00117     };
00118 
00119     typedef List<Section*>::ConstIterator ConstIterator;
00120 
00121     void Reset();
00122 
00123     void AddSection(Section* pSection);
00124     void RemoveSection(Section* pSection);
00125 
00126     ConstIterator GetConstIterator() const { return m_sectionList.GetConstIterator(); }
00127 
00128 private:
00129     typedef List<Section*>::Iterator Iterator;
00130 
00131     List<Section*> m_sectionList;
00132 
00133     Profiler();
00134     ~Profiler();
00135 
00136     void Initialise();
00137     void Finalise();
00138 
00139 };
00140 
00141 #endif // TA_PROFILER_ENABLED
00142 
00143 }
00144 
00145 #ifdef TA_PROFILER_ENABLED
00146 #include "Profiler.inl"
00147 #endif // TA_PROFILER_ENABLED
00148 
00149 #endif // TA_PROFILER_H
00150 


© Copyright 2004-2006 TRUE AXIS PTY LTD Australia. All rights reserved.