#include "config.h"
#include "ProfileNode.h"
#include "LegacyProfiler.h"
#include <wtf/DateMath.h>
#include <wtf/DataLog.h>
#include <wtf/text/StringHash.h>
using namespace WTF;
namespace JSC {
ProfileNode::ProfileNode(ExecState* callerCallFrame, const CallIdentifier& callIdentifier, ProfileNode* headNode, ProfileNode* parentNode)
: m_callerCallFrame(callerCallFrame)
, m_callIdentifier(callIdentifier)
, m_head(headNode)
, m_parent(parentNode)
, m_nextSibling(nullptr)
, m_totalTime(0)
, m_selfTime(0)
{
startTimer();
}
ProfileNode::ProfileNode(ExecState* callerCallFrame, ProfileNode* headNode, ProfileNode* nodeToCopy)
: m_callerCallFrame(callerCallFrame)
, m_callIdentifier(nodeToCopy->callIdentifier())
, m_head(headNode)
, m_parent(nodeToCopy->parent())
, m_nextSibling(0)
, m_totalTime(nodeToCopy->totalTime())
, m_selfTime(nodeToCopy->selfTime())
, m_calls(nodeToCopy->calls())
{
}
ProfileNode* ProfileNode::willExecute(ExecState* callerCallFrame, const CallIdentifier& callIdentifier)
{
for (StackIterator currentChild = m_children.begin(); currentChild != m_children.end(); ++currentChild) {
if ((*currentChild)->callIdentifier() == callIdentifier) {
(*currentChild)->startTimer();
return (*currentChild).get();
}
}
RefPtr<ProfileNode> newChild = ProfileNode::create(callerCallFrame, callIdentifier, m_head ? m_head : this, this); if (m_children.size())
m_children.last()->setNextSibling(newChild.get());
m_children.append(newChild.release());
return m_children.last().get();
}
ProfileNode* ProfileNode::didExecute()
{
endAndRecordCall();
return m_parent;
}
void ProfileNode::addChild(PassRefPtr<ProfileNode> prpChild)
{
RefPtr<ProfileNode> child = prpChild;
child->setParent(this);
if (m_children.size())
m_children.last()->setNextSibling(child.get());
m_children.append(child.release());
}
void ProfileNode::removeChild(ProfileNode* node)
{
if (!node)
return;
for (size_t i = 0; i < m_children.size(); ++i) {
if (*node == m_children[i].get()) {
m_children.remove(i);
break;
}
}
resetChildrensSiblings();
}
void ProfileNode::insertNode(PassRefPtr<ProfileNode> prpNode)
{
RefPtr<ProfileNode> node = prpNode;
for (unsigned i = 0; i < m_children.size(); ++i)
node->addChild(m_children[i].release());
m_children.clear();
m_children.append(node.release());
}
void ProfileNode::stopProfiling()
{
ASSERT(!m_calls.isEmpty());
if (isnan(m_calls.last().totalTime()))
endAndRecordCall();
for (unsigned i = 0; i < m_children.size(); ++i)
m_selfTime += m_children[i]->totalTime();
ASSERT(m_selfTime <= m_totalTime);
m_selfTime = m_totalTime - m_selfTime;
}
ProfileNode* ProfileNode::traverseNextNodePostOrder() const
{
ProfileNode* next = m_nextSibling;
if (!next)
return m_parent;
while (ProfileNode* firstChild = next->firstChild())
next = firstChild;
return next;
}
void ProfileNode::endAndRecordCall()
{
Call& last = lastCall();
ASSERT(isnan(last.totalTime()));
last.setTotalTime(currentTime() - last.startTime());
m_totalTime += last.totalTime();
}
void ProfileNode::startTimer()
{
m_calls.append(Call(currentTime()));
}
void ProfileNode::resetChildrensSiblings()
{
unsigned size = m_children.size();
for (unsigned i = 0; i < size; ++i)
m_children[i]->setNextSibling(i + 1 == size ? 0 : m_children[i + 1].get());
}
#ifndef NDEBUG
void ProfileNode::debugPrintData(int indentLevel) const
{
for (int i = 0; i < indentLevel; ++i)
dataLogF(" ");
dataLogF("Function Name %s %zu SelfTime %.3fms/%.3f%% TotalTime %.3fms/%.3f%% Next Sibling %s\n",
functionName().utf8().data(),
numberOfCalls(), m_selfTime, selfPercent(), m_totalTime, totalPercent(),
m_nextSibling ? m_nextSibling->functionName().utf8().data() : "");
++indentLevel;
for (StackIterator currentChild = m_children.begin(); currentChild != m_children.end(); ++currentChild)
(*currentChild)->debugPrintData(indentLevel);
}
double ProfileNode::debugPrintDataSampleStyle(int indentLevel, FunctionCallHashCount& countedFunctions) const
{
dataLogF(" ");
const char* name = functionName().utf8().data();
double sampleCount = m_totalTime * 1000;
if (indentLevel) {
for (int i = 0; i < indentLevel; ++i)
dataLogF(" ");
countedFunctions.add(functionName().impl());
dataLogF("%.0f %s\n", sampleCount ? sampleCount : 1, name);
} else
dataLogF("%s\n", name);
++indentLevel;
double sumOfChildrensCount = 0.0;
for (StackIterator currentChild = m_children.begin(); currentChild != m_children.end(); ++currentChild)
sumOfChildrensCount += (*currentChild)->debugPrintDataSampleStyle(indentLevel, countedFunctions);
sumOfChildrensCount *= 1000; if (sumOfChildrensCount < sampleCount) {
dataLogF(" ");
while (indentLevel--)
dataLogF(" ");
dataLogF("%.0f %s\n", sampleCount - sumOfChildrensCount, functionName().utf8().data());
}
return m_totalTime;
}
#endif
}