From 186582e8f9224b1e8756bacd13b9f8cdbfea5e61 Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Tue, 5 Jul 2022 00:23:08 -0400 Subject: [PATCH 1/2] Fix hermes profiler --- .../hermes/executor/HermesExecutorFactory.cpp | 2 ++ .../hermes/inspector/chrome/Connection.cpp | 24 +++++++------------ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/ReactCommon/hermes/executor/HermesExecutorFactory.cpp b/ReactCommon/hermes/executor/HermesExecutorFactory.cpp index bab34bf3805d74..52cf8c65f5f1d4 100644 --- a/ReactCommon/hermes/executor/HermesExecutorFactory.cpp +++ b/ReactCommon/hermes/executor/HermesExecutorFactory.cpp @@ -164,6 +164,7 @@ class DecoratedRuntime : public jsi::WithRuntimeDecorator { runtime_, hermesRuntime_, jsQueue); facebook::hermes::inspector::chrome::enableDebugging( std::move(adapter), "Hermes React Native"); + hermesRuntime_.registerForProfiling(); #else (void)hermesRuntime_; #endif @@ -172,6 +173,7 @@ class DecoratedRuntime : public jsi::WithRuntimeDecorator { ~DecoratedRuntime() { #ifdef HERMES_ENABLE_DEBUGGER facebook::hermes::inspector::chrome::disableDebugging(hermesRuntime_); + hermesRuntime_.unregisterForProfiling(); #endif } diff --git a/ReactCommon/hermes/inspector/chrome/Connection.cpp b/ReactCommon/hermes/inspector/chrome/Connection.cpp index b7b264093b221d..7ba720cfab9a6d 100644 --- a/ReactCommon/hermes/inspector/chrome/Connection.cpp +++ b/ReactCommon/hermes/inspector/chrome/Connection.cpp @@ -139,7 +139,7 @@ class Connection::Impl : public inspector::InspectorObserver, template void runInExecutor(int id, C callback) { - folly::via(executor_.get(), [cb = std::move(callback)]() { cb(); }); + executor_->add([cb = std::move(callback)]() { cb(); }); } std::shared_ptr runtimeAdapter_; @@ -1411,20 +1411,14 @@ Connection::Impl::makePropsFromValue( } void Connection::Impl::handle(const m::runtime::GetHeapUsageRequest &req) { - auto resp = std::make_shared(); - resp->id = req.id; - - inspector_ - ->executeIfEnabled( - "Runtime.getHeapUsage", - [this, req, resp](const debugger::ProgramState &state) { - auto heapInfo = getRuntime().instrumentation().getHeapInfo(false); - resp->usedSize = heapInfo["hermes_allocatedBytes"]; - resp->totalSize = heapInfo["hermes_heapSize"]; - }) - .via(executor_.get()) - .thenValue([this, resp](auto &&) { sendResponseToClient(*resp); }) - .thenError(sendErrorToClient(req.id)); + runInExecutor(req.id, [this, req]() { + auto heapInfo = getRuntime().instrumentation().getHeapInfo(false); + auto resp = std::make_shared(); + resp->id = req.id; + resp->usedSize = heapInfo["hermes_allocatedBytes"]; + resp->totalSize = heapInfo["hermes_heapSize"]; + sendResponseToClient(*resp); + }); } void Connection::Impl::handle(const m::runtime::GetPropertiesRequest &req) { From bc3a9cc0d1a9902fe3ffe407cc768f8f6db1368e Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Tue, 5 Jul 2022 22:35:36 -0400 Subject: [PATCH 2/2] Use hermes runtime config --- ReactCommon/hermes/executor/HermesExecutorFactory.cpp | 8 ++++++-- ReactCommon/hermes/executor/HermesExecutorFactory.h | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ReactCommon/hermes/executor/HermesExecutorFactory.cpp b/ReactCommon/hermes/executor/HermesExecutorFactory.cpp index 52cf8c65f5f1d4..b7e3879a849035 100644 --- a/ReactCommon/hermes/executor/HermesExecutorFactory.cpp +++ b/ReactCommon/hermes/executor/HermesExecutorFactory.cpp @@ -164,7 +164,6 @@ class DecoratedRuntime : public jsi::WithRuntimeDecorator { runtime_, hermesRuntime_, jsQueue); facebook::hermes::inspector::chrome::enableDebugging( std::move(adapter), "Hermes React Native"); - hermesRuntime_.registerForProfiling(); #else (void)hermesRuntime_; #endif @@ -173,7 +172,6 @@ class DecoratedRuntime : public jsi::WithRuntimeDecorator { ~DecoratedRuntime() { #ifdef HERMES_ENABLE_DEBUGGER facebook::hermes::inspector::chrome::disableDebugging(hermesRuntime_); - hermesRuntime_.unregisterForProfiling(); #endif } @@ -223,6 +221,12 @@ std::unique_ptr HermesExecutorFactory::createJSExecutor( decoratedRuntime, delegate, jsQueue, timeoutInvoker_, runtimeInstaller_); } +::hermes::vm::RuntimeConfig HermesExecutorFactory::defaultRuntimeConfig() { + return ::hermes::vm::RuntimeConfig::Builder() + .withEnableSampleProfiling(true) + .build(); +} + HermesExecutor::HermesExecutor( std::shared_ptr runtime, std::shared_ptr delegate, diff --git a/ReactCommon/hermes/executor/HermesExecutorFactory.h b/ReactCommon/hermes/executor/HermesExecutorFactory.h index 5d4468e3668bd1..d06323ec7d4ebf 100644 --- a/ReactCommon/hermes/executor/HermesExecutorFactory.h +++ b/ReactCommon/hermes/executor/HermesExecutorFactory.h @@ -21,7 +21,7 @@ class HermesExecutorFactory : public JSExecutorFactory { JSIExecutor::RuntimeInstaller runtimeInstaller, const JSIScopedTimeoutInvoker &timeoutInvoker = JSIExecutor::defaultTimeoutInvoker, - ::hermes::vm::RuntimeConfig runtimeConfig = ::hermes::vm::RuntimeConfig()) + ::hermes::vm::RuntimeConfig runtimeConfig = defaultRuntimeConfig()) : runtimeInstaller_(runtimeInstaller), timeoutInvoker_(timeoutInvoker), runtimeConfig_(std::move(runtimeConfig)) { @@ -33,6 +33,8 @@ class HermesExecutorFactory : public JSExecutorFactory { std::shared_ptr jsQueue) override; private: + static ::hermes::vm::RuntimeConfig defaultRuntimeConfig(); + JSIExecutor::RuntimeInstaller runtimeInstaller_; JSIScopedTimeoutInvoker timeoutInvoker_; ::hermes::vm::RuntimeConfig runtimeConfig_;