]>
Commit | Line | Data |
---|---|---|
9dae56ea | 1 | /* |
93a37866 | 2 | * Copyright (C) 2008, 2012 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 | * | |
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" | |
93a37866 | 30 | #include "LegacyProfiler.h" |
9dae56ea | 31 | |
9dae56ea | 32 | #include "CallFrame.h" |
f9bf01c6 | 33 | #include "CodeBlock.h" |
93a37866 | 34 | #include "CommonIdentifiers.h" |
14957cd0 | 35 | #include "InternalFunction.h" |
9dae56ea A |
36 | #include "JSFunction.h" |
37 | #include "JSGlobalObject.h" | |
ba379fdc | 38 | #include "Nodes.h" |
93a37866 | 39 | #include "Operations.h" |
9dae56ea A |
40 | #include "Profile.h" |
41 | #include "ProfileGenerator.h" | |
42 | #include "ProfileNode.h" | |
43 | #include <stdio.h> | |
44 | ||
45 | namespace JSC { | |
46 | ||
47 | static const char* GlobalCodeExecution = "(program)"; | |
48 | static const char* AnonymousFunction = "(anonymous function)"; | |
49 | static unsigned ProfilesUID = 0; | |
50 | ||
93a37866 | 51 | static CallIdentifier createCallIdentifierFromFunctionImp(ExecState*, JSObject*, const String& defaultSourceURL, int defaultLineNumber); |
9dae56ea | 52 | |
93a37866 | 53 | LegacyProfiler* LegacyProfiler::s_sharedLegacyProfiler = 0; |
9dae56ea | 54 | |
93a37866 | 55 | LegacyProfiler* LegacyProfiler::profiler() |
9dae56ea | 56 | { |
93a37866 A |
57 | if (!s_sharedLegacyProfiler) |
58 | s_sharedLegacyProfiler = new LegacyProfiler(); | |
59 | return s_sharedLegacyProfiler; | |
9dae56ea A |
60 | } |
61 | ||
93a37866 | 62 | void LegacyProfiler::startProfiling(ExecState* exec, const String& title) |
9dae56ea | 63 | { |
ba379fdc A |
64 | ASSERT_ARG(title, !title.isNull()); |
65 | ||
93a37866 A |
66 | if (!exec) |
67 | return; | |
68 | ||
9dae56ea A |
69 | // Check if we currently have a Profile for this global ExecState and title. |
70 | // If so return early and don't create a new Profile. | |
93a37866 | 71 | JSGlobalObject* origin = exec->lexicalGlobalObject(); |
9dae56ea A |
72 | |
73 | for (size_t i = 0; i < m_currentProfiles.size(); ++i) { | |
74 | ProfileGenerator* profileGenerator = m_currentProfiles[i].get(); | |
14957cd0 | 75 | if (profileGenerator->origin() == origin && profileGenerator->title() == title) |
9dae56ea A |
76 | return; |
77 | } | |
78 | ||
93a37866 | 79 | exec->vm().m_enabledProfiler = this; |
14957cd0 | 80 | RefPtr<ProfileGenerator> profileGenerator = ProfileGenerator::create(exec, title, ++ProfilesUID); |
9dae56ea A |
81 | m_currentProfiles.append(profileGenerator); |
82 | } | |
83 | ||
93a37866 | 84 | PassRefPtr<Profile> LegacyProfiler::stopProfiling(ExecState* exec, const String& title) |
9dae56ea | 85 | { |
93a37866 A |
86 | if (!exec) |
87 | return 0; | |
88 | ||
89 | JSGlobalObject* origin = exec->lexicalGlobalObject(); | |
9dae56ea A |
90 | for (ptrdiff_t i = m_currentProfiles.size() - 1; i >= 0; --i) { |
91 | ProfileGenerator* profileGenerator = m_currentProfiles[i].get(); | |
14957cd0 | 92 | if (profileGenerator->origin() == origin && (title.isNull() || profileGenerator->title() == title)) { |
9dae56ea A |
93 | profileGenerator->stopProfiling(); |
94 | RefPtr<Profile> returnProfile = profileGenerator->profile(); | |
95 | ||
96 | m_currentProfiles.remove(i); | |
97 | if (!m_currentProfiles.size()) | |
93a37866 | 98 | exec->vm().m_enabledProfiler = 0; |
9dae56ea A |
99 | |
100 | return returnProfile; | |
101 | } | |
102 | } | |
103 | ||
104 | return 0; | |
105 | } | |
106 | ||
93a37866 | 107 | void LegacyProfiler::stopProfiling(JSGlobalObject* origin) |
14957cd0 A |
108 | { |
109 | for (ptrdiff_t i = m_currentProfiles.size() - 1; i >= 0; --i) { | |
110 | ProfileGenerator* profileGenerator = m_currentProfiles[i].get(); | |
111 | if (profileGenerator->origin() == origin) { | |
112 | profileGenerator->stopProfiling(); | |
113 | m_currentProfiles.remove(i); | |
114 | if (!m_currentProfiles.size()) | |
93a37866 | 115 | origin->vm().m_enabledProfiler = 0; |
14957cd0 A |
116 | } |
117 | } | |
118 | } | |
119 | ||
120 | static inline void dispatchFunctionToProfiles(ExecState* callerOrHandlerCallFrame, const Vector<RefPtr<ProfileGenerator> >& profiles, ProfileGenerator::ProfileFunction function, const CallIdentifier& callIdentifier, unsigned currentProfileTargetGroup) | |
9dae56ea A |
121 | { |
122 | for (size_t i = 0; i < profiles.size(); ++i) { | |
14957cd0 A |
123 | if (profiles[i]->profileGroup() == currentProfileTargetGroup || !profiles[i]->origin()) |
124 | (profiles[i].get()->*function)(callerOrHandlerCallFrame, callIdentifier); | |
9dae56ea A |
125 | } |
126 | } | |
127 | ||
93a37866 | 128 | void LegacyProfiler::willExecute(ExecState* callerCallFrame, JSValue function) |
9dae56ea A |
129 | { |
130 | ASSERT(!m_currentProfiles.isEmpty()); | |
131 | ||
14957cd0 | 132 | dispatchFunctionToProfiles(callerCallFrame, m_currentProfiles, &ProfileGenerator::willExecute, createCallIdentifier(callerCallFrame, function, "", 0), callerCallFrame->lexicalGlobalObject()->profileGroup()); |
9dae56ea A |
133 | } |
134 | ||
93a37866 | 135 | void LegacyProfiler::willExecute(ExecState* callerCallFrame, const String& sourceURL, int startingLineNumber) |
9dae56ea A |
136 | { |
137 | ASSERT(!m_currentProfiles.isEmpty()); | |
138 | ||
14957cd0 A |
139 | CallIdentifier callIdentifier = createCallIdentifier(callerCallFrame, JSValue(), sourceURL, startingLineNumber); |
140 | ||
141 | dispatchFunctionToProfiles(callerCallFrame, m_currentProfiles, &ProfileGenerator::willExecute, callIdentifier, callerCallFrame->lexicalGlobalObject()->profileGroup()); | |
142 | } | |
143 | ||
93a37866 | 144 | void LegacyProfiler::didExecute(ExecState* callerCallFrame, JSValue function) |
14957cd0 A |
145 | { |
146 | ASSERT(!m_currentProfiles.isEmpty()); | |
9dae56ea | 147 | |
14957cd0 | 148 | dispatchFunctionToProfiles(callerCallFrame, m_currentProfiles, &ProfileGenerator::didExecute, createCallIdentifier(callerCallFrame, function, "", 0), callerCallFrame->lexicalGlobalObject()->profileGroup()); |
9dae56ea A |
149 | } |
150 | ||
93a37866 | 151 | void LegacyProfiler::didExecute(ExecState* callerCallFrame, const String& sourceURL, int startingLineNumber) |
9dae56ea A |
152 | { |
153 | ASSERT(!m_currentProfiles.isEmpty()); | |
154 | ||
14957cd0 | 155 | dispatchFunctionToProfiles(callerCallFrame, m_currentProfiles, &ProfileGenerator::didExecute, createCallIdentifier(callerCallFrame, JSValue(), sourceURL, startingLineNumber), callerCallFrame->lexicalGlobalObject()->profileGroup()); |
9dae56ea A |
156 | } |
157 | ||
93a37866 | 158 | void LegacyProfiler::exceptionUnwind(ExecState* handlerCallFrame) |
9dae56ea A |
159 | { |
160 | ASSERT(!m_currentProfiles.isEmpty()); | |
161 | ||
14957cd0 | 162 | dispatchFunctionToProfiles(handlerCallFrame, m_currentProfiles, &ProfileGenerator::exceptionUnwind, createCallIdentifier(handlerCallFrame, JSValue(), "", 0), handlerCallFrame->lexicalGlobalObject()->profileGroup()); |
9dae56ea A |
163 | } |
164 | ||
93a37866 | 165 | CallIdentifier LegacyProfiler::createCallIdentifier(ExecState* exec, JSValue functionValue, const String& defaultSourceURL, int defaultLineNumber) |
9dae56ea | 166 | { |
f9bf01c6 | 167 | if (!functionValue) |
9dae56ea | 168 | return CallIdentifier(GlobalCodeExecution, defaultSourceURL, defaultLineNumber); |
f9bf01c6 | 169 | if (!functionValue.isObject()) |
9dae56ea | 170 | return CallIdentifier("(unknown)", defaultSourceURL, defaultLineNumber); |
6fe7ccc8 A |
171 | if (asObject(functionValue)->inherits(&JSFunction::s_info) || asObject(functionValue)->inherits(&InternalFunction::s_info)) |
172 | return createCallIdentifierFromFunctionImp(exec, asObject(functionValue), defaultSourceURL, defaultLineNumber); | |
93a37866 | 173 | return CallIdentifier(makeString("(", asObject(functionValue)->methodTable()->className(asObject(functionValue)), " object)"), defaultSourceURL, defaultLineNumber); |
9dae56ea A |
174 | } |
175 | ||
93a37866 | 176 | CallIdentifier createCallIdentifierFromFunctionImp(ExecState* exec, JSObject* function, const String& defaultSourceURL, int defaultLineNumber) |
9dae56ea | 177 | { |
93a37866 | 178 | const String& name = getCalculatedDisplayName(exec, function); |
6fe7ccc8 A |
179 | JSFunction* jsFunction = jsDynamicCast<JSFunction*>(function); |
180 | if (jsFunction && !jsFunction->isHostFunction()) | |
181 | return CallIdentifier(name.isEmpty() ? AnonymousFunction : name, jsFunction->jsExecutable()->sourceURL(), jsFunction->jsExecutable()->lineNo()); | |
182 | return CallIdentifier(name.isEmpty() ? AnonymousFunction : name, defaultSourceURL, defaultLineNumber); | |
9dae56ea A |
183 | } |
184 | ||
185 | } // namespace JSC |