[657] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of OGRE
|
---|
| 4 | (Object-oriented Graphics Rendering Engine)
|
---|
| 5 | For the latest info, see http://www.ogre3d.org/
|
---|
| 6 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
| 18 |
|
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
| 23 | -----------------------------------------------------------------------------
|
---|
| 24 | */
|
---|
| 25 | #include "OgreStableHeaders.h"
|
---|
| 26 | /*
|
---|
| 27 |
|
---|
| 28 | Although the code is original, many of the ideas for the profiler were borrowed from
|
---|
| 29 | "Real-Time In-Game Profiling" by Steve Rabin which can be found in Game Programming
|
---|
| 30 | Gems 1.
|
---|
| 31 |
|
---|
| 32 | This code can easily be adapted to your own non-Ogre project. The only code that is
|
---|
| 33 | Ogre-dependent is in the visualization/logging routines and the use of the Timer class.
|
---|
| 34 |
|
---|
| 35 | Enjoy!
|
---|
| 36 |
|
---|
| 37 | */
|
---|
| 38 |
|
---|
| 39 | #include "OgreProfiler.h"
|
---|
| 40 | #include "OgreTimer.h"
|
---|
| 41 | #include "OgreLogManager.h"
|
---|
| 42 | #include "OgreStringConverter.h"
|
---|
| 43 | #include "OgreOverlayManager.h"
|
---|
| 44 | #include "OgreOverlayElement.h"
|
---|
| 45 | #include "OgreOverlayContainer.h"
|
---|
| 46 |
|
---|
| 47 | namespace Ogre {
|
---|
| 48 | //-----------------------------------------------------------------------
|
---|
| 49 | // PROFILE DEFINITIONS
|
---|
| 50 | //-----------------------------------------------------------------------
|
---|
| 51 | template<> Profiler* Singleton<Profiler>::ms_Singleton = 0;
|
---|
| 52 | Profiler* Profiler::getSingletonPtr(void)
|
---|
| 53 | {
|
---|
| 54 | return ms_Singleton;
|
---|
| 55 | }
|
---|
| 56 | Profiler& Profiler::getSingleton(void)
|
---|
| 57 | {
|
---|
| 58 | assert( ms_Singleton ); return ( *ms_Singleton );
|
---|
| 59 | }
|
---|
| 60 | //-----------------------------------------------------------------------
|
---|
| 61 | Profile::Profile(const String& profileName) {
|
---|
| 62 |
|
---|
| 63 | mName = profileName;
|
---|
| 64 |
|
---|
| 65 | Ogre::Profiler::getSingleton().beginProfile(profileName);
|
---|
| 66 |
|
---|
| 67 | }
|
---|
| 68 | //-----------------------------------------------------------------------
|
---|
| 69 | Profile::~Profile() {
|
---|
| 70 |
|
---|
| 71 | Ogre::Profiler::getSingleton().endProfile(mName);
|
---|
| 72 |
|
---|
| 73 | }
|
---|
| 74 | //-----------------------------------------------------------------------
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 | //-----------------------------------------------------------------------
|
---|
| 78 | // PROFILER DEFINITIONS
|
---|
| 79 | //-----------------------------------------------------------------------
|
---|
| 80 | Profiler::Profiler() {
|
---|
| 81 |
|
---|
| 82 | // init some variables
|
---|
| 83 | mTimer = 0;
|
---|
| 84 | mTotalFrameTime = 0;
|
---|
| 85 | mUpdateDisplayFrequency = 0;
|
---|
| 86 | mCurrentFrame = 0;
|
---|
| 87 | mEnabled = mNewEnableState = false; // the profiler starts out as disabled
|
---|
| 88 | mEnableStateChangePending = false;
|
---|
| 89 | mInitialized = false;
|
---|
| 90 | maxProfiles = 50;
|
---|
| 91 |
|
---|
| 92 | // by default the display will be updated every 10 frames
|
---|
| 93 | mUpdateDisplayFrequency = 10;
|
---|
| 94 |
|
---|
| 95 | }
|
---|
| 96 | //-----------------------------------------------------------------------
|
---|
| 97 | Profiler::~Profiler() {
|
---|
| 98 |
|
---|
| 99 | if (!mProfileHistory.empty()) {
|
---|
| 100 | // log the results of our profiling before we quit
|
---|
| 101 | logResults();
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | // clear all our lists
|
---|
| 105 | mProfiles.clear();
|
---|
| 106 | mProfileFrame.clear();
|
---|
| 107 | mProfileHistoryMap.clear();
|
---|
| 108 | mProfileHistory.clear();
|
---|
| 109 | mDisabledProfiles.clear();
|
---|
| 110 | mProfileBars.clear();
|
---|
| 111 |
|
---|
| 112 | }
|
---|
| 113 | //-----------------------------------------------------------------------
|
---|
| 114 | void Profiler::initialize() {
|
---|
| 115 |
|
---|
| 116 | // init some gui characteristics
|
---|
| 117 | mBarHeight = 10; //0.02;
|
---|
| 118 | mGuiBorderWidth = 10; //0.02;
|
---|
| 119 | mGuiHeight = 25; //0.05;
|
---|
| 120 | mGuiWidth = 250; //0.15;
|
---|
| 121 | mBarIndent = mGuiWidth;
|
---|
| 122 | mBarLineWidth = 2;
|
---|
| 123 |
|
---|
| 124 | // create a new overlay to hold our Profiler display
|
---|
| 125 | mOverlay = OverlayManager::getSingleton().create("Profiler");
|
---|
| 126 | mOverlay->setZOrder(500);
|
---|
| 127 |
|
---|
| 128 | // this panel will be the main container for our profile bars
|
---|
| 129 | mProfileGui = createContainer();
|
---|
| 130 |
|
---|
| 131 | OverlayElement* element;
|
---|
| 132 |
|
---|
| 133 | // we create the little "ticks" above the profiles
|
---|
| 134 | for (uint k = 1; k < 10; ++k) { // we don't want a tick at 0% or 100%
|
---|
| 135 |
|
---|
| 136 | if (k != 5) { // we don't want a tick at 50%
|
---|
| 137 | element = createTextArea("ProfileKeyLine" + StringConverter::toString(k), 20, 10, 2, mGuiWidth * (1 + k * .1), 9, "|");
|
---|
| 138 | mProfileGui->addChild(element);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | // we create a 0% marker
|
---|
| 144 | element = createTextArea("ProfileKey0", 50, 10, 2, mGuiWidth * 0.99, 9, "0%");
|
---|
| 145 | mProfileGui->addChild(element);
|
---|
| 146 |
|
---|
| 147 | // we create a 50% marker
|
---|
| 148 | element = createTextArea("ProfileyKey50", 50, 10, 2, mGuiWidth * 1.48, 9, "50%");
|
---|
| 149 | mProfileGui->addChild(element);
|
---|
| 150 |
|
---|
| 151 | // we create a 100% marker
|
---|
| 152 | element = createTextArea("ProfileKey100", 50, 10, 2, mGuiWidth * 1.98, 9, "100%");
|
---|
| 153 | mProfileGui->addChild(element);
|
---|
| 154 |
|
---|
| 155 | // we create an initial pool of 50 profile bars
|
---|
| 156 | for (uint i = 0; i < maxProfiles; ++i) {
|
---|
| 157 |
|
---|
| 158 | // this is for the profile name and the number of times it was called in a frame
|
---|
| 159 | element = createTextArea("profileText" + StringConverter::toString(i), 90, mBarHeight, mGuiBorderWidth + (mBarHeight * 2) * i, 0, 14, "", false);
|
---|
| 160 | mProfileGui->addChild(element);
|
---|
| 161 | mProfileBars.push_back(element);
|
---|
| 162 |
|
---|
| 163 | // this indicates the current frame time
|
---|
| 164 | element = createPanel("currBar" + StringConverter::toString(i), 0, mBarHeight, mGuiBorderWidth + (mBarHeight * 2) * i, mBarIndent, "Core/ProfilerCurrent", false);
|
---|
| 165 | mProfileGui->addChild(element);
|
---|
| 166 | mProfileBars.push_back(element);
|
---|
| 167 |
|
---|
| 168 | // this indicates the minimum frame time
|
---|
| 169 | element = createPanel("minBar" + StringConverter::toString(i), mBarLineWidth, mBarHeight, mGuiBorderWidth + (mBarHeight * 2) * i, 0, "Core/ProfilerMin", false);
|
---|
| 170 | mProfileGui->addChild(element);
|
---|
| 171 | mProfileBars.push_back(element);
|
---|
| 172 |
|
---|
| 173 | // this indicates the maximum frame time
|
---|
| 174 | element = createPanel("maxBar" + StringConverter::toString(i), mBarLineWidth, mBarHeight, mGuiBorderWidth + (mBarHeight * 2) * i, 0, "Core/ProfilerMax", false);
|
---|
| 175 | mProfileGui->addChild(element);
|
---|
| 176 | mProfileBars.push_back(element);
|
---|
| 177 |
|
---|
| 178 | // this indicates the average frame time
|
---|
| 179 | element = createPanel("avgBar" + StringConverter::toString(i), mBarLineWidth, mBarHeight, mGuiBorderWidth + (mBarHeight * 2) * i, 0, "Core/ProfilerAvg", false);
|
---|
| 180 | mProfileGui->addChild(element);
|
---|
| 181 | mProfileBars.push_back(element);
|
---|
| 182 |
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | // throw everything all the GUI stuff into the overlay and display it
|
---|
| 186 | mOverlay->add2D(mProfileGui);
|
---|
| 187 | mOverlay->show();
|
---|
| 188 |
|
---|
| 189 | }
|
---|
| 190 | //-----------------------------------------------------------------------
|
---|
| 191 | void Profiler::setTimer(Timer* t) {
|
---|
| 192 |
|
---|
| 193 | mTimer = t;
|
---|
| 194 |
|
---|
| 195 | }
|
---|
| 196 | //-----------------------------------------------------------------------
|
---|
| 197 | Timer* Profiler::getTimer() {
|
---|
| 198 |
|
---|
| 199 | assert(mTimer && "Timer not set!");
|
---|
| 200 | return mTimer;
|
---|
| 201 |
|
---|
| 202 | }
|
---|
| 203 | //-----------------------------------------------------------------------
|
---|
| 204 | void Profiler::setEnabled(bool enabled) {
|
---|
| 205 |
|
---|
| 206 | if (!mInitialized && enabled) {
|
---|
| 207 |
|
---|
| 208 | // the user wants to enable the Profiler for the first time
|
---|
| 209 | // so we initialize the GUI stuff
|
---|
| 210 | initialize();
|
---|
| 211 | mInitialized = true;
|
---|
| 212 | mEnabled = true;
|
---|
| 213 |
|
---|
| 214 | }
|
---|
| 215 | else {
|
---|
| 216 | // We store this enable/disable request until the frame ends
|
---|
| 217 | // (don't want to screw up any open profiles!)
|
---|
| 218 | mEnableStateChangePending = true;
|
---|
| 219 | mNewEnableState = enabled;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | }
|
---|
| 223 | //-----------------------------------------------------------------------
|
---|
| 224 | bool Profiler::getEnabled() const {
|
---|
| 225 |
|
---|
| 226 | return mEnabled;
|
---|
| 227 |
|
---|
| 228 | }
|
---|
| 229 | //-----------------------------------------------------------------------
|
---|
| 230 | void Profiler::disableProfile(const String& profileName) {
|
---|
| 231 |
|
---|
| 232 | // make sure the profile isn't already disabled
|
---|
| 233 | DisabledProfileMap::iterator iter;
|
---|
| 234 | iter = mDisabledProfiles.find(profileName);
|
---|
| 235 |
|
---|
| 236 | // make sure you don't disable a profile in the middle of that profile
|
---|
| 237 | ProfileStack::iterator pIter;
|
---|
| 238 | for (pIter = mProfiles.begin(); pIter != mProfiles.end(); ++pIter) {
|
---|
| 239 |
|
---|
| 240 | if (profileName == (*pIter).name)
|
---|
| 241 | break;
|
---|
| 242 |
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | // if those two conditions are met, disable the profile
|
---|
| 246 | if ( (iter == mDisabledProfiles.end()) && (pIter == mProfiles.end()) ) {
|
---|
| 247 |
|
---|
| 248 | mDisabledProfiles.insert(std::pair<String, bool>(profileName, true));
|
---|
| 249 |
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | }
|
---|
| 253 | //-----------------------------------------------------------------------
|
---|
| 254 | void Profiler::enableProfile(const String& profileName) {
|
---|
| 255 |
|
---|
| 256 | // make sure the profile is actually disabled
|
---|
| 257 | DisabledProfileMap::iterator iter;
|
---|
| 258 | iter = mDisabledProfiles.find(profileName);
|
---|
| 259 |
|
---|
| 260 | // make sure you don't enable a profile in the middle of that profile
|
---|
| 261 | ProfileStack::iterator pIter;
|
---|
| 262 | for (pIter = mProfiles.begin(); pIter != mProfiles.end(); ++pIter) {
|
---|
| 263 |
|
---|
| 264 | if (profileName == (*pIter).name)
|
---|
| 265 | break;
|
---|
| 266 |
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | // if those two conditions are met, enable the profile by removing it from
|
---|
| 270 | // the disabled list
|
---|
| 271 | if ( (iter != mDisabledProfiles.end()) && (pIter == mProfiles.end()) ) {
|
---|
| 272 |
|
---|
| 273 | mDisabledProfiles.erase(iter);
|
---|
| 274 |
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | }
|
---|
| 278 | //-----------------------------------------------------------------------
|
---|
| 279 | void Profiler::beginProfile(const String& profileName) {
|
---|
| 280 |
|
---|
| 281 | // if the profiler is enabled
|
---|
| 282 | if (!mEnabled) {
|
---|
| 283 |
|
---|
| 284 | return;
|
---|
| 285 |
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | // empty string is reserved for the root
|
---|
| 289 | assert ((profileName != "") && ("Profile name can't be an empty string"));
|
---|
| 290 |
|
---|
| 291 | ProfileStack::iterator iter;
|
---|
| 292 | for (iter = mProfiles.begin(); iter != mProfiles.end(); ++iter) {
|
---|
| 293 |
|
---|
| 294 | if ((*iter).name == profileName) {
|
---|
| 295 |
|
---|
| 296 | break;
|
---|
| 297 |
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | // make sure this profile isn't being used more than once
|
---|
| 303 | assert ((iter == mProfiles.end()) && ("This profile name is already being used"));
|
---|
| 304 |
|
---|
| 305 | // we only process this profile if isn't disabled
|
---|
| 306 | DisabledProfileMap::iterator dIter;
|
---|
| 307 | dIter = mDisabledProfiles.find(profileName);
|
---|
| 308 | if ( dIter != mDisabledProfiles.end() ) {
|
---|
| 309 |
|
---|
| 310 | return;
|
---|
| 311 |
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | ProfileInstance p;
|
---|
| 315 |
|
---|
| 316 | // this is the root, it has no parent
|
---|
| 317 | if (mProfiles.empty()) {
|
---|
| 318 |
|
---|
| 319 | p.parent = "";
|
---|
| 320 |
|
---|
| 321 | }
|
---|
| 322 | // otherwise peek at the stack and use the top as the parent
|
---|
| 323 | else {
|
---|
| 324 |
|
---|
| 325 | ProfileInstance parent = mProfiles.back();
|
---|
| 326 | p.parent = parent.name;
|
---|
| 327 |
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | // need a timer to profile!
|
---|
| 331 | assert (mTimer && "Timer not set!");
|
---|
| 332 |
|
---|
| 333 | ProfileFrameList::iterator fIter;
|
---|
| 334 | ProfileHistoryList::iterator hIter;
|
---|
| 335 |
|
---|
| 336 | // we check to see if this profile has been called in the frame before
|
---|
| 337 | for (fIter = mProfileFrame.begin(); fIter != mProfileFrame.end(); ++fIter) {
|
---|
| 338 |
|
---|
| 339 | if ((*fIter).name == profileName)
|
---|
| 340 | break;
|
---|
| 341 |
|
---|
| 342 | }
|
---|
| 343 | // if it hasn't been called before, set its position in the stack
|
---|
| 344 | if (fIter == mProfileFrame.end()) {
|
---|
| 345 |
|
---|
| 346 | ProfileFrame f;
|
---|
| 347 | f.name = profileName;
|
---|
| 348 | f.frameTime = 0;
|
---|
| 349 | f.calls = 0;
|
---|
| 350 | f.hierarchicalLvl = (uint) mProfiles.size();
|
---|
| 351 | mProfileFrame.push_back(f);
|
---|
| 352 |
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | // we check to see if this profile has been called in the app before
|
---|
| 356 | ProfileHistoryMap::iterator histMapIter;
|
---|
| 357 | histMapIter = mProfileHistoryMap.find(profileName);
|
---|
| 358 |
|
---|
| 359 | // if not we add a profile with just the name into the history
|
---|
| 360 | if (histMapIter == mProfileHistoryMap.end()) {
|
---|
| 361 |
|
---|
| 362 | ProfileHistory h;
|
---|
| 363 | h.name = profileName;
|
---|
| 364 | h.numCallsThisFrame = 0;
|
---|
| 365 | h.totalTime = 0;
|
---|
| 366 | h.totalCalls = 0;
|
---|
| 367 | h.maxTime = 0;
|
---|
| 368 | h.minTime = 1;
|
---|
| 369 | h.hierarchicalLvl = p.hierarchicalLvl;
|
---|
| 370 | h.currentTime = 0;
|
---|
| 371 |
|
---|
| 372 | // we add this to the history
|
---|
| 373 | hIter = mProfileHistory.insert(mProfileHistory.end(), h);
|
---|
| 374 |
|
---|
| 375 | // for quick look-ups, we'll add it to the history map as well
|
---|
| 376 | mProfileHistoryMap.insert(std::pair<String, ProfileHistoryList::iterator>(profileName, hIter));
|
---|
| 377 |
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | // add the stats to this profile and push it on the stack
|
---|
| 381 | // we do this at the very end of the function to get the most
|
---|
| 382 | // accurate timing results
|
---|
| 383 | p.name = profileName;
|
---|
| 384 | p.currTime = mTimer->getMicroseconds();
|
---|
| 385 | p.accum = 0;
|
---|
| 386 | p.hierarchicalLvl = (uint) mProfiles.size();
|
---|
| 387 | mProfiles.push_back(p);
|
---|
| 388 |
|
---|
| 389 | }
|
---|
| 390 | //-----------------------------------------------------------------------
|
---|
| 391 | void Profiler::endProfile(const String& profileName) {
|
---|
| 392 |
|
---|
| 393 | // if the profiler received a request to be enabled or disabled
|
---|
| 394 | // we reached the end of the frame so we can safely do this
|
---|
| 395 | if (mEnableStateChangePending) {
|
---|
| 396 |
|
---|
| 397 | changeEnableState();
|
---|
| 398 |
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | // if the profiler is enabled
|
---|
| 402 | if(!mEnabled) {
|
---|
| 403 |
|
---|
| 404 | return;
|
---|
| 405 |
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | // need a timer to profile!
|
---|
| 409 | assert (mTimer && "Timer not set!");
|
---|
| 410 |
|
---|
| 411 | // get the end time of this profile
|
---|
| 412 | // we do this as close the beginning of this function as possible
|
---|
| 413 | // to get more accurate timing results
|
---|
| 414 | ulong endTime = mTimer->getMicroseconds();
|
---|
| 415 |
|
---|
| 416 | // empty string is reserved for designating an empty parent
|
---|
| 417 | assert ((profileName != "") && ("Profile name can't be an empty string"));
|
---|
| 418 |
|
---|
| 419 | // we only process this profile if isn't disabled
|
---|
| 420 | DisabledProfileMap::iterator dIter;
|
---|
| 421 | dIter = mDisabledProfiles.find(profileName);
|
---|
| 422 | if ( dIter != mDisabledProfiles.end() ) {
|
---|
| 423 |
|
---|
| 424 | return;
|
---|
| 425 |
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | // stack shouldnt be empty
|
---|
| 429 | assert (!mProfiles.empty());
|
---|
| 430 |
|
---|
| 431 | // get the start of this profile
|
---|
| 432 | ProfileInstance bProfile;
|
---|
| 433 | bProfile = mProfiles.back();
|
---|
| 434 | mProfiles.pop_back();
|
---|
| 435 |
|
---|
| 436 | // calculate the elapsed time of this profile
|
---|
| 437 | ulong timeElapsed = endTime - bProfile.currTime;
|
---|
| 438 |
|
---|
| 439 | // update parent's accumalator if it isn't the root
|
---|
| 440 | if (bProfile.parent != "") {
|
---|
| 441 |
|
---|
| 442 | // find the parent
|
---|
| 443 | ProfileStack::iterator iter;
|
---|
| 444 | for(iter = mProfiles.begin(); iter != mProfiles.end(); ++iter) {
|
---|
| 445 |
|
---|
| 446 | if ((*iter).name == bProfile.parent)
|
---|
| 447 | break;
|
---|
| 448 |
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | // the parent should be found
|
---|
| 452 | assert(iter != mProfiles.end());
|
---|
| 453 |
|
---|
| 454 | // add this profile's time to the parent's accumlator
|
---|
| 455 | (*iter).accum += timeElapsed;
|
---|
| 456 |
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | // we find the profile in this frame
|
---|
| 460 | ProfileFrameList::iterator iter;
|
---|
| 461 | for (iter = mProfileFrame.begin(); iter != mProfileFrame.end(); ++iter) {
|
---|
| 462 |
|
---|
| 463 | if ((*iter).name == bProfile.name)
|
---|
| 464 | break;
|
---|
| 465 |
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 | // we subtract the time the children profiles took from this profile
|
---|
| 469 | (*iter).frameTime += timeElapsed - bProfile.accum;
|
---|
| 470 | (*iter).calls++;
|
---|
| 471 |
|
---|
| 472 | // the stack is empty and all the profiles have been completed
|
---|
| 473 | // we have reached the end of the frame so process the frame statistics
|
---|
| 474 | if (mProfiles.empty()) {
|
---|
| 475 |
|
---|
| 476 | // we know that the time elapsed of the main loop is the total time the frame took
|
---|
| 477 | mTotalFrameTime = timeElapsed;
|
---|
| 478 |
|
---|
| 479 | // we got all the information we need, so process the profiles
|
---|
| 480 | // for this frame
|
---|
| 481 | processFrameStats();
|
---|
| 482 |
|
---|
| 483 | // clear the frame stats for next frame
|
---|
| 484 | mProfileFrame.clear();
|
---|
| 485 |
|
---|
| 486 | // we display everything to the screen
|
---|
| 487 | displayResults();
|
---|
| 488 |
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 | }
|
---|
| 492 | //-----------------------------------------------------------------------
|
---|
| 493 | void Profiler::processFrameStats() {
|
---|
| 494 |
|
---|
| 495 | ProfileFrameList::iterator frameIter;
|
---|
| 496 | ProfileHistoryList::iterator historyIter;
|
---|
| 497 |
|
---|
| 498 | // we set the number of times each profile was called per frame to 0
|
---|
| 499 | // because not all profiles are called every frame
|
---|
| 500 | for (historyIter = mProfileHistory.begin(); historyIter != mProfileHistory.end(); ++historyIter) {
|
---|
| 501 |
|
---|
| 502 | (*historyIter).numCallsThisFrame = 0;
|
---|
| 503 |
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | // iterate through each of the profiles processed during this frame
|
---|
| 507 | for (frameIter = mProfileFrame.begin(); frameIter != mProfileFrame.end(); ++frameIter) {
|
---|
| 508 |
|
---|
| 509 | String s = (*frameIter).name;
|
---|
| 510 |
|
---|
| 511 | // use our map to find the appropriate profile in the history
|
---|
| 512 | historyIter = (*mProfileHistoryMap.find(s)).second;
|
---|
| 513 |
|
---|
| 514 | // extract the frame stats
|
---|
| 515 | ulong frameTime = (*frameIter).frameTime;
|
---|
| 516 | uint calls = (*frameIter).calls;
|
---|
| 517 | uint lvl = (*frameIter).hierarchicalLvl;
|
---|
| 518 |
|
---|
| 519 | // calculate what percentage of frame time this profile took
|
---|
| 520 | Real framePercentage = (Real) frameTime / (Real) mTotalFrameTime;
|
---|
| 521 |
|
---|
| 522 | // update the profile stats
|
---|
| 523 | (*historyIter).currentTime = framePercentage;
|
---|
| 524 | (*historyIter).totalTime += framePercentage;
|
---|
| 525 | (*historyIter).totalCalls++;
|
---|
| 526 | (*historyIter).numCallsThisFrame = calls;
|
---|
| 527 | (*historyIter).hierarchicalLvl = lvl;
|
---|
| 528 |
|
---|
| 529 | // if we find a new minimum for this profile, update it
|
---|
| 530 | if ((framePercentage) < ((*historyIter).minTime)) {
|
---|
| 531 |
|
---|
| 532 | (*historyIter).minTime = framePercentage;
|
---|
| 533 |
|
---|
| 534 | }
|
---|
| 535 |
|
---|
| 536 | // if we find a new maximum for this profile, update it
|
---|
| 537 | if ((framePercentage) > ((*historyIter).maxTime)) {
|
---|
| 538 |
|
---|
| 539 | (*historyIter).maxTime = framePercentage;
|
---|
| 540 |
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | }
|
---|
| 546 | //-----------------------------------------------------------------------
|
---|
| 547 | void Profiler::displayResults() {
|
---|
| 548 |
|
---|
| 549 | if (!mEnabled) {
|
---|
| 550 |
|
---|
| 551 | return;
|
---|
| 552 |
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | // if its time to update the display
|
---|
| 556 | if (mCurrentFrame >= mUpdateDisplayFrequency) {
|
---|
| 557 |
|
---|
| 558 | mCurrentFrame = 0;
|
---|
| 559 |
|
---|
| 560 | ProfileHistoryList::iterator iter;
|
---|
| 561 | ProfileBarList::iterator bIter;
|
---|
| 562 |
|
---|
| 563 | OverlayElement* g;
|
---|
| 564 |
|
---|
| 565 | Real newGuiHeight = mGuiHeight;
|
---|
| 566 |
|
---|
| 567 | int temp = 0; // dummy variable for weird Ogre issue
|
---|
| 568 |
|
---|
| 569 | // go through each profile and display it
|
---|
| 570 | for (iter = mProfileHistory.begin(), bIter = mProfileBars.begin();
|
---|
| 571 | iter != mProfileHistory.end() && bIter != mProfileBars.end();
|
---|
| 572 | ++iter, ++bIter)
|
---|
| 573 | {
|
---|
| 574 |
|
---|
| 575 | // display the profile's name and the number of times it was called in a frame
|
---|
| 576 | g = *bIter;
|
---|
| 577 | g->show();
|
---|
| 578 | g->setCaption((*iter).name + " (" + StringConverter::toString((*iter).numCallsThisFrame) + ")");
|
---|
| 579 | g->setLeft(10 + (*iter).hierarchicalLvl * 15);
|
---|
| 580 |
|
---|
| 581 | // display the main bar that show the percentage of the frame time that this
|
---|
| 582 | // profile has taken
|
---|
| 583 | bIter++;
|
---|
| 584 | g = *bIter;
|
---|
| 585 | g->show();
|
---|
| 586 | // most of this junk has been set before, but we do this to get around a weird
|
---|
| 587 | // Ogre gui issue (bug?)
|
---|
| 588 | g->setMetricsMode(GMM_PIXELS);
|
---|
| 589 | g->setHeight(mBarHeight);
|
---|
| 590 | g->setWidth(((*iter).currentTime) * mGuiWidth);
|
---|
| 591 | g->setLeft(mGuiWidth);
|
---|
| 592 | g->setTop(mGuiBorderWidth + temp * mBarHeight * 2);
|
---|
| 593 |
|
---|
| 594 | // display line to indicate the minimum frame time for this profile
|
---|
| 595 | bIter++;
|
---|
| 596 | g = *bIter;
|
---|
| 597 | g->show();
|
---|
| 598 | g->setLeft(mBarIndent + (*iter).minTime * mGuiWidth);
|
---|
| 599 |
|
---|
| 600 | // display line to indicate the maximum frame time for this profile
|
---|
| 601 | bIter++;
|
---|
| 602 | g = *bIter;
|
---|
| 603 | g->show();
|
---|
| 604 | g->setLeft(mBarIndent + (*iter).maxTime * mGuiWidth);
|
---|
| 605 |
|
---|
| 606 | // display line to indicate the average frame time for this profile
|
---|
| 607 | bIter++;
|
---|
| 608 | g = *bIter;
|
---|
| 609 | g->show();
|
---|
| 610 | if ((*iter).totalCalls != 0)
|
---|
| 611 | g->setLeft(mBarIndent + ((*iter).totalTime / (*iter).totalCalls) * mGuiWidth);
|
---|
| 612 | else
|
---|
| 613 | g->setLeft(mBarIndent);
|
---|
| 614 | // we set the height of the display with respect to the number of profiles displayed
|
---|
| 615 | newGuiHeight += mBarHeight * 2;
|
---|
| 616 |
|
---|
| 617 | temp++;
|
---|
| 618 |
|
---|
| 619 | }
|
---|
| 620 |
|
---|
| 621 | // set the main display dimensions
|
---|
| 622 | mProfileGui->setMetricsMode(GMM_PIXELS);
|
---|
| 623 | mProfileGui->setHeight(newGuiHeight);
|
---|
| 624 | mProfileGui->setWidth(mGuiWidth * 2 + 15);
|
---|
| 625 | mProfileGui->setTop(5);
|
---|
| 626 | mProfileGui->setLeft(5);
|
---|
| 627 |
|
---|
| 628 | // we hide all the remaining pre-created bars
|
---|
| 629 | for (; bIter != mProfileBars.end(); ++bIter) {
|
---|
| 630 |
|
---|
| 631 | (*bIter)->hide();
|
---|
| 632 |
|
---|
| 633 | }
|
---|
| 634 |
|
---|
| 635 | }
|
---|
| 636 |
|
---|
| 637 | // not time to update the display yet
|
---|
| 638 | else {
|
---|
| 639 |
|
---|
| 640 | mCurrentFrame++;
|
---|
| 641 |
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 | }
|
---|
| 645 | //-----------------------------------------------------------------------
|
---|
| 646 | bool Profiler::watchForMax(const String& profileName) {
|
---|
| 647 |
|
---|
| 648 | ProfileHistoryMap::iterator mapIter;
|
---|
| 649 | ProfileHistoryList::iterator iter;
|
---|
| 650 |
|
---|
| 651 | mapIter = mProfileHistoryMap.find(profileName);
|
---|
| 652 |
|
---|
| 653 | // if we don't find the profile, return false
|
---|
| 654 | if (mapIter == mProfileHistoryMap.end())
|
---|
| 655 | return false;
|
---|
| 656 |
|
---|
| 657 | iter = (*mapIter).second;
|
---|
| 658 |
|
---|
| 659 | return ((*iter).currentTime == (*iter).maxTime);
|
---|
| 660 |
|
---|
| 661 | }
|
---|
| 662 | //-----------------------------------------------------------------------
|
---|
| 663 | bool Profiler::watchForMin(const String& profileName) {
|
---|
| 664 |
|
---|
| 665 | ProfileHistoryMap::iterator mapIter;
|
---|
| 666 | ProfileHistoryList::iterator iter;
|
---|
| 667 |
|
---|
| 668 | mapIter = mProfileHistoryMap.find(profileName);
|
---|
| 669 |
|
---|
| 670 | // if we don't find the profile, return false
|
---|
| 671 | if (mapIter == mProfileHistoryMap.end())
|
---|
| 672 | return false;
|
---|
| 673 |
|
---|
| 674 | iter = (*mapIter).second;
|
---|
| 675 |
|
---|
| 676 | return ((*iter).currentTime == (*iter).minTime);
|
---|
| 677 |
|
---|
| 678 | }
|
---|
| 679 | //-----------------------------------------------------------------------
|
---|
| 680 | bool Profiler::watchForLimit(const String& profileName, Real limit, bool greaterThan) {
|
---|
| 681 |
|
---|
| 682 | ProfileHistoryMap::iterator mapIter;
|
---|
| 683 | ProfileHistoryList::iterator iter;
|
---|
| 684 |
|
---|
| 685 | mapIter = mProfileHistoryMap.find(profileName);
|
---|
| 686 |
|
---|
| 687 | // if we don't find the profile, return false
|
---|
| 688 | if (mapIter == mProfileHistoryMap.end())
|
---|
| 689 | return false;
|
---|
| 690 |
|
---|
| 691 | iter = (*mapIter).second;
|
---|
| 692 |
|
---|
| 693 | if (greaterThan)
|
---|
| 694 | return ((*iter).currentTime > limit);
|
---|
| 695 | else
|
---|
| 696 | return ((*iter).currentTime < limit);
|
---|
| 697 |
|
---|
| 698 | }
|
---|
| 699 | //-----------------------------------------------------------------------
|
---|
| 700 | void Profiler::logResults() {
|
---|
| 701 |
|
---|
| 702 | ProfileHistoryList::iterator iter;
|
---|
| 703 |
|
---|
| 704 | LogManager::getSingleton().logMessage("----------------------Profiler Results----------------------");
|
---|
| 705 |
|
---|
| 706 | for (iter = mProfileHistory.begin(); iter != mProfileHistory.end(); ++iter) {
|
---|
| 707 |
|
---|
| 708 | // create an indent that represents the hierarchical order of the profile
|
---|
| 709 | String indent = "";
|
---|
| 710 | for (uint i = 0; i < (*iter).hierarchicalLvl; ++i) {
|
---|
| 711 |
|
---|
| 712 | indent = indent + " ";
|
---|
| 713 |
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | LogManager::getSingleton().logMessage(indent + "Name " + (*iter).name + " | Min " + StringConverter::toString((*iter).minTime) + " | Max " + StringConverter::toString((*iter).maxTime) + " | Avg "+ StringConverter::toString((*iter).totalTime / (*iter).totalCalls));
|
---|
| 717 |
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 | LogManager::getSingleton().logMessage("------------------------------------------------------------");
|
---|
| 721 |
|
---|
| 722 | }
|
---|
| 723 | //-----------------------------------------------------------------------
|
---|
| 724 | void Profiler::reset() {
|
---|
| 725 |
|
---|
| 726 | ProfileHistoryList::iterator iter;
|
---|
| 727 | for (iter = mProfileHistory.begin(); iter != mProfileHistory.end(); ++iter) {
|
---|
| 728 |
|
---|
| 729 | (*iter).currentTime = (*iter).maxTime = (*iter).totalTime = 0;
|
---|
| 730 | (*iter).numCallsThisFrame = (*iter).totalCalls = 0;
|
---|
| 731 |
|
---|
| 732 | (*iter).minTime = 1;
|
---|
| 733 |
|
---|
| 734 | }
|
---|
| 735 |
|
---|
| 736 | }
|
---|
| 737 | //-----------------------------------------------------------------------
|
---|
| 738 | void Profiler::setUpdateDisplayFrequency(uint freq) {
|
---|
| 739 |
|
---|
| 740 | mUpdateDisplayFrequency = freq;
|
---|
| 741 |
|
---|
| 742 | }
|
---|
| 743 | //-----------------------------------------------------------------------
|
---|
| 744 | uint Profiler::getUpdateDisplayFrequency() const {
|
---|
| 745 |
|
---|
| 746 | return mUpdateDisplayFrequency;
|
---|
| 747 |
|
---|
| 748 | }
|
---|
| 749 | //-----------------------------------------------------------------------
|
---|
| 750 | void Profiler::changeEnableState() {
|
---|
| 751 |
|
---|
| 752 | if (mNewEnableState) {
|
---|
| 753 |
|
---|
| 754 | mOverlay->show();
|
---|
| 755 |
|
---|
| 756 | }
|
---|
| 757 | else {
|
---|
| 758 |
|
---|
| 759 | mOverlay->hide();
|
---|
| 760 |
|
---|
| 761 | }
|
---|
| 762 | mEnabled = mNewEnableState;
|
---|
| 763 | mEnableStateChangePending = false;
|
---|
| 764 |
|
---|
| 765 | }
|
---|
| 766 | //-----------------------------------------------------------------------
|
---|
| 767 | OverlayContainer* Profiler::createContainer() {
|
---|
| 768 |
|
---|
| 769 | OverlayContainer* container = (OverlayContainer*)
|
---|
| 770 | OverlayManager::getSingleton().createOverlayElement(
|
---|
| 771 | "BorderPanel", "profiler");
|
---|
| 772 | container->setMetricsMode(GMM_PIXELS);
|
---|
| 773 | container->setMaterialName("Core/StatsBlockCenter");
|
---|
| 774 | container->setHeight(mGuiHeight);
|
---|
| 775 | container->setWidth(mGuiWidth * 2 + 15);
|
---|
| 776 | container->setParameter("border_size", "1 1 1 1");
|
---|
| 777 | container->setParameter("border_material", "Core/StatsBlockBorder");
|
---|
| 778 | container->setParameter("border_topleft_uv", "0.0000 1.0000 0.0039 0.9961");
|
---|
| 779 | container->setParameter("border_top_uv", "0.0039 1.0000 0.9961 0.9961");
|
---|
| 780 | container->setParameter("border_topright_uv", "0.9961 1.0000 1.0000 0.9961");
|
---|
| 781 | container->setParameter("border_left_uv","0.0000 0.9961 0.0039 0.0039");
|
---|
| 782 | container->setParameter("border_right_uv","0.9961 0.9961 1.0000 0.0039");
|
---|
| 783 | container->setParameter("border_bottomleft_uv","0.0000 0.0039 0.0039 0.0000");
|
---|
| 784 | container->setParameter("border_bottom_uv","0.0039 0.0039 0.9961 0.0000");
|
---|
| 785 | container->setParameter("border_bottomright_uv","0.9961 0.0039 1.0000 0.0000");
|
---|
| 786 | container->setLeft(5);
|
---|
| 787 | container->setTop(5);
|
---|
| 788 |
|
---|
| 789 | return container;
|
---|
| 790 |
|
---|
| 791 | }
|
---|
| 792 | //-----------------------------------------------------------------------
|
---|
| 793 | OverlayElement* Profiler::createTextArea(const String& name, Real width, Real height, Real top, Real left,
|
---|
| 794 | uint fontSize, const String& caption, bool show) {
|
---|
| 795 |
|
---|
| 796 |
|
---|
| 797 | OverlayElement* textArea =
|
---|
| 798 | OverlayManager::getSingleton().createOverlayElement("TextArea", name);
|
---|
| 799 | textArea->setMetricsMode(GMM_PIXELS);
|
---|
| 800 | textArea->setWidth(width);
|
---|
| 801 | textArea->setHeight(height);
|
---|
| 802 | textArea->setTop(top);
|
---|
| 803 | textArea->setLeft(left);
|
---|
| 804 | textArea->setParameter("font_name", "TrebuchetMSBold");
|
---|
| 805 | textArea->setParameter("char_height", StringConverter::toString(fontSize));
|
---|
| 806 | textArea->setCaption(caption);
|
---|
| 807 | textArea->setParameter("colour_top", "1 1 1");
|
---|
| 808 | textArea->setParameter("colour_bottom", "1 1 1");
|
---|
| 809 |
|
---|
| 810 | if (show) {
|
---|
| 811 | textArea->show();
|
---|
| 812 | }
|
---|
| 813 | else {
|
---|
| 814 | textArea->hide();
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 | return textArea;
|
---|
| 818 |
|
---|
| 819 | }
|
---|
| 820 | //-----------------------------------------------------------------------
|
---|
| 821 | OverlayElement* Profiler::createPanel(const String& name, Real width, Real height, Real top, Real left,
|
---|
| 822 | const String& materialName, bool show) {
|
---|
| 823 |
|
---|
| 824 | OverlayElement* panel =
|
---|
| 825 | OverlayManager::getSingleton().createOverlayElement("Panel", name);
|
---|
| 826 | panel->setMetricsMode(GMM_PIXELS);
|
---|
| 827 | panel->setWidth(width);
|
---|
| 828 | panel->setHeight(height);
|
---|
| 829 | panel->setTop(top);
|
---|
| 830 | panel->setLeft(left);
|
---|
| 831 | panel->setMaterialName(materialName);
|
---|
| 832 |
|
---|
| 833 | if (show) {
|
---|
| 834 | panel->show();
|
---|
| 835 | }
|
---|
| 836 | else {
|
---|
| 837 | panel->hide();
|
---|
| 838 | }
|
---|
| 839 |
|
---|
| 840 | return panel;
|
---|
| 841 |
|
---|
| 842 | }
|
---|
| 843 | //-----------------------------------------------------------------------
|
---|
| 844 |
|
---|
| 845 | }
|
---|