]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2008, 2014 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 | * 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" | |
30 | #include "CodeBlock.h" | |
31 | #include "JSGlobalObject.h" | |
32 | #include "JSStringRef.h" | |
33 | #include "JSFunction.h" | |
34 | #include "LegacyProfiler.h" | |
35 | #include "JSCInlines.h" | |
36 | #include "Profile.h" | |
37 | #include "StackVisitor.h" | |
38 | #include "Tracing.h" | |
39 | ||
40 | namespace JSC { | |
41 | ||
42 | Ref<ProfileGenerator> ProfileGenerator::create(ExecState* exec, const String& title, unsigned uid, PassRefPtr<Stopwatch> stopwatch) | |
43 | { | |
44 | return adoptRef(*new ProfileGenerator(exec, title, uid, stopwatch)); | |
45 | } | |
46 | ||
47 | ProfileGenerator::ProfileGenerator(ExecState* exec, const String& title, unsigned uid, PassRefPtr<Stopwatch> stopwatch) | |
48 | : m_origin(exec ? exec->lexicalGlobalObject() : nullptr) | |
49 | , m_profileGroup(exec ? exec->lexicalGlobalObject()->profileGroup() : 0) | |
50 | , m_stopwatch(stopwatch) | |
51 | , m_foundConsoleStartParent(false) | |
52 | , m_suspended(false) | |
53 | { | |
54 | double startTime = m_stopwatch->elapsedTime(); | |
55 | m_profile = Profile::create(title, uid, startTime); | |
56 | m_currentNode = m_rootNode = m_profile->rootNode(); | |
57 | if (exec) | |
58 | addParentForConsoleStart(exec, startTime); | |
59 | } | |
60 | ||
61 | class AddParentForConsoleStartFunctor { | |
62 | public: | |
63 | AddParentForConsoleStartFunctor(ExecState* exec, RefPtr<ProfileNode>& rootNode, RefPtr<ProfileNode>& currentNode, double startTime) | |
64 | : m_exec(exec) | |
65 | , m_hasSkippedFirstFrame(false) | |
66 | , m_foundParent(false) | |
67 | , m_rootNode(rootNode) | |
68 | , m_currentNode(currentNode) | |
69 | , m_startTime(startTime) | |
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); | |
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()); | |
88 | ||
89 | m_foundParent = true; | |
90 | return StackVisitor::Done; | |
91 | } | |
92 | ||
93 | private: | |
94 | ExecState* m_exec; | |
95 | bool m_hasSkippedFirstFrame; | |
96 | bool m_foundParent; | |
97 | RefPtr<ProfileNode>& m_rootNode; | |
98 | RefPtr<ProfileNode>& m_currentNode; | |
99 | double m_startTime; | |
100 | }; | |
101 | ||
102 | void ProfileGenerator::addParentForConsoleStart(ExecState* exec, double startTime) | |
103 | { | |
104 | AddParentForConsoleStartFunctor functor(exec, m_rootNode, m_currentNode, startTime); | |
105 | exec->iterate(functor); | |
106 | ||
107 | m_foundConsoleStartParent = functor.foundParent(); | |
108 | } | |
109 | ||
110 | const String& ProfileGenerator::title() const | |
111 | { | |
112 | return m_profile->title(); | |
113 | } | |
114 | ||
115 | void 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 | ||
125 | void 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 | ||
136 | void ProfileGenerator::willExecute(ExecState* callerCallFrame, const CallIdentifier& callIdentifier) | |
137 | { | |
138 | if (JAVASCRIPTCORE_PROFILE_WILL_EXECUTE_ENABLED()) { | |
139 | CString name = callIdentifier.functionName().utf8(); | |
140 | CString url = callIdentifier.url().utf8(); | |
141 | JAVASCRIPTCORE_PROFILE_WILL_EXECUTE(m_profileGroup, const_cast<char*>(name.data()), const_cast<char*>(url.data()), callIdentifier.lineNumber(), callIdentifier.columnNumber()); | |
142 | } | |
143 | ||
144 | if (!m_origin) | |
145 | return; | |
146 | ||
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()); | |
165 | } | |
166 | ||
167 | void ProfileGenerator::didExecute(ExecState* callerCallFrame, const CallIdentifier& callIdentifier) | |
168 | { | |
169 | if (JAVASCRIPTCORE_PROFILE_DID_EXECUTE_ENABLED()) { | |
170 | CString name = callIdentifier.functionName().utf8(); | |
171 | CString url = callIdentifier.url().utf8(); | |
172 | JAVASCRIPTCORE_PROFILE_DID_EXECUTE(m_profileGroup, const_cast<char*>(name.data()), const_cast<char*>(url.data()), callIdentifier.lineNumber(), callIdentifier.columnNumber()); | |
173 | } | |
174 | ||
175 | if (!m_origin) | |
176 | return; | |
177 | ||
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. | |
183 | ASSERT(m_currentNode); | |
184 | if (m_currentNode->callIdentifier() != callIdentifier) { | |
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()); | |
189 | return; | |
190 | } | |
191 | ||
192 | endCallEntry(m_currentNode.get()); | |
193 | m_currentNode = m_currentNode->parent(); | |
194 | } | |
195 | ||
196 | void ProfileGenerator::exceptionUnwind(ExecState* handlerCallFrame, const CallIdentifier&) | |
197 | { | |
198 | if (m_suspended) | |
199 | return; | |
200 | ||
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 | ||
210 | void ProfileGenerator::stopProfiling() | |
211 | { | |
212 | for (ProfileNode* node = m_currentNode.get(); node != m_profile->rootNode(); node = node->parent()) | |
213 | endCallEntry(node); | |
214 | ||
215 | if (m_foundConsoleStartParent) { | |
216 | removeProfileStart(); | |
217 | removeProfileEnd(); | |
218 | } | |
219 | ||
220 | ASSERT(m_currentNode); | |
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(); | |
225 | } | |
226 | ||
227 | // The console.profile that started this ProfileGenerator will be the first child. | |
228 | void ProfileGenerator::removeProfileStart() | |
229 | { | |
230 | ProfileNode* currentNode = nullptr; | |
231 | for (ProfileNode* next = m_rootNode.get(); next; next = next->firstChild()) | |
232 | currentNode = next; | |
233 | ||
234 | if (currentNode->callIdentifier().functionName() != "profile") | |
235 | return; | |
236 | ||
237 | currentNode->parent()->removeChild(currentNode); | |
238 | } | |
239 | ||
240 | // The console.profileEnd that stopped this ProfileGenerator will be the last child. | |
241 | void ProfileGenerator::removeProfileEnd() | |
242 | { | |
243 | ProfileNode* currentNode = nullptr; | |
244 | for (ProfileNode* next = m_rootNode.get(); next; next = next->lastChild()) | |
245 | currentNode = next; | |
246 | ||
247 | if (currentNode->callIdentifier().functionName() != "profileEnd") | |
248 | return; | |
249 | ||
250 | ASSERT(currentNode->callIdentifier() == (currentNode->parent()->children()[currentNode->parent()->children().size() - 1])->callIdentifier()); | |
251 | currentNode->parent()->removeChild(currentNode); | |
252 | } | |
253 | ||
254 | } // namespace JSC |