Adding scroll_jank_caused_by_scheduling.sql

Any barrage tasks running for more than 8 ms combined in a window that
we could have started processing input but did not, is a cause of jank
for this input.

Change-Id: I11eddd7ee0982aa1dd4b1b31ef01e9ce2ac007c8
diff --git a/Android.bp b/Android.bp
index de17fc4..173062b 100644
--- a/Android.bp
+++ b/Android.bp
@@ -9046,6 +9046,7 @@
         "src/trace_processor/metrics/sql/chrome/chrome_input_to_browser_intervals.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_performance_mark_hashes.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_processes.sql",
+        "src/trace_processor/metrics/sql/chrome/chrome_scroll_jank_caused_by_scheduling.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_slice_names.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_tasks.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_thread_slice.sql",
diff --git a/BUILD b/BUILD
index 56a267d..fa9565b 100644
--- a/BUILD
+++ b/BUILD
@@ -1261,6 +1261,7 @@
         "src/trace_processor/metrics/sql/chrome/chrome_input_to_browser_intervals.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_performance_mark_hashes.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_processes.sql",
+        "src/trace_processor/metrics/sql/chrome/chrome_scroll_jank_caused_by_scheduling.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_slice_names.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_tasks.sql",
         "src/trace_processor/metrics/sql/chrome/chrome_thread_slice.sql",
diff --git a/src/trace_processor/metrics/sql/BUILD.gn b/src/trace_processor/metrics/sql/BUILD.gn
index 412e395..9b87f5c 100644
--- a/src/trace_processor/metrics/sql/BUILD.gn
+++ b/src/trace_processor/metrics/sql/BUILD.gn
@@ -103,6 +103,7 @@
   "chrome/chrome_input_to_browser_intervals.sql",
   "chrome/chrome_performance_mark_hashes.sql",
   "chrome/chrome_processes.sql",
+  "chrome/chrome_scroll_jank_caused_by_scheduling.sql",
   "chrome/chrome_slice_names.sql",
   "chrome/chrome_unsymbolized_args.sql",
   "chrome/chrome_tasks.sql",
diff --git a/src/trace_processor/metrics/sql/chrome/chrome_input_to_browser_intervals.sql b/src/trace_processor/metrics/sql/chrome/chrome_input_to_browser_intervals.sql
index 5b029b3..9f64718 100644
--- a/src/trace_processor/metrics/sql/chrome/chrome_input_to_browser_intervals.sql
+++ b/src/trace_processor/metrics/sql/chrome/chrome_input_to_browser_intervals.sql
@@ -196,7 +196,7 @@
           -- and is not a fling generated by the viz compositor thread(GPU process).
           ancestor_slices.name = "sendTouchEvent"
       )
-      > 0
+      = 1
       THEN FALSE
     ELSE TRUE
     END AS blocked_gesture
