[go: nahoru, domu]

Typed LatencyInfo.Flow events

Corresponding proto messages have been added in aosp/1215194,
aosp/1229899, and aosp/1234331.

Typed trace events will allow inclusion of the event arguments in slow
reports data in the future. The trace format is protocol-buffer based
under the hood already (see docs.perfetto.dev for details), this patch
adds strong typing for the arguments of the respective event(s) only.

Bug: 148777631
Change-Id: I77184a5ee9bbefad57aff9e4c5addd4d1321adcb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2049974
Reviewed-by: Jochen Eisinger <jochen@chromium.org>
Reviewed-by: Sadrul Chowdhury <sadrul@chromium.org>
Reviewed-by: Eric Seckler <eseckler@chromium.org>
Commit-Queue: Andrew Shulaev <ddrone@google.com>
Cr-Commit-Position: refs/heads/master@{#746780}
diff --git a/cc/BUILD.gn b/cc/BUILD.gn
index 146f433..e084b3f 100644
--- a/cc/BUILD.gn
+++ b/cc/BUILD.gn
@@ -383,7 +383,6 @@
     "//cc/base",
     "//cc/paint",
     "//components/viz/common",
-    "//services/tracing/public/cpp:cpp",
     "//skia",
   ]
   deps = [
@@ -399,6 +398,7 @@
     "//mojo/public/cpp/bindings:struct_traits",
     "//services/metrics/public/cpp:ukm_builders",
     "//services/metrics/public/mojom",
+    "//services/tracing/public/cpp:cpp",
     "//ui/events:events_base",
     "//ui/gfx",
     "//ui/gfx/geometry",
diff --git a/cc/trees/latency_info_swap_promise.cc b/cc/trees/latency_info_swap_promise.cc
index 3f96946..2edc793 100644
--- a/cc/trees/latency_info_swap_promise.cc
+++ b/cc/trees/latency_info_swap_promise.cc
@@ -8,6 +8,8 @@
 
 #include "base/logging.h"
 #include "base/trace_event/trace_event.h"
+#include "services/tracing/public/cpp/perfetto/flow_event_utils.h"
+#include "services/tracing/public/cpp/perfetto/macros.h"
 
 namespace cc {
 
@@ -38,10 +40,19 @@
 
 // Trace the original LatencyInfo of a LatencyInfoSwapPromise
 void LatencyInfoSwapPromise::OnCommit() {
-  TRACE_EVENT_WITH_FLOW1("input,benchmark", "LatencyInfo.Flow",
-                         TRACE_ID_GLOBAL(TraceId()),
-                         TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT,
-                         "step", "HandleInputEventMainCommit");
+  using perfetto::protos::pbzero::ChromeLatencyInfo;
+  using perfetto::protos::pbzero::TrackEvent;
+
+  TRACE_EVENT("input,benchmark", "LatencyInfo.Flow",
+              [this](perfetto::EventContext ctx) {
+                ChromeLatencyInfo* latency_info =
+                    ctx.event()->set_chrome_latency_info();
+                latency_info->set_trace_id(TraceId());
+                latency_info->set_step(
+                    ChromeLatencyInfo::STEP_HANDLE_INPUT_EVENT_MAIN_COMMIT);
+                tracing::FillFlowEvent(ctx, TrackEvent::LegacyEvent::FLOW_INOUT,
+                                       TraceId());
+              });
 }
 
 }  // namespace cc
diff --git a/cc/trees/layer_tree_host.cc b/cc/trees/layer_tree_host.cc
index 13573155..584aff4 100644
--- a/cc/trees/layer_tree_host.cc
+++ b/cc/trees/layer_tree_host.cc
@@ -61,6 +61,8 @@
 #include "cc/trees/tree_synchronizer.h"
 #include "cc/trees/ukm_manager.h"
 #include "services/metrics/public/cpp/ukm_recorder.h"
+#include "services/tracing/public/cpp/perfetto/flow_event_utils.h"
+#include "services/tracing/public/cpp/perfetto/macros.h"
 #include "ui/gfx/geometry/size_conversions.h"
 #include "ui/gfx/geometry/vector2d_conversions.h"
 #include "ui/gfx/presentation_feedback.h"
@@ -943,11 +945,20 @@
 void LayerTreeHost::ApplyScrollAndScale(ScrollAndScaleSet* info) {
   DCHECK(info);
   TRACE_EVENT0("cc", "LayerTreeHost::ApplyScrollAndScale");
+
+  using perfetto::protos::pbzero::ChromeLatencyInfo;
+  using perfetto::protos::pbzero::TrackEvent;
+
   for (auto& swap_promise : info->swap_promises) {
-    TRACE_EVENT_WITH_FLOW1("input,benchmark", "LatencyInfo.Flow",
-                           TRACE_ID_GLOBAL(swap_promise->TraceId()),
-                           TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT,
-                           "step", "Main thread scroll update");
+    TRACE_EVENT(
+        "input,benchmark", "LatencyInfo.Flow",
+        [&swap_promise](perfetto::EventContext ctx) {
+          ChromeLatencyInfo* info = ctx.event()->set_chrome_latency_info();
+          info->set_trace_id(swap_promise->TraceId());
+          info->set_step(ChromeLatencyInfo::STEP_MAIN_THREAD_SCROLL_UPDATE);
+          tracing::FillFlowEvent(ctx, TrackEvent::LegacyEvent::FLOW_INOUT,
+                                 swap_promise->TraceId());
+        });
     swap_promise_manager_.QueueSwapPromise(std::move(swap_promise));
   }
 
diff --git a/cc/trees/layer_tree_host_impl.cc b/cc/trees/layer_tree_host_impl.cc
index 35a18b6..5f1693c 100644
--- a/cc/trees/layer_tree_host_impl.cc
+++ b/cc/trees/layer_tree_host_impl.cc
@@ -112,6 +112,7 @@
 #include "gpu/command_buffer/client/shared_image_interface.h"
 #include "gpu/command_buffer/common/shared_image_usage.h"
 #include "services/metrics/public/cpp/ukm_recorder.h"
+#include "third_party/perfetto/protos/perfetto/trace/track_event/chrome_latency_info.pbzero.h"
 #include "third_party/skia/include/gpu/GrContext.h"
 #include "ui/gfx/display_color_spaces.h"
 #include "ui/gfx/geometry/point_conversions.h"
@@ -2471,8 +2472,9 @@
           ui::INPUT_EVENT_LATENCY_RENDERER_SWAP_COMPONENT, draw_time);
     }
   }
