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_COMPONENT
);
28 CASE_TYPE(INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT
);
29 CASE_TYPE(INPUT_EVENT_LATENCY_ACK_RWH_COMPONENT
);
30 CASE_TYPE(WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT
);
31 CASE_TYPE(WINDOW_OLD_SNAPSHOT_FRAME_NUMBER_COMPONENT
);
32 CASE_TYPE(INPUT_EVENT_BROWSER_RECEIVED_RENDERER_SWAP_COMPONENT
);
33 CASE_TYPE(INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT
);
34 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT
);
35 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT
);
36 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT
);
37 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT
);
38 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT
);
39 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT
);
40 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT
);
41 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT
);
43 DLOG(WARNING
) << "Unhandled LatencyComponentType.\n";
50 bool IsTerminalComponent(ui::LatencyComponentType type
) {
52 case ui::INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT
:
53 case ui::INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT
:
54 case ui::INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT
:
55 case ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT
:
56 case ui::INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT
:
57 case ui::INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT
:
58 case ui::INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT
:
59 case ui::INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT
:
66 bool IsBeginComponent(ui::LatencyComponentType type
) {
67 return (type
== ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
||
68 type
== ui::INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT
||
69 type
== ui::INPUT_EVENT_LATENCY_BEGIN_SCROLL_UPDATE_MAIN_COMPONENT
);
72 // This class is for converting latency info to trace buffer friendly format.
73 class LatencyInfoTracedValue
74 : public base::trace_event::ConvertableToTraceFormat
{
76 static scoped_refptr
<ConvertableToTraceFormat
> FromValue(
77 scoped_ptr
<base::Value
> value
);
79 void AppendAsTraceFormat(std::string
* out
) const override
;
82 explicit LatencyInfoTracedValue(base::Value
* value
);
83 ~LatencyInfoTracedValue() override
;
85 scoped_ptr
<base::Value
> value_
;
87 DISALLOW_COPY_AND_ASSIGN(LatencyInfoTracedValue
);
90 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
91 LatencyInfoTracedValue::FromValue(scoped_ptr
<base::Value
> value
) {
92 return scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>(
93 new LatencyInfoTracedValue(value
.release()));
96 LatencyInfoTracedValue::~LatencyInfoTracedValue() {
99 void LatencyInfoTracedValue::AppendAsTraceFormat(std::string
* out
) const {
101 base::JSONWriter::Write(value_
.get(), &tmp
);
105 LatencyInfoTracedValue::LatencyInfoTracedValue(base::Value
* value
)
109 // Converts latencyinfo into format that can be dumped into trace buffer.
110 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> AsTraceableData(
111 const ui::LatencyInfo
& latency
) {
112 scoped_ptr
<base::DictionaryValue
> record_data(new base::DictionaryValue());
113 for (ui::LatencyInfo::LatencyMap::const_iterator it
=
114 latency
.latency_components
.begin();
115 it
!= latency
.latency_components
.end(); ++it
) {
116 base::DictionaryValue
* component_info
= new base::DictionaryValue();
117 component_info
->SetDouble("comp_id", static_cast<double>(it
->first
.second
));
118 component_info
->SetDouble(
119 "time", static_cast<double>(it
->second
.event_time
.ToInternalValue()));
120 component_info
->SetDouble("count", it
->second
.event_count
);
121 record_data
->Set(GetComponentName(it
->first
.first
), component_info
);
123 record_data
->SetDouble("trace_id", static_cast<double>(latency
.trace_id
));
125 scoped_ptr
<base::ListValue
> coordinates(new base::ListValue());
126 for (size_t i
= 0; i
< latency
.input_coordinates_size
; i
++) {
127 scoped_ptr
<base::DictionaryValue
> coordinate_pair(
128 new base::DictionaryValue());
129 coordinate_pair
->SetDouble("x", latency
.input_coordinates
[i
].x
);
130 coordinate_pair
->SetDouble("y", latency
.input_coordinates
[i
].y
);
131 coordinates
->Append(coordinate_pair
.release());
133 record_data
->Set("coordinates", coordinates
.release());
134 return LatencyInfoTracedValue::FromValue(record_data
.Pass());
141 LatencyInfo::InputCoordinate::InputCoordinate() : x(0), y(0) {
144 LatencyInfo::InputCoordinate::InputCoordinate(float x
, float y
) : x(x
), y(y
) {
147 LatencyInfo::LatencyInfo()
148 : input_coordinates_size(0), trace_id(-1), terminated(false) {
151 LatencyInfo::~LatencyInfo() {
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.";
161 for (size_t i
= 0; i
< latency_info
.size(); i
++) {
162 if (latency_info
[i
].input_coordinates_size
> kMaxInputCoordinates
) {
163 LOG(ERROR
) << referring_msg
<< ", coordinate vector size "
164 << latency_info
[i
].input_coordinates_size
<< " is too big.";
172 void LatencyInfo::CopyLatencyFrom(const LatencyInfo
& other
,
173 LatencyComponentType type
) {
174 for (LatencyMap::const_iterator it
= other
.latency_components
.begin();
175 it
!= other
.latency_components
.end();
177 if (it
->first
.first
== type
) {
178 AddLatencyNumberWithTimestamp(it
->first
.first
,
180 it
->second
.sequence_number
,
181 it
->second
.event_time
,
182 it
->second
.event_count
);
187 void LatencyInfo::AddNewLatencyFrom(const LatencyInfo
& other
) {
188 for (LatencyMap::const_iterator it
= other
.latency_components
.begin();
189 it
!= other
.latency_components
.end();
191 if (!FindLatency(it
->first
.first
, it
->first
.second
, NULL
)) {
192 AddLatencyNumberWithTimestamp(it
->first
.first
,
194 it
->second
.sequence_number
,
195 it
->second
.event_time
,
196 it
->second
.event_count
);
201 void LatencyInfo::AddLatencyNumber(LatencyComponentType component
,
203 int64 component_sequence_number
) {
204 AddLatencyNumberWithTimestamp(component
, id
, component_sequence_number
,
205 base::TimeTicks::Now(), 1);
208 void LatencyInfo::AddLatencyNumberWithTimestamp(LatencyComponentType component
,
210 int64 component_sequence_number
,
211 base::TimeTicks time
,
212 uint32 event_count
) {
214 static const unsigned char* benchmark_enabled
=
215 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED("benchmark");
217 if (IsBeginComponent(component
)) {
218 // Should only ever add begin component once.
219 CHECK_EQ(-1, trace_id
);
220 trace_id
= component_sequence_number
;
222 if (*benchmark_enabled
) {
223 // The timestamp for ASYNC_BEGIN trace event is used for drawing the
224 // beginning of the trace event in trace viewer. For better visualization,
225 // for an input event, we want to draw the beginning as when the event is
226 // originally created, e.g. the timestamp of its ORIGINAL/UI_COMPONENT,
227 // not when we actually issue the ASYNC_BEGIN trace event.
228 LatencyComponent component
;
230 if (FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT
,
233 FindLatency(INPUT_EVENT_LATENCY_UI_COMPONENT
,
236 // The timestamp stored in ORIGINAL/UI_COMPONENT is using clock
237 // CLOCK_MONOTONIC while TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0
238 // expects timestamp using CLOCK_MONOTONIC or CLOCK_SYSTEM_TRACE (on
239 // CrOS). So we need to adjust the diff between in CLOCK_MONOTONIC and
240 // CLOCK_SYSTEM_TRACE. Note that the diff is drifting overtime so we
241 // can't use a static value.
242 int64 diff
= base::TimeTicks::Now().ToInternalValue() -
243 base::TimeTicks::NowFromSystemTraceTime().ToInternalValue();
244 ts
= component
.event_time
.ToInternalValue() - diff
;
246 ts
= base::TimeTicks::NowFromSystemTraceTime().ToInternalValue();
248 TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0(
251 TRACE_ID_DONT_MANGLE(trace_id
),
255 TRACE_EVENT_FLOW_BEGIN0(
256 "input,benchmark", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id
));
259 LatencyMap::key_type key
= std::make_pair(component
, id
);
260 LatencyMap::iterator it
= latency_components
.find(key
);
261 if (it
== latency_components
.end()) {
262 LatencyComponent info
= {component_sequence_number
, time
, event_count
};
263 latency_components
[key
] = info
;
265 it
->second
.sequence_number
= std::max(component_sequence_number
,
266 it
->second
.sequence_number
);
267 uint32 new_count
= event_count
+ it
->second
.event_count
;
268 if (event_count
> 0 && new_count
!= 0) {
269 // Do a weighted average, so that the new event_time is the average of
270 // the times of events currently in this structure with the time passed
272 it
->second
.event_time
+= (time
- it
->second
.event_time
) * event_count
/
274 it
->second
.event_count
= new_count
;
278 if (IsTerminalComponent(component
) && trace_id
!= -1) {
279 // Should only ever add terminal component once.
283 if (*benchmark_enabled
) {
284 TRACE_EVENT_ASYNC_END1("benchmark",
286 TRACE_ID_DONT_MANGLE(trace_id
),
287 "data", AsTraceableData(*this));
290 TRACE_EVENT_FLOW_END0(
291 "input,benchmark", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id
));
295 bool LatencyInfo::FindLatency(LatencyComponentType type
,
297 LatencyComponent
* output
) const {
298 LatencyMap::const_iterator it
= latency_components
.find(
299 std::make_pair(type
, id
));
300 if (it
== latency_components
.end())
303 *output
= it
->second
;
307 void LatencyInfo::RemoveLatency(LatencyComponentType type
) {
308 LatencyMap::iterator it
= latency_components
.begin();
309 while (it
!= latency_components
.end()) {
310 if (it
->first
.first
== type
) {
311 LatencyMap::iterator tmp
= it
;
313 latency_components
.erase(tmp
);
320 void LatencyInfo::Clear() {
321 latency_components
.clear();
324 void LatencyInfo::TraceEventType(const char* event_type
) {
325 TRACE_EVENT_ASYNC_STEP_INTO0("benchmark",
327 TRACE_ID_DONT_MANGLE(trace_id
),