00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef GRINLIZ_PERFORMANCE_MEASURE
00027 #define GRINLIZ_PERFORMANCE_MEASURE
00028
00029 #ifdef _MSC_VER
00030 #pragma warning( disable : 4530 )
00031 #pragma warning( disable : 4786 )
00032 #endif
00033
00034 #include "gltypes.h"
00035 #include "gldebug.h"
00036 #include <stdio.h>
00037
00038 namespace grinliz {
00039
00040 const int GL_MAX_PROFILE_DATAITEM = 64;
00041
00042 struct PerformanceData
00043 {
00044 PerformanceData( const char* name );
00045
00046 const char* name;
00047 U32 count;
00048 U64 totalTime;
00049 };
00050
00051
00052 struct ProfileDataItem
00053 {
00054 const char* name;
00055 U32 count;
00056 U32 totalTime;
00057 };
00058
00059 struct ProfileData
00060 {
00061 U32 totalTime;
00062 U32 count;
00063 grinliz::ProfileDataItem item[ GL_MAX_PROFILE_DATAITEM ];
00064 };
00065
00066
00067 #ifdef _MSC_VER
00068 inline U64 FastTime()
00069 {
00070 union
00071 {
00072 U64 result;
00073 struct
00074 {
00075 U32 lo;
00076 U32 hi;
00077 } split;
00078 } u;
00079 u.result = 0;
00080
00081 _asm {
00082
00083 cpuid;
00084 rdtsc;
00085 mov u.split.hi, edx;
00086 mov u.split.lo, eax;
00087
00088 }
00089 return u.result;
00090 }
00091
00092 #else
00093 inline U64 FastTime()
00094 {
00095 #ifdef __GNUC__
00096 U64 val;
00097 __asm__ __volatile__ ("rdtsc" : "=A" (val));
00098 return val;
00099 #else
00100 return SDL_GetTicks();
00101 #endif
00102 }
00103 #endif
00104
00116 class Performance
00117 {
00118 friend struct PerformanceData;
00119 public:
00120
00121 Performance( PerformanceData* data ) {
00122 this->data = data;
00123 ++data->count;
00124 start = FastTime();
00125 }
00126
00127 ~Performance()
00128 {
00129 U64 end = FastTime();
00130 GLASSERT( end >= start );
00131 data->totalTime += ( end - start );
00132 }
00133
00135 static void Dump( FILE* fp, const char* desc );
00137 static const grinliz::ProfileData& GetData();
00139 static void Clear();
00140
00141 protected:
00142
00143 static PerformanceData* map[ GL_MAX_PROFILE_DATAITEM ];
00144 static ProfileData profile;
00145 static int numMap;
00146
00147 PerformanceData* data;
00148 U64 start;
00149 };
00150 };
00151
00152 #endif