1 /*
2 * Copyright (C) 2013 Google 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 are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31 #include "config.h"
32 #include "core/inspector/InspectorHeapProfilerAgent.h"
33
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"
40
41 namespace WebCore {
42
43 typedef uint32_t SnapshotObjectId;
44
45 namespace HeapProfilerAgentState {
46 static const char heapProfilerEnabled[] = "heapProfilerEnabled";
47 static const char heapObjectsTrackingEnabled[] = "heapObjectsTrackingEnabled";
48 static const char allocationTrackingEnabled[] = "allocationTrackingEnabled";
49 }
50
51 class InspectorHeapProfilerAgent::HeapStatsUpdateTask {
52 public:
53 HeapStatsUpdateTask(InspectorHeapProfilerAgent*);
54 void startTimer();
resetTimer()55 void resetTimer() { m_timer.stop(); }
56 void onTimer(Timer<HeapStatsUpdateTask>*);
57
58 private:
59 InspectorHeapProfilerAgent* m_heapProfilerAgent;
60 Timer<HeapStatsUpdateTask> m_timer;
61 };
62
create(InjectedScriptManager * injectedScriptManager)63 PassOwnPtr<InspectorHeapProfilerAgent> InspectorHeapProfilerAgent::create(InjectedScriptManager* injectedScriptManager)
64 {
65 return adoptPtr(new InspectorHeapProfilerAgent(injectedScriptManager));
66 }
67
InspectorHeapProfilerAgent(InjectedScriptManager * injectedScriptManager)68 InspectorHeapProfilerAgent::InspectorHeapProfilerAgent(InjectedScriptManager* injectedScriptManager)
69 : InspectorBaseAgent<InspectorHeapProfilerAgent>("HeapProfiler")
70 , m_injectedScriptManager(injectedScriptManager)
71 , m_frontend(0)
72 , m_nextUserInitiatedHeapSnapshotNumber(1)
73 {
74 }
75
~InspectorHeapProfilerAgent()76 InspectorHeapProfilerAgent::~InspectorHeapProfilerAgent()
77 {
78 }
79
setFrontend(InspectorFrontend * frontend)80 void InspectorHeapProfilerAgent::setFrontend(InspectorFrontend* frontend)
81 {
82 m_frontend = frontend->heapprofiler();
83 }
84
clearFrontend()85 void InspectorHeapProfilerAgent::clearFrontend()
86 {
87 m_frontend = 0;
88
89 m_nextUserInitiatedHeapSnapshotNumber = 1;
90 stopTrackingHeapObjectsInternal();
91 m_injectedScriptManager->injectedScriptHost()->clearInspectedObjects();
92
93 ErrorString error;
94 disable(&error);
95 }
96
restore()97 void InspectorHeapProfilerAgent::restore()
98 {
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));
103 }
104
collectGarbage(WebCore::ErrorString *)105 void InspectorHeapProfilerAgent::collectGarbage(WebCore::ErrorString*)
106 {
107 ScriptProfiler::collectGarbage();
108 }
109
HeapStatsUpdateTask(InspectorHeapProfilerAgent * heapProfilerAgent)110 InspectorHeapProfilerAgent::HeapStatsUpdateTask::HeapStatsUpdateTask(InspectorHeapProfilerAgent* heapProfilerAgent)
111 : m_heapProfilerAgent(heapProfilerAgent)
112 , m_timer(this, &HeapStatsUpdateTask::onTimer)
113 {
114 }
115
onTimer(Timer<HeapStatsUpdateTask> *)116 void InspectorHeapProfilerAgent::HeapStatsUpdateTask::onTimer(Timer<HeapStatsUpdateTask>*)
117 {
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();
121 }
122
startTimer()123 void InspectorHeapProfilerAgent::HeapStatsUpdateTask::startTimer()
124 {
125 ASSERT(!m_timer.isActive());
126 m_timer.startRepeating(0.05, FROM_HERE);
127 }
128
129 class InspectorHeapProfilerAgent::HeapStatsStream FINAL : public ScriptProfiler::OutputStream {
130 public:
HeapStatsStream(InspectorHeapProfilerAgent * heapProfilerAgent)131 HeapStatsStream(InspectorHeapProfilerAgent* heapProfilerAgent)
132 : m_heapProfilerAgent(heapProfilerAgent)
133 {
134 }
135
write(const uint32_t * chunk,const int size)136 virtual void write(const uint32_t* chunk, const int size) OVERRIDE
137 {
138 ASSERT(chunk);
139 ASSERT(size > 0);
140 m_heapProfilerAgent->pushHeapStatsUpdate(chunk, size);
141 }
142 private:
143 InspectorHeapProfilerAgent* m_heapProfilerAgent;
144 };
145
startTrackingHeapObjects(ErrorString *,const bool * trackAllocations)146 void InspectorHeapProfilerAgent::startTrackingHeapObjects(ErrorString*, const bool* trackAllocations)
147 {
148 m_state->setBoolean(HeapProfilerAgentState::heapObjectsTrackingEnabled, true);
149 bool allocationTrackingEnabled = trackAllocations && *trackAllocations;
150 m_state->setBoolean(HeapProfilerAgentState::allocationTrackingEnabled, allocationTrackingEnabled);
151 startTrackingHeapObjectsInternal(allocationTrackingEnabled);
152 }
153
requestHeapStatsUpdate()154 void InspectorHeapProfilerAgent::requestHeapStatsUpdate()
155 {
156 if (!m_frontend)
157 return;
158 HeapStatsStream stream(this);
159 SnapshotObjectId lastSeenObjectId = ScriptProfiler::requestHeapStatsUpdate(&stream);
160 m_frontend->lastSeenObjectId(lastSeenObjectId, WTF::currentTimeMS());
161 }
162
pushHeapStatsUpdate(const uint32_t * const data,const int size)163 void InspectorHeapProfilerAgent::pushHeapStatsUpdate(const uint32_t* const data, const int size)
164 {
165 if (!m_frontend)
166 return;
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());
171 }
172
stopTrackingHeapObjects(ErrorString * error,const bool * reportProgress)173 void InspectorHeapProfilerAgent::stopTrackingHeapObjects(ErrorString* error, const bool* reportProgress)
174 {
175 if (!m_heapStatsUpdateTask) {
176 *error = "Heap object tracking is not started.";
177 return;
178 }
179 requestHeapStatsUpdate();
180 takeHeapSnapshot(error, reportProgress);
181 stopTrackingHeapObjectsInternal();
182 }
183
startTrackingHeapObjectsInternal(bool trackAllocations)184 void InspectorHeapProfilerAgent::startTrackingHeapObjectsInternal(bool trackAllocations)
185 {
186 if (m_heapStatsUpdateTask)
187 return;
188 ScriptProfiler::startTrackingHeapObjects(trackAllocations);
189 m_heapStatsUpdateTask = adoptPtr(new HeapStatsUpdateTask(this));
190 m_heapStatsUpdateTask->startTimer();
191 }
192
stopTrackingHeapObjectsInternal()193 void InspectorHeapProfilerAgent::stopTrackingHeapObjectsInternal()
194 {
195 if (!m_heapStatsUpdateTask)
196 return;
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);
202 }
203
enable(ErrorString *)204 void InspectorHeapProfilerAgent::enable(ErrorString*)
205 {
206 m_state->setBoolean(HeapProfilerAgentState::heapProfilerEnabled, true);
207 }
208
disable(ErrorString * error)209 void InspectorHeapProfilerAgent::disable(ErrorString* error)
210 {
211 stopTrackingHeapObjectsInternal();
212 ScriptProfiler::clearHeapObjectIds();
213 m_state->setBoolean(HeapProfilerAgentState::heapProfilerEnabled, false);
214 }
215
takeHeapSnapshot(ErrorString * errorString,const bool * reportProgress)216 void InspectorHeapProfilerAgent::takeHeapSnapshot(ErrorString* errorString, const bool* reportProgress)
217 {
218 class HeapSnapshotProgress FINAL : public ScriptProfiler::HeapSnapshotProgress {
219 public:
220 explicit HeapSnapshotProgress(InspectorFrontend::HeapProfiler* frontend)
221 : m_frontend(frontend) { }
222 virtual void Start(int totalWork) OVERRIDE
223 {
224 m_totalWork = totalWork;
225 }
226 virtual void Worked(int workDone) OVERRIDE
227 {
228 if (m_frontend) {
229 m_frontend->reportHeapSnapshotProgress(workDone, m_totalWork, 0);
230 m_frontend->flush();
231 }
232 }
233 virtual void Done() OVERRIDE
234 {
235 const bool finished = true;
236 if (m_frontend) {
237 m_frontend->reportHeapSnapshotProgress(m_totalWork, m_totalWork, &finished);
238 m_frontend->flush();
239 }
240 }
241 virtual bool isCanceled() OVERRIDE { return false; }
242 private:
243 InspectorFrontend::HeapProfiler* m_frontend;
244 int m_totalWork;
245 };
246
247 String title = "Snapshot " + String::number(m_nextUserInitiatedHeapSnapshotNumber++);
248 HeapSnapshotProgress progress(reportProgress && *reportProgress ? m_frontend : 0);
249 RefPtr<ScriptHeapSnapshot> snapshot = ScriptProfiler::takeHeapSnapshot(title, &progress);
250 if (!snapshot) {
251 *errorString = "Failed to take heap snapshot";
252 return;
253 }
254
255 class OutputStream : public ScriptHeapSnapshot::OutputStream {
256 public:
257 explicit OutputStream(InspectorFrontend::HeapProfiler* frontend)
258 : m_frontend(frontend) { }
259 void Write(const String& chunk)
260 {
261 m_frontend->addHeapSnapshotChunk(chunk);
262 m_frontend->flush();
263 }
264 void Close() { }
265 private:
266 InspectorFrontend::HeapProfiler* m_frontend;
267 };
268
269 if (m_frontend) {
270 OutputStream stream(m_frontend);
271 snapshot->writeJSON(&stream);
272 }
273 }
274
getObjectByHeapObjectId(ErrorString * error,const String & heapSnapshotObjectId,const String * objectGroup,RefPtr<TypeBuilder::Runtime::RemoteObject> & result)275 void InspectorHeapProfilerAgent::getObjectByHeapObjectId(ErrorString* error, const String& heapSnapshotObjectId, const String* objectGroup, RefPtr<TypeBuilder::Runtime::RemoteObject>& result)
276 {
277 bool ok;
278 unsigned id = heapSnapshotObjectId.toUInt(&ok);
279 if (!ok) {
280 *error = "Invalid heap snapshot object id";
281 return;
282 }
283 ScriptValue heapObject = ScriptProfiler::objectByHeapObjectId(id);
284 if (heapObject.isEmpty()) {
285 *error = "Object is not available";
286 return;
287 }
288 InjectedScript injectedScript = m_injectedScriptManager->injectedScriptFor(heapObject.scriptState());
289 if (injectedScript.isEmpty()) {
290 *error = "Object is not available. Inspected context is gone";
291 return;
292 }
293 result = injectedScript.wrapObject(heapObject, objectGroup ? *objectGroup : "");
294 if (!result)
295 *error = "Failed to wrap object";
296 }
297
getHeapObjectId(ErrorString * errorString,const String & objectId,String * heapSnapshotObjectId)298 void InspectorHeapProfilerAgent::getHeapObjectId(ErrorString* errorString, const String& objectId, String* heapSnapshotObjectId)
299 {
300 InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
301 if (injectedScript.isEmpty()) {
302 *errorString = "Inspected context has gone";
303 return;
304 }
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";
309 return;
310 }
311 unsigned id = ScriptProfiler::getHeapObjectId(value);
312 *heapSnapshotObjectId = String::number(id);
313 }
314
315 } // namespace WebCore
316
317