Commit 39020433 authored by kozyatinskiy's avatar kozyatinskiy Committed by Commit bot

[inspector] migrate HeapProfiler to new style

BUG=none
R=dgozman@chromium.org

Review-Url: https://codereview.chromium.org/2474483003
Cr-Commit-Position: refs/heads/master@{#40689}
parent 8c08d423
......@@ -164,39 +164,42 @@ void V8HeapProfilerAgentImpl::restore() {
HeapProfilerAgentState::allocationTrackingEnabled, false));
if (m_state->booleanProperty(
HeapProfilerAgentState::samplingHeapProfilerEnabled, false)) {
ErrorString error;
double samplingInterval = m_state->doubleProperty(
HeapProfilerAgentState::samplingHeapProfilerInterval, -1);
DCHECK_GE(samplingInterval, 0);
startSampling(&error, Maybe<double>(samplingInterval));
startSampling(Maybe<double>(samplingInterval));
}
}
void V8HeapProfilerAgentImpl::collectGarbage(ErrorString*) {
Response V8HeapProfilerAgentImpl::collectGarbage() {
m_isolate->LowMemoryNotification();
return Response::OK();
}
void V8HeapProfilerAgentImpl::startTrackingHeapObjects(
ErrorString*, const protocol::Maybe<bool>& trackAllocations) {
Response V8HeapProfilerAgentImpl::startTrackingHeapObjects(
Maybe<bool> trackAllocations) {
m_state->setBoolean(HeapProfilerAgentState::heapObjectsTrackingEnabled, true);
bool allocationTrackingEnabled = trackAllocations.fromMaybe(false);
m_state->setBoolean(HeapProfilerAgentState::allocationTrackingEnabled,
allocationTrackingEnabled);
startTrackingHeapObjectsInternal(allocationTrackingEnabled);
return Response::OK();
}
void V8HeapProfilerAgentImpl::stopTrackingHeapObjects(
ErrorString* error, const protocol::Maybe<bool>& reportProgress) {
Response V8HeapProfilerAgentImpl::stopTrackingHeapObjects(
Maybe<bool> reportProgress) {
requestHeapStatsUpdate();
takeHeapSnapshot(error, reportProgress);
takeHeapSnapshot(std::move(reportProgress));
stopTrackingHeapObjectsInternal();
return Response::OK();
}
void V8HeapProfilerAgentImpl::enable(ErrorString*) {
Response V8HeapProfilerAgentImpl::enable() {
m_state->setBoolean(HeapProfilerAgentState::heapProfilerEnabled, true);
return Response::OK();
}
void V8HeapProfilerAgentImpl::disable(ErrorString* error) {
Response V8HeapProfilerAgentImpl::disable() {
stopTrackingHeapObjectsInternal();
if (m_state->booleanProperty(
HeapProfilerAgentState::samplingHeapProfilerEnabled, false)) {
......@@ -205,15 +208,12 @@ void V8HeapProfilerAgentImpl::disable(ErrorString* error) {
}
m_isolate->GetHeapProfiler()->ClearObjectIds();
m_state->setBoolean(HeapProfilerAgentState::heapProfilerEnabled, false);
return Response::OK();
}
void V8HeapProfilerAgentImpl::takeHeapSnapshot(
ErrorString* errorString, const protocol::Maybe<bool>& reportProgress) {
Response V8HeapProfilerAgentImpl::takeHeapSnapshot(Maybe<bool> reportProgress) {
v8::HeapProfiler* profiler = m_isolate->GetHeapProfiler();
if (!profiler) {
*errorString = "Cannot access v8 heap profiler";
return;
}
if (!profiler) return Response::Error("Cannot access v8 heap profiler");
std::unique_ptr<HeapSnapshotProgress> progress;
if (reportProgress.fromMaybe(false))
progress = wrapUnique(new HeapSnapshotProgress(&m_frontend));
......@@ -221,80 +221,63 @@ void V8HeapProfilerAgentImpl::takeHeapSnapshot(
GlobalObjectNameResolver resolver(m_session);
const v8::HeapSnapshot* snapshot =
profiler->TakeHeapSnapshot(progress.get(), &resolver);
if (!snapshot) {
*errorString = "Failed to take heap snapshot";
return;
}
if (!snapshot) return Response::Error("Failed to take heap snapshot");
HeapSnapshotOutputStream stream(&m_frontend);
snapshot->Serialize(&stream);
const_cast<v8::HeapSnapshot*>(snapshot)->Delete();
return Response::OK();
}
void V8HeapProfilerAgentImpl::getObjectByHeapObjectId(
ErrorString* error, const String16& heapSnapshotObjectId,
const protocol::Maybe<String16>& objectGroup,
Response V8HeapProfilerAgentImpl::getObjectByHeapObjectId(
const String16& heapSnapshotObjectId, Maybe<String16> objectGroup,
std::unique_ptr<protocol::Runtime::RemoteObject>* result) {
bool ok;
int id = heapSnapshotObjectId.toInteger(&ok);
if (!ok) {
*error = "Invalid heap snapshot object id";
return;
}
if (!ok) return Response::Error("Invalid heap snapshot object id");
v8::HandleScope handles(m_isolate);
v8::Local<v8::Object> heapObject = objectByHeapObjectId(m_isolate, id);
if (heapObject.IsEmpty()) {
*error = "Object is not available";
return;
}
if (heapObject.IsEmpty()) return Response::Error("Object is not available");
if (!m_session->inspector()->client()->isInspectableHeapObject(heapObject)) {
*error = "Object is not available";
return;
}
if (!m_session->inspector()->client()->isInspectableHeapObject(heapObject))
return Response::Error("Object is not available");
*result = m_session->wrapObject(heapObject->CreationContext(), heapObject,
objectGroup.fromMaybe(""), false);
if (!result) *error = "Object is not available";
if (!result) return Response::Error("Object is not available");
return Response::OK();
}
void V8HeapProfilerAgentImpl::addInspectedHeapObject(
ErrorString* errorString, const String16& inspectedHeapObjectId) {
Response V8HeapProfilerAgentImpl::addInspectedHeapObject(
const String16& inspectedHeapObjectId) {
bool ok;
int id = inspectedHeapObjectId.toInteger(&ok);
if (!ok) {
*errorString = "Invalid heap snapshot object id";
return;
}
if (!ok) return Response::Error("Invalid heap snapshot object id");
v8::HandleScope handles(m_isolate);
v8::Local<v8::Object> heapObject = objectByHeapObjectId(m_isolate, id);
if (heapObject.IsEmpty()) {
*errorString = "Object is not available";
return;
}
if (!m_session->inspector()->client()->isInspectableHeapObject(heapObject)) {
*errorString = "Object is not available";
return;
}
if (heapObject.IsEmpty()) return Response::Error("Object is not available");
if (!m_session->inspector()->client()->isInspectableHeapObject(heapObject))
return Response::Error("Object is not available");
m_session->addInspectedObject(wrapUnique(new InspectableHeapObject(id)));
return Response::OK();
}
void V8HeapProfilerAgentImpl::getHeapObjectId(ErrorString* errorString,
const String16& objectId,
String16* heapSnapshotObjectId) {
Response V8HeapProfilerAgentImpl::getHeapObjectId(
const String16& objectId, String16* heapSnapshotObjectId) {
v8::HandleScope handles(m_isolate);
v8::Local<v8::Value> value;
v8::Local<v8::Context> context;
if (!m_session->unwrapObject(errorString, objectId, &value, &context,
protocol::ErrorString errorString;
if (!m_session->unwrapObject(&errorString, objectId, &value, &context,
nullptr) ||
value->IsUndefined())
return;
return Response::Error(errorString);
v8::SnapshotObjectId id = m_isolate->GetHeapProfiler()->GetObjectId(value);
*heapSnapshotObjectId = String16::fromInteger(static_cast<size_t>(id));
return Response::OK();
}
void V8HeapProfilerAgentImpl::requestHeapStatsUpdate() {
......@@ -332,13 +315,10 @@ void V8HeapProfilerAgentImpl::stopTrackingHeapObjectsInternal() {
m_state->setBoolean(HeapProfilerAgentState::allocationTrackingEnabled, false);
}
void V8HeapProfilerAgentImpl::startSampling(
ErrorString* errorString, const Maybe<double>& samplingInterval) {
Response V8HeapProfilerAgentImpl::startSampling(
Maybe<double> samplingInterval) {
v8::HeapProfiler* profiler = m_isolate->GetHeapProfiler();
if (!profiler) {
*errorString = "Cannot access v8 heap profiler";
return;
}
if (!profiler) return Response::Error("Cannot access v8 heap profiler");
const unsigned defaultSamplingInterval = 1 << 15;
double samplingIntervalValue =
samplingInterval.fromMaybe(defaultSamplingInterval);
......@@ -349,6 +329,7 @@ void V8HeapProfilerAgentImpl::startSampling(
profiler->StartSamplingHeapProfiler(
static_cast<uint64_t>(samplingIntervalValue), 128,
v8::HeapProfiler::kSamplingForceGC);
return Response::OK();
}
namespace {
......@@ -379,14 +360,10 @@ buildSampingHeapProfileNode(const v8::AllocationProfile::Node* node) {
}
} // namespace
void V8HeapProfilerAgentImpl::stopSampling(
ErrorString* errorString,
Response V8HeapProfilerAgentImpl::stopSampling(
std::unique_ptr<protocol::HeapProfiler::SamplingHeapProfile>* profile) {
v8::HeapProfiler* profiler = m_isolate->GetHeapProfiler();
if (!profiler) {
*errorString = "Cannot access v8 heap profiler";
return;
}
if (!profiler) return Response::Error("Cannot access v8 heap profiler");
v8::HandleScope scope(
m_isolate); // Allocation profile contains Local handles.
std::unique_ptr<v8::AllocationProfile> v8Profile(
......@@ -394,14 +371,13 @@ void V8HeapProfilerAgentImpl::stopSampling(
profiler->StopSamplingHeapProfiler();
m_state->setBoolean(HeapProfilerAgentState::samplingHeapProfilerEnabled,
false);
if (!v8Profile) {
*errorString = "Cannot access v8 sampled heap profile.";
return;
}
if (!v8Profile)
return Response::Error("Cannot access v8 sampled heap profile.");
v8::AllocationProfile::Node* root = v8Profile->GetRootNode();
*profile = protocol::HeapProfiler::SamplingHeapProfile::create()
.setHead(buildSampingHeapProfileNode(root))
.build();
return Response::OK();
}
} // namespace v8_inspector
......@@ -15,8 +15,8 @@ namespace v8_inspector {
class V8InspectorSessionImpl;
using protocol::ErrorString;
using protocol::Maybe;
using protocol::Response;
class V8HeapProfilerAgentImpl : public protocol::HeapProfiler::Backend {
public:
......@@ -25,32 +25,26 @@ class V8HeapProfilerAgentImpl : public protocol::HeapProfiler::Backend {
~V8HeapProfilerAgentImpl() override;
void restore();
void collectGarbage(ErrorString*) override;
Response collectGarbage() override;
void enable(ErrorString*) override;
void startTrackingHeapObjects(ErrorString*,
const Maybe<bool>& trackAllocations) override;
void stopTrackingHeapObjects(ErrorString*,
const Maybe<bool>& reportProgress) override;
Response enable() override;
Response startTrackingHeapObjects(Maybe<bool> trackAllocations) override;
Response stopTrackingHeapObjects(Maybe<bool> reportProgress) override;
void disable(ErrorString*) override;
Response disable() override;
void takeHeapSnapshot(ErrorString*,
const Maybe<bool>& reportProgress) override;
Response takeHeapSnapshot(Maybe<bool> reportProgress) override;
void getObjectByHeapObjectId(
ErrorString*, const String16& heapSnapshotObjectId,
const Maybe<String16>& objectGroup,
Response getObjectByHeapObjectId(
const String16& heapSnapshotObjectId, Maybe<String16> objectGroup,
std::unique_ptr<protocol::Runtime::RemoteObject>* result) override;
void addInspectedHeapObject(ErrorString*,
const String16& inspectedHeapObjectId) override;
void getHeapObjectId(ErrorString*, const String16& objectId,
String16* heapSnapshotObjectId) override;
void startSampling(ErrorString*,
const Maybe<double>& samplingInterval) override;
void stopSampling(
ErrorString*,
Response addInspectedHeapObject(
const String16& inspectedHeapObjectId) override;
Response getHeapObjectId(const String16& objectId,
String16* heapSnapshotObjectId) override;
Response startSampling(Maybe<double> samplingInterval) override;
Response stopSampling(
std::unique_ptr<protocol::HeapProfiler::SamplingHeapProfile>*) override;
private:
......
......@@ -107,7 +107,7 @@ V8InspectorSessionImpl::~V8InspectorSessionImpl() {
ErrorString errorString;
m_consoleAgent->disable();
m_profilerAgent->disable();
m_heapProfilerAgent->disable(&errorString);
m_heapProfilerAgent->disable();
m_debuggerAgent->disable(&errorString);
m_runtimeAgent->disable(&errorString);
......
......@@ -330,7 +330,7 @@ def resolve_type(protocol, prop):
def new_style(domain):
domains = [ "Schema", "Console", "Profiler" ]
domains = [ "Schema", "Console", "Profiler", "HeapProfiler" ]
return domain["domain"] in domains
......
......@@ -14,5 +14,5 @@ description.
Local modifications:
- This only includes the lib/ and templates/ directories, scripts, build
and the LICENSE files.
- New style domains [ "Schema", "Console", "Profiler" ] are added in
CodeGenerator.py.
\ No newline at end of file
- New style domains [ "Schema", "Console", "Profiler", "HeapProfiler" ] are
added in CodeGenerator.py.
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment