profiler.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "debug/profiler.h"
  24. #include "string/stringTable.h"
  25. #include <stdlib.h> // gotta use malloc and free directly
  26. #include <string.h>
  27. #include "console/console.h"
  28. #include "collection/vector.h"
  29. #include "io/fileStream.h"
  30. #include "platform/threads/thread.h"
  31. #ifdef TORQUE_ENABLE_PROFILER
  32. ProfilerRootData *ProfilerRootData::sRootList = NULL;
  33. Profiler *gProfiler = NULL;
  34. #ifdef TORQUE_MULTITHREAD
  35. U32 gMainThread = 0;
  36. #endif
  37. #if defined(TORQUE_SUPPORTS_VC_INLINE_X86_ASM)
  38. // platform specific get hires times...
  39. void startHighResolutionTimer(U32 time[2])
  40. {
  41. //time[0] = Platform::getRealMilliseconds();
  42. __asm
  43. {
  44. push eax
  45. push edx
  46. push ecx
  47. rdtsc
  48. mov ecx, time
  49. mov DWORD PTR [ecx], eax
  50. mov DWORD PTR [ecx + 4], edx
  51. pop ecx
  52. pop edx
  53. pop eax
  54. }
  55. }
  56. U32 endHighResolutionTimer(U32 time[2])
  57. {
  58. U32 ticks;
  59. //ticks = Platform::getRealMilliseconds() - time[0];
  60. //return ticks;
  61. __asm
  62. {
  63. push eax
  64. push edx
  65. push ecx
  66. //db 0fh, 31h
  67. rdtsc
  68. mov ecx, time
  69. sub edx, DWORD PTR [ecx+4]
  70. sbb eax, DWORD PTR [ecx]
  71. mov DWORD PTR ticks, eax
  72. pop ecx
  73. pop edx
  74. pop eax
  75. }
  76. return ticks;
  77. }
  78. #elif defined(TORQUE_SUPPORTS_GCC_INLINE_X86_ASM)
  79. // platform specific get hires times...
  80. void startHighResolutionTimer(U32 time[2])
  81. {
  82. __asm__ __volatile__(
  83. "rdtsc\n"
  84. : "=a" (time[0]), "=d" (time[1])
  85. );
  86. }
  87. U32 endHighResolutionTimer(U32 time[2])
  88. {
  89. U32 ticks;
  90. __asm__ __volatile__(
  91. "rdtsc\n"
  92. "sub 0x4(%%ecx), %%edx\n"
  93. "sbb (%%ecx), %%eax\n"
  94. : "=a" (ticks) : "c" (time)
  95. );
  96. return ticks;
  97. }
  98. #elif defined(TORQUE_OS_MAC_CARB)
  99. #include <Timer.h>
  100. #include <Math64.h>
  101. void startHighResolutionTimer(U32 time[2]) {
  102. UnsignedWide t;
  103. Microseconds(&t);
  104. time[0] = t.lo;
  105. time[1] = t.hi;
  106. }
  107. U32 endHighResolutionTimer(U32 time[2]) {
  108. UnsignedWide t;
  109. Microseconds(&t);
  110. return t.lo - time[0];
  111. // given that we're returning a 32 bit integer, and this is unsigned subtraction...
  112. // it will just wrap around, we don't need the upper word of the time.
  113. // NOTE: the code assumes that more than 3 hrs will not go by between calls to startHighResolutionTimer() and endHighResolutionTimer().
  114. // I mean... that damn well better not happen anyway.
  115. }
  116. #elif defined(TORQUE_OS_IOS)
  117. //-Mat added high resolution iPhone timer
  118. #import <mach/mach_time.h>
  119. #include <Math64.h>
  120. void startHighResolutionTimer(U32 time[2]) {
  121. time[0] = mach_absolute_time();
  122. }
  123. U32 endHighResolutionTimer(U32 time[2]) {
  124. return mach_absolute_time() - time[0];
  125. }
  126. #else
  127. void startHighResolutionTimer(U32 time[2])
  128. {
  129. }
  130. U32 endHighResolutionTimer(U32 time[2])
  131. {
  132. return 1;
  133. }
  134. #endif
  135. Profiler::Profiler()
  136. {
  137. mMaxStackDepth = MaxStackDepth;
  138. mCurrentHash = 0;
  139. mCurrentProfilerData = (ProfilerData *) malloc(sizeof(ProfilerData));
  140. mCurrentProfilerData->mRoot = NULL;
  141. mCurrentProfilerData->mNextForRoot = NULL;
  142. mCurrentProfilerData->mNextProfilerData = NULL;
  143. mCurrentProfilerData->mNextHash = NULL;
  144. mCurrentProfilerData->mParent = NULL;
  145. mCurrentProfilerData->mNextSibling = NULL;
  146. mCurrentProfilerData->mFirstChild = NULL;
  147. mCurrentProfilerData->mLastSeenProfiler = NULL;
  148. mCurrentProfilerData->mHash = 0;
  149. mCurrentProfilerData->mSubDepth = 0;
  150. mCurrentProfilerData->mInvokeCount = 0;
  151. mCurrentProfilerData->mTotalTime = 0;
  152. mCurrentProfilerData->mSubTime = 0;
  153. mRootProfilerData = mCurrentProfilerData;
  154. for(U32 i = 0; i < ProfilerData::HashTableSize; i++)
  155. mCurrentProfilerData->mChildHash[i] = 0;
  156. mProfileList = NULL;
  157. mEnabled = false;
  158. mStackDepth = 0;
  159. mNextEnable = false;
  160. gProfiler = this;
  161. mDumpToConsole = false;
  162. mDumpToFile = false;
  163. mDumpFileName[0] = '\0';
  164. #ifdef TORQUE_MULTITHREAD
  165. gMainThread = ThreadManager::getCurrentThreadId();
  166. #endif
  167. }
  168. Profiler::~Profiler()
  169. {
  170. reset();
  171. free(mRootProfilerData);
  172. gProfiler = NULL;
  173. }
  174. void Profiler::reset()
  175. {
  176. mEnabled = false; // in case we're in a profiler call.
  177. while(mProfileList)
  178. {
  179. free(mProfileList);
  180. mProfileList = NULL;
  181. }
  182. for(ProfilerRootData *walk = ProfilerRootData::sRootList; walk; walk = walk->mNextRoot)
  183. {
  184. walk->mFirstProfilerData = 0;
  185. walk->mTotalTime = 0;
  186. walk->mSubTime = 0;
  187. walk->mTotalInvokeCount = 0;
  188. }
  189. mCurrentProfilerData = mRootProfilerData;
  190. mCurrentProfilerData->mNextForRoot = 0;
  191. mCurrentProfilerData->mFirstChild = 0;
  192. for(U32 i = 0; i < ProfilerData::HashTableSize; i++)
  193. mCurrentProfilerData->mChildHash[i] = 0;
  194. mCurrentProfilerData->mInvokeCount = 0;
  195. mCurrentProfilerData->mTotalTime = 0;
  196. mCurrentProfilerData->mSubTime = 0;
  197. mCurrentProfilerData->mSubDepth = 0;
  198. mCurrentProfilerData->mLastSeenProfiler = 0;
  199. }
  200. static Profiler aProfiler; // allocate the global profiler
  201. ProfilerRootData::ProfilerRootData(const char *name)
  202. {
  203. for(ProfilerRootData *walk = sRootList; walk; walk = walk->mNextRoot)
  204. if(!dStrcmp(walk->mName, name))
  205. Platform::debugBreak();
  206. mName = name;
  207. mNameHash = _StringTable::hashString(name);
  208. mNextRoot = sRootList;
  209. sRootList = this;
  210. mTotalTime = 0;
  211. mTotalInvokeCount = 0;
  212. mFirstProfilerData = NULL;
  213. mEnabled = true;
  214. }
  215. void Profiler::validate()
  216. {
  217. for(ProfilerRootData *walk = ProfilerRootData::sRootList; walk; walk = walk->mNextRoot)
  218. {
  219. for(ProfilerData *dp = walk->mFirstProfilerData; dp; dp = dp->mNextForRoot)
  220. {
  221. if(dp->mRoot != walk)
  222. Platform::debugBreak();
  223. // check if it's in the parent's list...
  224. ProfilerData *wk;
  225. for(wk = dp->mParent->mFirstChild; wk; wk = wk->mNextSibling)
  226. if(wk == dp)
  227. break;
  228. if(!wk)
  229. Platform::debugBreak();
  230. for(wk = dp->mParent->mChildHash[walk->mNameHash & (ProfilerData::HashTableSize - 1)] ;
  231. wk; wk = wk->mNextHash)
  232. if(wk == dp)
  233. break;
  234. if(!wk)
  235. Platform::debugBreak();
  236. }
  237. }
  238. }
  239. void Profiler::hashPush(ProfilerRootData *root)
  240. {
  241. #ifdef TORQUE_MULTITHREAD
  242. // Ignore non-main-thread profiler activity.
  243. if(! ThreadManager::isCurrentThread(gMainThread) )
  244. return;
  245. #endif
  246. mStackDepth++;
  247. AssertFatal(mStackDepth <= (S32)mMaxStackDepth,
  248. "Stack overflow in profiler. You may have mismatched PROFILE_START and PROFILE_ENDs");
  249. if(!mEnabled)
  250. return;
  251. ProfilerData *nextProfiler = NULL;
  252. if(!root->mEnabled || mCurrentProfilerData->mRoot == root)
  253. {
  254. mCurrentProfilerData->mSubDepth++;
  255. return;
  256. }
  257. if(mCurrentProfilerData->mLastSeenProfiler &&
  258. mCurrentProfilerData->mLastSeenProfiler->mRoot == root)
  259. nextProfiler = mCurrentProfilerData->mLastSeenProfiler;
  260. if(!nextProfiler)
  261. {
  262. // first see if it's in the hash table...
  263. U32 index = root->mNameHash & (ProfilerData::HashTableSize - 1);
  264. nextProfiler = mCurrentProfilerData->mChildHash[index];
  265. while(nextProfiler)
  266. {
  267. if(nextProfiler->mRoot == root)
  268. break;
  269. nextProfiler = nextProfiler->mNextHash;
  270. }
  271. if(!nextProfiler)
  272. {
  273. nextProfiler = (ProfilerData *) malloc(sizeof(ProfilerData));
  274. for(U32 i = 0; i < ProfilerData::HashTableSize; i++)
  275. nextProfiler->mChildHash[i] = 0;
  276. nextProfiler->mRoot = root;
  277. nextProfiler->mNextForRoot = root->mFirstProfilerData;
  278. root->mFirstProfilerData = nextProfiler;
  279. nextProfiler->mNextProfilerData = mProfileList;
  280. mProfileList = nextProfiler;
  281. nextProfiler->mNextHash = mCurrentProfilerData->mChildHash[index];
  282. mCurrentProfilerData->mChildHash[index] = nextProfiler;
  283. nextProfiler->mParent = mCurrentProfilerData;
  284. nextProfiler->mNextSibling = mCurrentProfilerData->mFirstChild;
  285. mCurrentProfilerData->mFirstChild = nextProfiler;
  286. nextProfiler->mFirstChild = NULL;
  287. nextProfiler->mLastSeenProfiler = NULL;
  288. nextProfiler->mHash = root->mNameHash;
  289. nextProfiler->mInvokeCount = 0;
  290. nextProfiler->mTotalTime = 0;
  291. nextProfiler->mSubTime = 0;
  292. nextProfiler->mSubDepth = 0;
  293. }
  294. }
  295. root->mTotalInvokeCount++;
  296. nextProfiler->mInvokeCount++;
  297. startHighResolutionTimer(nextProfiler->mStartTime);
  298. mCurrentProfilerData->mLastSeenProfiler = nextProfiler;
  299. mCurrentProfilerData = nextProfiler;
  300. }
  301. void Profiler::enable(bool enabled)
  302. {
  303. mNextEnable = enabled;
  304. if ( enabled )
  305. Con::printf( "Profiler is on." );
  306. else
  307. Con::printf("Profiler is off." );
  308. }
  309. void Profiler::dumpToConsole()
  310. {
  311. mDumpToConsole = true;
  312. mDumpToFile = false;
  313. mDumpFileName[0] = '\0';
  314. }
  315. void Profiler::dumpToFile(const char* fileName)
  316. {
  317. AssertFatal(dStrlen(fileName) < DumpFileNameLength, "Error, dump filename too long");
  318. mDumpToFile = true;
  319. mDumpToConsole = false;
  320. dStrcpy(mDumpFileName, fileName);
  321. }
  322. void Profiler::hashPop()
  323. {
  324. #ifdef TORQUE_MULTITHREAD
  325. // Ignore non-main-thread profiler activity.
  326. if(! ThreadManager::isCurrentThread(gMainThread) )
  327. return;
  328. #endif
  329. mStackDepth--;
  330. AssertFatal(mStackDepth >= 0, "Stack underflow in profiler. You may have mismatched PROFILE_START and PROFILE_ENDs");
  331. if(mEnabled)
  332. {
  333. if(mCurrentProfilerData->mSubDepth)
  334. {
  335. mCurrentProfilerData->mSubDepth--;
  336. return;
  337. }
  338. F64 fElapsed = endHighResolutionTimer(mCurrentProfilerData->mStartTime);
  339. mCurrentProfilerData->mTotalTime += fElapsed;
  340. mCurrentProfilerData->mParent->mSubTime += fElapsed; // mark it in the parent as well...
  341. mCurrentProfilerData->mRoot->mTotalTime += fElapsed;
  342. if(mCurrentProfilerData->mParent->mRoot)
  343. mCurrentProfilerData->mParent->mRoot->mSubTime += fElapsed; // mark it in the parent as well...
  344. mCurrentProfilerData = mCurrentProfilerData->mParent;
  345. }
  346. if(mStackDepth == 0)
  347. {
  348. // apply the next enable...
  349. if(mDumpToConsole || mDumpToFile)
  350. {
  351. dump();
  352. startHighResolutionTimer(mCurrentProfilerData->mStartTime);
  353. }
  354. if(!mEnabled && mNextEnable)
  355. startHighResolutionTimer(mCurrentProfilerData->mStartTime);
  356. mEnabled = mNextEnable;
  357. }
  358. }
  359. static S32 QSORT_CALLBACK rootDataCompare(const void *s1, const void *s2)
  360. {
  361. const ProfilerRootData *r1 = *((ProfilerRootData **) s1);
  362. const ProfilerRootData *r2 = *((ProfilerRootData **) s2);
  363. return (S32)((r2->mTotalTime - r2->mSubTime) - (r1->mTotalTime - r1->mSubTime));
  364. }
  365. static void profilerDataDumpRecurse(ProfilerData *data, char *buffer, U32 bufferLen, F64 totalTime)
  366. {
  367. // dump out this one:
  368. Con::printf("%7.3f %7.3f %8d %s%s",
  369. 100 * data->mTotalTime / totalTime,
  370. 100 * (data->mTotalTime - data->mSubTime) / totalTime,
  371. data->mInvokeCount,
  372. buffer,
  373. data->mRoot ? data->mRoot->mName : "ROOT" );
  374. data->mTotalTime = 0;
  375. data->mSubTime = 0;
  376. data->mInvokeCount = 0;
  377. buffer[bufferLen] = ' ';
  378. buffer[bufferLen+1] = ' ';
  379. buffer[bufferLen+2] = 0;
  380. // sort data's children...
  381. ProfilerData *list = NULL;
  382. while(data->mFirstChild)
  383. {
  384. ProfilerData *ins = data->mFirstChild;
  385. data->mFirstChild = ins->mNextSibling;
  386. ProfilerData **walk = &list;
  387. while(*walk && (*walk)->mTotalTime > ins->mTotalTime)
  388. walk = &(*walk)->mNextSibling;
  389. ins->mNextSibling = *walk;
  390. *walk = ins;
  391. }
  392. data->mFirstChild = list;
  393. while(list)
  394. {
  395. if(list->mInvokeCount)
  396. profilerDataDumpRecurse(list, buffer, bufferLen + 2, totalTime);
  397. list = list->mNextSibling;
  398. }
  399. buffer[bufferLen] = 0;
  400. }
  401. static void profilerDataDumpRecurseFile(ProfilerData *data, char *buffer, U32 bufferLen, F64 totalTime, FileStream& fws)
  402. {
  403. char pbuffer[256];
  404. dSprintf(pbuffer, 255, "%7.3f %7.3f %8d %s%s\n",
  405. 100 * data->mTotalTime / totalTime,
  406. 100 * (data->mTotalTime - data->mSubTime) / totalTime,
  407. data->mInvokeCount,
  408. buffer,
  409. data->mRoot ? data->mRoot->mName : "ROOT" );
  410. fws.write(dStrlen(pbuffer), pbuffer);
  411. data->mTotalTime = 0;
  412. data->mSubTime = 0;
  413. data->mInvokeCount = 0;
  414. buffer[bufferLen] = ' ';
  415. buffer[bufferLen+1] = ' ';
  416. buffer[bufferLen+2] = 0;
  417. // sort data's children...
  418. ProfilerData *list = NULL;
  419. while(data->mFirstChild)
  420. {
  421. ProfilerData *ins = data->mFirstChild;
  422. data->mFirstChild = ins->mNextSibling;
  423. ProfilerData **walk = &list;
  424. while(*walk && (*walk)->mTotalTime > ins->mTotalTime)
  425. walk = &(*walk)->mNextSibling;
  426. ins->mNextSibling = *walk;
  427. *walk = ins;
  428. }
  429. data->mFirstChild = list;
  430. while(list)
  431. {
  432. if(list->mInvokeCount)
  433. profilerDataDumpRecurseFile(list, buffer, bufferLen + 2, totalTime, fws);
  434. list = list->mNextSibling;
  435. }
  436. buffer[bufferLen] = 0;
  437. }
  438. void Profiler::dump()
  439. {
  440. bool enableSave = mEnabled;
  441. mEnabled = false;
  442. mStackDepth++;
  443. // may have some profiled calls... gotta turn em off.
  444. Vector<ProfilerRootData *> rootVector;
  445. F64 totalTime = 0;
  446. for(ProfilerRootData *walk = ProfilerRootData::sRootList; walk; walk = walk->mNextRoot)
  447. {
  448. totalTime += walk->mTotalTime - walk->mSubTime;
  449. rootVector.push_back(walk);
  450. }
  451. //-Mat no sort, makes it easier to compare
  452. dQsort((void *)rootVector.address(), rootVector.size(), sizeof(ProfilerRootData *), rootDataCompare);
  453. if (mDumpToConsole == true)
  454. {
  455. Con::printf("Profiler Data Dump:");
  456. Con::printf("Ordered by non-sub total time -");
  457. Con::printf("%%NSTime %% Time Invoke # Name");
  458. for(U32 i = 0; i < (U32)rootVector.size(); i++)
  459. {
  460. Con::printf("%7.3f %7.3f %8d %s",
  461. 100 * (rootVector[i]->mTotalTime - rootVector[i]->mSubTime) / totalTime,
  462. 100 * rootVector[i]->mTotalTime / totalTime,
  463. rootVector[i]->mTotalInvokeCount,
  464. rootVector[i]->mName);
  465. rootVector[i]->mTotalInvokeCount = 0;
  466. rootVector[i]->mTotalTime = 0;
  467. rootVector[i]->mSubTime = 0;
  468. }
  469. Con::printf("");
  470. Con::printf("Ordered by stack trace total time -");
  471. Con::printf("%% Time %% NSTime Invoke # Name");
  472. mCurrentProfilerData->mTotalTime = endHighResolutionTimer(mCurrentProfilerData->mStartTime);
  473. char depthBuffer[MaxStackDepth * 2 + 1];
  474. depthBuffer[0] = 0;
  475. profilerDataDumpRecurse(mCurrentProfilerData, depthBuffer, 0, totalTime);
  476. mEnabled = enableSave;
  477. mStackDepth--;
  478. }
  479. else if (mDumpToFile == true && mDumpFileName[0] != '\0')
  480. {
  481. FileStream fws;
  482. bool success = fws.open(mDumpFileName, FileStream::Write);
  483. AssertFatal(success, "Nuts! Cannot write profile dump to specified file!");
  484. char buffer[1024];
  485. dStrcpy(buffer, "Profiler Data Dump:\n");
  486. fws.write(dStrlen(buffer), buffer);
  487. dStrcpy(buffer, "Ordered by non-sub total time -\n");
  488. fws.write(dStrlen(buffer), buffer);
  489. dStrcpy(buffer, "%%NSTime %% Time Invoke # Name\n");
  490. fws.write(dStrlen(buffer), buffer);
  491. for(U32 i = 0; i < (U32)rootVector.size(); i++)
  492. {
  493. dSprintf(buffer, 1023, "%7.3f %7.3f %8d %s\n",
  494. 100 * (rootVector[i]->mTotalTime - rootVector[i]->mSubTime) / totalTime,
  495. 100 * rootVector[i]->mTotalTime / totalTime,
  496. rootVector[i]->mTotalInvokeCount,
  497. rootVector[i]->mName);
  498. fws.write(dStrlen(buffer), buffer);
  499. rootVector[i]->mTotalInvokeCount = 0;
  500. rootVector[i]->mTotalTime = 0;
  501. rootVector[i]->mSubTime = 0;
  502. }
  503. dStrcpy(buffer, "\nOrdered by non-sub total time -\n");
  504. fws.write(dStrlen(buffer), buffer);
  505. dStrcpy(buffer, "%%NSTime %% Time Invoke # Name\n");
  506. fws.write(dStrlen(buffer), buffer);
  507. mCurrentProfilerData->mTotalTime = endHighResolutionTimer(mCurrentProfilerData->mStartTime);
  508. char depthBuffer[MaxStackDepth * 2 + 1];
  509. depthBuffer[0] = 0;
  510. profilerDataDumpRecurseFile(mCurrentProfilerData, depthBuffer, 0, totalTime, fws);
  511. mEnabled = enableSave;
  512. mStackDepth--;
  513. fws.close();
  514. }
  515. mDumpToConsole = false;
  516. mDumpToFile = false;
  517. mDumpFileName[0] = '\0';
  518. }
  519. void Profiler::enableMarker(const char *marker, bool enable)
  520. {
  521. reset();
  522. U32 markerLen = dStrlen(marker);
  523. if(markerLen == 0)
  524. return;
  525. bool sn = marker[markerLen - 1] == '*';
  526. for(ProfilerRootData *data = ProfilerRootData::sRootList; data; data = data->mNextRoot)
  527. {
  528. if(sn)
  529. {
  530. if(!dStrncmp(marker, data->mName, markerLen - 1))
  531. data->mEnabled = enable;
  532. }
  533. else
  534. {
  535. if(!dStrcmp(marker, data->mName))
  536. data->mEnabled = enable;
  537. }
  538. }
  539. }
  540. ConsoleFunctionGroupBegin( Profiler, "Profiler functionality.");
  541. ConsoleFunction(profilerMarkerEnable, void, 3, 3, "(string markerName, bool enable) Enables (or disables) a marker for the profiler\n"
  542. "@param markerName The name of the marker to (un)set\n"
  543. "@param enable Boolean value. Set if true, unset if false\n"
  544. "@return No Return Value")
  545. {
  546. if(gProfiler)
  547. gProfiler->enableMarker(argv[1], dAtob(argv[2]));
  548. }
  549. ConsoleFunction(profilerEnable, void, 2, 2, "( enable ) Use the profileEnable function to enable (or disable) engine profiling.\n"
  550. "@param enable A boolean value. If set to true and the engine was compiled with DEBUG specified, engine profiling is enabled, otherwise it is disabled.\n"
  551. "@return No return value.")
  552. {
  553. if(gProfiler)
  554. gProfiler->enable(dAtob(argv[1]));
  555. }
  556. ConsoleFunction(profilerDump, void, 1, 1, "() Use the profilerDump function to dump engine profile statistics to the console.\n"
  557. "@return No return value")
  558. {
  559. if(gProfiler)
  560. gProfiler->dumpToConsole();
  561. }
  562. ConsoleFunction(profilerDumpToFile, void, 2, 2, "(string filename) Dump profiling stats to a file.")
  563. {
  564. if(gProfiler)
  565. gProfiler->dumpToFile(argv[1]);
  566. }
  567. ConsoleFunction(profilerReset, void, 1, 1, "Resets the profiler, clearing all of its data.")
  568. {
  569. if(gProfiler)
  570. gProfiler->reset();
  571. }
  572. ConsoleFunctionGroupEnd( Profiler );
  573. #endif