-  ui::LatencyInfo::TraceIntermediateFlowEvents(metadata.latency_info,
-                                               "SwapBuffers");
+  ui::LatencyInfo::TraceIntermediateFlowEvents(
+      metadata.latency_info,
+      perfetto::protos::pbzero::ChromeLatencyInfo::STEP_SWAP_BUFFERS);
 
   // Collect all resource ids in the render passes into a single array.
   std::vector<viz::ResourceId> resources;
diff --git a/components/viz/service/BUILD.gn b/components/viz/service/BUILD.gn
index 5cd92d7..6cd9edd 100644
--- a/components/viz/service/BUILD.gn
+++ b/components/viz/service/BUILD.gn
@@ -196,6 +196,7 @@
     "//media",
     "//media/capture:capture_lib",
     "//media/mojo/services",
+    "//services/tracing/public/cpp:cpp",
     "//services/viz/privileged/mojom",
     "//skia:skcms",
     "//ui/display/types",
diff --git a/components/viz/service/display/DEPS b/components/viz/service/display/DEPS
index 23bfdee..96ec1f3e 100644
--- a/components/viz/service/display/DEPS
+++ b/components/viz/service/display/DEPS
@@ -25,6 +25,7 @@
   "+skia",
   "+third_party/khronos",
   "+third_party/skia",
+  "+third_party/perfetto/protos/perfetto/trace/track_event",
   "+ui/latency",
   "+ui/gfx/video_types.h",
   "+ui/gl/android/android_surface_control_compat.h",
