tp: rework version detection logic in startup metric

By using the Android SDK version to act as a first decision maker for
which table we should select from, this eliminates false positives.

Also while I'm here remove some unnecessary startup id row creating by
relying on the _auto_id column which trace processor adds.

Fixes: 341703657
Change-Id: Ib89cc2a80250ba6b30aa9c3ab24de53419faacb7
diff --git a/Android.bp b/Android.bp
index 2253368..7bd6f16 100644
--- a/Android.bp
+++ b/Android.bp
@@ -13189,6 +13189,7 @@
         "src/trace_processor/perfetto_sql/stdlib/android/statsd.sql",
         "src/trace_processor/perfetto_sql/stdlib/android/suspend.sql",
         "src/trace_processor/perfetto_sql/stdlib/android/thread.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/version.sql",
         "src/trace_processor/perfetto_sql/stdlib/android/winscope/inputmethod.sql",
         "src/trace_processor/perfetto_sql/stdlib/android/winscope/viewcapture.sql",
         "src/trace_processor/perfetto_sql/stdlib/chrome/**/*.sql",
diff --git a/BUILD b/BUILD
index 8c2e2e3..be9ebc9 100644
--- a/BUILD
+++ b/BUILD
@@ -2606,6 +2606,7 @@
         "src/trace_processor/perfetto_sql/stdlib/android/statsd.sql",
         "src/trace_processor/perfetto_sql/stdlib/android/suspend.sql",
         "src/trace_processor/perfetto_sql/stdlib/android/thread.sql",
+        "src/trace_processor/perfetto_sql/stdlib/android/version.sql",
     ],
 )
 
diff --git a/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
index b1266c0..b655e53 100644
--- a/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
@@ -49,5 +49,6 @@
     "statsd.sql",
     "suspend.sql",
     "thread.sql",
+    "version.sql",
   ]
 }
diff --git a/src/trace_processor/perfetto_sql/stdlib/android/startup/startups.sql b/src/trace_processor/perfetto_sql/stdlib/android/startup/startups.sql
index ff5c8f5..c6388b5 100644
--- a/src/trace_processor/perfetto_sql/stdlib/android/startup/startups.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/android/startup/startups.sql
@@ -17,21 +17,7 @@
 INCLUDE PERFETTO MODULE android.startup.startups_maxsdk28;
 INCLUDE PERFETTO MODULE android.startup.startups_minsdk29;
 INCLUDE PERFETTO MODULE android.startup.startups_minsdk33;
-
-CREATE PERFETTO FUNCTION _slice_count(
-  -- Name of the slices to counted.
-  slice_glob STRING)
--- Number of slices with the name.
-RETURNS INT AS
-SELECT COUNT(1) FROM slice WHERE name GLOB $slice_glob;
-
--- Gather all startup data. Populate by different sdks.
-CREATE PERFETTO TABLE _all_startups AS
-SELECT sdk, startup_id, ts, ts_end, dur, package, startup_type FROM _startups_maxsdk28
-UNION ALL
-SELECT sdk, startup_id, ts, ts_end, dur, package, startup_type FROM _startups_minsdk29
-UNION ALL
-SELECT sdk, startup_id, ts, ts_end, dur, package, startup_type FROM _startups_minsdk33;
+INCLUDE PERFETTO MODULE android.version;
 
 -- All activity startups in the trace by startup id.
 -- Populated by different scripts depending on the platform version/contents.
@@ -48,15 +34,37 @@
   package STRING,
   -- Startup type.
   startup_type STRING
