]>
Commit | Line | Data |
---|---|---|
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 | * | |
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" | |
30 | #include "Profiler.h" | |
31 | ||
32 | #include "CommonIdentifiers.h" | |
33 | #include "CallFrame.h" | |
34 | #include "JSFunction.h" | |
35 | #include "JSGlobalObject.h" | |
36 | #include "Profile.h" | |
37 | #include "ProfileGenerator.h" | |
38 | #include "ProfileNode.h" | |
39 | #include <stdio.h> | |
40 | ||
41 | namespace JSC { | |
42 | ||
43 | static const char* GlobalCodeExecution = "(program)"; | |
44 | static const char* AnonymousFunction = "(anonymous function)"; | |
45 | static unsigned ProfilesUID = 0; | |
46 | ||
47 | static CallIdentifier createCallIdentifierFromFunctionImp(JSGlobalData*, JSFunction*); | |
48 | ||
49 | Profiler* Profiler::s_sharedProfiler = 0; | |
50 | Profiler* Profiler::s_sharedEnabledProfilerReference = 0; | |
51 | ||
52 | Profiler* Profiler::profiler() | |
53 | { | |
54 | if (!s_sharedProfiler) | |
55 | s_sharedProfiler = new Profiler(); | |
56 | return s_sharedProfiler; | |
57 | } | |
58 | ||
59 | void Profiler::startProfiling(ExecState* exec, const UString& title) | |
60 | { | |
61 | // Check if we currently have a Profile for this global ExecState and title. | |
62 | // If so return early and don't create a new Profile. | |
63 | ExecState* globalExec = exec ? exec->lexicalGlobalObject()->globalExec() : 0; | |
64 | ||
65 | for (size_t i = 0; i < m_currentProfiles.size(); ++i) { | |
66 | ProfileGenerator* profileGenerator = m_currentProfiles[i].get(); | |
67 | if (profileGenerator->originatingGlobalExec() == globalExec && profileGenerator->title() == title) | |
68 | return; | |
69 | } | |
70 | ||
71 | s_sharedEnabledProfilerReference = this; | |
72 | RefPtr<ProfileGenerator> profileGenerator = ProfileGenerator::create(title, exec, ++ProfilesUID); | |
73 | m_currentProfiles.append(profileGenerator); | |
74 | } | |
75 | ||
76 | PassRefPtr<Profile> Profiler::stopProfiling(ExecState* exec, const UString& title) | |
77 | { | |
78 | ExecState* globalExec = exec ? exec->lexicalGlobalObject()->globalExec() : 0; | |
79 | for (ptrdiff_t i = m_currentProfiles.size() - 1; i >= 0; --i) { | |
80 | ProfileGenerator* profileGenerator = m_currentProfiles[i].get(); | |
81 | if (profileGenerator->originatingGlobalExec() == globalExec && (title.isNull() || profileGenerator->title() == title)) { | |
82 | profileGenerator->stopProfiling(); | |
83 | RefPtr<Profile> returnProfile = profileGenerator->profile(); | |
84 | ||
85 | m_currentProfiles.remove(i); | |
86 | if (!m_currentProfiles.size()) | |
87 | s_sharedEnabledProfilerReference = 0; | |
88 | ||
89 | return returnProfile; | |
90 | } | |
91 | } | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
96 | static inline void dispatchFunctionToProfiles(const Vector<RefPtr<ProfileGenerator> >& profiles, ProfileGenerator::ProfileFunction function, const CallIdentifier& callIdentifier, unsigned currentProfileTargetGroup) | |
97 | { | |
98 | for (size_t i = 0; i < profiles.size(); ++i) { | |
99 | if (profiles[i]->profileGroup() == currentProfileTargetGroup || !profiles[i]->originatingGlobalExec()) | |
100 | (profiles[i].get()->*function)(callIdentifier); | |
101 | } | |
102 | } | |
103 | ||
104 | void Profiler::willExecute(ExecState* exec, JSValuePtr function) | |
105 | { | |
106 | ASSERT(!m_currentProfiles.isEmpty()); | |
107 | ||
108 | dispatchFunctionToProfiles(m_currentProfiles, &ProfileGenerator::willExecute, createCallIdentifier(&exec->globalData(), function, "", 0), exec->lexicalGlobalObject()->profileGroup()); | |
109 | } | |
110 | ||
111 | void Profiler::willExecute(ExecState* exec, const UString& sourceURL, int startingLineNumber) | |
112 | { | |
113 | ASSERT(!m_currentProfiles.isEmpty()); | |
114 | ||
115 | CallIdentifier callIdentifier = createCallIdentifier(&exec->globalData(), noValue(), sourceURL, startingLineNumber); | |
116 | ||
117 | dispatchFunctionToProfiles(m_currentProfiles, &ProfileGenerator::willExecute, callIdentifier, exec->lexicalGlobalObject()->profileGroup()); | |
118 | } | |
119 | ||
120 | void Profiler::didExecute(ExecState* exec, JSValuePtr function) | |
121 | { | |
122 | ASSERT(!m_currentProfiles.isEmpty()); | |
123 | ||
124 | dispatchFunctionToProfiles(m_currentProfiles, &ProfileGenerator::didExecute, createCallIdentifier(&exec->globalData(), function, "", 0), exec->lexicalGlobalObject()->profileGroup()); | |
125 | } | |
126 | ||
127 | void Profiler::didExecute(ExecState* exec, const UString& sourceURL, int startingLineNumber) | |
128 | { | |
129 | ASSERT(!m_currentProfiles.isEmpty()); | |
130 | ||
131 | dispatchFunctionToProfiles(m_currentProfiles, &ProfileGenerator::didExecute, createCallIdentifier(&exec->globalData(), noValue(), sourceURL, startingLineNumber), exec->lexicalGlobalObject()->profileGroup()); | |
132 | } | |
133 | ||
134 | CallIdentifier Profiler::createCallIdentifier(JSGlobalData* globalData, JSValuePtr function, const UString& defaultSourceURL, int defaultLineNumber) | |
135 | { | |
136 | if (!function) | |
137 | return CallIdentifier(GlobalCodeExecution, defaultSourceURL, defaultLineNumber); | |
138 | if (!function.isObject()) | |
139 | return CallIdentifier("(unknown)", defaultSourceURL, defaultLineNumber); | |
140 | if (asObject(function)->inherits(&JSFunction::info)) | |
141 | return createCallIdentifierFromFunctionImp(globalData, asFunction(function)); | |
142 | if (asObject(function)->inherits(&InternalFunction::info)) | |
143 | return CallIdentifier(static_cast<InternalFunction*>(asObject(function))->name(globalData), defaultSourceURL, defaultLineNumber); | |
144 | return CallIdentifier("(" + asObject(function)->className() + " object)", defaultSourceURL, defaultLineNumber); | |
145 | } | |
146 | ||
147 | CallIdentifier createCallIdentifierFromFunctionImp(JSGlobalData* globalData, JSFunction* function) | |
148 | { | |
149 | const UString& name = function->name(globalData); | |
150 | return CallIdentifier(name.isEmpty() ? AnonymousFunction : name, function->body()->sourceURL(), function->body()->lineNo()); | |
151 | } | |
152 | ||
153 | } // namespace JSC |