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/memory/scoped_ptr.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/trace_event/trace_event.h"
18 const size_t kMaxLatencyInfoNumber
= 100;
20 const char* GetComponentName(ui::LatencyComponentType type
) {
21 #define CASE_TYPE(t) case ui::t: return #t
23 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
);
24 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT
);
25 CASE_TYPE(LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT
);
26 CASE_TYPE(INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT
);
27 CASE_TYPE(INPUT_EVENT_LATENCY_FIRST_SCROLL_UPDATE_ORIGINAL_COMPONENT
);
28 CASE_TYPE(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT
);
29 CASE_TYPE(INPUT_EVENT_LATENCY_UI_COMPONENT
);
30 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_MAIN_COMPONENT
);
31 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_IMPL_COMPONENT
);
32 CASE_TYPE(INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT
);
33 CASE_TYPE(INPUT_EVENT_LATENCY_ACK_RWH_COMPONENT
);
34 CASE_TYPE(WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT
);
35 CASE_TYPE(TAB_SHOW_COMPONENT
);
36 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERER_SWAP_COMPONENT
);
37 CASE_TYPE(INPUT_EVENT_BROWSER_RECEIVED_RENDERER_SWAP_COMPONENT
);
38 CASE_TYPE(INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT
);
39 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT
);
40 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT
);
41 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT
);
42 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT
);
43 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT
);
44 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT
);
45 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT
);
46 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT
);
48 DLOG(WARNING
) << "Unhandled LatencyComponentType.\n";
55 bool IsTerminalComponent(ui::LatencyComponentType type
) {
57 case ui::INPUT_EVENT_LATENCY_TERMINATED_MOUSE_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
:
64 case ui::INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT
:
71 bool IsBeginComponent(ui::LatencyComponentType type
) {
72 return (type
== ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
||
73 type
== ui::INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT
||
74 type
== ui::LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT
);
77 bool IsInputLatencyBeginComponent(ui::LatencyComponentType type
) {
78 return (type
== ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
||
79 type
== ui::INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT
);
82 // This class is for converting latency info to trace buffer friendly format.
83 class LatencyInfoTracedValue
84 : public base::trace_event::ConvertableToTraceFormat
{
86 static scoped_refptr
<ConvertableToTraceFormat
> FromValue(
87 scoped_ptr
<base::Value
> value
);
89 void AppendAsTraceFormat(std::string
* out
) const override
;
92 explicit LatencyInfoTracedValue(base::Value
* value
);
93 ~LatencyInfoTracedValue() override
;
95 scoped_ptr
<base::Value
> value_
;
97 DISALLOW_COPY_AND_ASSIGN(LatencyInfoTracedValue
);
100 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
101 LatencyInfoTracedValue::FromValue(scoped_ptr
<base::Value
> value
) {
102 return scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>(
103 new LatencyInfoTracedValue(value
.release()));
106 LatencyInfoTracedValue::~LatencyInfoTracedValue() {
109 void LatencyInfoTracedValue::AppendAsTraceFormat(std::string
* out
) const {
111 base::JSONWriter::Write(value_
.get(), &tmp
);
115 LatencyInfoTracedValue::LatencyInfoTracedValue(base::Value
* value
)
119 // Converts latencyinfo into format that can be dumped into trace buffer.
120 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> AsTraceableData(
121 const ui::LatencyInfo
& latency
) {
122 scoped_ptr
<base::DictionaryValue
> record_data(new base::DictionaryValue());
123 for (ui::LatencyInfo::LatencyMap::const_iterator it
=
124 latency
.latency_components
.begin();
125 it
!= latency
.latency_components
.end(); ++it
) {
126 base::DictionaryValue
* component_info
= new base::DictionaryValue();
127 component_info
->SetDouble("comp_id", static_cast<double>(it
->first
.second
));
128 component_info
->SetDouble(
129 "time", static_cast<double>(it
->second
.event_time
.ToInternalValue()));
130 component_info
->SetDouble("count", it
->second
.event_count
);
131 record_data
->Set(GetComponentName(it
->first
.first
), component_info
);
133 record_data
->SetDouble("trace_id", static_cast<double>(latency
.trace_id
));
135 scoped_ptr
<base::ListValue
> coordinates(new base::ListValue());
136 for (size_t i
= 0; i
< latency
.input_coordinates_size
; i
++) {
137 scoped_ptr
<base::DictionaryValue
> coordinate_pair(
138 new base::DictionaryValue());
139 coordinate_pair
->SetDouble("x", latency
.input_coordinates
[i
].x
);
140 coordinate_pair
->SetDouble("y", latency
.input_coordinates
[i
].y
);
141 coordinates
->Append(coordinate_pair
.release());
143 record_data
->Set("coordinates", coordinates
.release());
144 return LatencyInfoTracedValue::FromValue(record_data
.Pass());
147 struct BenchmarkEnabledInitializer
{
148 BenchmarkEnabledInitializer() :
149 benchmark_enabled(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
153 const unsigned char* benchmark_enabled
;
156 static base::LazyInstance
<BenchmarkEnabledInitializer
>::Leaky
157 g_benchmark_enabled
= LAZY_INSTANCE_INITIALIZER
;
163 LatencyInfo::InputCoordinate::InputCoordinate() : x(0), y(0) {
166 LatencyInfo::InputCoordinate::InputCoordinate(float x
, float y
) : x(x
), y(y
) {
169 LatencyInfo::LatencyInfo()
170 : input_coordinates_size(0), trace_id(-1), terminated(false) {
173 LatencyInfo::~LatencyInfo() {
176 bool LatencyInfo::Verify(const std::vector
<LatencyInfo
>& latency_info
,
177 const char* referring_msg
) {
178 if (latency_info
.size() > kMaxLatencyInfoNumber
) {
179 LOG(ERROR
) << referring_msg
<< ", LatencyInfo vector size "
180 << latency_info
.size() << " is too big.";
183 for (size_t i
= 0; i
< latency_info
.size(); i
++) {
184 if (latency_info
[i
].input_coordinates_size
> kMaxInputCoordinates
) {
185 LOG(ERROR
) << referring_msg
<< ", coordinate vector size "
186 << latency_info
[i
].input_coordinates_size
<< " is too big.";
194 void LatencyInfo::CopyLatencyFrom(const LatencyInfo
& other
,
195 LatencyComponentType type
) {
196 for (LatencyMap::const_iterator it
= other
.latency_components
.begin();
197 it
!= other
.latency_components
.end();
199 if (it
->first
.first
== type
) {
200 AddLatencyNumberWithTimestamp(it
->first
.first
,
202 it
->second
.sequence_number
,
203 it
->second
.event_time
,
204 it
->second
.event_count
);
209 void LatencyInfo::AddNewLatencyFrom(const LatencyInfo
& other
) {
210 for (LatencyMap::const_iterator it
= other
.latency_components
.begin();
211 it
!= other
.latency_components
.end();
213 if (!FindLatency(it
->first
.first
, it
->first
.second
, NULL
)) {
214 AddLatencyNumberWithTimestamp(it
->first
.first
,
216 it
->second
.sequence_number
,
217 it
->second
.event_time
,
218 it
->second
.event_count
);
223 void LatencyInfo::AddLatencyNumber(LatencyComponentType component
,
225 int64 component_sequence_number
) {
226 AddLatencyNumberWithTimestampImpl(component
, id
, component_sequence_number
,
227 base::TimeTicks::Now(), 1, nullptr);
230 void LatencyInfo::AddLatencyNumberWithTraceName(
231 LatencyComponentType component
,
233 int64 component_sequence_number
,
234 const char* trace_name_str
) {
235 AddLatencyNumberWithTimestampImpl(component
, id
, component_sequence_number
,
236 base::TimeTicks::Now(), 1, trace_name_str
);
239 void LatencyInfo::AddLatencyNumberWithTimestamp(LatencyComponentType component
,
241 int64 component_sequence_number
,
242 base::TimeTicks time
,
243 uint32 event_count
) {
244 AddLatencyNumberWithTimestampImpl(component
, id
, component_sequence_number
,
245 time
, event_count
, nullptr);
248 void LatencyInfo::AddLatencyNumberWithTimestampImpl(
249 LatencyComponentType component
,
251 int64 component_sequence_number
,
252 base::TimeTicks time
,
254 const char* trace_name_str
) {
256 const unsigned char* benchmark_enabled
=
257 g_benchmark_enabled
.Get().benchmark_enabled
;
259 if (IsBeginComponent(component
)) {
260 // Should only ever add begin component once.
261 CHECK_EQ(-1, trace_id
);
262 trace_id
= component_sequence_number
;
264 if (*benchmark_enabled
) {
265 // The timestamp for ASYNC_BEGIN trace event is used for drawing the
266 // beginning of the trace event in trace viewer. For better visualization,
267 // for an input event, we want to draw the beginning as when the event is
268 // originally created, e.g. the timestamp of its ORIGINAL/UI_COMPONENT,
269 // not when we actually issue the ASYNC_BEGIN trace event.
270 LatencyComponent begin_component
;
272 if (FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT
,
275 FindLatency(INPUT_EVENT_LATENCY_UI_COMPONENT
,
278 // The timestamp stored in ORIGINAL/UI_COMPONENT is using clock
279 // CLOCK_MONOTONIC while TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0
280 // expects timestamp using CLOCK_MONOTONIC or CLOCK_SYSTEM_TRACE (on
281 // CrOS). So we need to adjust the diff between in CLOCK_MONOTONIC and
282 // CLOCK_SYSTEM_TRACE. Note that the diff is drifting overtime so we
283 // can't use a static value.
284 int64 diff
= base::TimeTicks::Now().ToInternalValue() -
285 base::TimeTicks::NowFromSystemTraceTime().ToInternalValue();
286 ts
= begin_component
.event_time
.ToInternalValue() - diff
;
288 ts
= base::TimeTicks::NowFromSystemTraceTime().ToInternalValue();
291 if (trace_name_str
) {
292 if (IsInputLatencyBeginComponent(component
))
293 trace_name
= std::string("InputLatency::") + trace_name_str
;
295 trace_name
= std::string("Latency::") + trace_name_str
;
298 TRACE_EVENT_COPY_ASYNC_BEGIN_WITH_TIMESTAMP0(
301 TRACE_ID_DONT_MANGLE(trace_id
),
305 TRACE_EVENT_FLOW_BEGIN0(
306 "input,benchmark", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id
));
309 LatencyMap::key_type key
= std::make_pair(component
, id
);
310 LatencyMap::iterator it
= latency_components
.find(key
);
311 if (it
== latency_components
.end()) {
312 LatencyComponent info
= {component_sequence_number
, time
, event_count
};
313 latency_components
[key
] = info
;
315 it
->second
.sequence_number
= std::max(component_sequence_number
,
316 it
->second
.sequence_number
);
317 uint32 new_count
= event_count
+ it
->second
.event_count
;
318 if (event_count
> 0 && new_count
!= 0) {
319 // Do a weighted average, so that the new event_time is the average of
320 // the times of events currently in this structure with the time passed
322 it
->second
.event_time
+= (time
- it
->second
.event_time
) * event_count
/
324 it
->second
.event_count
= new_count
;
328 if (IsTerminalComponent(component
) && trace_id
!= -1) {
329 // Should only ever add terminal component once.
333 if (*benchmark_enabled
) {
334 TRACE_EVENT_COPY_ASYNC_END1("benchmark",
336 TRACE_ID_DONT_MANGLE(trace_id
),
337 "data", AsTraceableData(*this));
340 TRACE_EVENT_FLOW_END0(
341 "input,benchmark", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id
));
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 void LatencyInfo::Clear() {
371 latency_components
.clear();