@@ -28,18 +28,20 @@ PerformanceTracer::PerformanceTracer()
28
28
29
29
bool PerformanceTracer::startTracing () {
30
30
{
31
- std::lock_guard lock (mutex_ );
32
- if (tracing_ ) {
31
+ std::lock_guard lock (tracingMutex_ );
32
+ if (isTracing () ) {
33
33
return false ;
34
34
}
35
-
36
35
tracing_ = true ;
37
36
}
38
37
39
38
reportProcess (processId_, " React Native" );
40
39
41
40
{
42
- std::lock_guard lock (mutex_);
41
+ std::lock_guard lock (tracingMutex_);
42
+ if (!isTracing ()) {
43
+ return false ;
44
+ }
43
45
buffer_.push_back (TraceEvent{
44
46
.name = " TracingStartedInPage" ,
45
47
.cat = " disabled-by-default-devtools.timeline" ,
@@ -49,16 +51,17 @@ bool PerformanceTracer::startTracing() {
49
51
.tid = oscompat::getCurrentThreadId (),
50
52
.args = folly::dynamic::object (" data" , folly::dynamic::object ()),
51
53
});
52
-
53
- return true ;
54
54
}
55
+
56
+ return true ;
55
57
}
56
58
57
59
bool PerformanceTracer::stopTracing () {
58
- std::lock_guard lock (mutex_ );
59
- if (!tracing_ ) {
60
+ std::lock_guard lock (tracingMutex_ );
61
+ if (!isTracing () ) {
60
62
return false ;
61
63
}
64
+ tracing_ = false ;
62
65
63
66
// This is synthetic Trace Event, which should not be represented on a
64
67
// timeline. CDT is not using Profile or ProfileChunk events for determining
@@ -75,16 +78,16 @@ bool PerformanceTracer::stopTracing() {
75
78
.tid = oscompat::getCurrentThreadId (),
76
79
});
77
80
81
+ // Potential increments of this counter are covered by tracing_ atomic flag.
78
82
performanceMeasureCount_ = 0 ;
79
- tracing_ = false ;
80
83
return true ;
81
84
}
82
85
83
86
void PerformanceTracer::collectEvents (
84
87
const std::function<void (const folly::dynamic& eventsChunk)>&
85
88
resultCallback,
86
89
uint16_t chunkSize) {
87
- std::lock_guard lock (mutex_ );
90
+ std::lock_guard lock (tracingMutex_ );
88
91
89
92
if (buffer_.empty ()) {
90
93
return ;
@@ -110,12 +113,12 @@ void PerformanceTracer::collectEvents(
110
113
void PerformanceTracer::reportMark (
111
114
const std::string_view& name,
112
115
HighResTimeStamp start) {
113
- if (!tracing_ ) {
116
+ if (!isTracing () ) {
114
117
return ;
115
118
}
116
119
117
- std::lock_guard<std::mutex> lock (mutex_ );
118
- if (!tracing_ ) {
120
+ std::lock_guard<std::mutex> lock (tracingMutex_ );
121
+ if (!isTracing () ) {
119
122
return ;
120
123
}
121
124
@@ -134,12 +137,7 @@ void PerformanceTracer::reportMeasure(
134
137
HighResTimeStamp start,
135
138
HighResDuration duration,
136
139
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 ()) {
143
141
return ;
144
142
}
145
143
@@ -152,10 +150,16 @@ void PerformanceTracer::reportMeasure(
152
150
folly::dynamic::object (" detail" , folly::toJson (devtoolsObject));
153
151
}
154
152
155
- ++performanceMeasureCount_;
156
153
auto currentThreadId = oscompat::getCurrentThreadId ();
154
+
155
+ std::lock_guard<std::mutex> lock (tracingMutex_);
156
+ if (!isTracing ()) {
157
+ return ;
158
+ }
159
+ auto eventId = ++performanceMeasureCount_;
160
+
157
161
buffer_.push_back (TraceEvent{
158
- .id = performanceMeasureCount_ ,
162
+ .id = eventId ,
159
163
.name = std::string (name),
160
164
.cat = " blink.user_timing" ,
161
165
.ph = ' b' ,
@@ -165,7 +169,7 @@ void PerformanceTracer::reportMeasure(
165
169
.args = beginEventArgs,
166
170
});
167
171
buffer_.push_back (TraceEvent{
168
- .id = performanceMeasureCount_ ,
172
+ .id = eventId ,
169
173
.name = std::string (name),
170
174
.cat = " blink.user_timing" ,
171
175
.ph = ' e' ,
@@ -176,12 +180,12 @@ void PerformanceTracer::reportMeasure(
176
180
}
177
181
178
182
void PerformanceTracer::reportProcess (uint64_t id, const std::string& name) {
179
- if (!tracing_ ) {
183
+ if (!isTracing () ) {
180
184
return ;
181
185
}
182
186
183
- std::lock_guard<std::mutex> lock (mutex_ );
184
- if (!tracing_ ) {
187
+ std::lock_guard<std::mutex> lock (tracingMutex_ );
188
+ if (!isTracing () ) {
185
189
return ;
186
190
}
187
191
@@ -201,12 +205,12 @@ void PerformanceTracer::reportJavaScriptThread() {
201
205
}
202
206
203
207
void PerformanceTracer::reportThread (uint64_t id, const std::string& name) {
204
- if (!tracing_ ) {
208
+ if (!isTracing () ) {
205
209
return ;
206
210
}
207
211
208
- std::lock_guard<std::mutex> lock (mutex_ );
209
- if (!tracing_ ) {
212
+ std::lock_guard<std::mutex> lock (tracingMutex_ );
213
+ if (!isTracing () ) {
210
214
return ;
211
215
}
212
216
@@ -238,12 +242,12 @@ void PerformanceTracer::reportThread(uint64_t id, const std::string& name) {
238
242
void PerformanceTracer::reportEventLoopTask (
239
243
HighResTimeStamp start,
240
244
HighResTimeStamp end) {
241
- if (!tracing_ ) {
245
+ if (!isTracing () ) {
242
246
return ;
243
247
}
244
248
245
- std::lock_guard lock (mutex_ );
246
- if (!tracing_ ) {
249
+ std::lock_guard<std::mutex> lock (tracingMutex_ );
250
+ if (!isTracing () ) {
247
251
return ;
248
252
}
249
253
@@ -261,12 +265,12 @@ void PerformanceTracer::reportEventLoopTask(
261
265
void PerformanceTracer::reportEventLoopMicrotasks (
262
266
HighResTimeStamp start,
263
267
HighResTimeStamp end) {
264
- if (!tracing_ ) {
268
+ if (!isTracing () ) {
265
269
return ;
266
270
}
267
271
268
- std::lock_guard lock (mutex_ );
269
- if (!tracing_ ) {
272
+ std::lock_guard<std::mutex> lock (tracingMutex_ );
273
+ if (!isTracing () ) {
270
274
return ;
271
275
}
272
276
0 commit comments