diff --git a/src/trace_processor/metrics/sql/chrome/chrome_scroll_jank_caused_by_scheduling.sql b/src/trace_processor/metrics/sql/chrome/chrome_scroll_jank_caused_by_scheduling.sql
new file mode 100644
index 0000000..9fe4516
--- /dev/null
+++ b/src/trace_processor/metrics/sql/chrome/chrome_scroll_jank_caused_by_scheduling.sql
@@ -0,0 +1,90 @@
+--
+-- Copyright 2022 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/chrome_input_to_browser_intervals.sql');
+
+-- Script params:
+-- {{dur_causes_jank_ms}} : The duration of a task barrage on the Chrome
+-- main thread that will delay input causing jank.
+
+-- Filter intervals to only durations longer than {{dur_causes_jank_ms}}.
+DROP VIEW IF EXISTS chrome_input_to_browser_longer_intervals;
+CREATE VIEW chrome_input_to_browser_longer_intervals AS
+SELECT
+  *
+FROM chrome_input_to_browser_intervals
+WHERE
+  (window_end_ts - window_start_ts) >= {{dur_causes_jank_ms}} * 1e6;
+
+-- Assign tasks to each delay interval that we could have started
+-- processing input but didn't on the main thread, and sum those
+-- tasks.
+-- We filter java out here as we're interested in tasks that delayed
+-- yielding to java native work, and we filter tasks that are more
+-- than 8ms here as those are handled separately and are not regarded
+-- as scheduling issues.
+DROP VIEW IF EXISTS chrome_task_barrages_per_interval;
+CREATE VIEW chrome_task_barrages_per_interval AS
+SELECT
+  GROUP_CONCAT(DISTINCT full_name) AS full_name,
+  SUM(dur / 1e6) AS total_duration_ms,
+  SUM(thread_dur / 1e6) AS total_thread_duration_ms,
+  MIN(id) AS first_id_per_task_barrage,
+  MAX(id) AS last_id_per_task_barrage,
+  COUNT(*) as count,
+  window_start_id,
+  window_start_ts,
+  window_end_id,
+  window_end_ts
+FROM
+  (SELECT * FROM (
+    (
+      SELECT
+        chrome_tasks.full_name AS full_name,
+        chrome_tasks.dur  AS dur,
+        chrome_tasks.thread_dur AS thread_dur,
+        chrome_tasks.ts AS ts,
+        chrome_tasks.id,
+        chrome_tasks.upid
+      FROM
+        chrome_tasks
+      WHERE
+         chrome_tasks.thread_name = "CrBrowserMain"
+         AND task_type != "java"
+         AND task_type != "choreographer"
+      ORDER BY chrome_tasks.ts
+    ) tasks
+    JOIN chrome_input_to_browser_longer_intervals
+      ON (tasks.ts + tasks.dur) >
+      chrome_input_to_browser_longer_intervals.window_start_ts
+      AND (tasks.ts + tasks.dur) <
+      chrome_input_to_browser_longer_intervals.window_end_ts
+      AND tasks.ts > chrome_input_to_browser_longer_intervals.window_start_ts
+      AND tasks.ts < chrome_input_to_browser_longer_intervals.window_end_ts
+      -- For cases when there are multiple chrome instances.
+      and tasks.upid = chrome_input_to_browser_longer_intervals.upid)
+    ORDER BY
+    window_start_ts, window_end_ts
+  )
+  GROUP BY window_start_ts, window_end_ts;
+
+-- Filter to task barrages that took more than 8ms, as barrages
+-- that lasted less than that are unlikely to have caused jank.
+DROP VIEW IF EXISTS chrome_scroll_jank_caused_by_scheduling;
+CREATE VIEW chrome_scroll_jank_caused_by_scheduling AS
+  SELECT *
+  FROM chrome_task_barrages_per_interval
+  WHERE total_duration_ms > {{dur_causes_jank_ms}} AND count > 1
+  ORDER BY total_duration_ms DESC;
\ No newline at end of file
diff --git a/test/data/fling_with_input_delay.pftrace.sha256 b/test/data/fling_with_input_delay.pftrace.sha256
new file mode 100644
index 0000000..c8ea1a2
--- /dev/null
+++ b/test/data/fling_with_input_delay.pftrace.sha256
@@ -0,0 +1 @@
+8c968b20e71481475a429399b4366bd796c527293218fe80789f9ed6ab9db5b4
\ No newline at end of file
diff --git a/test/trace_processor/chrome/chrome_scroll_jank_caused_by_scheduling_test.out b/test/trace_processor/chrome/chrome_scroll_jank_caused_by_scheduling_test.out
new file mode 100644
index 0000000..5b78dca
--- /dev/null
+++ b/test/trace_processor/chrome/chrome_scroll_jank_caused_by_scheduling_test.out
@@ -0,0 +1,3 @@
+
+"full_name","total_duration_ms","total_thread_duration_ms","count","window_start_ts","window_end_ts"
+"RunTask(posted_from=cc/scheduler/scheduler.cc:PostPendingBeginFrameTask),RunTask(posted_from=cc/scheduler/scheduler.cc:ScheduleBeginImplFrameDeadline),SingleThreadProxy::BeginMainFrame(java_views=ToolbarLayout),blink.mojom.WidgetInputHandler reply (hash=3392143105),cc.mojom.RenderFrameMetadataObserverClient message (hash=330497194),viz.mojom.CompositorFrameSinkClient message (hash=3114070324),viz.mojom.CompositorFrameSinkClient message (hash=50871626),viz.mojom.FrameSinkManagerClient message (hash=532012934)",7.568000,6.745000,11,666960999011,666972176011
diff --git a/test/trace_processor/chrome/chrome_scroll_jank_caused_by_scheduling_test.sql b/test/trace_processor/chrome/chrome_scroll_jank_caused_by_scheduling_test.sql
new file mode 100644
index 0000000..4caea94
--- /dev/null
+++ b/test/trace_processor/chrome/chrome_scroll_jank_caused_by_scheduling_test.sql
@@ -0,0 +1,27 @@
+--
+-- Copyright 2022 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/chrome_scroll_jank_caused_by_scheduling.sql',
+'dur_causes_jank_ms',
+/* dur_causes_jank_ms = */ '5') AS suppress_query_output;
+
+SELECT
+  full_name,
+  total_duration_ms,
+  total_thread_duration_ms,
+  count,
+  window_start_ts,
+  window_end_ts
+FROM chrome_scroll_jank_caused_by_scheduling;
\ No newline at end of file
diff --git a/test/trace_processor/chrome/index b/test/trace_processor/chrome/index
index e5ef48a..7f16013 100644
--- a/test/trace_processor/chrome/index
+++ b/test/trace_processor/chrome/index
@@ -15,6 +15,7 @@
 ../../data/chrome_scroll_without_vsync.pftrace scroll_jank_cause_queuing_delay_general_validation_test.sql scroll_jank_cause_queuing_delay_general_validation.out
 ../../data/chrome_scroll_without_vsync.pftrace chrome_thread_slice_test.sql chrome_thread_slice.out
 ../../data/scrolling_with_blocked_nonblocked_frames.pftrace chrome_input_to_browser_intervals_test.sql chrome_input_to_browser_intervals.out
+../../data/fling_with_input_delay.pftrace chrome_scroll_jank_caused_by_scheduling_test.sql chrome_scroll_jank_caused_by_scheduling_test.out
 ../track_event/track_event_counters.textproto chrome_thread_slice_repeated_test.sql chrome_thread_slice_repeated.out
 ../../data/chrome_rendering_desktop.pftrace frame_times frame_times_metric.out
 ../../data/chrome_rendering_desktop.pftrace chrome_dropped_frames chrome_dropped_frames_metric.out