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), trace_id_(trace_id
), terminated_(terminated
) {
152 bool LatencyInfo::Verify(const std::vector
<LatencyInfo
>& latency_info
,
153 const char* referring_msg
) {
154 if (latency_info
.size() > kMaxLatencyInfoNumber
) {
155 LOG(ERROR
) << referring_msg
<< ", LatencyInfo vector size "
156 << latency_info
.size() << " is too big.";
162 void LatencyInfo::CopyLatencyFrom(const LatencyInfo
& other
,
163 LatencyComponentType type
) {
164 for (const auto& lc
: other
.latency_components()) {
165 if (lc
.first
.first
== type
) {
166 AddLatencyNumberWithTimestamp(lc
.first
.first
,
168 lc
.second
.sequence_number
,
169 lc
.second
.event_time
,
170 lc
.second
.event_count
);
175 void LatencyInfo::AddNewLatencyFrom(const LatencyInfo
& other
) {
176 for (const auto& lc
: other
.latency_components()) {
177 if (!FindLatency(lc
.first
.first
, lc
.first
.second
, NULL
)) {
178 AddLatencyNumberWithTimestamp(lc
.first
.first
,
180 lc
.second
.sequence_number
,
181 lc
.second
.event_time
,
182 lc
.second
.event_count
);
187 void LatencyInfo::AddLatencyNumber(LatencyComponentType component
,
189 int64 component_sequence_number
) {
190 AddLatencyNumberWithTimestampImpl(component
, id
, component_sequence_number
,
191 base::TimeTicks::Now(), 1, nullptr);
194 void LatencyInfo::AddLatencyNumberWithTraceName(
195 LatencyComponentType component
,
197 int64 component_sequence_number
,
198 const char* trace_name_str
) {
199 AddLatencyNumberWithTimestampImpl(component
, id
, component_sequence_number
,
200 base::TimeTicks::Now(), 1, trace_name_str
);
203 void LatencyInfo::AddLatencyNumberWithTimestamp(LatencyComponentType component
,
205 int64 component_sequence_number
,
206 base::TimeTicks time
,
207 uint32 event_count
) {
208 AddLatencyNumberWithTimestampImpl(component
, id
, component_sequence_number
,
209 time
, event_count
, nullptr);
212 void LatencyInfo::AddLatencyNumberWithTimestampImpl(
213 LatencyComponentType component
,
215 int64 component_sequence_number
,
216 base::TimeTicks time
,
218 const char* trace_name_str
) {
220 const unsigned char* benchmark_enabled
=
221 g_benchmark_enabled
.Get().benchmark_enabled
;
223 if (IsBeginComponent(component
)) {
224 // Should only ever add begin component once.
225 CHECK_EQ(-1, trace_id_
);
226 trace_id_
= component_sequence_number
;
228 if (*benchmark_enabled
) {
229 // The timestamp for ASYNC_BEGIN trace event is used for drawing the
230 // beginning of the trace event in trace viewer. For better visualization,
231 // for an input event, we want to draw the beginning as when the event is
232 // originally created, e.g. the timestamp of its ORIGINAL/UI_COMPONENT,
233 // not when we actually issue the ASYNC_BEGIN trace event.
234 LatencyComponent begin_component
;
236 if (FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT
,
239 FindLatency(INPUT_EVENT_LATENCY_UI_COMPONENT
,
242 // The timestamp stored in ORIGINAL/UI_COMPONENT is using clock
243 // CLOCK_MONOTONIC while TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0
244 // expects timestamp using CLOCK_MONOTONIC or CLOCK_SYSTEM_TRACE (on
245 // CrOS). So we need to adjust the diff between in CLOCK_MONOTONIC and
246 // CLOCK_SYSTEM_TRACE. Note that the diff is drifting overtime so we
247 // can't use a static value.
248 base::TimeDelta diff
= (base::TimeTicks::Now() - base::TimeTicks()) -
249 (base::TraceTicks::Now() - base::TraceTicks());
250 ts
= (begin_component
.event_time
- diff
).ToInternalValue();
252 ts
= base::TraceTicks::Now().ToInternalValue();
255 if (trace_name_str
) {
256 if (IsInputLatencyBeginComponent(component
))
257 trace_name_
= std::string("InputLatency::") + trace_name_str
;
259 trace_name_
= std::string("Latency::") + trace_name_str
;
262 TRACE_EVENT_COPY_ASYNC_BEGIN_WITH_TIMESTAMP0(
263 "benchmark,latencyInfo",
265 TRACE_ID_DONT_MANGLE(trace_id_
),
269 TRACE_EVENT_WITH_FLOW1("input,benchmark",
271 TRACE_ID_DONT_MANGLE(trace_id_
),
272 TRACE_EVENT_FLAG_FLOW_OUT
,
273 "trace_id", trace_id_
);
276 LatencyMap::key_type key
= std::make_pair(component
, id
);
277 LatencyMap::iterator it
= latency_components_
.find(key
);
278 if (it
== latency_components_
.end()) {
279 LatencyComponent info
= {component_sequence_number
, time
, event_count
};
280 latency_components_
[key
] = info
;
282 it
->second
.sequence_number
= std::max(component_sequence_number
,
283 it
->second
.sequence_number
);
284 uint32 new_count
= event_count
+ it
->second
.event_count
;
285 if (event_count
> 0 && new_count
!= 0) {
286 // Do a weighted average, so that the new event_time is the average of
287 // the times of events currently in this structure with the time passed
289 it
->second
.event_time
+= (time
- it
->second
.event_time
) * event_count
/
291 it
->second
.event_count
= new_count
;
295 if (IsTerminalComponent(component
) && trace_id_
!= -1) {
296 // Should only ever add terminal component once.
300 if (*benchmark_enabled
) {
301 TRACE_EVENT_COPY_ASYNC_END1("benchmark,latencyInfo",
303 TRACE_ID_DONT_MANGLE(trace_id_
),
304 "data", AsTraceableData());
307 TRACE_EVENT_WITH_FLOW0("input,benchmark",
309 TRACE_ID_DONT_MANGLE(trace_id_
),
310 TRACE_EVENT_FLAG_FLOW_IN
);
314 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
315 LatencyInfo::AsTraceableData() {
316 scoped_ptr
<base::DictionaryValue
> record_data(new base::DictionaryValue());
317 for (const auto& lc
: latency_components_
) {
318 scoped_ptr
<base::DictionaryValue
>
319 component_info(new base::DictionaryValue());
320 component_info
->SetDouble("comp_id", static_cast<double>(lc
.first
.second
));
321 component_info
->SetDouble(
323 static_cast<double>(lc
.second
.event_time
.ToInternalValue()));
324 component_info
->SetDouble("count", lc
.second
.event_count
);
325 component_info
->SetDouble("sequence_number",
326 lc
.second
.sequence_number
);
327 record_data
->Set(GetComponentName(lc
.first
.first
), component_info
.Pass());
329 record_data
->SetDouble("trace_id", static_cast<double>(trace_id_
));
331 scoped_ptr
<base::ListValue
> coordinates(new base::ListValue());
332 for (size_t i
= 0; i
< input_coordinates_size_
; i
++) {
333 scoped_ptr
<base::DictionaryValue
> coordinate_pair(
334 new base::DictionaryValue());
335 coordinate_pair
->SetDouble("x", input_coordinates_
[i
].x
);
336 coordinate_pair
->SetDouble("y", input_coordinates_
[i
].y
);
337 coordinates
->Append(coordinate_pair
.release());
339 record_data
->Set("coordinates", coordinates
.release());
340 return LatencyInfoTracedValue::FromValue(record_data
.Pass());
343 bool LatencyInfo::FindLatency(LatencyComponentType type
,
345 LatencyComponent
* output
) const {
346 LatencyMap::const_iterator it
= latency_components_
.find(
347 std::make_pair(type
, id
));
348 if (it
== latency_components_
.end())
351 *output
= it
->second
;
355 void LatencyInfo::RemoveLatency(LatencyComponentType type
) {
356 LatencyMap::iterator it
= latency_components_
.begin();
357 while (it
!= latency_components_
.end()) {
358 if (it
->first
.first
== type
) {
359 LatencyMap::iterator tmp
= it
;
361 latency_components_
.erase(tmp
);
368 bool LatencyInfo::AddInputCoordinate(const InputCoordinate
& input_coordinate
) {
369 if (input_coordinates_size_
>= kMaxInputCoordinates
)
371 input_coordinates_
[input_coordinates_size_
++] = input_coordinate
;
375 bool LatencyInfo::AddCoalescedEventTimestamp(double timestamp
) {
376 if (coalesced_events_size_
>= kMaxCoalescedEventTimestamps
)
378 timestamps_of_coalesced_events_
[coalesced_events_size_
++] = timestamp
;