From a633d93f7f4e8c3f0711446627551efab1bd47fb Mon Sep 17 00:00:00 2001 From: Ruslan Lesiutin Date: Fri, 23 May 2025 05:33:38 -0700 Subject: [PATCH] Define sampling methods implementations for Hermes fallback target delegate (#51519) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51519 # Changelog: [Internal] Hermes Sampling Profiler doesn't require debugger to be enabled - we can use it in fallback Hermes Runtime target delegate. That's probably the last thing to make Hermes really run in `opt` mode. When Hermes Target is compiled with no Debugger support, we can still define implementation for sampling profiler methods and call them on Hermes Runtime. Reviewed By: huntie Differential Revision: D75188276 fbshipit-source-id: e3e0dccd09e0870264e8abc65f96052735d63ad8 --- .../chrome/HermesRuntimeTargetDelegate.cpp | 68 ++++++++++++++++--- 1 file changed, 57 insertions(+), 11 deletions(-) diff --git a/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeTargetDelegate.cpp b/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeTargetDelegate.cpp index 2b486f71b1c..0b95f4922b2 100644 --- a/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeTargetDelegate.cpp +++ b/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeTargetDelegate.cpp @@ -28,13 +28,39 @@ using namespace facebook::hermes; namespace facebook::react::jsinspector_modern { -#ifdef HERMES_ENABLE_DEBUGGER namespace { const uint16_t HERMES_SAMPLING_FREQUENCY_HZ = 10000; +class HermesRuntimeSamplingProfileDelegate { + public: + explicit HermesRuntimeSamplingProfileDelegate( + std::shared_ptr hermesRuntime) + : hermesRuntime_(std::move(hermesRuntime)) {} + + void startSampling() { + auto* hermesAPI = castInterface(makeHermesRootAPI()); + hermesAPI->enableSamplingProfiler(HERMES_SAMPLING_FREQUENCY_HZ); + } + + void stopSampling() { + auto* hermesAPI = castInterface(makeHermesRootAPI()); + hermesAPI->disableSamplingProfiler(); + } + + tracing::RuntimeSamplingProfile collectSamplingProfile() { + return tracing::HermesRuntimeSamplingProfileSerializer:: + serializeToTracingSamplingProfile( + hermesRuntime_->dumpSampledTraceToProfile()); + } + + private: + std::shared_ptr hermesRuntime_; +}; + } // namespace +#ifdef HERMES_ENABLE_DEBUGGER class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate { using HermesStackTrace = debugger::StackTrace; @@ -60,8 +86,11 @@ class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate { HermesRuntimeTargetDelegate& delegate, std::shared_ptr hermesRuntime) : delegate_(delegate), - runtime_(std::move(hermesRuntime)), - cdpDebugAPI_(CDPDebugAPI::create(*runtime_)) {} + runtime_(hermesRuntime), + cdpDebugAPI_(CDPDebugAPI::create(*runtime_)), + samplingProfileDelegate_( + std::make_unique( + std::move(hermesRuntime))) {} CDPDebugAPI& getCDPDebugAPI() { return *cdpDebugAPI_; @@ -175,25 +204,23 @@ class HermesRuntimeTargetDelegate::Impl final : public RuntimeTargetDelegate { } void enableSamplingProfiler() override { - auto* hermesAPI = castInterface(makeHermesRootAPI()); - hermesAPI->enableSamplingProfiler(HERMES_SAMPLING_FREQUENCY_HZ); + samplingProfileDelegate_->startSampling(); } void disableSamplingProfiler() override { - auto* hermesAPI = castInterface(makeHermesRootAPI()); - hermesAPI->disableSamplingProfiler(); + samplingProfileDelegate_->stopSampling(); } tracing::RuntimeSamplingProfile collectSamplingProfile() override { - return tracing::HermesRuntimeSamplingProfileSerializer:: - serializeToTracingSamplingProfile( - runtime_->dumpSampledTraceToProfile()); + return samplingProfileDelegate_->collectSamplingProfile(); } private: HermesRuntimeTargetDelegate& delegate_; std::shared_ptr runtime_; const std::unique_ptr cdpDebugAPI_; + std::unique_ptr + samplingProfileDelegate_; }; #else @@ -208,7 +235,26 @@ class HermesRuntimeTargetDelegate::Impl final explicit Impl( HermesRuntimeTargetDelegate&, std::shared_ptr hermesRuntime) - : FallbackRuntimeTargetDelegate{hermesRuntime->description()} {} + : FallbackRuntimeTargetDelegate{hermesRuntime->description()}, + samplingProfileDelegate_( + std::make_unique( + std::move(hermesRuntime))) {} + + void enableSamplingProfiler() override { + samplingProfileDelegate_->startSampling(); + } + + void disableSamplingProfiler() override { + samplingProfileDelegate_->stopSampling(); + } + + tracing::RuntimeSamplingProfile collectSamplingProfile() override { + return samplingProfileDelegate_->collectSamplingProfile(); + } + + private: + std::unique_ptr + samplingProfileDelegate_; }; #endif // HERMES_ENABLE_DEBUGGER