X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/2d39b0e377c0896910ee49ae70082ba665faf986..refs/heads/master:/profiler/ProfileGenerator.cpp diff --git a/profiler/ProfileGenerator.cpp b/profiler/ProfileGenerator.cpp index d37b356..f70e4a3 100644 --- a/profiler/ProfileGenerator.cpp +++ b/profiler/ProfileGenerator.cpp @@ -39,30 +39,34 @@ namespace JSC { -PassRefPtr ProfileGenerator::create(ExecState* exec, const String& title, unsigned uid) +Ref ProfileGenerator::create(ExecState* exec, const String& title, unsigned uid, PassRefPtr stopwatch) { - return adoptRef(new ProfileGenerator(exec, title, uid)); + return adoptRef(*new ProfileGenerator(exec, title, uid, stopwatch)); } -ProfileGenerator::ProfileGenerator(ExecState* exec, const String& title, unsigned uid) - : m_origin(exec ? exec->lexicalGlobalObject() : 0) +ProfileGenerator::ProfileGenerator(ExecState* exec, const String& title, unsigned uid, PassRefPtr stopwatch) + : m_origin(exec ? exec->lexicalGlobalObject() : nullptr) , m_profileGroup(exec ? exec->lexicalGlobalObject()->profileGroup() : 0) + , m_stopwatch(stopwatch) , m_foundConsoleStartParent(false) + , m_suspended(false) { - m_profile = Profile::create(title, uid); - m_currentNode = m_head = m_profile->head(); + double startTime = m_stopwatch->elapsedTime(); + m_profile = Profile::create(title, uid, startTime); + m_currentNode = m_rootNode = m_profile->rootNode(); if (exec) - addParentForConsoleStart(exec); + addParentForConsoleStart(exec, startTime); } class AddParentForConsoleStartFunctor { public: - AddParentForConsoleStartFunctor(ExecState* exec, RefPtr& head, RefPtr& currentNode) + AddParentForConsoleStartFunctor(ExecState* exec, RefPtr& rootNode, RefPtr& currentNode, double startTime) : m_exec(exec) , m_hasSkippedFirstFrame(false) , m_foundParent(false) - , m_head(head) + , m_rootNode(rootNode) , m_currentNode(currentNode) + , m_startTime(startTime) { } @@ -78,8 +82,9 @@ public: unsigned line = 0; unsigned column = 0; visitor->computeLineAndColumn(line, column); - m_currentNode = ProfileNode::create(m_exec, LegacyProfiler::createCallIdentifier(m_exec, visitor->callee(), visitor->sourceURL(), line, column), m_head.get(), m_head.get()); - m_head->insertNode(m_currentNode.get()); + m_currentNode = ProfileNode::create(m_exec, LegacyProfiler::createCallIdentifier(m_exec, visitor->callee(), visitor->sourceURL(), line, column), m_rootNode.get()); + m_currentNode->appendCall(ProfileNode::Call(m_startTime)); + m_rootNode->spliceNode(m_currentNode.get()); m_foundParent = true; return StackVisitor::Done; @@ -88,14 +93,15 @@ public: private: ExecState* m_exec; bool m_hasSkippedFirstFrame; - bool m_foundParent; - RefPtr& m_head; + bool m_foundParent; + RefPtr& m_rootNode; RefPtr& m_currentNode; + double m_startTime; }; -void ProfileGenerator::addParentForConsoleStart(ExecState* exec) +void ProfileGenerator::addParentForConsoleStart(ExecState* exec, double startTime) { - AddParentForConsoleStartFunctor functor(exec, m_head, m_currentNode); + AddParentForConsoleStartFunctor functor(exec, m_rootNode, m_currentNode, startTime); exec->iterate(functor); m_foundConsoleStartParent = functor.foundParent(); @@ -106,19 +112,56 @@ const String& ProfileGenerator::title() const return m_profile->title(); } +void ProfileGenerator::beginCallEntry(ProfileNode* node, double startTime) +{ + ASSERT_ARG(node, node); + + if (std::isnan(startTime)) + startTime = m_stopwatch->elapsedTime(); + + node->appendCall(ProfileNode::Call(startTime)); +} + +void ProfileGenerator::endCallEntry(ProfileNode* node) +{ + ASSERT_ARG(node, node); + + ProfileNode::Call& last = node->lastCall(); + + double previousElapsedTime = std::isnan(last.elapsedTime()) ? 0.0 : last.elapsedTime(); + double newlyElapsedTime = m_stopwatch->elapsedTime() - last.startTime(); + last.setElapsedTime(previousElapsedTime + newlyElapsedTime); +} + void ProfileGenerator::willExecute(ExecState* callerCallFrame, const CallIdentifier& callIdentifier) { if (JAVASCRIPTCORE_PROFILE_WILL_EXECUTE_ENABLED()) { CString name = callIdentifier.functionName().utf8(); CString url = callIdentifier.url().utf8(); - JAVASCRIPTCORE_PROFILE_WILL_EXECUTE(m_profileGroup, const_cast(name.data()), const_cast(url.data()), callIdentifier.lineNumber()); + JAVASCRIPTCORE_PROFILE_WILL_EXECUTE(m_profileGroup, const_cast(name.data()), const_cast(url.data()), callIdentifier.lineNumber(), callIdentifier.columnNumber()); } if (!m_origin) return; - ASSERT(m_currentNode); - m_currentNode = m_currentNode->willExecute(callerCallFrame, callIdentifier); + if (m_suspended) + return; + + RefPtr calleeNode = nullptr; + + // Find or create a node for the callee call frame. + for (const RefPtr& child : m_currentNode->children()) { + if (child->callIdentifier() == callIdentifier) + calleeNode = child; + } + + if (!calleeNode) { + calleeNode = ProfileNode::create(callerCallFrame, callIdentifier, m_currentNode.get()); + m_currentNode->addChild(calleeNode); + } + + m_currentNode = calleeNode; + beginCallEntry(calleeNode.get(), m_stopwatch->elapsedTime()); } void ProfileGenerator::didExecute(ExecState* callerCallFrame, const CallIdentifier& callIdentifier) @@ -126,26 +169,35 @@ void ProfileGenerator::didExecute(ExecState* callerCallFrame, const CallIdentifi if (JAVASCRIPTCORE_PROFILE_DID_EXECUTE_ENABLED()) { CString name = callIdentifier.functionName().utf8(); CString url = callIdentifier.url().utf8(); - JAVASCRIPTCORE_PROFILE_DID_EXECUTE(m_profileGroup, const_cast(name.data()), const_cast(url.data()), callIdentifier.lineNumber()); + JAVASCRIPTCORE_PROFILE_DID_EXECUTE(m_profileGroup, const_cast(name.data()), const_cast(url.data()), callIdentifier.lineNumber(), callIdentifier.columnNumber()); } if (!m_origin) return; + if (m_suspended) + return; + + // Make a new node if the caller node has never seen this callee call frame before. + // This can happen if |console.profile()| is called several frames deep in the call stack. ASSERT(m_currentNode); if (m_currentNode->callIdentifier() != callIdentifier) { - RefPtr returningNode = ProfileNode::create(callerCallFrame, callIdentifier, m_head.get(), m_currentNode.get()); - returningNode->lastCall().setStartTime(m_currentNode->lastCall().startTime()); - returningNode->didExecute(); - m_currentNode->insertNode(returningNode.release()); + RefPtr calleeNode = ProfileNode::create(callerCallFrame, callIdentifier, m_currentNode.get()); + beginCallEntry(calleeNode.get(), m_currentNode->lastCall().startTime()); + endCallEntry(calleeNode.get()); + m_currentNode->spliceNode(calleeNode.release()); return; } - m_currentNode = m_currentNode->didExecute(); + endCallEntry(m_currentNode.get()); + m_currentNode = m_currentNode->parent(); } void ProfileGenerator::exceptionUnwind(ExecState* handlerCallFrame, const CallIdentifier&) { + if (m_suspended) + return; + // If the current node was called by the handler (==) or any // more nested function (>) the we have exited early from it. ASSERT(m_currentNode); @@ -157,7 +209,8 @@ void ProfileGenerator::exceptionUnwind(ExecState* handlerCallFrame, const CallId void ProfileGenerator::stopProfiling() { - m_profile->forEach(&ProfileNode::stopProfiling); + for (ProfileNode* node = m_currentNode.get(); node != m_profile->rootNode(); node = node->parent()) + endCallEntry(node); if (m_foundConsoleStartParent) { removeProfileStart(); @@ -169,41 +222,31 @@ void ProfileGenerator::stopProfiling() // Set the current node to the parent, because we are in a call that // will not get didExecute call. m_currentNode = m_currentNode->parent(); - - if (double headSelfTime = m_head->selfTime()) { - m_head->setSelfTime(0.0); - m_profile->setIdleTime(headSelfTime); - } } // The console.profile that started this ProfileGenerator will be the first child. void ProfileGenerator::removeProfileStart() { - ProfileNode* currentNode = 0; - for (ProfileNode* next = m_head.get(); next; next = next->firstChild()) + ProfileNode* currentNode = nullptr; + for (ProfileNode* next = m_rootNode.get(); next; next = next->firstChild()) currentNode = next; if (currentNode->callIdentifier().functionName() != "profile") return; - // Attribute the time of the node aobut to be removed to the self time of its parent - currentNode->parent()->setSelfTime(currentNode->parent()->selfTime() + currentNode->totalTime()); currentNode->parent()->removeChild(currentNode); } // The console.profileEnd that stopped this ProfileGenerator will be the last child. void ProfileGenerator::removeProfileEnd() { - ProfileNode* currentNode = 0; - for (ProfileNode* next = m_head.get(); next; next = next->lastChild()) + ProfileNode* currentNode = nullptr; + for (ProfileNode* next = m_rootNode.get(); next; next = next->lastChild()) currentNode = next; if (currentNode->callIdentifier().functionName() != "profileEnd") return; - // Attribute the time of the node aobut to be removed to the self time of its parent - currentNode->parent()->setSelfTime(currentNode->parent()->selfTime() + currentNode->totalTime()); - ASSERT(currentNode->callIdentifier() == (currentNode->parent()->children()[currentNode->parent()->children().size() - 1])->callIdentifier()); currentNode->parent()->removeChild(currentNode); }