Merge "Improve clustering of restricted_location for mojo pipes."
diff --git a/src/trace_processor/metrics/sql/chrome/gesture_flow_event.sql b/src/trace_processor/metrics/sql/chrome/gesture_flow_event.sql
index b9b54c2..1276b76 100644
--- a/src/trace_processor/metrics/sql/chrome/gesture_flow_event.sql
+++ b/src/trace_processor/metrics/sql/chrome/gesture_flow_event.sql
@@ -69,7 +69,8 @@
         track_id AS gesture_track_id,
         trace_id AS {{id_field}},
         jank,
-        {{id_field}}
+        {{id_field}},
+        avg_vsync_interval
       FROM {{prefix}}_jank
   ) gesture ON
     flow.trace_id = gesture.{{id_field}}
@@ -96,7 +97,8 @@
     track_id AS gesture_track_id,
     trace_id AS {{id_field}},
     jank,
-    {{id_field}}
+    {{id_field}},
+    avg_vsync_interval
   FROM {{prefix}}_jank
   ORDER BY {{id_field}} ASC, trace_id ASC, ts ASC;
 
@@ -183,6 +185,7 @@
     curr.track_id,
     curr.trace_id,
     curr.{{id_field}},
+    curr.avg_vsync_interval,
     curr.gesture_slice_id,
     curr.gesture_ts,
     curr.{{prefix}}_dur,
@@ -230,6 +233,7 @@
     curr.dur,
     curr.track_id,
     curr.{{id_field}},
+    curr.avg_vsync_interval,
     curr.gesture_slice_id AS {{prefix}}_slice_id,
     curr.gesture_ts AS {{prefix}}_ts,
     curr.{{prefix}}_dur AS {{prefix}}_dur,
diff --git a/src/trace_processor/metrics/sql/chrome/gesture_flow_event_queuing_delay.sql b/src/trace_processor/metrics/sql/chrome/gesture_flow_event_queuing_delay.sql
index 6701158..e2c069c 100644
--- a/src/trace_processor/metrics/sql/chrome/gesture_flow_event_queuing_delay.sql
+++ b/src/trace_processor/metrics/sql/chrome/gesture_flow_event_queuing_delay.sql
@@ -35,6 +35,7 @@
     dur,
     track_id,
     {{id_field}},
+    avg_vsync_interval,
     {{prefix}}_slice_id,
     {{prefix}}_ts,
     {{prefix}}_dur,
diff --git a/src/trace_processor/metrics/sql/chrome/gesture_jank.sql b/src/trace_processor/metrics/sql/chrome/gesture_jank.sql
index d025622..cfbc96b 100644
--- a/src/trace_processor/metrics/sql/chrome/gesture_jank.sql
+++ b/src/trace_processor/metrics/sql/chrome/gesture_jank.sql
@@ -170,7 +170,8 @@
     dur,
     track_id,
     trace_id,
-    dur/avg_vsync_interval AS gesture_frames_exact
+    dur/avg_vsync_interval AS gesture_frames_exact,
+    avg_vsync_interval
   FROM joined_{{prefix}}_begin_and_end begin_and_end JOIN gesture_update ON
   gesture_update.ts <= begin_and_end.end_ts AND
   gesture_update.ts >= begin_and_end.begin_ts AND
diff --git a/src/trace_processor/metrics/sql/chrome/scroll_jank_cause_queuing_delay.sql b/src/trace_processor/metrics/sql/chrome/scroll_jank_cause_queuing_delay.sql
index 23a9908..35220d9 100644
--- a/src/trace_processor/metrics/sql/chrome/scroll_jank_cause_queuing_delay.sql
+++ b/src/trace_processor/metrics/sql/chrome/scroll_jank_cause_queuing_delay.sql
@@ -79,6 +79,7 @@
     EXTRACT_ARG(slice.arg_set_id, "task.posted_from.function_name") as function,
     trace_id,
     queuing_time_ns,
+    avg_vsync_interval,
     next_track_id,
     CASE WHEN queuing.ancestor_end <= slice.ts THEN
       CASE WHEN slice.ts + slice.dur <= queuing.maybe_next_ancestor_ts THEN
@@ -116,24 +117,23 @@
 -- descendant slice. So all fields in base.* will be repeated ONCE for each
 -- child, but if it has no slice it will occur only once but all the
 -- |descendant_.*| fields will be NULL because of the LEFT JOIN.
--- Additionally for mojo events, append "(interface_name)" to the end of the
--- descendant name.
+-- Additionally for mojo events we replace the descendant_name with just the
+-- "interface_name" since that is more descriptive for our jank purposes.
 DROP VIEW IF EXISTS all_descendant_blocking_tasks_queuing_delay;
 CREATE VIEW all_descendant_blocking_tasks_queuing_delay AS
   SELECT
     descendant.id AS descendant_id,
     descendant.ts AS descendant_ts,
     descendant.dur AS descendant_dur,
