Skip to content

Commit 8bf29ed

Browse files
committed
refactor: well-defined behaviour (facebook#52187)
Summary: Pull Request resolved: facebook#52187 # Changelog: [Internal] - `tracing_` is now `std::atomic<bool>`. - `performanceMeasureCount_` is now`std::atomic<uint32_t>`. - `mutex_` -> `tracingMutex_`. The biggest change is that we no longer read from `tracing_` directly for an early-return when not tracing. This should not significantly regress performance of the subsystem, see the test plan for numbers. Differential Revision: D77053030
1 parent 60967fd commit 8bf29ed

File tree

2 files changed

+46
-41
lines changed

2 files changed

+46
-41
lines changed

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

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,20 @@ PerformanceTracer::PerformanceTracer()
2828

2929
bool PerformanceTracer::startTracing() {
3030
{
31-
std::lock_guard lock(mutex_);
32-
if (tracing_) {
31+
std::lock_guard lock(tracingMutex_);
32+
if (isTracing()) {
3333
return false;
3434
}
35-
3635
tracing_ = true;
3736
}
3837

3938
reportProcess(processId_, "React Native");
4039

4140
{
42-
std::lock_guard lock(mutex_);
41+
std::lock_guard lock(tracingMutex_);
42+
if (!isTracing()) {
43+
return false;
44+
}
4345
buffer_.push_back(TraceEvent{
4446
.name = "TracingStartedInPage",
4547
.cat = "disabled-by-default-devtools.timeline",
@@ -49,16 +51,17 @@ bool PerformanceTracer::startTracing() {
4951
.tid = oscompat::getCurrentThreadId(),
5052
.args = folly::dynamic::object("data", folly::dynamic::object()),
5153
});
52-
53-
return true;
5454
}
55+
56+
return true;
5557
}
5658

5759
bool PerformanceTracer::stopTracing() {
58-
std::lock_guard lock(mutex_);
59-
if (!tracing_) {
60+
std::lock_guard lock(tracingMutex_);
61+
if (!isTracing()) {
6062
return false;
6163
}
64+
tracing_ = false;
6265

6366
// This is synthetic Trace Event, which should not be represented on a
6467
// timeline. CDT is not using Profile or ProfileChunk events for determining
@@ -75,16 +78,16 @@ bool PerformanceTracer::stopTracing() {
7578
.tid = oscompat::getCurrentThreadId(),
7679
});
7780

81+
// Potential increments of this counter are covered by tracing_ atomic flag.
7882
performanceMeasureCount_ = 0;
79-
tracing_ = false;
8083
return true;
8184
}
8285

8386
void PerformanceTracer::collectEvents(
8487
const std::function<void(const folly::dynamic& eventsChunk)>&
8588
resultCallback,
8689
uint16_t chunkSize) {
87-
std::lock_guard lock(mutex_);
90+
std::lock_guard lock(tracingMutex_);
8891

8992
if (buffer_.empty()) {
9093
return;
@@ -110,12 +113,12 @@ void PerformanceTracer::collectEvents(
110113
void PerformanceTracer::reportMark(
111114
const std::string_view& name,
112115
HighResTimeStamp start) {
113-
if (!tracing_) {
116+
if (!isTracing()) {
114117
return;
115118
}
116119

117-
std::lock_guard<std::mutex> lock(mutex_);
118-
if (!tracing_) {
120+
std::lock_guard<std::mutex> lock(tracingMutex_);
121+
if (!isTracing()) {
119122
return;
120123
}
121124

@@ -134,12 +137,7 @@ void PerformanceTracer::reportMeasure(
134137
HighResTimeStamp start,
135138
HighResDuration duration,
136139
const std::optional<DevToolsTrackEntryPayload>& trackMetadata) {
137-
if (!tracing_) {
138-
return;
139-
}
140-
141-
std::lock_guard<std::mutex> lock(mutex_);
142-
if (!tracing_) {
140+
if (!isTracing()) {
143141
return;
144142
}
145143

@@ -152,10 +150,16 @@ void PerformanceTracer::reportMeasure(
152150
folly::dynamic::object("detail", folly::toJson(devtoolsObject));
153151
}
154152

155-
++performanceMeasureCount_;
156153
auto currentThreadId = oscompat::getCurrentThreadId();
154+
155+
std::lock_guard<std::mutex> lock(tracingMutex_);
156+
if (!isTracing()) {
157+
return;
158+
}
159+
auto eventId = ++performanceMeasureCount_;
160+
157161
buffer_.push_back(TraceEvent{
158-
.id = performanceMeasureCount_,
162+
.id = eventId,
159163
.name = std::string(name),
160164
.cat = "blink.user_timing",
161165
.ph = 'b',
@@ -165,7 +169,7 @@ void PerformanceTracer::reportMeasure(
165169
.args = beginEventArgs,
166170
});
167171
buffer_.push_back(TraceEvent{
168-
.id = performanceMeasureCount_,
172+
.id = eventId,
169173
.name = std::string(name),
170174
.cat = "blink.user_timing",
171175
.ph = 'e',
@@ -176,12 +180,12 @@ void PerformanceTracer::reportMeasure(
176180
}
177181

178182
void PerformanceTracer::reportProcess(uint64_t id, const std::string& name) {
179-
if (!tracing_) {
183+
if (!isTracing()) {
180184
return;
181185
}
182186

183-
std::lock_guard<std::mutex> lock(mutex_);
184-
if (!tracing_) {
187+
std::lock_guard<std::mutex> lock(tracingMutex_);
188+
if (!isTracing()) {
185189
return;
186190
}
187191

@@ -201,12 +205,12 @@ void PerformanceTracer::reportJavaScriptThread() {
201205
}
202206

203207
void PerformanceTracer::reportThread(uint64_t id, const std::string& name) {
204-
if (!tracing_) {
208+
if (!isTracing()) {
205209
return;
206210
}
207211

208-
std::lock_guard<std::mutex> lock(mutex_);
209-
if (!tracing_) {
212+
std::lock_guard<std::mutex> lock(tracingMutex_);
213+
if (!isTracing()) {
210214
return;
211215
}
212216

@@ -238,12 +242,12 @@ void PerformanceTracer::reportThread(uint64_t id, const std::string& name) {
238242
void PerformanceTracer::reportEventLoopTask(
239243
HighResTimeStamp start,
240244
HighResTimeStamp end) {
241-
if (!tracing_) {
245+
if (!isTracing()) {
242246
return;
243247
}
244248

245-
std::lock_guard lock(mutex_);
246-
if (!tracing_) {
249+
std::lock_guard<std::mutex> lock(tracingMutex_);
250+
if (!isTracing()) {
247251
return;
248252
}
249253

@@ -261,12 +265,12 @@ void PerformanceTracer::reportEventLoopTask(
261265
void PerformanceTracer::reportEventLoopMicrotasks(
262266
HighResTimeStamp start,
263267
HighResTimeStamp end) {
264-
if (!tracing_) {
268+
if (!isTracing()) {
265269
return;
266270
}
267271

268-
std::lock_guard lock(mutex_);
269-
if (!tracing_) {
272+
std::lock_guard<std::mutex> lock(tracingMutex_);
273+
if (!isTracing()) {
270274
return;
271275
}
272276

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <react/timing/primitives.h>
1515

1616
#include <folly/dynamic.h>
17+
#include <atomic>
1718
#include <functional>
1819
#include <mutex>
1920
#include <optional>
@@ -47,9 +48,7 @@ class PerformanceTracer {
4748
* avoid doing expensive work (like formatting strings) if tracing is not
4849
* enabled.
4950
*/
50-
bool isTracing() const {
51-
// This is not thread safe but it's only a performance optimization. The
52-
// call to report marks and measures is already thread safe.
51+
inline bool isTracing() const {
5352
return tracing_;
5453
}
5554

@@ -135,12 +134,14 @@ class PerformanceTracer {
135134

136135
folly::dynamic serializeTraceEvent(const TraceEvent& event) const;
137136

138-
bool tracing_{false};
139-
140137
uint64_t processId_;
141-
uint32_t performanceMeasureCount_{0};
138+
139+
std::atomic<bool> tracing_{false};
140+
std::atomic<uint32_t> performanceMeasureCount_{0};
141+
142142
std::vector<TraceEvent> buffer_;
143-
std::mutex mutex_;
143+
// Protects buffer_ operations and tracing_ modifications.
144+
std::mutex tracingMutex_;
144145
};
145146

146147
} // namespace facebook::react::jsinspector_modern::tracing

0 commit comments

Comments
 (0)