2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 Stuart Carnie
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "base/CCProfiling.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-03-26 14:54:50 +08:00
|
|
|
// Profiling Categories
|
2013-08-01 16:39:42 +08:00
|
|
|
/* set to false the categories that you don't want to profile */
|
2013-06-20 14:13:12 +08:00
|
|
|
bool kProfilerCategorySprite = false;
|
|
|
|
bool kProfilerCategoryBatchSprite = false;
|
|
|
|
bool kProfilerCategoryParticles = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
static Profiler* g_sSharedProfiler = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Profiler* Profiler::getInstance()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (! g_sSharedProfiler)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
g_sSharedProfiler = new Profiler();
|
2012-04-19 14:35:52 +08:00
|
|
|
g_sSharedProfiler->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_sSharedProfiler;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ProfilingTimer* Profiler::createAndAddTimerWithName(const char* timerName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
ProfilingTimer *t = new ProfilingTimer();
|
2012-04-19 14:35:52 +08:00
|
|
|
t->initWithName(timerName);
|
2013-12-11 17:15:27 +08:00
|
|
|
_activeTimers.insert(timerName, t);
|
2012-04-19 14:35:52 +08:00
|
|
|
t->release();
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Profiler::releaseTimer(const char* timerName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
_activeTimers.erase(timerName);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Profiler::releaseAllTimers()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
_activeTimers.clear();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Profiler::init()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-22 09:38:46 +08:00
|
|
|
Profiler::~Profiler()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Profiler::displayTimers()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for (auto& iter : _activeTimers)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
ProfilingTimer* timer = iter.second;
|
2013-12-13 06:38:12 +08:00
|
|
|
log("%s", timer->getDescription().c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of ProfilingTimer
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-21 07:06:34 +08:00
|
|
|
ProfilingTimer::ProfilingTimer()
|
2013-11-13 11:22:34 +08:00
|
|
|
: _averageTime1(0)
|
2013-08-21 07:06:34 +08:00
|
|
|
, _averageTime2(0)
|
|
|
|
, minTime(100000000)
|
|
|
|
, maxTime(0)
|
2013-11-13 11:22:34 +08:00
|
|
|
, totalTime(0)
|
|
|
|
, numberOfCalls(0)
|
2013-08-21 07:06:34 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ProfilingTimer::initWithName(const char* timerName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_nameStr = timerName;
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-22 09:38:46 +08:00
|
|
|
ProfilingTimer::~ProfilingTimer()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-13 06:38:12 +08:00
|
|
|
std::string ProfilingTimer::getDescription() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-06-29 10:04:11 +08:00
|
|
|
static char s_description[512] = {0};
|
2013-08-21 05:18:06 +08:00
|
|
|
|
2021-09-02 18:30:20 +08:00
|
|
|
sprintf(s_description, "%s ::\tavg1: %u,\tavg2: %u,\tmin: %u,\tmax: %u,\ttotal: %.2fs,\tnr calls: %d", _nameStr.c_str(), _averageTime1, _averageTime2, minTime, maxTime, totalTime/1000000., numberOfCalls);
|
2016-06-29 10:04:11 +08:00
|
|
|
return s_description;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ProfilingTimer::reset()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
numberOfCalls = 0;
|
2013-08-21 07:06:34 +08:00
|
|
|
_averageTime1 = 0;
|
|
|
|
_averageTime2 = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
totalTime = 0;
|
2013-08-21 07:06:34 +08:00
|
|
|
minTime = 100000000;
|
2012-04-19 14:35:52 +08:00
|
|
|
maxTime = 0;
|
2013-08-21 07:06:34 +08:00
|
|
|
_startTime = chrono::high_resolution_clock::now();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ProfilingBeginTimingBlock(const char *timerName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Profiler* p = Profiler::getInstance();
|
2013-12-11 17:15:27 +08:00
|
|
|
ProfilingTimer* timer = p->_activeTimers.at(timerName);
|
2012-04-19 14:35:52 +08:00
|
|
|
if( ! timer )
|
|
|
|
{
|
|
|
|
timer = p->createAndAddTimerWithName(timerName);
|
|
|
|
}
|
|
|
|
|
|
|
|
timer->numberOfCalls++;
|
2013-08-21 07:06:34 +08:00
|
|
|
|
2013-08-29 09:29:09 +08:00
|
|
|
// should be the last instruction in order to be more reliable
|
2013-08-21 07:06:34 +08:00
|
|
|
timer->_startTime = chrono::high_resolution_clock::now();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ProfilingEndTimingBlock(const char *timerName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-08-29 09:29:09 +08:00
|
|
|
// should be the 1st instruction in order to be more reliable
|
|
|
|
auto now = chrono::high_resolution_clock::now();
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Profiler* p = Profiler::getInstance();
|
2013-12-11 17:15:27 +08:00
|
|
|
ProfilingTimer* timer = p->_activeTimers.at(timerName);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(timer, "CCProfilingTimer not found");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
2021-09-02 13:39:28 +08:00
|
|
|
int32_t duration = static_cast<int32_t>(chrono::duration_cast<chrono::microseconds>(now - timer->_startTime).count());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
timer->totalTime += duration;
|
2013-08-21 07:06:34 +08:00
|
|
|
timer->_averageTime1 = (timer->_averageTime1 + duration) / 2.0f;
|
|
|
|
timer->_averageTime2 = timer->totalTime / timer->numberOfCalls;
|
2012-04-19 14:35:52 +08:00
|
|
|
timer->maxTime = MAX( timer->maxTime, duration);
|
|
|
|
timer->minTime = MIN( timer->minTime, duration);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ProfilingResetTimingBlock(const char *timerName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Profiler* p = Profiler::getInstance();
|
2013-12-11 17:15:27 +08:00
|
|
|
ProfilingTimer *timer = p->_activeTimers.at(timerName);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(timer, "CCProfilingTimer not found");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
timer->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|