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 #ifndef UI_EVENTS_LATENCY_INFO_H_
6 #define UI_EVENTS_LATENCY_INFO_H_
12 #include "base/basictypes.h"
13 #include "base/containers/small_map.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/time/time.h"
17 #include "base/trace_event/trace_event.h"
18 #include "ipc/ipc_param_traits.h"
19 #include "ui/events/events_base_export.h"
23 enum LatencyComponentType
{
24 // ---------------------------BEGIN COMPONENT-------------------------------
25 // BEGIN COMPONENT is when we show the latency begin in chrome://tracing.
26 // Timestamp when the input event is sent from RenderWidgetHost to renderer.
27 INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
,
28 // In threaded scrolling, main thread scroll listener update is async to
29 // scroll processing in impl thread. This is the timestamp when we consider
30 // the main thread scroll listener update is begun.
31 LATENCY_BEGIN_SCROLL_LISTENER_UPDATE_MAIN_COMPONENT
,
32 // ---------------------------NORMAL COMPONENT-------------------------------
33 // The original timestamp of the touch event which converts to scroll update.
34 INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT
,
35 // The original timestamp of the touch event which converts to the *first*
36 // scroll update in a scroll gesture sequence.
37 INPUT_EVENT_LATENCY_FIRST_SCROLL_UPDATE_ORIGINAL_COMPONENT
,
38 // Original timestamp for input event (e.g. timestamp from kernel).
39 INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT
,
40 // Timestamp when the UI event is created.
41 INPUT_EVENT_LATENCY_UI_COMPONENT
,
42 // This is special component indicating there is rendering scheduled for
43 // the event associated with this LatencyInfo on main thread.
44 INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_MAIN_COMPONENT
,
45 // This is special component indicating there is rendering scheduled for
46 // the event associated with this LatencyInfo on impl thread.
47 INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_IMPL_COMPONENT
,
48 // Timestamp when a scroll update is forwarded to the main thread.
49 INPUT_EVENT_LATENCY_FORWARD_SCROLL_UPDATE_TO_MAIN_COMPONENT
,
50 // Timestamp when the event's ack is received by the RWH.
51 INPUT_EVENT_LATENCY_ACK_RWH_COMPONENT
,
52 // Frame number when a window snapshot was requested. The snapshot
53 // is taken when the rendering results actually reach the screen.
54 WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT
,
55 // Timestamp when a tab is requested to be shown.
57 // Timestamp when the frame is swapped in renderer.
58 INPUT_EVENT_LATENCY_RENDERER_SWAP_COMPONENT
,
59 // Timestamp of when the browser process receives a buffer swap notification
61 INPUT_EVENT_BROWSER_RECEIVED_RENDERER_SWAP_COMPONENT
,
62 // Timestamp of when the gpu service began swap buffers, unlike
63 // INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT which measures after.
64 INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT
,
65 // ---------------------------TERMINAL COMPONENT-----------------------------
66 // TERMINAL COMPONENT is when we show the latency end in chrome://tracing.
67 // Timestamp when the mouse event is acked from renderer and it does not
68 // cause any rendering scheduled.
69 INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT
,
70 // Timestamp when the mouse wheel event is acked from renderer and it does not
71 // cause any rendering scheduled.
72 INPUT_EVENT_LATENCY_TERMINATED_MOUSE_WHEEL_COMPONENT
,
73 // Timestamp when the keyboard event is acked from renderer and it does not
74 // cause any rendering scheduled.
75 INPUT_EVENT_LATENCY_TERMINATED_KEYBOARD_COMPONENT
,
76 // Timestamp when the touch event is acked from renderer and it does not
77 // cause any rendering schedueld and does not generate any gesture event.
78 INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT
,
79 // Timestamp when the gesture event is acked from renderer, and it does not
80 // cause any rendering schedueld.
81 INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT
,
82 // Timestamp when the frame is swapped (i.e. when the rendering caused by
83 // input event actually takes effect).
84 INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT
,
85 // This component indicates that the input causes a commit to be scheduled
86 // but the commit failed.
87 INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT
,
88 // This component indicates that the input causes a commit to be scheduled
89 // but the commit was aborted since it carried no new information.
90 INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT
,
91 // This component indicates that the input causes a swap to be scheduled
92 // but the swap failed.
93 INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT
,
94 LATENCY_COMPONENT_TYPE_LAST
=
95 INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT
,
98 class EVENTS_BASE_EXPORT LatencyInfo
{
100 struct LatencyComponent
{
101 // Nondecreasing number that can be used to determine what events happened
102 // in the component at the time this struct was sent on to the next
104 int64 sequence_number
;
105 // Average time of events that happened in this component.
106 base::TimeTicks event_time
;
107 // Count of events that happened in this component
111 struct EVENTS_BASE_EXPORT InputCoordinate
{
113 InputCoordinate(float x
, float y
);
119 // Empirically determined constant based on a typical scroll sequence.
120 enum { kTypicalMaxComponentsPerLatencyInfo
= 10 };
122 enum { kMaxInputCoordinates
= 2 };
124 // Map a Latency Component (with a component-specific int64 id) to a
126 typedef base::SmallMap
<
127 std::map
<std::pair
<LatencyComponentType
, int64
>, LatencyComponent
>,
128 kTypicalMaxComponentsPerLatencyInfo
> LatencyMap
;
134 LatencyInfo(int64 trace_id
, bool terminated
);
136 // Returns true if the vector |latency_info| is valid. Returns false
137 // if it is not valid and log the |referring_msg|.
138 // This function is mainly used to check the latency_info vector that
139 // is passed between processes using IPC message has reasonable size
140 // so that we are confident the IPC message is not corrupted/compromised.
141 // This check will go away once the IPC system has better built-in scheme
142 // for corruption/compromise detection.
143 static bool Verify(const std::vector
<LatencyInfo
>& latency_info
,
144 const char* referring_msg
);
146 // Copy LatencyComponents with type |type| from |other| into |this|.
147 void CopyLatencyFrom(const LatencyInfo
& other
, LatencyComponentType type
);
149 // Add LatencyComponents that are in |other| but not in |this|.
150 void AddNewLatencyFrom(const LatencyInfo
& other
);
152 // Modifies the current sequence number for a component, and adds a new
153 // sequence number with the current timestamp.
154 void AddLatencyNumber(LatencyComponentType component
,
156 int64 component_sequence_number
);
158 // Similar to |AddLatencyNumber|, and also appends |trace_name_str| to
159 // the trace event's name.
160 // This function should only be called when adding a BEGIN component.
161 void AddLatencyNumberWithTraceName(LatencyComponentType component
,
163 int64 component_sequence_number
,
164 const char* trace_name_str
);
166 // Modifies the current sequence number and adds a certain number of events
167 // for a specific component.
168 void AddLatencyNumberWithTimestamp(LatencyComponentType component
,
170 int64 component_sequence_number
,
171 base::TimeTicks time
,
174 // Returns true if the a component with |type| and |id| is found in
175 // the latency_components and the component is stored to |output| if
176 // |output| is not NULL. Returns false if no such component is found.
177 bool FindLatency(LatencyComponentType type
,
179 LatencyComponent
* output
) const;
181 void RemoveLatency(LatencyComponentType type
);
183 // Returns true if there is still room for keeping the |input_coordinate|,
185 bool AddInputCoordinate(const InputCoordinate
& input_coordinate
);
187 uint32
input_coordinates_size() const { return input_coordinates_size_
; }
188 const InputCoordinate
* input_coordinates() const {
189 return input_coordinates_
;
191 const LatencyMap
& latency_components() const { return latency_components_
; }
193 bool terminated() const { return terminated_
; }
194 int64
trace_id() const { return trace_id_
; }
197 void AddLatencyNumberWithTimestampImpl(LatencyComponentType component
,
199 int64 component_sequence_number
,
200 base::TimeTicks time
,
202 const char* trace_name_str
);
204 // Converts latencyinfo into format that can be dumped into trace buffer.
205 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> AsTraceableData();
207 // Shown as part of the name of the trace event for this LatencyInfo.
208 // String is empty if no tracing is enabled.
209 std::string trace_name_
;
211 LatencyMap latency_components_
;
213 // These coordinates represent window coordinates of the original input event.
214 uint32 input_coordinates_size_
;
215 InputCoordinate input_coordinates_
[kMaxInputCoordinates
];
217 // The unique id for matching the ASYNC_BEGIN/END trace event.
219 // Whether a terminal component has been added.
222 FRIEND_TEST_ALL_PREFIXES(LatencyInfoParamTraitsTest
, Basic
);
223 friend struct IPC::ParamTraits
<ui::LatencyInfo
>;
228 #endif // UI_EVENTS_LATENCY_INFO_H_