tp: replace task_state creation in startup metric with thread state

This signifcantly reduces the memory usage on large traces.

Bug: 181237261
Change-Id: Ibf425bc1918beb4ac84d365628d2f6bd689e2363
diff --git a/Android.bp b/Android.bp
index 838eb79..6202698 100644
--- a/Android.bp
+++ b/Android.bp
@@ -7724,7 +7724,6 @@
     "src/trace_processor/metrics/android/android_surfaceflinger.sql",
     "src/trace_processor/metrics/android/android_sysui_cuj.sql",
     "src/trace_processor/metrics/android/android_task_names.sql",
-    "src/trace_processor/metrics/android/android_task_state.sql",
     "src/trace_processor/metrics/android/android_thread_time_in_state.sql",
     "src/trace_processor/metrics/android/composition_layers.sql",
     "src/trace_processor/metrics/android/cpu_info.sql",
diff --git a/BUILD b/BUILD
index 9a6cd9e..f8a5942 100644
--- a/BUILD
+++ b/BUILD
@@ -921,7 +921,6 @@
         "src/trace_processor/metrics/android/android_surfaceflinger.sql",
         "src/trace_processor/metrics/android/android_sysui_cuj.sql",
         "src/trace_processor/metrics/android/android_task_names.sql",
-        "src/trace_processor/metrics/android/android_task_state.sql",
         "src/trace_processor/metrics/android/android_thread_time_in_state.sql",
         "src/trace_processor/metrics/android/composition_layers.sql",
         "src/trace_processor/metrics/android/cpu_info.sql",
diff --git a/src/trace_processor/metrics/BUILD.gn b/src/trace_processor/metrics/BUILD.gn
index c20d65a..3b6f5f5 100644
--- a/src/trace_processor/metrics/BUILD.gn
+++ b/src/trace_processor/metrics/BUILD.gn
@@ -37,7 +37,6 @@
   "android/android_powrails.sql",
   "android/android_proxy_power.sql",
   "android/android_startup_launches.sql",
-  "android/android_task_state.sql",
   "android/android_startup.sql",
   "android/android_package_list.sql",
   "android/android_task_names.sql",
diff --git a/src/trace_processor/metrics/android/android_startup.sql b/src/trace_processor/metrics/android/android_startup.sql
index 0652471..77745c4 100644
--- a/src/trace_processor/metrics/android/android_startup.sql
+++ b/src/trace_processor/metrics/android/android_startup.sql
@@ -16,7 +16,6 @@
 
 -- Create the base tables and views containing the launch spans.
 SELECT RUN_METRIC('android/android_startup_launches.sql');
-SELECT RUN_METRIC('android/android_task_state.sql');
 SELECT RUN_METRIC('android/process_metadata.sql');
 SELECT RUN_METRIC('android/hsc_startups.sql');
 
@@ -54,11 +53,20 @@
 JOIN thread ON (process.upid = thread.upid AND process.pid = thread.tid)
 ORDER BY ts;
 
+DROP VIEW IF EXISTS thread_state_extended;
+CREATE VIEW thread_state_extended AS
+SELECT
+  ts,
+  IIF(dur = -1, (SELECT end_ts FROM trace_bounds), dur) AS dur,
+  utid,
+  state
+FROM thread_state;
+
 DROP TABLE IF EXISTS main_thread_state;
 CREATE VIRTUAL TABLE main_thread_state
 USING SPAN_JOIN(
   launch_main_threads PARTITIONED utid,
-  task_state PARTITIONED utid);
+  thread_state_extended PARTITIONED utid);
 
 DROP VIEW IF EXISTS launch_by_thread_state;
 CREATE VIEW launch_by_thread_state AS
@@ -185,22 +193,22 @@
         'running_dur_ns', IFNULL(
             (
             SELECT dur FROM launch_by_thread_state l
-            WHERE l.launch_id = launches.id AND state = 'running'
+            WHERE l.launch_id = launches.id AND state = 'Running'
             ), 0),
         'runnable_dur_ns', IFNULL(
             (
             SELECT dur FROM launch_by_thread_state l
-            WHERE l.launch_id = launches.id AND state = 'runnable'
+            WHERE l.launch_id = launches.id AND state = 'R'
             ), 0),
         'uninterruptible_sleep_dur_ns', IFNULL(
             (
             SELECT dur FROM launch_by_thread_state l
-            WHERE l.launch_id = launches.id AND state = 'uninterruptible'
+            WHERE l.launch_id = launches.id AND (state = 'D' or state = 'DK')
             ), 0),
         'interruptible_sleep_dur_ns', IFNULL(
             (
             SELECT dur FROM launch_by_thread_state l
-            WHERE l.launch_id = launches.id AND state = 'interruptible'
+            WHERE l.launch_id = launches.id AND state = 'S'
             ), 0)
       ),
       'to_post_fork', (
diff --git a/src/trace_processor/metrics/android/android_task_state.sql b/src/trace_processor/metrics/android/android_task_state.sql
deleted file mode 100644
index 48b51a6..0000000
--- a/src/trace_processor/metrics/android/android_task_state.sql
+++ /dev/null
@@ -1,39 +0,0 @@
---
--- Copyright 2019 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.
---
-
--- Spans for each thread not in a running state.
-DROP TABLE IF EXISTS unsched_state;
-CREATE TABLE unsched_state (ts BIG INT, dur BIG INT, utid BIG INT, state STRING);
-
-INSERT INTO unsched_state
-SELECT
-  ts_end AS ts,
-  LEAD(ts, 1, null) OVER(PARTITION BY utid ORDER BY ts) - ts_end AS dur,
-  utid,
-  CASE
-    WHEN INSTR(end_state, 'R') > 0 THEN 'runnable'
-    WHEN INSTR(end_state, 'D') > 0 THEN 'uninterruptible'
-    WHEN INSTR(end_state, 'S') > 0 THEN 'interruptible'
-  END AS state
-FROM sched;
-
--- Create a single view for the task states.
-DROP VIEW IF EXISTS task_state;
-CREATE VIEW task_state AS
-SELECT utid, state, ts, dur FROM unsched_state
-UNION
-SELECT utid, 'running', ts, dur FROM sched
-ORDER BY ts;
diff --git a/test/trace_processor/startup/android_startup.out b/test/trace_processor/startup/android_startup.out
index b9f7628..578295a 100644
--- a/test/trace_processor/startup/android_startup.out
+++ b/test/trace_processor/startup/android_startup.out
@@ -23,7 +23,7 @@
       dur_ns: 108
       main_thread_by_task_state {
         running_dur_ns: 41
-        runnable_dur_ns: 49
+        runnable_dur_ns: 129
         uninterruptible_sleep_dur_ns: 0
         interruptible_sleep_dur_ns: 10
       }