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