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"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
//#pragma mark - Profiling Categories
|
|
|
|
/* set to NO the categories that you don't want to profile */
|
|
|
|
bool kCCProfilerCategorySprite = false;
|
|
|
|
bool kCCProfilerCategoryBatchSprite = false;
|
|
|
|
bool kCCProfilerCategoryParticles = false;
|
|
|
|
|
|
|
|
|
|
|
|
static CCProfiler* g_sSharedProfiler = NULL;
|
|
|
|
|
|
|
|
CCProfiler* CCProfiler::sharedProfiler(void)
|
|
|
|
{
|
|
|
|
if (! g_sSharedProfiler)
|
|
|
|
{
|
|
|
|
g_sSharedProfiler = new CCProfiler();
|
|
|
|
g_sSharedProfiler->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_sSharedProfiler;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCProfilingTimer* CCProfiler::createAndAddTimerWithName(const char* timerName)
|
|
|
|
{
|
|
|
|
CCProfilingTimer *t = new CCProfilingTimer();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCProfiler::releaseTimer(const char* timerName)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_activeTimers->removeObjectForKey(timerName);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCProfiler::releaseAllTimers()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_activeTimers->removeAllObjects();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCProfiler::init()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_activeTimers = new CCDictionary();
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCProfiler::~CCProfiler(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_activeTimers);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCProfiler::displayTimers()
|
|
|
|
{
|
|
|
|
CCDictElement* pElement = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCDICT_FOREACH(_activeTimers, pElement)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCProfilingTimer* timer = (CCProfilingTimer*)pElement->getObject();
|
2013-04-19 15:47:14 +08:00
|
|
|
CCLog("%s", timer->description());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of CCProfilingTimer
|
|
|
|
|
|
|
|
bool CCProfilingTimer::initWithName(const char* timerName)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_nameStr = timerName;
|
2012-04-19 14:35:52 +08:00
|
|
|
numberOfCalls = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
_averageTime = 0.0;
|
2012-04-19 14:35:52 +08:00
|
|
|
totalTime = 0.0;
|
|
|
|
minTime = 10000.0;
|
|
|
|
maxTime = 0.0;
|
2013-06-15 14:03:30 +08:00
|
|
|
gettimeofday((struct timeval *)&_startTime, NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCProfilingTimer::~CCProfilingTimer(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCProfilingTimer::description()
|
|
|
|
{
|
|
|
|
static char s_szDesciption[256] = {0};
|
2013-06-15 14:03:30 +08:00
|
|
|
sprintf(s_szDesciption, "%s: avg time, %fms", _nameStr.c_str(), _averageTime);
|
2012-04-19 14:35:52 +08:00
|
|
|
return s_szDesciption;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCProfilingTimer::reset()
|
|
|
|
{
|
|
|
|
numberOfCalls = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
_averageTime = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
totalTime = 0;
|
|
|
|
minTime = 10000;
|
|
|
|
maxTime = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
gettimeofday((struct timeval *)&_startTime, NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCProfilingBeginTimingBlock(const char *timerName)
|
|
|
|
{
|
|
|
|
CCProfiler* p = CCProfiler::sharedProfiler();
|
2013-06-15 14:03:30 +08:00
|
|
|
CCProfilingTimer* timer = (CCProfilingTimer*)p->_activeTimers->objectForKey(timerName);
|
2012-04-19 14:35:52 +08:00
|
|
|
if( ! timer )
|
|
|
|
{
|
|
|
|
timer = p->createAndAddTimerWithName(timerName);
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
gettimeofday((struct timeval *)&timer->_startTime, NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
timer->numberOfCalls++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCProfilingEndTimingBlock(const char *timerName)
|
|
|
|
{
|
|
|
|
CCProfiler* p = CCProfiler::sharedProfiler();
|
2013-06-15 14:03:30 +08:00
|
|
|
CCProfilingTimer* timer = (CCProfilingTimer*)p->_activeTimers->objectForKey(timerName);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CCAssert(timer, "CCProfilingTimer not found");
|
|
|
|
|
|
|
|
struct timeval currentTime;
|
|
|
|
gettimeofday(¤tTime, NULL);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
double duration = CCTime::timersubCocos2d((struct cc_timeval *)&timer->_startTime, (struct cc_timeval *)¤tTime);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// milliseconds
|
2013-06-15 14:03:30 +08:00
|
|
|
timer->_averageTime = (timer->_averageTime + duration) / 2.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
timer->totalTime += duration;
|
|
|
|
timer->maxTime = MAX( timer->maxTime, duration);
|
|
|
|
timer->minTime = MIN( timer->minTime, duration);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCProfilingResetTimingBlock(const char *timerName)
|
|
|
|
{
|
|
|
|
CCProfiler* p = CCProfiler::sharedProfiler();
|
2013-06-15 14:03:30 +08:00
|
|
|
CCProfilingTimer *timer = (CCProfilingTimer*)p->_activeTimers->objectForKey(timerName);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CCAssert(timer, "CCProfilingTimer not found");
|
|
|
|
|
|
|
|
timer->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|