base/threading: remove ScopedTracker placed for experiments
[chromium-blink-merge.git] / ui / events / latency_info.cc
blobfb7660a4eecf118547a9e6e2632e42a9b34c2c23
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.
5 #include "ui/events/latency_info.h"
7 #include <algorithm>
8 #include <string>
10 #include "base/json/json_writer.h"
11 #include "base/lazy_instance.h"
12 #include "base/strings/stringprintf.h"
14 namespace {
16 const size_t kMaxLatencyInfoNumber = 100;
18 const char* GetComponentName(ui::LatencyComponentType type) {
19 #define CASE_TYPE(t) case ui::t: return #t
20 switch (type) {
21 CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT);
22 CASE_TYPE(LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT);
23 CASE_TYPE(INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT);
24 CASE_TYPE(INPUT_EVENT_LATENCY_FIRST_SCROLL_UPDATE_ORIGINAL_COMPONENT);
25 CASE_TYPE(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT);
26 CASE_TYPE(INPUT_EVENT_LATENCY_UI_COMPONENT);
27 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_MAIN_COMPONENT);
28 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_IMPL_COMPONENT);
29 CASE_TYPE(INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT);
30 CASE_TYPE(INPUT_EVENT_LATENCY_ACK_RWH_COMPONENT);
31 CASE_TYPE(WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT);
32 CASE_TYPE(TAB_SHOW_COMPONENT);
33 CASE_TYPE(INPUT_EVENT_LATENCY_RENDERER_SWAP_COMPONENT);
34 CASE_TYPE(INPUT_EVENT_BROWSER_RECEIVED_RENDERER_SWAP_COMPONENT);
35 CASE_TYPE(INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT);
36 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT);
37 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT);
38 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT);
39 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT);
40 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT);
41 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT);
42 CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT);
43 default:
44 DLOG(WARNING) << "Unhandled LatencyComponentType.\n";
45 break;
47 #undef CASE_TYPE
48 return "unknown";
51 bool IsTerminalComponent(ui::LatencyComponentType type) {
52 switch (type) {
53 case ui::INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT:
54 case ui::INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT:
55 case ui::INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT:
56 case ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT:
57 case ui::INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT:
58 case ui::INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT:
59 case ui::INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT:
60 return true;
61 default:
62 return false;
66 bool IsBeginComponent(ui::LatencyComponentType type) {
67 return (type == ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT ||
68 type == ui::LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT);
71 bool IsInputLatencyBeginComponent(ui::LatencyComponentType type) {
72 return type == ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT;
75 // This class is for converting latency info to trace buffer friendly format.
76 class LatencyInfoTracedValue
77 : public base::trace_event::ConvertableToTraceFormat {
78 public:
79 static scoped_refptr<ConvertableToTraceFormat> FromValue(
80 scoped_ptr<base::Value> value);
82 void AppendAsTraceFormat(std::string* out) const override;
84 private:
85 explicit LatencyInfoTracedValue(base::Value* value);
86 ~LatencyInfoTracedValue() override;
88 scoped_ptr<base::Value> value_;
90 DISALLOW_COPY_AND_ASSIGN(LatencyInfoTracedValue);
93 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
94 LatencyInfoTracedValue::FromValue(scoped_ptr<base::Value> value) {
95 return scoped_refptr<base::trace_event::ConvertableToTraceFormat>(
96 new LatencyInfoTracedValue(value.release()));
99 LatencyInfoTracedValue::~LatencyInfoTracedValue() {
102 void LatencyInfoTracedValue::AppendAsTraceFormat(std::string* out) const {
103 std::string tmp;
104 base::JSONWriter::Write(*value_, &tmp);
105 *out += tmp;
108 LatencyInfoTracedValue::LatencyInfoTracedValue(base::Value* value)
109 : value_(value) {
112 struct BenchmarkEnabledInitializer {
113 BenchmarkEnabledInitializer() :
114 benchmark_enabled(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
115 "benchmark")) {
118 const unsigned char* benchmark_enabled;
121 static base::LazyInstance<BenchmarkEnabledInitializer>::Leaky
122 g_benchmark_enabled = LAZY_INSTANCE_INITIALIZER;
124 } // namespace
126 namespace ui {
128 LatencyInfo::InputCoordinate::InputCoordinate() : x(0), y(0) {
131 LatencyInfo::InputCoordinate::InputCoordinate(float x, float y) : x(x), y(y) {
134 LatencyInfo::LatencyInfo()
135 : input_coordinates_size_(0), trace_id_(-1), terminated_(false) {
138 LatencyInfo::~LatencyInfo() {
141 LatencyInfo::LatencyInfo(int64 trace_id, bool terminated)
142 : input_coordinates_size_(0), trace_id_(trace_id), terminated_(terminated) {
145 bool LatencyInfo::Verify(const std::vector<LatencyInfo>& latency_info,
146 const char* referring_msg) {
147 if (latency_info.size() > kMaxLatencyInfoNumber) {
148 LOG(ERROR) << referring_msg << ", LatencyInfo vector size "
149 << latency_info.size() << " is too big.";
150 return false;
152 return true;
155 void LatencyInfo::CopyLatencyFrom(const LatencyInfo& other,
156 LatencyComponentType type) {
157 for (const auto& lc : other.latency_components()) {
158 if (lc.first.first == type) {
159 AddLatencyNumberWithTimestamp(lc.first.first,
160 lc.first.second,
161 lc.second.sequence_number,
162 lc.second.event_time,
163 lc.second.event_count);
168 void LatencyInfo::AddNewLatencyFrom(const LatencyInfo& other) {
169 for (const auto& lc : other.latency_components()) {
170 if (!FindLatency(lc.first.first, lc.first.second, NULL)) {
171 AddLatencyNumberWithTimestamp(lc.first.first,
172 lc.first.second,
173 lc.second.sequence_number,
174 lc.second.event_time,
175 lc.second.event_count);
180 void LatencyInfo::AddLatencyNumber(LatencyComponentType component,
181 int64 id,
182 int64 component_sequence_number) {
183 AddLatencyNumberWithTimestampImpl(component, id, component_sequence_number,
184 base::TimeTicks::Now(), 1, nullptr);
187 void LatencyInfo::AddLatencyNumberWithTraceName(
188 LatencyComponentType component,
189 int64 id,
190 int64 component_sequence_number,
191 const char* trace_name_str) {
192 AddLatencyNumberWithTimestampImpl(component, id, component_sequence_number,
193 base::TimeTicks::Now(), 1, trace_name_str);
196 void LatencyInfo::AddLatencyNumberWithTimestamp(LatencyComponentType component,
197 int64 id,
198 int64 component_sequence_number,
199 base::TimeTicks time,
200 uint32 event_count) {
201 AddLatencyNumberWithTimestampImpl(component, id, component_sequence_number,
202 time, event_count, nullptr);
205 void LatencyInfo::AddLatencyNumberWithTimestampImpl(
206 LatencyComponentType component,
207 int64 id,
208 int64 component_sequence_number,
209 base::TimeTicks time,
210 uint32 event_count,
211 const char* trace_name_str) {
213 const unsigned char* benchmark_enabled =
214 g_benchmark_enabled.Get().benchmark_enabled;
216 if (IsBeginComponent(component)) {
217 // Should only ever add begin component once.
218 CHECK_EQ(-1, trace_id_);
219 trace_id_ = component_sequence_number;
221 if (*benchmark_enabled) {
222 // The timestamp for ASYNC_BEGIN trace event is used for drawing the
223 // beginning of the trace event in trace viewer. For better visualization,
224 // for an input event, we want to draw the beginning as when the event is
225 // originally created, e.g. the timestamp of its ORIGINAL/UI_COMPONENT,
226 // not when we actually issue the ASYNC_BEGIN trace event.
227 LatencyComponent begin_component;
228 int64 ts = 0;
229 if (FindLatency(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
231 &begin_component) ||
232 FindLatency(INPUT_EVENT_LATENCY_UI_COMPONENT,
234 &begin_component)) {
235 // The timestamp stored in ORIGINAL/UI_COMPONENT is using clock
236 // CLOCK_MONOTONIC while TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0
237 // expects timestamp using CLOCK_MONOTONIC or CLOCK_SYSTEM_TRACE (on
238 // CrOS). So we need to adjust the diff between in CLOCK_MONOTONIC and
239 // CLOCK_SYSTEM_TRACE. Note that the diff is drifting overtime so we
240 // can't use a static value.
241 base::TimeDelta diff = (base::TimeTicks::Now() - base::TimeTicks()) -
242 (base::TraceTicks::Now() - base::TraceTicks());
243 ts = (begin_component.event_time - diff).ToInternalValue();
244 } else {
245 ts = base::TraceTicks::Now().ToInternalValue();
248 if (trace_name_str) {
249 if (IsInputLatencyBeginComponent(component))
250 trace_name_ = std::string("InputLatency::") + trace_name_str;
251 else
252 trace_name_ = std::string("Latency::") + trace_name_str;
255 TRACE_EVENT_COPY_ASYNC_BEGIN_WITH_TIMESTAMP0(
256 "benchmark,latencyInfo",
257 trace_name_.c_str(),
258 TRACE_ID_DONT_MANGLE(trace_id_),
259 ts);
262 TRACE_EVENT_FLOW_BEGIN1(
263 "input,benchmark", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id_),
264 "trace_id", trace_id_);
267 LatencyMap::key_type key = std::make_pair(component, id);
268 LatencyMap::iterator it = latency_components_.find(key);
269 if (it == latency_components_.end()) {
270 LatencyComponent info = {component_sequence_number, time, event_count};
271 latency_components_[key] = info;
272 } else {
273 it->second.sequence_number = std::max(component_sequence_number,
274 it->second.sequence_number);
275 uint32 new_count = event_count + it->second.event_count;
276 if (event_count > 0 && new_count != 0) {
277 // Do a weighted average, so that the new event_time is the average of
278 // the times of events currently in this structure with the time passed
279 // into this method.
280 it->second.event_time += (time - it->second.event_time) * event_count /
281 new_count;
282 it->second.event_count = new_count;
286 if (IsTerminalComponent(component) && trace_id_ != -1) {
287 // Should only ever add terminal component once.
288 CHECK(!terminated_);
289 terminated_ = true;
291 if (*benchmark_enabled) {
292 TRACE_EVENT_COPY_ASYNC_END1("benchmark,latencyInfo",
293 trace_name_.c_str(),
294 TRACE_ID_DONT_MANGLE(trace_id_),
295 "data", AsTraceableData());
298 TRACE_EVENT_FLOW_END_BIND_TO_ENCLOSING0(
299 "input,benchmark", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id_));
303 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
304 LatencyInfo::AsTraceableData() {
305 scoped_ptr<base::DictionaryValue> record_data(new base::DictionaryValue());
306 for (const auto& lc : latency_components_) {
307 scoped_ptr<base::DictionaryValue>
308 component_info(new base::DictionaryValue());
309 component_info->SetDouble("comp_id", static_cast<double>(lc.first.second));
310 component_info->SetDouble(
311 "time",
312 static_cast<double>(lc.second.event_time.ToInternalValue()));
313 component_info->SetDouble("count", lc.second.event_count);
314 component_info->SetDouble("sequence_number",
315 lc.second.sequence_number);
316 record_data->Set(GetComponentName(lc.first.first), component_info.Pass());
318 record_data->SetDouble("trace_id", static_cast<double>(trace_id_));
320 scoped_ptr<base::ListValue> coordinates(new base::ListValue());
321 for (size_t i = 0; i < input_coordinates_size_; i++) {
322 scoped_ptr<base::DictionaryValue> coordinate_pair(
323 new base::DictionaryValue());
324 coordinate_pair->SetDouble("x", input_coordinates_[i].x);
325 coordinate_pair->SetDouble("y", input_coordinates_[i].y);
326 coordinates->Append(coordinate_pair.release());
328 record_data->Set("coordinates", coordinates.release());
329 return LatencyInfoTracedValue::FromValue(record_data.Pass());
332 bool LatencyInfo::FindLatency(LatencyComponentType type,
333 int64 id,
334 LatencyComponent* output) const {
335 LatencyMap::const_iterator it = latency_components_.find(
336 std::make_pair(type, id));
337 if (it == latency_components_.end())
338 return false;
339 if (output)
340 *output = it->second;
341 return true;
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++;
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;
364 } // namespace ui