From 14686259d946fe02a2a433cd47c8fe6660c9eb76 Mon Sep 17 00:00:00 2001 From: Ashok Menon Date: Fri, 28 Jun 2019 06:12:00 -0700 Subject: [PATCH] Create Snapshot From Stream Summary: Adding an instrumentation endpoint that allows us to write a snapshot out to an arbitrary output stream. This is in addition to `createSnapshotFromFile`. I reserve the right to replace the latter with the former in a later diff. This is necessary to allow snapshots to be requested over the chrome debugger protocol. The protocol sends the snapshot over the wire in chunks so we need to be able to use an output stream that can do the chunking. Because LLVM types are not available at the JSI layer, we accept a `std::ostream` reference which we wrap with `llvm::raw_os_ostream` within Hermes. We should not incur the static initializer cost (or other code bloat) of using ostreams as a result. Reviewed By: cwdick Differential Revision: D16016319 fbshipit-source-id: 2d0b4848fd5cbe9ddee371d856cd8eb19dd80396 --- ReactCommon/jsi/jsi/decorator.h | 4 ++++ ReactCommon/jsi/jsi/instrumentation.h | 10 ++++++++++ ReactCommon/jsi/jsi/jsi.cpp | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/ReactCommon/jsi/jsi/decorator.h b/ReactCommon/jsi/jsi/decorator.h index a2ce31a787f..2f2a4a915e5 100644 --- a/ReactCommon/jsi/jsi/decorator.h +++ b/ReactCommon/jsi/jsi/decorator.h @@ -337,6 +337,10 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation { return plain().instrumentation().createSnapshotToFile(path, compact); } + bool createSnapshotToStream(std::ostream& os, bool compact) override { + return plain().instrumentation().createSnapshotToStream(os, compact); + } + void writeBridgeTrafficTraceToFile( const std::string& fileName) const override { const_cast(plain()).instrumentation().writeBridgeTrafficTraceToFile( diff --git a/ReactCommon/jsi/jsi/instrumentation.h b/ReactCommon/jsi/jsi/instrumentation.h index 595e6889c1c..3a602e6b15b 100644 --- a/ReactCommon/jsi/jsi/instrumentation.h +++ b/ReactCommon/jsi/jsi/instrumentation.h @@ -6,6 +6,7 @@ */ #pragma once +#include #include #include @@ -55,6 +56,15 @@ class Instrumentation { /// \return true iff the heap capture succeeded virtual bool createSnapshotToFile(const std::string& path, bool compact) = 0; + /// Captures the heap to an output stream + /// + /// \param os output stream to write to. + /// + /// \param compact Whether the JSON should be compact or pretty + /// + /// \return true iff the heap capture succeeded. + virtual bool createSnapshotToStream(std::ostream& os, bool compact) = 0; + /// Write a trace of bridge traffic to the given file name. virtual void writeBridgeTrafficTraceToFile( const std::string& fileName) const = 0; diff --git a/ReactCommon/jsi/jsi/jsi.cpp b/ReactCommon/jsi/jsi/jsi.cpp index 90ec784ef2e..408b321c542 100644 --- a/ReactCommon/jsi/jsi/jsi.cpp +++ b/ReactCommon/jsi/jsi/jsi.cpp @@ -81,6 +81,10 @@ Instrumentation& Runtime::instrumentation() { return false; } + bool createSnapshotToStream(std::ostream&, bool) override { + return false; + } + void writeBridgeTrafficTraceToFile(const std::string&) const override { std::abort(); }