Allow the externalfile scheme to be whitelisted as an allowed scheme for component...
[chromium-blink-merge.git] / ui / events / latency_info.cc
blob1bcd43f8b504ba246989bfdfc9390a2d3b1f9763
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"
11 #include <algorithm>
13 namespace {
15 const size_t kMaxLatencyInfoNumber = 100;
17 const char* GetComponentName(ui::LatencyComponentType type) {
18 #define CASE_TYPE(t) case ui::t: return #t
19 switch (type) {
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);
42 default:
43 DLOG(WARNING) << "Unhandled LatencyComponentType.\n";
44 break;
46 #undef CASE_TYPE
47 return "unknown";
50 bool IsTerminalComponent(ui::LatencyComponentType type) {
51 switch (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:
60 return true;
61 default:
62 return false;
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 {
75 public:
76 static scoped_refptr<ConvertableToTraceFormat> FromValue(
77 scoped_ptr<base::Value> value);
79 void AppendAsTraceFormat(std::string* out) const override;
81 private:
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 {
100 std::string tmp;
101 base::JSONWriter::Write(value_.get(), &tmp);
102 *out += tmp;
105 LatencyInfoTracedValue::LatencyInfoTracedValue(base::Value* value)
106 : 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());
137 } // namespace
139 namespace ui {
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.";
159 return false;
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.";
165 return false;
169 return true;
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();
176 ++it) {
177 if (it->first.first == type) {
178 AddLatencyNumberWithTimestamp(it->first.first,
179 it->first.second,
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();
190 ++it) {
191 if (!FindLatency(it->first.first, it->first.second, NULL)) {
192 AddLatencyNumberWithTimestamp(it->first.first,
193 it->first.second,
194 it->second.sequence_number,
195 it->second.event_time,
196 it->second.event_count);
201 void LatencyInfo::AddLatencyNumber(LatencyComponentType component,
202 int64 id,
203 int64 component_sequence_number) {
204 AddLatencyNumberWithTimestamp(component, id, component_sequence_number,
205 base::TimeTicks::Now(), 1);
208 void LatencyInfo::AddLatencyNumberWithTimestamp(LatencyComponentType component,
209 int64 id,
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;
229 int64 ts = 0;
230 if (FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
232 &component) ||
233 FindLatency(INPUT_EVENT_LATENCY_UI_COMPONENT,
235 &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;
245 } else {
246 ts = base::TimeTicks::NowFromSystemTraceTime().ToInternalValue();
248 TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0(
249 "benchmark",
250 "InputLatency",
251 TRACE_ID_DONT_MANGLE(trace_id),
252 ts);
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;
264 } else {
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
271 // into this method.
272 it->second.event_time += (time - it->second.event_time) * event_count /
273 new_count;
274 it->second.event_count = new_count;
278 if (IsTerminalComponent(component) && trace_id != -1) {
279 // Should only ever add terminal component once.
280 CHECK(!terminated);
281 terminated = true;
283 if (*benchmark_enabled) {
284 TRACE_EVENT_ASYNC_END1("benchmark",
285 "InputLatency",
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,
296 int64 id,
297 LatencyComponent* output) const {
298 LatencyMap::const_iterator it = latency_components.find(
299 std::make_pair(type, id));
300 if (it == latency_components.end())
301 return false;
302 if (output)
303 *output = it->second;
304 return true;
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;
312 ++it;
313 latency_components.erase(tmp);
314 } else {
315 it++;
320 void LatencyInfo::Clear() {
321 latency_components.clear();
324 void LatencyInfo::TraceEventType(const char* event_type) {
325 TRACE_EVENT_ASYNC_STEP_INTO0("benchmark",
326 "InputLatency",
327 TRACE_ID_DONT_MANGLE(trace_id),
328 event_type);
331 } // namespace ui