Moves the notification icon out of the status area overflow.
[chromium-blink-merge.git] / ui / events / latency_info.cc
blob2fbe2ec1fe768a9af1c681309990327d37e6a62e
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 "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"
11 #include <algorithm>
13 namespace {
15 const size_t kMaxLatencyInfoNumber = 100;
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;
39 #undef CASE_TYPE
40 return "unknown";
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;
58 bool IsBeginComponent(ui::LatencyComponentType type) {
59 return (type == ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT);
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);
68 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE;
70 private:
71 explicit LatencyInfoTracedValue(base::Value* value);
72 virtual ~LatencyInfoTracedValue();
74 scoped_ptr<base::Value> value_;
76 DISALLOW_COPY_AND_ASSIGN(LatencyInfoTracedValue);
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()));
85 LatencyInfoTracedValue::~LatencyInfoTracedValue() {
88 void LatencyInfoTracedValue::AppendAsTraceFormat(std::string* out) const {
89 std::string tmp;
90 base::JSONWriter::Write(value_.get(), &tmp);
91 *out += tmp;
94 LatencyInfoTracedValue::LatencyInfoTracedValue(base::Value* value)
95 : value_(value) {
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);
111 record_data->SetDouble("trace_id", latency.trace_id);
112 return LatencyInfoTracedValue::FromValue(record_data.PassAs<base::Value>());
115 } // namespace
117 namespace ui {
119 LatencyInfo::LatencyInfo() : trace_id(-1), terminated(false) {
122 LatencyInfo::~LatencyInfo() {
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;
132 return true;
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);
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);
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);
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 TRACE_EVENT_FLOW_BEGIN0(
184 "input", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id));
187 LatencyMap::key_type key = std::make_pair(component, id);
188 LatencyMap::iterator it = latency_components.find(key);
189 if (it == latency_components.end()) {
190 LatencyComponent info = {component_sequence_number, time, event_count};
191 latency_components[key] = info;
192 } else {
193 it->second.sequence_number = std::max(component_sequence_number,
194 it->second.sequence_number);
195 uint32 new_count = event_count + it->second.event_count;
196 if (event_count > 0 && new_count != 0) {
197 // Do a weighted average, so that the new event_time is the average of
198 // the times of events currently in this structure with the time passed
199 // into this method.
200 it->second.event_time += (time - it->second.event_time) * event_count /
201 new_count;
202 it->second.event_count = new_count;
206 if (IsTerminalComponent(component) && trace_id != -1) {
207 // Should only ever add terminal component once.
208 CHECK(!terminated);
209 terminated = true;
210 TRACE_EVENT_ASYNC_END1("benchmark",
211 "InputLatency",
212 TRACE_ID_DONT_MANGLE(trace_id),
213 "data", AsTraceableData(*this));
214 TRACE_EVENT_FLOW_END0(
215 "input", "LatencyInfo.Flow", TRACE_ID_DONT_MANGLE(trace_id));
219 bool LatencyInfo::FindLatency(LatencyComponentType type,
220 int64 id,
221 LatencyComponent* output) const {
222 LatencyMap::const_iterator it = latency_components.find(
223 std::make_pair(type, id));
224 if (it == latency_components.end())
225 return false;
226 if (output)
227 *output = it->second;
228 return true;
231 void LatencyInfo::RemoveLatency(LatencyComponentType type) {
232 LatencyMap::iterator it = latency_components.begin();
233 while (it != latency_components.end()) {
234 if (it->first.first == type) {
235 LatencyMap::iterator tmp = it;
236 ++it;
237 latency_components.erase(tmp);
238 } else {
239 it++;
244 void LatencyInfo::Clear() {
245 latency_components.clear();
248 void LatencyInfo::TraceEventType(const char* event_type) {
249 TRACE_EVENT_ASYNC_STEP_INTO0("benchmark",
250 "InputLatency",
251 TRACE_ID_DONT_MANGLE(trace_id),
252 event_type);
255 } // namespace ui