latency_info.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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 <map>
9#include <utility>
10
11#include "base/basictypes.h"
12#include "base/time/time.h"
13#include "ui/events/events_base_export.h"
14
15namespace ui {
16
17enum LatencyComponentType {
18  // ---------------------------BEGIN COMPONENT-------------------------------
19  // BEGIN COMPONENT is when we show the latency begin in chrome://tracing.
20  // Timestamp when the input event is sent from RenderWidgetHost to renderer.
21  INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT,
22  // ---------------------------NORMAL COMPONENT-------------------------------
23  // Timestamp when the scroll update gesture event is sent from RWH to
24  // renderer. In Aura, touch event's LatencyInfo is carried over to the gesture
25  // event. So gesture event's INPUT_EVENT_LATENCY_RWH_COMPONENT is the
26  // timestamp when its original touch events is sent from RWH to renderer.
27  // In non-aura platform, INPUT_EVENT_LATENCY_SCROLL_UPDATE_RWH_COMPONENT
28  // is the same as INPUT_EVENT_LATENCY_RWH_COMPONENT.
29  INPUT_EVENT_LATENCY_SCROLL_UPDATE_RWH_COMPONENT,
30  // The original timestamp of the touch event which converts to scroll update.
31  INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT,
32  // Original timestamp for input event (e.g. timestamp from kernel).
33  INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
34  // Timestamp when the UI event is created.
35  INPUT_EVENT_LATENCY_UI_COMPONENT,
36  // This is special component indicating there is rendering scheduled for
37  // the event associated with this LatencyInfo.
38  INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_COMPONENT,
39  // Timestamp when the touch event is acked.
40  INPUT_EVENT_LATENCY_ACKED_TOUCH_COMPONENT,
41  // Frame number when a window snapshot was requested. The snapshot
42  // is taken when the rendering results actually reach the screen.
43  WINDOW_SNAPSHOT_FRAME_NUMBER_COMPONENT,
44  // ---------------------------TERMINAL COMPONENT-----------------------------
45  // TERMINAL COMPONENT is when we show the latency end in chrome://tracing.
46  // Timestamp when the mouse event is acked from renderer and it does not
47  // cause any rendering scheduled.
48  INPUT_EVENT_LATENCY_TERMINATED_MOUSE_COMPONENT,
49  // Timestamp when the touch event is acked from renderer and it does not
50  // cause any rendering schedueld and does not generate any gesture event.
51  INPUT_EVENT_LATENCY_TERMINATED_TOUCH_COMPONENT,
52  // Timestamp when the gesture event is acked from renderer, and it does not
53  // cause any rendering schedueld.
54  INPUT_EVENT_LATENCY_TERMINATED_GESTURE_COMPONENT,
55  // Timestamp when the frame is swapped (i.e. when the rendering caused by
56  // input event actually takes effect).
57  INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT,
58  // This component indicates that the input causes a commit to be scheduled
59  // but the commit failed.
60  INPUT_EVENT_LATENCY_TERMINATED_COMMIT_FAILED_COMPONENT,
61  // This component indicates that the input causes a swap to be scheduled
62  // but the swap failed.
63  INPUT_EVENT_LATENCY_TERMINATED_SWAP_FAILED_COMPONENT,
64  // This component indicates that the cached LatencyInfo number exceeds the
65  // maximal allowed size.
66  LATENCY_INFO_LIST_TERMINATED_OVERFLOW_COMPONENT,
67};
68
69struct EVENTS_BASE_EXPORT LatencyInfo {
70  struct LatencyComponent {
71    // Nondecreasing number that can be used to determine what events happened
72    // in the component at the time this struct was sent on to the next
73    // component.
74    int64 sequence_number;
75    // Average time of events that happened in this component.
76    base::TimeTicks event_time;
77    // Count of events that happened in this component
78    uint32 event_count;
79  };
80
81  // Map a Latency Component (with a component-specific int64 id) to a
82  // component info.
83  typedef std::map<std::pair<LatencyComponentType, int64>, LatencyComponent>
84      LatencyMap;
85
86  LatencyInfo();
87
88  ~LatencyInfo();
89
90  // Merges the contents of another LatencyInfo into this one.
91  void MergeWith(const LatencyInfo& other);
92
93  // Add LatencyComponents that are in |other| but not in |this|.
94  void AddNewLatencyFrom(const LatencyInfo& other);
95
96  // Modifies the current sequence number for a component, and adds a new
97  // sequence number with the current timestamp.
98  void AddLatencyNumber(LatencyComponentType component,
99                        int64 id,
100                        int64 component_sequence_number);
101
102  // Modifies the current sequence number and adds a certain number of events
103  // for a specific component.
104  // TODO(miletus): Remove the |dump_to_trace| once we remove MergeWith().
105  void AddLatencyNumberWithTimestamp(LatencyComponentType component,
106                                     int64 id,
107                                     int64 component_sequence_number,
108                                     base::TimeTicks time,
109                                     uint32 event_count,
110                                     bool dump_to_trace);
111
112  // Returns true if the a component with |type| and |id| is found in
113  // the latency_components and the component is stored to |output| if
114  // |output| is not NULL. Returns false if no such component is found.
115  bool FindLatency(LatencyComponentType type,
116                   int64 id,
117                   LatencyComponent* output) const;
118
119  void RemoveLatency(LatencyComponentType type);
120
121  void Clear();
122
123  LatencyMap latency_components;
124  // The unique id for matching the ASYNC_BEGIN/END trace event.
125  int64 trace_id;
126  // Whether a terminal component has been added.
127  bool terminated;
128};
129
130}  // namespace ui
131
132#endif  // UI_EVENTS_LATENCY_INFO_H_
133