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
cc / metrics / event_latency_tracing_recorder.cc [blame]
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/metrics/event_latency_tracing_recorder.h"
#include "base/feature_list.h"
#include "base/notreached.h"
#include "base/ranges/algorithm.h"
#include "base/time/time.h"
#include "base/trace_event/trace_id_helper.h"
#include "base/trace_event/typed_macros.h"
#include "base/tracing/protos/chrome_track_event.pbzero.h"
#include "cc/base/features.h"
#include "cc/metrics/event_metrics.h"
#include "third_party/perfetto/include/perfetto/tracing/track.h"
namespace cc {
namespace {
constexpr char kTracingCategory[] = "cc,benchmark,input,input.scrolling";
bool IsTracingEnabled() {
bool enabled;
TRACE_EVENT_CATEGORY_GROUP_ENABLED(kTracingCategory, &enabled);
return enabled;
}
constexpr base::TimeDelta high_latency_threshold = base::Milliseconds(90);
constexpr perfetto::protos::pbzero::EventLatency::EventType ToProtoEnum(
EventMetrics::EventType event_type) {
#define CASE(event_type, proto_event_type) \
case EventMetrics::EventType::event_type: \
return perfetto::protos::pbzero::EventLatency::proto_event_type
switch (event_type) {
CASE(kMousePressed, MOUSE_PRESSED);
CASE(kMouseReleased, MOUSE_RELEASED);
CASE(kMouseWheel, MOUSE_WHEEL);
CASE(kKeyPressed, KEY_PRESSED);
CASE(kKeyReleased, KEY_RELEASED);
CASE(kTouchPressed, TOUCH_PRESSED);
CASE(kTouchReleased, TOUCH_RELEASED);
CASE(kTouchMoved, TOUCH_MOVED);
CASE(kGestureScrollBegin, GESTURE_SCROLL_BEGIN);
CASE(kGestureScrollUpdate, GESTURE_SCROLL_UPDATE);
CASE(kGestureScrollEnd, GESTURE_SCROLL_END);
CASE(kGestureDoubleTap, GESTURE_DOUBLE_TAP);
CASE(kGestureLongPress, GESTURE_LONG_PRESS);
CASE(kGestureLongTap, GESTURE_LONG_TAP);
CASE(kGestureShowPress, GESTURE_SHOW_PRESS);
CASE(kGestureTap, GESTURE_TAP);
CASE(kGestureTapCancel, GESTURE_TAP_CANCEL);
CASE(kGestureTapDown, GESTURE_TAP_DOWN);
CASE(kGestureTapUnconfirmed, GESTURE_TAP_UNCONFIRMED);
CASE(kGestureTwoFingerTap, GESTURE_TWO_FINGER_TAP);
CASE(kFirstGestureScrollUpdate, FIRST_GESTURE_SCROLL_UPDATE);
CASE(kMouseDragged, MOUSE_DRAGGED);
CASE(kGesturePinchBegin, GESTURE_PINCH_BEGIN);
CASE(kGesturePinchEnd, GESTURE_PINCH_END);
CASE(kGesturePinchUpdate, GESTURE_PINCH_UPDATE);
CASE(kInertialGestureScrollUpdate, INERTIAL_GESTURE_SCROLL_UPDATE);
CASE(kMouseMoved, MOUSE_MOVED_EVENT);
}
}
const char* GetVizBreakdownToPresentationName(
CompositorFrameReporter::VizBreakdown breakdown) {
switch (breakdown) {
case CompositorFrameReporter::VizBreakdown::kSwapStartToSwapEnd:
return "SwapStartToPresentation";
case CompositorFrameReporter::VizBreakdown::kLatchToSwapEnd:
return "LatchToPresentation";
default:
base::UmaHistogramEnumeration(
"Compositing.VizBreakdownToPresentationUnexpected", breakdown);
return "Unknown";
}
}
} // namespace
// static
const char* EventLatencyTracingRecorder::GetDispatchBreakdownName(
EventMetrics::DispatchStage start_stage,
EventMetrics::DispatchStage end_stage) {
switch (start_stage) {
case EventMetrics::DispatchStage::kGenerated:
switch (end_stage) {
case EventMetrics::DispatchStage::
kScrollsBlockingTouchDispatchedToRenderer:
case EventMetrics::DispatchStage::kArrivedInBrowserMain:
return "GenerationToBrowserMain";
case EventMetrics::DispatchStage::kArrivedInRendererCompositor:
return "GenerationToRendererCompositor";
default:
NOTREACHED() << static_cast<int>(end_stage);
}
case EventMetrics::DispatchStage::kScrollsBlockingTouchDispatchedToRenderer:
switch (end_stage) {
case EventMetrics::DispatchStage::kArrivedInBrowserMain:
// This stage can only be in a Scroll EventLatency. It means a path of
// a corresponding blocking TouchMove from BrowserMain To Renderer To
// BrowserMain. Look at the corresponding TouchMove EventLatency for
// a more detailed breakdown of this stage.
return "TouchRendererHandlingToBrowserMain";
default:
NOTREACHED() << static_cast<int>(end_stage);
}
case EventMetrics::DispatchStage::kArrivedInBrowserMain:
DCHECK_EQ(end_stage,
EventMetrics::DispatchStage::kArrivedInRendererCompositor);
return "BrowserMainToRendererCompositor";
case EventMetrics::DispatchStage::kArrivedInRendererCompositor:
switch (end_stage) {
case EventMetrics::DispatchStage::kRendererCompositorStarted:
return "RendererCompositorQueueingDelay";
case EventMetrics::DispatchStage::kRendererMainStarted:
return "RendererCompositorToMain";
default:
NOTREACHED() << static_cast<int>(end_stage);
}
case EventMetrics::DispatchStage::kRendererCompositorStarted:
DCHECK_EQ(end_stage,
EventMetrics::DispatchStage::kRendererCompositorFinished);
return "RendererCompositorProcessing";
case EventMetrics::DispatchStage::kRendererCompositorFinished:
DCHECK_EQ(end_stage, EventMetrics::DispatchStage::kRendererMainStarted);
return "RendererCompositorToMain";
case EventMetrics::DispatchStage::kRendererMainStarted:
DCHECK_EQ(end_stage, EventMetrics::DispatchStage::kRendererMainFinished);
return "RendererMainProcessing";
case EventMetrics::DispatchStage::kRendererMainFinished:
NOTREACHED();
}
}
// static
const char* EventLatencyTracingRecorder::GetDispatchToCompositorBreakdownName(
EventMetrics::DispatchStage dispatch_stage,
CompositorFrameReporter::StageType compositor_stage) {
switch (dispatch_stage) {
case EventMetrics::DispatchStage::kRendererCompositorFinished:
switch (compositor_stage) {
case CompositorFrameReporter::StageType::
kBeginImplFrameToSendBeginMainFrame:
return "RendererCompositorFinishedToBeginImplFrame";
case CompositorFrameReporter::StageType::kSendBeginMainFrameToCommit:
return "RendererCompositorFinishedToSendBeginMainFrame";
case CompositorFrameReporter::StageType::kCommit:
return "RendererCompositorFinishedToCommit";
case CompositorFrameReporter::StageType::kEndCommitToActivation:
return "RendererCompositorFinishedToEndCommit";
case CompositorFrameReporter::StageType::kActivation:
return "RendererCompositorFinishedToActivation";
case CompositorFrameReporter::StageType::
kEndActivateToSubmitCompositorFrame:
return "RendererCompositorFinishedToEndActivate";
case CompositorFrameReporter::StageType::
kSubmitCompositorFrameToPresentationCompositorFrame:
return "RendererCompositorFinishedToSubmitCompositorFrame";
default:
NOTREACHED() << "Invalid CC stage after compositor thread: "
<< static_cast<int>(compositor_stage);
}
case EventMetrics::DispatchStage::kRendererMainFinished:
switch (compositor_stage) {
case CompositorFrameReporter::StageType::
kBeginImplFrameToSendBeginMainFrame:
return "RendererMainFinishedToBeginImplFrame";
case CompositorFrameReporter::StageType::kSendBeginMainFrameToCommit:
return "RendererMainFinishedToSendBeginMainFrame";
case CompositorFrameReporter::StageType::kCommit:
return "RendererMainFinishedToCommit";
case CompositorFrameReporter::StageType::kEndCommitToActivation:
return "RendererMainFinishedToEndCommit";
case CompositorFrameReporter::StageType::kActivation:
return "RendererMainFinishedToActivation";
case CompositorFrameReporter::StageType::
kEndActivateToSubmitCompositorFrame:
return "RendererMainFinishedToEndActivate";
case CompositorFrameReporter::StageType::
kSubmitCompositorFrameToPresentationCompositorFrame:
return "RendererMainFinishedToSubmitCompositorFrame";
default:
NOTREACHED() << "Invalid CC stage after main thread: "
<< static_cast<int>(compositor_stage);
}
default:
NOTREACHED();
}
}
// static
const char* EventLatencyTracingRecorder::GetDispatchToTerminationBreakdownName(
EventMetrics::DispatchStage dispatch_stage) {
switch (dispatch_stage) {
case EventMetrics::DispatchStage::kArrivedInRendererCompositor:
return "ArrivedInRendererCompositorToTermination";
case EventMetrics::DispatchStage::kRendererCompositorStarted:
return "RendererCompositorStartedToTermination";
case EventMetrics::DispatchStage::kRendererCompositorFinished:
return "RendererCompositorFinishedToTermination";
case EventMetrics::DispatchStage::kRendererMainStarted:
return "RendererMainStartedToTermination";
case EventMetrics::DispatchStage::kRendererMainFinished:
return "RendererMainFinishedToTermination";
default:
NOTREACHED();
}
}
// static
void EventLatencyTracingRecorder::RecordEventLatencyTraceEvent(
EventMetrics* event_metrics,
base::TimeTicks termination_time,
base::TimeDelta vsync_interval,
const std::vector<CompositorFrameReporter::StageData>* stage_history,
const CompositorFrameReporter::ProcessedVizBreakdown* viz_breakdown) {
// As there are multiple teardown paths for EventMetrics, we want to denote
// the attempt to trace, even if tracing is currently disabled.
if (IsTracingEnabled()) {
RecordEventLatencyTraceEventInternal(event_metrics, termination_time,
vsync_interval, stage_history,
viz_breakdown);
}
event_metrics->tracing_recorded();
}
// static
bool EventLatencyTracingRecorder::IsEventLatencyTracingEnabled() {
return IsTracingEnabled() ||
!base::FeatureList::IsEnabled(
::features::kMetricsTracingCalculationReduction);
}
void EventLatencyTracingRecorder::RecordEventLatencyTraceEventInternal(
const EventMetrics* event_metrics,
base::TimeTicks termination_time,
base::TimeDelta vsync_interval,
const std::vector<CompositorFrameReporter::StageData>* stage_history,
const CompositorFrameReporter::ProcessedVizBreakdown* viz_breakdown) {
DCHECK(event_metrics);
DCHECK(event_metrics->should_record_tracing());
const base::TimeTicks generated_timestamp =
event_metrics->GetDispatchStageTimestamp(
EventMetrics::DispatchStage::kGenerated);
const auto trace_track =
perfetto::Track(base::trace_event::GetNextGlobalTraceId());
TRACE_EVENT_BEGIN(
kTracingCategory, "EventLatency", trace_track, generated_timestamp,
[&](perfetto::EventContext context) {
auto* event =
context.event<perfetto::protos::pbzero::ChromeTrackEvent>();
auto* event_latency = event->set_event_latency();
event_latency->set_event_type(ToProtoEnum(event_metrics->type()));
bool has_high_latency =
(termination_time - generated_timestamp) > high_latency_threshold;
event_latency->set_has_high_latency(has_high_latency);
for (auto stage : event_metrics->GetHighLatencyStages()) {
// TODO(crbug.com/40228308): Consider changing the high_latency_stage
// type from a string to enum type in chrome_track_event.proto,
// similar to event_type.
event_latency->add_high_latency_stage(stage);
}
if (event_metrics->trace_id().has_value()) {
event_latency->set_event_latency_id(
event_metrics->trace_id()->value());
}
const ScrollUpdateEventMetrics* scroll_update =
event_metrics->AsScrollUpdate();
if (scroll_update &&
scroll_update->is_janky_scrolled_frame().has_value()) {
event_latency->set_is_janky_scrolled_frame(
scroll_update->is_janky_scrolled_frame().value());
}
event_latency->set_vsync_interval_ms(vsync_interval.InMillisecondsF());
});
// Event dispatch stages.
EventMetrics::DispatchStage dispatch_stage =
EventMetrics::DispatchStage::kGenerated;
base::TimeTicks dispatch_timestamp =
event_metrics->GetDispatchStageTimestamp(dispatch_stage);
while (dispatch_stage != EventMetrics::DispatchStage::kMaxValue) {
DCHECK(!dispatch_timestamp.is_null());
// Find the end dispatch stage.
auto end_stage = static_cast<EventMetrics::DispatchStage>(
static_cast<int>(dispatch_stage) + 1);
base::TimeTicks end_timestamp =
event_metrics->GetDispatchStageTimestamp(end_stage);
while (end_timestamp.is_null() &&
end_stage != EventMetrics::DispatchStage::kMaxValue) {
end_stage = static_cast<EventMetrics::DispatchStage>(
static_cast<int>(end_stage) + 1);
end_timestamp = event_metrics->GetDispatchStageTimestamp(end_stage);
}
if (end_timestamp.is_null())
break;
const char* breakdown_name =
GetDispatchBreakdownName(dispatch_stage, end_stage);
TRACE_EVENT_BEGIN(kTracingCategory, perfetto::StaticString{breakdown_name},
trace_track, dispatch_timestamp);
TRACE_EVENT_END(kTracingCategory, trace_track, end_timestamp);
dispatch_stage = end_stage;
dispatch_timestamp = end_timestamp;
}
if (stage_history) {
DCHECK(viz_breakdown);
// Find the first compositor stage that starts at the same time or after the
// end of the final event dispatch stage.
auto stage_it = base::ranges::lower_bound(
*stage_history, dispatch_timestamp, {},
&CompositorFrameReporter::StageData::start_time);
// TODO(crbug.com/40843545): Ideally, at least the start time of
// SubmitCompositorFrameToPresentationCompositorFrame stage should be
// greater than or equal to the final event dispatch timestamp, but
// apparently, this is not always the case (see crbug.com/1330903). Skip
// recording compositor stages for now until we investigate the issue.
if (stage_it != stage_history->end()) {
DCHECK(dispatch_stage ==
EventMetrics::DispatchStage::kRendererCompositorFinished ||
dispatch_stage ==
EventMetrics::DispatchStage::kRendererMainFinished);
// Record dispatch-to-compositor stage only if it has non-zero duration.
if (dispatch_timestamp < stage_it->start_time) {
const char* d2c_breakdown_name = GetDispatchToCompositorBreakdownName(
dispatch_stage, stage_it->stage_type);
TRACE_EVENT_BEGIN(kTracingCategory,
perfetto::StaticString{d2c_breakdown_name},
trace_track, dispatch_timestamp);
TRACE_EVENT_END(kTracingCategory, trace_track, stage_it->start_time);
}
// Compositor stages.
for (; stage_it != stage_history->end(); ++stage_it) {
if (stage_it->start_time >= termination_time)
break;
DCHECK_GE(stage_it->end_time, stage_it->start_time);
if (stage_it->start_time == stage_it->end_time)
continue;
const char* stage_name =
CompositorFrameReporter::GetStageName(stage_it->stage_type);
TRACE_EVENT_BEGIN(kTracingCategory, perfetto::StaticString{stage_name},
trace_track, stage_it->start_time);
if (stage_it->stage_type ==
CompositorFrameReporter::StageType::
kSubmitCompositorFrameToPresentationCompositorFrame) {
DCHECK(viz_breakdown);
for (auto it = viz_breakdown->CreateIterator(true); it.IsValid();
it.Advance()) {
base::TimeTicks start_time = it.GetStartTime();
base::TimeTicks end_time = it.GetEndTime();
// Only record events with positive duration that start before
// termination.
// For example, in WebView, swap start time is the same as
// presentation time, and it wouldn't make sense to have a
// zero-duration `SwapStartToPresentation` event. As a result, the
// last stage for WebView is `StartDrawToSwapStart`.
//
// http://b/337195538 tracks a feature request for receiving
// presentation time in WebView, which should make it consistent
// with Chrome.
if (start_time >= end_time || start_time >= termination_time) {
continue;
}
CompositorFrameReporter::VizBreakdown breakdown = it.GetBreakdown();
const char* breakdown_name = nullptr;
if (end_time > termination_time) {
end_time = termination_time;
// A breakdown ending in swap-end can end after termination time
// (because swap-end is actually the time the post swap end
// callback is run, which can happen after presentation). In this
// case we truncate the breakdown to presentation.
DCHECK(
breakdown == CompositorFrameReporter::VizBreakdown::
kSwapStartToSwapEnd ||
breakdown ==
CompositorFrameReporter::VizBreakdown::kLatchToSwapEnd);
breakdown_name = GetVizBreakdownToPresentationName(breakdown);
} else {
breakdown_name =
CompositorFrameReporter::GetVizBreakdownName(breakdown);
}
TRACE_EVENT_BEGIN(kTracingCategory,
perfetto::StaticString{breakdown_name},
trace_track, start_time);
TRACE_EVENT_END(kTracingCategory, trace_track, end_time);
}
}
TRACE_EVENT_END(kTracingCategory, trace_track, stage_it->end_time);
}
}
} else {
DCHECK(!viz_breakdown);
const char* d2t_breakdown_name =
GetDispatchToTerminationBreakdownName(dispatch_stage);
TRACE_EVENT_BEGIN(kTracingCategory,
perfetto::StaticString{d2t_breakdown_name}, trace_track,
dispatch_timestamp);
TRACE_EVENT_END(kTracingCategory, trace_track, termination_time);
}
TRACE_EVENT_END(kTracingCategory, trace_track, termination_time);
}
} // namespace cc