1
0

CCProfiling.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /****************************************************************************
  2. Copyright (c) 2010 Stuart Carnie
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  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 FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "base/CCProfiling.h"
  23. using namespace std;
  24. NS_CC_BEGIN
  25. // Profiling Categories
  26. /* set to false the categories that you don't want to profile */
  27. bool kProfilerCategorySprite = false;
  28. bool kProfilerCategoryBatchSprite = false;
  29. bool kProfilerCategoryParticles = false;
  30. static Profiler* g_sSharedProfiler = nullptr;
  31. Profiler* Profiler::getInstance()
  32. {
  33. if (! g_sSharedProfiler)
  34. {
  35. g_sSharedProfiler = new (std::nothrow) Profiler();
  36. g_sSharedProfiler->init();
  37. }
  38. return g_sSharedProfiler;
  39. }
  40. // FIXME:: deprecated
  41. Profiler* Profiler::sharedProfiler(void)
  42. {
  43. return Profiler::getInstance();
  44. }
  45. ProfilingTimer* Profiler::createAndAddTimerWithName(const char* timerName)
  46. {
  47. ProfilingTimer *t = new (std::nothrow) ProfilingTimer();
  48. t->initWithName(timerName);
  49. _activeTimers.insert(timerName, t);
  50. t->release();
  51. return t;
  52. }
  53. void Profiler::releaseTimer(const char* timerName)
  54. {
  55. _activeTimers.erase(timerName);
  56. }
  57. void Profiler::releaseAllTimers()
  58. {
  59. _activeTimers.clear();
  60. }
  61. bool Profiler::init()
  62. {
  63. return true;
  64. }
  65. Profiler::~Profiler(void)
  66. {
  67. }
  68. void Profiler::displayTimers()
  69. {
  70. for (auto& iter : _activeTimers)
  71. {
  72. ProfilingTimer* timer = iter.second;
  73. log("%s", timer->getDescription().c_str());
  74. }
  75. }
  76. // implementation of ProfilingTimer
  77. ProfilingTimer::ProfilingTimer()
  78. : _averageTime1(0)
  79. , _averageTime2(0)
  80. , minTime(100000000)
  81. , maxTime(0)
  82. , totalTime(0)
  83. , numberOfCalls(0)
  84. {
  85. }
  86. bool ProfilingTimer::initWithName(const char* timerName)
  87. {
  88. _nameStr = timerName;
  89. return true;
  90. }
  91. ProfilingTimer::~ProfilingTimer(void)
  92. {
  93. }
  94. std::string ProfilingTimer::getDescription() const
  95. {
  96. static char s_description[512] = {0};
  97. sprintf(s_description, "%s ::\tavg1: %ldu,\tavg2: %ldu,\tmin: %ldu,\tmax: %ldu,\ttotal: %.2fs,\tnr calls: %ld", _nameStr.c_str(), _averageTime1, _averageTime2, minTime, maxTime, totalTime/1000000., numberOfCalls);
  98. return s_description;
  99. }
  100. void ProfilingTimer::reset()
  101. {
  102. numberOfCalls = 0;
  103. _averageTime1 = 0;
  104. _averageTime2 = 0;
  105. totalTime = 0;
  106. minTime = 100000000;
  107. maxTime = 0;
  108. _startTime = chrono::high_resolution_clock::now();
  109. }
  110. void ProfilingBeginTimingBlock(const char *timerName)
  111. {
  112. Profiler* p = Profiler::getInstance();
  113. ProfilingTimer* timer = p->_activeTimers.at(timerName);
  114. if( ! timer )
  115. {
  116. timer = p->createAndAddTimerWithName(timerName);
  117. }
  118. timer->numberOfCalls++;
  119. // should be the last instruction in order to be more reliable
  120. timer->_startTime = chrono::high_resolution_clock::now();
  121. }
  122. void ProfilingEndTimingBlock(const char *timerName)
  123. {
  124. // should be the 1st instruction in order to be more reliable
  125. auto now = chrono::high_resolution_clock::now();
  126. Profiler* p = Profiler::getInstance();
  127. ProfilingTimer* timer = p->_activeTimers.at(timerName);
  128. CCASSERT(timer, "CCProfilingTimer not found");
  129. long duration = static_cast<long>(chrono::duration_cast<chrono::microseconds>(now - timer->_startTime).count());
  130. timer->totalTime += duration;
  131. timer->_averageTime1 = (timer->_averageTime1 + duration) / 2.0f;
  132. timer->_averageTime2 = timer->totalTime / timer->numberOfCalls;
  133. timer->maxTime = MAX( timer->maxTime, duration);
  134. timer->minTime = MIN( timer->minTime, duration);
  135. }
  136. void ProfilingResetTimingBlock(const char *timerName)
  137. {
  138. Profiler* p = Profiler::getInstance();
  139. ProfilingTimer *timer = p->_activeTimers.at(timerName);
  140. CCASSERT(timer, "CCProfilingTimer not found");
  141. timer->reset();
  142. }
  143. NS_CC_END