Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

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 #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;                      // # of calls
00056         U32 totalTime;          // total time - in no particular unit (multiple of clock cycle)
00057 };
00058 
00059 struct ProfileData
00060 {
00061         U32 totalTime;          // total time of all items - no particular unit
00062         U32 count;                      // number of items
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                         //pushad;       // don't need - aren't using "emit"
00083                         cpuid;          // force all previous instructions to complete - else out of order execution can confuse things
00084                         rdtsc;
00085                         mov u.split.hi, edx;
00086                         mov u.split.lo, eax;
00087                         //popad;
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

Generated on Sun Sep 25 16:25:49 2005 for Kyra by  doxygen 1.4.3