diff --git a/components/viz/service/display/display.cc b/components/viz/service/display/display.cc
index ced4a1d..3e81e49 100644
--- a/components/viz/service/display/display.cc
+++ b/components/viz/service/display/display.cc
@@ -38,6 +38,7 @@
 #include "gpu/command_buffer/client/gles2_interface.h"
 #include "gpu/ipc/scheduler_sequence.h"
 #include "services/viz/public/mojom/compositing/compositor_frame_sink.mojom.h"
+#include "third_party/perfetto/protos/perfetto/trace/track_event/chrome_latency_info.pbzero.h"
 #include "ui/gfx/buffer_types.h"
 #include "ui/gfx/geometry/rect_conversions.h"
 #include "ui/gfx/overlay_transform_utils.h"
@@ -688,8 +689,9 @@
                                  swapped_trace_id_, "WaitForSwap");
     swapped_since_resize_ = true;
 
-    ui::LatencyInfo::TraceIntermediateFlowEvents(frame.metadata.latency_info,
-                                                 "Display::DrawAndSwap");
+    ui::LatencyInfo::TraceIntermediateFlowEvents(
+        frame.metadata.latency_info,
+        perfetto::protos::pbzero::ChromeLatencyInfo::STEP_DRAW_AND_SWAP);
 
     cc::benchmark_instrumentation::IssueDisplayRenderingStatsEvent();
     DirectRenderer::SwapFrameData swap_frame_data;
diff --git a/content/browser/renderer_host/input/input_router_impl.cc b/content/browser/renderer_host/input/input_router_impl.cc
index 3606a3b..4687453 100644
--- a/content/browser/renderer_host/input/input_router_impl.cc
+++ b/content/browser/renderer_host/input/input_router_impl.cc
@@ -26,6 +26,7 @@
 #include "content/public/common/content_switches.h"
 #include "content/public/common/input_event_ack_state.h"
 #include "ipc/ipc_sender.h"
+#include "services/tracing/public/cpp/perfetto/flow_event_utils.h"
 #include "ui/events/blink/blink_event_util.h"
 #include "ui/events/blink/blink_features.h"
 #include "ui/events/blink/web_input_event_traits.h"
@@ -40,6 +41,8 @@
 using blink::WebMouseEvent;
 using blink::WebMouseWheelEvent;
 using blink::WebTouchEvent;