-    COALESCE(descendant.name || "(" ||
+    COALESCE(
       IIF(descendant.arg_set_id IS NOT NULL,
           EXTRACT_ARG(descendant.arg_set_id,
               "chrome_mojo_event_info.watcher_notify_interface_tag"),
-          NULL) || ")",
-      descendant.name || "(" ||
-          IIF(descendant.arg_set_id IS NOT NULL,
+          NULL),
+      IIF(descendant.arg_set_id IS NOT NULL,
           EXTRACT_ARG(descendant.arg_set_id,
               "chrome_mojo_event_info.mojo_interface_tag"),
-          NULL) || ")",
+          NULL),
       descendant.name) AS descendant_name,
     descendant.parent_id As descendant_parent_id,
     descendant.depth AS descendant_depth,
@@ -244,6 +244,7 @@
     thread_name,
     process_name,
     function,
+    avg_vsync_interval,
     GROUP_CONCAT(
       CASE WHEN descendant_depth < invalid_depth OR descendant_major_slice THEN
         descendant_id
@@ -310,7 +311,7 @@
     , "-") AS java_name
   FROM
     blocking_tasks_queuing_delay_with_invalid_depth
-  GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
+  GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
   ORDER BY descendant_cpu_percentage DESC;
 
 
@@ -351,20 +352,46 @@
       END'
 );
 
+SELECT CREATE_FUNCTION(
+  -- Function prototype: Takes a slice name, function, and file, and determines
+  -- if we should use the slice name, or if its a RunTask event uses the
+  -- function & file name, however if the RunTask posted from is one of the
+  -- simple_watcher paths we collapse them for attributation.
+  'TopLevelName(name STRING, function STRING, file STRING)',
+  'STRING',
+  -- The difference for the mojom functions are:
+  --  1) PostDispatchNextMessageFromPipe:
+  --         We knew that there is a message in the pipe, didn't try to set up a
+  --         SimpleWatcher to monitor when a new one arrives.
+  --  2) ArmOrNotify:
+  --         We tried to set up SimpleWatcher, but the setup failed as the
+  --         message arrived as we were setting this up, so we posted a task
+  --         instead.
+  --  3) Notify:
+  --         SimpleWatcher was set up and after a period of monitoring detected
+  --         a new message.
+  -- For our jank use case this distinction isn't very useful so we group them
+  -- together.
+  'SELECT
+     CASE WHEN $name = "ThreadControllerImpl::RunTask" THEN
+       CASE WHEN $function IN
+           ("PostDispatchNextMessageFromPipe", "ArmOrNotify", "Notify") THEN
+         "posted-from-mojo-pipe"
+        ELSE
+         "posted-from-" || $function || "()-in-" || $file
+        END
+    ELSE
+      $name
+    END'
+);
+
 -- Create a common name for each "cause" based on the slice stack we found.
 DROP VIEW IF EXISTS scroll_jank_cause_queuing_delay_temp;
 CREATE VIEW scroll_jank_cause_queuing_delay_temp AS
   SELECT
-    CASE WHEN name = "ThreadControllerImpl::RunTask" THEN
-      'posted-from-' || function || '()-in-' || file
-    ELSE
-      name
-    END || COALESCE("-" || descendant_name, "") AS location,
-    CASE WHEN name = "ThreadControllerImpl::RunTask" THEN
-      'posted-from-' || function || '()-in-' || file
-    ELSE
-      name
-    END || COALESCE(
+    TopLevelName(name, function, file) || COALESCE(
+      "-" || descendant_name, "") AS location,
+    TopLevelName(name, function, file) || COALESCE(
       "-" || GetFirstSliceNameOrNull(mojom_name),
       "-" || GetFirstSliceNameOrNull(toplevel_name),
       "-" || GetFirstSliceNameOrNull(java_name),
diff --git a/test/trace_processor/chrome/scroll_jank_mojo_simple_watcher.out b/test/trace_processor/chrome/scroll_jank_mojo_simple_watcher.out
index 13709b3..207e7d2 100644
--- a/test/trace_processor/chrome/scroll_jank_mojo_simple_watcher.out
+++ b/test/trace_processor/chrome/scroll_jank_mojo_simple_watcher.out
@@ -2,4 +2,4 @@
 "trace_id","jank","dur_overlapping_ns","metric_name"
 34577,0,6000000,"InputLatency.LatencyInfo.Flow.QueuingDelay.NoJank.BlockingTasksUs.task-subtask"
 34578,1,29000000,"InputLatency.LatencyInfo.Flow.QueuingDelay.Jank.BlockingTasksUs.task-subtask"
-34579,0,29000000,"InputLatency.LatencyInfo.Flow.QueuingDelay.NoJank.BlockingTasksUs.task-subtask(foo)"
+34579,0,29000000,"InputLatency.LatencyInfo.Flow.QueuingDelay.NoJank.BlockingTasksUs.task-foo"