source: webkit/trunk/JavaScriptCore/profiler/Profiler.cpp@ 72351

Last change on this file since 72351 was 72351, checked in by [email protected], 15 years ago

Bug 49635 - Profiler implementation is fragile

Reviewed by Oliver Hunt.

JavaScriptCore:

The profile presently requires the exception handling mechanism to explicitly
remove all stack frames that are exited during the exception unwind mechanism.
This is fragile in a number of ways:

  • We have to change bytecode register allocation when compiling code to run when profiling, to preserve the callee function (this is also required to call did_call after the call has returned).
  • In the JIT we have to maintain additional data structures (CodeBlock::RareData::m_functionRegisterInfos) to map back to the register containing the callee.
  • In the interpreter we use 'magic values' to offset into the instruction stream to rediscover the register containing the function.

Instead, move profiling into the head and tail of functions.

  • This correctly accounts the cost of the call itself to the caller.
  • This allows us to access the callee function object from the callframe.
  • This means that at the point a call is made we can track the stack depth on the ProfileNode.
  • When unwinding we can simply report the depth at which the exception is being handled - all call frames above this level are freed.
  • bytecode/CodeBlock.cpp:

(JSC::CodeBlock::shrinkToFit):

  • bytecode/CodeBlock.h:

(JSC::CodeBlock::bytecodeOffset):
(JSC::CodeBlock::methodCallLinkInfo):

  • bytecompiler/BytecodeGenerator.cpp:

(JSC::BytecodeGenerator::emitCall):
(JSC::BytecodeGenerator::emitCallVarargs):

  • interpreter/Interpreter.cpp:

(JSC::Interpreter::unwindCallFrame):
(JSC::Interpreter::throwException):
(JSC::Interpreter::execute):
(JSC::Interpreter::executeCall):
(JSC::Interpreter::executeConstruct):
(JSC::Interpreter::privateExecute):

  • jit/JITStubs.cpp:

(JSC::DEFINE_STUB_FUNCTION):

  • profiler/Profile.cpp:

(JSC::Profile::Profile):

  • profiler/ProfileGenerator.cpp:

(JSC::ProfileGenerator::addParentForConsoleStart):
(JSC::ProfileGenerator::willExecute):
(JSC::ProfileGenerator::didExecute):
(JSC::ProfileGenerator::exceptionUnwind):
(JSC::ProfileGenerator::stopProfiling):

  • profiler/ProfileGenerator.h:
  • profiler/ProfileNode.cpp:

(JSC::ProfileNode::ProfileNode):
(JSC::ProfileNode::willExecute):

  • profiler/ProfileNode.h:

(JSC::ProfileNode::create):
(JSC::ProfileNode::callerCallFrame):

  • profiler/Profiler.cpp:

(JSC::dispatchFunctionToProfiles):
(JSC::Profiler::_willExecute):
(JSC::Profiler::_didExecute):
(JSC::Profiler::exceptionUnwind):

  • profiler/Profiler.h:

LayoutTests:

Fixes previously failing tests - output was incorrect, showing duplicate entries
for '(program) (no file) (line 1)'.

  • fast/profiler/throw-exception-from-eval-expected.txt:
