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