X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/f9bf01c6616d5ddcf65b13b33cedf9e387ff7a63..81345200c95645a1b0d2635520f96ad55dfde63f:/profiler/ProfileGenerator.cpp diff --git a/profiler/ProfileGenerator.cpp b/profiler/ProfileGenerator.cpp index 17d37d7..d37b356 100644 --- a/profiler/ProfileGenerator.cpp +++ b/profiler/ProfileGenerator.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Apple Inc. All Rights Reserved. + * Copyright (C) 2008, 2014 Apple Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -31,77 +31,111 @@ #include "JSGlobalObject.h" #include "JSStringRef.h" #include "JSFunction.h" -#include "Interpreter.h" +#include "LegacyProfiler.h" +#include "JSCInlines.h" #include "Profile.h" -#include "Profiler.h" +#include "StackVisitor.h" #include "Tracing.h" namespace JSC { -static const char* NonJSExecution = "(idle)"; - -PassRefPtr ProfileGenerator::create(const UString& title, ExecState* originatingExec, unsigned uid) +PassRefPtr ProfileGenerator::create(ExecState* exec, const String& title, unsigned uid) { - return adoptRef(new ProfileGenerator(title, originatingExec, uid)); + return adoptRef(new ProfileGenerator(exec, title, uid)); } -ProfileGenerator::ProfileGenerator(const UString& title, ExecState* originatingExec, unsigned uid) - : m_originatingGlobalExec(originatingExec ? originatingExec->lexicalGlobalObject()->globalExec() : 0) - , m_profileGroup(originatingExec ? originatingExec->lexicalGlobalObject()->profileGroup() : 0) +ProfileGenerator::ProfileGenerator(ExecState* exec, const String& title, unsigned uid) + : m_origin(exec ? exec->lexicalGlobalObject() : 0) + , m_profileGroup(exec ? exec->lexicalGlobalObject()->profileGroup() : 0) + , m_foundConsoleStartParent(false) { m_profile = Profile::create(title, uid); m_currentNode = m_head = m_profile->head(); - if (originatingExec) - addParentForConsoleStart(originatingExec); + if (exec) + addParentForConsoleStart(exec); } +class AddParentForConsoleStartFunctor { +public: + AddParentForConsoleStartFunctor(ExecState* exec, RefPtr& head, RefPtr& currentNode) + : m_exec(exec) + , m_hasSkippedFirstFrame(false) + , m_foundParent(false) + , m_head(head) + , m_currentNode(currentNode) + { + } + + bool foundParent() const { return m_foundParent; } + + StackVisitor::Status operator()(StackVisitor& visitor) + { + if (!m_hasSkippedFirstFrame) { + m_hasSkippedFirstFrame = true; + return StackVisitor::Continue; + } + + 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_foundParent = true; + return StackVisitor::Done; + } + +private: + ExecState* m_exec; + bool m_hasSkippedFirstFrame; + bool m_foundParent; + RefPtr& m_head; + RefPtr& m_currentNode; +}; + void ProfileGenerator::addParentForConsoleStart(ExecState* exec) { - int lineNumber; - intptr_t sourceID; - UString sourceURL; - JSValue function; - - exec->interpreter()->retrieveLastCaller(exec, lineNumber, sourceID, sourceURL, function); - m_currentNode = ProfileNode::create(Profiler::createCallIdentifier(exec, function ? function.toThisObject(exec) : 0, sourceURL, lineNumber), m_head.get(), m_head.get()); - m_head->insertNode(m_currentNode.get()); + AddParentForConsoleStartFunctor functor(exec, m_head, m_currentNode); + exec->iterate(functor); + + m_foundConsoleStartParent = functor.foundParent(); } -const UString& ProfileGenerator::title() const +const String& ProfileGenerator::title() const { return m_profile->title(); } -void ProfileGenerator::willExecute(const CallIdentifier& callIdentifier) +void ProfileGenerator::willExecute(ExecState* callerCallFrame, const CallIdentifier& callIdentifier) { if (JAVASCRIPTCORE_PROFILE_WILL_EXECUTE_ENABLED()) { - CString name = callIdentifier.m_name.UTF8String(); - CString url = callIdentifier.m_url.UTF8String(); - JAVASCRIPTCORE_PROFILE_WILL_EXECUTE(m_profileGroup, const_cast(name.c_str()), const_cast(url.c_str()), callIdentifier.m_lineNumber); + 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()); } - if (!m_originatingGlobalExec) + if (!m_origin) return; - ASSERT_ARG(m_currentNode, m_currentNode); - m_currentNode = m_currentNode->willExecute(callIdentifier); + ASSERT(m_currentNode); + m_currentNode = m_currentNode->willExecute(callerCallFrame, callIdentifier); } -void ProfileGenerator::didExecute(const CallIdentifier& callIdentifier) +void ProfileGenerator::didExecute(ExecState* callerCallFrame, const CallIdentifier& callIdentifier) { if (JAVASCRIPTCORE_PROFILE_DID_EXECUTE_ENABLED()) { - CString name = callIdentifier.m_name.UTF8String(); - CString url = callIdentifier.m_url.UTF8String(); - JAVASCRIPTCORE_PROFILE_DID_EXECUTE(m_profileGroup, const_cast(name.c_str()), const_cast(url.c_str()), callIdentifier.m_lineNumber); + 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()); } - if (!m_originatingGlobalExec) + if (!m_origin) return; - ASSERT_ARG(m_currentNode, m_currentNode); + ASSERT(m_currentNode); if (m_currentNode->callIdentifier() != callIdentifier) { - RefPtr returningNode = ProfileNode::create(callIdentifier, m_head.get(), m_currentNode.get()); - returningNode->setStartTime(m_currentNode->startTime()); + 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()); return; @@ -110,39 +144,46 @@ void ProfileGenerator::didExecute(const CallIdentifier& callIdentifier) m_currentNode = m_currentNode->didExecute(); } +void ProfileGenerator::exceptionUnwind(ExecState* handlerCallFrame, const CallIdentifier&) +{ + // If the current node was called by the handler (==) or any + // more nested function (>) the we have exited early from it. + ASSERT(m_currentNode); + while (m_currentNode->callerCallFrame() >= handlerCallFrame) { + didExecute(m_currentNode->callerCallFrame(), m_currentNode->callIdentifier()); + ASSERT(m_currentNode); + } +} + void ProfileGenerator::stopProfiling() { m_profile->forEach(&ProfileNode::stopProfiling); - removeProfileStart(); - removeProfileEnd(); + if (m_foundConsoleStartParent) { + removeProfileStart(); + removeProfileEnd(); + } - ASSERT_ARG(m_currentNode, m_currentNode); + ASSERT(m_currentNode); // 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()) { - RefPtr idleNode = ProfileNode::create(CallIdentifier(NonJSExecution, 0, 0), m_head.get(), m_head.get()); - - idleNode->setTotalTime(headSelfTime); - idleNode->setSelfTime(headSelfTime); - idleNode->setVisible(true); - + if (double headSelfTime = m_head->selfTime()) { m_head->setSelfTime(0.0); - m_head->addChild(idleNode.release()); + m_profile->setIdleTime(headSelfTime); } } -// The console.ProfileGenerator that started this ProfileGenerator will be the first child. +// 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()) currentNode = next; - if (currentNode->callIdentifier().m_name != "profile") + if (currentNode->callIdentifier().functionName() != "profile") return; // Attribute the time of the node aobut to be removed to the self time of its parent @@ -150,14 +191,14 @@ void ProfileGenerator::removeProfileStart() currentNode->parent()->removeChild(currentNode); } -// The console.ProfileGeneratorEnd that stopped this ProfileGenerator will be the last child. +// 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()) currentNode = next; - if (currentNode->callIdentifier().m_name != "profileEnd") + if (currentNode->callIdentifier().functionName() != "profileEnd") return; // Attribute the time of the node aobut to be removed to the self time of its parent