diff --git a/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeSamplingProfileSerializer.cpp b/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeSamplingProfileSerializer.cpp index 64548dadc8c..076321a402d 100644 --- a/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeSamplingProfileSerializer.cpp +++ b/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeSamplingProfileSerializer.cpp @@ -18,9 +18,9 @@ namespace fhsp = facebook::hermes::sampling_profiler; /// Fallback script ID for call frames, when Hermes didn't provide one or when /// this frame is part of the VM, like native functions, used for parity with /// Chromium + V8. -const uint32_t FALLBACK_SCRIPT_ID = 0; +constexpr uint32_t FALLBACK_SCRIPT_ID = 0; /// Garbage collector frame name, used for parity with Chromium + V8. -const std::string GARBAGE_COLLECTOR_FRAME_NAME = "(garbage collector)"; +constexpr std::string_view GARBAGE_COLLECTOR_FRAME_NAME = "(garbage collector)"; /// Filters out Hermes Suspend frames related to Debugger. /// Even though Debugger domain is expected to be disabled, Hermes might run @@ -74,8 +74,9 @@ RuntimeSamplingProfile::SampleCallStackFrame convertJSFunctionHermesFrame( RuntimeSamplingProfile::SampleCallStackFrame::Kind::JSFunction, frame.getScriptId(), frame.getFunctionName(), - frame.hasUrl() ? std::optional{frame.getUrl()} - : std::nullopt, + frame.hasScriptUrl() + ? std::optional{frame.getScriptUrl()} + : std::nullopt, frame.hasFunctionLineNumber() ? std::optional{frame.getFunctionLineNumber() - 1} // Hermes VM keeps line numbers as 1-based. Convert @@ -145,7 +146,7 @@ RuntimeSamplingProfile::Sample convertHermesSampleToTracingSample( /* static */ RuntimeSamplingProfile HermesRuntimeSamplingProfileSerializer::serializeToTracingSamplingProfile( - const hermes::sampling_profiler::Profile& hermesProfile) { + hermes::sampling_profiler::Profile hermesProfile) { const auto samplesRange = hermesProfile.getSamplesRange(); std::vector reconciledSamples; reconciledSamples.reserve(hermesProfile.getSamplesCount()); @@ -156,7 +157,10 @@ HermesRuntimeSamplingProfileSerializer::serializeToTracingSamplingProfile( reconciledSamples.push_back(std::move(reconciledSample)); } - return RuntimeSamplingProfile{"Hermes", std::move(reconciledSamples)}; + return RuntimeSamplingProfile{ + "Hermes", + std::move(reconciledSamples), + std::make_unique(std::move(hermesProfile))}; } } // namespace facebook::react::jsinspector_modern::tracing diff --git a/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeSamplingProfileSerializer.h b/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeSamplingProfileSerializer.h index 1e205af8524..7bc3396be2a 100644 --- a/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeSamplingProfileSerializer.h +++ b/packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeSamplingProfileSerializer.h @@ -13,10 +13,20 @@ namespace facebook::react::jsinspector_modern::tracing { +class RawHermesRuntimeProfile : public RawRuntimeProfile { + public: + explicit RawHermesRuntimeProfile( + hermes::sampling_profiler::Profile hermesProfile) + : hermesProfile_{std::move(hermesProfile)} {} + + private: + hermes::sampling_profiler::Profile hermesProfile_; +}; + class HermesRuntimeSamplingProfileSerializer { public: static tracing::RuntimeSamplingProfile serializeToTracingSamplingProfile( - const hermes::sampling_profiler::Profile& hermesProfile); + hermes::sampling_profiler::Profile hermesProfile); }; } // namespace facebook::react::jsinspector_modern::tracing diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfile.h b/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfile.h index e6373029b06..4db95511b56 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfile.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfile.h @@ -7,13 +7,23 @@ #pragma once +#include #include #include +#include #include #include namespace facebook::react::jsinspector_modern::tracing { +/// Opaque class to represent the original runtime profile returned by the +/// Runtime. RuntimeSamplingProfile class is designed to be agnostic to the +/// Runtime where sampling occurred. +class RawRuntimeProfile { + public: + virtual ~RawRuntimeProfile() = default; +}; + /// Contains relevant information about the sampled runtime from start to /// finish. struct RuntimeSamplingProfile { @@ -33,8 +43,8 @@ struct RuntimeSamplingProfile { SampleCallStackFrame( const Kind kind, const uint32_t scriptId, - std::string functionName, - std::optional url = std::nullopt, + std::string_view functionName, + std::optional url = std::nullopt, const std::optional& lineNumber = std::nullopt, const std::optional& columnNumber = std::nullopt) : kind_(kind), @@ -55,7 +65,7 @@ struct RuntimeSamplingProfile { } /// \return name of the function that represents call frame. - const std::string& getFunctionName() const { + std::string_view getFunctionName() const { return functionName_; } @@ -64,7 +74,7 @@ struct RuntimeSamplingProfile { } /// \return source url of the corresponding script in the VM. - const std::string& getUrl() const { + std::string_view getUrl() const { return url_.value(); } @@ -95,8 +105,8 @@ struct RuntimeSamplingProfile { private: Kind kind_; uint32_t scriptId_; - std::string functionName_; - std::optional url_; + std::string_view functionName_; + std::optional url_; std::optional lineNumber_; std::optional columnNumber_; }; @@ -148,8 +158,13 @@ struct RuntimeSamplingProfile { std::vector callStack_; }; - RuntimeSamplingProfile(std::string runtimeName, std::vector samples) - : runtimeName_(std::move(runtimeName)), samples_(std::move(samples)) {} + RuntimeSamplingProfile( + std::string runtimeName, + std::vector samples, + std::unique_ptr rawRuntimeProfile) + : runtimeName_(std::move(runtimeName)), + samples_(std::move(samples)), + rawRuntimeProfile_(std::move(rawRuntimeProfile)) {} // Movable. RuntimeSamplingProfile& operator=(RuntimeSamplingProfile&&) = default; @@ -174,6 +189,11 @@ struct RuntimeSamplingProfile { std::string runtimeName_; /// List of recorded samples, should be chronologically sorted. std::vector samples_; + /// A unique pointer to the original raw runtime profile, collected from the + /// runtime in RuntimeTargetDelegate. Keeping a pointer to the original + /// profile allows it to remain alive as long as RuntimeSamplingProfile is + /// alive, since it may be using the same std::string_view. + std::unique_ptr rawRuntimeProfile_; }; } // namespace facebook::react::jsinspector_modern::tracing diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfileTraceEventSerializer.cpp b/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfileTraceEventSerializer.cpp index 69bc6ecfd95..27e44039e40 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfileTraceEventSerializer.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfileTraceEventSerializer.cpp @@ -5,8 +5,10 @@ * LICENSE file in the root directory of this source tree. */ -#include "RuntimeSamplingProfileTraceEventSerializer.h" +#include + #include "ProfileTreeNode.h" +#include "RuntimeSamplingProfileTraceEventSerializer.h" namespace facebook::react::jsinspector_modern::tracing { @@ -21,6 +23,11 @@ constexpr uint16_t PROFILE_ID = 1; /// by Chrome DevTools. constexpr uint32_t FALLBACK_SCRIPT_ID = 0; +constexpr std::string_view GARBAGE_COLLECTOR_FRAME_NAME = "(garbage collector)"; +constexpr std::string_view ROOT_FRAME_NAME = "(root)"; +constexpr std::string_view IDLE_FRAME_NAME = "(idle)"; +constexpr std::string_view PROGRAM_FRAME_NAME = "(program)"; + uint64_t formatTimePointToUnixTimestamp( std::chrono::steady_clock::time_point timestamp) { return std::chrono::duration_cast( @@ -37,9 +44,10 @@ TraceEventProfileChunk::CPUProfile::Node convertToTraceEventProfileNode( node.getCodeType() == ProfileTreeNode::CodeType::JavaScript ? "JS" : "other", callFrame.getScriptId(), - callFrame.getFunctionName(), - callFrame.hasUrl() ? std::optional(callFrame.getUrl()) - : std::nullopt, + std::string(callFrame.getFunctionName()), + callFrame.hasUrl() + ? std::optional(std::string(callFrame.getUrl())) + : std::nullopt, callFrame.hasLineNumber() ? std::optional(callFrame.getLineNumber()) : std::nullopt, @@ -55,18 +63,18 @@ TraceEventProfileChunk::CPUProfile::Node convertToTraceEventProfileNode( } RuntimeSamplingProfile::SampleCallStackFrame createArtificialCallFrame( - std::string callFrameName) { + std::string_view callFrameName) { return RuntimeSamplingProfile::SampleCallStackFrame{ RuntimeSamplingProfile::SampleCallStackFrame::Kind::JSFunction, FALLBACK_SCRIPT_ID, - std::move(callFrameName)}; + callFrameName}; }; RuntimeSamplingProfile::SampleCallStackFrame createGarbageCollectorCallFrame() { return RuntimeSamplingProfile::SampleCallStackFrame{ RuntimeSamplingProfile::SampleCallStackFrame::Kind::GarbageCollector, FALLBACK_SCRIPT_ID, - "(garbage collector)"}; + GARBAGE_COLLECTOR_FRAME_NAME}; }; class ProfileTreeRootNode : public ProfileTreeNode { @@ -75,7 +83,7 @@ class ProfileTreeRootNode : public ProfileTreeNode { : ProfileTreeNode( id, CodeType::Other, - createArtificialCallFrame("(root)")) {} + createArtificialCallFrame(ROOT_FRAME_NAME)) {} }; } // namespace @@ -204,13 +212,13 @@ void RuntimeSamplingProfileTraceEventSerializer::serializeAndNotify( ProfileTreeNode* programNode = rootNode.addChild( nodeIdGenerator.getNext(), ProfileTreeNode::CodeType::Other, - createArtificialCallFrame("(program)")); + createArtificialCallFrame(PROGRAM_FRAME_NAME)); chunk.nodes.push_back(*programNode); ProfileTreeNode* idleNode = rootNode.addChild( nodeIdGenerator.getNext(), ProfileTreeNode::CodeType::Other, - createArtificialCallFrame("(idle)")); + createArtificialCallFrame(IDLE_FRAME_NAME)); chunk.nodes.push_back(*idleNode); uint32_t idleNodeId = idleNode->getId(); diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/tests/RuntimeSamplingProfileTraceEventSerializerTest.cpp b/packages/react-native/ReactCommon/jsinspector-modern/tracing/tests/RuntimeSamplingProfileTraceEventSerializerTest.cpp index a1c10e4c4f7..e63cc80192b 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/tests/RuntimeSamplingProfileTraceEventSerializerTest.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/tests/RuntimeSamplingProfileTraceEventSerializerTest.cpp @@ -26,16 +26,16 @@ class RuntimeSamplingProfileTraceEventSerializerTest : public ::testing::Test { } RuntimeSamplingProfile::SampleCallStackFrame createJSCallFrame( - std::string functionName, + std::string_view functionName, uint32_t scriptId = 1, - std::optional url = std::nullopt, + std::optional url = std::nullopt, std::optional lineNumber = std::nullopt, std::optional columnNumber = std::nullopt) { return RuntimeSamplingProfile::SampleCallStackFrame( RuntimeSamplingProfile::SampleCallStackFrame::Kind::JSFunction, scriptId, - std::move(functionName), - std::move(url), + functionName, + url, lineNumber, columnNumber); } @@ -55,12 +55,12 @@ class RuntimeSamplingProfileTraceEventSerializerTest : public ::testing::Test { } RuntimeSamplingProfile createEmptyProfile() { - return {"TestRuntime", {}}; + return {"TestRuntime", {}, {}}; } RuntimeSamplingProfile createProfileWithSamples( std::vector samples) { - return {"TestRuntime", std::move(samples)}; + return {"TestRuntime", std::move(samples), {}}; } };