OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "ui/events/latency_info.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <string> |
| 9 |
5 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/lazy_instance.h" |
6 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
7 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
8 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
9 #include "ui/events/latency_info.h" | |
10 | |
11 #include <algorithm> | |
12 | 15 |
13 namespace { | 16 namespace { |
14 | 17 |
15 const size_t kMaxLatencyInfoNumber = 100; | 18 const size_t kMaxLatencyInfoNumber = 100; |
16 | 19 |
17 const char* GetComponentName(ui::LatencyComponentType type) { | 20 const char* GetComponentName(ui::LatencyComponentType type) { |
18 #define CASE_TYPE(t) case ui::t: return #t | 21 #define CASE_TYPE(t) case ui::t: return #t |
19 switch (type) { | 22 switch (type) { |
20 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT); | 23 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT); |
21 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT); | 24 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT); |
22 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_SCROLL_UPDATE_MAIN_COMPONENT); | 25 CASE_TYPE(LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT); |
23 CASE_TYPE(INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT); | 26 CASE_TYPE(INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT); |
24 CASE_TYPE(INPUT_EVENT_LATENCY_FIRST_SCROLL_UPDATE_ORIGINAL_COMPONENT); | 27 CASE_TYPE(INPUT_EVENT_LATENCY_FIRST_SCROLL_UPDATE_ORIGINAL_COMPONENT); |
25 CASE_TYPE(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT); | 28 CASE_TYPE(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT); |
26 CASE_TYPE(INPUT_EVENT_LATENCY_UI_COMPONENT); | 29 CASE_TYPE(INPUT_EVENT_LATENCY_UI_COMPONENT); |
27 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_MAIN_COMPONENT); | 30 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_MAIN_COMPONENT); |
28 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_IMPL_COMPONENT); | 31 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_IMPL_COMPONENT); |
29 CASE_TYPE(INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT); | 32 CASE_TYPE(INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT); |
30 CASE_TYPE(INPUT_EVENT_LATENCY_ACK_RWH_COMPONENT); | 33 CASE_TYPE(INPUT_EVENT_LATENCY_ACK_RWH_COMPONENT); |
31 CASE_TYPE(WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT); | 34 CASE_TYPE(WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT); |
32 CASE_TYPE(WINDOW_OLD_SNAPSHOT_FRAME_NUMBER_COMPONENT); | |
33 CASE_TYPE(TAB_SHOW_COMPONENT); | 35 CASE_TYPE(TAB_SHOW_COMPONENT); |
34 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERER_SWAP_COMPONENT); | 36 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERER_SWAP_COMPONENT); |
35 CASE_TYPE(INPUT_EVENT_BROWSER_RECEIVED_RENDERER_SWAP_COMPONENT); | 37 CASE_TYPE(INPUT_EVENT_BROWSER_RECEIVED_RENDERER_SWAP_COMPONENT); |
36 CASE_TYPE(INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT); | 38 CASE_TYPE(INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT); |
37 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT); | 39 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT); |
38 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT); | 40 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT); |
39 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT); | 41 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT); |
40 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT); | 42 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT); |
41 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT); | 43 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT); |
42 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT); | 44 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT); |
(...skipping 19 matching lines...) Expand all Loading... |
62 case ui::INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT: | 64 case ui::INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT: |
63 return true; | 65 return true; |
64 default: | 66 default: |
65 return false; | 67 return false; |
66 } | 68 } |
67 } | 69 } |
68 | 70 |
69 bool IsBeginComponent(ui::LatencyComponentType type) { | 71 bool IsBeginComponent(ui::LatencyComponentType type) { |
70 return (type == ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT || | 72 return (type == ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT || |
71 type == ui::INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT || | 73 type == ui::INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT || |
72 type == ui::INPUT_EVENT_LATENCY_BEGIN_SCROLL_UPDATE_MAIN_COMPONENT); | 74 type == ui::LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT); |
| 75 } |
| 76 |
| 77 bool IsInputLatencyBeginComponent(ui::LatencyComponentType type) { |
| 78 return (type == ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT || |
| 79 type == ui::INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT); |
73 } | 80 } |
74 | 81 |
75 // This class is for converting latency info to trace buffer friendly format. | 82 // This class is for converting latency info to trace buffer friendly format. |
76 class LatencyInfoTracedValue | 83 class LatencyInfoTracedValue |
77 : public base::trace_event::ConvertableToTraceFormat { | 84 : public base::trace_event::ConvertableToTraceFormat { |
78 public: | 85 public: |
79 static scoped_refptr<ConvertableToTraceFormat> FromValue( | 86 static scoped_refptr<ConvertableToTraceFormat> FromValue( |
80 scoped_ptr<base::Value> value); | 87 scoped_ptr<base::Value> value); |
81 | 88 |
82 void AppendAsTraceFormat(std::string* out) const override; | 89 void AppendAsTraceFormat(std::string* out) const override; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 const ui::LatencyInfo& latency) { | 121 const ui::LatencyInfo& latency) { |
115 scoped_ptr<base::DictionaryValue> record_data(new base::DictionaryValue()); | 122 scoped_ptr<base::DictionaryValue> record_data(new base::DictionaryValue()); |
116 for (ui::LatencyInfo::LatencyMap::const_iterator it = | 123 for (ui::LatencyInfo::LatencyMap::const_iterator it = |
117 latency.latency_components.begin(); | 124 latency.latency_components.begin(); |
118 it != latency.latency_components.end(); ++it) { | 125 it != latency.latency_components.end(); ++it) { |
119 base::DictionaryValue* component_info = new base::DictionaryValue(); | 126 base::DictionaryValue* component_info = new base::DictionaryValue(); |
120 component_info->SetDouble("comp_id", static_cast<double>(it->first.second)); | 127 component_info->SetDouble("comp_id", static_cast<double>(it->first.second)); |
121 component_info->SetDouble( | 128 component_info->SetDouble( |
122 "time", static_cast<double>(it->second.event_time.ToInternalValue())); | 129 "time", static_cast<double>(it->second.event_time.ToInternalValue())); |
123 component_info->SetDouble("count", it->second.event_count); | 130 component_info->SetDouble("count", it->second.event_count); |
| 131 component_info->SetDouble("sequence_number", it->second.sequence_number); |
124 record_data->Set(GetComponentName(it->first.first), component_info); | 132 record_data->Set(GetComponentName(it->first.first), component_info); |
125 } | 133 } |
126 record_data->SetDouble("trace_id", static_cast<double>(latency.trace_id)); | 134 record_data->SetDouble("trace_id", static_cast<double>(latency.trace_id)); |
127 | 135 |
128 scoped_ptr<base::ListValue> coordinates(new base::ListValue()); | 136 scoped_ptr<base::ListValue> coordinates(new base::ListValue()); |
129 for (size_t i = 0; i < latency.input_coordinates_size; i++) { | 137 for (size_t i = 0; i < latency.input_coordinates_size; i++) { |
130 scoped_ptr<base::DictionaryValue> coordinate_pair( | 138 scoped_ptr<base::DictionaryValue> coordinate_pair( |
131 new base::DictionaryValue()); | 139 new base::DictionaryValue()); |
132 coordinate_pair->SetDouble("x", latency.input_coordinates[i].x); | 140 coordinate_pair->SetDouble("x", latency.input_coordinates[i].x); |
133 coordinate_pair->SetDouble("y", latency.input_coordinates[i].y); | 141 coordinate_pair->SetDouble("y", latency.input_coordinates[i].y); |
134 coordinates->Append(coordinate_pair.release()); | 142 coordinates->Append(coordinate_pair.release()); |
135 } | 143 } |
136 record_data->Set("coordinates", coordinates.release()); | 144 record_data->Set("coordinates", coordinates.release()); |
137 return LatencyInfoTracedValue::FromValue(record_data.Pass()); | 145 return LatencyInfoTracedValue::FromValue(record_data.Pass()); |
138 } | 146 } |
139 | 147 |
| 148 struct BenchmarkEnabledInitializer { |
| 149 BenchmarkEnabledInitializer() : |
| 150 benchmark_enabled(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( |
| 151 "benchmark")) { |
| 152 } |
| 153 |
| 154 const unsigned char* benchmark_enabled; |
| 155 }; |
| 156 |
| 157 static base::LazyInstance<BenchmarkEnabledInitializer>::Leaky |
| 158 g_benchmark_enabled = LAZY_INSTANCE_INITIALIZER; |
| 159 |
140 } // namespace | 160 } // namespace |
141 | 161 |
142 namespace ui { | 162 namespace ui { |
143 | 163 |
144 LatencyInfo::InputCoordinate::InputCoordinate() : x(0), y(0) { | 164 LatencyInfo::InputCoordinate::InputCoordinate() : x(0), y(0) { |
145 } | 165 } |
146 | 166 |
147 LatencyInfo::InputCoordinate::InputCoordinate(float x, float y) : x(x), y(y) { | 167 LatencyInfo::InputCoordinate::InputCoordinate(float x, float y) : x(x), y(y) { |
148 } | 168 } |
149 | 169 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 it->second.sequence_number, | 217 it->second.sequence_number, |
198 it->second.event_time, | 218 it->second.event_time, |
199 it->second.event_count); | 219 it->second.event_count); |
200 } | 220 } |
201 } | 221 } |
202 } | 222 } |
203 | 223 |
204 void LatencyInfo::AddLatencyNumber(LatencyComponentType component, | 224 void LatencyInfo::AddLatencyNumber(LatencyComponentType component, |
205 int64 id, | 225 int64 id, |
206 int64 component_sequence_number) { | 226 int64 component_sequence_number) { |
207 AddLatencyNumberWithTimestamp(component, id, component_sequence_number, | 227 AddLatencyNumberWithTimestampImpl(component, id, component_sequence_number, |
208 base::TimeTicks::Now(), 1); | 228 base::TimeTicks::Now(), 1, nullptr); |
| 229 } |
| 230 |
| 231 void LatencyInfo::AddLatencyNumberWithTraceName( |
| 232 LatencyComponentType component, |
| 233 int64 id, |
| 234 int64 component_sequence_number, |
| 235 const char* trace_name_str) { |
| 236 AddLatencyNumberWithTimestampImpl(component, id, component_sequence_number, |
| 237 base::TimeTicks::Now(), 1, trace_name_str); |
209 } | 238 } |
210 | 239 |
211 void LatencyInfo::AddLatencyNumberWithTimestamp(LatencyComponentType component, | 240 void LatencyInfo::AddLatencyNumberWithTimestamp(LatencyComponentType component, |
212 int64 id, | 241 int64 id, |
213 int64 component_sequence_number, | 242 int64 component_sequence_number, |
214 base::TimeTicks time, | 243 base::TimeTicks time, |
215 uint32 event_count) { | 244 uint32 event_count) { |
| 245 AddLatencyNumberWithTimestampImpl(component, id, component_sequence_number, |
| 246 time, event_count, nullptr); |
| 247 } |
216 | 248 |
217 static const unsigned char* benchmark_enabled = | 249 void LatencyInfo::AddLatencyNumberWithTimestampImpl( |
218 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED("benchmark"); | 250 LatencyComponentType component, |
| 251 int64 id, |
| 252 int64 component_sequence_number, |
| 253 base::TimeTicks time, |
| 254 uint32 event_count, |
| 255 const char* trace_name_str) { |
| 256 |
| 257 const unsigned char* benchmark_enabled = |
| 258 g_benchmark_enabled.Get().benchmark_enabled; |
219 | 259 |
220 if (IsBeginComponent(component)) { | 260 if (IsBeginComponent(component)) { |
221 // Should only ever add begin component once. | 261 // Should only ever add begin component once. |
222 CHECK_EQ(-1, trace_id); | 262 CHECK_EQ(-1, trace_id); |
223 trace_id = component_sequence_number; | 263 trace_id = component_sequence_number; |
224 | 264 |
225 if (*benchmark_enabled) { | 265 if (*benchmark_enabled) { |
226 // The timestamp for ASYNC_BEGIN trace event is used for drawing the | 266 // The timestamp for ASYNC_BEGIN trace event is used for drawing the |
227 // beginning of the trace event in trace viewer. For better visualization, | 267 // 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 | 268 // for an input event, we want to draw the beginning as when the event is |
(...skipping 12 matching lines...) Expand all Loading... |
241 // expects timestamp using CLOCK_MONOTONIC or CLOCK_SYSTEM_TRACE (on | 281 // expects timestamp using CLOCK_MONOTONIC or CLOCK_SYSTEM_TRACE (on |
242 // CrOS). So we need to adjust the diff between in CLOCK_MONOTONIC and | 282 // 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 | 283 // CLOCK_SYSTEM_TRACE. Note that the diff is drifting overtime so we |
244 // can't use a static value. | 284 // can't use a static value. |
245 base::TimeDelta diff = (base::TimeTicks::Now() - base::TimeTicks()) - | 285 base::TimeDelta diff = (base::TimeTicks::Now() - base::TimeTicks()) - |
246 (base::TraceTicks::Now() - base::TraceTicks()); | 286 (base::TraceTicks::Now() - base::TraceTicks()); |
247 ts = (begin_component.event_time - diff).ToInternalValue(); | 287 ts = (begin_component.event_time - diff).ToInternalValue(); |
248 } else { | 288 } else { |
249 ts = base::TraceTicks::Now().ToInternalValue(); | 289 ts = base::TraceTicks::Now().ToInternalValue(); |
250 } | 290 } |
251 TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0( | 291 |
252 "benchmark", | 292 if (trace_name_str) { |
253 "InputLatency", | 293 if (IsInputLatencyBeginComponent(component)) |
| 294 trace_name = std::string("InputLatency::") + trace_name_str; |
| 295 else |
| 296 trace_name = std::string("Latency::") + trace_name_str; |
| 297 } |
| 298 |
| 299 TRACE_EVENT_COPY_ASYNC_BEGIN_WITH_TIMESTAMP0( |
| 300 "benchmark,latencyInfo", |
| 301 trace_name.c_str(), |
254 TRACE_ID_DONT_MANGLE(trace_id), | 302 TRACE_ID_DONT_MANGLE(trace_id), |
255 ts); | 303 ts); |
256 } | 304 } |
257 | 305 |
258 TRACE_EVENT_FLOW_BEGIN0( | 306 TRACE_EVENT_FLOW_BEGIN1( |
259 "input,benchmark", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id)); | 307 "input,benchmark", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id), |
| 308 "trace_id", trace_id); |
260 } | 309 } |
261 | 310 |
262 LatencyMap::key_type key = std::make_pair(component, id); | 311 LatencyMap::key_type key = std::make_pair(component, id); |
263 LatencyMap::iterator it = latency_components.find(key); | 312 LatencyMap::iterator it = latency_components.find(key); |
264 if (it == latency_components.end()) { | 313 if (it == latency_components.end()) { |
265 LatencyComponent info = {component_sequence_number, time, event_count}; | 314 LatencyComponent info = {component_sequence_number, time, event_count}; |
266 latency_components[key] = info; | 315 latency_components[key] = info; |
267 } else { | 316 } else { |
268 it->second.sequence_number = std::max(component_sequence_number, | 317 it->second.sequence_number = std::max(component_sequence_number, |
269 it->second.sequence_number); | 318 it->second.sequence_number); |
270 uint32 new_count = event_count + it->second.event_count; | 319 uint32 new_count = event_count + it->second.event_count; |
271 if (event_count > 0 && new_count != 0) { | 320 if (event_count > 0 && new_count != 0) { |
272 // Do a weighted average, so that the new event_time is the average of | 321 // 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 | 322 // the times of events currently in this structure with the time passed |
274 // into this method. | 323 // into this method. |
275 it->second.event_time += (time - it->second.event_time) * event_count / | 324 it->second.event_time += (time - it->second.event_time) * event_count / |
276 new_count; | 325 new_count; |
277 it->second.event_count = new_count; | 326 it->second.event_count = new_count; |
278 } | 327 } |
279 } | 328 } |
280 | 329 |
281 if (IsTerminalComponent(component) && trace_id != -1) { | 330 if (IsTerminalComponent(component) && trace_id != -1) { |
282 // Should only ever add terminal component once. | 331 // Should only ever add terminal component once. |
283 CHECK(!terminated); | 332 CHECK(!terminated); |
284 terminated = true; | 333 terminated = true; |
285 | 334 |
286 if (*benchmark_enabled) { | 335 if (*benchmark_enabled) { |
287 TRACE_EVENT_ASYNC_END1("benchmark", | 336 TRACE_EVENT_COPY_ASYNC_END1("benchmark,latencyInfo", |
288 "InputLatency", | 337 trace_name.c_str(), |
289 TRACE_ID_DONT_MANGLE(trace_id), | 338 TRACE_ID_DONT_MANGLE(trace_id), |
290 "data", AsTraceableData(*this)); | 339 "data", AsTraceableData(*this)); |
291 } | 340 } |
292 | 341 |
293 TRACE_EVENT_FLOW_END0( | 342 // Removed - does not compile |
294 "input,benchmark", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id)); | 343 // TRACE_EVENT_FLOW_END_BIND_TO_ENCLOSING0( |
| 344 // "input,benchmark", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id)
); |
295 } | 345 } |
296 } | 346 } |
297 | 347 |
298 bool LatencyInfo::FindLatency(LatencyComponentType type, | 348 bool LatencyInfo::FindLatency(LatencyComponentType type, |
299 int64 id, | 349 int64 id, |
300 LatencyComponent* output) const { | 350 LatencyComponent* output) const { |
301 LatencyMap::const_iterator it = latency_components.find( | 351 LatencyMap::const_iterator it = latency_components.find( |
302 std::make_pair(type, id)); | 352 std::make_pair(type, id)); |
303 if (it == latency_components.end()) | 353 if (it == latency_components.end()) |
304 return false; | 354 return false; |
(...skipping 12 matching lines...) Expand all Loading... |
317 } else { | 367 } else { |
318 it++; | 368 it++; |
319 } | 369 } |
320 } | 370 } |
321 } | 371 } |
322 | 372 |
323 void LatencyInfo::Clear() { | 373 void LatencyInfo::Clear() { |
324 latency_components.clear(); | 374 latency_components.clear(); |
325 } | 375 } |
326 | 376 |
327 void LatencyInfo::TraceEventType(const char* event_type) { | |
328 TRACE_EVENT_ASYNC_STEP_INTO0("benchmark", | |
329 "InputLatency", | |
330 TRACE_ID_DONT_MANGLE(trace_id), | |
331 event_type); | |
332 } | |
333 | |
334 } // namespace ui | 377 } // namespace ui |
OLD | NEW |