2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2010 Stuart Carnie
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCProfiling.h"
|
|
|
|
|
2013-08-21 07:06:34 +08:00
|
|
|
#include <chrono>
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
//#pragma mark - 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-06-20 14:13:12 +08:00
|
|
|
static Profiler* g_sSharedProfiler = NULL;
|
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)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
g_sSharedProfiler = new Profiler();
|
2012-04-19 14:35:52 +08:00
|
|
|
g_sSharedProfiler->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_sSharedProfiler;
|
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
// XXX: deprecated
|
|
|
|
Profiler* Profiler::sharedProfiler(void)
|
|
|
|
{
|
|
|
|
return Profiler::getInstance();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ProfilingTimer* Profiler::createAndAddTimerWithName(const char* timerName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
ProfilingTimer *t = new ProfilingTimer();
|
2012-04-19 14:35:52 +08:00
|
|
|
t->initWithName(timerName);
|
2013-06-15 14:03:30 +08:00
|
|
|
_activeTimers->setObject(t, timerName);
|
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-06-15 14:03:30 +08:00
|
|
|
_activeTimers->removeObjectForKey(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-06-15 14:03:30 +08:00
|
|
|
_activeTimers->removeAllObjects();
|
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
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_activeTimers = new Dictionary();
|
2013-08-22 11:12:09 +08:00
|
|
|
_activeTimers->init();
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Profiler::~Profiler(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_activeTimers);
|
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
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement* pElement = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCDICT_FOREACH(_activeTimers, pElement)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-09 14:40:43 +08:00
|
|
|
ProfilingTimer* timer = static_cast<ProfilingTimer*>(pElement->getObject());
|
2013-07-24 06:20:22 +08:00
|
|
|
log("%s", timer->description());
|
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()
|
|
|
|
: numberOfCalls(0)
|
|
|
|
, _averageTime1(0)
|
|
|
|
, _averageTime2(0)
|
|
|
|
, totalTime(0)
|
|
|
|
, minTime(100000000)
|
|
|
|
, maxTime(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ProfilingTimer::~ProfilingTimer(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
const char* ProfilingTimer::description() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-08-21 05:18:06 +08:00
|
|
|
static char s_desciption[512] = {0};
|
|
|
|
|
2013-11-07 15:12:19 +08:00
|
|
|
sprintf(s_desciption, "%s ::\tavg1: %ldµ,\tavg2: %ldµ,\tmin: %ldµ,\tmax: %ldµ,\ttotal: %.2fs,\tnr calls: %ld", _nameStr.c_str(), _averageTime1, _averageTime2, minTime, maxTime, totalTime/1000000., numberOfCalls);
|
2013-08-21 05:18:06 +08:00
|
|
|
return s_desciption;
|
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-08-21 07:06:34 +08:00
|
|
|
ProfilingTimer* timer = static_cast<ProfilingTimer*>( p->_activeTimers->objectForKey(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-06-20 14:13:12 +08:00
|
|
|
ProfilingTimer* timer = (ProfilingTimer*)p->_activeTimers->objectForKey(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
|
|
|
|
|
|
|
|
2013-11-05 08:31:36 +08:00
|
|
|
long duration = 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-06-20 14:13:12 +08:00
|
|
|
ProfilingTimer *timer = (ProfilingTimer*)p->_activeTimers->objectForKey(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
|
|
|
|
|