latency_info.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #ifndef UI_LATENCY_LATENCY_INFO_H_
  5. #define UI_LATENCY_LATENCY_INFO_H_
  6. #include <vector>
  7. #include "base/containers/flat_map.h"
  8. #include "base/time/time.h"
  9. #include "build/build_config.h"
  10. #include "services/metrics/public/cpp/ukm_source_id.h"
  11. #include "third_party/perfetto/protos/perfetto/trace/track_event/chrome_latency_info.pbzero.h"
  12. #if !BUILDFLAG(IS_IOS)
  13. #include "ipc/ipc_param_traits.h" // nogncheck
  14. #include "mojo/public/cpp/bindings/struct_traits.h" // nogncheck
  15. #endif
  16. namespace ui {
  17. #if !BUILDFLAG(IS_IOS)
  18. namespace mojom {
  19. class LatencyInfoDataView;
  20. }
  21. #endif
  22. // When adding new components, or new metrics based on LatencyInfo,
  23. // please update latency_info.dot.
  24. //
  25. // When adding new components, please update
  26. // //third_party/perfetto/protos/perfetto/trace/track_event/chrome_latency_info.proto
  27. // so both this and the internal versions can be kept up to date. Or reach out
  28. // to tracing@chromium.org so we can assist.
  29. enum LatencyComponentType {
  30. // ---------------------------BEGIN COMPONENT-------------------------------
  31. // BEGIN COMPONENT is when we show the latency begin in chrome://tracing.
  32. // Timestamp when the input event is sent from RenderWidgetHost to renderer.
  33. INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT,
  34. // ---------------------------NORMAL COMPONENT-------------------------------
  35. // The original timestamp of the touch event which converts to scroll update.
  36. INPUT_EVENT_LATENCY_SCROLL_UPDATE_ORIGINAL_COMPONENT,
  37. // The original timestamp of the touch event which converts to the *first*
  38. // scroll update in a scroll gesture sequence.
  39. INPUT_EVENT_LATENCY_FIRST_SCROLL_UPDATE_ORIGINAL_COMPONENT,
  40. // Original timestamp for input event (e.g. timestamp from kernel).
  41. INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
  42. // Timestamp when the UI event is created.
  43. INPUT_EVENT_LATENCY_UI_COMPONENT,
  44. // Timestamp when the event is dispatched on the main thread of the renderer.
  45. INPUT_EVENT_LATENCY_RENDERER_MAIN_COMPONENT,
  46. // This is special component indicating there is rendering scheduled for
  47. // the event associated with this LatencyInfo on main thread.
  48. INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_MAIN_COMPONENT,
  49. // This is special component indicating there is rendering scheduled for
  50. // the event associated with this LatencyInfo on impl thread.
  51. INPUT_EVENT_LATENCY_RENDERING_SCHEDULED_IMPL_COMPONENT,
  52. // Timestamp when the frame is swapped in renderer.
  53. INPUT_EVENT_LATENCY_RENDERER_SWAP_COMPONENT,
  54. // Timestamp of when the display compositor receives a compositor frame from
  55. // the renderer.
  56. // Display compositor can be either in the browser process or in Mus.
  57. DISPLAY_COMPOSITOR_RECEIVED_FRAME_COMPONENT,
  58. // Timestamp of when the gpu service began swap buffers, unlike
  59. // INPUT_EVENT_LATENCY_FRAME_SWAP_COMPONENT which measures after.
  60. INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT,
  61. // Timestamp when the frame is swapped (i.e. when the rendering caused by
  62. // input event actually takes effect).
  63. INPUT_EVENT_LATENCY_FRAME_SWAP_COMPONENT,
  64. LATENCY_COMPONENT_TYPE_LAST = INPUT_EVENT_LATENCY_FRAME_SWAP_COMPONENT,
  65. };
  66. enum class SourceEventType {
  67. UNKNOWN,
  68. WHEEL,
  69. MOUSE,
  70. TOUCH,
  71. INERTIAL,
  72. KEY_PRESS,
  73. // TODO(crbug.com/868056) Touchpad scrolling latency report as WHEEL.
  74. TOUCHPAD,
  75. SCROLLBAR,
  76. OTHER,
  77. LAST = OTHER,
  78. };
  79. class LatencyInfo {
  80. public:
  81. // Map a Latency Component (with a component-specific int64_t id) to a
  82. // timestamp.
  83. using LatencyMap = base::flat_map<LatencyComponentType, base::TimeTicks>;
  84. LatencyInfo();
  85. LatencyInfo(const LatencyInfo& other);
  86. LatencyInfo(LatencyInfo&& other);
  87. LatencyInfo(SourceEventType type);
  88. ~LatencyInfo();
  89. // For test only.
  90. LatencyInfo(int64_t trace_id, bool terminated);
  91. LatencyInfo& operator=(const LatencyInfo& other);
  92. // Returns true if the vector |latency_info| is valid. Returns false
  93. // if it is not valid and log the |referring_msg|.
  94. // This function is mainly used to check the latency_info vector that
  95. // is passed between processes using IPC message has reasonable size
  96. // so that we are confident the IPC message is not corrupted/compromised.
  97. // This check will go away once the IPC system has better built-in scheme
  98. // for corruption/compromise detection.
  99. static bool Verify(const std::vector<LatencyInfo>& latency_info,
  100. const char* referring_msg);
  101. // Adds trace flow events only to LatencyInfos that are being traced.
  102. static void TraceIntermediateFlowEvents(
  103. const std::vector<LatencyInfo>& latency_info,
  104. perfetto::protos::pbzero::ChromeLatencyInfo::Step step);
  105. // Add timestamps for components that are in |other| but not in |this|.
  106. void AddNewLatencyFrom(const LatencyInfo& other);
  107. // Modifies the current sequence number for a component, and adds a new
  108. // sequence number with the current timestamp.
  109. void AddLatencyNumber(LatencyComponentType component);
  110. // Similar to |AddLatencyNumber|, and also appends |trace_name_str| to
  111. // the trace event's name.
  112. // This function should only be called when adding a BEGIN component.
  113. void AddLatencyNumberWithTraceName(LatencyComponentType component,
  114. const char* trace_name_str);
  115. // Modifies the current sequence number and adds a certain number of events
  116. // for a specific component.
  117. void AddLatencyNumberWithTimestamp(LatencyComponentType component,
  118. base::TimeTicks time);
  119. // Returns true if a component with |type| is found in the latency component.
  120. // The first such component (when iterating over latency_components_) is
  121. // stored to |output| if |output| is not NULL. Returns false if no such
  122. // component is found.
  123. bool FindLatency(LatencyComponentType type, base::TimeTicks* output) const;
  124. void Terminate();
  125. const LatencyMap& latency_components() const { return latency_components_; }
  126. const SourceEventType& source_event_type() const {
  127. return source_event_type_;
  128. }
  129. void set_source_event_type(SourceEventType type) {
  130. source_event_type_ = type;
  131. }
  132. bool began() const { return began_; }
  133. bool terminated() const { return terminated_; }
  134. void set_coalesced() { coalesced_ = true; }
  135. bool coalesced() const { return coalesced_; }
  136. int64_t trace_id() const { return trace_id_; }
  137. void set_trace_id(int64_t trace_id) { trace_id_ = trace_id; }
  138. ukm::SourceId ukm_source_id() const { return ukm_source_id_; }
  139. void set_ukm_source_id(ukm::SourceId id) { ukm_source_id_ = id; }
  140. int64_t gesture_scroll_id() const { return gesture_scroll_id_; }
  141. void set_gesture_scroll_id(int64_t id) { gesture_scroll_id_ = id; }
  142. int64_t touch_trace_id() const { return touch_trace_id_; }
  143. void set_touch_trace_id(int64_t id) { touch_trace_id_ = id; }
  144. private:
  145. void AddLatencyNumberWithTimestampImpl(LatencyComponentType component,
  146. base::TimeTicks time,
  147. const char* trace_name_str);
  148. LatencyMap latency_components_;
  149. // The unique id for matching the ASYNC_BEGIN/END trace event.
  150. int64_t trace_id_ = -1;
  151. // UKM Source id to be used for recording UKM metrics associated with this
  152. // event.
  153. ukm::SourceId ukm_source_id_ = ukm::kInvalidSourceId;
  154. // Whether this event has been coalesced into another event.
  155. bool coalesced_ = false;
  156. // Whether a begin component has been added.
  157. bool began_ = false;
  158. // Whether a terminal component has been added.
  159. bool terminated_ = false;
  160. // Stores the type of the first source event.
  161. SourceEventType source_event_type_ = SourceEventType::UNKNOWN;
  162. // The unique id for denoting a scroll gesture. This is only set for
  163. // GestureScrollBegin, GestureScrollUpdate, and GestureScrollEnd events, and
  164. // allows easy grouping of these global async events into a single logical
  165. // scroll in the sql interface of TBMv3 (Trace Based Metrics v3). As a current
  166. // implementation detail this unique id comes from the |trace_id| of the
  167. // associated GestureScrollBegin (-1 if there was none or it wasn't valid).
  168. int64_t gesture_scroll_id_ = 0;
  169. // The unique id for denoting a touch, tracking from TouchStart through
  170. // TouchMoves to TouchEnd. Used for TBMv3 metrics as in the same way as
  171. // gesture_scroll_id_.
  172. int64_t touch_trace_id_ = 0;
  173. #if !BUILDFLAG(IS_IOS)
  174. friend struct IPC::ParamTraits<ui::LatencyInfo>;
  175. friend struct mojo::StructTraits<ui::mojom::LatencyInfoDataView,
  176. ui::LatencyInfo>;
  177. #endif
  178. };
  179. // This is declared here for use in gtest-based unit tests, but is defined in
  180. // //ui/latency:test_support target.
  181. // Without this the default PrintTo template in gtest tries to pass LatencyInfo
  182. // by value, which leads to an alignment compile error on Windows.
  183. void PrintTo(const LatencyInfo& latency, ::std::ostream* os);
  184. } // namespace ui
  185. #endif // UI_LATENCY_LATENCY_INFO_H_