From 3da74f07990ec92e312f42be2b4119cd254eea6b Mon Sep 17 00:00:00 2001 From: Ruslan Lesiutin Date: Tue, 22 Jul 2025 04:34:50 -0700 Subject: [PATCH] Add method for collecting all events in a single container (#52734) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/52734 # Changelog: [Internal] We are going to need it at the top of the stack, once we will capture the Trace Events as part of the Tracing Profile for the whole Host. This is also would be used for always-on tracing. Reviewed By: sbuggay Differential Revision: D78660071 fbshipit-source-id: 4f876bed992b8a794e561940ad12405fef88cb62 --- .../tracing/PerformanceTracer.cpp | 30 +++++++++++++++++++ .../tracing/PerformanceTracer.h | 6 ++++ 2 files changed, 36 insertions(+) diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp b/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp index 3b848f20b31..03cb4dd6371 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp @@ -111,6 +111,36 @@ void PerformanceTracer::collectEvents( } } +folly::dynamic PerformanceTracer::collectEvents(uint16_t chunkSize) { + std::vector localBuffer; + { + std::lock_guard lock(mutex_); + buffer_.swap(localBuffer); + } + + auto chunks = folly::dynamic::array(); + if (localBuffer.empty()) { + return chunks; + } + + auto chunk = folly::dynamic::array(); + chunk.reserve(chunkSize); + for (auto&& event : localBuffer) { + chunk.push_back(serializeTraceEvent(std::move(event))); + + if (chunk.size() == chunkSize) { + chunks.push_back(std::move(chunk)); + chunk = folly::dynamic::array(); + chunk.reserve(chunkSize); + } + } + + if (!chunk.empty()) { + chunks.push_back(std::move(chunk)); + } + return chunks; +} + void PerformanceTracer::reportMark( const std::string_view& name, HighResTimeStamp start, diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h b/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h index 7623b773a25..d2673d7e43a 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h @@ -60,6 +60,12 @@ class PerformanceTracer { resultCallback, uint16_t chunkSize); + /** + * Flush out buffered CDP Trace Events into a folly::dynamic collection of + * chunks, which can be sent over CDP later. + */ + folly::dynamic collectEvents(uint16_t chunkSize); + /** * Record a `Performance.mark()` event - a labelled timestamp. If not * currently tracing, this is a no-op.