00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00031 #include "AEngine.h"
00032 #include "AState.h"
00033 #include "AConstants.h"
00034 #include "ADevice.h"
00035 #include "ASharedFunc.h"
00036
00037
00038 inline __int64 getOSTime()
00039 {
00040 __int64 time;
00041 int *dest_low = (int*) (&time);
00042 int *dest_high = dest_low + 1;
00043
00044 __asm
00045 {
00046 rdtsc;
00047 mov ebx, dest_low;
00048 mov ecx, dest_high;
00049 mov [ebx], eax;
00050 mov [ecx], edx;
00051 }
00052
00053 return time;
00054 }
00055
00056 void profiler_begin(string s)
00057 {
00058 map<string,__int64>::iterator iter;
00059 iter = AS.profiler_startTime.find(s);
00060 if( iter==AS.profiler_startTime.end() )
00061 {
00062 AS.profiler_startTime[s] = getOSTime();
00063 AS.profiler_totalTime[s] = 0;
00064 AS.profiler_names.push_back(s);
00065 }
00066 else
00067 {
00068 AS.profiler_startTime[s] = getOSTime();
00069 }
00070 }
00071
00072 void profiler_end(string s)
00073 {
00074 map<string,__int64>::iterator iter;
00075 iter = AS.profiler_totalTime.find(s);
00076 if( iter==AS.profiler_totalTime.end() )
00077 {
00078 throw Error("Profiler_end("+s+") not preceded by Profiler_begin");
00079 }
00080 else
00081 {
00082 iter->second += getOSTime() - AS.profiler_startTime.find(s)->second;
00083 }
00084 }
00085
00086 void profiler_reset()
00087 {
00088 for( UINT i=0; i<AS.profiler_names.size(); i++ )
00089 AS.profiler_totalTime.find( AS.profiler_names[i] )->second = 0;
00090 AS.profiler_RDTSClastReset = getOSTime();
00091 AS.profiler_MSlastReset = sys_getMilliSec();
00092 }
00093
00094 void profiler_render()
00095 {
00096 sys_console() << "-------BEGIN-PROFILER-SUMMARY-------" << endl;
00097 __int64 t = getOSTime();
00098 __int64 deltaT = t - AS.profiler_RDTSClastReset;
00099 for( UINT i=0; i<AS.profiler_names.size(); i++ )
00100 {
00101 string s = AS.profiler_names[i];
00102 FLOAT percent = AS.profiler_totalTime.find(s)->second * 100.0f / deltaT;
00103 while( s.length()<24 ) s += " ";
00104 sys_console() << s << percent << endl;
00105 }
00106 }
00107