2010-08-23 16:32:37 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2010 Stuart Carnie
|
2010-08-23 16:32:37 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCProfiling.h"
|
|
|
|
|
|
|
|
#if CC_ENABLE_PROFILERS
|
|
|
|
|
|
|
|
namespace cocos2d
|
|
|
|
{
|
|
|
|
using namespace std;
|
|
|
|
static CCProfiler *g_sSharedProfiler;
|
|
|
|
|
2010-11-16 15:12:09 +08:00
|
|
|
CCProfiler* CCProfiler::sharedProfiler(void)
|
2010-08-23 16:32:37 +08:00
|
|
|
{
|
|
|
|
if (! g_sSharedProfiler)
|
|
|
|
{
|
|
|
|
g_sSharedProfiler = new CCProfiler();
|
|
|
|
g_sSharedProfiler->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_sSharedProfiler;
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCProfilingTimer* CCProfiler::timerWithName(const char *pszTimerName, CCObject *pInstance)
|
2010-08-23 16:32:37 +08:00
|
|
|
{
|
2010-11-16 15:12:09 +08:00
|
|
|
CCProfiler *p = CCProfiler::sharedProfiler();
|
2010-08-23 16:32:37 +08:00
|
|
|
CCProfilingTimer *t = new CCProfilingTimer();
|
|
|
|
t->initWithName(pszTimerName, pInstance);
|
|
|
|
p->m_pActiveTimers->addObject(t);
|
|
|
|
t->release();
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCProfiler::releaseTimer(CCProfilingTimer *pTimer)
|
|
|
|
{
|
2010-11-16 15:12:09 +08:00
|
|
|
CCProfiler *p = CCProfiler::sharedProfiler();
|
2010-08-23 16:32:37 +08:00
|
|
|
p->m_pActiveTimers->removeObject(pTimer);
|
2011-04-25 14:30:30 +08:00
|
|
|
|
|
|
|
if (0 == (p->m_pActiveTimers->count()))
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(g_sSharedProfiler);
|
|
|
|
}
|
2010-08-23 16:32:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCProfiler::init()
|
|
|
|
{
|
2011-04-25 14:30:30 +08:00
|
|
|
m_pActiveTimers = CCArray::array();
|
|
|
|
m_pActiveTimers->retain();
|
2010-08-23 16:32:37 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCProfiler::~CCProfiler(void)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_RELEASE(m_pActiveTimers);
|
2010-08-23 16:32:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCProfiler::displayTimers()
|
|
|
|
{
|
2011-04-25 14:30:30 +08:00
|
|
|
CCObject* pObject = NULL;
|
|
|
|
CCProfilingTimer* pTimer = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pActiveTimers, pObject)
|
2010-08-23 16:32:37 +08:00
|
|
|
{
|
2011-04-25 14:30:30 +08:00
|
|
|
pTimer = (CCProfilingTimer*) pObject;
|
|
|
|
char *pszDescription = pTimer->description();
|
|
|
|
CCLog(pszDescription);
|
2010-08-23 16:32:37 +08:00
|
|
|
delete pszDescription;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of CCProfilingTimer
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
bool CCProfilingTimer::initWithName(const char* pszTimerName, CCObject *pInstance)
|
2010-08-23 16:32:37 +08:00
|
|
|
{
|
2011-04-25 14:30:30 +08:00
|
|
|
char tmp[160];
|
2010-08-23 16:32:37 +08:00
|
|
|
sprintf(tmp, "%s (0x%.8x)", pszTimerName, pInstance);
|
|
|
|
m_NameStr = string(tmp);
|
2011-04-25 14:30:30 +08:00
|
|
|
m_dAverageTime = 0.0;
|
2010-08-23 16:32:37 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCProfilingTimer::~CCProfilingTimer(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
char* CCProfilingTimer::description()
|
|
|
|
{
|
2011-04-25 14:30:30 +08:00
|
|
|
char *pszDes = new char[m_NameStr.length() + sizeof(double) + 32];
|
2010-08-23 16:32:37 +08:00
|
|
|
sprintf(pszDes, "%s: avg time, %fms", m_NameStr.c_str(), m_dAverageTime);
|
|
|
|
return pszDes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCProfilingBeginTimingBlock(CCProfilingTimer *pTimer)
|
|
|
|
{
|
2011-04-25 14:30:30 +08:00
|
|
|
CCTime::gettimeofdayCocos2d(pTimer->getStartTime(), NULL);
|
2010-08-23 16:32:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCProfilingEndTimingBlock(CCProfilingTimer *pTimer)
|
|
|
|
{
|
|
|
|
struct cc_timeval currentTime;
|
2010-12-18 08:17:53 +08:00
|
|
|
CCTime::gettimeofdayCocos2d(¤tTime, NULL);
|
2011-04-25 14:30:30 +08:00
|
|
|
CCTime::timersubCocos2d(¤tTime, pTimer->getStartTime(), ¤tTime);
|
2010-08-23 16:32:37 +08:00
|
|
|
double duration = currentTime.tv_sec * 1000.0 + currentTime.tv_usec / 1000.0;
|
|
|
|
|
|
|
|
// return in milliseconds
|
|
|
|
pTimer->setAverageTime((pTimer->getAverageTime() + duration) / 2.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end of namespace cocos2d
|
|
|
|
|
|
|
|
#endif // CC_ENABLE_PROFILERS
|