2 * Copyright (C) 2013 Google 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/inspector/InspectorHeapProfilerAgent.h"
34 #include "bindings/v8/ScriptProfiler.h"
35 #include "core/inspector/InjectedScript.h"
36 #include "core/inspector/InjectedScriptHost.h"
37 #include "core/inspector/InspectorState.h"
38 #include "platform/Timer.h"
39 #include "wtf/CurrentTime.h"
43 typedef uint32_t SnapshotObjectId;
45 namespace HeapProfilerAgentState {
46 static const char heapProfilerEnabled[] = "heapProfilerEnabled";
47 static const char heapObjectsTrackingEnabled[] = "heapObjectsTrackingEnabled";
48 static const char allocationTrackingEnabled[] = "allocationTrackingEnabled";
51 class InspectorHeapProfilerAgent::HeapStatsUpdateTask {
53 HeapStatsUpdateTask(InspectorHeapProfilerAgent*);
55 void resetTimer() { m_timer.stop(); }
56 void onTimer(Timer<HeapStatsUpdateTask>*);
59 InspectorHeapProfilerAgent* m_heapProfilerAgent;
60 Timer<HeapStatsUpdateTask> m_timer;
63 PassOwnPtr<InspectorHeapProfilerAgent> InspectorHeapProfilerAgent::create(InjectedScriptManager* injectedScriptManager)
65 return adoptPtr(new InspectorHeapProfilerAgent(injectedScriptManager));
68 InspectorHeapProfilerAgent::InspectorHeapProfilerAgent(InjectedScriptManager* injectedScriptManager)
69 : InspectorBaseAgent<InspectorHeapProfilerAgent>("HeapProfiler")
70 , m_injectedScriptManager(injectedScriptManager)
72 , m_nextUserInitiatedHeapSnapshotNumber(1)
76 InspectorHeapProfilerAgent::~InspectorHeapProfilerAgent()
80 void InspectorHeapProfilerAgent::setFrontend(InspectorFrontend* frontend)
82 m_frontend = frontend->heapprofiler();
85 void InspectorHeapProfilerAgent::clearFrontend()
89 m_nextUserInitiatedHeapSnapshotNumber = 1;
90 stopTrackingHeapObjectsInternal();
91 m_injectedScriptManager->injectedScriptHost()->clearInspectedObjects();
97 void InspectorHeapProfilerAgent::restore()
99 if (m_state->getBoolean(HeapProfilerAgentState::heapProfilerEnabled))
100 m_frontend->resetProfiles();
101 if (m_state->getBoolean(HeapProfilerAgentState::heapObjectsTrackingEnabled))
102 startTrackingHeapObjectsInternal(m_state->getBoolean(HeapProfilerAgentState::allocationTrackingEnabled));
105 void InspectorHeapProfilerAgent::collectGarbage(WebCore::ErrorString*)
107 ScriptProfiler::collectGarbage();
110 InspectorHeapProfilerAgent::HeapStatsUpdateTask::HeapStatsUpdateTask(InspectorHeapProfilerAgent* heapProfilerAgent)
111 : m_heapProfilerAgent(heapProfilerAgent)
112 , m_timer(this, &HeapStatsUpdateTask::onTimer)
116 void InspectorHeapProfilerAgent::HeapStatsUpdateTask::onTimer(Timer<HeapStatsUpdateTask>*)
118 // The timer is stopped on m_heapProfilerAgent destruction,
119 // so this method will never be called after m_heapProfilerAgent has been destroyed.
120 m_heapProfilerAgent->requestHeapStatsUpdate();
123 void InspectorHeapProfilerAgent::HeapStatsUpdateTask::startTimer()
125 ASSERT(!m_timer.isActive());
126 m_timer.startRepeating(0.05, FROM_HERE);
129 class InspectorHeapProfilerAgent::HeapStatsStream FINAL : public ScriptProfiler::OutputStream {
131 HeapStatsStream(InspectorHeapProfilerAgent* heapProfilerAgent)
132 : m_heapProfilerAgent(heapProfilerAgent)
136 virtual void write(const uint32_t* chunk, const int size) OVERRIDE
140 m_heapProfilerAgent->pushHeapStatsUpdate(chunk, size);
143 InspectorHeapProfilerAgent* m_heapProfilerAgent;
146 void InspectorHeapProfilerAgent::startTrackingHeapObjects(ErrorString*, const bool* trackAllocations)
148 m_state->setBoolean(HeapProfilerAgentState::heapObjectsTrackingEnabled, true);
149 bool allocationTrackingEnabled = trackAllocations && *trackAllocations;
150 m_state->setBoolean(HeapProfilerAgentState::allocationTrackingEnabled, allocationTrackingEnabled);
151 startTrackingHeapObjectsInternal(allocationTrackingEnabled);
154 void InspectorHeapProfilerAgent::requestHeapStatsUpdate()
158 HeapStatsStream stream(this);
159 SnapshotObjectId lastSeenObjectId = ScriptProfiler::requestHeapStatsUpdate(&stream);
160 m_frontend->lastSeenObjectId(lastSeenObjectId, WTF::currentTimeMS());
163 void InspectorHeapProfilerAgent::pushHeapStatsUpdate(const uint32_t* const data, const int size)
167 RefPtr<TypeBuilder::Array<int> > statsDiff = TypeBuilder::Array<int>::create();
168 for (int i = 0; i < size; ++i)
169 statsDiff->addItem(data[i]);
170 m_frontend->heapStatsUpdate(statsDiff.release());
173 void InspectorHeapProfilerAgent::stopTrackingHeapObjects(ErrorString* error, const bool* reportProgress)
175 if (!m_heapStatsUpdateTask) {
176 *error = "Heap object tracking is not started.";
179 requestHeapStatsUpdate();
180 takeHeapSnapshot(error, reportProgress);
181 stopTrackingHeapObjectsInternal();
184 void InspectorHeapProfilerAgent::startTrackingHeapObjectsInternal(bool trackAllocations)
186 if (m_heapStatsUpdateTask)
188 ScriptProfiler::startTrackingHeapObjects(trackAllocations);
189 m_heapStatsUpdateTask = adoptPtr(new HeapStatsUpdateTask(this));
190 m_heapStatsUpdateTask->startTimer();
193 void InspectorHeapProfilerAgent::stopTrackingHeapObjectsInternal()
195 if (!m_heapStatsUpdateTask)
197 ScriptProfiler::stopTrackingHeapObjects();
198 m_heapStatsUpdateTask->resetTimer();
199 m_heapStatsUpdateTask.clear();
200 m_state->setBoolean(HeapProfilerAgentState::heapObjectsTrackingEnabled, false);
201 m_state->setBoolean(HeapProfilerAgentState::allocationTrackingEnabled, false);
204 void InspectorHeapProfilerAgent::enable(ErrorString*)
206 m_state->setBoolean(HeapProfilerAgentState::heapProfilerEnabled, true);
209 void InspectorHeapProfilerAgent::disable(ErrorString* error)
211 stopTrackingHeapObjectsInternal();
212 ScriptProfiler::clearHeapObjectIds();
213 m_state->setBoolean(HeapProfilerAgentState::heapProfilerEnabled, false);
216 void InspectorHeapProfilerAgent::takeHeapSnapshot(ErrorString* errorString, const bool* reportProgress)
218 class HeapSnapshotProgress FINAL : public ScriptProfiler::HeapSnapshotProgress {
220 explicit HeapSnapshotProgress(InspectorFrontend::HeapProfiler* frontend)
221 : m_frontend(frontend) { }
222 virtual void Start(int totalWork) OVERRIDE
224 m_totalWork = totalWork;
226 virtual void Worked(int workDone) OVERRIDE
229 m_frontend->reportHeapSnapshotProgress(workDone, m_totalWork, 0);
233 virtual void Done() OVERRIDE
235 const bool finished = true;
237 m_frontend->reportHeapSnapshotProgress(m_totalWork, m_totalWork, &finished);
241 virtual bool isCanceled() OVERRIDE { return false; }
243 InspectorFrontend::HeapProfiler* m_frontend;
247 String title = "Snapshot " + String::number(m_nextUserInitiatedHeapSnapshotNumber++);
248 HeapSnapshotProgress progress(reportProgress && *reportProgress ? m_frontend : 0);
249 RefPtr<ScriptHeapSnapshot> snapshot = ScriptProfiler::takeHeapSnapshot(title, &progress);
251 *errorString = "Failed to take heap snapshot";
255 class OutputStream : public ScriptHeapSnapshot::OutputStream {
257 explicit OutputStream(InspectorFrontend::HeapProfiler* frontend)
258 : m_frontend(frontend) { }
259 void Write(const String& chunk)
261 m_frontend->addHeapSnapshotChunk(chunk);
266 InspectorFrontend::HeapProfiler* m_frontend;
270 OutputStream stream(m_frontend);
271 snapshot->writeJSON(&stream);
275 void InspectorHeapProfilerAgent::getObjectByHeapObjectId(ErrorString* error, const String& heapSnapshotObjectId, const String* objectGroup, RefPtr<TypeBuilder::Runtime::RemoteObject>& result)
278 unsigned id = heapSnapshotObjectId.toUInt(&ok);
280 *error = "Invalid heap snapshot object id";
283 ScriptObject heapObject = ScriptProfiler::objectByHeapObjectId(id);
284 if (heapObject.isEmpty()) {
285 *error = "Object is not available";
288 InjectedScript injectedScript = m_injectedScriptManager->injectedScriptFor(heapObject.scriptState());
289 if (injectedScript.isEmpty()) {
290 *error = "Object is not available. Inspected context is gone";
293 result = injectedScript.wrapObject(heapObject, objectGroup ? *objectGroup : "");
295 *error = "Failed to wrap object";
298 void InspectorHeapProfilerAgent::getHeapObjectId(ErrorString* errorString, const String& objectId, String* heapSnapshotObjectId)
300 InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
301 if (injectedScript.isEmpty()) {
302 *errorString = "Inspected context has gone";
305 ScriptValue value = injectedScript.findObjectById(objectId);
306 ScriptState::Scope scope(injectedScript.scriptState());
307 if (value.isEmpty() || value.isUndefined()) {
308 *errorString = "Object with given id not found";
311 unsigned id = ScriptProfiler::getHeapObjectId(value);
312 *heapSnapshotObjectId = String::number(id);
315 } // namespace WebCore