-) AS
-SELECT startup_id, ts, ts_end, dur, package, startup_type FROM
-_all_startups WHERE ( CASE
-  WHEN _slice_count('launchingActivity#*:*') > 0
-    THEN sdk = "minsdk33"
-  WHEN _slice_count('MetricsLogger:*') > 0
-    THEN sdk = "minsdk29"
-  ELSE sdk = "maxsdk28"
-  END);
+)
+AS
+WITH version AS (
+  SELECT CASE
+    WHEN _android_sdk_version() >= 33 THEN 33
+    WHEN _android_sdk_version() >= 29 THEN 29
+    WHEN _android_sdk_version() IS NOT NULL THEN 28
+    WHEN (
+      SELECT COUNT()
+      FROM slice
+      WHERE name GLOB 'launchingActivity#*:*'
+    ) > 0 THEN 33
+    WHEN (
+      SELECT COUNT()
+      FROM slice
+      WHERE name GLOB 'MetricsLogger:*'
+    ) > 0 THEN 29
+    ELSE 28
+  END AS v
+)
+SELECT _auto_id as startup_id, ts, ts_end, dur, package, startup_type
+FROM _startups_maxsdk28
+WHERE (SELECT v from version) = 28
+UNION ALL
+SELECT _auto_id as startup_id, ts, ts_end, dur, package, startup_type
+FROM _startups_minsdk29
+WHERE (SELECT v from version) = 29
+UNION ALL
+SELECT startup_id, ts, ts_end, dur, package, startup_type
+FROM _startups_minsdk33
+WHERE (SELECT v from version) = 33;
 
 -- Create a table containing only the slices which are necessary for determining
 -- whether a startup happened.
diff --git a/src/trace_processor/perfetto_sql/stdlib/android/startup/startups_maxsdk28.sql b/src/trace_processor/perfetto_sql/stdlib/android/startup/startups_maxsdk28.sql
index 071968a..c1cc506 100644
--- a/src/trace_processor/perfetto_sql/stdlib/android/startup/startups_maxsdk28.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/android/startup/startups_maxsdk28.sql
@@ -13,9 +13,9 @@
 -- See the License for the specific language governing permissions and
 -- limitations under the License.
 
-INCLUDE PERFETTO MODULE slices.with_context;
-INCLUDE PERFETTO MODULE android.startup.startup_events;
 INCLUDE PERFETTO MODULE android.frames.timeline;
+INCLUDE PERFETTO MODULE android.startup.startup_events;
+INCLUDE PERFETTO MODULE slices.with_context;
 
 CREATE PERFETTO TABLE _startups_maxsdk28 AS
 -- Warm and cold starts only are based on the launching slice
@@ -42,29 +42,26 @@
   FROM thread_slice sl
   JOIN android_first_frame_after(sl.ts) rs
   WHERE name = 'activityResume'
-  AND sl.is_main_thread
-  -- Remove any launches here where the activityResume slices happens during
-  -- a warm/cold startup.
-  AND NOT EXISTS (
-    SELECT 1
-    FROM warm_and_cold wac
-    WHERE sl.ts BETWEEN wac.ts AND wac.ts_end
-    LIMIT 1)
+    AND sl.is_main_thread
+    -- Remove any launches here where the activityResume slices happens during
+    -- a warm/cold startup.
+    AND NOT EXISTS (
+      SELECT 1
+      FROM warm_and_cold wac
+      WHERE sl.ts BETWEEN wac.ts AND wac.ts_end
+      LIMIT 1
+    )
 ),
 cold_warm_hot AS (
   SELECT * FROM warm_and_cold
   UNION ALL
   SELECT * FROM maybe_hot
-
 )
 SELECT
-  "maxsdk28" AS sdk,
-  ROW_NUMBER() OVER(ORDER BY ts) AS startup_id,
   ts,
   ts_end,
   ts_end - ts AS dur,
   package,
   startup_type
-FROM cold_warm_hot;
-
-
+FROM cold_warm_hot
+ORDER BY ts;
diff --git a/src/trace_processor/perfetto_sql/stdlib/android/startup/startups_minsdk29.sql b/src/trace_processor/perfetto_sql/stdlib/android/startup/startups_minsdk29.sql
index fcc1fa9..7041bdd 100644
--- a/src/trace_processor/perfetto_sql/stdlib/android/startup/startups_minsdk29.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/android/startup/startups_minsdk29.sql
@@ -26,8 +26,6 @@
 -- activity starts.
 CREATE PERFETTO TABLE _activity_intent_recv_spans AS
 SELECT
