2 #include "profiler.h"
\r
4 int CProfileSample::lastOpenedSample=-1;
\r
5 int CProfileSample::openSampleCount=0;
\r
6 CProfileSample::profileSample CProfileSample::samples[MAX_PROFILER_SAMPLES];
\r
7 IProfilerOutputHandler *CProfileSample::outputHandler=0;
\r
8 float CProfileSample::rootBegin=0.0f;
\r
9 float CProfileSample::rootEnd=0.0f;
\r
10 bool CProfileSample::bProfilerIsRunning=true;
\r
12 CProfileSample::CProfileSample(std::string sampleName)
\r
14 if(!bProfilerIsRunning)return;
\r
18 for(i=0;i<MAX_PROFILER_SAMPLES;++i)
\r
20 if(!samples[i].bIsValid)
\r
22 if(storeIndex<0)storeIndex=i;
\r
24 if(samples[i].name==sampleName)
\r
26 //this is the sample we want
\r
27 //check that it's not already open
\r
28 assert(!samples[i].bIsOpen && "Tried to profile a sample which was already being profiled");
\r
29 //first, store it's index
\r
31 //the parent sample is the last opened sample
\r
32 iParentIndex=lastOpenedSample;
\r
34 samples[i].parentCount=openSampleCount;
\r
36 samples[i].bIsOpen=true;
\r
37 ++samples[i].callCount;
\r
38 samples[i].startTime=GetTime();
\r
39 //if this has no parent, it must be the 'main loop' sample, so do the global timer
\r
40 if(iParentIndex<0)rootBegin=samples[i].startTime;
\r
45 //we've not found it, so it must be a new sample
\r
46 //use the storeIndex value to store the new sample
\r
47 assert(storeIndex>=0 && "Profiler has run out of sample slots!");
\r
48 samples[storeIndex].bIsValid=true;
\r
49 samples[storeIndex].name=sampleName;
\r
50 iSampleIndex=storeIndex;
\r
51 iParentIndex=lastOpenedSample;
\r
52 lastOpenedSample=storeIndex;
\r
53 samples[i].parentCount=openSampleCount;
\r
55 samples[storeIndex].bIsOpen=true;
\r
56 samples[storeIndex].callCount=1;
\r
58 samples[storeIndex].totalTime=0.0f;
\r
59 samples[storeIndex].childTime=0.0f;
\r
60 samples[storeIndex].startTime=GetTime();
\r
61 if(iParentIndex<0)rootBegin=samples[storeIndex].startTime;
\r
64 CProfileSample::~CProfileSample()
\r
66 if(!bProfilerIsRunning)return;
\r
67 float fEndTime=GetTime();
\r
68 //phew... ok, we're done timing
\r
69 samples[iSampleIndex].bIsOpen=false;
\r
70 //calculate the time taken this profile, for ease of use later on
\r
71 float fTimeTaken = fEndTime - samples[iSampleIndex].startTime;
\r
75 samples[iParentIndex].childTime+=fTimeTaken;
\r
77 //no parent, so this is the end of the main loop sample
\r
80 samples[iSampleIndex].totalTime+=fTimeTaken;
\r
81 lastOpenedSample=iParentIndex;
\r
85 void CProfileSample::Output()
\r
87 if(!bProfilerIsRunning)return;
\r
89 assert(outputHandler && "Profiler has no output handler set");
\r
91 outputHandler->BeginOutput(rootEnd-rootBegin);
\r
93 for(int i=0;i<MAX_PROFILER_SAMPLES; ++i)
\r
95 if(samples[i].bIsValid)
\r
97 float sampleTime, percentage;
\r
98 //calculate the time spend on the sample itself (excluding children)
\r
99 sampleTime = samples[i].totalTime-samples[i].childTime;
\r
100 percentage = ( sampleTime / ( rootEnd - rootBegin ) ) * 100.0f;
\r
102 //add it to the sample's values
\r
104 totalPc=samples[i].averagePc*samples[i].dataCount;
\r
105 totalPc+=percentage; samples[i].dataCount++;
\r
106 samples[i].averagePc=totalPc/samples[i].dataCount;
\r
107 if((samples[i].minPc==-1)||(percentage<samples[i].minPc))samples[i].minPc=percentage;
\r
108 if((samples[i].maxPc==-1)||(percentage>samples[i].maxPc))samples[i].maxPc=percentage;
\r
110 //output these values
\r
111 outputHandler->Sample(samples[i].minPc,
\r
112 samples[i].averagePc,
\r
115 samples[i].callCount,
\r
117 samples[i].parentCount);
\r
119 //reset the sample for next time
\r
120 samples[i].callCount=0;
\r
121 samples[i].totalTime=0;
\r
122 samples[i].childTime=0;
\r
126 outputHandler->EndOutput();
\r
129 void CProfileSample::ResetSample(std::string strName)
\r
131 for(int i=0;i<MAX_PROFILER_SAMPLES; ++i)
\r
133 if((samples[i].bIsValid)&&(samples[i].name==strName))
\r
136 //reset avg/min/max ONLY
\r
137 //because the sample may be running
\r
138 samples[i].maxPc=samples[i].minPc=-1;
\r
139 samples[i].dataCount=0;
\r
145 void CProfileSample::ResetAll()
\r
147 for(int i=0;i<MAX_PROFILER_SAMPLES;++i)
\r
149 if(samples[i].bIsValid)
\r
151 samples[i].maxPc=samples[i].minPc=-1;
\r
152 samples[i].dataCount=0;
\r