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
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.
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.
32 #include "CommonIdentifiers.h"
33 #include "CallFrame.h"
34 #include "CodeBlock.h"
35 #include "InternalFunction.h"
36 #include "JSFunction.h"
37 #include "JSGlobalObject.h"
40 #include "ProfileGenerator.h"
41 #include "ProfileNode.h"
42 #include "UStringConcatenate.h"
47 static const char* GlobalCodeExecution
= "(program)";
48 static const char* AnonymousFunction
= "(anonymous function)";
49 static unsigned ProfilesUID
= 0;
51 static CallIdentifier
createCallIdentifierFromFunctionImp(ExecState
*, JSObject
*, const UString
& defaultSourceURL
, int defaultLineNumber
);
53 Profiler
* Profiler::s_sharedProfiler
= 0;
54 Profiler
* Profiler::s_sharedEnabledProfilerReference
= 0;
56 Profiler
* Profiler::profiler()
58 if (!s_sharedProfiler
)
59 s_sharedProfiler
= new Profiler();
60 return s_sharedProfiler
;
63 void Profiler::startProfiling(ExecState
* exec
, const UString
& title
)
65 ASSERT_ARG(title
, !title
.isNull());
67 // Check if we currently have a Profile for this global ExecState and title.
68 // If so return early and don't create a new Profile.
69 JSGlobalObject
* origin
= exec
? exec
->lexicalGlobalObject() : 0;
71 for (size_t i
= 0; i
< m_currentProfiles
.size(); ++i
) {
72 ProfileGenerator
* profileGenerator
= m_currentProfiles
[i
].get();
73 if (profileGenerator
->origin() == origin
&& profileGenerator
->title() == title
)
77 s_sharedEnabledProfilerReference
= this;
78 RefPtr
<ProfileGenerator
> profileGenerator
= ProfileGenerator::create(exec
, title
, ++ProfilesUID
);
79 m_currentProfiles
.append(profileGenerator
);
82 PassRefPtr
<Profile
> Profiler::stopProfiling(ExecState
* exec
, const UString
& title
)
84 JSGlobalObject
* origin
= exec
? exec
->lexicalGlobalObject() : 0;
85 for (ptrdiff_t i
= m_currentProfiles
.size() - 1; i
>= 0; --i
) {
86 ProfileGenerator
* profileGenerator
= m_currentProfiles
[i
].get();
87 if (profileGenerator
->origin() == origin
&& (title
.isNull() || profileGenerator
->title() == title
)) {
88 profileGenerator
->stopProfiling();
89 RefPtr
<Profile
> returnProfile
= profileGenerator
->profile();
91 m_currentProfiles
.remove(i
);
92 if (!m_currentProfiles
.size())
93 s_sharedEnabledProfilerReference
= 0;
102 void Profiler::stopProfiling(JSGlobalObject
* origin
)
104 for (ptrdiff_t i
= m_currentProfiles
.size() - 1; i
>= 0; --i
) {
105 ProfileGenerator
* profileGenerator
= m_currentProfiles
[i
].get();
106 if (profileGenerator
->origin() == origin
) {
107 profileGenerator
->stopProfiling();
108 m_currentProfiles
.remove(i
);
109 if (!m_currentProfiles
.size())
110 s_sharedEnabledProfilerReference
= 0;
115 static inline void dispatchFunctionToProfiles(ExecState
* callerOrHandlerCallFrame
, const Vector
<RefPtr
<ProfileGenerator
> >& profiles
, ProfileGenerator::ProfileFunction function
, const CallIdentifier
& callIdentifier
, unsigned currentProfileTargetGroup
)
117 for (size_t i
= 0; i
< profiles
.size(); ++i
) {
118 if (profiles
[i
]->profileGroup() == currentProfileTargetGroup
|| !profiles
[i
]->origin())
119 (profiles
[i
].get()->*function
)(callerOrHandlerCallFrame
, callIdentifier
);
123 void Profiler::willExecute(ExecState
* callerCallFrame
, JSValue function
)
125 ASSERT(!m_currentProfiles
.isEmpty());
127 dispatchFunctionToProfiles(callerCallFrame
, m_currentProfiles
, &ProfileGenerator::willExecute
, createCallIdentifier(callerCallFrame
, function
, "", 0), callerCallFrame
->lexicalGlobalObject()->profileGroup());
130 void Profiler::willExecute(ExecState
* callerCallFrame
, const UString
& sourceURL
, int startingLineNumber
)
132 ASSERT(!m_currentProfiles
.isEmpty());
134 CallIdentifier callIdentifier
= createCallIdentifier(callerCallFrame
, JSValue(), sourceURL
, startingLineNumber
);
136 dispatchFunctionToProfiles(callerCallFrame
, m_currentProfiles
, &ProfileGenerator::willExecute
, callIdentifier
, callerCallFrame
->lexicalGlobalObject()->profileGroup());
139 void Profiler::didExecute(ExecState
* callerCallFrame
, JSValue function
)
141 ASSERT(!m_currentProfiles
.isEmpty());
143 dispatchFunctionToProfiles(callerCallFrame
, m_currentProfiles
, &ProfileGenerator::didExecute
, createCallIdentifier(callerCallFrame
, function
, "", 0), callerCallFrame
->lexicalGlobalObject()->profileGroup());
146 void Profiler::didExecute(ExecState
* callerCallFrame
, const UString
& sourceURL
, int startingLineNumber
)
148 ASSERT(!m_currentProfiles
.isEmpty());
150 dispatchFunctionToProfiles(callerCallFrame
, m_currentProfiles
, &ProfileGenerator::didExecute
, createCallIdentifier(callerCallFrame
, JSValue(), sourceURL
, startingLineNumber
), callerCallFrame
->lexicalGlobalObject()->profileGroup());
153 void Profiler::exceptionUnwind(ExecState
* handlerCallFrame
)
155 ASSERT(!m_currentProfiles
.isEmpty());
157 dispatchFunctionToProfiles(handlerCallFrame
, m_currentProfiles
, &ProfileGenerator::exceptionUnwind
, createCallIdentifier(handlerCallFrame
, JSValue(), "", 0), handlerCallFrame
->lexicalGlobalObject()->profileGroup());
160 CallIdentifier
Profiler::createCallIdentifier(ExecState
* exec
, JSValue functionValue
, const UString
& defaultSourceURL
, int defaultLineNumber
)
163 return CallIdentifier(GlobalCodeExecution
, defaultSourceURL
, defaultLineNumber
);
164 if (!functionValue
.isObject())
165 return CallIdentifier("(unknown)", defaultSourceURL
, defaultLineNumber
);
166 if (asObject(functionValue
)->inherits(&JSFunction::s_info
) || asObject(functionValue
)->inherits(&InternalFunction::s_info
))
167 return createCallIdentifierFromFunctionImp(exec
, asObject(functionValue
), defaultSourceURL
, defaultLineNumber
);
168 return CallIdentifier(makeUString("(", asObject(functionValue
)->methodTable()->className(asObject(functionValue
)), " object)"), defaultSourceURL
, defaultLineNumber
);
171 CallIdentifier
createCallIdentifierFromFunctionImp(ExecState
* exec
, JSObject
* function
, const UString
& defaultSourceURL
, int defaultLineNumber
)
173 const UString
& name
= getCalculatedDisplayName(exec
, function
);
174 JSFunction
* jsFunction
= jsDynamicCast
<JSFunction
*>(function
);
175 if (jsFunction
&& !jsFunction
->isHostFunction())
176 return CallIdentifier(name
.isEmpty() ? AnonymousFunction
: name
, jsFunction
->jsExecutable()->sourceURL(), jsFunction
->jsExecutable()->lineNo());
177 return CallIdentifier(name
.isEmpty() ? AnonymousFunction
: name
, defaultSourceURL
, defaultLineNumber
);