diff --git a/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.cpp b/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.cpp index fb0f425a63b..f00c175fad1 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.cpp @@ -179,7 +179,7 @@ RuntimeTargetController::collectSamplingProfile() { void RuntimeTarget::registerForTracing() { jsExecutor_([](auto& /*runtime*/) { - tracing::PerformanceTracer::getInstance().reportJavaScriptThread(); + PerformanceTracer::getInstance().reportJavaScriptThread(); }); } diff --git a/packages/react-native/ReactCommon/jsinspector-modern/TracingAgent.cpp b/packages/react-native/ReactCommon/jsinspector-modern/TracingAgent.cpp index 0a3e365fd3d..28407ee865d 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/TracingAgent.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/TracingAgent.cpp @@ -44,7 +44,7 @@ bool TracingAgent::handleRequest(const cdp::PreparsedRequest& req) { } bool correctlyStartedPerformanceTracer = - tracing::PerformanceTracer::getInstance().startTracing(); + PerformanceTracer::getInstance().startTracing(); if (!correctlyStartedPerformanceTracer) { frontendChannel_(cdp::jsonError( @@ -56,7 +56,7 @@ bool TracingAgent::handleRequest(const cdp::PreparsedRequest& req) { } instanceAgent_->startTracing(); - instanceTracingStartTimestamp_ = HighResTimeStamp::now(); + instanceTracingStartTimestamp_ = std::chrono::steady_clock::now(); frontendChannel_(cdp::jsonResult(req.id)); return true; @@ -73,8 +73,7 @@ bool TracingAgent::handleRequest(const cdp::PreparsedRequest& req) { instanceAgent_->stopTracing(); - tracing::PerformanceTracer& performanceTracer = - tracing::PerformanceTracer::getInstance(); + PerformanceTracer& performanceTracer = PerformanceTracer::getInstance(); bool correctlyStopped = performanceTracer.stopTracing(); if (!correctlyStopped) { frontendChannel_(cdp::jsonError( diff --git a/packages/react-native/ReactCommon/jsinspector-modern/TracingAgent.h b/packages/react-native/ReactCommon/jsinspector-modern/TracingAgent.h index 95ed57efc45..cd5792fef5f 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/TracingAgent.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/TracingAgent.h @@ -11,8 +11,6 @@ #include "InstanceAgent.h" #include -#include -#include namespace facebook::react::jsinspector_modern { @@ -56,10 +54,9 @@ class TracingAgent { /** * Timestamp of when we started tracing of an Instance, will be used as a - * a start of JavaScript samples recording and as a time origin for the events - * in this trace. + * a start of JavaScript samples recording. */ - HighResTimeStamp instanceTracingStartTimestamp_; + std::chrono::steady_clock::time_point instanceTracingStartTimestamp_; }; } // namespace facebook::react::jsinspector_modern diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/CMakeLists.txt b/packages/react-native/ReactCommon/jsinspector-modern/tracing/CMakeLists.txt index 36ea342318c..282c84ecc69 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/CMakeLists.txt +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/CMakeLists.txt @@ -19,7 +19,6 @@ target_include_directories(jsinspector_tracing PUBLIC ${REACT_COMMON_DIR}) target_link_libraries(jsinspector_tracing folly_runtime oscompat - react_timing ) target_compile_reactnative_options(jsinspector_tracing PRIVATE) target_compile_options(jsinspector_tracing PRIVATE -Wpedantic) diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/EventLoopReporter.cpp b/packages/react-native/ReactCommon/jsinspector-modern/tracing/EventLoopReporter.cpp index 0c9ccba0208..d2c44ae3263 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/EventLoopReporter.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/EventLoopReporter.cpp @@ -14,21 +14,35 @@ namespace facebook::react::jsinspector_modern::tracing { #if defined(REACT_NATIVE_DEBUGGER_ENABLED) +namespace { + +inline uint64_t formatTimePointToUnixTimestamp( + std::chrono::steady_clock::time_point timestamp) { + return std::chrono::duration_cast( + timestamp.time_since_epoch()) + .count(); +} + +} // namespace EventLoopReporter::EventLoopReporter(EventLoopPhase phase) - : startTimestamp_(HighResTimeStamp::now()), phase_(phase) {} + : startTimestamp_(std::chrono::steady_clock::now()), phase_(phase) {} EventLoopReporter::~EventLoopReporter() { PerformanceTracer& performanceTracer = PerformanceTracer::getInstance(); if (performanceTracer.isTracing()) { - auto end = HighResTimeStamp::now(); + auto end = std::chrono::steady_clock::now(); switch (phase_) { case EventLoopPhase::Task: - performanceTracer.reportEventLoopTask(startTimestamp_, end); + performanceTracer.reportEventLoopTask( + formatTimePointToUnixTimestamp(startTimestamp_), + formatTimePointToUnixTimestamp(end)); break; case EventLoopPhase::Microtasks: - performanceTracer.reportEventLoopMicrotasks(startTimestamp_, end); + performanceTracer.reportEventLoopMicrotasks( + formatTimePointToUnixTimestamp(startTimestamp_), + formatTimePointToUnixTimestamp(end)); break; default: diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/EventLoopReporter.h b/packages/react-native/ReactCommon/jsinspector-modern/tracing/EventLoopReporter.h index 211fe139cba..bff47ca4155 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/EventLoopReporter.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/EventLoopReporter.h @@ -7,9 +7,7 @@ #pragma once -#if defined(REACT_NATIVE_DEBUGGER_ENABLED) -#include -#endif +#include namespace facebook::react::jsinspector_modern::tracing { @@ -31,7 +29,7 @@ struct EventLoopReporter { private: #if defined(REACT_NATIVE_DEBUGGER_ENABLED) - HighResTimeStamp startTimestamp_; + std::chrono::steady_clock::time_point startTimestamp_; EventLoopPhase phase_; #endif }; diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp b/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp index 0197b82c7f0..6f958ea3a5c 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp @@ -6,7 +6,6 @@ */ #include "PerformanceTracer.h" -#include "Timing.h" #include @@ -15,7 +14,17 @@ #include #include -namespace facebook::react::jsinspector_modern::tracing { +namespace facebook::react::jsinspector_modern { + +namespace { + +uint64_t getUnixTimestampOfNow() { + return std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()) + .count(); +} + +} // namespace PerformanceTracer& PerformanceTracer::getInstance() { static PerformanceTracer tracer; @@ -43,7 +52,7 @@ bool PerformanceTracer::startTracing() { .name = "TracingStartedInPage", .cat = "disabled-by-default-devtools.timeline", .ph = 'I', - .ts = HighResTimeStamp::now(), + .ts = getUnixTimestampOfNow(), .pid = processId_, .tid = oscompat::getCurrentThreadId(), .args = folly::dynamic::object("data", folly::dynamic::object()), @@ -69,7 +78,7 @@ bool PerformanceTracer::stopTracing() { .name = "ReactNative-TracingStopped", .cat = "disabled-by-default-devtools.timeline", .ph = 'I', - .ts = HighResTimeStamp::now(), + .ts = getUnixTimestampOfNow(), .pid = processId_, .tid = oscompat::getCurrentThreadId(), }); @@ -108,7 +117,7 @@ void PerformanceTracer::collectEvents( void PerformanceTracer::reportMark( const std::string_view& name, - HighResTimeStamp start) { + uint64_t start) { if (!tracing_) { return; } @@ -130,8 +139,8 @@ void PerformanceTracer::reportMark( void PerformanceTracer::reportMeasure( const std::string_view& name, - HighResTimeStamp start, - HighResDuration duration, + uint64_t start, + uint64_t duration, const std::optional& trackMetadata) { if (!tracing_) { return; @@ -188,7 +197,7 @@ void PerformanceTracer::reportProcess(uint64_t id, const std::string& name) { .name = "process_name", .cat = "__metadata", .ph = 'M', - .ts = TRACING_TIME_ORIGIN, + .ts = 0, .pid = id, .tid = 0, .args = folly::dynamic::object("name", name), @@ -213,7 +222,7 @@ void PerformanceTracer::reportThread(uint64_t id, const std::string& name) { .name = "thread_name", .cat = "__metadata", .ph = 'M', - .ts = TRACING_TIME_ORIGIN, + .ts = 0, .pid = processId_, .tid = id, .args = folly::dynamic::object("name", name), @@ -228,15 +237,13 @@ void PerformanceTracer::reportThread(uint64_t id, const std::string& name) { .name = "ReactNative-ThreadRegistered", .cat = "disabled-by-default-devtools.timeline", .ph = 'I', - .ts = TRACING_TIME_ORIGIN, + .ts = 0, .pid = processId_, .tid = id, }); } -void PerformanceTracer::reportEventLoopTask( - HighResTimeStamp start, - HighResTimeStamp end) { +void PerformanceTracer::reportEventLoopTask(uint64_t start, uint64_t end) { if (!tracing_) { return; } @@ -258,8 +265,8 @@ void PerformanceTracer::reportEventLoopTask( } void PerformanceTracer::reportEventLoopMicrotasks( - HighResTimeStamp start, - HighResTimeStamp end) { + uint64_t start, + uint64_t end) { if (!tracing_) { return; } @@ -283,7 +290,7 @@ void PerformanceTracer::reportEventLoopMicrotasks( folly::dynamic PerformanceTracer::getSerializedRuntimeProfileTraceEvent( uint64_t threadId, uint16_t profileId, - HighResTimeStamp profileTimestamp) { + uint64_t eventUnixTimestamp) { // CDT prioritizes event timestamp over startTime metadata field. // https://fburl.com/lo764pf4 return serializeTraceEvent(TraceEvent{ @@ -291,28 +298,25 @@ folly::dynamic PerformanceTracer::getSerializedRuntimeProfileTraceEvent( .name = "Profile", .cat = "disabled-by-default-v8.cpu_profiler", .ph = 'P', - .ts = profileTimestamp, + .ts = eventUnixTimestamp, .pid = processId_, .tid = threadId, .args = folly::dynamic::object( - "data", - folly ::dynamic::object( - "startTime", - highResTimeStampToTracingClockTimeStamp(profileTimestamp))), + "data", folly ::dynamic::object("startTime", eventUnixTimestamp)), }); } folly::dynamic PerformanceTracer::getSerializedRuntimeProfileChunkTraceEvent( uint16_t profileId, uint64_t threadId, - HighResTimeStamp chunkTimestamp, + uint64_t eventUnixTimestamp, const tracing::TraceEventProfileChunk& traceEventProfileChunk) { return serializeTraceEvent(TraceEvent{ .id = profileId, .name = "ProfileChunk", .cat = "disabled-by-default-v8.cpu_profiler", .ph = 'P', - .ts = chunkTimestamp, + .ts = eventUnixTimestamp, .pid = processId_, .tid = threadId, .args = @@ -332,15 +336,15 @@ folly::dynamic PerformanceTracer::serializeTraceEvent( result["name"] = event.name; result["cat"] = event.cat; result["ph"] = std::string(1, event.ph); - result["ts"] = highResTimeStampToTracingClockTimeStamp(event.ts); + result["ts"] = event.ts; result["pid"] = event.pid; result["tid"] = event.tid; result["args"] = event.args; if (event.dur.has_value()) { - result["dur"] = highResDurationToTracingClockDuration(event.dur.value()); + result["dur"] = event.dur.value(); } return result; } -} // namespace facebook::react::jsinspector_modern::tracing +} // namespace facebook::react::jsinspector_modern diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h b/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h index 9acc978dfc7..31a06674ac8 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h @@ -11,15 +11,14 @@ #include "TraceEvent.h" #include "TraceEventProfile.h" -#include - #include + #include #include #include #include -namespace facebook::react::jsinspector_modern::tracing { +namespace facebook::react::jsinspector_modern { // TODO: Review how this API is integrated into jsinspector_modern (singleton // design is copied from earlier FuseboxTracer prototype). @@ -66,7 +65,7 @@ class PerformanceTracer { * * See https://w3c.github.io/user-timing/#mark-method. */ - void reportMark(const std::string_view& name, HighResTimeStamp start); + void reportMark(const std::string_view& name, uint64_t start); /** * Record a `Performance.measure()` event - a labelled duration. If not @@ -76,8 +75,8 @@ class PerformanceTracer { */ void reportMeasure( const std::string_view& name, - HighResTimeStamp start, - HighResDuration duration, + uint64_t start, + uint64_t duration, const std::optional& trackMetadata); /** @@ -100,13 +99,13 @@ class PerformanceTracer { * Record an Event Loop tick, which will be represented as an Event Loop task * on a timeline view and grouped with JavaScript samples. */ - void reportEventLoopTask(HighResTimeStamp start, HighResTimeStamp end); + void reportEventLoopTask(uint64_t start, uint64_t end); /** * Record Microtasks phase of the Event Loop tick. Will be represented as a * "Run Microtasks" block under a task. */ - void reportEventLoopMicrotasks(HighResTimeStamp start, HighResTimeStamp end); + void reportEventLoopMicrotasks(uint64_t start, uint64_t end); /** * Create and serialize Profile Trace Event. @@ -115,7 +114,7 @@ class PerformanceTracer { folly::dynamic getSerializedRuntimeProfileTraceEvent( uint64_t threadId, uint16_t profileId, - HighResTimeStamp profileTimestamp); + uint64_t eventUnixTimestamp); /** * Create and serialize ProfileChunk Trace Event. @@ -124,8 +123,8 @@ class PerformanceTracer { folly::dynamic getSerializedRuntimeProfileChunkTraceEvent( uint16_t profileId, uint64_t threadId, - HighResTimeStamp chunkTimestamp, - const TraceEventProfileChunk& traceEventProfileChunk); + uint64_t eventUnixTimestamp, + const tracing::TraceEventProfileChunk& traceEventProfileChunk); private: PerformanceTracer(); @@ -136,11 +135,10 @@ class PerformanceTracer { folly::dynamic serializeTraceEvent(const TraceEvent& event) const; bool tracing_{false}; - uint64_t processId_; uint32_t performanceMeasureCount_{0}; std::vector buffer_; std::mutex mutex_; }; -} // namespace facebook::react::jsinspector_modern::tracing +} // namespace facebook::react::jsinspector_modern diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/React-jsinspectortracing.podspec b/packages/react-native/ReactCommon/jsinspector-modern/tracing/React-jsinspectortracing.podspec index 4765a6b32f4..9df0699bc78 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/React-jsinspectortracing.podspec +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/React-jsinspectortracing.podspec @@ -47,7 +47,6 @@ Pod::Spec.new do |s| end s.dependency "React-oscompat" - s.dependency "React-timing" add_rn_third_party_dependencies(s) end diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfileTraceEventSerializer.cpp b/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfileTraceEventSerializer.cpp index 6e181b5e0b7..27e44039e40 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfileTraceEventSerializer.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfileTraceEventSerializer.cpp @@ -14,17 +14,6 @@ namespace facebook::react::jsinspector_modern::tracing { namespace { -// To capture samples timestamps Hermes is using steady_clock and returns -// them in microseconds granularity since epoch. In the future we might want to -// update Hermes to return timestamps in chrono type. -HighResTimeStamp getHighResTimeStampForSample( - const RuntimeSamplingProfile::Sample& sample) { - auto microsecondsSinceSteadyClockEpoch = sample.getTimestamp(); - auto chronoTimePoint = std::chrono::steady_clock::time_point( - std::chrono::microseconds(microsecondsSinceSteadyClockEpoch)); - return HighResTimeStamp::fromChronoSteadyClockTimePoint(chronoTimePoint); -} - // Right now we only emit single Profile. We might revisit this decision in the // future, once we support multiple VMs being sampled at the same time. constexpr uint16_t PROFILE_ID = 1; @@ -39,6 +28,13 @@ 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( + timestamp.time_since_epoch()) + .count(); +} + TraceEventProfileChunk::CPUProfile::Node convertToTraceEventProfileNode( const ProfileTreeNode& node) { const RuntimeSamplingProfile::SampleCallStackFrame& callFrame = @@ -95,10 +91,10 @@ class ProfileTreeRootNode : public ProfileTreeNode { void RuntimeSamplingProfileTraceEventSerializer::sendProfileTraceEvent( uint64_t threadId, uint16_t profileId, - HighResTimeStamp profileStartTimestamp) const { + uint64_t profileStartUnixTimestamp) const { folly::dynamic serializedTraceEvent = performanceTracer_.getSerializedRuntimeProfileTraceEvent( - threadId, profileId, profileStartTimestamp); + threadId, profileId, profileStartUnixTimestamp); notificationCallback_(folly::dynamic::array(serializedTraceEvent)); } @@ -106,7 +102,7 @@ void RuntimeSamplingProfileTraceEventSerializer::sendProfileTraceEvent( void RuntimeSamplingProfileTraceEventSerializer::chunkEmptySample( ProfileChunk& chunk, uint32_t idleNodeId, - HighResDuration samplesTimeDelta) { + long long samplesTimeDelta) { chunk.samples.push_back(idleNodeId); chunk.timeDeltas.push_back(samplesTimeDelta); } @@ -143,7 +139,7 @@ void RuntimeSamplingProfileTraceEventSerializer::processCallStack( ProfileChunk& chunk, ProfileTreeNode& rootNode, uint32_t idleNodeId, - HighResDuration samplesTimeDelta, + long long samplesTimeDelta, NodeIdGenerator& nodeIdGenerator) { if (callStack.empty()) { chunkEmptySample(chunk, idleNodeId, samplesTimeDelta); @@ -187,7 +183,7 @@ void RuntimeSamplingProfileTraceEventSerializer:: void RuntimeSamplingProfileTraceEventSerializer::serializeAndNotify( const RuntimeSamplingProfile& profile, - HighResTimeStamp tracingStartTime) { + std::chrono::steady_clock::time_point tracingStartTime) { const std::vector& samples = profile.getSamples(); if (samples.empty()) { @@ -195,15 +191,18 @@ void RuntimeSamplingProfileTraceEventSerializer::serializeAndNotify( } uint64_t firstChunkThreadId = samples.front().getThreadId(); - HighResTimeStamp previousSampleTimestamp = tracingStartTime; - HighResTimeStamp currentChunkTimestamp = tracingStartTime; + uint64_t tracingStartUnixTimestamp = + formatTimePointToUnixTimestamp(tracingStartTime); + uint64_t previousSampleUnixTimestamp = tracingStartUnixTimestamp; + uint64_t currentChunkUnixTimestamp = tracingStartUnixTimestamp; - sendProfileTraceEvent(firstChunkThreadId, PROFILE_ID, tracingStartTime); + sendProfileTraceEvent( + firstChunkThreadId, PROFILE_ID, tracingStartUnixTimestamp); // There could be any number of new nodes in this chunk. Empty if all nodes // are already emitted in previous chunks. ProfileChunk chunk{ - profileChunkSize_, firstChunkThreadId, currentChunkTimestamp}; + profileChunkSize_, firstChunkThreadId, currentChunkUnixTimestamp}; NodeIdGenerator nodeIdGenerator{}; @@ -225,7 +224,7 @@ void RuntimeSamplingProfileTraceEventSerializer::serializeAndNotify( for (const auto& sample : samples) { uint64_t currentSampleThreadId = sample.getThreadId(); - auto currentSampleTimestamp = getHighResTimeStampForSample(sample); + long long currentSampleUnixTimestamp = sample.getTimestamp(); // We should not attempt to merge samples from different threads. // From past observations, this only happens for GC nodes. @@ -234,7 +233,7 @@ void RuntimeSamplingProfileTraceEventSerializer::serializeAndNotify( if (currentSampleThreadId != chunk.threadId || chunk.isFull()) { bufferProfileChunkTraceEvent(chunk, PROFILE_ID); chunk = ProfileChunk{ - profileChunkSize_, currentSampleThreadId, currentChunkTimestamp}; + profileChunkSize_, currentSampleThreadId, currentChunkUnixTimestamp}; } if (traceEventBuffer_.size() == traceEventChunkSize_) { @@ -246,10 +245,10 @@ void RuntimeSamplingProfileTraceEventSerializer::serializeAndNotify( chunk, rootNode, idleNodeId, - currentSampleTimestamp - previousSampleTimestamp, + currentSampleUnixTimestamp - previousSampleUnixTimestamp, nodeIdGenerator); - previousSampleTimestamp = currentSampleTimestamp; + previousSampleUnixTimestamp = currentSampleUnixTimestamp; } if (!chunk.isEmpty()) { diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfileTraceEventSerializer.h b/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfileTraceEventSerializer.h index 620d42df119..6f4cffa78a5 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfileTraceEventSerializer.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfileTraceEventSerializer.h @@ -11,8 +11,6 @@ #include "ProfileTreeNode.h" #include "RuntimeSamplingProfile.h" -#include - namespace facebook::react::jsinspector_modern::tracing { namespace { @@ -38,7 +36,7 @@ class RuntimeSamplingProfileTraceEventSerializer { ProfileChunk( uint16_t chunkSize, uint64_t chunkThreadId, - HighResTimeStamp chunkTimestamp) + uint64_t chunkTimestamp) : size(chunkSize), threadId(chunkThreadId), timestamp(chunkTimestamp) { samples.reserve(size); timeDeltas.reserve(size); @@ -54,10 +52,10 @@ class RuntimeSamplingProfileTraceEventSerializer { std::vector nodes; std::vector samples; - std::vector timeDeltas; + std::vector timeDeltas; uint16_t size; uint64_t threadId; - HighResTimeStamp timestamp; + uint64_t timestamp; }; public: @@ -91,7 +89,7 @@ class RuntimeSamplingProfileTraceEventSerializer { */ void serializeAndNotify( const RuntimeSamplingProfile& profile, - HighResTimeStamp tracingStartTime); + std::chrono::steady_clock::time_point tracingStartTime); private: /** @@ -104,7 +102,7 @@ class RuntimeSamplingProfileTraceEventSerializer { void sendProfileTraceEvent( uint64_t threadId, uint16_t profileId, - HighResTimeStamp profileStartTimestamp) const; + uint64_t profileStartUnixTimestamp) const; /** * Encapsulates logic for processing the empty sample, when the VM was idling. @@ -116,7 +114,7 @@ class RuntimeSamplingProfileTraceEventSerializer { void chunkEmptySample( ProfileChunk& chunk, uint32_t idleNodeId, - HighResDuration samplesTimeDelta); + long long samplesTimeDelta); /** * Records ProfileChunk as a "ProfileChunk" Trace Event in traceEventBuffer_. @@ -145,7 +143,7 @@ class RuntimeSamplingProfileTraceEventSerializer { ProfileChunk& chunk, ProfileTreeNode& rootNode, uint32_t idleNodeId, - HighResDuration samplesTimeDelta, + long long samplesTimeDelta, NodeIdGenerator& nodeIdGenerator); /** diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/Timing.h b/packages/react-native/ReactCommon/jsinspector-modern/tracing/Timing.h deleted file mode 100644 index bda8f65df22..00000000000 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/Timing.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include - -#include - -namespace facebook::react::jsinspector_modern::tracing { - -// The Tracing Clock time origin is the steady_clock epoch. This is mostly done -// to replicate Chromium's behavior, but also saves us from aligning custom -// DOMHighResTimeStamps that can be specified in performance.mark / -// performance.measure calls: these should not extend the timeline window, this -// is the current approach in Chromium. -constexpr HighResTimeStamp TRACING_TIME_ORIGIN = - HighResTimeStamp::fromChronoSteadyClockTimePoint( - std::chrono::steady_clock::time_point()); - -// Tracing timestamps are represented a time value in microseconds since -// arbitrary time origin (epoch) with no fractional part. -inline uint64_t highResTimeStampToTracingClockTimeStamp( - HighResTimeStamp timestamp) { - assert( - timestamp >= TRACING_TIME_ORIGIN && - "Provided timestamp is before time origin"); - auto duration = timestamp - TRACING_TIME_ORIGIN; - return static_cast( - static_cast(duration.toNanoseconds()) / 1e3); -} - -inline int64_t highResDurationToTracingClockDuration(HighResDuration duration) { - return static_cast( - static_cast(duration.toNanoseconds()) / 1e3); -} - -} // namespace facebook::react::jsinspector_modern::tracing diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/TraceEvent.h b/packages/react-native/ReactCommon/jsinspector-modern/tracing/TraceEvent.h index fd961d90d27..8c1a930548e 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/TraceEvent.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/TraceEvent.h @@ -7,11 +7,11 @@ #pragma once -#include - #include -namespace facebook::react::jsinspector_modern::tracing { +namespace facebook::react::jsinspector_modern { + +namespace { /** * A trace event to send to the debugger frontend, as defined by the Trace Event @@ -42,7 +42,7 @@ struct TraceEvent { char ph; /** The tracing clock timestamp of the event, in microseconds (µs). */ - HighResTimeStamp ts; + uint64_t ts; /** The process ID for the process that output this event. */ uint64_t pid; @@ -57,7 +57,9 @@ struct TraceEvent { * The duration of the event, in microseconds (µs). Only applicable to * complete events ("ph": "X"). */ - std::optional dur; + std::optional dur; }; -} // namespace facebook::react::jsinspector_modern::tracing +} // namespace + +} // namespace facebook::react::jsinspector_modern diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/TraceEventProfile.h b/packages/react-native/ReactCommon/jsinspector-modern/tracing/TraceEventProfile.h index cbafe64d527..48685444909 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/TraceEventProfile.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/TraceEventProfile.h @@ -7,9 +7,6 @@ #pragma once -#include -#include - #include namespace facebook::react::jsinspector_modern::tracing { @@ -21,15 +18,10 @@ struct TraceEventProfileChunk { /// Will be sent as part of the "ProfileChunk" trace event. struct TimeDeltas { folly::dynamic toDynamic() const { - auto value = folly::dynamic::array(); - value.reserve(deltas.size()); - for (const auto& delta : deltas) { - value.push_back(highResDurationToTracingClockDuration(delta)); - } - return value; + return folly::dynamic::array(deltas.begin(), deltas.end()); } - std::vector deltas; + std::vector deltas; }; /// Contains Profile information that will be emitted in this chunk: nodes and 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 1303f560fba..e63cc80192b 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/tests/RuntimeSamplingProfileTraceEventSerializerTest.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/tests/RuntimeSamplingProfileTraceEventSerializerTest.cpp @@ -6,10 +6,10 @@ */ #include -#include #include #include +#include #include namespace facebook::react::jsinspector_modern::tracing { @@ -71,7 +71,7 @@ TEST_F(RuntimeSamplingProfileTraceEventSerializerTest, EmptyProfile) { PerformanceTracer::getInstance(), notificationCallback, 10); auto profile = createEmptyProfile(); - auto tracingStartTime = HighResTimeStamp::now(); + auto tracingStartTime = std::chrono::steady_clock::now(); // Execute serializer.serializeAndNotify(profile, tracingStartTime); @@ -119,7 +119,7 @@ TEST_F( samples.emplace_back(createSample(timestamp3, threadId, callStack3)); auto profile = createProfileWithSamples(std::move(samples)); - auto tracingStartTime = HighResTimeStamp::now(); + auto tracingStartTime = std::chrono::steady_clock::now(); // Execute serializer.serializeAndNotify(profile, tracingStartTime); @@ -148,7 +148,7 @@ TEST_F(RuntimeSamplingProfileTraceEventSerializerTest, EmptySample) { samples.emplace_back(createSample(timestamp, threadId, emptyCallStack)); auto profile = createProfileWithSamples(std::move(samples)); - auto tracingStartTime = HighResTimeStamp::now(); + auto tracingStartTime = std::chrono::steady_clock::now(); // Mock the performance tracer methods folly::dynamic profileEvent = folly::dynamic::object; @@ -189,7 +189,7 @@ TEST_F( auto profile = createProfileWithSamples(std::move(samples)); - auto tracingStartTime = HighResTimeStamp::now(); + auto tracingStartTime = std::chrono::steady_clock::now(); // Execute serializer.serializeAndNotify(profile, tracingStartTime); @@ -228,7 +228,7 @@ TEST_F( } auto profile = createProfileWithSamples(std::move(samples)); - auto tracingStartTime = HighResTimeStamp::now(); + auto tracingStartTime = std::chrono::steady_clock::now(); // Execute serializer.serializeAndNotify(profile, tracingStartTime); @@ -269,7 +269,7 @@ TEST_F(RuntimeSamplingProfileTraceEventSerializerTest, ProfileChunkSizeLimit) { } auto profile = createProfileWithSamples(std::move(samples)); - auto tracingStartTime = HighResTimeStamp::now(); + auto tracingStartTime = std::chrono::steady_clock::now(); // Execute serializer.serializeAndNotify(profile, tracingStartTime); diff --git a/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.cpp b/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.cpp index 441409b8571..4f7090bdfcb 100644 --- a/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.cpp +++ b/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.cpp @@ -37,6 +37,10 @@ std::vector getSupportedEntryTypesInternal() { return supportedEntryTypes; } +uint64_t timestampToMicroseconds(DOMHighResTimeStamp timestamp) { + return static_cast(timestamp * 1000); +} + double performanceNow() { return chronoToDOMHighResTimeStamp(std::chrono::steady_clock::now()); } @@ -308,14 +312,13 @@ PerformanceResourceTiming PerformanceEntryReporter::reportResourceTiming( void PerformanceEntryReporter::traceMark(const PerformanceMark& entry) const { auto& performanceTracer = - jsinspector_modern::tracing::PerformanceTracer::getInstance(); + jsinspector_modern::PerformanceTracer::getInstance(); if (ReactPerfettoLogger::isTracing() || performanceTracer.isTracing()) { auto [trackName, eventName] = parseTrackName(entry.name); if (performanceTracer.isTracing()) { performanceTracer.reportMark( - entry.name, - HighResTimeStamp::fromDOMHighResTimeStamp(entry.startTime)); + entry.name, timestampToMicroseconds(entry.startTime)); } if (ReactPerfettoLogger::isTracing()) { @@ -327,7 +330,7 @@ void PerformanceEntryReporter::traceMark(const PerformanceMark& entry) const { void PerformanceEntryReporter::traceMeasure( const PerformanceMeasure& entry) const { auto& performanceTracer = - jsinspector_modern::tracing::PerformanceTracer::getInstance(); + jsinspector_modern::PerformanceTracer::getInstance(); if (performanceTracer.isTracing() || ReactPerfettoLogger::isTracing()) { auto [trackName, eventName] = parseTrackName(entry.name); @@ -340,8 +343,8 @@ void PerformanceEntryReporter::traceMeasure( } performanceTracer.reportMeasure( eventName, - HighResTimeStamp::fromDOMHighResTimeStamp(entry.startTime), - HighResDuration::fromDOMHighResTimeStamp(entry.duration), + timestampToMicroseconds(entry.startTime), + timestampToMicroseconds(entry.duration), trackMetadata); }