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_
11 #include "base/basictypes.h"
12 #include "base/containers/small_map.h"
13 #include "base/time/time.h"
14 #include "ui/events/events_base_export.h"
18 enum LatencyComponentType
{
19 // ---------------------------BEGIN COMPONENT-------------------------------
20 // BEGIN COMPONENT is when we show the latency begin in chrome://tracing.
21 // Timestamp when the input event is sent from RenderWidgetHost to renderer.
22 INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT
,
23 // Timestamp when the input event is received in plugin.
24 INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT
,
25 // ---------------------------NORMAL COMPONENT-------------------------------
26 // Timestamp when the scroll update gesture event is sent from RWH to
27 // renderer. In Aura, touch event's LatencyInfo is carried over to the gesture
28 // event. So gesture event's INPUT_EVENT_LATENCY_RWH_COMPONENT is the
29 // timestamp when its original touch events is sent from RWH to renderer.
30 // In non-aura platform, INPUT_EVENT_LATENCY_SCROLL_UPDATE_RWH_COMPONENT
31 // is the same as INPUT_EVENT_LATENCY_RWH_COMPONENT.
32 INPUT_EVENT_LATENCY_SCROLL_UPDATE_RWH_COMPONENT
,
33 // The original timestamp of the touch event which converts to scroll update.
34 INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT
,
35 // Original timestamp for input event (e.g. timestamp from kernel).
36 INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT
,
37 // Timestamp when the UI event is created.
38 INPUT_EVENT_LATENCY_UI_COMPONENT
,
39 // This is special component indicating there is rendering scheduled for
40 // the event associated with this LatencyInfo.
41 INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_COMPONENT
,
42 // Timestamp when the touch event is acked.
43 INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT
,
44 // Frame number when a window snapshot was requested. The snapshot
45 // is taken when the rendering results actually reach the screen.
46 WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT
,
47 // Frame number for a snapshot requested via
48 // gpuBenchmarking.beginWindowSnapshotPNG
49 // TODO(vkuzkokov): remove when patch adding this hits Stable
50 WINDOW_OLD_SNAPSHOT_FRAME_NUMBER_COMPONENT
,
51 // ---------------------------TERMINAL COMPONENT-----------------------------
52 // TERMINAL COMPONENT is when we show the latency end in chrome://tracing.
53 // Timestamp when the mouse event is acked from renderer and it does not
54 // cause any rendering scheduled.
55 INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT
,
56 // Timestamp when the touch event is acked from renderer and it does not
57 // cause any rendering schedueld and does not generate any gesture event.
58 INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT
,
59 // Timestamp when the gesture event is acked from renderer, and it does not
60 // cause any rendering schedueld.
61 INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT
,
62 // Timestamp when the frame is swapped (i.e. when the rendering caused by
63 // input event actually takes effect).
64 INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT
,
65 // This component indicates that the input causes a commit to be scheduled
66 // but the commit failed.
67 INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT
,
68 // This component indicates that the input causes a swap to be scheduled
69 // but the swap failed.
70 INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT
,
71 // This component indicates that the cached LatencyInfo number exceeds the
72 // maximal allowed size.
73 LATENCY_INFO_LIST_TERMINATED_OVERFLOW_COMPONENT
,
74 // Timestamp when the input event is considered not cause any rendering
75 // damage in plugin and thus terminated.
76 INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT
,
77 LATENCY_COMPONENT_TYPE_LAST
= INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT
80 struct EVENTS_BASE_EXPORT LatencyInfo
{
81 struct LatencyComponent
{
82 // Nondecreasing number that can be used to determine what events happened
83 // in the component at the time this struct was sent on to the next
85 int64 sequence_number
;
86 // Average time of events that happened in this component.
87 base::TimeTicks event_time
;
88 // Count of events that happened in this component
92 // Empirically determined constant based on a typical scroll sequence.
93 enum { kTypicalMaxComponentsPerLatencyInfo
= 6 };
95 // Map a Latency Component (with a component-specific int64 id) to a
97 typedef base::SmallMap
<
98 std::map
<std::pair
<LatencyComponentType
, int64
>, LatencyComponent
>,
99 kTypicalMaxComponentsPerLatencyInfo
> LatencyMap
;
105 // Returns true if the vector |latency_info| is valid. Returns false
106 // if it is not valid and log the |referring_msg|.
107 // This function is mainly used to check the latency_info vector that
108 // is passed between processes using IPC message has reasonable size
109 // so that we are confident the IPC message is not corrupted/compromised.
110 // This check will go away once the IPC system has better built-in scheme
111 // for corruption/compromise detection.
112 static bool Verify(const std::vector
<LatencyInfo
>& latency_info
,
113 const char* referring_msg
);
115 // Copy LatencyComponents with type |type| from |other| into |this|.
116 void CopyLatencyFrom(const LatencyInfo
& other
, LatencyComponentType type
);
118 // Add LatencyComponents that are in |other| but not in |this|.
119 void AddNewLatencyFrom(const LatencyInfo
& other
);
121 // Modifies the current sequence number for a component, and adds a new
122 // sequence number with the current timestamp.
123 void AddLatencyNumber(LatencyComponentType component
,
125 int64 component_sequence_number
);
127 // Modifies the current sequence number and adds a certain number of events
128 // for a specific component.
129 void AddLatencyNumberWithTimestamp(LatencyComponentType component
,
131 int64 component_sequence_number
,
132 base::TimeTicks time
,
135 // Returns true if the a component with |type| and |id| is found in
136 // the latency_components and the component is stored to |output| if
137 // |output| is not NULL. Returns false if no such component is found.
138 bool FindLatency(LatencyComponentType type
,
140 LatencyComponent
* output
) const;
142 void RemoveLatency(LatencyComponentType type
);
146 // Records the |event_type| in trace buffer as TRACE_EVENT_ASYNC_STEP.
147 void TraceEventType(const char* event_type
);
149 LatencyMap latency_components
;
150 // The unique id for matching the ASYNC_BEGIN/END trace event.
152 // Whether a terminal component has been added.
158 #endif // UI_EVENTS_LATENCY_INFO_H_