Merge "metrics: Add touch_flow_event_queuing_delay.sql"
diff --git a/Android.bp b/Android.bp
index 70f9c9c..0e7185e 100644
--- a/Android.bp
+++ b/Android.bp
@@ -8048,6 +8048,7 @@
     "src/trace_processor/metrics/chrome/estimated_power_by_category.sql",
     "src/trace_processor/metrics/chrome/estimated_power_by_rail_mode.sql",
     "src/trace_processor/metrics/chrome/gesture_flow_event.sql",
+    "src/trace_processor/metrics/chrome/gesture_flow_event_queuing_delay.sql",
     "src/trace_processor/metrics/chrome/gesture_jank.sql",
     "src/trace_processor/metrics/chrome/rail_modes.sql",
     "src/trace_processor/metrics/chrome/scroll_flow_event.sql",
@@ -8060,6 +8061,7 @@
     "src/trace_processor/metrics/chrome/scroll_jank_cause_queuing_delay.sql",
     "src/trace_processor/metrics/chrome/test_chrome_metric.sql",
     "src/trace_processor/metrics/chrome/touch_flow_event.sql",
+    "src/trace_processor/metrics/chrome/touch_flow_event_queuing_delay.sql",
     "src/trace_processor/metrics/chrome/touch_jank.sql",
     "src/trace_processor/metrics/experimental/blink_gc_metric.sql",
     "src/trace_processor/metrics/experimental/frame_times.sql",
diff --git a/BUILD b/BUILD
index 960d33b..59842a6 100644
--- a/BUILD
+++ b/BUILD
@@ -1072,6 +1072,7 @@
         "src/trace_processor/metrics/chrome/estimated_power_by_category.sql",
         "src/trace_processor/metrics/chrome/estimated_power_by_rail_mode.sql",
         "src/trace_processor/metrics/chrome/gesture_flow_event.sql",
+        "src/trace_processor/metrics/chrome/gesture_flow_event_queuing_delay.sql",
         "src/trace_processor/metrics/chrome/gesture_jank.sql",
         "src/trace_processor/metrics/chrome/rail_modes.sql",
         "src/trace_processor/metrics/chrome/scroll_flow_event.sql",
@@ -1084,6 +1085,7 @@
         "src/trace_processor/metrics/chrome/scroll_jank_cause_queuing_delay.sql",
         "src/trace_processor/metrics/chrome/test_chrome_metric.sql",
         "src/trace_processor/metrics/chrome/touch_flow_event.sql",
+        "src/trace_processor/metrics/chrome/touch_flow_event_queuing_delay.sql",
         "src/trace_processor/metrics/chrome/touch_jank.sql",
         "src/trace_processor/metrics/experimental/blink_gc_metric.sql",
         "src/trace_processor/metrics/experimental/frame_times.sql",
diff --git a/src/trace_processor/metrics/BUILD.gn b/src/trace_processor/metrics/BUILD.gn
index c473391..84b211b 100644
--- a/src/trace_processor/metrics/BUILD.gn
+++ b/src/trace_processor/metrics/BUILD.gn
@@ -80,6 +80,7 @@
   "chrome/estimated_power_by_rail_mode.sql",
   "chrome/gesture_jank.sql",
   "chrome/gesture_flow_event.sql",
+  "chrome/gesture_flow_event_queuing_delay.sql",
   "chrome/rail_modes.sql",
   "chrome/scroll_jank.sql",
   "chrome/scroll_jank_cause.sql",
@@ -91,6 +92,7 @@
   "chrome/scroll_flow_event_queuing_delay.sql",
   "chrome/test_chrome_metric.sql",
   "chrome/touch_flow_event.sql",
+  "chrome/touch_flow_event_queuing_delay.sql",
   "chrome/touch_jank.sql",
   "experimental/blink_gc_metric.sql",
   "experimental/frame_times.sql",