File size: 7.7 KB
Line 
1/*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "Profiler.h"
31
32#include "CommonIdentifiers.h"
33#include "CallFrame.h"
34#include "CodeBlock.h"
35#include "InternalFunction.h"
36#include "JSFunction.h"
37#include "JSGlobalObject.h"
38#include "Nodes.h"
39#include "Profile.h"
40#include "ProfileGenerator.h"
41#include "ProfileNode.h"
42#include "UStringConcatenate.h"
43#include <stdio.h>
44
45namespace JSC {
46
47static const char* GlobalCodeExecution = "(program)";
48static const char* AnonymousFunction = "(anonymous function)";
49static unsigned ProfilesUID = 0;
50
51static CallIdentifier createCallIdentifierFromFunctionImp(ExecState*, JSFunction*);
52
53Profiler* Profiler::s_sharedProfiler = 0;
54Profiler* Profiler::s_sharedEnabledProfilerReference = 0;
55
56Profiler* Profiler::profiler()
57{
58 if (!s_sharedProfiler)
59 s_sharedProfiler = new Profiler();
60 return s_sharedProfiler;
61}
62
63void Profiler::startProfiling(ExecState* exec, const UString& title)
64{
65 ASSERT_ARG(title, !title.isNull());
66
67 // Check if we currently have a Profile for this global ExecState and title.
68 // If so return early and don't create a new Profile.
69 ExecState* globalExec = exec ? exec->lexicalGlobalObject()->globalExec() : 0;
70
71 for (size_t i = 0; i < m_currentProfiles.size(); ++i) {
72 ProfileGenerator* profileGenerator = m_currentProfiles[i].get();
73 if (profileGenerator->originatingGlobalExec() == globalExec && profileGenerator->title() == title)
74 return;
75 }
76
77 s_sharedEnabledProfilerReference = this;
78 RefPtr<ProfileGenerator> profileGenerator = ProfileGenerator::create(title, exec, ++ProfilesUID);
79 m_currentProfiles.append(profileGenerator);
80}
81
82PassRefPtr<Profile> Profiler::stopProfiling(ExecState* exec, const UString& title)
83{
84 ExecState* globalExec = exec ? exec->lexicalGlobalObject()->globalExec() : 0;
85 for (ptrdiff_t i = m_currentProfiles.size() - 1; i >= 0; --i) {
86 ProfileGenerator* profileGenerator = m_currentProfiles[i].get();
87 if (profileGenerator->originatingGlobalExec() == globalExec && (title.isNull() || profileGenerator->title() == title)) {
88 profileGenerator->stopProfiling();
89 RefPtr<Profile> returnProfile = profileGenerator->profile();
90
91 m_currentProfiles.remove(i);
92 if (!m_currentProfiles.size())
93 s_sharedEnabledProfilerReference = 0;
94
95 return returnProfile;
96 }
97 }
98
99 return 0;
100}
101
102static inline void dispatchFunctionToProfiles(ExecState* callerOrHandlerCallFrame, const Vector<RefPtr<ProfileGenerator> >& profiles, ProfileGenerator::ProfileFunction function, const CallIdentifier& callIdentifier, unsigned currentProfileTargetGroup)
103{
104 for (size_t i = 0; i < profiles.size(); ++i) {
105 if (profiles[i]->profileGroup() == currentProfileTargetGroup || !profiles[i]->originatingGlobalExec())
106 (profiles[i].get()->*function)(callerOrHandlerCallFrame, callIdentifier);
107 }
108}
109
110void Profiler::willExecute(ExecState* callerCallFrame, JSValue function)
111{
112 ASSERT(!m_currentProfiles.isEmpty());
113
114 dispatchFunctionToProfiles(callerCallFrame, m_currentProfiles, &ProfileGenerator::willExecute, createCallIdentifier(callerCallFrame, function, "", 0), callerCallFrame->lexicalGlobalObject()->profileGroup());
115}
116
117void Profiler::willExecute(ExecState* callerCallFrame, const UString& sourceURL, int startingLineNumber)
118{
119 ASSERT(!m_currentProfiles.isEmpty());
120
121 CallIdentifier callIdentifier = createCallIdentifier(callerCallFrame, JSValue(), sourceURL, startingLineNumber);
122
123 dispatchFunctionToProfiles(callerCallFrame, m_currentProfiles, &ProfileGenerator::willExecute, callIdentifier, callerCallFrame->lexicalGlobalObject()->profileGroup());
124}
125
126void Profiler::didExecute(ExecState* callerCallFrame, JSValue function)
127{
128 ASSERT(!m_currentProfiles.isEmpty());
129
130 dispatchFunctionToProfiles(callerCallFrame, m_currentProfiles, &ProfileGenerator::didExecute, createCallIdentifier(callerCallFrame, function, "", 0), callerCallFrame->lexicalGlobalObject()->profileGroup());
131}
132
133void Profiler::didExecute(ExecState* callerCallFrame, const UString& sourceURL, int startingLineNumber)
134{
135 ASSERT(!m_currentProfiles.isEmpty());
136
137 dispatchFunctionToProfiles(callerCallFrame, m_currentProfiles, &ProfileGenerator::didExecute, createCallIdentifier(callerCallFrame, JSValue(), sourceURL, startingLineNumber), callerCallFrame->lexicalGlobalObject()->profileGroup());
138}
139
140void Profiler::exceptionUnwind(ExecState* handlerCallFrame)
141{
142 ASSERT(!m_currentProfiles.isEmpty());
143
144 dispatchFunctionToProfiles(handlerCallFrame, m_currentProfiles, &ProfileGenerator::exceptionUnwind, createCallIdentifier(handlerCallFrame, JSValue(), "", 0), handlerCallFrame->lexicalGlobalObject()->profileGroup());
145}
146
147CallIdentifier Profiler::createCallIdentifier(ExecState* exec, JSValue functionValue, const UString& defaultSourceURL, int defaultLineNumber)
148{
149 if (!functionValue)
150 return CallIdentifier(GlobalCodeExecution, defaultSourceURL, defaultLineNumber);
151 if (!functionValue.isObject())
152 return CallIdentifier("(unknown)", defaultSourceURL, defaultLineNumber);
153 if (asObject(functionValue)->inherits(&JSFunction::info)) {
154 JSFunction* function = asFunction(functionValue);
155 if (!function->executable()->isHostFunction())
156 return createCallIdentifierFromFunctionImp(exec, function);
157 }
158 if (asObject(functionValue)->inherits(&JSFunction::info))
159 return CallIdentifier(static_cast<JSFunction*>(asObject(functionValue))->name(exec), defaultSourceURL, defaultLineNumber);
160 if (asObject(functionValue)->inherits(&InternalFunction::info))
161 return CallIdentifier(static_cast<InternalFunction*>(asObject(functionValue))->name(exec), defaultSourceURL, defaultLineNumber);
162 return CallIdentifier(makeUString("(", asObject(functionValue)->className(), " object)"), defaultSourceURL, defaultLineNumber);
163}
164
165CallIdentifier createCallIdentifierFromFunctionImp(ExecState* exec, JSFunction* function)
166{
167 ASSERT(!function->isHostFunction());
168 const UString& name = function->calculatedDisplayName(exec);
169 return CallIdentifier(name.isEmpty() ? AnonymousFunction : name, function->jsExecutable()->sourceURL(), function->jsExecutable()->lineNo());
170}
171
172} // namespace JSC
Note: See TracBrowser for help on using the repository browser.