OLD | NEW |
| (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 #ifndef UI_EVENTS_LATENCY_INFO_H_ | |
6 #define UI_EVENTS_LATENCY_INFO_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <memory> | |
11 #include <string> | |
12 #include <utility> | |
13 #include <vector> | |
14 | |
15 #include "base/containers/small_map.h" | |
16 #include "base/time/time.h" | |
17 #include "base/trace_event/trace_event.h" | |
18 #include "ui/events/events_base_export.h" | |
19 | |
20 #if !defined(OS_IOS) | |
21 #include "ipc/ipc_param_traits.h" // nogncheck | |
22 #endif | |
23 | |
24 namespace ui { | |
25 | |
26 // When adding new components, or new metrics based on LatencyInfo, | |
27 // please update latency_info.dot. | |
28 enum LatencyComponentType { | |
29 // ---------------------------BEGIN COMPONENT------------------------------- | |
30 // BEGIN COMPONENT is when we show the latency begin in chrome://tracing. | |
31 // Timestamp when the input event is sent from RenderWidgetHost to renderer. | |
32 INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT, | |
33 // In threaded scrolling, main thread scroll listener update is async to | |
34 // scroll processing in impl thread. This is the timestamp when we consider | |
35 // the main thread scroll listener update is begun. | |
36 LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT, | |
37 // ---------------------------NORMAL COMPONENT------------------------------- | |
38 // The original timestamp of the touch event which converts to scroll update. | |
39 INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT, | |
40 // The original timestamp of the touch event which converts to the *first* | |
41 // scroll update in a scroll gesture sequence. | |
42 INPUT_EVENT_LATENCY_FIRST_SCROLL_UPDATE_ORIGINAL_COMPONENT, | |
43 // Original timestamp for input event (e.g. timestamp from kernel). | |
44 INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, | |
45 // Timestamp when the UI event is created. | |
46 INPUT_EVENT_LATENCY_UI_COMPONENT, | |
47 // Timestamp when the event is dispatched on the main thread of the renderer. | |
48 INPUT_EVENT_LATENCY_RENDERER_MAIN_COMPONENT, | |
49 // This is special component indicating there is rendering scheduled for | |
50 // the event associated with this LatencyInfo on main thread. | |
51 INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_MAIN_COMPONENT, | |
52 // This is special component indicating there is rendering scheduled for | |
53 // the event associated with this LatencyInfo on impl thread. | |
54 INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_IMPL_COMPONENT, | |
55 // Timestamp when a scroll update is forwarded to the main thread. | |
56 INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT, | |
57 // Timestamp when the event's ack is received by the RWH. | |
58 INPUT_EVENT_LATENCY_ACK_RWH_COMPONENT, | |
59 // Frame number when a window snapshot was requested. The snapshot | |
60 // is taken when the rendering results actually reach the screen. | |
61 WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT, | |
62 // Timestamp when a tab is requested to be shown. | |
63 TAB_SHOW_COMPONENT, | |
64 // Timestamp when the frame is swapped in renderer. | |
65 INPUT_EVENT_LATENCY_RENDERER_SWAP_COMPONENT, | |
66 // Timestamp of when the browser process receives a buffer swap notification | |
67 // from the renderer. | |
68 INPUT_EVENT_BROWSER_RECEIVED_RENDERER_SWAP_COMPONENT, | |
69 // Timestamp of when the gpu service began swap buffers, unlike | |
70 // INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT which measures after. | |
71 INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT, | |
72 // Timestamp of when the gesture scroll update is generated from a mouse wheel | |
73 // event. | |
74 INPUT_EVENT_LATENCY_GENERATE_SCROLL_UPDATE_FROM_MOUSE_WHEEL, | |
75 // ---------------------------TERMINAL COMPONENT----------------------------- | |
76 // TERMINAL COMPONENT is when we show the latency end in chrome://tracing. | |
77 // Timestamp when the mouse event is acked from renderer and it does not | |
78 // cause any rendering scheduled. | |
79 INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT, | |
80 // Timestamp when the mouse wheel event is acked from renderer and it does not | |
81 // cause any rendering scheduled. | |
82 INPUT_EVENT_LATENCY_TERMINATED_MOUSE_WHEEL_COMPONENT, | |
83 // Timestamp when the keyboard event is acked from renderer and it does not | |
84 // cause any rendering scheduled. | |
85 INPUT_EVENT_LATENCY_TERMINATED_KEYBOARD_COMPONENT, | |
86 // Timestamp when the touch event is acked from renderer and it does not | |
87 // cause any rendering scheduled and does not generate any gesture event. | |
88 INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT, | |
89 // Timestamp when the gesture event is acked from renderer, and it does not | |
90 // cause any rendering scheduled. | |
91 INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT, | |
92 // Timestamp when the frame is swapped (i.e. when the rendering caused by | |
93 // input event actually takes effect). | |
94 INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT, | |
95 // This component indicates that the input causes a commit to be scheduled | |
96 // but the commit failed. | |
97 INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT, | |
98 // This component indicates that the input causes a commit to be scheduled | |
99 // but the commit was aborted since it carried no new information. | |
100 INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT, | |
101 // This component indicates that the input causes a swap to be scheduled | |
102 // but the swap failed. | |
103 INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT, | |
104 LATENCY_COMPONENT_TYPE_LAST = | |
105 INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT, | |
106 }; | |
107 | |
108 class EVENTS_BASE_EXPORT LatencyInfo { | |
109 public: | |
110 struct LatencyComponent { | |
111 // Nondecreasing number that can be used to determine what events happened | |
112 // in the component at the time this struct was sent on to the next | |
113 // component. | |
114 int64_t sequence_number; | |
115 // Average time of events that happened in this component. | |
116 base::TimeTicks event_time; | |
117 // Count of events that happened in this component | |
118 uint32_t event_count; | |
119 }; | |
120 | |
121 struct EVENTS_BASE_EXPORT InputCoordinate { | |
122 InputCoordinate(); | |
123 InputCoordinate(float x, float y); | |
124 | |
125 float x; | |
126 float y; | |
127 }; | |
128 | |
129 // Empirically determined constant based on a typical scroll sequence. | |
130 enum { kTypicalMaxComponentsPerLatencyInfo = 10 }; | |
131 | |
132 enum { kMaxInputCoordinates = 2 }; | |
133 | |
134 // Map a Latency Component (with a component-specific int64_t id) to a | |
135 // component info. | |
136 typedef base::SmallMap< | |
137 std::map<std::pair<LatencyComponentType, int64_t>, LatencyComponent>, | |
138 kTypicalMaxComponentsPerLatencyInfo> LatencyMap; | |
139 | |
140 LatencyInfo(); | |
141 LatencyInfo(const LatencyInfo& other); | |
142 ~LatencyInfo(); | |
143 | |
144 // For test only. | |
145 LatencyInfo(int64_t trace_id, bool terminated); | |
146 | |
147 // Returns true if the vector |latency_info| is valid. Returns false | |
148 // if it is not valid and log the |referring_msg|. | |
149 // This function is mainly used to check the latency_info vector that | |
150 // is passed between processes using IPC message has reasonable size | |
151 // so that we are confident the IPC message is not corrupted/compromised. | |
152 // This check will go away once the IPC system has better built-in scheme | |
153 // for corruption/compromise detection. | |
154 static bool Verify(const std::vector<LatencyInfo>& latency_info, | |
155 const char* referring_msg); | |
156 | |
157 // Copy LatencyComponents with type |type| from |other| into |this|. | |
158 void CopyLatencyFrom(const LatencyInfo& other, LatencyComponentType type); | |
159 | |
160 // Add LatencyComponents that are in |other| but not in |this|. | |
161 void AddNewLatencyFrom(const LatencyInfo& other); | |
162 | |
163 // Modifies the current sequence number for a component, and adds a new | |
164 // sequence number with the current timestamp. | |
165 void AddLatencyNumber(LatencyComponentType component, | |
166 int64_t id, | |
167 int64_t component_sequence_number); | |
168 | |
169 // Similar to |AddLatencyNumber|, and also appends |trace_name_str| to | |
170 // the trace event's name. | |
171 // This function should only be called when adding a BEGIN component. | |
172 void AddLatencyNumberWithTraceName(LatencyComponentType component, | |
173 int64_t id, | |
174 int64_t component_sequence_number, | |
175 const char* trace_name_str); | |
176 | |
177 // Modifies the current sequence number and adds a certain number of events | |
178 // for a specific component. | |
179 void AddLatencyNumberWithTimestamp(LatencyComponentType component, | |
180 int64_t id, | |
181 int64_t component_sequence_number, | |
182 base::TimeTicks time, | |
183 uint32_t event_count); | |
184 | |
185 // Returns true if the a component with |type| and |id| is found in | |
186 // the latency_components and the component is stored to |output| if | |
187 // |output| is not NULL. Returns false if no such component is found. | |
188 bool FindLatency(LatencyComponentType type, | |
189 int64_t id, | |
190 LatencyComponent* output) const; | |
191 | |
192 void RemoveLatency(LatencyComponentType type); | |
193 | |
194 // Returns true if there is still room for keeping the |input_coordinate|, | |
195 // false otherwise. | |
196 bool AddInputCoordinate(const InputCoordinate& input_coordinate); | |
197 | |
198 uint32_t input_coordinates_size() const { return input_coordinates_size_; } | |
199 const InputCoordinate* input_coordinates() const { | |
200 return input_coordinates_; | |
201 } | |
202 | |
203 const LatencyMap& latency_components() const { return latency_components_; } | |
204 | |
205 bool terminated() const { return terminated_; } | |
206 void set_coalesced() { coalesced_ = true; } | |
207 bool coalesced() const { return coalesced_; } | |
208 int64_t trace_id() const { return trace_id_; } | |
209 | |
210 private: | |
211 void AddLatencyNumberWithTimestampImpl(LatencyComponentType component, | |
212 int64_t id, | |
213 int64_t component_sequence_number, | |
214 base::TimeTicks time, | |
215 uint32_t event_count, | |
216 const char* trace_name_str); | |
217 | |
218 // Converts latencyinfo into format that can be dumped into trace buffer. | |
219 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> | |
220 AsTraceableData(); | |
221 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> | |
222 CoordinatesAsTraceableData(); | |
223 | |
224 // Shown as part of the name of the trace event for this LatencyInfo. | |
225 // String is empty if no tracing is enabled. | |
226 std::string trace_name_; | |
227 | |
228 LatencyMap latency_components_; | |
229 | |
230 // These coordinates represent window coordinates of the original input event. | |
231 uint32_t input_coordinates_size_; | |
232 InputCoordinate input_coordinates_[kMaxInputCoordinates]; | |
233 | |
234 // The unique id for matching the ASYNC_BEGIN/END trace event. | |
235 int64_t trace_id_; | |
236 // Whether this event has been coalesced into another event. | |
237 bool coalesced_; | |
238 // Whether a terminal component has been added. | |
239 bool terminated_; | |
240 | |
241 #if !defined(OS_IOS) | |
242 friend struct IPC::ParamTraits<ui::LatencyInfo>; | |
243 #endif | |
244 }; | |
245 | |
246 } // namespace ui | |
247 | |
248 #endif // UI_EVENTS_LATENCY_INFO_H_ | |
OLD | NEW |