Merge "tp: move preprocessor into its own folder" into main
diff --git a/Android.bp b/Android.bp
index c066dd8..1b7b970 100644
--- a/Android.bp
+++ b/Android.bp
@@ -13133,6 +13133,7 @@
         "src/trace_processor/metrics/sql/android/startup/mcycles_per_launch.sql",
         "src/trace_processor/metrics/sql/android/startup/slice_functions.sql",
         "src/trace_processor/metrics/sql/android/startup/slow_start_reasons.sql",
+        "src/trace_processor/metrics/sql/android/startup/slow_start_thresholds.sql",
         "src/trace_processor/metrics/sql/android/startup/system_state.sql",
         "src/trace_processor/metrics/sql/android/startup/thread_state_breakdown.sql",
         "src/trace_processor/metrics/sql/android/sysui_notif_shade_list_builder_metric.sql",
diff --git a/BUILD b/BUILD
index 8a632cb..f29c84a 100644
--- a/BUILD
+++ b/BUILD
@@ -2244,6 +2244,7 @@
         "src/trace_processor/metrics/sql/android/startup/mcycles_per_launch.sql",
         "src/trace_processor/metrics/sql/android/startup/slice_functions.sql",
         "src/trace_processor/metrics/sql/android/startup/slow_start_reasons.sql",
+        "src/trace_processor/metrics/sql/android/startup/slow_start_thresholds.sql",
         "src/trace_processor/metrics/sql/android/startup/system_state.sql",
         "src/trace_processor/metrics/sql/android/startup/thread_state_breakdown.sql",
         "src/trace_processor/metrics/sql/android/sysui_notif_shade_list_builder_metric.sql",
diff --git a/src/trace_processor/metrics/sql/android/BUILD.gn b/src/trace_processor/metrics/sql/android/BUILD.gn
index 04f3116..a10366b 100644
--- a/src/trace_processor/metrics/sql/android/BUILD.gn
+++ b/src/trace_processor/metrics/sql/android/BUILD.gn
@@ -137,6 +137,7 @@
     "startup/mcycles_per_launch.sql",
     "startup/slice_functions.sql",
     "startup/slow_start_reasons.sql",
+    "startup/slow_start_thresholds.sql",
     "startup/system_state.sql",
     "startup/thread_state_breakdown.sql",
     "sysui_notif_shade_list_builder_metric.sql",
diff --git a/src/trace_processor/metrics/sql/android/startup/slow_start_reasons.sql b/src/trace_processor/metrics/sql/android/startup/slow_start_reasons.sql
index 82c5cbe..5743dfd 100644
--- a/src/trace_processor/metrics/sql/android/startup/slow_start_reasons.sql
+++ b/src/trace_processor/metrics/sql/android/startup/slow_start_reasons.sql
@@ -19,6 +19,8 @@
 SELECT RUN_METRIC('android/startup/thread_state_breakdown.sql');
 SELECT RUN_METRIC('android/startup/system_state.sql');
 SELECT RUN_METRIC('android/startup/mcycles_per_launch.sql');
