2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "core/inspector/InspectorProfilerAgent.h"
33 #include "bindings/v8/ScriptCallStackFactory.h"
34 #include "bindings/v8/ScriptProfiler.h"
35 #include "core/inspector/InjectedScript.h"
36 #include "core/inspector/InjectedScriptHost.h"
37 #include "core/inspector/InspectorOverlay.h"
38 #include "core/inspector/InspectorState.h"
39 #include "core/inspector/InstrumentingAgents.h"
40 #include "core/inspector/ScriptCallStack.h"
41 #include "core/inspector/ScriptProfile.h"
42 #include "core/frame/ConsoleTypes.h"
43 #include "wtf/CurrentTime.h"
44 #include "wtf/text/StringConcatenate.h"
48 namespace ProfilerAgentState {
49 static const char samplingInterval[] = "samplingInterval";
50 static const char userInitiatedProfiling[] = "userInitiatedProfiling";
51 static const char profilerEnabled[] = "profilerEnabled";
52 static const char nextProfileId[] = "nextProfileId";
55 static PassRefPtr<TypeBuilder::Profiler::CPUProfile> createCPUProfile(const ScriptProfile& scriptProfile)
57 RefPtr<TypeBuilder::Profiler::CPUProfile> profile = TypeBuilder::Profiler::CPUProfile::create()
58 .setHead(scriptProfile.buildInspectorObjectForHead())
59 .setStartTime(scriptProfile.startTime())
60 .setEndTime(scriptProfile.endTime());
61 profile->setSamples(scriptProfile.buildInspectorObjectForSamples());
62 profile->setTimestamps(scriptProfile.buildInspectorObjectForTimestamps());
63 return profile.release();
66 static PassRefPtr<TypeBuilder::Debugger::Location> currentDebugLocation(ScriptState* scriptState)
68 RefPtrWillBeRawPtr<ScriptCallStack> callStack(createScriptCallStackForConsole(scriptState, 1));
69 const ScriptCallFrame& lastCaller = callStack->at(0);
70 RefPtr<TypeBuilder::Debugger::Location> location = TypeBuilder::Debugger::Location::create()
71 .setScriptId(lastCaller.scriptId())
72 .setLineNumber(lastCaller.lineNumber());
73 location->setColumnNumber(lastCaller.columnNumber());
74 return location.release();
77 class InspectorProfilerAgent::ProfileDescriptor {
79 ProfileDescriptor(const String& id, const String& title)
86 PassOwnPtr<InspectorProfilerAgent> InspectorProfilerAgent::create(InjectedScriptManager* injectedScriptManager, InspectorOverlay* overlay)
88 return adoptPtr(new InspectorProfilerAgent(injectedScriptManager, overlay));
91 InspectorProfilerAgent::InspectorProfilerAgent(InjectedScriptManager* injectedScriptManager, InspectorOverlay* overlay)
92 : InspectorBaseAgent<InspectorProfilerAgent>("Profiler")
93 , m_injectedScriptManager(injectedScriptManager)
95 , m_recordingCPUProfile(false)
96 , m_profileNameIdleTimeMap(ScriptProfiler::currentProfileNameIdleTimeMap())
97 , m_idleStartTime(0.0)
102 InspectorProfilerAgent::~InspectorProfilerAgent()
106 void InspectorProfilerAgent::consoleProfile(const String& title, ScriptState* scriptState)
108 ASSERT(m_frontend && enabled());
109 String id = nextProfileId();
110 m_startedProfiles.append(ProfileDescriptor(id, title));
111 ScriptProfiler::start(id);
112 m_frontend->consoleProfileStarted(id, currentDebugLocation(scriptState), title.isNull() ? 0 : &title);
115 void InspectorProfilerAgent::consoleProfileEnd(const String& title, ScriptState* scriptState)
117 ASSERT(m_frontend && enabled());
119 String resolvedTitle;
120 // Take last started profile if no title was passed.
121 if (title.isNull()) {
122 if (m_startedProfiles.isEmpty())
124 id = m_startedProfiles.last().m_id;
125 resolvedTitle = m_startedProfiles.last().m_title;
126 m_startedProfiles.removeLast();
128 for (size_t i = 0; i < m_startedProfiles.size(); i++) {
129 if (m_startedProfiles[i].m_title == title) {
130 resolvedTitle = title;
131 id = m_startedProfiles[i].m_id;
132 m_startedProfiles.remove(i);
139 RefPtrWillBeRawPtr<ScriptProfile> profile = ScriptProfiler::stop(id);
142 RefPtr<TypeBuilder::Debugger::Location> location = currentDebugLocation(scriptState);
143 m_frontend->consoleProfileFinished(id, location, createCPUProfile(*profile), resolvedTitle.isNull() ? 0 : &resolvedTitle);
146 void InspectorProfilerAgent::enable(ErrorString*)
148 m_state->setBoolean(ProfilerAgentState::profilerEnabled, true);
152 void InspectorProfilerAgent::doEnable()
154 m_instrumentingAgents->setInspectorProfilerAgent(this);
157 void InspectorProfilerAgent::disable(ErrorString*)
159 for (Vector<ProfileDescriptor>::reverse_iterator it = m_startedProfiles.rbegin(); it != m_startedProfiles.rend(); ++it)
160 ScriptProfiler::stop(it->m_id);
161 m_startedProfiles.clear();
164 m_instrumentingAgents->setInspectorProfilerAgent(0);
165 m_state->setBoolean(ProfilerAgentState::profilerEnabled, false);
168 bool InspectorProfilerAgent::enabled()
170 return m_state->getBoolean(ProfilerAgentState::profilerEnabled);
173 void InspectorProfilerAgent::setSamplingInterval(ErrorString* error, int interval)
175 if (m_recordingCPUProfile) {
176 *error = "Cannot change sampling interval when profiling.";
179 m_state->setLong(ProfilerAgentState::samplingInterval, interval);
180 ScriptProfiler::setSamplingInterval(interval);
183 void InspectorProfilerAgent::setFrontend(InspectorFrontend* frontend)
185 m_frontend = frontend->profiler();
188 void InspectorProfilerAgent::clearFrontend()
193 m_injectedScriptManager->injectedScriptHost()->clearInspectedObjects();
196 void InspectorProfilerAgent::restore()
198 if (m_state->getBoolean(ProfilerAgentState::profilerEnabled))
200 if (long interval = m_state->getLong(ProfilerAgentState::samplingInterval, 0))
201 ScriptProfiler::setSamplingInterval(interval);
202 if (m_state->getBoolean(ProfilerAgentState::userInitiatedProfiling)) {
208 void InspectorProfilerAgent::start(ErrorString* error)
210 if (m_recordingCPUProfile)
213 *error = "Profiler is not enabled";
216 m_recordingCPUProfile = true;
218 m_overlay->startedRecordingProfile();
219 m_frontendInitiatedProfileId = nextProfileId();
220 ScriptProfiler::start(m_frontendInitiatedProfileId);
221 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, true);
224 void InspectorProfilerAgent::stop(ErrorString* errorString, RefPtr<TypeBuilder::Profiler::CPUProfile>& profile)
226 stop(errorString, &profile);
229 void InspectorProfilerAgent::stop(ErrorString* errorString, RefPtr<TypeBuilder::Profiler::CPUProfile>* profile)
231 if (!m_recordingCPUProfile) {
233 *errorString = "No recording profiles found";
236 m_recordingCPUProfile = false;
238 m_overlay->finishedRecordingProfile();
239 RefPtrWillBeRawPtr<ScriptProfile> scriptProfile = ScriptProfiler::stop(m_frontendInitiatedProfileId);
240 m_frontendInitiatedProfileId = String();
241 if (scriptProfile && profile)
242 *profile = createCPUProfile(*scriptProfile);
243 else if (errorString)
244 *errorString = "Profile wasn't found";
245 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, false);
248 String InspectorProfilerAgent::nextProfileId()
250 long nextId = m_state->getLong(ProfilerAgentState::nextProfileId, 1);
251 m_state->setLong(ProfilerAgentState::nextProfileId, nextId + 1);
252 return String::number(nextId);
255 void InspectorProfilerAgent::idleFinished()
257 if (!m_profileNameIdleTimeMap || !m_profileNameIdleTimeMap->size())
259 ScriptProfiler::setIdle(false);
260 if (!m_idleStartTime)
263 double idleTime = WTF::monotonicallyIncreasingTime() - m_idleStartTime;
264 m_idleStartTime = 0.0;
265 ProfileNameIdleTimeMap::iterator end = m_profileNameIdleTimeMap->end();
266 for (ProfileNameIdleTimeMap::iterator it = m_profileNameIdleTimeMap->begin(); it != end; ++it)
267 it->value += idleTime;
270 void InspectorProfilerAgent::idleStarted()
272 if (!m_profileNameIdleTimeMap || !m_profileNameIdleTimeMap->size())
274 m_idleStartTime = WTF::monotonicallyIncreasingTime();
275 ScriptProfiler::setIdle(true);
278 void InspectorProfilerAgent::willProcessTask()
283 void InspectorProfilerAgent::didProcessTask()
288 void InspectorProfilerAgent::willEnterNestedRunLoop()
293 void InspectorProfilerAgent::didLeaveNestedRunLoop()
298 } // namespace WebCore