28 ProfileInstance::ProfileInstance(
const char * pname) {
29 strncpy(this->
name, pname, 255);
32 this->totalTime = 0.0;
39 Profiler * Profiler::profiler = NULL;
41 Profiler::Profiler() {
46 Profiler::~Profiler() {
50 Profiler * Profiler::getInstance() {
51 if (profiler == NULL) {
52 profiler =
new Profiler();
57 void Profiler::startProfile(
const char *
name) {
58 ProfileInstance * newProfile;
60 std::vector<ProfileInstance *>::iterator iter = vecProfiles.begin();
61 std::vector<ProfileInstance *>::iterator end = vecProfiles.end();
62 for (; iter != end; ++iter) {
63 if (strncmp((*iter)->name,
name, 255) == 0) {
68 newProfile =
new ProfileInstance(
name);
69 vecProfiles.push_back(newProfile);
72 ++(newProfile->openCalls);
73 ++(newProfile->calls);
76 if (curProfile != NULL) {
77 curProfile->mapChildren.insert(std::pair<ProfileInstance*, void *>(newProfile, NULL));
78 stkProfiles.push_back(curProfile);
79 assert(stkProfiles.size() < 100);
81 curProfile = newProfile;
84 void Profiler::stopProfile() {
85 assert(curProfile != NULL);
86 assert(curProfile->openCalls > 0);
91 if (elapsed < 0.00001) {
92 this->mapWarning.insert(std::pair<ProfileInstance *, void*>(curProfile, NULL));
94 curProfile->addTime += elapsed;
96 --(curProfile->openCalls);
97 if (curProfile->openCalls == 0) {
98 curProfile->totalTime += elapsed;
102 std::vector<ProfileInstance *>::reverse_iterator
last = stkProfiles.rbegin();
103 if (
last != stkProfiles.rend()) {
104 curProfile = (*last);
105 curProfile->subTime += elapsed;
106 stkProfiles.pop_back();
112 void Profiler::stopActiveProfiles() {
113 while (curProfile != NULL) {
118 bool instanceLess(
const ProfileInstance * p1,
const ProfileInstance * p2) {
119 return p1->totalTime < p2->totalTime;
122 #define TRUNC(a) ( (a) < 0.001 ? 0.0 : (a) ) 124 void Profiler::printProfile() {
126 std::cerr <<
"****************** PROFILE ***********************" << std::endl;
127 std::cerr <<
"Total : " << total << std::endl;
128 std::cerr <<
"| TOTAL % | OWN % | % of PA | CALLS | NAME" << std::endl;
129 std::cerr <<
"|---------|---------|---------|---------|---------" << std::endl;
130 sort(vecProfiles.begin(), vecProfiles.end(), instanceLess);
131 std::vector<ProfileInstance *>::reverse_iterator iter = vecProfiles.rbegin();
132 std::vector<ProfileInstance *>::reverse_iterator end = vecProfiles.rend();
133 for (; iter != end; ++iter) {
134 std::cerr <<
"| " << std::setprecision(3) << std::setw(7)
135 << TRUNC( (*iter)->totalTime * 100 / total )
136 <<
" | " << std::setprecision(3) << std::setw(7)
137 << TRUNC( ((*iter)->addTime -
138 (*iter)->subTime) * 100.0 / total )
140 << std::setw(7) << (*iter)->calls <<
" | " << (*iter)->name << std::endl;
141 std::map<ProfileInstance *, void*>::iterator childiter =
142 (*iter)->mapChildren.begin();
143 std::map<ProfileInstance *, void*>::iterator childend =
144 (*iter)->mapChildren.end();
145 for (; childiter != childend; ++childiter) {
146 std::cerr <<
"| " << std::setprecision(3) << std::setw(7)
147 << TRUNC( (*childiter).first->totalTime * 100 / total )
148 <<
" | " << std::setprecision(3) << std::setw(7)
149 << TRUNC( ((*childiter).first->addTime - (*childiter).first->subTime) * 100.0 / total )
150 <<
" | " << std::setprecision(3) << std::setw(7)
151 << TRUNC( 100 * (*childiter).first->totalTime / (*iter)->totalTime )
152 <<
" | " << std::setw(7) << (*childiter).first->calls
153 <<
" | " << (*childiter).first->name << std::endl;
155 std::cerr <<
"----------------------------------------------------------" << std::endl;
157 std::map<ProfileInstance *, void*>::iterator warniter = mapWarning.begin();
158 std::map<ProfileInstance *, void*>::iterator warnend = mapWarning.end();
159 for (; warniter != warnend; ++warniter) {
160 std::cerr <<
"Warning: Profile " << (*warniter).first->name <<
" may be to short" << std::endl;
The Gaming Framework API.
char * name
Name of the parameter.
double GfTimeClock(void)
Get the time in seconds.