diff --git a/src/trace_processor/metrics/chrome/gesture_flow_event_queuing_delay.sql b/src/trace_processor/metrics/chrome/gesture_flow_event_queuing_delay.sql
new file mode 100644
index 0000000..6701158
--- /dev/null
+++ b/src/trace_processor/metrics/chrome/gesture_flow_event_queuing_delay.sql
@@ -0,0 +1,75 @@
+--
+-- Copyright 2021 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the 'License');
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an 'AS IS' BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- This metric takes each flow event in a InputLatency::{gesture_update} and
+-- and computes the time from the ancestor_end of the current flow to the
+-- ancestor_ts of the next flow event. This is a reasonable approximation of the
+-- time we waited for the next step in the critical flow to start.
+
+-- Provides the {{prefix}}_flow_event table which gives us all the flow events
+-- with associated {gesture_update} events we care about and labels them
+-- janky or not.
+SELECT RUN_METRIC('chrome/{{prefix}}_flow_event.sql');
+
+-- Take each flow and next flow (from {{prefix}}_flow_event table) and generate
+-- the metric name as well as compute the time between.
+DROP VIEW IF EXISTS {{prefix}}_flow_event_queuing_delay;
+
+CREATE VIEW {{prefix}}_flow_event_queuing_delay AS
+  SELECT
+    trace_id,
+    id,
+    ts,
+    dur,
+    track_id,
+    {{id_field}},
+    {{prefix}}_slice_id,
+    {{prefix}}_ts,
+    {{prefix}}_dur,
+    {{prefix}}_track_id,
+    jank,
+    step,
+    ancestor_id,
+    ancestor_ts,
+    ancestor_end,
+    next_id,
+    next_step,
+    maybe_next_ancestor_ts,
+    next_track_id,
+      CASE WHEN trace_id = next_trace_id THEN
+      'InputLatency.LatencyInfo.Flow.QueuingDelay.' ||
+      CASE WHEN
+        jank IS NOT NULL AND
+        jank = 1
+      THEN
+          'Jank.'
+      ELSE
+          'NoJank.'
+      END
+      || step || '-to-' || next_step
+    ELSE
+      step
+    END AS description,
+    CASE WHEN maybe_next_ancestor_ts IS NULL THEN
+      NULL
+    ELSE
+      CASE WHEN maybe_next_ancestor_ts > ancestor_end THEN
+        (maybe_next_ancestor_ts - ancestor_end)
+      ELSE
+        0
+      END
+    END AS queuing_time_ns
+  FROM {{prefix}}_flow_event
+  ORDER BY {{id_field}}, trace_id, ts;
diff --git a/src/trace_processor/metrics/chrome/scroll_flow_event_queuing_delay.sql b/src/trace_processor/metrics/chrome/scroll_flow_event_queuing_delay.sql
index 81eba40..94a8f48 100644
--- a/src/trace_processor/metrics/chrome/scroll_flow_event_queuing_delay.sql
+++ b/src/trace_processor/metrics/chrome/scroll_flow_event_queuing_delay.sql
@@ -21,55 +21,7 @@
 -- Provides the scroll_flow_event table which gives us all the flow events with
 -- associated GestureScrollUpdate events we care about and labels them janky or
 -- not.
-SELECT RUN_METRIC('chrome/scroll_flow_event.sql');
+SELECT RUN_METRIC('chrome/gesture_flow_event_queuing_delay.sql',
+    'prefix', 'scroll',
+    'id_field', 'gesture_scroll_id');
 
