1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/events/latency_info.h"
10 #include "base/json/json_writer.h"
11 #include "base/lazy_instance.h"
12 #include "base/strings/stringprintf.h"
16 const size_t kMaxLatencyInfoNumber
= 100;
18 const char* GetComponentName(ui::LatencyComponentType type
) {
19 #define CASE_TYPE(t) case ui::t: return #t
21 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
);
22 CASE_TYPE(LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT
);
23 CASE_TYPE(INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT
);
24 CASE_TYPE(INPUT_EVENT_LATENCY_FIRST_SCROLL_UPDATE_ORIGINAL_COMPONENT
);
25 CASE_TYPE(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT
);
26 CASE_TYPE(INPUT_EVENT_LATENCY_UI_COMPONENT
);
27 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_MAIN_COMPONENT
);
28 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_IMPL_COMPONENT
);
29 CASE_TYPE(INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT
);
30 CASE_TYPE(INPUT_EVENT_LATENCY_ACK_RWH_COMPONENT
);
31 CASE_TYPE(WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT
);
32 CASE_TYPE(TAB_SHOW_COMPONENT
);
33 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERER_SWAP_COMPONENT
);
34 CASE_TYPE(INPUT_EVENT_BROWSER_RECEIVED_RENDERER_SWAP_COMPONENT
);
35 CASE_TYPE(INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT
);
36 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT
);
37 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_MOUSE_WHEEL_COMPONENT
);
38 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_KEYBOARD_COMPONENT
);
39 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT
);
40 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT
);
41 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT
);
42 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT
);
43 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT
);
44 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT
);
46 DLOG(WARNING
) << "Unhandled LatencyComponentType.\n";
53 bool IsTerminalComponent(ui::LatencyComponentType type
) {
55 case ui::INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT
:
56 case ui::INPUT_EVENT_LATENCY_TERMINATED_MOUSE_WHEEL_COMPONENT
:
57 case ui::INPUT_EVENT_LATENCY_TERMINATED_KEYBOARD_COMPONENT
:
58 case ui::INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT
:
59 case ui::INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT
:
60 case ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT
:
61 case ui::INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT
:
62 case ui::INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT
:
63 case ui::INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT
:
70 bool IsBeginComponent(ui::LatencyComponentType type
) {
71 return (type
== ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
||
72 type
== ui::LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT
);
75 bool IsInputLatencyBeginComponent(ui::LatencyComponentType type
) {
76 return type
== ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
;
79 // This class is for converting latency info to trace buffer friendly format.
80 class LatencyInfoTracedValue
81 : public base::trace_event::ConvertableToTraceFormat
{
83 static scoped_refptr
<ConvertableToTraceFormat
> FromValue(
84 scoped_ptr
<base::Value
> value
);
86 void AppendAsTraceFormat(std::string
* out
) const override
;
89 explicit LatencyInfoTracedValue(base::Value
* value
);
90 ~LatencyInfoTracedValue() override
;
92 scoped_ptr
<base::Value
> value_
;
94 DISALLOW_COPY_AND_ASSIGN(LatencyInfoTracedValue
);
97 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
98 LatencyInfoTracedValue::FromValue(scoped_ptr
<base::Value
> value
) {
99 return scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>(
100 new LatencyInfoTracedValue(value
.release()));
103 LatencyInfoTracedValue::~LatencyInfoTracedValue() {
106 void LatencyInfoTracedValue::AppendAsTraceFormat(std::string
* out
) const {
108 base::JSONWriter::Write(*value_
, &tmp
);
112 LatencyInfoTracedValue::LatencyInfoTracedValue(base::Value
* value
)
116 struct BenchmarkEnabledInitializer
{
117 BenchmarkEnabledInitializer() :
118 benchmark_enabled(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
122 const unsigned char* benchmark_enabled
;
125 static base::LazyInstance
<BenchmarkEnabledInitializer
>::Leaky
126 g_benchmark_enabled
= LAZY_INSTANCE_INITIALIZER
;
132 LatencyInfo::InputCoordinate::InputCoordinate() : x(0), y(0) {
135 LatencyInfo::InputCoordinate::InputCoordinate(float x
, float y
) : x(x
), y(y
) {
138 LatencyInfo::LatencyInfo()
139 : input_coordinates_size_(0),
140 coalesced_events_size_(0),
145 LatencyInfo::~LatencyInfo() {
148 LatencyInfo::LatencyInfo(int64 trace_id
, bool terminated
)
149 : input_coordinates_size_(0),
150 coalesced_events_size_(0),
152 terminated_(terminated
) {}
154 bool LatencyInfo::Verify(const std::vector
<LatencyInfo
>& latency_info
,
155 const char* referring_msg
) {
156 if (latency_info
.size() > kMaxLatencyInfoNumber
) {
157 LOG(ERROR
) << referring_msg
<< ", LatencyInfo vector size "
158 << latency_info
.size() << " is too big.";
164 void LatencyInfo::CopyLatencyFrom(const LatencyInfo
& other
,
165 LatencyComponentType type
) {
166 for (const auto& lc
: other
.latency_components()) {
167 if (lc
.first
.first
== type
) {
168 AddLatencyNumberWithTimestamp(lc
.first
.first
,
170 lc
.second
.sequence_number
,
171 lc
.second
.event_time
,
172 lc
.second
.event_count
);
177 void LatencyInfo::AddNewLatencyFrom(const LatencyInfo
& other
) {
178 for (const auto& lc
: other
.latency_components()) {
179 if (!FindLatency(lc
.first
.first
, lc
.first
.second
, NULL
)) {
180 AddLatencyNumberWithTimestamp(lc
.first
.first
,
182 lc
.second
.sequence_number
,
183 lc
.second
.event_time
,
184 lc
.second
.event_count
);
189 void LatencyInfo::AddLatencyNumber(LatencyComponentType component
,
191 int64 component_sequence_number
) {
192 AddLatencyNumberWithTimestampImpl(component
, id
, component_sequence_number
,
193 base::TimeTicks::Now(), 1, nullptr);
196 void LatencyInfo::AddLatencyNumberWithTraceName(
197 LatencyComponentType component
,
199 int64 component_sequence_number
,
200 const char* trace_name_str
) {
201 AddLatencyNumberWithTimestampImpl(component
, id
, component_sequence_number
,
202 base::TimeTicks::Now(), 1, trace_name_str
);
205 void LatencyInfo::AddLatencyNumberWithTimestamp(LatencyComponentType component
,
207 int64 component_sequence_number
,
208 base::TimeTicks time
,
209 uint32 event_count
) {
210 AddLatencyNumberWithTimestampImpl(component
, id
, component_sequence_number
,
211 time
, event_count
, nullptr);
214 void LatencyInfo::AddLatencyNumberWithTimestampImpl(
215 LatencyComponentType component
,
217 int64 component_sequence_number
,
218 base::TimeTicks time
,
220 const char* trace_name_str
) {
222 const unsigned char* benchmark_enabled
=
223 g_benchmark_enabled
.Get().benchmark_enabled
;
225 if (IsBeginComponent(component
)) {
226 // Should only ever add begin component once.
227 CHECK_EQ(-1, trace_id_
);
228 trace_id_
= component_sequence_number
;
230 if (*benchmark_enabled
) {
231 // The timestamp for ASYNC_BEGIN trace event is used for drawing the
232 // beginning of the trace event in trace viewer. For better visualization,
233 // for an input event, we want to draw the beginning as when the event is
234 // originally created, e.g. the timestamp of its ORIGINAL/UI_COMPONENT,
235 // not when we actually issue the ASYNC_BEGIN trace event.
236 LatencyComponent begin_component
;
238 if (FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT
,
241 FindLatency(INPUT_EVENT_LATENCY_UI_COMPONENT
,
244 // The timestamp stored in ORIGINAL/UI_COMPONENT is using clock
245 // CLOCK_MONOTONIC while TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0
246 // expects timestamp using CLOCK_MONOTONIC or CLOCK_SYSTEM_TRACE (on
247 // CrOS). So we need to adjust the diff between in CLOCK_MONOTONIC and
248 // CLOCK_SYSTEM_TRACE. Note that the diff is drifting overtime so we
249 // can't use a static value.
250 base::TimeDelta diff
= (base::TimeTicks::Now() - base::TimeTicks()) -
251 (base::TraceTicks::Now() - base::TraceTicks());
252 ts
= (begin_component
.event_time
- diff
).ToInternalValue();
254 ts
= base::TraceTicks::Now().ToInternalValue();
257 if (trace_name_str
) {
258 if (IsInputLatencyBeginComponent(component
))
259 trace_name_
= std::string("InputLatency::") + trace_name_str
;
261 trace_name_
= std::string("Latency::") + trace_name_str
;
264 TRACE_EVENT_COPY_ASYNC_BEGIN_WITH_TIMESTAMP0(
265 "benchmark,latencyInfo",
267 TRACE_ID_DONT_MANGLE(trace_id_
),
271 TRACE_EVENT_WITH_FLOW1("input,benchmark",
273 TRACE_ID_DONT_MANGLE(trace_id_
),
274 TRACE_EVENT_FLAG_FLOW_OUT
,
275 "trace_id", trace_id_
);
278 LatencyMap::key_type key
= std::make_pair(component
, id
);
279 LatencyMap::iterator it
= latency_components_
.find(key
);
280 if (it
== latency_components_
.end()) {
281 LatencyComponent info
= {component_sequence_number
, time
, event_count
};
282 latency_components_
[key
] = info
;
284 it
->second
.sequence_number
= std::max(component_sequence_number
,
285 it
->second
.sequence_number
);
286 uint32 new_count
= event_count
+ it
->second
.event_count
;
287 if (event_count
> 0 && new_count
!= 0) {
288 // Do a weighted average, so that the new event_time is the average of
289 // the times of events currently in this structure with the time passed
291 it
->second
.event_time
+= (time
- it
->second
.event_time
) * event_count
/
293 it
->second
.event_count
= new_count
;
297 if (IsTerminalComponent(component
) && trace_id_
!= -1) {
298 // Should only ever add terminal component once.
302 if (*benchmark_enabled
) {
303 TRACE_EVENT_COPY_ASYNC_END1("benchmark,latencyInfo",
305 TRACE_ID_DONT_MANGLE(trace_id_
),
306 "data", AsTraceableData());
309 TRACE_EVENT_WITH_FLOW0("input,benchmark",
311 TRACE_ID_DONT_MANGLE(trace_id_
),
312 TRACE_EVENT_FLAG_FLOW_IN
);
316 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
317 LatencyInfo::AsTraceableData() {
318 scoped_ptr
<base::DictionaryValue
> record_data(new base::DictionaryValue());
319 for (const auto& lc
: latency_components_
) {
320 scoped_ptr
<base::DictionaryValue
>
321 component_info(new base::DictionaryValue());
322 component_info
->SetDouble("comp_id", static_cast<double>(lc
.first
.second
));
323 component_info
->SetDouble(
325 static_cast<double>(lc
.second
.event_time
.ToInternalValue()));
326 component_info
->SetDouble("count", lc
.second
.event_count
);
327 component_info
->SetDouble("sequence_number",
328 lc
.second
.sequence_number
);
329 record_data
->Set(GetComponentName(lc
.first
.first
), component_info
.Pass());
331 record_data
->SetDouble("trace_id", static_cast<double>(trace_id_
));
333 scoped_ptr
<base::ListValue
> coordinates(new base::ListValue());
334 for (size_t i
= 0; i
< input_coordinates_size_
; i
++) {
335 scoped_ptr
<base::DictionaryValue
> coordinate_pair(
336 new base::DictionaryValue());
337 coordinate_pair
->SetDouble("x", input_coordinates_
[i
].x
);
338 coordinate_pair
->SetDouble("y", input_coordinates_
[i
].y
);
339 coordinates
->Append(coordinate_pair
.release());
341 record_data
->Set("coordinates", coordinates
.release());
342 return LatencyInfoTracedValue::FromValue(record_data
.Pass());
345 bool LatencyInfo::FindLatency(LatencyComponentType type
,
347 LatencyComponent
* output
) const {
348 LatencyMap::const_iterator it
= latency_components_
.find(
349 std::make_pair(type
, id
));
350 if (it
== latency_components_
.end())
353 *output
= it
->second
;
357 void LatencyInfo::RemoveLatency(LatencyComponentType type
) {
358 LatencyMap::iterator it
= latency_components_
.begin();
359 while (it
!= latency_components_
.end()) {
360 if (it
->first
.first
== type
) {
361 LatencyMap::iterator tmp
= it
;
363 latency_components_
.erase(tmp
);
370 bool LatencyInfo::AddInputCoordinate(const InputCoordinate
& input_coordinate
) {
371 if (input_coordinates_size_
>= kMaxInputCoordinates
)
373 input_coordinates_
[input_coordinates_size_
++] = input_coordinate
;
377 bool LatencyInfo::AddCoalescedEventTimestamp(double timestamp
) {
378 if (coalesced_events_size_
>= kMaxCoalescedEventTimestamps
)
380 timestamps_of_coalesced_events_
[coalesced_events_size_
++] = timestamp
;