Skip to content

Commit 7488097

Browse files
hoxyqfacebook-github-bot
authored andcommitted
Extract TraceEvent serialization logic (#52869)
Summary: Pull Request resolved: #52869 # Changelog: [Internal] We are exctracting a logic for serialization of TraceEvent into a dedicated serializer class. Conceptually: - PerformanceTracer would be a local TraceEvent engine that is responsible for constructing and buffering TraceEvents. - TraceEventSerializer will have a single responsibility: transforming from local structs to serialized json objects that are ready to be dispatched over CDP Tracing domain. This would help avoid scenarios, where we are passing around `folly:dynamic` between internal subsystems: serialization should only happen right before emtting a CDP message. Reviewed By: huntie Differential Revision: D78738370 fbshipit-source-id: a8e33e857192a02dc048d504abe072679ef8ce82
1 parent 75d6cb1 commit 7488097

File tree

4 files changed

+75
-36
lines changed

4 files changed

+75
-36
lines changed

packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
#include "PerformanceTracer.h"
99
#include "Timing.h"
10+
#include "TraceEventSerializer.h"
1011

1112
#include <oscompat/OSCompat.h>
1213
#include <react/timing/primitives.h>
1314

1415
#include <folly/json.h>
1516

16-
#include <array>
1717
#include <mutex>
1818

1919
namespace facebook::react::jsinspector_modern::tracing {
@@ -98,7 +98,8 @@ void PerformanceTracer::collectEvents(
9898
auto serializedTraceEvents = folly::dynamic::array();
9999
for (auto&& event : localBuffer) {
100100
// Emit trace events
101-
serializedTraceEvents.push_back(serializeTraceEvent(std::move(event)));
101+
serializedTraceEvents.push_back(
102+
TraceEventSerializer::serialize(std::move(event)));
102103

103104
if (serializedTraceEvents.size() == chunkSize) {
104105
resultCallback(std::move(serializedTraceEvents));
@@ -125,7 +126,7 @@ folly::dynamic PerformanceTracer::collectEvents(uint16_t chunkSize) {
125126
auto chunk = folly::dynamic::array();
126127
chunk.reserve(chunkSize);
127128
for (auto&& event : localBuffer) {
128-
chunk.push_back(serializeTraceEvent(std::move(event)));
129+
chunk.push_back(TraceEventSerializer::serialize(std::move(event)));
129130

130131
if (chunk.size() == chunkSize) {
131132
chunks.push_back(std::move(chunk));
@@ -380,7 +381,7 @@ folly::dynamic PerformanceTracer::getSerializedRuntimeProfileTraceEvent(
380381
HighResTimeStamp profileTimestamp) {
381382
// CDT prioritizes event timestamp over startTime metadata field.
382383
// https://fburl.com/lo764pf4
383-
return serializeTraceEvent(TraceEvent{
384+
return TraceEventSerializer::serialize(TraceEvent{
384385
.id = profileId,
385386
.name = "Profile",
386387
.cat = "disabled-by-default-v8.cpu_profiler",
@@ -401,7 +402,7 @@ folly::dynamic PerformanceTracer::getSerializedRuntimeProfileChunkTraceEvent(
401402
uint64_t threadId,
402403
HighResTimeStamp chunkTimestamp,
403404
const tracing::TraceEventProfileChunk& traceEventProfileChunk) {
404-
return serializeTraceEvent(TraceEvent{
405+
return TraceEventSerializer::serialize(TraceEvent{
405406
.id = profileId,
406407
.name = "ProfileChunk",
407408
.cat = "disabled-by-default-v8.cpu_profiler",
@@ -414,27 +415,4 @@ folly::dynamic PerformanceTracer::getSerializedRuntimeProfileChunkTraceEvent(
414415
});
415416
}
416417

417-
folly::dynamic PerformanceTracer::serializeTraceEvent(
418-
TraceEvent&& event) const {
419-
folly::dynamic result = folly::dynamic::object;
420-
421-
if (event.id.has_value()) {
422-
std::array<char, 16> buffer{};
423-
snprintf(buffer.data(), buffer.size(), "0x%x", event.id.value());
424-
result["id"] = buffer.data();
425-
}
426-
result["name"] = std::move(event.name);
427-
result["cat"] = std::move(event.cat);
428-
result["ph"] = std::string(1, event.ph);
429-
result["ts"] = highResTimeStampToTracingClockTimeStamp(event.ts);
430-
result["pid"] = event.pid;
431-
result["tid"] = event.tid;
432-
result["args"] = std::move(event.args);
433-
if (event.dur.has_value()) {
434-
result["dur"] = highResDurationToTracingClockDuration(event.dur.value());
435-
}
436-
437-
return result;
438-
}
439-
440418
} // namespace facebook::react::jsinspector_modern::tracing

packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,6 @@ class PerformanceTracer {
156156
PerformanceTracer& operator=(const PerformanceTracer&) = delete;
157157
~PerformanceTracer() = default;
158158

159-
/**
160-
* Serialize a TraceEvent into a folly::dynamic object.
161-
* \param event rvalue reference to the TraceEvent object.
162-
* \return folly::dynamic object that represents a serialized into JSON Trace
163-
* Event for CDP.
164-
*/
165-
folly::dynamic serializeTraceEvent(TraceEvent&& event) const;
166-
167159
const uint64_t processId_;
168160

169161
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "TraceEventSerializer.h"
9+
#include "Timing.h"
10+
11+
namespace facebook::react::jsinspector_modern::tracing {
12+
13+
/* static */ folly::dynamic TraceEventSerializer::serialize(
14+
TraceEvent&& event) {
15+
folly::dynamic result = folly::dynamic::object;
16+
17+
if (event.id.has_value()) {
18+
std::array<char, 16> buffer{};
19+
snprintf(buffer.data(), buffer.size(), "0x%x", event.id.value());
20+
result["id"] = buffer.data();
21+
}
22+
result["name"] = std::move(event.name);
23+
result["cat"] = std::move(event.cat);
24+
result["ph"] = std::string(1, event.ph);
25+
result["ts"] = highResTimeStampToTracingClockTimeStamp(event.ts);
26+
result["pid"] = event.pid;
27+
result["tid"] = event.tid;
28+
result["args"] = std::move(event.args);
29+
if (event.dur.has_value()) {
30+
result["dur"] = highResDurationToTracingClockDuration(event.dur.value());
31+
}
32+
33+
return result;
34+
}
35+
36+
} // namespace facebook::react::jsinspector_modern::tracing
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include "TraceEvent.h"
11+
12+
#include <folly/dynamic.h>
13+
14+
namespace facebook::react::jsinspector_modern::tracing {
15+
16+
/**
17+
* This class is only responsible for serializing a local TraceEvent
18+
* representation into JSON, that should be ready to be dispatched over the
19+
* protocol.
20+
*/
21+
class TraceEventSerializer {
22+
public:
23+
/**
24+
* Serializes a TraceEvent to a folly::dynamic object.
25+
*
26+
* \param event rvalue reference to the TraceEvent object.
27+
* \return A folly::dynamic object that represents a serialized into JSON
28+
* Trace Event for CDP.
29+
*/
30+
static folly::dynamic serialize(TraceEvent&& event);
31+
};
32+
33+
} // namespace facebook::react::jsinspector_modern::tracing

0 commit comments

Comments
 (0)