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/debug/trace_event.h"
6 #include "base/json/json_writer.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/stringprintf.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_SCROLL_UPDATE_RWH_COMPONENT
);
22 CASE_TYPE(INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT
);
23 CASE_TYPE(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT
);
24 CASE_TYPE(INPUT_EVENT_LATENCY_UI_COMPONENT
);
25 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_COMPONENT
);
26 CASE_TYPE(INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT
);
27 CASE_TYPE(WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT
);
28 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT
);
29 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT
);
30 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT
);
31 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT
);
32 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT
);
33 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT
);
34 CASE_TYPE(LATENCY_INFO_LIST_TERMINATED_OVERFLOW_COMPONENT
);
36 DLOG(WARNING
) << "Unhandled LatencyComponentType.\n";
43 bool IsTerminalComponent(ui::LatencyComponentType type
) {
45 case ui::INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT
:
46 case ui::INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT
:
47 case ui::INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT
:
48 case ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT
:
49 case ui::INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT
:
50 case ui::INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT
:
51 case ui::LATENCY_INFO_LIST_TERMINATED_OVERFLOW_COMPONENT
:
58 bool IsBeginComponent(ui::LatencyComponentType type
) {
59 return (type
== ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
);
62 // This class is for converting latency info to trace buffer friendly format.
63 class LatencyInfoTracedValue
: public base::debug::ConvertableToTraceFormat
{
65 static scoped_refptr
<ConvertableToTraceFormat
> FromValue(
66 scoped_ptr
<base::Value
> value
);
68 virtual void AppendAsTraceFormat(std::string
* out
) const OVERRIDE
;
71 explicit LatencyInfoTracedValue(base::Value
* value
);
72 virtual ~LatencyInfoTracedValue();
74 scoped_ptr
<base::Value
> value_
;
76 DISALLOW_COPY_AND_ASSIGN(LatencyInfoTracedValue
);
79 scoped_refptr
<base::debug::ConvertableToTraceFormat
>
80 LatencyInfoTracedValue::FromValue(scoped_ptr
<base::Value
> value
) {
81 return scoped_refptr
<base::debug::ConvertableToTraceFormat
>(
82 new LatencyInfoTracedValue(value
.release()));
85 LatencyInfoTracedValue::~LatencyInfoTracedValue() {
88 void LatencyInfoTracedValue::AppendAsTraceFormat(std::string
* out
) const {
90 base::JSONWriter::Write(value_
.get(), &tmp
);
94 LatencyInfoTracedValue::LatencyInfoTracedValue(base::Value
* value
)
98 // Converts latencyinfo into format that can be dumped into trace buffer.
99 scoped_refptr
<base::debug::ConvertableToTraceFormat
> AsTraceableData(
100 const ui::LatencyInfo
& latency
) {
101 scoped_ptr
<base::DictionaryValue
> record_data(new base::DictionaryValue());
102 for (ui::LatencyInfo::LatencyMap::const_iterator it
=
103 latency
.latency_components
.begin();
104 it
!= latency
.latency_components
.end(); ++it
) {
105 base::DictionaryValue
* component_info
= new base::DictionaryValue();
106 component_info
->SetDouble("comp_id", it
->first
.second
);
107 component_info
->SetDouble("time", it
->second
.event_time
.ToInternalValue());
108 component_info
->SetDouble("count", it
->second
.event_count
);
109 record_data
->Set(GetComponentName(it
->first
.first
), component_info
);
111 record_data
->SetDouble("trace_id", latency
.trace_id
);
112 return LatencyInfoTracedValue::FromValue(record_data
.PassAs
<base::Value
>());
119 LatencyInfo::LatencyInfo() : trace_id(-1), terminated(false) {
122 LatencyInfo::~LatencyInfo() {
125 bool LatencyInfo::Verify(const std::vector
<LatencyInfo
>& latency_info
,
126 const char* referring_msg
) {
127 if (latency_info
.size() > kMaxLatencyInfoNumber
) {
128 LOG(ERROR
) << referring_msg
<< ", LatencyInfo vector size "
129 << latency_info
.size() << " is too big.";
135 void LatencyInfo::CopyLatencyFrom(const LatencyInfo
& other
,
136 LatencyComponentType type
) {
137 for (LatencyMap::const_iterator it
= other
.latency_components
.begin();
138 it
!= other
.latency_components
.end();
140 if (it
->first
.first
== type
) {
141 AddLatencyNumberWithTimestamp(it
->first
.first
,
143 it
->second
.sequence_number
,
144 it
->second
.event_time
,
145 it
->second
.event_count
);
150 void LatencyInfo::AddNewLatencyFrom(const LatencyInfo
& other
) {
151 for (LatencyMap::const_iterator it
= other
.latency_components
.begin();
152 it
!= other
.latency_components
.end();
154 if (!FindLatency(it
->first
.first
, it
->first
.second
, NULL
)) {
155 AddLatencyNumberWithTimestamp(it
->first
.first
,
157 it
->second
.sequence_number
,
158 it
->second
.event_time
,
159 it
->second
.event_count
);
164 void LatencyInfo::AddLatencyNumber(LatencyComponentType component
,
166 int64 component_sequence_number
) {
167 AddLatencyNumberWithTimestamp(component
, id
, component_sequence_number
,
168 base::TimeTicks::HighResNow(), 1);
171 void LatencyInfo::AddLatencyNumberWithTimestamp(LatencyComponentType component
,
173 int64 component_sequence_number
,
174 base::TimeTicks time
,
175 uint32 event_count
) {
176 if (IsBeginComponent(component
)) {
177 // Should only ever add begin component once.
178 CHECK_EQ(-1, trace_id
);
179 trace_id
= component_sequence_number
;
180 TRACE_EVENT_ASYNC_BEGIN0("benchmark",
182 TRACE_ID_DONT_MANGLE(trace_id
));
183 TRACE_EVENT_FLOW_BEGIN0(
184 "input", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id
));
187 LatencyMap::key_type key
= std::make_pair(component
, id
);
188 LatencyMap::iterator it
= latency_components
.find(key
);
189 if (it
== latency_components
.end()) {
190 LatencyComponent info
= {component_sequence_number
, time
, event_count
};
191 latency_components
[key
] = info
;
193 it
->second
.sequence_number
= std::max(component_sequence_number
,
194 it
->second
.sequence_number
);
195 uint32 new_count
= event_count
+ it
->second
.event_count
;
196 if (event_count
> 0 && new_count
!= 0) {
197 // Do a weighted average, so that the new event_time is the average of
198 // the times of events currently in this structure with the time passed
200 it
->second
.event_time
+= (time
- it
->second
.event_time
) * event_count
/
202 it
->second
.event_count
= new_count
;
206 if (IsTerminalComponent(component
) && trace_id
!= -1) {
207 // Should only ever add terminal component once.
210 TRACE_EVENT_ASYNC_END1("benchmark",
212 TRACE_ID_DONT_MANGLE(trace_id
),
213 "data", AsTraceableData(*this));
214 TRACE_EVENT_FLOW_END0(
215 "input", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id
));
219 bool LatencyInfo::FindLatency(LatencyComponentType type
,
221 LatencyComponent
* output
) const {
222 LatencyMap::const_iterator it
= latency_components
.find(
223 std::make_pair(type
, id
));
224 if (it
== latency_components
.end())
227 *output
= it
->second
;
231 void LatencyInfo::RemoveLatency(LatencyComponentType type
) {
232 LatencyMap::iterator it
= latency_components
.begin();
233 while (it
!= latency_components
.end()) {
234 if (it
->first
.first
== type
) {
235 LatencyMap::iterator tmp
= it
;
237 latency_components
.erase(tmp
);
244 void LatencyInfo::Clear() {
245 latency_components
.clear();
248 void LatencyInfo::TraceEventType(const char* event_type
) {
249 TRACE_EVENT_ASYNC_STEP_INTO0("benchmark",
251 TRACE_ID_DONT_MANGLE(trace_id
),