]>
git.saurik.com Git - apple/javascriptcore.git/blob - profiler/ProfileGenerator.cpp
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "ProfileGenerator.h"
29 #include "CallFrame.h"
30 #include "CodeBlock.h"
31 #include "JSGlobalObject.h"
32 #include "JSStringRef.h"
33 #include "JSFunction.h"
34 #include "Interpreter.h"
41 static const char* NonJSExecution
= "(idle)";
43 PassRefPtr
<ProfileGenerator
> ProfileGenerator::create(const UString
& title
, ExecState
* originatingExec
, unsigned uid
)
45 return adoptRef(new ProfileGenerator(title
, originatingExec
, uid
));
48 ProfileGenerator::ProfileGenerator(const UString
& title
, ExecState
* originatingExec
, unsigned uid
)
49 : m_originatingGlobalExec(originatingExec
? originatingExec
->lexicalGlobalObject()->globalExec() : 0)
50 , m_profileGroup(originatingExec
? originatingExec
->lexicalGlobalObject()->profileGroup() : 0)
52 m_profile
= Profile::create(title
, uid
);
53 m_currentNode
= m_head
= m_profile
->head();
55 addParentForConsoleStart(originatingExec
);
58 void ProfileGenerator::addParentForConsoleStart(ExecState
* exec
)
65 exec
->interpreter()->retrieveLastCaller(exec
, lineNumber
, sourceID
, sourceURL
, function
);
66 m_currentNode
= ProfileNode::create(Profiler::createCallIdentifier(exec
, function
? function
.toThisObject(exec
) : 0, sourceURL
, lineNumber
), m_head
.get(), m_head
.get());
67 m_head
->insertNode(m_currentNode
.get());
70 const UString
& ProfileGenerator::title() const
72 return m_profile
->title();
75 void ProfileGenerator::willExecute(const CallIdentifier
& callIdentifier
)
77 if (JAVASCRIPTCORE_PROFILE_WILL_EXECUTE_ENABLED()) {
78 CString name
= callIdentifier
.m_name
.UTF8String();
79 CString url
= callIdentifier
.m_url
.UTF8String();
80 JAVASCRIPTCORE_PROFILE_WILL_EXECUTE(m_profileGroup
, const_cast<char*>(name
.c_str()), const_cast<char*>(url
.c_str()), callIdentifier
.m_lineNumber
);
83 if (!m_originatingGlobalExec
)
86 ASSERT_ARG(m_currentNode
, m_currentNode
);
87 m_currentNode
= m_currentNode
->willExecute(callIdentifier
);
90 void ProfileGenerator::didExecute(const CallIdentifier
& callIdentifier
)
92 if (JAVASCRIPTCORE_PROFILE_DID_EXECUTE_ENABLED()) {
93 CString name
= callIdentifier
.m_name
.UTF8String();
94 CString url
= callIdentifier
.m_url
.UTF8String();
95 JAVASCRIPTCORE_PROFILE_DID_EXECUTE(m_profileGroup
, const_cast<char*>(name
.c_str()), const_cast<char*>(url
.c_str()), callIdentifier
.m_lineNumber
);
98 if (!m_originatingGlobalExec
)
101 ASSERT_ARG(m_currentNode
, m_currentNode
);
102 if (m_currentNode
->callIdentifier() != callIdentifier
) {
103 RefPtr
<ProfileNode
> returningNode
= ProfileNode::create(callIdentifier
, m_head
.get(), m_currentNode
.get());
104 returningNode
->setStartTime(m_currentNode
->startTime());
105 returningNode
->didExecute();
106 m_currentNode
->insertNode(returningNode
.release());
110 m_currentNode
= m_currentNode
->didExecute();
113 void ProfileGenerator::stopProfiling()
115 m_profile
->forEach(&ProfileNode::stopProfiling
);
117 removeProfileStart();
120 ASSERT_ARG(m_currentNode
, m_currentNode
);
122 // Set the current node to the parent, because we are in a call that
123 // will not get didExecute call.
124 m_currentNode
= m_currentNode
->parent();
126 if (double headSelfTime
= m_head
->selfTime()) {
127 RefPtr
<ProfileNode
> idleNode
= ProfileNode::create(CallIdentifier(NonJSExecution
, 0, 0), m_head
.get(), m_head
.get());
129 idleNode
->setTotalTime(headSelfTime
);
130 idleNode
->setSelfTime(headSelfTime
);
131 idleNode
->setVisible(true);
133 m_head
->setSelfTime(0.0);
134 m_head
->addChild(idleNode
.release());
138 // The console.ProfileGenerator that started this ProfileGenerator will be the first child.
139 void ProfileGenerator::removeProfileStart()
141 ProfileNode
* currentNode
= 0;
142 for (ProfileNode
* next
= m_head
.get(); next
; next
= next
->firstChild())
145 if (currentNode
->callIdentifier().m_name
!= "profile")
148 // Attribute the time of the node aobut to be removed to the self time of its parent
149 currentNode
->parent()->setSelfTime(currentNode
->parent()->selfTime() + currentNode
->totalTime());
150 currentNode
->parent()->removeChild(currentNode
);
153 // The console.ProfileGeneratorEnd that stopped this ProfileGenerator will be the last child.
154 void ProfileGenerator::removeProfileEnd()
156 ProfileNode
* currentNode
= 0;
157 for (ProfileNode
* next
= m_head
.get(); next
; next
= next
->lastChild())
160 if (currentNode
->callIdentifier().m_name
!= "profileEnd")
163 // Attribute the time of the node aobut to be removed to the self time of its parent
164 currentNode
->parent()->setSelfTime(currentNode
->parent()->selfTime() + currentNode
->totalTime());
166 ASSERT(currentNode
->callIdentifier() == (currentNode
->parent()->children()[currentNode
->parent()->children().size() - 1])->callIdentifier());
167 currentNode
->parent()->removeChild(currentNode
);