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

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

Issue 2783973002: Moving LatencyInfo into a separate component. (Closed)
Patch Set: Rebase again Created 3 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
« no previous file with comments | « ui/events/latency_info.h ('k') | ui/events/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
(Empty)
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.
4
5 #include "ui/events/latency_info.h"
6
7 #include <stddef.h>
8
9 #include <algorithm>
10 #include <string>
11 #include <utility>
12
13 #include "base/json/json_writer.h"
14 #include "base/lazy_instance.h"
15 #include "base/macros.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/trace_event/trace_event.h"
18
19 namespace {
20
21 const size_t kMaxLatencyInfoNumber = 100;
22
23 const char* GetComponentName(ui::LatencyComponentType type) {
24 #define CASE_TYPE(t) case ui::t: return #t
25 switch (type) {
26 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT);
27 CASE_TYPE(LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT);
28 CASE_TYPE(INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT);
29 CASE_TYPE(INPUT_EVENT_LATENCY_FIRST_SCROLL_UPDATE_ORIGINAL_COMPONENT);
30 CASE_TYPE(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT);
31 CASE_TYPE(INPUT_EVENT_LATENCY_UI_COMPONENT);
32 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_MAIN_COMPONENT);
33 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_IMPL_COMPONENT);
34 CASE_TYPE(INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT);
35 CASE_TYPE(INPUT_EVENT_LATENCY_ACK_RWH_COMPONENT);
36 CASE_TYPE(WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT);
37 CASE_TYPE(TAB_SHOW_COMPONENT);
38 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERER_MAIN_COMPONENT);
39 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERER_SWAP_COMPONENT);
40 CASE_TYPE(INPUT_EVENT_BROWSER_RECEIVED_RENDERER_SWAP_COMPONENT);
41 CASE_TYPE(INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT);
42 CASE_TYPE(INPUT_EVENT_LATENCY_GENERATE_SCROLL_UPDATE_FROM_MOUSE_WHEEL);
43 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_NO_SWAP_COMPONENT);
44 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT);
45 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT);
46 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT);
47 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT);
48 default:
49 DLOG(WARNING) << "Unhandled LatencyComponentType.\n";
50 break;
51 }
52 #undef CASE_TYPE
53 return "unknown";
54 }
55
56 bool IsTerminalComponent(ui::LatencyComponentType type) {
57 switch (type) {
58 case ui::INPUT_EVENT_LATENCY_TERMINATED_NO_SWAP_COMPONENT:
59 case ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT:
60 case ui::INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT:
61 case ui::INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT:
62 case ui::INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT:
63 return true;
64 default:
65 return false;
66 }
67 }
68
69 bool IsBeginComponent(ui::LatencyComponentType type) {
70 return (type == ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT ||
71 type == ui::LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT);
72 }
73
74 bool IsInputLatencyBeginComponent(ui::LatencyComponentType type) {
75 return type == ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT;
76 }
77
78 // This class is for converting latency info to trace buffer friendly format.
79 class LatencyInfoTracedValue
80 : public base::trace_event::ConvertableToTraceFormat {
81 public:
82 static std::unique_ptr<ConvertableToTraceFormat> FromValue(
83 std::unique_ptr<base::Value> value);
84
85 void AppendAsTraceFormat(std::string* out) const override;
86
87 private:
88 explicit LatencyInfoTracedValue(base::Value* value);
89 ~LatencyInfoTracedValue() override;
90
91 std::unique_ptr<base::Value> value_;
92
93 DISALLOW_COPY_AND_ASSIGN(LatencyInfoTracedValue);
94 };
95
96 std::unique_ptr<base::trace_event::ConvertableToTraceFormat>
97 LatencyInfoTracedValue::FromValue(std::unique_ptr<base::Value> value) {
98 return std::unique_ptr<base::trace_event::ConvertableToTraceFormat>(
99 new LatencyInfoTracedValue(value.release()));
100 }
101
102 LatencyInfoTracedValue::~LatencyInfoTracedValue() {
103 }
104
105 void LatencyInfoTracedValue::AppendAsTraceFormat(std::string* out) const {
106 std::string tmp;
107 base::JSONWriter::Write(*value_, &tmp);
108 *out += tmp;
109 }
110
111 LatencyInfoTracedValue::LatencyInfoTracedValue(base::Value* value)
112 : value_(value) {
113 }
114
115 const char kTraceCategoriesForAsyncEvents[] = "benchmark,latencyInfo,rail";
116
117 struct LatencyInfoEnabledInitializer {
118 LatencyInfoEnabledInitializer() :
119 latency_info_enabled(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
120 kTraceCategoriesForAsyncEvents)) {
121 }
122
123 const unsigned char* latency_info_enabled;
124 };
125
126 static base::LazyInstance<LatencyInfoEnabledInitializer>::Leaky
127 g_latency_info_enabled = LAZY_INSTANCE_INITIALIZER;
128
129 } // namespace
130
131 namespace ui {
132
133 LatencyInfo::LatencyInfo() : LatencyInfo(SourceEventType::UNKNOWN) {}
134
135 LatencyInfo::LatencyInfo(SourceEventType type)
136 : input_coordinates_size_(0),
137 trace_id_(-1),
138 coalesced_(false),
139 terminated_(false),
140 source_event_type_(type) {}
141
142 LatencyInfo::LatencyInfo(const LatencyInfo& other) = default;
143
144 LatencyInfo::~LatencyInfo() {}
145
146 LatencyInfo::LatencyInfo(int64_t trace_id, bool terminated)
147 : input_coordinates_size_(0),
148 trace_id_(trace_id),
149 terminated_(terminated),
150 source_event_type_(SourceEventType::UNKNOWN) {}
151
152 bool LatencyInfo::Verify(const std::vector<LatencyInfo>& latency_info,
153 const char* referring_msg) {
154 if (latency_info.size() > kMaxLatencyInfoNumber) {
155 LOG(ERROR) << referring_msg << ", LatencyInfo vector size "
156 << latency_info.size() << " is too big.";
157 TRACE_EVENT_INSTANT1("input,benchmark", "LatencyInfo::Verify Fails",
158 TRACE_EVENT_SCOPE_GLOBAL,
159 "size", latency_info.size());
160 return false;
161 }
162 return true;
163 }
164
165 void LatencyInfo::CopyLatencyFrom(const LatencyInfo& other,
166 LatencyComponentType type) {
167 for (const auto& lc : other.latency_components()) {
168 if (lc.first.first == type) {
169 AddLatencyNumberWithTimestamp(lc.first.first,
170 lc.first.second,
171 lc.second.sequence_number,
172 lc.second.event_time,
173 lc.second.event_count);
174 }
175 }
176 }
177
178 void LatencyInfo::AddNewLatencyFrom(const LatencyInfo& other) {
179 for (const auto& lc : other.latency_components()) {
180 if (!FindLatency(lc.first.first, lc.first.second, NULL)) {
181 AddLatencyNumberWithTimestamp(lc.first.first,
182 lc.first.second,
183 lc.second.sequence_number,
184 lc.second.event_time,
185 lc.second.event_count);
186 }
187 }
188 }
189
190 void LatencyInfo::AddLatencyNumber(LatencyComponentType component,
191 int64_t id,
192 int64_t component_sequence_number) {
193 AddLatencyNumberWithTimestampImpl(component, id, component_sequence_number,
194 base::TimeTicks::Now(), 1, nullptr);
195 }
196
197 void LatencyInfo::AddLatencyNumberWithTraceName(
198 LatencyComponentType component,
199 int64_t id,
200 int64_t component_sequence_number,
201 const char* trace_name_str) {
202 AddLatencyNumberWithTimestampImpl(component, id, component_sequence_number,
203 base::TimeTicks::Now(), 1, trace_name_str);
204 }
205
206 void LatencyInfo::AddLatencyNumberWithTimestamp(
207 LatencyComponentType component,
208 int64_t id,
209 int64_t component_sequence_number,
210 base::TimeTicks time,
211 uint32_t event_count) {
212 AddLatencyNumberWithTimestampImpl(component, id, component_sequence_number,
213 time, event_count, nullptr);
214 }
215
216 void LatencyInfo::AddLatencyNumberWithTimestampImpl(
217 LatencyComponentType component,
218 int64_t id,
219 int64_t component_sequence_number,
220 base::TimeTicks time,
221 uint32_t event_count,
222 const char* trace_name_str) {
223 const unsigned char* latency_info_enabled =
224 g_latency_info_enabled.Get().latency_info_enabled;
225
226 if (IsBeginComponent(component)) {
227 // Should only ever add begin component once.
228 CHECK_EQ(-1, trace_id_);
229 trace_id_ = component_sequence_number;
230
231 if (*latency_info_enabled) {
232 // The timestamp for ASYNC_BEGIN trace event is used for drawing the
233 // beginning of the trace event in trace viewer. For better visualization,
234 // for an input event, we want to draw the beginning as when the event is
235 // originally created, e.g. the timestamp of its ORIGINAL/UI_COMPONENT,
236 // not when we actually issue the ASYNC_BEGIN trace event.
237 LatencyComponent begin_component;
238 base::TimeTicks ts;
239 if (FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
240 0,
241 &begin_component) ||
242 FindLatency(INPUT_EVENT_LATENCY_UI_COMPONENT,
243 0,
244 &begin_component)) {
245 ts = begin_component.event_time;
246 } else {
247 ts = base::TimeTicks::Now();
248 }
249
250 if (trace_name_str) {
251 if (IsInputLatencyBeginComponent(component))
252 trace_name_ = std::string("InputLatency::") + trace_name_str;
253 else
254 trace_name_ = std::string("Latency::") + trace_name_str;
255 }
256
257 TRACE_EVENT_COPY_ASYNC_BEGIN_WITH_TIMESTAMP0(
258 kTraceCategoriesForAsyncEvents,
259 trace_name_.c_str(),
260 TRACE_ID_DONT_MANGLE(trace_id_),
261 ts);
262 }
263
264 TRACE_EVENT_WITH_FLOW1("input,benchmark",
265 "LatencyInfo.Flow",
266 TRACE_ID_DONT_MANGLE(trace_id_),
267 TRACE_EVENT_FLAG_FLOW_OUT,
268 "trace_id", trace_id_);
269 }
270
271 LatencyMap::key_type key = std::make_pair(component, id);
272 LatencyMap::iterator it = latency_components_.find(key);
273 if (it == latency_components_.end()) {
274 LatencyComponent info = {component_sequence_number, time, event_count, time,
275 time};
276 latency_components_[key] = info;
277 } else {
278 it->second.sequence_number = std::max(component_sequence_number,
279 it->second.sequence_number);
280 uint32_t new_count = event_count + it->second.event_count;
281 if (event_count > 0 && new_count != 0) {
282 // Do a weighted average, so that the new event_time is the average of
283 // the times of events currently in this structure with the time passed
284 // into this method.
285 it->second.event_time += (time - it->second.event_time) * event_count /
286 new_count;
287 it->second.event_count = new_count;
288 it->second.last_event_time = std::max(it->second.last_event_time, time);
289 }
290 }
291
292 if (IsTerminalComponent(component) && trace_id_ != -1) {
293 // Should only ever add terminal component once.
294 CHECK(!terminated_);
295 terminated_ = true;
296
297 if (*latency_info_enabled) {
298 TRACE_EVENT_COPY_ASYNC_END2(kTraceCategoriesForAsyncEvents,
299 trace_name_.c_str(),
300 TRACE_ID_DONT_MANGLE(trace_id_),
301 "data", AsTraceableData(),
302 "coordinates", CoordinatesAsTraceableData());
303 }
304
305 TRACE_EVENT_WITH_FLOW0("input,benchmark",
306 "LatencyInfo.Flow",
307 TRACE_ID_DONT_MANGLE(trace_id_),
308 TRACE_EVENT_FLAG_FLOW_IN);
309 }
310 }
311
312 std::unique_ptr<base::trace_event::ConvertableToTraceFormat>
313 LatencyInfo::AsTraceableData() {
314 std::unique_ptr<base::DictionaryValue> record_data(
315 new base::DictionaryValue());
316 for (const auto& lc : latency_components_) {
317 std::unique_ptr<base::DictionaryValue> component_info(
318 new base::DictionaryValue());
319 component_info->SetDouble("comp_id", static_cast<double>(lc.first.second));
320 component_info->SetDouble(
321 "time",
322 static_cast<double>(lc.second.event_time.ToInternalValue()));
323 component_info->SetDouble("count", lc.second.event_count);
324 component_info->SetDouble("sequence_number",
325 lc.second.sequence_number);
326 record_data->Set(GetComponentName(lc.first.first),
327 std::move(component_info));
328 }
329 record_data->SetDouble("trace_id", static_cast<double>(trace_id_));
330 return LatencyInfoTracedValue::FromValue(std::move(record_data));
331 }
332
333 std::unique_ptr<base::trace_event::ConvertableToTraceFormat>
334 LatencyInfo::CoordinatesAsTraceableData() {
335 std::unique_ptr<base::ListValue> coordinates(new base::ListValue());
336 for (size_t i = 0; i < input_coordinates_size_; i++) {
337 std::unique_ptr<base::DictionaryValue> coordinate_pair(
338 new base::DictionaryValue());
339 coordinate_pair->SetDouble("x", input_coordinates_[i].x());
340 coordinate_pair->SetDouble("y", input_coordinates_[i].y());
341 coordinates->Append(std::move(coordinate_pair));
342 }
343 return LatencyInfoTracedValue::FromValue(std::move(coordinates));
344 }
345
346 bool LatencyInfo::FindLatency(LatencyComponentType type,
347 int64_t id,
348 LatencyComponent* output) const {
349 LatencyMap::const_iterator it = latency_components_.find(
350 std::make_pair(type, id));
351 if (it == latency_components_.end())
352 return false;
353 if (output)
354 *output = it->second;
355 return true;
356 }
357
358 void LatencyInfo::RemoveLatency(LatencyComponentType type) {
359 LatencyMap::iterator it = latency_components_.begin();
360 while (it != latency_components_.end()) {
361 if (it->first.first == type)
362 it = latency_components_.erase(it);
363 else
364 it++;
365 }
366 }
367
368 bool LatencyInfo::AddInputCoordinate(const gfx::PointF& input_coordinate) {
369 if (input_coordinates_size_ >= kMaxInputCoordinates)
370 return false;
371 input_coordinates_[input_coordinates_size_++] = input_coordinate;
372 return true;
373 }
374
375 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/latency_info.h ('k') | ui/events/latency_info.dot » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698