-  ROW_NUMBER()
-  OVER(ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS startup_id,
   ts,
   LEAD(ts, 1, trace_end()) OVER(ORDER BY ts) - ts AS dur
 FROM _activity_intent_received
@@ -52,8 +50,6 @@
 -- is not reliable in the case of failed startups.
 CREATE PERFETTO TABLE _startups_minsdk29 AS
 SELECT
-  "minsdk29" as sdk,
-  lpart.startup_id,
   lpart.ts,
   le.ts_end,
   le.ts_end - lpart.ts AS dur,
@@ -67,4 +63,5 @@
   SELECT COUNT(1)
   FROM _activity_intent_startup_successful AS successful
   WHERE successful.ts BETWEEN lpart.ts AND lpart.ts + lpart.dur
-) > 0;
+) > 0
+ORDER BY lpart.ts;
diff --git a/src/trace_processor/perfetto_sql/stdlib/android/startup/startups_minsdk33.sql b/src/trace_processor/perfetto_sql/stdlib/android/startup/startups_minsdk33.sql
index b643808..2378473 100644
--- a/src/trace_processor/perfetto_sql/stdlib/android/startup/startups_minsdk33.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/android/startup/startups_minsdk33.sql
@@ -50,7 +50,6 @@
 
 CREATE PERFETTO TABLE _startups_minsdk33 AS
 SELECT
-  "minsdk33" as sdk,
   startup_id,
   ts,
   ts + dur AS ts_end,
@@ -59,6 +58,3 @@
   startup_type
 FROM _startup_async_events
 JOIN _startup_complete_events USING (startup_id);