+-- Define helper functions related to slow start thresholds
+SELECT RUN_METRIC('android/startup/slow_start_thresholds.sql');
 
 CREATE OR REPLACE PERFETTO FUNCTION _is_spans_overlapping(
   ts1 LONG,
@@ -411,7 +413,7 @@
           'MAIN_THREAD_TIME_SPENT_IN_RUNNABLE' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 15,
+            'value', threshold_runnable_percentage(),
             'unit', 'PERCENTAGE',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -423,7 +425,8 @@
           NULL as extra
         FROM android_startups launch
         WHERE launch.startup_id = $startup_id
-          AND main_thread_time_for_launch_in_runnable_state(launch.startup_id) > launch.dur * 0.15
+          AND main_thread_time_for_launch_in_runnable_state(launch.startup_id) >
+            launch.dur / 100 * threshold_runnable_percentage()
 
         UNION ALL
         SELECT 'Main Thread - Time spent in interruptible sleep state' as slow_cause,
@@ -431,7 +434,7 @@
           'MAIN_THREAD_TIME_SPENT_IN_INTERRUPTIBLE_SLEEP' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 2900000000,
+            'value', threshold_interruptible_sleep_ns(),
             'unit', 'NS',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -441,7 +444,8 @@
           NULL as extra
         FROM android_startups launch
         WHERE launch.startup_id = $startup_id
-          AND main_thread_time_for_launch_and_state(launch.startup_id, 'S') > 2900e6
+          AND main_thread_time_for_launch_and_state(launch.startup_id, 'S') >
+            threshold_interruptible_sleep_ns()
 
         UNION ALL
         SELECT 'Main Thread - Time spent in Blocking I/O' as slow_cause,
@@ -449,7 +453,7 @@
           'MAIN_THREAD_TIME_SPENT_IN_BLOCKING_IO' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 450000000,
+            'value', threshold_blocking_io_ns(),
             'unit', 'NS',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -461,7 +465,8 @@
           NULL as extra
         FROM android_startups launch
         WHERE launch.startup_id = $startup_id
-          AND main_thread_time_for_launch_state_and_io_wait(launch.startup_id, 'D*', TRUE) > 450e6
+          AND main_thread_time_for_launch_state_and_io_wait(launch.startup_id, 'D*', TRUE) >
+            threshold_blocking_io_ns()
 
         UNION ALL
         SELECT 'Main Thread - Time spent in OpenDexFilesFromOat*' as slow_cause,
@@ -469,7 +474,7 @@
           'MAIN_THREAD_TIME_SPENT_IN_OPEN_DEX_FILES_FROM_OAT' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 20,
+            'value', threshold_open_dex_files_from_oat_percentage(),
             'unit', 'PERCENTAGE',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -484,7 +489,8 @@
         FROM android_startups launch
         WHERE launch.startup_id = $startup_id AND
           android_sum_dur_on_main_thread_for_startup_and_slice(
-          launch.startup_id, 'OpenDexFilesFromOat*') > launch.dur * 0.2
+          launch.startup_id, 'OpenDexFilesFromOat*') >
+            launch.dur / 100 * threshold_open_dex_files_from_oat_percentage()
 
         UNION ALL
         SELECT 'Time spent in bindApplication' as slow_cause,
@@ -492,7 +498,7 @@
           'TIME_SPENT_IN_BIND_APPLICATION' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 1250000000,
+            'value', threshold_bind_application_ns(),
             'unit', 'NS',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -504,7 +510,8 @@
           NULL as extra
         FROM android_startups launch
         WHERE launch.startup_id = $startup_id
-          AND android_sum_dur_for_startup_and_slice(launch.startup_id, 'bindApplication') > 1250e6
+          AND android_sum_dur_for_startup_and_slice(launch.startup_id, 'bindApplication') >
+            threshold_bind_application_ns()
 
         UNION ALL
         SELECT 'Time spent in view inflation' as slow_cause,
@@ -512,7 +519,7 @@
           'TIME_SPENT_IN_VIEW_INFLATION' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 450000000,
+            'value', threshold_view_inflation_ns(),
             'unit', 'NS',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -524,7 +531,8 @@
           NULL as extra
         FROM android_startups launch
         WHERE launch.startup_id = $startup_id
-          AND android_sum_dur_for_startup_and_slice(launch.startup_id, 'inflate') > 450e6
+          AND android_sum_dur_for_startup_and_slice(launch.startup_id, 'inflate') >
+            threshold_view_inflation_ns()
 
         UNION ALL
         SELECT 'Time spent in ResourcesManager#getResources' as slow_cause,
@@ -532,7 +540,7 @@
           'TIME_SPENT_IN_RESOURCES_MANAGER_GET_RESOURCES' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 130000000,
+            'value', threshold_resources_manager_get_resources_ns(),
             'unit', 'NS',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -545,7 +553,8 @@
         FROM android_startups launch
         WHERE launch.startup_id = $startup_id
           AND android_sum_dur_for_startup_and_slice(
-          launch.startup_id, 'ResourcesManager#getResources') > 130e6
+          launch.startup_id, 'ResourcesManager#getResources') >
+            threshold_resources_manager_get_resources_ns()
 
         UNION ALL
         SELECT 'Time spent verifying classes' as slow_cause,
@@ -553,7 +562,7 @@
           'TIME_SPENT_VERIFYING_CLASSES' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 15,
