CCProfiling.cpp 4.8 KB

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