profiler.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00034 #ifndef __profiler__header__
00035 #define __profiler__header__
00036
00037 #include "gpucalc/singleton.h"
00038 #include "gpucalc/timer.h"
00039
00040 namespace gpucalc
00041 {
00042
00043 const char _ProfilerName [] = "Profiler";
00044
00045
00046 class _GpuCalcExport Profiler: public Singleton<Profiler>
00047 {
00048
00049
00050 OBJ_DISABLE_COPY(Profiler)
00051
00052
00053 public:
00054
00055 Profiler(): Singleton<Profiler>(_ProfilerName)
00056 {
00057 }
00058
00059
00060 ~Profiler()
00061 {
00062 }
00063
00064
00065 void beginProfile(const std::string & where)
00066 {
00067 if (!addToContainer(std::make_pair(Timer::getSingleton().getTime(), -1.0), where, mProfileMap))
00068 {
00069 Warning<ERR_DUPLICATE_ITEM>(this, "Profile label \"" + where + "\" already exists. Profiling does not start.", "Profiler::beginProfile()", __FILE__, __LINE__);
00070 }
00071 }
00072
00073
00074 void endProfile(const std::string & where)
00075 {
00076 double stop = Timer::getSingleton().getTime();
00077
00078 ProfileMap::iterator i = mProfileMap.find(where);
00079 if (i == mProfileMap.end())
00080 {
00081 Warning<ERR_ITEM_NOT_FOUND>(this, "Profile label \"" + where + "\" cannot be found.", "Profiler::endProfile()", __FILE__, __LINE__);
00082 }
00083 else
00084 {
00085 i->second.second = stop;
00086 }
00087 }
00088
00089 static Profiler & getSingleton();
00090
00091
00092 static Profiler * getSingletonPtr();
00093
00094
00095 double getProfilingTime(const std::string & where)
00096 {
00097 ProfileMap::iterator i = mProfileMap.find(where);
00098 if (i == mProfileMap.end())
00099 {
00100 Warning<ERR_ITEM_NOT_FOUND>(this, "Profile label \"" + where + "\" cannot be found.", "Profiler::endProfile()", __FILE__, __LINE__);
00101 return -1.0;
00102 }
00103 else
00104 {
00105 if (i->second.second > 0.0)
00106 {
00107 double result = i->second.second - i->second.first;
00108 mProfileMap.erase(i);
00109 return result;
00110 }
00111 else
00112 {
00113 Except<ERR_INVALID_STATE>(this, "Profiling \"" + where + "\" is not stopped yet.", "Profiler::getProfilingTime()", __FILE__, __LINE__);
00114 return -1.0;
00115 }
00116 }
00117 }
00118
00119 private:
00120
00121 typedef std::pair<double, double> ProfileEntry;
00122 typedef std::map<std::string, ProfileEntry> ProfileMap;
00123 ProfileMap mProfileMap;
00124
00125
00126 };
00127 }
00128 #endif