-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
tracing_helper.h
487 lines (394 loc) · 13.9 KB
/
tracing_helper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_COMMON_TRACING_HELPER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_COMMON_TRACING_HELPER_H_
#include <string>
#include <unordered_set>
#include "base/compiler_specific.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/typed_macros.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/perfetto/include/perfetto/tracing/event_context.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_value.h"
#include "third_party/perfetto/include/perfetto/tracing/track.h"
#include "third_party/perfetto/protos/perfetto/trace/track_event/track_event.pbzero.h"
namespace blink {
namespace scheduler {
// Available scheduler tracing categories for use with `StateTracer` and
// friends.
enum class TracingCategory { kTopLevel, kDefault, kInfo, kDebug };
namespace internal {
constexpr const char* TracingCategoryToString(TracingCategory category) {
switch (category) {
case TracingCategory::kTopLevel:
return "toplevel";
case TracingCategory::kDefault:
return "renderer.scheduler";
case TracingCategory::kInfo:
return TRACE_DISABLED_BY_DEFAULT("renderer.scheduler");
case TracingCategory::kDebug:
return TRACE_DISABLED_BY_DEFAULT("renderer.scheduler.debug");
}
return nullptr;
}
} // namespace internal
PLATFORM_EXPORT double TimeDeltaToMilliseconds(const base::TimeDelta& value);
PLATFORM_EXPORT const char* YesNoStateToString(bool is_yes);
PLATFORM_EXPORT
perfetto::protos::pbzero::RendererMainThreadTaskExecution::TaskType
TaskTypeToProto(TaskType task_type);
class TraceableVariable;
// Unfortunately, using |base::trace_event::TraceLog::EnabledStateObserver|
// wouldn't be helpful in our case because removing one takes linear time
// and tracers may be created and disposed frequently.
class PLATFORM_EXPORT TraceableVariableController {
DISALLOW_NEW();
public:
TraceableVariableController();
~TraceableVariableController();
// Not thread safe.
void RegisterTraceableVariable(TraceableVariable* traceable_variable);
void DeregisterTraceableVariable(TraceableVariable* traceable_variable);
void OnTraceLogEnabled();
private:
std::unordered_set<TraceableVariable*> traceable_variables_;
};
class TraceableVariable {
public:
TraceableVariable(TraceableVariableController* controller)
: controller_(controller) {
controller_->RegisterTraceableVariable(this);
}
virtual ~TraceableVariable() {
controller_->DeregisterTraceableVariable(this);
}
virtual void OnTraceLogEnabled() = 0;
private:
TraceableVariableController* const controller_; // Not owned.
};
// TRACE_EVENT macros define static variable to cache a pointer to the state
// of category. Hence, we need distinct version for each category in order to
// prevent unintended leak of state.
template <TracingCategory category>
class StateTracer {
DISALLOW_NEW();
public:
explicit StateTracer(const char* name) : name_(name), slice_is_open_(false) {}
StateTracer(const StateTracer&) = delete;
StateTracer& operator=(const StateTracer&) = delete;
~StateTracer() {
if (slice_is_open_) {
TRACE_EVENT_NESTABLE_ASYNC_END0(
internal::TracingCategoryToString(category), name_,
TRACE_ID_LOCAL(this));
}
}
// String will be copied before leaving this function.
void TraceString(const String& state) {
TraceImpl(state.Utf8().c_str(), true);
}
// Trace compile-time defined const string, so no copy needed.
// Null may be passed to indicate the absence of state.
void TraceCompileTimeString(const char* state) { TraceImpl(state, false); }
protected:
bool is_enabled() const {
bool result = false;
TRACE_EVENT_CATEGORY_GROUP_ENABLED(
internal::TracingCategoryToString(category), &result); // Cached.
return result;
}
private:
void TraceImpl(const char* state, bool need_copy) {
if (slice_is_open_) {
TRACE_EVENT_NESTABLE_ASYNC_END0(
internal::TracingCategoryToString(category), name_,
TRACE_ID_LOCAL(this));
slice_is_open_ = false;
}
if (!state || !is_enabled())
return;
if (need_copy) {
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(
internal::TracingCategoryToString(category), name_,
TRACE_ID_LOCAL(this), "state", TRACE_STR_COPY(state));
} else {
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(
internal::TracingCategoryToString(category), name_,
TRACE_ID_LOCAL(this), "state", state);
}
slice_is_open_ = true;
}
const char* const name_; // Not owned.
// We have to track whether slice is open to avoid confusion since assignment,
// "absent" state and OnTraceLogEnabled can happen anytime.
bool slice_is_open_;
};
// TODO(kraynov): Rename to something less generic and reflecting
// the enum nature of such variables.
template <typename T, TracingCategory category>
class TraceableState : public TraceableVariable, private StateTracer<category> {
public:
// Converter must return compile-time defined const strings because tracing
// will not make a copy of them.
using ConverterFuncPtr = const char* (*)(T);
TraceableState(T initial_state,
const char* name,
TraceableVariableController* controller,
ConverterFuncPtr converter)
: TraceableVariable(controller),
StateTracer<category>(name),
converter_(converter),
state_(initial_state) {
Trace();
}
TraceableState(const TraceableState&) = delete;
~TraceableState() override = default;
TraceableState& operator=(const T& value) {
Assign(value);
return *this;
}
TraceableState& operator=(const TraceableState& another) {
Assign(another.state_);
return *this;
}
operator T() const { return state_; }
const T& get() const { return state_; }
void OnTraceLogEnabled() final { Trace(); }
// TraceableState<T> is serialisable into trace iff T is serialisable.
template <typename V = T>
typename perfetto::check_traced_value_support<V>::type WriteIntoTrace(
perfetto::TracedValue context) const {
perfetto::WriteIntoTracedValue(std::move(context), get());
}
protected:
void Assign(T new_state) {
if (state_ != new_state) {
state_ = new_state;
Trace();
}
}
void (*mock_trace_for_test_)(const char*) = nullptr;
private:
void Trace() {
if (UNLIKELY(mock_trace_for_test_)) {
mock_trace_for_test_(converter_(state_));
return;
}
// Null state string means the absence of state.
const char* state_str = nullptr;
if (StateTracer<category>::is_enabled()) {
state_str = converter_(state_);
}
// We have to be explicit to deal with two-phase name lookup in templates:
// http://blog.llvm.org/2009/12/dreaded-two-phase-name-lookup.html
StateTracer<category>::TraceCompileTimeString(state_str);
}
const ConverterFuncPtr converter_;
T state_;
};
template <TracingCategory category, typename TypedValue>
class ProtoStateTracer {
DISALLOW_NEW();
public:
explicit ProtoStateTracer(const char* name)
: name_(name), slice_is_open_(false) {}
ProtoStateTracer(const ProtoStateTracer&) = delete;
ProtoStateTracer& operator=(const ProtoStateTracer&) = delete;
~ProtoStateTracer() {
if (slice_is_open_) {
TRACE_EVENT_END(internal::TracingCategoryToString(category), track());
}
}
void TraceProto(TypedValue* value) {
const auto trace_track = track();
if (slice_is_open_) {
TRACE_EVENT_END(internal::TracingCategoryToString(category), trace_track);
slice_is_open_ = false;
}
if (!is_enabled())
return;
TRACE_EVENT_BEGIN(internal::TracingCategoryToString(category),
perfetto::StaticString{name_}, trace_track,
[value](perfetto::EventContext ctx) {
value->AsProtozeroInto(ctx.event());
});
slice_is_open_ = true;
}
protected:
bool is_enabled() const {
bool result = false;
TRACE_EVENT_CATEGORY_GROUP_ENABLED(
internal::TracingCategoryToString(category), &result); // Cached.
return result;
}
private:
perfetto::Track track() const {
return perfetto::Track(reinterpret_cast<uint64_t>(this));
}
const char* const name_; // Not owned.
// We have to track whether slice is open to avoid confusion since assignment,
// "absent" state and OnTraceLogEnabled can happen anytime.
bool slice_is_open_;
};
template <typename T>
using InitializeProtoFuncPtr =
void (*)(perfetto::protos::pbzero::TrackEvent* event, T e);
template <typename T, TracingCategory category>
class TraceableObjectState
: public TraceableVariable,
public ProtoStateTracer<category, TraceableObjectState<T, category>> {
public:
TraceableObjectState(T initial_state,
const char* name,
TraceableVariableController* controller,
InitializeProtoFuncPtr<T> proto_init_func)
: TraceableVariable(controller),
ProtoStateTracer<category, TraceableObjectState<T, category>>(name),
state_(initial_state),
proto_init_func_(proto_init_func) {
Trace();
}
TraceableObjectState(const TraceableObjectState&) = delete;
~TraceableObjectState() override = default;
TraceableObjectState& operator=(const T& value) {
Assign(value);
return *this;
}
TraceableObjectState& operator=(const TraceableObjectState& another) {
Assign(another.state_);
return *this;
}
const T& get() const { return state_; }
void OnTraceLogEnabled() final { Trace(); }
void AsProtozeroInto(perfetto::protos::pbzero::TrackEvent* event) {
proto_init_func_(event, state_);
}
protected:
void Assign(T new_state) {
if (state_ != new_state) {
state_ = new_state;
Trace();
}
}
private:
void Trace() {
ProtoStateTracer<category, TraceableObjectState<T, category>>::TraceProto(
this);
}
bool is_enabled() const {
bool result = false;
TRACE_EVENT_CATEGORY_GROUP_ENABLED(
internal::TracingCategoryToString(category), &result); // Cached.
return result;
}
T state_;
InitializeProtoFuncPtr<T> proto_init_func_;
};
template <typename T, TracingCategory category>
class TraceableCounter : public TraceableVariable {
public:
using ConverterFuncPtr = double (*)(const T&);
TraceableCounter(T initial_value,
const char* name,
TraceableVariableController* controller,
ConverterFuncPtr converter)
: TraceableVariable(controller),
name_(name),
converter_(converter),
value_(initial_value) {
Trace();
}
TraceableCounter(T initial_value,
const char* name,
TraceableVariableController* controller)
: TraceableCounter(initial_value, name, controller, [](const T& value) {
return static_cast<double>(value);
}) {}
TraceableCounter(const TraceableCounter&) = delete;
TraceableCounter& operator=(const T& value) {
value_ = value;
Trace();
return *this;
}
TraceableCounter& operator=(const TraceableCounter& another) {
value_ = another.value_;
Trace();
return *this;
}
TraceableCounter& operator+=(const T& value) {
value_ += value;
Trace();
return *this;
}
TraceableCounter& operator-=(const T& value) {
value_ -= value;
Trace();
return *this;
}
const T& value() const { return value_; }
const T* operator->() const { return &value_; }
operator T() const { return value_; }
void OnTraceLogEnabled() final { Trace(); }
void Trace() const {
TRACE_COUNTER_ID1(internal::TracingCategoryToString(category), name_, this,
converter_(value_));
}
private:
const char* const name_; // Not owned.
const ConverterFuncPtr converter_;
T value_;
};
// Add operators when it's needed.
template <typename T, TracingCategory category>
constexpr T operator-(const TraceableCounter<T, category>& counter) {
return -counter.value();
}
template <typename T, TracingCategory category>
constexpr T operator/(const TraceableCounter<T, category>& lhs, const T& rhs) {
return lhs.value() / rhs;
}
template <typename T, TracingCategory category>
constexpr bool operator>(const TraceableCounter<T, category>& lhs,
const T& rhs) {
return lhs.value() > rhs;
}
template <typename T, TracingCategory category>
constexpr bool operator<(const TraceableCounter<T, category>& lhs,
const T& rhs) {
return lhs.value() < rhs;
}
template <typename T, TracingCategory category>
constexpr bool operator!=(const TraceableCounter<T, category>& lhs,
const T& rhs) {
return lhs.value() != rhs;
}
template <typename T, TracingCategory category>
constexpr T operator++(TraceableCounter<T, category>& counter) {
counter = counter.value() + 1;
return counter.value();
}
template <typename T, TracingCategory category>
constexpr T operator--(TraceableCounter<T, category>& counter) {
counter = counter.value() - 1;
return counter.value();
}
template <typename T, TracingCategory category>
constexpr T operator++(TraceableCounter<T, category>& counter, int) {
T value = counter.value();
counter = value + 1;
return value;
}
template <typename T, TracingCategory category>
constexpr T operator--(TraceableCounter<T, category>& counter, int) {
T value = counter.value();
counter = value - 1;
return value;
}
} // namespace scheduler
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_COMMON_TRACING_HELPER_H_