--- Take each flow and next flow (from scroll_flow_event table) and generate the
--- metric name as well as compute the time between.
-DROP VIEW IF EXISTS scroll_flow_event_queuing_delay;
-
-CREATE VIEW scroll_flow_event_queuing_delay AS
-  SELECT
-    trace_id,
-    id,
-    ts,
-    dur,
-    track_id,
-    gesture_scroll_id,
-    scroll_slice_id,
-    scroll_ts,
-    scroll_dur,
-    scroll_track_id,
-    jank,
-    step,
-    ancestor_id,
-    ancestor_ts,
-    ancestor_end,
-    next_id,
-    next_step,
-    maybe_next_ancestor_ts,
-    next_track_id,
-      CASE WHEN trace_id = next_trace_id THEN
-      'InputLatency.LatencyInfo.Flow.QueuingDelay.' ||
-      CASE WHEN
-        jank IS NOT NULL AND
-        jank = 1
-      THEN
-          'Jank.'
-      ELSE
-          'NoJank.'
-      END
-      || step || '-to-' || next_step
-    ELSE
-      step
-    END AS description,
-    CASE WHEN maybe_next_ancestor_ts IS NULL THEN
-      NULL
-    ELSE
-      CASE WHEN maybe_next_ancestor_ts > ancestor_end THEN
-        (maybe_next_ancestor_ts - ancestor_end)
-      ELSE
-        0
-      END
-    END AS queuing_time_ns
-  FROM scroll_flow_event
-  ORDER BY gesture_scroll_id, trace_id, ts;
diff --git a/src/trace_processor/metrics/chrome/touch_flow_event_queuing_delay.sql b/src/trace_processor/metrics/chrome/touch_flow_event_queuing_delay.sql
new file mode 100644
index 0000000..2c493d1
--- /dev/null
+++ b/src/trace_processor/metrics/chrome/touch_flow_event_queuing_delay.sql
@@ -0,0 +1,26 @@
+--
+-- Copyright 2021 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the 'License');
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an 'AS IS' BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- This metric takes each flow event in a InputLatency::GestureScrollUpdate and
+-- and computes the time from the ancestor_end of the current flow to the
+-- ancestor_ts of the next flow event. This is a reasonable approximation of the
+-- time we waited for the next step in the critical flow to start.
+
+-- Provides the scroll_flow_event table which gives us all the flow events with
+-- associated TouchMove events we care about and labels them janky or not.
+SELECT RUN_METRIC('chrome/gesture_flow_event_queuing_delay.sql',
+    'prefix', 'touch',
+    'id_field', 'touch_id');
+
diff --git a/test/trace_processor/chrome/index b/test/trace_processor/chrome/index
index 5509924..3ba13d8 100644
--- a/test/trace_processor/chrome/index
+++ b/test/trace_processor/chrome/index
@@ -21,8 +21,10 @@
 # Touch gesture metrics
 ../../data/chrome_touch_gesture_scroll.pftrace touch_jank.sql touch_jank.out
 ../../data/chrome_touch_gesture_scroll.pftrace touch_flow_event.sql touch_flow_event.out
+../../data/chrome_touch_gesture_scroll.pftrace touch_flow_event_queuing_delay.sql touch_flow_event_queuing_delay.out
 touch_jank.py touch_jank.sql touch_jank_synth.out
 touch_jank.py touch_flow_event.sql touch_flow_event_synth.out
+touch_jank.py touch_flow_event_queuing_delay_full.sql touch_flow_event_queuing_delay_synth.out
 
 # Chrome memory snapshots.
 ../../data/chrome_memory_snapshot.pftrace memory_snapshot_general_validation.sql memory_snapshot_general_validation.out
