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

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

Powered by Google App Engine
This is Rietveld 408576698