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 "base/json/json_writer.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/trace_event/trace_event.h"
9 #include "ui/events/latency_info.h"
15 const size_t kMaxLatencyInfoNumber
= 100;
17 const char* GetComponentName(ui::LatencyComponentType type
) {
18 #define CASE_TYPE(t) case ui::t: return #t
20 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
);
21 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT
);
22 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_SCROLL_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(WINDOW_OLD_SNAPSHOT_FRAME_NUMBER_COMPONENT
);
33 CASE_TYPE(TAB_SHOW_COMPONENT
);
34 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERER_SWAP_COMPONENT
);
35 CASE_TYPE(INPUT_EVENT_BROWSER_RECEIVED_RENDERER_SWAP_COMPONENT
);
36 CASE_TYPE(INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT
);
37 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT
);
38 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT
);
39 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT
);
40 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT
);
41 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT
);
42 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT
);
43 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT
);
44 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_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_TOUCH_COMPONENT
:
57 case ui::INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT
:
58 case ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT
:
59 case ui::INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT
:
60 case ui::INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT
:
61 case ui::INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT
:
62 case ui::INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT
:
69 bool IsBeginComponent(ui::LatencyComponentType type
) {
70 return (type
== ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
||
71 type
== ui::INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT
||
72 type
== ui::INPUT_EVENT_LATENCY_BEGIN_SCROLL_UPDATE_MAIN_COMPONENT
);
75 // This class is for converting latency info to trace buffer friendly format.
76 class LatencyInfoTracedValue
77 : public base::trace_event::ConvertableToTraceFormat
{
79 static scoped_refptr
<ConvertableToTraceFormat
> FromValue(
80 scoped_ptr
<base::Value
> value
);
82 void AppendAsTraceFormat(std::string
* out
) const override
;
85 explicit LatencyInfoTracedValue(base::Value
* value
);
86 ~LatencyInfoTracedValue() override
;
88 scoped_ptr
<base::Value
> value_
;
90 DISALLOW_COPY_AND_ASSIGN(LatencyInfoTracedValue
);
93 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
94 LatencyInfoTracedValue::FromValue(scoped_ptr
<base::Value
> value
) {
95 return scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>(
96 new LatencyInfoTracedValue(value
.release()));
99 LatencyInfoTracedValue::~LatencyInfoTracedValue() {
102 void LatencyInfoTracedValue::AppendAsTraceFormat(std::string
* out
) const {
104 base::JSONWriter::Write(value_
.get(), &tmp
);
108 LatencyInfoTracedValue::LatencyInfoTracedValue(base::Value
* value
)
112 // Converts latencyinfo into format that can be dumped into trace buffer.
113 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> AsTraceableData(
114 const ui::LatencyInfo
& latency
) {
115 scoped_ptr
<base::DictionaryValue
> record_data(new base::DictionaryValue());
116 for (ui::LatencyInfo::LatencyMap::const_iterator it
=
117 latency
.latency_components
.begin();
118 it
!= latency
.latency_components
.end(); ++it
) {
119 base::DictionaryValue
* component_info
= new base::DictionaryValue();
120 component_info
->SetDouble("comp_id", static_cast<double>(it
->first
.second
));
121 component_info
->SetDouble(
122 "time", static_cast<double>(it
->second
.event_time
.ToInternalValue()));
123 component_info
->SetDouble("count", it
->second
.event_count
);
124 record_data
->Set(GetComponentName(it
->first
.first
), component_info
);
126 record_data
->SetDouble("trace_id", static_cast<double>(latency
.trace_id
));
128 scoped_ptr
<base::ListValue
> coordinates(new base::ListValue());
129 for (size_t i
= 0; i
< latency
.input_coordinates_size
; i
++) {
130 scoped_ptr
<base::DictionaryValue
> coordinate_pair(
131 new base::DictionaryValue());
132 coordinate_pair
->SetDouble("x", latency
.input_coordinates
[i
].x
);
133 coordinate_pair
->SetDouble("y", latency
.input_coordinates
[i
].y
);
134 coordinates
->Append(coordinate_pair
.release());
136 record_data
->Set("coordinates", coordinates
.release());
137 return LatencyInfoTracedValue::FromValue(record_data
.Pass());
144 LatencyInfo::InputCoordinate::InputCoordinate() : x(0), y(0) {
147 LatencyInfo::InputCoordinate::InputCoordinate(float x
, float y
) : x(x
), y(y
) {
150 LatencyInfo::LatencyInfo()
151 : input_coordinates_size(0), trace_id(-1), terminated(false) {
154 LatencyInfo::~LatencyInfo() {
157 bool LatencyInfo::Verify(const std::vector
<LatencyInfo
>& latency_info
,
158 const char* referring_msg
) {
159 if (latency_info
.size() > kMaxLatencyInfoNumber
) {
160 LOG(ERROR
) << referring_msg
<< ", LatencyInfo vector size "
161 << latency_info
.size() << " is too big.";
164 for (size_t i
= 0; i
< latency_info
.size(); i
++) {
165 if (latency_info
[i
].input_coordinates_size
> kMaxInputCoordinates
) {
166 LOG(ERROR
) << referring_msg
<< ", coordinate vector size "
167 << latency_info
[i
].input_coordinates_size
<< " is too big.";
175 void LatencyInfo::CopyLatencyFrom(const LatencyInfo
& other
,
176 LatencyComponentType type
) {
177 for (LatencyMap::const_iterator it
= other
.latency_components
.begin();
178 it
!= other
.latency_components
.end();
180 if (it
->first
.first
== type
) {
181 AddLatencyNumberWithTimestamp(it
->first
.first
,
183 it
->second
.sequence_number
,
184 it
->second
.event_time
,
185 it
->second
.event_count
);
190 void LatencyInfo::AddNewLatencyFrom(const LatencyInfo
& other
) {
191 for (LatencyMap::const_iterator it
= other
.latency_components
.begin();
192 it
!= other
.latency_components
.end();
194 if (!FindLatency(it
->first
.first
, it
->first
.second
, NULL
)) {
195 AddLatencyNumberWithTimestamp(it
->first
.first
,
197 it
->second
.sequence_number
,
198 it
->second
.event_time
,
199 it
->second
.event_count
);
204 void LatencyInfo::AddLatencyNumber(LatencyComponentType component
,
206 int64 component_sequence_number
) {
207 AddLatencyNumberWithTimestamp(component
, id
, component_sequence_number
,
208 base::TimeTicks::Now(), 1);
211 void LatencyInfo::AddLatencyNumberWithTimestamp(LatencyComponentType component
,
213 int64 component_sequence_number
,
214 base::TimeTicks time
,
215 uint32 event_count
) {
217 static const unsigned char* benchmark_enabled
=
218 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED("benchmark");
220 if (IsBeginComponent(component
)) {
221 // Should only ever add begin component once.
222 CHECK_EQ(-1, trace_id
);
223 trace_id
= component_sequence_number
;
225 if (*benchmark_enabled
) {
226 // The timestamp for ASYNC_BEGIN trace event is used for drawing the
227 // beginning of the trace event in trace viewer. For better visualization,
228 // for an input event, we want to draw the beginning as when the event is
229 // originally created, e.g. the timestamp of its ORIGINAL/UI_COMPONENT,
230 // not when we actually issue the ASYNC_BEGIN trace event.
231 LatencyComponent component
;
233 if (FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT
,
236 FindLatency(INPUT_EVENT_LATENCY_UI_COMPONENT
,
239 // The timestamp stored in ORIGINAL/UI_COMPONENT is using clock
240 // CLOCK_MONOTONIC while TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0
241 // expects timestamp using CLOCK_MONOTONIC or CLOCK_SYSTEM_TRACE (on
242 // CrOS). So we need to adjust the diff between in CLOCK_MONOTONIC and
243 // CLOCK_SYSTEM_TRACE. Note that the diff is drifting overtime so we
244 // can't use a static value.
245 int64 diff
= base::TimeTicks::Now().ToInternalValue() -
246 base::TimeTicks::NowFromSystemTraceTime().ToInternalValue();
247 ts
= component
.event_time
.ToInternalValue() - diff
;
249 ts
= base::TimeTicks::NowFromSystemTraceTime().ToInternalValue();
251 TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0(
254 TRACE_ID_DONT_MANGLE(trace_id
),
258 TRACE_EVENT_FLOW_BEGIN0(
259 "input,benchmark", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id
));
262 LatencyMap::key_type key
= std::make_pair(component
, id
);
263 LatencyMap::iterator it
= latency_components
.find(key
);
264 if (it
== latency_components
.end()) {
265 LatencyComponent info
= {component_sequence_number
, time
, event_count
};
266 latency_components
[key
] = info
;
268 it
->second
.sequence_number
= std::max(component_sequence_number
,
269 it
->second
.sequence_number
);
270 uint32 new_count
= event_count
+ it
->second
.event_count
;
271 if (event_count
> 0 && new_count
!= 0) {
272 // Do a weighted average, so that the new event_time is the average of
273 // the times of events currently in this structure with the time passed
275 it
->second
.event_time
+= (time
- it
->second
.event_time
) * event_count
/
277 it
->second
.event_count
= new_count
;
281 if (IsTerminalComponent(component
) && trace_id
!= -1) {
282 // Should only ever add terminal component once.
286 if (*benchmark_enabled
) {
287 TRACE_EVENT_ASYNC_END1("benchmark",
289 TRACE_ID_DONT_MANGLE(trace_id
),
290 "data", AsTraceableData(*this));
293 TRACE_EVENT_FLOW_END0(
294 "input,benchmark", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id
));
298 bool LatencyInfo::FindLatency(LatencyComponentType type
,
300 LatencyComponent
* output
) const {
301 LatencyMap::const_iterator it
= latency_components
.find(
302 std::make_pair(type
, id
));
303 if (it
== latency_components
.end())
306 *output
= it
->second
;
310 void LatencyInfo::RemoveLatency(LatencyComponentType type
) {
311 LatencyMap::iterator it
= latency_components
.begin();
312 while (it
!= latency_components
.end()) {
313 if (it
->first
.first
== type
) {
314 LatencyMap::iterator tmp
= it
;
316 latency_components
.erase(tmp
);
323 void LatencyInfo::Clear() {
324 latency_components
.clear();
327 void LatencyInfo::TraceEventType(const char* event_type
) {
328 TRACE_EVENT_ASYNC_STEP_INTO0("benchmark",
330 TRACE_ID_DONT_MANGLE(trace_id
),