]> git.saurik.com Git - apple/javascriptcore.git/blame - profiler/ProfileGenerator.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / profiler / ProfileGenerator.cpp
CommitLineData
9dae56ea 1/*
81345200 2 * Copyright (C) 2008, 2014 Apple Inc. All Rights Reserved.
9dae56ea
A
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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "ProfileGenerator.h"
28
29#include "CallFrame.h"
f9bf01c6 30#include "CodeBlock.h"
9dae56ea
A
31#include "JSGlobalObject.h"
32#include "JSStringRef.h"
33#include "JSFunction.h"
93a37866 34#include "LegacyProfiler.h"
81345200 35#include "JSCInlines.h"
9dae56ea 36#include "Profile.h"
81345200 37#include "StackVisitor.h"
9dae56ea
A
38#include "Tracing.h"
39
40namespace JSC {
41
ed1e77d3 42Ref<ProfileGenerator> ProfileGenerator::create(ExecState* exec, const String& title, unsigned uid, PassRefPtr<Stopwatch> stopwatch)
9dae56ea 43{
ed1e77d3 44 return adoptRef(*new ProfileGenerator(exec, title, uid, stopwatch));
9dae56ea
A
45}
46
ed1e77d3
A
47ProfileGenerator::ProfileGenerator(ExecState* exec, const String& title, unsigned uid, PassRefPtr<Stopwatch> stopwatch)
48 : m_origin(exec ? exec->lexicalGlobalObject() : nullptr)
14957cd0 49 , m_profileGroup(exec ? exec->lexicalGlobalObject()->profileGroup() : 0)
ed1e77d3 50 , m_stopwatch(stopwatch)
81345200 51 , m_foundConsoleStartParent(false)
ed1e77d3 52 , m_suspended(false)
9dae56ea 53{
ed1e77d3
A
54 double startTime = m_stopwatch->elapsedTime();
55 m_profile = Profile::create(title, uid, startTime);
56 m_currentNode = m_rootNode = m_profile->rootNode();
14957cd0 57 if (exec)
ed1e77d3 58 addParentForConsoleStart(exec, startTime);
9dae56ea
A
59}
60
81345200
A
61class AddParentForConsoleStartFunctor {
62public:
ed1e77d3 63 AddParentForConsoleStartFunctor(ExecState* exec, RefPtr<ProfileNode>& rootNode, RefPtr<ProfileNode>& currentNode, double startTime)
81345200
A
64 : m_exec(exec)
65 , m_hasSkippedFirstFrame(false)
66 , m_foundParent(false)
ed1e77d3 67 , m_rootNode(rootNode)
81345200 68 , m_currentNode(currentNode)
ed1e77d3 69 , m_startTime(startTime)
81345200
A
70 {
71 }
72
73 bool foundParent() const { return m_foundParent; }
74
75 StackVisitor::Status operator()(StackVisitor& visitor)
76 {
77 if (!m_hasSkippedFirstFrame) {
78 m_hasSkippedFirstFrame = true;
79 return StackVisitor::Continue;
80 }
81
82 unsigned line = 0;
83 unsigned column = 0;
84 visitor->computeLineAndColumn(line, column);
ed1e77d3
A
85 m_currentNode = ProfileNode::create(m_exec, LegacyProfiler::createCallIdentifier(m_exec, visitor->callee(), visitor->sourceURL(), line, column), m_rootNode.get());
86 m_currentNode->appendCall(ProfileNode::Call(m_startTime));
87 m_rootNode->spliceNode(m_currentNode.get());
81345200
A
88
89 m_foundParent = true;
90 return StackVisitor::Done;
91 }
92
93private:
94 ExecState* m_exec;
95 bool m_hasSkippedFirstFrame;
ed1e77d3
A
96 bool m_foundParent;
97 RefPtr<ProfileNode>& m_rootNode;
81345200 98 RefPtr<ProfileNode>& m_currentNode;
ed1e77d3 99 double m_startTime;
81345200
A
100};
101
ed1e77d3 102void ProfileGenerator::addParentForConsoleStart(ExecState* exec, double startTime)
9dae56ea 103{
ed1e77d3 104 AddParentForConsoleStartFunctor functor(exec, m_rootNode, m_currentNode, startTime);
81345200
A
105 exec->iterate(functor);
106
107 m_foundConsoleStartParent = functor.foundParent();
9dae56ea
A
108}
109
93a37866 110const String& ProfileGenerator::title() const
9dae56ea
A
111{
112 return m_profile->title();
113}
114
ed1e77d3
A
115void ProfileGenerator::beginCallEntry(ProfileNode* node, double startTime)
116{
117 ASSERT_ARG(node, node);
118
119 if (std::isnan(startTime))
120 startTime = m_stopwatch->elapsedTime();
121
122 node->appendCall(ProfileNode::Call(startTime));
123}
124
125void ProfileGenerator::endCallEntry(ProfileNode* node)
126{
127 ASSERT_ARG(node, node);
128
129 ProfileNode::Call& last = node->lastCall();
130
131 double previousElapsedTime = std::isnan(last.elapsedTime()) ? 0.0 : last.elapsedTime();
132 double newlyElapsedTime = m_stopwatch->elapsedTime() - last.startTime();
133 last.setElapsedTime(previousElapsedTime + newlyElapsedTime);
134}
135
14957cd0 136void ProfileGenerator::willExecute(ExecState* callerCallFrame, const CallIdentifier& callIdentifier)
9dae56ea
A
137{
138 if (JAVASCRIPTCORE_PROFILE_WILL_EXECUTE_ENABLED()) {
81345200
A
139 CString name = callIdentifier.functionName().utf8();
140 CString url = callIdentifier.url().utf8();
ed1e77d3 141 JAVASCRIPTCORE_PROFILE_WILL_EXECUTE(m_profileGroup, const_cast<char*>(name.data()), const_cast<char*>(url.data()), callIdentifier.lineNumber(), callIdentifier.columnNumber());
9dae56ea
A
142 }
143
14957cd0 144 if (!m_origin)
9dae56ea
A
145 return;
146
ed1e77d3
A
147 if (m_suspended)
148 return;
149
150 RefPtr<ProfileNode> calleeNode = nullptr;
151
152 // Find or create a node for the callee call frame.
153 for (const RefPtr<ProfileNode>& child : m_currentNode->children()) {
154 if (child->callIdentifier() == callIdentifier)
155 calleeNode = child;
156 }
157
158 if (!calleeNode) {
159 calleeNode = ProfileNode::create(callerCallFrame, callIdentifier, m_currentNode.get());
160 m_currentNode->addChild(calleeNode);
161 }
162
163 m_currentNode = calleeNode;
164 beginCallEntry(calleeNode.get(), m_stopwatch->elapsedTime());
9dae56ea
A
165}
166
14957cd0 167void ProfileGenerator::didExecute(ExecState* callerCallFrame, const CallIdentifier& callIdentifier)
9dae56ea
A
168{
169 if (JAVASCRIPTCORE_PROFILE_DID_EXECUTE_ENABLED()) {
81345200
A
170 CString name = callIdentifier.functionName().utf8();
171 CString url = callIdentifier.url().utf8();
ed1e77d3 172 JAVASCRIPTCORE_PROFILE_DID_EXECUTE(m_profileGroup, const_cast<char*>(name.data()), const_cast<char*>(url.data()), callIdentifier.lineNumber(), callIdentifier.columnNumber());
9dae56ea
A
173 }
174
14957cd0 175 if (!m_origin)
9dae56ea
A
176 return;
177
ed1e77d3
A
178 if (m_suspended)
179 return;
180
181 // Make a new node if the caller node has never seen this callee call frame before.
182 // This can happen if |console.profile()| is called several frames deep in the call stack.
14957cd0 183 ASSERT(m_currentNode);
9dae56ea 184 if (m_currentNode->callIdentifier() != callIdentifier) {
ed1e77d3
A
185 RefPtr<ProfileNode> calleeNode = ProfileNode::create(callerCallFrame, callIdentifier, m_currentNode.get());
186 beginCallEntry(calleeNode.get(), m_currentNode->lastCall().startTime());
187 endCallEntry(calleeNode.get());
188 m_currentNode->spliceNode(calleeNode.release());
9dae56ea
A
189 return;
190 }
191
ed1e77d3
A
192 endCallEntry(m_currentNode.get());
193 m_currentNode = m_currentNode->parent();
9dae56ea
A
194}
195
14957cd0
A
196void ProfileGenerator::exceptionUnwind(ExecState* handlerCallFrame, const CallIdentifier&)
197{
ed1e77d3
A
198 if (m_suspended)
199 return;
200
14957cd0
A
201 // If the current node was called by the handler (==) or any
202 // more nested function (>) the we have exited early from it.
203 ASSERT(m_currentNode);
204 while (m_currentNode->callerCallFrame() >= handlerCallFrame) {
205 didExecute(m_currentNode->callerCallFrame(), m_currentNode->callIdentifier());
206 ASSERT(m_currentNode);
207 }
208}
209
9dae56ea
A
210void ProfileGenerator::stopProfiling()
211{
ed1e77d3
A
212 for (ProfileNode* node = m_currentNode.get(); node != m_profile->rootNode(); node = node->parent())
213 endCallEntry(node);
9dae56ea 214
81345200
A
215 if (m_foundConsoleStartParent) {
216 removeProfileStart();
217 removeProfileEnd();
218 }
9dae56ea 219
14957cd0 220 ASSERT(m_currentNode);
9dae56ea
A
221
222 // Set the current node to the parent, because we are in a call that
223 // will not get didExecute call.
224 m_currentNode = m_currentNode->parent();
9dae56ea
A
225}
226
81345200 227// The console.profile that started this ProfileGenerator will be the first child.
9dae56ea
A
228void ProfileGenerator::removeProfileStart()
229{
ed1e77d3
A
230 ProfileNode* currentNode = nullptr;
231 for (ProfileNode* next = m_rootNode.get(); next; next = next->firstChild())
9dae56ea
A
232 currentNode = next;
233
81345200 234 if (currentNode->callIdentifier().functionName() != "profile")
9dae56ea
A
235 return;
236
9dae56ea
A
237 currentNode->parent()->removeChild(currentNode);
238}
239
81345200 240// The console.profileEnd that stopped this ProfileGenerator will be the last child.
9dae56ea
A
241void ProfileGenerator::removeProfileEnd()
242{
ed1e77d3
A
243 ProfileNode* currentNode = nullptr;
244 for (ProfileNode* next = m_rootNode.get(); next; next = next->lastChild())
9dae56ea
A
245 currentNode = next;
246
81345200 247 if (currentNode->callIdentifier().functionName() != "profileEnd")
9dae56ea
A
248 return;
249
9dae56ea
A
250 ASSERT(currentNode->callIdentifier() == (currentNode->parent()->children()[currentNode->parent()->children().size() - 1])->callIdentifier());
251 currentNode->parent()->removeChild(currentNode);
252}
253
254} // namespace JSC