+            'value', threshold_verify_classes_percentage(),
             'unit', 'PERCENTAGE',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -568,7 +577,7 @@
         FROM android_startups launch
         WHERE launch.startup_id = $startup_id AND
           android_sum_dur_for_startup_and_slice(launch.startup_id, 'VerifyClass*')
-            > launch.dur * 0.15
+            > launch.dur / 100 * threshold_verify_classes_percentage()
 
         UNION ALL
         SELECT 'Potential CPU contention with another process' AS slow_cause,
@@ -576,7 +585,7 @@
           'POTENTIAL_CPU_CONTENTION_WITH_ANOTHER_PROCESS' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 100000000,
+            'value', threshold_potential_cpu_contention_ns(),
             'unit', 'NS',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -587,7 +596,8 @@
           NULL as extra
         FROM android_startups launch
         WHERE launch.startup_id = $startup_id AND
-          main_thread_time_for_launch_in_runnable_state(launch.startup_id) > 100e6 AND
+          main_thread_time_for_launch_in_runnable_state(launch.startup_id) >
+            threshold_potential_cpu_contention_ns() AND
           most_active_process_for_launch(launch.startup_id) IS NOT NULL
 
         UNION ALL
@@ -596,7 +606,7 @@
           'JIT_ACTIVITY' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 100000000,
+            'value', threshold_jit_activity_ns(),
             'unit', 'NS',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -612,7 +622,7 @@
           launch.startup_id,
           'Running',
           'Jit thread pool'
-        ) > 100e6
+        ) > threshold_jit_activity_ns()
 
         UNION ALL
         SELECT 'Main Thread - Lock contention' as slow_cause,
@@ -620,7 +630,7 @@
           'MAIN_THREAD_LOCK_CONTENTION' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 20,
+            'value', threshold_lock_contention_percentage(),
             'unit', 'PERCENTAGE',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -637,7 +647,7 @@
           AND android_sum_dur_on_main_thread_for_startup_and_slice(
           launch.startup_id,
           'Lock contention on*'
-        ) > launch.dur * 0.2
+        ) > launch.dur / 100 * threshold_lock_contention_percentage()
 
         UNION ALL
         SELECT 'Main Thread - Monitor contention' as slow_cause,
@@ -645,7 +655,7 @@
           'MAIN_THREAD_MONITOR_CONTENTION' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 15,
+            'value', threshold_monitor_contention_percentage(),
             'unit', 'PERCENTAGE',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -662,7 +672,7 @@
           AND android_sum_dur_on_main_thread_for_startup_and_slice(
             launch.startup_id,
             'Lock contention on a monitor*'
-          ) > launch.dur * 0.15
+          ) > launch.dur / 100 * threshold_monitor_contention_percentage()
 
         UNION ALL
         SELECT 'JIT compiled methods' as slow_cause,
@@ -670,7 +680,7 @@
           'JIT_COMPILED_METHODS' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 65,
+            'value', threshold_jit_compiled_methods_count(),
             'unit', 'COUNT',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -686,7 +696,7 @@
           AND (
           SELECT COUNT(1)
           FROM ANDROID_SLICES_FOR_STARTUP_AND_SLICE_NAME(launch.startup_id, 'JIT compiling*')
-          WHERE thread_name = 'Jit thread pool') > 65
+          WHERE thread_name = 'Jit thread pool') > threshold_jit_compiled_methods_count()
 
         UNION ALL
         SELECT 'Broadcast dispatched count' as slow_cause,
@@ -694,7 +704,7 @@
           'BROADCAST_DISPATCHED_COUNT' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 15,
+            'value', threshold_broadcast_dispatched_count(),
             'unit', 'COUNT',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -708,7 +718,7 @@
         WHERE launch.startup_id = $startup_id
           AND count_slices_concurrent_to_launch(
           launch.startup_id,
-          'Broadcast dispatched*') > 15
+          'Broadcast dispatched*') > threshold_broadcast_dispatched_count()
 
         UNION ALL
         SELECT 'Broadcast received count' as slow_cause,
