ivaria/profile.h
Go to the documentation of this file.
00001 /* 00002 Copyright (C) 2007 by Marten Svanfeldt 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 00019 #ifndef __CS_IVARIA_PROFILE_H__ 00020 #define __CS_IVARIA_PROFILE_H__ 00021 00026 #include "csutil/array.h" 00027 #include "csutil/scf_interface.h" 00028 #include "csutil/sysfunc.h" 00029 #include "csutil/threading/atomicops.h" 00030 00031 struct iObjectRegistry; 00032 00033 //#define CS_USE_PROFILER 00034 00035 namespace CS 00036 { 00037 namespace Debug 00038 { 00039 class ProfileZone 00040 { 00041 public: 00042 // Methods 00043 ProfileZone () 00044 : zoneName (0), parentZone (0), totalTime (0), enterCount (0) 00045 {} 00046 00047 ~ProfileZone () 00048 { 00049 delete[] zoneName; 00050 } 00051 00052 // Data 00053 const char* zoneName; 00054 ProfileZone* parentZone; 00055 uint64 totalTime; 00056 uint32 enterCount; 00057 }; 00058 00059 00060 class ProfileCounter 00061 { 00062 public: 00063 // Methods 00064 00065 ProfileCounter () 00066 : counterName (0), counterValue (0) 00067 { 00068 } 00069 00070 ~ProfileCounter () 00071 { 00072 delete[] counterName; 00073 } 00074 00075 // Data 00076 const char* counterName; 00077 uint64 counterValue; 00078 }; 00079 00080 class ProfilerZoneScope 00081 { 00082 public: 00083 ProfilerZoneScope (ProfileZone* zone) 00084 { 00085 this->zone = zone; 00086 startTime = csGetMicroTicks (); 00087 } 00088 00089 ~ProfilerZoneScope () 00090 { 00091 int64 stopTime = csGetMicroTicks (); 00092 00093 zone->enterCount++; 00094 zone->totalTime += (stopTime - startTime); 00095 } 00096 00097 00098 private: 00099 int64 startTime; 00100 ProfileZone* zone; 00101 }; 00102 00103 inline void ProfilerCounterAdd (ProfileCounter* counter) 00104 { 00105 counter->counterValue++; 00106 } 00107 } 00108 } 00109 00110 00114 struct iProfiler : public virtual iBase 00115 { 00116 SCF_INTERFACE (iProfiler, 3,0,1); 00117 00123 CS_DEPRECATED_METHOD_MSG("Old profiling discontinued; check docs for new API") 00124 static void RegisterProfilePoint (const char*, const char*, int, uint32*, 00125 uint32*, uint32*, uint32*) {} 00126 CS_DEPRECATED_METHOD_MSG("Old profiling discontinued; check docs for new API") 00127 static void Dump () {} 00135 virtual CS::Debug::ProfileZone* GetProfileZone (const char* zonename) = 0; 00136 00142 virtual CS::Debug::ProfileCounter* GetProfileCounter (const char* countername) = 0; 00143 00147 virtual void Reset () = 0; 00148 00152 virtual const csArray<CS::Debug::ProfileZone*>& GetProfileZones () = 0; 00153 00157 virtual const csArray<CS::Debug::ProfileCounter*>& GetProfileCounters () = 0; 00158 00167 virtual void StartLogging (const char* filenamebase, 00168 iObjectRegistry* objreg) = 0; 00169 00173 virtual void StopLogging () = 0; 00174 }; 00175 00179 struct iProfilerFactory : public virtual iBase 00180 { 00181 SCF_INTERFACE (iProfilerFactory, 1,0,0); 00182 00186 virtual iProfiler* GetProfiler () = 0; 00187 }; 00188 00189 #ifdef CS_USE_PROFILER 00190 #define CS_DECLARE_PROFILER \ 00191 static iProfiler* CS_DEBUG_Profiler_staticProfilerPtr = 0; \ 00192 static inline iProfiler* CS_DEBUG_Profiler_GetProfiler () \ 00193 { \ 00194 if (!CS_DEBUG_Profiler_staticProfilerPtr) \ 00195 { \ 00196 csRef<iProfilerFactory> fact = \ 00197 scfCreateInstance<iProfilerFactory> ("crystalspace.utilities.profiler"); \ 00198 CS_DEBUG_Profiler_staticProfilerPtr = fact->GetProfiler (); \ 00199 } \ 00200 CS_ASSERT (CS_DEBUG_Profiler_staticProfilerPtr); \ 00201 return CS_DEBUG_Profiler_staticProfilerPtr; \ 00202 } 00203 #define CS_DECLARE_PROFILER_ZONE(name) \ 00204 static CS::Debug::ProfileZone* CS_DEBUG_Profiler_staticProfileZone ## name = 0; \ 00205 static inline CS::Debug::ProfileZone* CS_DEBUG_Profiler_GetProfileZone ## name () \ 00206 {\ 00207 if (!CS_DEBUG_Profiler_staticProfileZone ## name) \ 00208 {\ 00209 CS_DEBUG_Profiler_staticProfileZone ## name = CS_DEBUG_Profiler_GetProfiler ()->GetProfileZone (#name); \ 00210 }\ 00211 return CS_DEBUG_Profiler_staticProfileZone ## name; \ 00212 } 00213 #define CS_DECLARE_PROFILER_COUNTER(name) \ 00214 static CS::Debug::ProfileCounter* CS_DEBUG_Profiler_staticProfileCounter ## name = 0; \ 00215 static inline CS::Debug::ProfileCounter* CS_DEBUG_Profiler_GetProfileCounter ## name () \ 00216 {\ 00217 if (!CS_DEBUG_Profiler_staticProfileCounter ## name) \ 00218 {\ 00219 CS_DEBUG_Profiler_staticProfileCounter ## name = CS_DEBUG_Profiler_GetProfiler ()->GetProfileZone (#name); \ 00220 }\ 00221 return CS_DEBUG_Profiler_staticProfileCounter ## name; \ 00222 } 00223 #define CS_PROFILER_GET_PROFILER \ 00224 CS_DEBUG_Profiler_GetProfiler () 00225 #define CS_PROFILER_ZONE(name) \ 00226 CS::Debug::ProfilerZoneScope CS_DEBUG_Profiler_zone ## name ## __LINE__ \ 00227 (CS_DEBUG_Profiler_GetProfileZone ## name()); 00228 #define CS_PROFILER_COUNTER(name) \ 00229 CS::Debug::ProfilerCounterAdd (CS_DEBUG_Profiler_GetProfileCounter ## name()); 00230 #define CS_PROFILER_START_LOGGING(filebase, objectreg) \ 00231 CS_DEBUG_Profiler_GetProfiler ()->StartLogging (filebase, objectreg); 00232 #define CS_PROFILER_STOP_LOGGING() \ 00233 CS_DEBUG_Profiler_GetProfiler ()->StopLogging (); 00234 #define CS_PROFILER_RESET() \ 00235 CS_DEBUG_Profiler_GetProfiler ()->Reset (); 00236 #else 00237 00238 #define CS_DECLARE_PROFILER 00239 #define CS_DECLARE_PROFILER_ZONE(name) 00240 #define CS_DECLARE_PROFILER_COUNTER(name) 00241 #define CS_PROFILER_GET_PROFILER (0) 00242 #define CS_PROFILER_ZONE(name) 00243 #define CS_PROFILER_COUNTER(name) 00244 #define CS_PROFILER_START_LOGGING(filebase, objectreg) 00245 #define CS_PROFILER_STOP_LOGGING() 00246 #define CS_PROFILER_RESET() 00247 #endif 00248 00249 00250 #endif
Generated for Crystal Space 1.4.1 by doxygen 1.7.1