diff --git a/test/trace_processor/chrome/touch_flow_event_queuing_delay.out b/test/trace_processor/chrome/touch_flow_event_queuing_delay.out
new file mode 100644
index 0000000..aa26ef6
--- /dev/null
+++ b/test/trace_processor/chrome/touch_flow_event_queuing_delay.out
@@ -0,0 +1,38 @@
+
+"trace_id","jank","step","next_step","ancestor_end","maybe_next_ancestor_ts","queuing_time_ns"
+6911,1,"AsyncBegin","Begin",1383130519558418,1383130544003418,24445000
+6911,1,"Begin","STEP_SEND_INPUT_EVENT_UI",1383130544237418,1383130544003418,0
+6911,1,"STEP_SEND_INPUT_EVENT_UI","STEP_HANDLE_INPUT_EVENT_IMPL",1383130544237418,1383130546898418,2661000
+6911,1,"STEP_HANDLE_INPUT_EVENT_IMPL","STEP_DID_HANDLE_INPUT_AND_OVERSCROLL",1383130547772418,1383130546898418,0
+6911,1,"STEP_DID_HANDLE_INPUT_AND_OVERSCROLL","STEP_HANDLE_INPUT_EVENT_MAIN",1383130547772418,1383130555182418,7410000
+6911,1,"STEP_HANDLE_INPUT_EVENT_MAIN","STEP_HANDLED_INPUT_EVENT_MAIN_OR_IMPL",1383130573445418,1383130555182418,0
+6911,1,"STEP_HANDLED_INPUT_EVENT_MAIN_OR_IMPL","STEP_HANDLED_INPUT_EVENT_IMPL",1383130573445418,1383130573311418,0
+6911,1,"STEP_HANDLED_INPUT_EVENT_IMPL","STEP_HANDLE_INPUT_EVENT_MAIN_COMMIT",1383130573321418,1383130656314418,82993000
+6911,1,"STEP_HANDLE_INPUT_EVENT_MAIN_COMMIT","STEP_SWAP_BUFFERS",1383130656325418,1383130667367418,11042000
+6911,1,"STEP_SWAP_BUFFERS","STEP_DRAW_AND_SWAP",1383130667384418,1383130681350418,13966000
+6911,1,"STEP_DRAW_AND_SWAP","STEP_FINISHED_SWAP_BUFFERS",1383130681375418,1383130717639418,36264000
+6911,1,"STEP_FINISHED_SWAP_BUFFERS","AsyncBegin",1383130717657418,"[NULL]","[NULL]"
+6915,0,"AsyncBegin","Begin",1383130553558418,1383130558151418,4593000
+6915,0,"Begin","STEP_SEND_INPUT_EVENT_UI",1383130558425418,1383130558151418,0
+6915,0,"STEP_SEND_INPUT_EVENT_UI","STEP_HANDLE_INPUT_EVENT_IMPL",1383130558425418,1383130558645418,220000
+6915,0,"STEP_HANDLE_INPUT_EVENT_IMPL","STEP_DID_HANDLE_INPUT_AND_OVERSCROLL",1383130558834418,1383130558645418,0
+6915,0,"STEP_DID_HANDLE_INPUT_AND_OVERSCROLL","STEP_HANDLE_INPUT_EVENT_MAIN",1383130558834418,1383130587655418,28821000
+6915,0,"STEP_HANDLE_INPUT_EVENT_MAIN","STEP_HANDLED_INPUT_EVENT_MAIN_OR_IMPL",1383130594793418,1383130587655418,0
+6915,0,"STEP_HANDLED_INPUT_EVENT_MAIN_OR_IMPL","STEP_HANDLED_INPUT_EVENT_IMPL",1383130594793418,1383130594791418,0
+6915,0,"STEP_HANDLED_INPUT_EVENT_IMPL","STEP_HANDLE_INPUT_EVENT_MAIN_COMMIT",1383130594811418,1383130656335418,61524000
+6915,0,"STEP_HANDLE_INPUT_EVENT_MAIN_COMMIT","STEP_SWAP_BUFFERS",1383130656339418,1383130667392418,11053000
+6915,0,"STEP_SWAP_BUFFERS","STEP_DRAW_AND_SWAP",1383130667395418,1383130681397418,14002000
+6915,0,"STEP_DRAW_AND_SWAP","STEP_FINISHED_SWAP_BUFFERS",1383130681404418,1383130717665418,36261000
+6915,0,"STEP_FINISHED_SWAP_BUFFERS","AsyncBegin",1383130717668418,"[NULL]","[NULL]"
+6940,0,"AsyncBegin","Begin",1383130681558418,1383130686495418,4937000
+6940,0,"Begin","STEP_SEND_INPUT_EVENT_UI",1383130686909418,1383130686495418,0
+6940,0,"STEP_SEND_INPUT_EVENT_UI","STEP_HANDLE_INPUT_EVENT_IMPL",1383130686909418,1383130687055418,146000
+6940,0,"STEP_HANDLE_INPUT_EVENT_IMPL","STEP_DID_HANDLE_INPUT_AND_OVERSCROLL",1383130687419418,1383130687055418,0
+6940,0,"STEP_DID_HANDLE_INPUT_AND_OVERSCROLL","STEP_HANDLE_INPUT_EVENT_MAIN",1383130687419418,1383130719656418,32237000
+6940,0,"STEP_HANDLE_INPUT_EVENT_MAIN","STEP_HANDLED_INPUT_EVENT_MAIN_OR_IMPL",1383130727954418,1383130719656418,0
+6940,0,"STEP_HANDLED_INPUT_EVENT_MAIN_OR_IMPL","STEP_HANDLED_INPUT_EVENT_IMPL",1383130727954418,1383130728343418,389000
+6940,0,"STEP_HANDLED_INPUT_EVENT_IMPL","STEP_HANDLE_INPUT_EVENT_MAIN_COMMIT",1383130728356418,1383130815135418,86779000
+6940,0,"STEP_HANDLE_INPUT_EVENT_MAIN_COMMIT","STEP_SWAP_BUFFERS",1383130815154418,1383130825051418,9897000
+6940,0,"STEP_SWAP_BUFFERS","STEP_DRAW_AND_SWAP",1383130825086418,1383130834387418,9301000
+6940,0,"STEP_DRAW_AND_SWAP","STEP_FINISHED_SWAP_BUFFERS",1383130834405418,1383130849338418,14933000
+6940,0,"STEP_FINISHED_SWAP_BUFFERS","[NULL]",1383130849355418,"[NULL]","[NULL]"
diff --git a/test/trace_processor/chrome/touch_flow_event_queuing_delay.sql b/test/trace_processor/chrome/touch_flow_event_queuing_delay.sql
new file mode 100644
index 0000000..0230908
--- /dev/null
+++ b/test/trace_processor/chrome/touch_flow_event_queuing_delay.sql
@@ -0,0 +1,30 @@
+--
+-- Copyright 2021 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the 'License');
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an 'AS IS' BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+SELECT RUN_METRIC('chrome/touch_flow_event_queuing_delay.sql')
+  AS suppress_query_output;
+
+-- trace 6911 is janky and 6915 and 6940 succeed it (both are not janky).
+SELECT
+  trace_id,
+  jank,
+  step,
+  next_step,
+  ancestor_end,
+  maybe_next_ancestor_ts,
+  queuing_time_ns
+FROM touch_flow_event_queuing_delay
+WHERE trace_id = 6915 OR trace_id = 6911 OR trace_id = 6940
+ORDER BY trace_id, ts;
diff --git a/test/trace_processor/chrome/touch_flow_event_queuing_delay_full.sql b/test/trace_processor/chrome/touch_flow_event_queuing_delay_full.sql
new file mode 100644
index 0000000..f1e9c33
--- /dev/null
+++ b/test/trace_processor/chrome/touch_flow_event_queuing_delay_full.sql
@@ -0,0 +1,29 @@
+--
+-- Copyright 2021 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the 'License');
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an 'AS IS' BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+SELECT RUN_METRIC('chrome/touch_flow_event_queuing_delay.sql')
+  AS suppress_query_output;
+
+-- trace 6911 is janky and 6915 and 6940 succeed it (both are not janky).
+SELECT
+  trace_id,
+  jank,
+  step,
+  next_step,
+  ancestor_end,
+  maybe_next_ancestor_ts,
+  queuing_time_ns
+FROM touch_flow_event_queuing_delay
+ORDER BY trace_id, ts;
diff --git a/test/trace_processor/chrome/touch_flow_event_queuing_delay_synth.out b/test/trace_processor/chrome/touch_flow_event_queuing_delay_synth.out
new file mode 100644
index 0000000..7c5c8b0
--- /dev/null
+++ b/test/trace_processor/chrome/touch_flow_event_queuing_delay_synth.out
@@ -0,0 +1,12 @@
+
+"trace_id","jank","step","next_step","ancestor_end","maybe_next_ancestor_ts","queuing_time_ns"
+34577,0,"AsyncBegin","Begin",0,0,0
+34577,0,"Begin","End",1000000,11000000,10000000
+34577,0,"End","AsyncBegin",12000000,"[NULL]","[NULL]"
+34578,1,"AsyncBegin","Begin",16000000,16000000,0
+34578,1,"Begin","End",17000000,50000000,33000000
+34578,1,"End","AsyncBegin",51000000,"[NULL]","[NULL]"
+34579,0,"AsyncBegin","Begin",55000000,55000000,0
+34579,0,"Begin","STEP_SEND_INPUT_EVENT_UI",56000000,87000000,31000000
+34579,0,"STEP_SEND_INPUT_EVENT_UI","End",88000000,89000000,1000000
+34579,0,"End","[NULL]",90000000,"[NULL]","[NULL]"