@@ -716,7 +726,7 @@
           'BROADCAST_RECEIVED_COUNT' as reason_id,
           'WARNING' as severity,
           AndroidStartupMetric_ThresholdValue(
-            'value', 50,
+            'value', threshold_broadcast_received_count(),
             'unit', 'COUNT',
             'higher_expected', FALSE) as expected_val,
           AndroidStartupMetric_ActualValue(
@@ -730,7 +740,7 @@
         WHERE launch.startup_id = $startup_id
           AND count_slices_concurrent_to_launch(
             launch.startup_id,
-            'broadcastReceiveReg*') > 50
+            'broadcastReceiveReg*') > threshold_broadcast_received_count()
 
         UNION ALL
         SELECT 'Startup running concurrent to launch' as slow_cause,
diff --git a/src/trace_processor/metrics/sql/android/startup/slow_start_thresholds.sql b/src/trace_processor/metrics/sql/android/startup/slow_start_thresholds.sql
new file mode 100644
index 0000000..f8ef31b
--- /dev/null
+++ b/src/trace_processor/metrics/sql/android/startup/slow_start_thresholds.sql
@@ -0,0 +1,111 @@
+--
+-- 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.
+--
+
+INCLUDE PERFETTO MODULE android.startup.startups;
+
+DROP VIEW IF EXISTS slow_start_thresholds;
+CREATE PERFETTO VIEW slow_start_thresholds AS
+SELECT
+  15 AS runnable_percentage,
+  2900000000 AS interruptible_sleep_ns,
+  450000000 AS blocking_io_ns,
+  20 AS open_dex_files_from_oat_percentage,
+  1250000000 AS bind_application_ns,
+  450000000 AS view_inflation_ns,
+  130000000 AS resources_manager_get_resources_ns,
+  15 AS verify_classes_percentage,
+  100000000 AS potential_cpu_contention_ns,
+  100000000 AS jit_activity_ns,
+  20 AS lock_contention_percentage,
+  15 AS monitor_contention_percentage,
+  65 AS jit_compiled_methods_count,
+  15 AS broadcast_dispatched_count,
+  50 AS broadcast_received_count;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_runnable_percentage()
+RETURNS INT AS
+  SELECT runnable_percentage 
+  FROM slow_start_thresholds;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_interruptible_sleep_ns()
+RETURNS INT AS
+  SELECT interruptible_sleep_ns
+  FROM slow_start_thresholds;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_blocking_io_ns()
+RETURNS INT AS
+  SELECT blocking_io_ns
+  FROM slow_start_thresholds;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_open_dex_files_from_oat_percentage()
+RETURNS INT AS
+  SELECT open_dex_files_from_oat_percentage
+  FROM slow_start_thresholds;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_bind_application_ns()
+RETURNS INT AS
+  SELECT bind_application_ns
+  FROM slow_start_thresholds;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_view_inflation_ns()
+RETURNS INT AS
+  SELECT view_inflation_ns
+  FROM slow_start_thresholds;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_resources_manager_get_resources_ns()
+RETURNS INT AS
+  SELECT resources_manager_get_resources_ns
+  FROM slow_start_thresholds;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_verify_classes_percentage()
+RETURNS INT AS
+  SELECT verify_classes_percentage
+  FROM slow_start_thresholds;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_potential_cpu_contention_ns()
+RETURNS INT AS
+  SELECT potential_cpu_contention_ns
+  FROM slow_start_thresholds;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_jit_activity_ns()
+RETURNS INT AS
+  SELECT jit_activity_ns
+  FROM slow_start_thresholds;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_lock_contention_percentage()
+RETURNS INT AS
+  SELECT lock_contention_percentage
+  FROM slow_start_thresholds;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_monitor_contention_percentage()
+RETURNS INT AS
+  SELECT monitor_contention_percentage
+  FROM slow_start_thresholds;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_jit_compiled_methods_count()
+RETURNS INT AS
+  SELECT jit_compiled_methods_count
+  FROM slow_start_thresholds;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_broadcast_dispatched_count()
+RETURNS INT AS
+  SELECT broadcast_dispatched_count
+  FROM slow_start_thresholds;
+
+CREATE OR REPLACE PERFETTO FUNCTION threshold_broadcast_received_count()
+RETURNS INT AS
+  SELECT broadcast_received_count
+  FROM slow_start_thresholds;