Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: ui/latency_info/latency_info.cc

Issue 1871233002: Move LatencyInfo to ui/latency_info (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Simplify ui/events OWNERS Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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" 5 #include "ui/latency_info/latency_info.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <string> 10 #include <string>
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/json/json_writer.h" 13 #include "base/json/json_writer.h"
14 #include "base/lazy_instance.h" 14 #include "base/lazy_instance.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 17
18 namespace { 18 namespace {
19 19
20 const size_t kMaxLatencyInfoNumber = 100; 20 const size_t kMaxLatencyInfoNumber = 100;
21 21
22 const char* GetComponentName(ui::LatencyComponentType type) { 22 const char* GetComponentName(ui::LatencyComponentType type) {
23 #define CASE_TYPE(t) case ui::t: return #t 23 #define CASE_TYPE(t) \
24 case ui::t: \
25 return #t
24 switch (type) { 26 switch (type) {
25 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT); 27 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT);
26 CASE_TYPE(LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT); 28 CASE_TYPE(LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT);
27 CASE_TYPE(INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT); 29 CASE_TYPE(INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT);
28 CASE_TYPE(INPUT_EVENT_LATENCY_FIRST_SCROLL_UPDATE_ORIGINAL_COMPONENT); 30 CASE_TYPE(INPUT_EVENT_LATENCY_FIRST_SCROLL_UPDATE_ORIGINAL_COMPONENT);
29 CASE_TYPE(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT); 31 CASE_TYPE(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT);
30 CASE_TYPE(INPUT_EVENT_LATENCY_UI_COMPONENT); 32 CASE_TYPE(INPUT_EVENT_LATENCY_UI_COMPONENT);
31 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_MAIN_COMPONENT); 33 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_MAIN_COMPONENT);
32 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_IMPL_COMPONENT); 34 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_IMPL_COMPONENT);
33 CASE_TYPE(INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT); 35 CASE_TYPE(INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 99
98 DISALLOW_COPY_AND_ASSIGN(LatencyInfoTracedValue); 100 DISALLOW_COPY_AND_ASSIGN(LatencyInfoTracedValue);
99 }; 101 };
100 102
101 scoped_ptr<base::trace_event::ConvertableToTraceFormat> 103 scoped_ptr<base::trace_event::ConvertableToTraceFormat>
102 LatencyInfoTracedValue::FromValue(scoped_ptr<base::Value> value) { 104 LatencyInfoTracedValue::FromValue(scoped_ptr<base::Value> value) {
103 return scoped_ptr<base::trace_event::ConvertableToTraceFormat>( 105 return scoped_ptr<base::trace_event::ConvertableToTraceFormat>(
104 new LatencyInfoTracedValue(value.release())); 106 new LatencyInfoTracedValue(value.release()));
105 } 107 }
106 108
107 LatencyInfoTracedValue::~LatencyInfoTracedValue() { 109 LatencyInfoTracedValue::~LatencyInfoTracedValue() {}
108 }
109 110
110 void LatencyInfoTracedValue::AppendAsTraceFormat(std::string* out) const { 111 void LatencyInfoTracedValue::AppendAsTraceFormat(std::string* out) const {
111 std::string tmp; 112 std::string tmp;
112 base::JSONWriter::Write(*value_, &tmp); 113 base::JSONWriter::Write(*value_, &tmp);
113 *out += tmp; 114 *out += tmp;
114 } 115 }
115 116
116 LatencyInfoTracedValue::LatencyInfoTracedValue(base::Value* value) 117 LatencyInfoTracedValue::LatencyInfoTracedValue(base::Value* value)
117 : value_(value) { 118 : value_(value) {}
118 }
119 119
120 const char kTraceCategoriesForAsyncEvents[] = "benchmark,latencyInfo"; 120 const char kTraceCategoriesForAsyncEvents[] = "benchmark,latencyInfo";
121 121
122 struct LatencyInfoEnabledInitializer { 122 struct LatencyInfoEnabledInitializer {
123 LatencyInfoEnabledInitializer() : 123 LatencyInfoEnabledInitializer()
124 latency_info_enabled(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( 124 : latency_info_enabled(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
125 kTraceCategoriesForAsyncEvents)) { 125 kTraceCategoriesForAsyncEvents)) {}
126 }
127 126
128 const unsigned char* latency_info_enabled; 127 const unsigned char* latency_info_enabled;
129 }; 128 };
130 129
131 static base::LazyInstance<LatencyInfoEnabledInitializer>::Leaky 130 static base::LazyInstance<LatencyInfoEnabledInitializer>::Leaky
132 g_latency_info_enabled = LAZY_INSTANCE_INITIALIZER; 131 g_latency_info_enabled = LAZY_INSTANCE_INITIALIZER;
133 132
134 } // namespace 133 } // namespace
135 134
136 namespace ui { 135 namespace ui {
137 136
138 LatencyInfo::InputCoordinate::InputCoordinate() : x(0), y(0) { 137 LatencyInfo::InputCoordinate::InputCoordinate() : x(0), y(0) {}
139 }
140 138
141 LatencyInfo::InputCoordinate::InputCoordinate(float x, float y) : x(x), y(y) { 139 LatencyInfo::InputCoordinate::InputCoordinate(float x, float y) : x(x), y(y) {}
142 }
143 140
144 LatencyInfo::LatencyInfo() 141 LatencyInfo::LatencyInfo()
145 : input_coordinates_size_(0), 142 : input_coordinates_size_(0),
146 trace_id_(-1), 143 trace_id_(-1),
147 coalesced_(false), 144 coalesced_(false),
148 terminated_(false) {} 145 terminated_(false) {}
149 146
150 LatencyInfo::LatencyInfo(const LatencyInfo& other) = default; 147 LatencyInfo::LatencyInfo(const LatencyInfo& other) = default;
151 148
152 LatencyInfo::~LatencyInfo() {} 149 LatencyInfo::~LatencyInfo() {}
153 150
154 LatencyInfo::LatencyInfo(int64_t trace_id, bool terminated) 151 LatencyInfo::LatencyInfo(int64_t trace_id, bool terminated)
155 : input_coordinates_size_(0), 152 : input_coordinates_size_(0),
156 trace_id_(trace_id), 153 trace_id_(trace_id),
157 terminated_(terminated) {} 154 terminated_(terminated) {}
158 155
159 bool LatencyInfo::Verify(const std::vector<LatencyInfo>& latency_info, 156 bool LatencyInfo::Verify(const std::vector<LatencyInfo>& latency_info,
160 const char* referring_msg) { 157 const char* referring_msg) {
161 if (latency_info.size() > kMaxLatencyInfoNumber) { 158 if (latency_info.size() > kMaxLatencyInfoNumber) {
162 LOG(ERROR) << referring_msg << ", LatencyInfo vector size " 159 LOG(ERROR) << referring_msg << ", LatencyInfo vector size "
163 << latency_info.size() << " is too big."; 160 << latency_info.size() << " is too big.";
164 TRACE_EVENT_INSTANT1("input,benchmark", "LatencyInfo::Verify Fails", 161 TRACE_EVENT_INSTANT1("input,benchmark", "LatencyInfo::Verify Fails",
165 TRACE_EVENT_SCOPE_GLOBAL, 162 TRACE_EVENT_SCOPE_GLOBAL, "size", latency_info.size());
166 "size", latency_info.size());
167 return false; 163 return false;
168 } 164 }
169 return true; 165 return true;
170 } 166 }
171 167
172 void LatencyInfo::CopyLatencyFrom(const LatencyInfo& other, 168 void LatencyInfo::CopyLatencyFrom(const LatencyInfo& other,
173 LatencyComponentType type) { 169 LatencyComponentType type) {
174 for (const auto& lc : other.latency_components()) { 170 for (const auto& lc : other.latency_components()) {
175 if (lc.first.first == type) { 171 if (lc.first.first == type) {
176 AddLatencyNumberWithTimestamp(lc.first.first, 172 AddLatencyNumberWithTimestamp(
177 lc.first.second, 173 lc.first.first, lc.first.second, lc.second.sequence_number,
178 lc.second.sequence_number, 174 lc.second.event_time, lc.second.event_count);
179 lc.second.event_time,
180 lc.second.event_count);
181 } 175 }
182 } 176 }
183 } 177 }
184 178
185 void LatencyInfo::AddNewLatencyFrom(const LatencyInfo& other) { 179 void LatencyInfo::AddNewLatencyFrom(const LatencyInfo& other) {
186 for (const auto& lc : other.latency_components()) { 180 for (const auto& lc : other.latency_components()) {
187 if (!FindLatency(lc.first.first, lc.first.second, NULL)) { 181 if (!FindLatency(lc.first.first, lc.first.second, NULL)) {
188 AddLatencyNumberWithTimestamp(lc.first.first, 182 AddLatencyNumberWithTimestamp(
189 lc.first.second, 183 lc.first.first, lc.first.second, lc.second.sequence_number,
190 lc.second.sequence_number, 184 lc.second.event_time, lc.second.event_count);
191 lc.second.event_time,
192 lc.second.event_count);
193 } 185 }
194 } 186 }
195 } 187 }
196 188
197 void LatencyInfo::AddLatencyNumber(LatencyComponentType component, 189 void LatencyInfo::AddLatencyNumber(LatencyComponentType component,
198 int64_t id, 190 int64_t id,
199 int64_t component_sequence_number) { 191 int64_t component_sequence_number) {
200 AddLatencyNumberWithTimestampImpl(component, id, component_sequence_number, 192 AddLatencyNumberWithTimestampImpl(component, id, component_sequence_number,
201 base::TimeTicks::Now(), 1, nullptr); 193 base::TimeTicks::Now(), 1, nullptr);
202 } 194 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 trace_id_ = component_sequence_number; 228 trace_id_ = component_sequence_number;
237 229
238 if (*latency_info_enabled) { 230 if (*latency_info_enabled) {
239 // The timestamp for ASYNC_BEGIN trace event is used for drawing the 231 // The timestamp for ASYNC_BEGIN trace event is used for drawing the
240 // beginning of the trace event in trace viewer. For better visualization, 232 // beginning of the trace event in trace viewer. For better visualization,
241 // for an input event, we want to draw the beginning as when the event is 233 // for an input event, we want to draw the beginning as when the event is
242 // originally created, e.g. the timestamp of its ORIGINAL/UI_COMPONENT, 234 // originally created, e.g. the timestamp of its ORIGINAL/UI_COMPONENT,
243 // not when we actually issue the ASYNC_BEGIN trace event. 235 // not when we actually issue the ASYNC_BEGIN trace event.
244 LatencyComponent begin_component; 236 LatencyComponent begin_component;
245 int64_t ts = 0; 237 int64_t ts = 0;
246 if (FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 238 if (FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, 0,
247 0,
248 &begin_component) || 239 &begin_component) ||
249 FindLatency(INPUT_EVENT_LATENCY_UI_COMPONENT, 240 FindLatency(INPUT_EVENT_LATENCY_UI_COMPONENT, 0, &begin_component)) {
250 0,
251 &begin_component)) {
252 ts = begin_component.event_time.ToInternalValue(); 241 ts = begin_component.event_time.ToInternalValue();
253 } else { 242 } else {
254 ts = base::TimeTicks::Now().ToInternalValue(); 243 ts = base::TimeTicks::Now().ToInternalValue();
255 } 244 }
256 245
257 if (trace_name_str) { 246 if (trace_name_str) {
258 if (IsInputLatencyBeginComponent(component)) 247 if (IsInputLatencyBeginComponent(component))
259 trace_name_ = std::string("InputLatency::") + trace_name_str; 248 trace_name_ = std::string("InputLatency::") + trace_name_str;
260 else 249 else
261 trace_name_ = std::string("Latency::") + trace_name_str; 250 trace_name_ = std::string("Latency::") + trace_name_str;
262 } 251 }
263 252
264 TRACE_EVENT_COPY_ASYNC_BEGIN_WITH_TIMESTAMP0( 253 TRACE_EVENT_COPY_ASYNC_BEGIN_WITH_TIMESTAMP0(
265 kTraceCategoriesForAsyncEvents, 254 kTraceCategoriesForAsyncEvents, trace_name_.c_str(),
266 trace_name_.c_str(), 255 TRACE_ID_DONT_MANGLE(trace_id_), ts);
267 TRACE_ID_DONT_MANGLE(trace_id_),
268 ts);
269 } 256 }
270 257
271 TRACE_EVENT_WITH_FLOW1("input,benchmark", 258 TRACE_EVENT_WITH_FLOW1("input,benchmark", "LatencyInfo.Flow",
272 "LatencyInfo.Flow",
273 TRACE_ID_DONT_MANGLE(trace_id_), 259 TRACE_ID_DONT_MANGLE(trace_id_),
274 TRACE_EVENT_FLAG_FLOW_OUT, 260 TRACE_EVENT_FLAG_FLOW_OUT, "trace_id", trace_id_);
275 "trace_id", trace_id_);
276 } 261 }
277 262
278 LatencyMap::key_type key = std::make_pair(component, id); 263 LatencyMap::key_type key = std::make_pair(component, id);
279 LatencyMap::iterator it = latency_components_.find(key); 264 LatencyMap::iterator it = latency_components_.find(key);
280 if (it == latency_components_.end()) { 265 if (it == latency_components_.end()) {
281 LatencyComponent info = {component_sequence_number, time, event_count}; 266 LatencyComponent info = {component_sequence_number, time, event_count};
282 latency_components_[key] = info; 267 latency_components_[key] = info;
283 } else { 268 } else {
284 it->second.sequence_number = std::max(component_sequence_number, 269 it->second.sequence_number =
285 it->second.sequence_number); 270 std::max(component_sequence_number, it->second.sequence_number);
286 uint32_t new_count = event_count + it->second.event_count; 271 uint32_t new_count = event_count + it->second.event_count;
287 if (event_count > 0 && new_count != 0) { 272 if (event_count > 0 && new_count != 0) {
288 // Do a weighted average, so that the new event_time is the average of 273 // Do a weighted average, so that the new event_time is the average of
289 // the times of events currently in this structure with the time passed 274 // the times of events currently in this structure with the time passed
290 // into this method. 275 // into this method.
291 it->second.event_time += (time - it->second.event_time) * event_count / 276 it->second.event_time +=
292 new_count; 277 (time - it->second.event_time) * event_count / new_count;
293 it->second.event_count = new_count; 278 it->second.event_count = new_count;
294 } 279 }
295 } 280 }
296 281
297 if (IsTerminalComponent(component) && trace_id_ != -1) { 282 if (IsTerminalComponent(component) && trace_id_ != -1) {
298 // Should only ever add terminal component once. 283 // Should only ever add terminal component once.
299 CHECK(!terminated_); 284 CHECK(!terminated_);
300 terminated_ = true; 285 terminated_ = true;
301 286
302 if (*latency_info_enabled) { 287 if (*latency_info_enabled) {
303 TRACE_EVENT_COPY_ASYNC_END2(kTraceCategoriesForAsyncEvents, 288 TRACE_EVENT_COPY_ASYNC_END2(
304 trace_name_.c_str(), 289 kTraceCategoriesForAsyncEvents, trace_name_.c_str(),
305 TRACE_ID_DONT_MANGLE(trace_id_), 290 TRACE_ID_DONT_MANGLE(trace_id_), "data", AsTraceableData(),
306 "data", AsTraceableData(), 291 "coordinates", CoordinatesAsTraceableData());
307 "coordinates", CoordinatesAsTraceableData());
308 } 292 }
309 293
310 TRACE_EVENT_WITH_FLOW0("input,benchmark", 294 TRACE_EVENT_WITH_FLOW0("input,benchmark", "LatencyInfo.Flow",
311 "LatencyInfo.Flow",
312 TRACE_ID_DONT_MANGLE(trace_id_), 295 TRACE_ID_DONT_MANGLE(trace_id_),
313 TRACE_EVENT_FLAG_FLOW_IN); 296 TRACE_EVENT_FLAG_FLOW_IN);
314 } 297 }
315 } 298 }
316 299
317 scoped_ptr<base::trace_event::ConvertableToTraceFormat> 300 scoped_ptr<base::trace_event::ConvertableToTraceFormat>
318 LatencyInfo::AsTraceableData() { 301 LatencyInfo::AsTraceableData() {
319 scoped_ptr<base::DictionaryValue> record_data(new base::DictionaryValue()); 302 scoped_ptr<base::DictionaryValue> record_data(new base::DictionaryValue());
320 for (const auto& lc : latency_components_) { 303 for (const auto& lc : latency_components_) {
321 scoped_ptr<base::DictionaryValue> 304 scoped_ptr<base::DictionaryValue> component_info(
322 component_info(new base::DictionaryValue()); 305 new base::DictionaryValue());
323 component_info->SetDouble("comp_id", static_cast<double>(lc.first.second)); 306 component_info->SetDouble("comp_id", static_cast<double>(lc.first.second));
324 component_info->SetDouble( 307 component_info->SetDouble(
325 "time", 308 "time", static_cast<double>(lc.second.event_time.ToInternalValue()));
326 static_cast<double>(lc.second.event_time.ToInternalValue()));
327 component_info->SetDouble("count", lc.second.event_count); 309 component_info->SetDouble("count", lc.second.event_count);
328 component_info->SetDouble("sequence_number", 310 component_info->SetDouble("sequence_number", lc.second.sequence_number);
329 lc.second.sequence_number);
330 record_data->Set(GetComponentName(lc.first.first), 311 record_data->Set(GetComponentName(lc.first.first),
331 std::move(component_info)); 312 std::move(component_info));
332 } 313 }
333 record_data->SetDouble("trace_id", static_cast<double>(trace_id_)); 314 record_data->SetDouble("trace_id", static_cast<double>(trace_id_));
334 return LatencyInfoTracedValue::FromValue(std::move(record_data)); 315 return LatencyInfoTracedValue::FromValue(std::move(record_data));
335 } 316 }
336 317
337 scoped_ptr<base::trace_event::ConvertableToTraceFormat> 318 scoped_ptr<base::trace_event::ConvertableToTraceFormat>
338 LatencyInfo::CoordinatesAsTraceableData() { 319 LatencyInfo::CoordinatesAsTraceableData() {
339 scoped_ptr<base::ListValue> coordinates(new base::ListValue()); 320 scoped_ptr<base::ListValue> coordinates(new base::ListValue());
340 for (size_t i = 0; i < input_coordinates_size_; i++) { 321 for (size_t i = 0; i < input_coordinates_size_; i++) {
341 scoped_ptr<base::DictionaryValue> coordinate_pair( 322 scoped_ptr<base::DictionaryValue> coordinate_pair(
342 new base::DictionaryValue()); 323 new base::DictionaryValue());
343 coordinate_pair->SetDouble("x", input_coordinates_[i].x); 324 coordinate_pair->SetDouble("x", input_coordinates_[i].x);
344 coordinate_pair->SetDouble("y", input_coordinates_[i].y); 325 coordinate_pair->SetDouble("y", input_coordinates_[i].y);
345 coordinates->Append(coordinate_pair.release()); 326 coordinates->Append(coordinate_pair.release());
346 } 327 }
347 return LatencyInfoTracedValue::FromValue(std::move(coordinates)); 328 return LatencyInfoTracedValue::FromValue(std::move(coordinates));
348 } 329 }
349 330
350 bool LatencyInfo::FindLatency(LatencyComponentType type, 331 bool LatencyInfo::FindLatency(LatencyComponentType type,
351 int64_t id, 332 int64_t id,
352 LatencyComponent* output) const { 333 LatencyComponent* output) const {
353 LatencyMap::const_iterator it = latency_components_.find( 334 LatencyMap::const_iterator it =
354 std::make_pair(type, id)); 335 latency_components_.find(std::make_pair(type, id));
355 if (it == latency_components_.end()) 336 if (it == latency_components_.end())
356 return false; 337 return false;
357 if (output) 338 if (output)
358 *output = it->second; 339 *output = it->second;
359 return true; 340 return true;
360 } 341 }
361 342
362 void LatencyInfo::RemoveLatency(LatencyComponentType type) { 343 void LatencyInfo::RemoveLatency(LatencyComponentType type) {
363 LatencyMap::iterator it = latency_components_.begin(); 344 LatencyMap::iterator it = latency_components_.begin();
364 while (it != latency_components_.end()) { 345 while (it != latency_components_.end()) {
365 if (it->first.first == type) { 346 if (it->first.first == type) {
366 LatencyMap::iterator tmp = it; 347 LatencyMap::iterator tmp = it;
367 ++it; 348 ++it;
368 latency_components_.erase(tmp); 349 latency_components_.erase(tmp);
369 } else { 350 } else {
370 it++; 351 it++;
371 } 352 }
372 } 353 }
373 } 354 }
374 355
375 bool LatencyInfo::AddInputCoordinate(const InputCoordinate& input_coordinate) { 356 bool LatencyInfo::AddInputCoordinate(const InputCoordinate& input_coordinate) {
376 if (input_coordinates_size_ >= kMaxInputCoordinates) 357 if (input_coordinates_size_ >= kMaxInputCoordinates)
377 return false; 358 return false;
378 input_coordinates_[input_coordinates_size_++] = input_coordinate; 359 input_coordinates_[input_coordinates_size_++] = input_coordinate;
379 return true; 360 return true;
380 } 361 }
381 362
382 } // namespace ui 363 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698