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 touch event is acked from renderer and it does not
71 // cause any rendering schedueld and does not generate any gesture event.
72 INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT
,
73 // Timestamp when the gesture event is acked from renderer, and it does not
74 // cause any rendering schedueld.
75 INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT
,
76 // Timestamp when the frame is swapped (i.e. when the rendering caused by
77 // input event actually takes effect).
78 INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT
,
79 // This component indicates that the input causes a commit to be scheduled
80 // but the commit failed.
81 INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT
,
82 // This component indicates that the input causes a commit to be scheduled
83 // but the commit was aborted since it carried no new information.
84 INPUT_EVENT_LATENCY_TERMINATED_COMMIT_NO_UPDATE_COMPONENT
,
85 // This component indicates that the input causes a swap to be scheduled
86 // but the swap failed.
87 INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT
,
88 LATENCY_COMPONENT_TYPE_LAST
=
89 INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT
,
92 class EVENTS_BASE_EXPORT LatencyInfo
{
94 struct LatencyComponent
{
95 // Nondecreasing number that can be used to determine what events happened
96 // in the component at the time this struct was sent on to the next
98 int64 sequence_number
;
99 // Average time of events that happened in this component.
100 base::TimeTicks event_time
;
101 // Count of events that happened in this component
105 struct EVENTS_BASE_EXPORT InputCoordinate
{
107 InputCoordinate(float x
, float y
);
113 // Empirically determined constant based on a typical scroll sequence.
114 enum { kTypicalMaxComponentsPerLatencyInfo
= 10 };
116 enum { kMaxInputCoordinates
= 2 };
118 // Map a Latency Component (with a component-specific int64 id) to a
120 typedef base::SmallMap
<
121 std::map
<std::pair
<LatencyComponentType
, int64
>, LatencyComponent
>,
122 kTypicalMaxComponentsPerLatencyInfo
> LatencyMap
;
128 LatencyInfo(int64 trace_id
, bool terminated
);
130 // Returns true if the vector |latency_info| is valid. Returns false
131 // if it is not valid and log the |referring_msg|.
132 // This function is mainly used to check the latency_info vector that
133 // is passed between processes using IPC message has reasonable size
134 // so that we are confident the IPC message is not corrupted/compromised.
135 // This check will go away once the IPC system has better built-in scheme
136 // for corruption/compromise detection.
137 static bool Verify(const std::vector
<LatencyInfo
>& latency_info
,
138 const char* referring_msg
);
140 // Copy LatencyComponents with type |type| from |other| into |this|.
141 void CopyLatencyFrom(const LatencyInfo
& other
, LatencyComponentType type
);
143 // Add LatencyComponents that are in |other| but not in |this|.
144 void AddNewLatencyFrom(const LatencyInfo
& other
);
146 // Modifies the current sequence number for a component, and adds a new
147 // sequence number with the current timestamp.
148 void AddLatencyNumber(LatencyComponentType component
,
150 int64 component_sequence_number
);
152 // Similar to |AddLatencyNumber|, and also appends |trace_name_str| to
153 // the trace event's name.
154 // This function should only be called when adding a BEGIN component.
155 void AddLatencyNumberWithTraceName(LatencyComponentType component
,
157 int64 component_sequence_number
,
158 const char* trace_name_str
);
160 // Modifies the current sequence number and adds a certain number of events
161 // for a specific component.
162 void AddLatencyNumberWithTimestamp(LatencyComponentType component
,
164 int64 component_sequence_number
,
165 base::TimeTicks time
,
168 // Returns true if the a component with |type| and |id| is found in
169 // the latency_components and the component is stored to |output| if
170 // |output| is not NULL. Returns false if no such component is found.
171 bool FindLatency(LatencyComponentType type
,
173 LatencyComponent
* output
) const;
175 void RemoveLatency(LatencyComponentType type
);
177 // Returns true if there is still room for keeping the |input_coordinate|,
179 bool AddInputCoordinate(const InputCoordinate
& input_coordinate
);
181 uint32
input_coordinates_size() const { return input_coordinates_size_
; }
182 const InputCoordinate
* input_coordinates() const {
183 return input_coordinates_
;
185 const LatencyMap
& latency_components() const { return latency_components_
; }
187 bool terminated() const { return terminated_
; }
188 int64
trace_id() const { return trace_id_
; }
191 void AddLatencyNumberWithTimestampImpl(LatencyComponentType component
,
193 int64 component_sequence_number
,
194 base::TimeTicks time
,
196 const char* trace_name_str
);
198 // Converts latencyinfo into format that can be dumped into trace buffer.
199 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> AsTraceableData();
201 // Shown as part of the name of the trace event for this LatencyInfo.
202 // String is empty if no tracing is enabled.
203 std::string trace_name_
;
205 LatencyMap latency_components_
;
207 // These coordinates represent window coordinates of the original input event.
208 uint32 input_coordinates_size_
;
209 InputCoordinate input_coordinates_
[kMaxInputCoordinates
];
211 // The unique id for matching the ASYNC_BEGIN/END trace event.
213 // Whether a terminal component has been added.
216 FRIEND_TEST_ALL_PREFIXES(LatencyInfoParamTraitsTest
, Basic
);
217 friend struct IPC::ParamTraits
<ui::LatencyInfo
>;
222 #endif // UI_EVENTS_LATENCY_INFO_H_