glperformance.h

00001 /*
00002 Copyright (c) 2000-2003 Lee Thomason (www.grinninglizard.com)
00003 Grinning Lizard Utilities.
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any 
00010 purpose, including commercial applications, and to alter it and 
00011 redistribute it freely, subject to the following restrictions:
00012 
00013 1. The origin of this software must not be misrepresented; you must 
00014 not claim that you wrote the original software. If you use this 
00015 software in a product, an acknowledgment in the product documentation 
00016 would be appreciated but is not required.
00017 
00018 2. Altered source versions must be plainly marked as such, and 
00019 must not be misrepresented as being the original software.
00020 
00021 3. This notice may not be removed or altered from any source 
00022 distribution.
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;                      // # of calls
00055         U32 totalTime;          // total time - in no particular unit (multiple of clock cycle)
00056 };
00057 
00058 struct ProfileData
00059 {
00060         U32 totalTime;          // total time of all items - no particular unit
00061         U32 count;                      // number of items
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                         //pushad;       // don't need - aren't using "emit"
00082                         cpuid;          // force all previous instructions to complete - else out of order execution can confuse things
00083                         rdtsc;
00084                         mov u.split.hi, edx;
00085                         mov u.split.lo, eax;
00086                         //popad;
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

Generated on Thu Jul 20 20:45:31 2006 for Kyra by  doxygen 1.4.7