+using perfetto::protos::pbzero::ChromeLatencyInfo;
+using perfetto::protos::pbzero::TrackEvent;
 using ui::WebInputEventTraits;
 
 namespace {
@@ -514,11 +517,20 @@
     mojom::WidgetInputHandler::DispatchEventCallback callback) {
   TRACE_EVENT1("input", "InputRouterImpl::FilterAndSendWebInputEvent", "type",
                WebInputEvent::GetName(input_event.GetType()));
-  TRACE_EVENT_WITH_FLOW2(
-      "input,benchmark,devtools.timeline", "LatencyInfo.Flow",
-      TRACE_ID_GLOBAL(latency_info.trace_id()),
-      TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT, "step",
-      "SendInputEventUI", "frameTreeNodeId", frame_tree_node_id_);
+  TRACE_EVENT("input,benchmark,devtools.timeline", "LatencyInfo.Flow",
+              [&latency_info, this](perfetto::EventContext ctx) {
+                ChromeLatencyInfo* info =
+                    ctx.event()->set_chrome_latency_info();
+                info->set_trace_id(latency_info.trace_id());
+                info->set_step(ChromeLatencyInfo::STEP_SEND_INPUT_EVENT_UI);
+                info->set_frame_tree_node_id(frame_tree_node_id_);
+
+                tracing::FillFlowEvent(
+                    ctx,
+                    perfetto::protos::pbzero::
+                        TrackEvent_LegacyEvent_FlowDirection_FLOW_INOUT,
+                    latency_info.trace_id());
+              });
 
   output_stream_validator_.Validate(input_event);
   InputEventAckState filtered_state =
diff --git a/content/renderer/input/render_widget_input_handler.cc b/content/renderer/input/render_widget_input_handler.cc
index dd0de3f..7db490c 100644
--- a/content/renderer/input/render_widget_input_handler.cc
+++ b/content/renderer/input/render_widget_input_handler.cc
@@ -25,6 +25,8 @@
 #include "content/renderer/render_frame_proxy.h"
 #include "content/renderer/render_thread_impl.h"
 #include "content/renderer/render_widget.h"
+#include "services/tracing/public/cpp/perfetto/flow_event_utils.h"
+#include "services/tracing/public/cpp/perfetto/macros.h"
 #include "third_party/blink/public/common/input/web_gesture_device.h"
 #include "third_party/blink/public/common/input/web_gesture_event.h"
 #include "third_party/blink/public/common/input/web_keyboard_event.h"
@@ -56,6 +58,8 @@
 using blink::WebPointerEvent;
 using blink::WebTouchEvent;
 using blink::WebTouchPoint;
+using perfetto::protos::pbzero::ChromeLatencyInfo;
+using perfetto::protos::pbzero::TrackEvent;
 using ui::DidOverscrollParams;
 
 namespace content {
@@ -351,10 +355,15 @@
   TRACE_EVENT1("renderer,benchmark,rail",
                "RenderWidgetInputHandler::OnHandleInputEvent", "event",
                WebInputEvent::GetName(input_event.GetType()));
-  TRACE_EVENT_WITH_FLOW1("input,benchmark", "LatencyInfo.Flow",
-                         TRACE_ID_GLOBAL(latency_info.trace_id()),
-                         TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT,
-                         "step", "HandleInputEventMain");
+  TRACE_EVENT("input,benchmark", "LatencyInfo.Flow",
+              [&latency_info](perfetto::EventContext ctx) {
+                ChromeLatencyInfo* info =
+                    ctx.event()->set_chrome_latency_info();
+                info->set_trace_id(latency_info.trace_id());
+                info->set_step(ChromeLatencyInfo::STEP_HANDLE_INPUT_EVENT_MAIN);
+                tracing::FillFlowEvent(ctx, TrackEvent::LegacyEvent::FLOW_INOUT,
+                                       latency_info.trace_id());
+              });
 
   // If we don't have a high res timer, these metrics won't be accurate enough
   // to be worth collecting. Note that this does introduce some sampling bias.
diff --git a/remoting/host/input_monitor/BUILD.gn b/remoting/host/input_monitor/BUILD.gn
index 3fde2d1..c25d0c5 100644
--- a/remoting/host/input_monitor/BUILD.gn
+++ b/remoting/host/input_monitor/BUILD.gn
@@ -31,6 +31,7 @@
   deps = [
     "//remoting/proto",
     "//third_party/webrtc_overrides:webrtc_component",
+    "//ui/events",
   ]
 
   if (use_ozone) {
diff --git a/services/tracing/BUILD.gn b/services/tracing/BUILD.gn
index 101b8c61..8a4a066 100644
--- a/services/tracing/BUILD.gn
+++ b/services/tracing/BUILD.gn
@@ -47,7 +47,6 @@
   deps = [
     "//base",
     "//third_party/perfetto:libperfetto",
-    "//third_party/perfetto/src/protozero:protozero",
   ]
 }
 
diff --git a/services/tracing/public/cpp/BUILD.gn b/services/tracing/public/cpp/BUILD.gn
index 4e333ca..f1e605d 100644
--- a/services/tracing/public/cpp/BUILD.gn
+++ b/services/tracing/public/cpp/BUILD.gn
@@ -56,6 +56,8 @@
       "base_agent.h",
       "perfetto/dummy_producer.cc",
       "perfetto/dummy_producer.h",
+      "perfetto/flow_event_utils.cc",
+      "perfetto/flow_event_utils.h",
       "perfetto/interning_index.h",
       "perfetto/java_heap_profiler/hprof_buffer_android.cc",
       "perfetto/java_heap_profiler/hprof_buffer_android.h",
diff --git a/services/tracing/public/cpp/perfetto/flow_event_utils.cc b/services/tracing/public/cpp/perfetto/flow_event_utils.cc
new file mode 100644
index 0000000..055019ca
--- /dev/null
+++ b/services/tracing/public/cpp/perfetto/flow_event_utils.cc
@@ -0,0 +1,27 @@
+// Copyright 2020 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "services/tracing/public/cpp/perfetto/flow_event_utils.h"
+
+namespace tracing {
+
+// Fill the information about flow event in EventContext.
+//
+// BEWARE: this function currently sets the TrackEvent's LegacyEvent field, and
+// thus should be used from within trace macros that do not set the LegacyEvent
+// field themselves. As it is, it is fine to call this method from the typed
+// TRACE_EVENT macro.
+//
+// TODO(b/TODO): Change to the new model flow events when finalized
+void FillFlowEvent(
+    const perfetto::EventContext& ctx,
+    perfetto::protos::pbzero::TrackEvent_LegacyEvent_FlowDirection direction,
+    uint64_t bind_id) {
+  perfetto::protos::pbzero::TrackEvent_LegacyEvent* legacy_event =
+      ctx.event()->set_legacy_event();
+  legacy_event->set_flow_direction(direction);
+  legacy_event->set_bind_id(bind_id);
+}
+
+}  // namespace tracing
\ No newline at end of file
diff --git a/services/tracing/public/cpp/perfetto/flow_event_utils.h b/services/tracing/public/cpp/perfetto/flow_event_utils.h
new file mode 100644
index 0000000..de5694d
--- /dev/null
+++ b/services/tracing/public/cpp/perfetto/flow_event_utils.h
@@ -0,0 +1,30 @@
+// Copyright 2020 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SERVICES_TRACING_PUBLIC_CPP_PERFETTO_FLOW_EVENT_UTILS_H_
+#define SERVICES_TRACING_PUBLIC_CPP_PERFETTO_FLOW_EVENT_UTILS_H_
+
+#include "base/component_export.h"
+#include "services/tracing/public/cpp/perfetto/macros.h"
+#include "third_party/perfetto/protos/perfetto/trace/track_event/chrome_latency_info.pbzero.h"
+#include "third_party/perfetto/protos/perfetto/trace/track_event/track_event.pbzero.h"
+
+namespace tracing {
+
+// Fill the information about flow event in EventContext.
+//
+// BEWARE: this function currently sets the TrackEvent's LegacyEvent field, and
+// thus should be used from within trace macros that do not set the LegacyEvent
+// field themselves. As it is, it is fine to call this method from the typed
+// TRACE_EVENT macro.
+//
+// TODO(b/147673438): Change to the new model flow events when finalized
+void COMPONENT_EXPORT(TRACING_CPP) FillFlowEvent(
+    const perfetto::EventContext&,
+    perfetto::protos::pbzero::TrackEvent_LegacyEvent_FlowDirection,
+    uint64_t bind_id);
+
+}  // namespace tracing
+
+#endif  // SERVICES_TRACING_PUBLIC_CPP_PERFETTO_FLOW_EVENT_UTILS_H_
diff --git a/services/tracing/public/cpp/perfetto/macros.h b/services/tracing/public/cpp/perfetto/macros.h
index 935fdbd..3d7b473 100644
--- a/services/tracing/public/cpp/perfetto/macros.h
+++ b/services/tracing/public/cpp/perfetto/macros.h
@@ -43,6 +43,8 @@
 //           auto* event = ctx.event();
 //           // Fill in some field in track_event.
 //       });
+//
+// When lambda is passed as an argument, it is executed synchronously.
 #define TRACE_EVENT_BEGIN(category, name, ...)                              \
   TRACING_INTERNAL_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_BEGIN, category, name, \
                                    TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__)
@@ -55,6 +57,9 @@
 
 // Begin a thread-scoped slice which gets automatically closed when going out
 // of scope.
+//
+// Similarly to TRACE_EVENT_BEGIN, when lambda is passed as an argument, it is
+// executed synchronously.
 #define TRACE_EVENT(category, name, ...) \
   TRACING_INTERNAL_SCOPED_ADD_TRACE_EVENT(category, name, ##__VA_ARGS__)
 
diff --git a/ui/events/blink/BUILD.gn b/ui/events/blink/BUILD.gn
index 2b8760e..b4c7d56 100644
--- a/ui/events/blink/BUILD.gn
+++ b/ui/events/blink/BUILD.gn
@@ -72,6 +72,7 @@
 
   deps = [
     "//cc:cc",
+    "//services/tracing/public/cpp:cpp",
     "//third_party/blink/public:blink_headers",
     "//third_party/one_euro_filter",
     "//ui/base:base",
diff --git a/ui/events/blink/DEPS b/ui/events/blink/DEPS
index 2f904b2..0a508d0 100644
--- a/ui/events/blink/DEPS
+++ b/ui/events/blink/DEPS
@@ -21,4 +21,6 @@
   "+ui/gfx/geometry",
 
   "+third_party/one_euro_filter/src/one_euro_filter.h",
+
+  "+services/tracing/public/cpp",
 ]
diff --git a/ui/events/blink/input_handler_proxy.cc b/ui/events/blink/input_handler_proxy.cc
index 0956f07..0ba5c5b1 100644
--- a/ui/events/blink/input_handler_proxy.cc
+++ b/ui/events/blink/input_handler_proxy.cc
@@ -22,6 +22,8 @@
 #include "base/trace_event/trace_event.h"
 #include "build/build_config.h"
 #include "cc/input/main_thread_scrolling_reason.h"
+#include "services/tracing/public/cpp/perfetto/flow_event_utils.h"
+#include "services/tracing/public/cpp/perfetto/macros.h"
 #include "third_party/blink/public/common/input/web_input_event.h"
 #include "third_party/blink/public/common/input/web_mouse_wheel_event.h"
 #include "third_party/blink/public/common/input/web_touch_event.h"
@@ -43,6 +45,8 @@
 using blink::WebMouseWheelEvent;
 using blink::WebTouchEvent;
 using blink::WebTouchPoint;
+using perfetto::protos::pbzero::ChromeLatencyInfo;
+using perfetto::protos::pbzero::TrackEvent;
 
 namespace {
 
@@ -241,10 +245,15 @@
     EventDispositionCallback callback) {
   DCHECK(input_handler_);
 
-  TRACE_EVENT_WITH_FLOW1("input,benchmark", "LatencyInfo.Flow",
-                         TRACE_ID_GLOBAL(latency_info.trace_id()),
-                         TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT,
-                         "step", "HandleInputEventImpl");
+  TRACE_EVENT("input,benchmark", "LatencyInfo.Flow",
+              [&latency_info](perfetto::EventContext ctx) {
+                ChromeLatencyInfo* info =
+                    ctx.event()->set_chrome_latency_info();
+                info->set_trace_id(latency_info.trace_id());
+                info->set_step(ChromeLatencyInfo::STEP_HANDLE_INPUT_EVENT_IMPL);
+                tracing::FillFlowEvent(ctx, TrackEvent::LegacyEvent::FLOW_INOUT,
+                                       latency_info.trace_id());
+              });
 
   std::unique_ptr<EventWithCallback> event_with_callback =
       std::make_unique<EventWithCallback>(std::move(event), latency_info,
diff --git a/ui/latency/BUILD.gn b/ui/latency/BUILD.gn
index 13978ba..0e3639e5 100644
--- a/ui/latency/BUILD.gn
+++ b/ui/latency/BUILD.gn
@@ -18,6 +18,7 @@
 
   deps = [
     "//base",
+    "//services/tracing/public/cpp:cpp",
     "//ui/gfx",
   ]
 
diff --git a/ui/latency/DEPS b/ui/latency/DEPS
index 40d9b9f..2a39e69 100644
--- a/ui/latency/DEPS
+++ b/ui/latency/DEPS
@@ -1,5 +1,7 @@
 include_rules = [
   "+services/metrics/public/cpp",
+  "+services/tracing/public/cpp",
+  "+third_party/perfetto/protos/perfetto/trace/track_event",
   "+ui/gfx",
 ]
 
diff --git a/ui/latency/latency_info.cc b/ui/latency/latency_info.cc
index 6dca0cd..90c77e1 100644
--- a/ui/latency/latency_info.cc
+++ b/ui/latency/latency_info.cc
@@ -15,9 +15,14 @@
 #include "base/macros.h"
 #include "base/strings/stringprintf.h"
 #include "base/trace_event/trace_event.h"
+#include "services/tracing/public/cpp/perfetto/flow_event_utils.h"
+#include "services/tracing/public/cpp/perfetto/macros.h"
 
 namespace {
 
+using perfetto::protos::pbzero::ChromeLatencyInfo;
+using perfetto::protos::pbzero::TrackEvent;
+
 const size_t kMaxLatencyInfoNumber = 100;
 
 const char* GetComponentName(ui::LatencyComponentType type) {
@@ -148,14 +153,20 @@
 
 void LatencyInfo::TraceIntermediateFlowEvents(
     const std::vector<LatencyInfo>& latency_info,
-    const char* event_name) {
+    perfetto::protos::pbzero::ChromeLatencyInfo::Step step) {
   for (auto& latency : latency_info) {
     if (latency.trace_id() == -1)
       continue;
-    TRACE_EVENT_WITH_FLOW1("input,benchmark", "LatencyInfo.Flow",
-                           TRACE_ID_GLOBAL(latency.trace_id()),
-                           TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT,
-                           "step", event_name);
+
+    TRACE_EVENT(
+        "input,benchmark", "LatencyInfo.Flow",
+        [&latency, &step](perfetto::EventContext ctx) {
+          ChromeLatencyInfo* info = ctx.event()->set_chrome_latency_info();
+          info->set_step(step);
+          info->set_trace_id(latency.trace_id());
+          tracing::FillFlowEvent(ctx, TrackEvent::LegacyEvent::FLOW_INOUT,
+                                 latency.trace_id());
+        });
   }
 }
 
@@ -260,9 +271,14 @@
           TRACE_ID_GLOBAL(trace_id_), ts);
     }
 
-    TRACE_EVENT_WITH_FLOW1("input,benchmark", "LatencyInfo.Flow",
-                           TRACE_ID_GLOBAL(trace_id_),
-                           TRACE_EVENT_FLAG_FLOW_OUT, "trace_id", trace_id_);
+    TRACE_EVENT("input,benchmark", "LatencyInfo.Flow",
+                [this](perfetto::EventContext ctx) {
+                  ChromeLatencyInfo* info =
+                      ctx.event()->set_chrome_latency_info();
+                  info->set_trace_id(trace_id_);
+                  tracing::FillFlowEvent(ctx, TrackEvent::LegacyEvent::FLOW_OUT,
+                                         trace_id_);
+                });
   }
 
   auto it = latency_components_.find(component);
@@ -293,8 +309,14 @@
                                     "data", AsTraceableData());
   }
 
-  TRACE_EVENT_WITH_FLOW0("input,benchmark", "LatencyInfo.Flow",
-                         TRACE_ID_GLOBAL(trace_id_), TRACE_EVENT_FLAG_FLOW_IN);
+  TRACE_EVENT("input,benchmark", "LatencyInfo.Flow",
+              [this](perfetto::EventContext ctx) {
+                ChromeLatencyInfo* info =
+                    ctx.event()->set_chrome_latency_info();
+                info->set_trace_id(trace_id_);
+                tracing::FillFlowEvent(ctx, TrackEvent::LegacyEvent::FLOW_IN,
+                                       trace_id_);
+              });
 }
 
 void LatencyInfo::CoalesceScrollUpdateWith(const LatencyInfo& other) {
diff --git a/ui/latency/latency_info.h b/ui/latency/latency_info.h
index 24322a0d..fb1a15a 100644
--- a/ui/latency/latency_info.h
+++ b/ui/latency/latency_info.h
@@ -16,6 +16,7 @@
 #include "base/containers/flat_map.h"
 #include "base/time/time.h"
 #include "services/metrics/public/cpp/ukm_source_id.h"
+#include "third_party/perfetto/protos/perfetto/trace/track_event/chrome_latency_info.pbzero.h"
 #include "ui/gfx/geometry/point_f.h"
 
 #if !defined(OS_IOS)
@@ -128,7 +129,7 @@
   // Adds trace flow events only to LatencyInfos that are being traced.
   static void TraceIntermediateFlowEvents(
       const std::vector<LatencyInfo>& latency_info,
-      const char* trace_name);
+      perfetto::protos::pbzero::ChromeLatencyInfo::Step step);
 
   // Copy timestamp with type |type| from |other| into |this|.
   void CopyLatencyFrom(const LatencyInfo& other, LatencyComponentType type);