]> git.saurik.com Git - apple/javascriptcore.git/blame - profiler/ProfileGenerator.cpp
JavaScriptCore-1218.tar.gz
[apple/javascriptcore.git] / profiler / ProfileGenerator.cpp
CommitLineData
9dae56ea
A
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 * 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"
34#include "Interpreter.h"
93a37866
A
35#include "LegacyProfiler.h"
36#include "Operations.h"
9dae56ea 37#include "Profile.h"
9dae56ea
A
38#include "Tracing.h"
39
40namespace JSC {
41
42static const char* NonJSExecution = "(idle)";
43
93a37866 44PassRefPtr<ProfileGenerator> ProfileGenerator::create(ExecState* exec, const String& title, unsigned uid)
9dae56ea 45{
14957cd0 46 return adoptRef(new ProfileGenerator(exec, title, uid));
9dae56ea
A
47}
48
93a37866 49ProfileGenerator::ProfileGenerator(ExecState* exec, const String& title, unsigned uid)
14957cd0
A
50 : m_origin(exec ? exec->lexicalGlobalObject() : 0)
51 , m_profileGroup(exec ? exec->lexicalGlobalObject()->profileGroup() : 0)
9dae56ea
A
52{
53 m_profile = Profile::create(title, uid);
54 m_currentNode = m_head = m_profile->head();
14957cd0
A
55 if (exec)
56 addParentForConsoleStart(exec);
9dae56ea
A
57}
58
59void ProfileGenerator::addParentForConsoleStart(ExecState* exec)
60{
61 int lineNumber;
62 intptr_t sourceID;
93a37866 63 String sourceURL;
ba379fdc 64 JSValue function;
9dae56ea
A
65
66 exec->interpreter()->retrieveLastCaller(exec, lineNumber, sourceID, sourceURL, function);
93a37866 67 m_currentNode = ProfileNode::create(exec, LegacyProfiler::createCallIdentifier(exec, function ? function.toThisObject(exec) : 0, sourceURL, lineNumber), m_head.get(), m_head.get());
9dae56ea
A
68 m_head->insertNode(m_currentNode.get());
69}
70
93a37866 71const String& ProfileGenerator::title() const
9dae56ea
A
72{
73 return m_profile->title();
74}
75
14957cd0 76void ProfileGenerator::willExecute(ExecState* callerCallFrame, const CallIdentifier& callIdentifier)
9dae56ea
A
77{
78 if (JAVASCRIPTCORE_PROFILE_WILL_EXECUTE_ENABLED()) {
14957cd0
A
79 CString name = callIdentifier.m_name.utf8();
80 CString url = callIdentifier.m_url.utf8();
4e4e5a6f 81 JAVASCRIPTCORE_PROFILE_WILL_EXECUTE(m_profileGroup, const_cast<char*>(name.data()), const_cast<char*>(url.data()), callIdentifier.m_lineNumber);
9dae56ea
A
82 }
83
14957cd0 84 if (!m_origin)
9dae56ea
A
85 return;
86
14957cd0
A
87 ASSERT(m_currentNode);
88 m_currentNode = m_currentNode->willExecute(callerCallFrame, callIdentifier);
9dae56ea
A
89}
90
14957cd0 91void ProfileGenerator::didExecute(ExecState* callerCallFrame, const CallIdentifier& callIdentifier)
9dae56ea
A
92{
93 if (JAVASCRIPTCORE_PROFILE_DID_EXECUTE_ENABLED()) {
14957cd0
A
94 CString name = callIdentifier.m_name.utf8();
95 CString url = callIdentifier.m_url.utf8();
4e4e5a6f 96 JAVASCRIPTCORE_PROFILE_DID_EXECUTE(m_profileGroup, const_cast<char*>(name.data()), const_cast<char*>(url.data()), callIdentifier.m_lineNumber);
9dae56ea
A
97 }
98
14957cd0 99 if (!m_origin)
9dae56ea
A
100 return;
101
14957cd0 102 ASSERT(m_currentNode);
9dae56ea 103 if (m_currentNode->callIdentifier() != callIdentifier) {
14957cd0 104 RefPtr<ProfileNode> returningNode = ProfileNode::create(callerCallFrame, callIdentifier, m_head.get(), m_currentNode.get());
9dae56ea
A
105 returningNode->setStartTime(m_currentNode->startTime());
106 returningNode->didExecute();
107 m_currentNode->insertNode(returningNode.release());
108 return;
109 }
110
111 m_currentNode = m_currentNode->didExecute();
112}
113
14957cd0
A
114void ProfileGenerator::exceptionUnwind(ExecState* handlerCallFrame, const CallIdentifier&)
115{
116 // If the current node was called by the handler (==) or any
117 // more nested function (>) the we have exited early from it.
118 ASSERT(m_currentNode);
119 while (m_currentNode->callerCallFrame() >= handlerCallFrame) {
120 didExecute(m_currentNode->callerCallFrame(), m_currentNode->callIdentifier());
121 ASSERT(m_currentNode);
122 }
123}
124
9dae56ea
A
125void ProfileGenerator::stopProfiling()
126{
127 m_profile->forEach(&ProfileNode::stopProfiling);
128
129 removeProfileStart();
130 removeProfileEnd();
131
14957cd0 132 ASSERT(m_currentNode);
9dae56ea
A
133
134 // Set the current node to the parent, because we are in a call that
135 // will not get didExecute call.
136 m_currentNode = m_currentNode->parent();
137
138 if (double headSelfTime = m_head->selfTime()) {
93a37866 139 RefPtr<ProfileNode> idleNode = ProfileNode::create(0, CallIdentifier(NonJSExecution, String(), 0), m_head.get(), m_head.get());
9dae56ea
A
140
141 idleNode->setTotalTime(headSelfTime);
142 idleNode->setSelfTime(headSelfTime);
143 idleNode->setVisible(true);
144
145 m_head->setSelfTime(0.0);
146 m_head->addChild(idleNode.release());
147 }
148}
149
150// The console.ProfileGenerator that started this ProfileGenerator will be the first child.
151void ProfileGenerator::removeProfileStart()
152{
153 ProfileNode* currentNode = 0;
154 for (ProfileNode* next = m_head.get(); next; next = next->firstChild())
155 currentNode = next;
156
157 if (currentNode->callIdentifier().m_name != "profile")
158 return;
159
160 // Attribute the time of the node aobut to be removed to the self time of its parent
161 currentNode->parent()->setSelfTime(currentNode->parent()->selfTime() + currentNode->totalTime());
162 currentNode->parent()->removeChild(currentNode);
163}
164
165// The console.ProfileGeneratorEnd that stopped this ProfileGenerator will be the last child.
166void ProfileGenerator::removeProfileEnd()
167{
168 ProfileNode* currentNode = 0;
169 for (ProfileNode* next = m_head.get(); next; next = next->lastChild())
170 currentNode = next;
171
172 if (currentNode->callIdentifier().m_name != "profileEnd")
173 return;
174
175 // Attribute the time of the node aobut to be removed to the self time of its parent
176 currentNode->parent()->setSelfTime(currentNode->parent()->selfTime() + currentNode->totalTime());
177
178 ASSERT(currentNode->callIdentifier() == (currentNode->parent()->children()[currentNode->parent()->children().size() - 1])->callIdentifier());
179 currentNode->parent()->removeChild(currentNode);
180}
181
182} // namespace JSC