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
00037 namespace grinliz {
00038
00039 const int GL_MAX_PROFILE_DATAITEM = 64;
00040
00041 struct PerformanceData
00042 {
00043 PerformanceData( const char* name );
00044
00045 const char* name;
00046 U32 count;
00047 U64 totalTime;
00048 };
00049
00050
00051 struct ProfileDataItem
00052 {
00053 const char* name;
00054 U32 count;
00055 U32 totalTime;
00056 };
00057
00058 struct ProfileData
00059 {
00060 U32 totalTime;
00061 U32 count;
00062 grinliz::ProfileDataItem item[ GL_MAX_PROFILE_DATAITEM ];
00063 };
00064
00065
00066 #ifdef _MSC_VER
00067 inline U64 FastTime()
00068 {
00069 union
00070 {
00071 U64 result;
00072 struct
00073 {
00074 U32 lo;
00075 U32 hi;
00076 } split;
00077 } u;
00078 u.result = 0;
00079
00080 _asm {
00081
00082 cpuid;
00083 rdtsc;
00084 mov u.split.hi, edx;
00085 mov u.split.lo, eax;
00086
00087 }
00088 return u.result;
00089 }
00090
00091 #else
00092 inline U64 FastTime()
00093 {
00094 #ifdef __GNUC__
00095 U64 val;
00096 __asm__ __volatile__ ("rdtsc" : "=A" (val));
00097 return val;
00098 #else
00099 return SDL_GetTicks();
00100 #endif
00101 }
00102 #endif
00103
00115 class Performance
00116 {
00117 friend struct PerformanceData;
00118 public:
00119
00120 Performance( PerformanceData* data ) {
00121 this->data = data;
00122 ++data->count;
00123 start = FastTime();
00124 }
00125
00126 ~Performance()
00127 {
00128 U64 end = FastTime();
00129 GLASSERT( end >= start );
00130 data->totalTime += ( end - start );
00131 }
00132
00134 static void Dump( FILE* fp, const char* desc );
00136 static const grinliz::ProfileData& GetData();
00138 static void Clear();
00140 static const grinliz::ProfileData& GetSortedData();
00141
00142
00143 protected:
00144
00145 static PerformanceData* map[ GL_MAX_PROFILE_DATAITEM ];
00146 static ProfileData profile;
00147 static ProfileData sortedProfile;
00148 static int numMap;
00149
00150 PerformanceData* data;
00151 U64 start;
00152 };
00153 };
00154
00155 #endif