/** \file ProfileSample.cpp
* Defines the ProfileSample class.
*
* @author Vovansim (aka Scorpion)
* @version Version 1.0
* @date September 14, 2003
*/
#include "SDL/SDL.h"
#include "ProfileSample.h"
//////////////////// Initialize static members of the class ///////////////////
int ProfileSample::lastOpenedSample = -1;
int ProfileSample::openSampleCount = 0;
sProfileSample ProfileSample::samples[MAX_PROFILE_SAMPLES];
IProfilerOutputHandler *ProfileSample::outputHandler = 0;
float ProfileSample::rootBegin = 0.0f;
float ProfileSample::rootEnd = 0.0f;
bool ProfileSample::profilerIsRunning = true;
////////////////// End Initialize static members of the class /////////////////
//======================================= ProfileSample::ProfileSample ======//
ProfileSample::ProfileSample(std::string sampleName) {
if(!profilerIsRunning) {
return;
}
//find the sample
int i = 0;
int storeIndex = -1;
for(i=0; i= 0 && "Profiler has run out of sample slots!");
samples[storeIndex].IsValid = true;
samples[storeIndex].Name = sampleName;
sampleIndex = storeIndex;
parentIndex = lastOpenedSample;
lastOpenedSample = storeIndex;
samples[i].ParentCount = openSampleCount;
openSampleCount++;
samples[storeIndex].IsOpen = true;
samples[storeIndex].CallCount = 1;
samples[storeIndex].TotalTime = 0.0f;
samples[storeIndex].ChildTime = 0.0f;
samples[storeIndex].StartTime = GetTime();
if(parentIndex<0) {
rootBegin = samples[storeIndex].StartTime;
}
}
//====================================== ProfileSample::~ProfileSample ======//
ProfileSample::~ProfileSample() {
if(!profilerIsRunning) {
return;
}
float endTime = GetTime();
//done timing
samples[sampleIndex].IsOpen = false;
//calculate the time taken this profile, for ease of use later on
float timeTaken = endTime - samples[sampleIndex].StartTime;
if(parentIndex >= 0) {
samples[parentIndex].ChildTime += timeTaken;
} else {
//no parent, so this is the end of the main loop sample
rootEnd = endTime;
}
samples[sampleIndex].TotalTime += timeTaken;
lastOpenedSample = parentIndex;
openSampleCount --;
}
//========================================= void ProfileSample::Output ======//
void ProfileSample::Output() {
if(!profilerIsRunning) {
return;
}
assert(outputHandler && "Profiler has no output handler set");
outputHandler->BeginOutput(rootEnd - rootBegin);
for(int i=0; i samples[i].MaximumPc)) {
samples[i].MaximumPc = percentage;
}
//output these values
outputHandler->Sample(samples[i].MinimumPc,
samples[i].AveragePc,
samples[i].MaximumPc,
sampleTime,
samples[i].CallCount,
samples[i].Name,
samples[i].ParentCount);
//reset the sample
samples[i].CallCount = 0;
samples[i].TotalTime = 0;
samples[i].ChildTime = 0;
}
}
outputHandler->EndOutput();
}
//==================================== void ProfileSample::ResetSample ======//
void ProfileSample::ResetSample(std::string sampleName) {
for(int i=0; i