~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/JavaScriptCore/profiler/Profiler.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

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