| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/v8_inspector/V8HeapProfilerAgentImpl.h" | 5 #include "platform/v8_inspector/V8HeapProfilerAgentImpl.h" |
| 6 | 6 |
| 7 #include "platform/v8_inspector/InjectedScript.h" | 7 #include "platform/v8_inspector/InjectedScript.h" |
| 8 #include "platform/v8_inspector/InjectedScriptManager.h" | 8 #include "platform/v8_inspector/InjectedScriptManager.h" |
| 9 #include "platform/v8_inspector/V8RuntimeAgentImpl.h" | 9 #include "platform/v8_inspector/V8RuntimeAgentImpl.h" |
| 10 #include "platform/v8_inspector/V8StringUtil.h" | 10 #include "platform/v8_inspector/V8StringUtil.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 class HeapSnapshotProgress final : public v8::ActivityControl { | 25 class HeapSnapshotProgress final : public v8::ActivityControl { |
| 26 public: | 26 public: |
| 27 HeapSnapshotProgress(protocol::Frontend::HeapProfiler* frontend) | 27 HeapSnapshotProgress(protocol::Frontend::HeapProfiler* frontend) |
| 28 : m_frontend(frontend) { } | 28 : m_frontend(frontend) { } |
| 29 ControlOption ReportProgressValue(int done, int total) override | 29 ControlOption ReportProgressValue(int done, int total) override |
| 30 { | 30 { |
| 31 m_frontend->reportHeapSnapshotProgress(done, total, protocol::OptionalVa
lue<bool>()); | 31 m_frontend->reportHeapSnapshotProgress(done, total, protocol::Maybe<bool
>()); |
| 32 if (done >= total) { | 32 if (done >= total) { |
| 33 m_frontend->reportHeapSnapshotProgress(total, total, true); | 33 m_frontend->reportHeapSnapshotProgress(total, total, true); |
| 34 } | 34 } |
| 35 m_frontend->flush(); | 35 m_frontend->flush(); |
| 36 return kContinue; | 36 return kContinue; |
| 37 } | 37 } |
| 38 private: | 38 private: |
| 39 protocol::Frontend::HeapProfiler* m_frontend; | 39 protocol::Frontend::HeapProfiler* m_frontend; |
| 40 }; | 40 }; |
| 41 | 41 |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 ErrorString error; | 163 ErrorString error; |
| 164 startSampling(&error); | 164 startSampling(&error); |
| 165 } | 165 } |
| 166 } | 166 } |
| 167 | 167 |
| 168 void V8HeapProfilerAgentImpl::collectGarbage(ErrorString*) | 168 void V8HeapProfilerAgentImpl::collectGarbage(ErrorString*) |
| 169 { | 169 { |
| 170 m_isolate->LowMemoryNotification(); | 170 m_isolate->LowMemoryNotification(); |
| 171 } | 171 } |
| 172 | 172 |
| 173 void V8HeapProfilerAgentImpl::startTrackingHeapObjects(ErrorString*, const proto
col::OptionalValue<bool>& trackAllocations) | 173 void V8HeapProfilerAgentImpl::startTrackingHeapObjects(ErrorString*, const proto
col::Maybe<bool>& trackAllocations) |
| 174 { | 174 { |
| 175 m_state->setBoolean(HeapProfilerAgentState::heapObjectsTrackingEnabled, true
); | 175 m_state->setBoolean(HeapProfilerAgentState::heapObjectsTrackingEnabled, true
); |
| 176 bool allocationTrackingEnabled = trackAllocations.get(false); | 176 bool allocationTrackingEnabled = trackAllocations.fromMaybe(false); |
| 177 m_state->setBoolean(HeapProfilerAgentState::allocationTrackingEnabled, alloc
ationTrackingEnabled); | 177 m_state->setBoolean(HeapProfilerAgentState::allocationTrackingEnabled, alloc
ationTrackingEnabled); |
| 178 startTrackingHeapObjectsInternal(allocationTrackingEnabled); | 178 startTrackingHeapObjectsInternal(allocationTrackingEnabled); |
| 179 } | 179 } |
| 180 | 180 |
| 181 void V8HeapProfilerAgentImpl::stopTrackingHeapObjects(ErrorString* error, const
protocol::OptionalValue<bool>& reportProgress) | 181 void V8HeapProfilerAgentImpl::stopTrackingHeapObjects(ErrorString* error, const
protocol::Maybe<bool>& reportProgress) |
| 182 { | 182 { |
| 183 requestHeapStatsUpdate(); | 183 requestHeapStatsUpdate(); |
| 184 takeHeapSnapshot(error, reportProgress); | 184 takeHeapSnapshot(error, reportProgress); |
| 185 stopTrackingHeapObjectsInternal(); | 185 stopTrackingHeapObjectsInternal(); |
| 186 } | 186 } |
| 187 | 187 |
| 188 void V8HeapProfilerAgentImpl::enable(ErrorString*) | 188 void V8HeapProfilerAgentImpl::enable(ErrorString*) |
| 189 { | 189 { |
| 190 m_state->setBoolean(HeapProfilerAgentState::heapProfilerEnabled, true); | 190 m_state->setBoolean(HeapProfilerAgentState::heapProfilerEnabled, true); |
| 191 } | 191 } |
| 192 | 192 |
| 193 void V8HeapProfilerAgentImpl::disable(ErrorString* error) | 193 void V8HeapProfilerAgentImpl::disable(ErrorString* error) |
| 194 { | 194 { |
| 195 stopTrackingHeapObjectsInternal(); | 195 stopTrackingHeapObjectsInternal(); |
| 196 if (m_state->booleanProperty(HeapProfilerAgentState::samplingHeapProfilerEna
bled, false)) { | 196 if (m_state->booleanProperty(HeapProfilerAgentState::samplingHeapProfilerEna
bled, false)) { |
| 197 v8::HeapProfiler* profiler = m_isolate->GetHeapProfiler(); | 197 v8::HeapProfiler* profiler = m_isolate->GetHeapProfiler(); |
| 198 if (profiler) | 198 if (profiler) |
| 199 profiler->StopSamplingHeapProfiler(); | 199 profiler->StopSamplingHeapProfiler(); |
| 200 } | 200 } |
| 201 m_isolate->GetHeapProfiler()->ClearObjectIds(); | 201 m_isolate->GetHeapProfiler()->ClearObjectIds(); |
| 202 m_state->setBoolean(HeapProfilerAgentState::heapProfilerEnabled, false); | 202 m_state->setBoolean(HeapProfilerAgentState::heapProfilerEnabled, false); |
| 203 } | 203 } |
| 204 | 204 |
| 205 void V8HeapProfilerAgentImpl::takeHeapSnapshot(ErrorString* errorString, const p
rotocol::OptionalValue<bool>& reportProgress) | 205 void V8HeapProfilerAgentImpl::takeHeapSnapshot(ErrorString* errorString, const p
rotocol::Maybe<bool>& reportProgress) |
| 206 { | 206 { |
| 207 v8::HeapProfiler* profiler = m_isolate->GetHeapProfiler(); | 207 v8::HeapProfiler* profiler = m_isolate->GetHeapProfiler(); |
| 208 if (!profiler) { | 208 if (!profiler) { |
| 209 *errorString = "Cannot access v8 heap profiler"; | 209 *errorString = "Cannot access v8 heap profiler"; |
| 210 return; | 210 return; |
| 211 } | 211 } |
| 212 OwnPtr<HeapSnapshotProgress> progress; | 212 OwnPtr<HeapSnapshotProgress> progress; |
| 213 if (reportProgress.get(false)) | 213 if (reportProgress.fromMaybe(false)) |
| 214 progress = adoptPtr(new HeapSnapshotProgress(m_frontend)); | 214 progress = adoptPtr(new HeapSnapshotProgress(m_frontend)); |
| 215 | 215 |
| 216 GlobalObjectNameResolver resolver(static_cast<V8RuntimeAgentImpl*>(m_runtime
Agent)); | 216 GlobalObjectNameResolver resolver(static_cast<V8RuntimeAgentImpl*>(m_runtime
Agent)); |
| 217 const v8::HeapSnapshot* snapshot = profiler->TakeHeapSnapshot(progress.get()
, &resolver); | 217 const v8::HeapSnapshot* snapshot = profiler->TakeHeapSnapshot(progress.get()
, &resolver); |
| 218 if (!snapshot) { | 218 if (!snapshot) { |
| 219 *errorString = "Failed to take heap snapshot"; | 219 *errorString = "Failed to take heap snapshot"; |
| 220 return; | 220 return; |
| 221 } | 221 } |
| 222 HeapSnapshotOutputStream stream(m_frontend); | 222 HeapSnapshotOutputStream stream(m_frontend); |
| 223 snapshot->Serialize(&stream); | 223 snapshot->Serialize(&stream); |
| 224 const_cast<v8::HeapSnapshot*>(snapshot)->Delete(); | 224 const_cast<v8::HeapSnapshot*>(snapshot)->Delete(); |
| 225 } | 225 } |
| 226 | 226 |
| 227 void V8HeapProfilerAgentImpl::getObjectByHeapObjectId(ErrorString* error, const
String& heapSnapshotObjectId, const protocol::OptionalValue<String>& objectGroup
, OwnPtr<protocol::Runtime::RemoteObject>* result) | 227 void V8HeapProfilerAgentImpl::getObjectByHeapObjectId(ErrorString* error, const
String& heapSnapshotObjectId, const protocol::Maybe<String>& objectGroup, OwnPtr
<protocol::Runtime::RemoteObject>* result) |
| 228 { | 228 { |
| 229 bool ok; | 229 bool ok; |
| 230 unsigned id = heapSnapshotObjectId.toUInt(&ok); | 230 unsigned id = heapSnapshotObjectId.toUInt(&ok); |
| 231 if (!ok) { | 231 if (!ok) { |
| 232 *error = "Invalid heap snapshot object id"; | 232 *error = "Invalid heap snapshot object id"; |
| 233 return; | 233 return; |
| 234 } | 234 } |
| 235 | 235 |
| 236 v8::HandleScope handles(m_isolate); | 236 v8::HandleScope handles(m_isolate); |
| 237 v8::Local<v8::Object> heapObject = objectByHeapObjectId(m_isolate, id); | 237 v8::Local<v8::Object> heapObject = objectByHeapObjectId(m_isolate, id); |
| 238 if (heapObject.IsEmpty()) { | 238 if (heapObject.IsEmpty()) { |
| 239 *error = "Object is not available"; | 239 *error = "Object is not available"; |
| 240 return; | 240 return; |
| 241 } | 241 } |
| 242 *result = m_runtimeAgent->wrapObject(heapObject->CreationContext(), heapObje
ct, objectGroup.get("")); | 242 *result = m_runtimeAgent->wrapObject(heapObject->CreationContext(), heapObje
ct, objectGroup.fromMaybe("")); |
| 243 if (!result) | 243 if (!result) |
| 244 *error = "Object is not available"; | 244 *error = "Object is not available"; |
| 245 } | 245 } |
| 246 | 246 |
| 247 void V8HeapProfilerAgentImpl::addInspectedHeapObject(ErrorString* errorString, c
onst String& inspectedHeapObjectId) | 247 void V8HeapProfilerAgentImpl::addInspectedHeapObject(ErrorString* errorString, c
onst String& inspectedHeapObjectId) |
| 248 { | 248 { |
| 249 bool ok; | 249 bool ok; |
| 250 unsigned id = inspectedHeapObjectId.toUInt(&ok); | 250 unsigned id = inspectedHeapObjectId.toUInt(&ok); |
| 251 if (!ok) { | 251 if (!ok) { |
| 252 *errorString = "Invalid heap snapshot object id"; | 252 *errorString = "Invalid heap snapshot object id"; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 if (!v8Profile) { | 335 if (!v8Profile) { |
| 336 *errorString = "Cannot access v8 sampled heap profile."; | 336 *errorString = "Cannot access v8 sampled heap profile."; |
| 337 return; | 337 return; |
| 338 } | 338 } |
| 339 v8::AllocationProfile::Node* root = v8Profile->GetRootNode(); | 339 v8::AllocationProfile::Node* root = v8Profile->GetRootNode(); |
| 340 *profile = protocol::HeapProfiler::SamplingHeapProfile::create() | 340 *profile = protocol::HeapProfiler::SamplingHeapProfile::create() |
| 341 .setHead(buildSampingHeapProfileNode(root)).build(); | 341 .setHead(buildSampingHeapProfileNode(root)).build(); |
| 342 } | 342 } |
| 343 | 343 |
| 344 } // namespace blink | 344 } // namespace blink |
| OLD | NEW |