Skip to content

Commit

Permalink
Add HeapProfiler.collectGarbage implementation
Browse files Browse the repository at this point in the history
Summary:
In Chrome, there's a garbage can icon in the memory profiler page that you can
click to force a garbage collection.
Hermes was previously not responding to that button. Add support for this inspector
message type.

The collection it starts is fully synchronous and will block JS from running. It typically
completes quite fast for small heaps, and about 1 second for heaps over 100 MB big.

Changelog:
[Internal][Added] Add support for garbage collection during heap profiling with Hermes

Reviewed By: neildhar

Differential Revision: D24349262

fbshipit-source-id: fe62b8df4d2b67ab3930e5d57f94478b2a88a549
  • Loading branch information
Riley Dulin authored and facebook-github-bot committed Oct 19, 2020
1 parent 2a3c26e commit 3c154c8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
17 changes: 17 additions & 0 deletions ReactCommon/hermes/inspector/chrome/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Connection::Impl : public inspector::InspectorObserver,
const m::heapProfiler::StartTrackingHeapObjectsRequest &req) override;
void handle(
const m::heapProfiler::StopTrackingHeapObjectsRequest &req) override;
void handle(const m::heapProfiler::CollectGarbageRequest &req) override;
void handle(const m::runtime::EvaluateRequest &req) override;
void handle(const m::runtime::GetPropertiesRequest &req) override;
void handle(const m::runtime::RunIfWaitingForDebuggerRequest &req) override;
Expand Down Expand Up @@ -579,6 +580,22 @@ void Connection::Impl::handle(
/* stopStackTraceCapture */ true);
}

void Connection::Impl::handle(
const m::heapProfiler::CollectGarbageRequest &req) {
const auto id = req.id;

inspector_
->executeIfEnabled(
"HeapProfiler.collectGarbage",
[this](const debugger::ProgramState &) {
getRuntime().instrumentation().collectGarbage("inspector");
})
.via(executor_.get())
.thenValue(
[this, id](auto &&) { sendResponseToClient(m::makeOkResponse(id)); })
.thenError<std::exception>(sendErrorToClient(req.id));
}

void Connection::Impl::handle(const m::runtime::EvaluateRequest &req) {
auto remoteObjPtr = std::make_shared<m::runtime::RemoteObject>();

Expand Down
25 changes: 24 additions & 1 deletion ReactCommon/hermes/inspector/chrome/MessageTypes.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2004-present Facebook. All Rights Reserved.
// @generated SignedSource<<80b4da5580a1fe18b9551723bdf7cab0>>
// @generated SignedSource<<e4c911229f0e8cac24dbc3ec8a933d5e>>

#include "MessageTypes.h"

Expand Down Expand Up @@ -44,6 +44,8 @@ std::unique_ptr<Request> Request::fromJsonThrowOnError(const std::string &str) {
{"Debugger.stepInto", makeUnique<debugger::StepIntoRequest>},
{"Debugger.stepOut", makeUnique<debugger::StepOutRequest>},
{"Debugger.stepOver", makeUnique<debugger::StepOverRequest>},
{"HeapProfiler.collectGarbage",
makeUnique<heapProfiler::CollectGarbageRequest>},
{"HeapProfiler.startTrackingHeapObjects",
makeUnique<heapProfiler::StartTrackingHeapObjectsRequest>},
{"HeapProfiler.stopTrackingHeapObjects",
Expand Down Expand Up @@ -656,6 +658,27 @@ void debugger::StepOverRequest::accept(RequestHandler &handler) const {
handler.handle(*this);
}

heapProfiler::CollectGarbageRequest::CollectGarbageRequest()
: Request("HeapProfiler.collectGarbage") {}

heapProfiler::CollectGarbageRequest::CollectGarbageRequest(const dynamic &obj)
: Request("HeapProfiler.collectGarbage") {
assign(id, obj, "id");
assign(method, obj, "method");
}

dynamic heapProfiler::CollectGarbageRequest::toDynamic() const {
dynamic obj = dynamic::object;
put(obj, "id", id);
put(obj, "method", method);
return obj;
}

void heapProfiler::CollectGarbageRequest::accept(
RequestHandler &handler) const {
handler.handle(*this);
}

heapProfiler::StartTrackingHeapObjectsRequest::StartTrackingHeapObjectsRequest()
: Request("HeapProfiler.startTrackingHeapObjects") {}

Expand Down
13 changes: 12 additions & 1 deletion ReactCommon/hermes/inspector/chrome/MessageTypes.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2004-present Facebook. All Rights Reserved.
// @generated SignedSource<<7383ffe2d956ba3aef8262dbee2f3427>>
// @generated SignedSource<<e3e5526b8e266b560b9dc9e42cc0d6c5>>

#pragma once

Expand Down Expand Up @@ -71,6 +71,7 @@ using UnserializableValue = std::string;

namespace heapProfiler {
struct AddHeapSnapshotChunkNotification;
struct CollectGarbageRequest;
struct HeapStatsUpdateNotification;
struct LastSeenObjectIdNotification;
struct ReportHeapSnapshotProgressNotification;
Expand Down Expand Up @@ -99,6 +100,7 @@ struct RequestHandler {
virtual void handle(const debugger::StepIntoRequest &req) = 0;
virtual void handle(const debugger::StepOutRequest &req) = 0;
virtual void handle(const debugger::StepOverRequest &req) = 0;
virtual void handle(const heapProfiler::CollectGarbageRequest &req) = 0;
virtual void handle(
const heapProfiler::StartTrackingHeapObjectsRequest &req) = 0;
virtual void handle(
Expand Down Expand Up @@ -127,6 +129,7 @@ struct NoopRequestHandler : public RequestHandler {
void handle(const debugger::StepIntoRequest &req) override {}
void handle(const debugger::StepOutRequest &req) override {}
void handle(const debugger::StepOverRequest &req) override {}
void handle(const heapProfiler::CollectGarbageRequest &req) override {}
void handle(
const heapProfiler::StartTrackingHeapObjectsRequest &req) override {}
void handle(
Expand Down Expand Up @@ -413,6 +416,14 @@ struct debugger::StepOverRequest : public Request {
void accept(RequestHandler &handler) const override;
};

struct heapProfiler::CollectGarbageRequest : public Request {
CollectGarbageRequest();
explicit CollectGarbageRequest(const folly::dynamic &obj);

folly::dynamic toDynamic() const override;
void accept(RequestHandler &handler) const override;
};

struct heapProfiler::StartTrackingHeapObjectsRequest : public Request {
StartTrackingHeapObjectsRequest();
explicit StartTrackingHeapObjectsRequest(const folly::dynamic &obj);
Expand Down
1 change: 1 addition & 0 deletions ReactCommon/hermes/inspector/tools/message_types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Debugger.stepInto
Debugger.stepOut
Debugger.stepOver
HeapProfiler.addHeapSnapshotChunk
HeapProfiler.collectGarbage
HeapProfiler.reportHeapSnapshotProgress
HeapProfiler.takeHeapSnapshot
HeapProfiler.startTrackingHeapObjects
Expand Down

0 comments on commit 3c154c8

Please sign in to comment.