Profiler.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 /*
00006 An example of profiling a section of code:
00007   ProfileStart("Code Bit 1");
00008   DoSomething();
00009   DoSomethingElse();
00010   ProfileStop("Code Bit 1");
00011 
00012 An example of automatically profiling the scope of a function:
00013   void SomeFunction()
00014   {
00015       ProfileScope("name of a profile");
00016       //..normal code and stuff....
00017   }
00018 */
00019 /*
00020 Written by Luke Lenhart (2002)
00021  Updated in late 2004 to be thread-safe, and to use stl
00022  Updated 2005 for auto-scope profiling
00023  Updated 2007 for multiple platforms
00024 See /docs/License.txt for details on how this code may be used.
00025 */
00026 
00027 #ifndef _CProfiler_
00028 #define _CProfiler_
00029 
00030 #include "Setup.h"
00031 
00032 //macro replacement
00033 #if defined(_DEBUG) || defined(PROFILE_RELEASE)
00035     #define ProfileStart(name) _instProfile._ProfileStart(name,__FILE__)
00037     #define ProfileStop(name) _instProfile._ProfileStop(name,__FILE__)
00039     #define ProfileScope(name) CAutoProfileHelper _auto_scope_profiler(name); ProfileStart(name)
00040 #else
00041     #define ProfileStart(name)
00042     #define ProfileStop(name)
00043     #define ProfileScope(name)
00044 #endif
00045 
00046 
00047 //
00048 #if defined(_DEBUG) || defined(PROFILE_RELEASE)
00049 
00050 #include <string>
00051 #include <list>
00052 #include "Locks.h"
00053 #include "Types.h"
00054 #include "Timer.h"
00055 
00056 //main profiler
00057 class CProfiler
00058 {
00059 public:
00060     CProfiler();
00061     virtual ~CProfiler();
00062 
00063     void _ProfileStart(const std::string &pName, const std::string &fName);
00064     void _ProfileStop(const std::string &pName, const std::string &fName);
00065 
00066 private:
00067     struct SProfile
00068     {
00069         std::string name; //name for this profile
00070         std::string file; //filename for this profile
00071 
00072         double timeTotal; //total time spent
00073         double timeMax; //largest time spent
00074         double timeMin; //smallest time spent
00075 
00076         MPMA::Timer timeStart; //time of last profile start
00077 
00078         int samples; //number of samples taken
00079         
00080         bool started;
00081 
00082         inline bool operator <(const SProfile &o) { return timeTotal>o.timeTotal; }
00083     };
00084 
00085     std::list<SProfile> llProf; //profile list
00086 
00087     MPMA::Timer timeStart; //time class was created
00088 
00089     //
00090 
00091     void Write(FILE* f, double val);
00092     void Write(FILE* f, const char* str);
00093     void Write(FILE* f, int num);
00094 
00095     SProfile* FindProfile(const std::string &name);
00096 
00097     void WriteProfilesToFile(FILE *f, double finalTime);
00098     void DeallocProfiles();
00099 
00100     void Error(const char *lpzMsg);
00101 
00102     //thread safety
00103     MPMA::MutexLock crit;
00104 };
00105 
00106 #endif
00107 
00108 //access to profiler object
00109 #if defined(_DEBUG) || defined(PROFILE_RELEASE)
00110  extern CProfiler _instProfile;
00111 #endif
00112 
00113  //auto-scope profiler (exception-safe and return-safe) -- use macro, not this!!
00114 #if defined(_DEBUG) || defined(PROFILE_RELEASE)
00115 class CAutoProfileHelper
00116 {
00117 public:
00118     inline CAutoProfileHelper(const std::string &profileName) { name=profileName; }
00119     inline ~CAutoProfileHelper() { ProfileStop(name); }
00120 
00121 private:
00122     std::string name;
00123 };
00124 #endif
00125 
00126 #endif //#ifdef _CProfiler_

Generated on Sat Aug 9 15:05:05 2008 for MPMA Framework by  doxygen 1.5.6