-
-
-
diff --git a/src/trace_processor/perfetto_sql/stdlib/android/version.sql b/src/trace_processor/perfetto_sql/stdlib/android/version.sql
new file mode 100644
index 0000000..e827778
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/android/version.sql
@@ -0,0 +1,20 @@
+--
+-- Copyright 2023 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.
+
+CREATE PERFETTO FUNCTION _android_sdk_version()
+RETURNS INT AS
+SELECT int_value AS sdk_version
+FROM metadata
+WHERE name = 'android_sdk_version';
diff --git a/test/trace_processor/diff_tests/metrics/startup/android_startup.out b/test/trace_processor/diff_tests/metrics/startup/android_startup.out
index 1be03ba..24b7e85 100644
--- a/test/trace_processor/diff_tests/metrics/startup/android_startup.out
+++ b/test/trace_processor/diff_tests/metrics/startup/android_startup.out
@@ -1,6 +1,6 @@
 android_startup {
   startup {
-    startup_id: 2
+    startup_id: 0
     package_name: "com.google.android.calendar"
     process_name: "com.google.android.calendar"
     zygote_new_process: false
diff --git a/test/trace_processor/diff_tests/metrics/startup/android_startup_attribution.out b/test/trace_processor/diff_tests/metrics/startup/android_startup_attribution.out
index 510d9cd..0e00268 100644
--- a/test/trace_processor/diff_tests/metrics/startup/android_startup_attribution.out
+++ b/test/trace_processor/diff_tests/metrics/startup/android_startup_attribution.out
@@ -1,6 +1,6 @@
 android_startup {
   startup {
-    startup_id: 1
+    startup_id: 0
     package_name: "com.some.app"
     process_name: "com.some.app"
     zygote_new_process: false
diff --git a/test/trace_processor/diff_tests/metrics/startup/android_startup_attribution_slow.out b/test/trace_processor/diff_tests/metrics/startup/android_startup_attribution_slow.out
index eecb9ba..777e34f 100644
--- a/test/trace_processor/diff_tests/metrics/startup/android_startup_attribution_slow.out
+++ b/test/trace_processor/diff_tests/metrics/startup/android_startup_attribution_slow.out
@@ -1,6 +1,6 @@
 android_startup {
   startup {
-    startup_id: 1
+    startup_id: 0
     package_name: "com.some.app"
     process_name: "com.some.app"
     zygote_new_process: false
diff --git a/test/trace_processor/diff_tests/metrics/startup/android_startup_breakdown.out b/test/trace_processor/diff_tests/metrics/startup/android_startup_breakdown.out
index 35155e9..5fc494c 100644
--- a/test/trace_processor/diff_tests/metrics/startup/android_startup_breakdown.out
+++ b/test/trace_processor/diff_tests/metrics/startup/android_startup_breakdown.out
@@ -1,6 +1,6 @@
 android_startup {
   startup {
-    startup_id: 1
+    startup_id: 0
     package_name: "com.google.android.calendar"
     process_name: "com.google.android.calendar"
     zygote_new_process: true
diff --git a/test/trace_processor/diff_tests/metrics/startup/android_startup_breakdown_slow.out b/test/trace_processor/diff_tests/metrics/startup/android_startup_breakdown_slow.out
index fb937f3..ad0ed79 100644
--- a/test/trace_processor/diff_tests/metrics/startup/android_startup_breakdown_slow.out
+++ b/test/trace_processor/diff_tests/metrics/startup/android_startup_breakdown_slow.out
@@ -1,6 +1,6 @@
 android_startup {
   startup {
-    startup_id: 1
+    startup_id: 0
     package_name: "com.google.android.calendar"
     process_name: "com.google.android.calendar"
     zygote_new_process: true
diff --git a/test/trace_processor/diff_tests/metrics/startup/android_startup_process_track.out b/test/trace_processor/diff_tests/metrics/startup/android_startup_process_track.out
index 5c6548d..a0e8eca 100644
--- a/test/trace_processor/diff_tests/metrics/startup/android_startup_process_track.out
+++ b/test/trace_processor/diff_tests/metrics/startup/android_startup_process_track.out
@@ -1,6 +1,6 @@
 android_startup {
   startup {
-    startup_id: 1
+    startup_id: 0
     package_name: "com.google.android.calendar"
     process_name: "com.google.android.calendar:debug"
     zygote_new_process: false
@@ -83,7 +83,7 @@
     }
   }
   startup {
-    startup_id: 2
+    startup_id: 1
     package_name: "com.google.android.calendar"
     process_name: "com.google.android.calendar"
     zygote_new_process: false
diff --git a/test/trace_processor/diff_tests/metrics/startup/android_startup_slow.out b/test/trace_processor/diff_tests/metrics/startup/android_startup_slow.out
index 85a0281..58c91a5 100644
--- a/test/trace_processor/diff_tests/metrics/startup/android_startup_slow.out
+++ b/test/trace_processor/diff_tests/metrics/startup/android_startup_slow.out
@@ -1,6 +1,6 @@
 android_startup {
   startup {
-    startup_id: 2
+    startup_id: 0
     package_name: "com.google.android.calendar"
     process_name: "com.google.android.calendar"
     zygote_new_process: false
diff --git a/test/trace_processor/diff_tests/metrics/startup/ttid_and_ttfd.out b/test/trace_processor/diff_tests/metrics/startup/ttid_and_ttfd.out
index f5dcfcf..8999dd6 100644
--- a/test/trace_processor/diff_tests/metrics/startup/ttid_and_ttfd.out
+++ b/test/trace_processor/diff_tests/metrics/startup/ttid_and_ttfd.out
@@ -1,6 +1,6 @@
 android_startup {
     startup {
-        startup_id: 1
+        startup_id: 0
         package_name: "androidx.benchmark.integration.macrobenchmark.target"
         process_name: "androidx.benchmark.integration.macrobenchmark.target"
         zygote_new_process: false
diff --git a/test/trace_processor/diff_tests/stdlib/android/startups_tests.py b/test/trace_processor/diff_tests/stdlib/android/startups_tests.py
index 95ff515..c4f5a2e 100644
--- a/test/trace_processor/diff_tests/stdlib/android/startups_tests.py
+++ b/test/trace_processor/diff_tests/stdlib/android/startups_tests.py
@@ -30,7 +30,7 @@
         """,
         out=Csv("""
         "startup_id","ts","ts_end","dur","package","startup_type"
-        1,186969441973689,186969489302704,47329015,"androidx.benchmark.integration.macrobenchmark.target","[NULL]"
+        0,186969441973689,186969489302704,47329015,"androidx.benchmark.integration.macrobenchmark.target","[NULL]"
         """))
 
   def test_warm_startups(self):
@@ -42,7 +42,7 @@
         """,
         out=Csv("""
         "startup_id","ts","ts_end","dur","package","startup_type"
-        1,186982050780778,186982115528805,64748027,"androidx.benchmark.integration.macrobenchmark.target","[NULL]"
+        0,186982050780778,186982115528805,64748027,"androidx.benchmark.integration.macrobenchmark.target","[NULL]"
         """))
 
   def test_cold_startups(self):
@@ -54,7 +54,7 @@
         """,
         out=Csv("""
         "startup_id","ts","ts_end","dur","package","startup_type"
-        1,186974938196632,186975083989042,145792410,"androidx.benchmark.integration.macrobenchmark.target","[NULL]"
+        0,186974938196632,186975083989042,145792410,"androidx.benchmark.integration.macrobenchmark.target","[NULL]"
         """))
 
   def test_hot_startups_maxsdk28(self):
@@ -66,8 +66,8 @@
         """,
         out=Csv("""
         "startup_id","ts","ts_end","dur","package","startup_type"
-        1,779860286416,779893485322,33198906,"com.google.android.googlequicksearchbox","hot"
-        2,780778904571,780813944498,35039927,"androidx.benchmark.integration.macrobenchmark.target","hot"
+        0,779860286416,779893485322,33198906,"com.google.android.googlequicksearchbox","hot"
+        1,780778904571,780813944498,35039927,"androidx.benchmark.integration.macrobenchmark.target","hot"
         """))
 
   def test_warm_startups_maxsdk28(self):
@@ -79,8 +79,8 @@
         """,
         out=Csv("""
         "startup_id","ts","ts_end","dur","package","startup_type"
-        1,799979565075,800014194731,34629656,"com.google.android.googlequicksearchbox","hot"
-        2,800868511677,800981929562,113417885,"androidx.benchmark.integration.macrobenchmark.target","[NULL]"
+        0,799979565075,800014194731,34629656,"com.google.android.googlequicksearchbox","hot"
+        1,800868511677,800981929562,113417885,"androidx.benchmark.integration.macrobenchmark.target","[NULL]"
         """))
 
   def test_cold_startups_maxsdk28(self):
@@ -92,7 +92,7 @@
         """,
         out=Csv("""
         "startup_id","ts","ts_end","dur","package","startup_type"
-        1,791231114368,791501060868,269946500,"androidx.benchmark.integration.macrobenchmark.target","[NULL]"
+        0,791231114368,791501060868,269946500,"androidx.benchmark.integration.macrobenchmark.target","[NULL]"
         """))
 
   def test_android_startup_time_to_display_hot_maxsdk28(self):
@@ -104,8 +104,8 @@
         """,
         out=Csv("""
         "startup_id","time_to_initial_display","time_to_full_display","ttid_frame_id","ttfd_frame_id","upid"
-        1,33198906,"[NULL]",1,"[NULL]",355
-        2,35039927,537343160,4,5,383
+        0,33198906,"[NULL]",1,"[NULL]",355
+        1,35039927,537343160,4,5,383
         """))
 
   def test_android_startup_time_to_display_warm_maxsdk28(self):
@@ -117,8 +117,8 @@
         """,
         out=Csv("""
         "startup_id","time_to_initial_display","time_to_full_display","ttid_frame_id","ttfd_frame_id","upid"
-        1,34629656,"[NULL]",1,"[NULL]",355
-        2,108563770,581026583,4,5,388
+        0,34629656,"[NULL]",1,"[NULL]",355
+        1,108563770,581026583,4,5,388
         """))
 
   def test_android_startup_time_to_display_cold_maxsdk28(self):
@@ -130,7 +130,7 @@
         """,
         out=Csv("""
         "startup_id","time_to_initial_display","time_to_full_display","ttid_frame_id","ttfd_frame_id","upid"
-        1,264869885,715406822,65,66,396
+        0,264869885,715406822,65,66,396
         """))
 
   def test_android_startup_time_to_display_hot(self):
@@ -142,7 +142,7 @@
         """,
         out=Csv("""
         "startup_id","time_to_initial_display","time_to_full_display","ttid_frame_id","ttfd_frame_id","upid"
-        1,40534066,542222554,5872867,5872953,184
+        0,40534066,542222554,5872867,5872953,184
         """))
 
   def test_android_startup_time_to_display_warm(self):
@@ -154,7 +154,7 @@
         """,
         out=Csv("""
         "startup_id","time_to_initial_display","time_to_full_display","ttid_frame_id","ttfd_frame_id","upid"
-        1,62373965,555968701,5873800,5873889,185
+        0,62373965,555968701,5873800,5873889,185
         """))
 
   def test_android_startup_time_to_display_cold(self):
@@ -166,5 +166,5 @@
         """,
         out=Csv("""
         "startup_id","time_to_initial_display","time_to_full_display","ttid_frame_id","ttfd_frame_id","upid"
-        1,143980066,620815843,5873276,5873353,229
+        0,143980066,620815843,5873276,5873353,229
         """))