Skip to content

Commit 692356a

Browse files
hoxyqfacebook-github-bot
authored andcommitted
Create a primitive for timestamp, unify code (facebook#50585)
Summary: # Changelog: [Internal] We've been using same function that formats chrono to unix timestamp in microseconds in multiple different places. In this diff: - Introduced `TracingTimeStamp` that can be used in Tracing domain. - Moved the formatter to `chronoToTracingTimeStamp` in `react/timing/primitives.h` and re-used it. Differential Revision: D72649815
1 parent 0617acc commit 692356a

File tree

8 files changed

+41
-36
lines changed

8 files changed

+41
-36
lines changed

packages/react-native/ReactCommon/jsinspector-modern/tracing/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ target_include_directories(jsinspector_tracing PUBLIC ${REACT_COMMON_DIR})
1919
target_link_libraries(jsinspector_tracing
2020
folly_runtime
2121
oscompat
22+
react_timing
2223
)
2324
target_compile_reactnative_options(jsinspector_tracing PRIVATE)
2425
target_compile_options(jsinspector_tracing PRIVATE -Wpedantic)

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,13 @@
88
#include "EventLoopTaskReporter.h"
99

1010
#if defined(REACT_NATIVE_DEBUGGER_ENABLED)
11+
#include <react/timing/primitives.h>
1112
#include "PerformanceTracer.h"
1213
#endif
1314

1415
namespace facebook::react::jsinspector_modern::tracing {
1516

1617
#if defined(REACT_NATIVE_DEBUGGER_ENABLED)
17-
namespace {
18-
19-
inline uint64_t formatTimePointToUnixTimestamp(
20-
std::chrono::steady_clock::time_point timestamp) {
21-
return std::chrono::duration_cast<std::chrono::microseconds>(
22-
timestamp.time_since_epoch())
23-
.count();
24-
}
25-
26-
} // namespace
2718

2819
EventLoopTaskReporter::EventLoopTaskReporter()
2920
: startTimestamp_(std::chrono::steady_clock::now()) {}
@@ -33,8 +24,8 @@ EventLoopTaskReporter::~EventLoopTaskReporter() {
3324
if (performanceTracer.isTracing()) {
3425
auto end = std::chrono::steady_clock::now();
3526
performanceTracer.reportEventLoopTask(
36-
formatTimePointToUnixTimestamp(startTimestamp_),
37-
formatTimePointToUnixTimestamp(end));
27+
chronoToTracingTimeStamp(startTimestamp_),
28+
chronoToTracingTimeStamp(end));
3829
}
3930
}
4031

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "PerformanceTracer.h"
99

1010
#include <oscompat/OSCompat.h>
11+
#include <react/timing/primitives.h>
1112

1213
#include <folly/json.h>
1314

@@ -18,10 +19,8 @@ namespace facebook::react::jsinspector_modern {
1819

1920
namespace {
2021

21-
uint64_t getUnixTimestampOfNow() {
22-
return std::chrono::duration_cast<std::chrono::microseconds>(
23-
std::chrono::steady_clock::now().time_since_epoch())
24-
.count();
22+
inline TracingTimeStamp getTracingTimeStampOfNow() {
23+
return chronoToTracingTimeStamp(std::chrono::steady_clock::now());
2524
}
2625

2726
} // namespace
@@ -52,7 +51,7 @@ bool PerformanceTracer::startTracing() {
5251
.name = "TracingStartedInPage",
5352
.cat = "disabled-by-default-devtools.timeline",
5453
.ph = 'I',
55-
.ts = getUnixTimestampOfNow(),
54+
.ts = getTracingTimeStampOfNow(),
5655
.pid = processId_,
5756
.tid = oscompat::getCurrentThreadId(),
5857
.args = folly::dynamic::object("data", folly::dynamic::object()),
@@ -78,7 +77,7 @@ bool PerformanceTracer::stopTracing() {
7877
.name = "ReactNative-TracingStopped",
7978
.cat = "disabled-by-default-devtools.timeline",
8079
.ph = 'I',
81-
.ts = getUnixTimestampOfNow(),
80+
.ts = getTracingTimeStampOfNow(),
8281
.pid = processId_,
8382
.tid = oscompat::getCurrentThreadId(),
8483
});
@@ -117,7 +116,7 @@ void PerformanceTracer::collectEvents(
117116

118117
void PerformanceTracer::reportMark(
119118
const std::string_view& name,
120-
uint64_t start) {
119+
TracingTimeStamp start) {
121120
if (!tracing_) {
122121
return;
123122
}
@@ -139,7 +138,7 @@ void PerformanceTracer::reportMark(
139138

140139
void PerformanceTracer::reportMeasure(
141140
const std::string_view& name,
142-
uint64_t start,
141+
TracingTimeStamp start,
143142
uint64_t duration,
144143
const std::optional<DevToolsTrackEntryPayload>& trackMetadata) {
145144
if (!tracing_) {
@@ -243,7 +242,9 @@ void PerformanceTracer::reportThread(uint64_t id, const std::string& name) {
243242
});
244243
}
245244

246-
void PerformanceTracer::reportEventLoopTask(uint64_t start, uint64_t end) {
245+
void PerformanceTracer::reportEventLoopTask(
246+
TracingTimeStamp start,
247+
TracingTimeStamp end) {
247248
if (!tracing_) {
248249
return;
249250
}
@@ -286,7 +287,7 @@ folly::dynamic PerformanceTracer::getSerializedRuntimeProfileTraceEvent(
286287
folly::dynamic PerformanceTracer::getSerializedRuntimeProfileChunkTraceEvent(
287288
uint16_t profileId,
288289
uint64_t threadId,
289-
uint64_t eventUnixTimestamp,
290+
TracingTimeStamp eventUnixTimestamp,
290291
const tracing::TraceEventProfileChunk& traceEventProfileChunk) {
291292
return serializeTraceEvent(TraceEvent{
292293
.id = profileId,

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "TraceEvent.h"
1212
#include "TraceEventProfile.h"
1313

14+
#include <react/timing/primitives.h>
15+
1416
#include <folly/dynamic.h>
1517

1618
#include <functional>
@@ -65,7 +67,7 @@ class PerformanceTracer {
6567
*
6668
* See https://w3c.github.io/user-timing/#mark-method.
6769
*/
68-
void reportMark(const std::string_view& name, uint64_t start);
70+
void reportMark(const std::string_view& name, TracingTimeStamp start);
6971

7072
/**
7173
* Record a `Performance.measure()` event - a labelled duration. If not
@@ -75,7 +77,7 @@ class PerformanceTracer {
7577
*/
7678
void reportMeasure(
7779
const std::string_view& name,
78-
uint64_t start,
80+
TracingTimeStamp start,
7981
uint64_t duration,
8082
const std::optional<DevToolsTrackEntryPayload>& trackMetadata);
8183

@@ -99,7 +101,7 @@ class PerformanceTracer {
99101
* Record an Event Loop tick, which will be represented as an Event Loop task
100102
* on a timeline view and grouped with JavaScript samples.
101103
*/
102-
void reportEventLoopTask(uint64_t start, uint64_t end);
104+
void reportEventLoopTask(TracingTimeStamp start, TracingTimeStamp end);
103105

104106
/**
105107
* Create and serialize Profile Trace Event.
@@ -108,7 +110,7 @@ class PerformanceTracer {
108110
folly::dynamic getSerializedRuntimeProfileTraceEvent(
109111
uint64_t threadId,
110112
uint16_t profileId,
111-
uint64_t eventUnixTimestamp);
113+
TracingTimeStamp eventUnixTimestamp);
112114

113115
/**
114116
* Create and serialize ProfileChunk Trace Event.

packages/react-native/ReactCommon/jsinspector-modern/tracing/React-jsinspectortracing.podspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Pod::Spec.new do |s|
4747
end
4848

4949
s.dependency "React-oscompat"
50+
s.dependency "React-timing"
5051

5152
add_rn_third_party_dependencies(s)
5253
end

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "RuntimeSamplingProfileTraceEventSerializer.h"
99
#include "ProfileTreeNode.h"
1010

11+
#include <react/timing/primitives.h>
12+
1113
namespace facebook::react::jsinspector_modern::tracing {
1214

1315
namespace {
@@ -21,13 +23,6 @@ const uint16_t PROFILE_ID = 1;
2123
/// by Chrome DevTools.
2224
const uint32_t FALLBACK_SCRIPT_ID = 0;
2325

24-
uint64_t formatTimePointToUnixTimestamp(
25-
std::chrono::steady_clock::time_point timestamp) {
26-
return std::chrono::duration_cast<std::chrono::microseconds>(
27-
timestamp.time_since_epoch())
28-
.count();
29-
}
30-
3126
TraceEventProfileChunk::CPUProfile::Node convertToTraceEventProfileNode(
3227
std::shared_ptr<ProfileTreeNode> node) {
3328
ProfileTreeNode* nodeParent = node->getParent();
@@ -210,8 +205,8 @@ void RuntimeSamplingProfileTraceEventSerializer::serializeAndNotify(
210205
}
211206

212207
uint64_t firstChunkThreadId = samples.front().getThreadId();
213-
uint64_t tracingStartUnixTimestamp =
214-
formatTimePointToUnixTimestamp(tracingStartTime);
208+
TracingTimeStamp tracingStartUnixTimestamp =
209+
chronoToTracingTimeStamp(tracingStartTime);
215210
uint64_t previousSampleUnixTimestamp = tracingStartUnixTimestamp;
216211
uint64_t currentChunkUnixTimestamp = tracingStartUnixTimestamp;
217212

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#pragma once
99

10+
#include <react/timing/primitives.h>
11+
1012
#include <folly/dynamic.h>
1113

1214
namespace facebook::react::jsinspector_modern {
@@ -42,7 +44,7 @@ struct TraceEvent {
4244
char ph;
4345

4446
/** The tracing clock timestamp of the event, in microseconds (µs). */
45-
uint64_t ts;
47+
TracingTimeStamp ts;
4648

4749
/** The process ID for the process that output this event. */
4850
uint64_t pid;

packages/react-native/ReactCommon/react/timing/primitives.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ using DOMHighResTimeStamp = double;
1919

2020
constexpr DOMHighResTimeStamp DOM_HIGH_RES_TIME_STAMP_UNSET = -1.0;
2121

22+
// A unix time stamp in microseconds (μs) that is used in Tracing. All timeline
23+
// events of the Performance panel in DevTools are in this time base.
24+
// https://chromedevtools.github.io/devtools-protocol/tot/Tracing/
25+
using TracingTimeStamp = uint64_t;
26+
2227
inline DOMHighResTimeStamp chronoToDOMHighResTimeStamp(
2328
std::chrono::steady_clock::duration duration) {
2429
return static_cast<std::chrono::duration<double, std::milli>>(duration)
@@ -30,4 +35,11 @@ inline DOMHighResTimeStamp chronoToDOMHighResTimeStamp(
3035
return chronoToDOMHighResTimeStamp(timePoint.time_since_epoch());
3136
}
3237

38+
inline TracingTimeStamp chronoToTracingTimeStamp(
39+
std::chrono::steady_clock::time_point timePoint) {
40+
return std::chrono::duration_cast<std::chrono::microseconds>(
41+
timePoint.time_since_epoch())
42+
.count();
43+
}
44+
3345
} // namespace facebook::react

0 commit comments

Comments
 (0)