Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / events / latency_info.cc
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 "base/debug/trace_event.h"
6 #include "base/json/json_writer.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/stringprintf.h"
9 #include "ui/events/latency_info.h"
10
11 #include <algorithm>
12
13 namespace {
14
15 const unsigned int kMaxLatencyInfoNumber = 100;
16
17 const char* GetComponentName(ui::LatencyComponentType type) {
18 #define CASE_TYPE(t) case ui::t:  return #t
19   switch (type) {
20     CASE_TYPE(INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT);
21     CASE_TYPE(INPUT_EVENT_LATENCY_SCROLL_UPDATE_RWH_COMPONENT);
22     CASE_TYPE(INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT);
23     CASE_TYPE(INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT);
24     CASE_TYPE(INPUT_EVENT_LATENCY_UI_COMPONENT);
25     CASE_TYPE(INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_COMPONENT);
26     CASE_TYPE(INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT);
27     CASE_TYPE(WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT);
28     CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT);
29     CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT);
30     CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT);
31     CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT);
32     CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT);
33     CASE_TYPE(INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT);
34     CASE_TYPE(LATENCY_INFO_LIST_TERMINATED_OVERFLOW_COMPONENT);
35     default:
36       DLOG(WARNING) << "Unhandled LatencyComponentType.\n";
37       break;
38   }
39 #undef CASE_TYPE
40   return "unknown";
41 }
42
43 bool IsTerminalComponent(ui::LatencyComponentType type) {
44   switch (type) {
45     case ui::INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT:
46     case ui::INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT:
47     case ui::INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT:
48     case ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT:
49     case ui::INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT:
50     case ui::INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT:
51     case ui::LATENCY_INFO_LIST_TERMINATED_OVERFLOW_COMPONENT:
52       return true;
53     default:
54       return false;
55   }
56 }
57
58 bool IsBeginComponent(ui::LatencyComponentType type) {
59   return (type == ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT);
60 }
61
62 // This class is for converting latency info to trace buffer friendly format.
63 class LatencyInfoTracedValue : public base::debug::ConvertableToTraceFormat {
64  public:
65   static scoped_refptr<ConvertableToTraceFormat> FromValue(
66       scoped_ptr<base::Value> value);
67
68   virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE;
69
70  private:
71   explicit LatencyInfoTracedValue(base::Value* value);
72   virtual ~LatencyInfoTracedValue();
73
74   scoped_ptr<base::Value> value_;
75
76   DISALLOW_COPY_AND_ASSIGN(LatencyInfoTracedValue);
77 };
78
79 scoped_refptr<base::debug::ConvertableToTraceFormat>
80 LatencyInfoTracedValue::FromValue(scoped_ptr<base::Value> value) {
81   return scoped_refptr<base::debug::ConvertableToTraceFormat>(
82       new LatencyInfoTracedValue(value.release()));
83 }
84
85 LatencyInfoTracedValue::~LatencyInfoTracedValue() {
86 }
87
88 void LatencyInfoTracedValue::AppendAsTraceFormat(std::string* out) const {
89   std::string tmp;
90   base::JSONWriter::Write(value_.get(), &tmp);
91   *out += tmp;
92 }
93
94 LatencyInfoTracedValue::LatencyInfoTracedValue(base::Value* value)
95     : value_(value) {
96 }
97
98 // Converts latencyinfo into format that can be dumped into trace buffer.
99 scoped_refptr<base::debug::ConvertableToTraceFormat> AsTraceableData(
100     const ui::LatencyInfo& latency) {
101   scoped_ptr<base::DictionaryValue> record_data(new base::DictionaryValue());
102   for (ui::LatencyInfo::LatencyMap::const_iterator it =
103            latency.latency_components.begin();
104        it != latency.latency_components.end(); ++it) {
105     base::DictionaryValue* component_info = new base::DictionaryValue();
106     component_info->SetDouble("comp_id", it->first.second);
107     component_info->SetDouble("time", it->second.event_time.ToInternalValue());
108     component_info->SetDouble("count", it->second.event_count);
109     record_data->Set(GetComponentName(it->first.first), component_info);
110   }
111   record_data->SetDouble("trace_id", latency.trace_id);
112   return LatencyInfoTracedValue::FromValue(record_data.PassAs<base::Value>());
113 }
114
115 }  // namespace
116
117 namespace ui {
118
119 LatencyInfo::LatencyInfo() : trace_id(-1), terminated(false) {
120 }
121
122 LatencyInfo::~LatencyInfo() {
123 }
124
125 bool LatencyInfo::Verify(const std::vector<LatencyInfo>& latency_info,
126                          const char* referring_msg) {
127   if (latency_info.size() > kMaxLatencyInfoNumber) {
128     LOG(ERROR) << referring_msg << ", LatencyInfo vector size "
129                << latency_info.size() << " is too big.";
130     return false;
131   }
132   return true;
133 }
134
135 void LatencyInfo::CopyLatencyFrom(const LatencyInfo& other,
136                                   LatencyComponentType type) {
137   for (LatencyMap::const_iterator it = other.latency_components.begin();
138        it != other.latency_components.end();
139        ++it) {
140     if (it->first.first == type) {
141       AddLatencyNumberWithTimestamp(it->first.first,
142                                     it->first.second,
143                                     it->second.sequence_number,
144                                     it->second.event_time,
145                                     it->second.event_count);
146     }
147   }
148 }
149
150 void LatencyInfo::AddNewLatencyFrom(const LatencyInfo& other) {
151     for (LatencyMap::const_iterator it = other.latency_components.begin();
152          it != other.latency_components.end();
153          ++it) {
154       if (!FindLatency(it->first.first, it->first.second, NULL)) {
155         AddLatencyNumberWithTimestamp(it->first.first,
156                                       it->first.second,
157                                       it->second.sequence_number,
158                                       it->second.event_time,
159                                       it->second.event_count);
160       }
161     }
162 }
163
164 void LatencyInfo::AddLatencyNumber(LatencyComponentType component,
165                                    int64 id,
166                                    int64 component_sequence_number) {
167   AddLatencyNumberWithTimestamp(component, id, component_sequence_number,
168                                 base::TimeTicks::HighResNow(), 1);
169 }
170
171 void LatencyInfo::AddLatencyNumberWithTimestamp(LatencyComponentType component,
172                                                 int64 id,
173                                                 int64 component_sequence_number,
174                                                 base::TimeTicks time,
175                                                 uint32 event_count) {
176   if (IsBeginComponent(component)) {
177     // Should only ever add begin component once.
178     CHECK_EQ(-1, trace_id);
179     trace_id = component_sequence_number;
180     TRACE_EVENT_ASYNC_BEGIN0("benchmark",
181                              "InputLatency",
182                              TRACE_ID_DONT_MANGLE(trace_id));
183   }
184
185   LatencyMap::key_type key = std::make_pair(component, id);
186   LatencyMap::iterator it = latency_components.find(key);
187   if (it == latency_components.end()) {
188     LatencyComponent info = {component_sequence_number, time, event_count};
189     latency_components[key] = info;
190   } else {
191     it->second.sequence_number = std::max(component_sequence_number,
192                                           it->second.sequence_number);
193     uint32 new_count = event_count + it->second.event_count;
194     if (event_count > 0 && new_count != 0) {
195       // Do a weighted average, so that the new event_time is the average of
196       // the times of events currently in this structure with the time passed
197       // into this method.
198       it->second.event_time += (time - it->second.event_time) * event_count /
199           new_count;
200       it->second.event_count = new_count;
201     }
202   }
203
204   if (IsTerminalComponent(component) && trace_id != -1) {
205     // Should only ever add terminal component once.
206     CHECK(!terminated);
207     terminated = true;
208     TRACE_EVENT_ASYNC_END1("benchmark",
209                            "InputLatency",
210                            TRACE_ID_DONT_MANGLE(trace_id),
211                            "data", AsTraceableData(*this));
212   }
213 }
214
215 bool LatencyInfo::FindLatency(LatencyComponentType type,
216                               int64 id,
217                               LatencyComponent* output) const {
218   LatencyMap::const_iterator it = latency_components.find(
219       std::make_pair(type, id));
220   if (it == latency_components.end())
221     return false;
222   if (output)
223     *output = it->second;
224   return true;
225 }
226
227 void LatencyInfo::RemoveLatency(LatencyComponentType type) {
228   LatencyMap::iterator it = latency_components.begin();
229   while (it != latency_components.end()) {
230     if (it->first.first == type) {
231       LatencyMap::iterator tmp = it;
232       ++it;
233       latency_components.erase(tmp);
234     } else {
235       it++;
236     }
237   }
238 }
239
240 void LatencyInfo::Clear() {
241   latency_components.clear();
242 }
243
244 void LatencyInfo::TraceEventType(const char* event_type) {
245   TRACE_EVENT_ASYNC_STEP_INTO0("benchmark",
246                                "InputLatency",
247                                TRACE_ID_DONT_MANGLE(trace_id),
248                                event_type);
249 }
250
251 }  // namespace ui