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

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

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