tp: Migrate time in state tables from sched/states

Change-Id: I92ab2d80bb34d0b73a34ca64c7d3ab166c38de94
diff --git a/Android.bp b/Android.bp
index bd2375b..5ad4d69 100644
--- a/Android.bp
+++ b/Android.bp
@@ -12064,6 +12064,7 @@
         "src/trace_processor/perfetto_sql/stdlib/sched/thread_executing_span.sql",
         "src/trace_processor/perfetto_sql/stdlib/sched/thread_level_parallelism.sql",
         "src/trace_processor/perfetto_sql/stdlib/sched/thread_state_flattened.sql",
+        "src/trace_processor/perfetto_sql/stdlib/sched/time_in_state.sql",
         "src/trace_processor/perfetto_sql/stdlib/sched/utilization/general.sql",
         "src/trace_processor/perfetto_sql/stdlib/sched/utilization/process.sql",
         "src/trace_processor/perfetto_sql/stdlib/sched/utilization/system.sql",
diff --git a/BUILD b/BUILD
index 592c1ec..643be9d 100644
--- a/BUILD
+++ b/BUILD
@@ -2509,6 +2509,7 @@
         "src/trace_processor/perfetto_sql/stdlib/sched/thread_executing_span.sql",
         "src/trace_processor/perfetto_sql/stdlib/sched/thread_level_parallelism.sql",
         "src/trace_processor/perfetto_sql/stdlib/sched/thread_state_flattened.sql",
+        "src/trace_processor/perfetto_sql/stdlib/sched/time_in_state.sql",
     ],
 )
 
diff --git a/src/trace_processor/perfetto_sql/stdlib/sched/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/sched/BUILD.gn
index 642b822..bd2dc94 100644
--- a/src/trace_processor/perfetto_sql/stdlib/sched/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/sched/BUILD.gn
@@ -21,5 +21,6 @@
     "thread_executing_span.sql",
     "thread_level_parallelism.sql",
     "thread_state_flattened.sql",
+    "time_in_state.sql",
   ]
 }
diff --git a/src/trace_processor/perfetto_sql/stdlib/sched/states.sql b/src/trace_processor/perfetto_sql/stdlib/sched/states.sql
index 0a7c7ee..37f759a 100644
--- a/src/trace_processor/perfetto_sql/stdlib/sched/states.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/sched/states.sql
@@ -69,81 +69,4 @@
     WHEN 0 THEN ' (non-IO)'
     ELSE ''
   END
-);
-
--- The time a thread spent in each scheduling state during it's lifetime.
-CREATE PERFETTO TABLE sched_thread_time_in_state(
-  -- Utid of the thread.
-  utid INT,
-  -- Total runtime of thread.
-  total_runtime INT,
-  -- One of the scheduling states of kernel thread.
-  state STRING,
-  -- Total time spent in the scheduling state.
-  time_in_state INT,
-  -- Percentage of time thread spent in scheduling state in [0-100] range.
-  percentage_in_state INT
-) AS
-WITH total_dur AS (
-  SELECT
-    utid,
-    sum(dur) AS sum_dur
-  FROM thread_state
-  GROUP BY 1
-),
-summed AS (
-  SELECT
-    utid,
-    state,
-    sum(dur) AS time_in_state
-  FROM thread_state group by 1, 2
-)
-SELECT
-  utid,
-  sum_dur AS total_runtime,
-  state,
-  time_in_state,
-  (time_in_state*100)/(sum_dur) AS percentage_in_state
-FROM summed JOIN total_dur USING (utid);
-
-CREATE PERFETTO MACRO _case_for_state(state Expr)
-RETURNS Expr AS
-MAX(CASE WHEN state = $state THEN percentage_in_state END);
-
--- Summary of time spent by thread in each scheduling state, in percentage ([0, 100]
--- ranges). Sum of all states might be smaller than 100, as those values
--- are rounded down.
-CREATE PERFETTO TABLE sched_percentage_of_time_in_state(
-  -- Utid of the thread.
-  utid INT,
-  -- Percentage of time thread spent in running ('Running') state in [0, 100]
-  -- range.
-  running INT,
-  -- Percentage of time thread spent in runnable ('R') state in [0, 100]
-  -- range.
-  runnable INT,
-  -- Percentage of time thread spent in preempted runnable ('R+') state in
-  -- [0, 100] range.
-  runnable_preempted INT,
-  -- Percentage of time thread spent in sleeping ('S') state in [0, 100] range.
-  sleeping INT,
-  -- Percentage of time thread spent in uninterruptible sleep ('D') state in
-  -- [0, 100] range.
-  uninterruptible_sleep INT,
-  -- Percentage of time thread spent in other ('T', 't', 'X', 'Z', 'x', 'I',
-  -- 'K', 'W', 'P', 'N') states in [0, 100] range.
-  other INT
-) AS
-SELECT
-  utid,
-  _case_for_state!('Running') AS running,
-  _case_for_state!('R') AS runnable,
-  _case_for_state!('R+') AS runnable_preempted,
-  _case_for_state!('S') AS sleeping,
-  _case_for_state!('D') AS uninterruptible_sleep,
-  SUM(
-    CASE WHEN state IN ('T', 't', 'X', 'Z', 'x', 'I', 'K', 'W', 'P', 'N')
-    THEN time_in_state END
-  ) * 100/total_runtime AS other
-FROM sched_thread_time_in_state
-GROUP BY utid;
\ No newline at end of file
+);
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/sched/time_in_state.sql b/src/trace_processor/perfetto_sql/stdlib/sched/time_in_state.sql
new file mode 100644
index 0000000..6f9b3a3
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/sched/time_in_state.sql
@@ -0,0 +1,91 @@
+--
+-- Copyright 2024 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.
+
+-- The time a thread spent in each scheduling state during it's lifetime.
+CREATE PERFETTO TABLE sched_thread_time_in_state(
+  -- Utid of the thread.
+  utid INT,
+  -- Total runtime of thread.
+  total_runtime INT,
+  -- One of the scheduling states of kernel thread.
+  state STRING,
+  -- Total time spent in the scheduling state.
+  time_in_state INT,
+  -- Percentage of time thread spent in scheduling state in [0-100] range.
+  percentage_in_state INT
+) AS
+WITH total_dur AS (
+  SELECT
+    utid,
+    sum(dur) AS sum_dur
+  FROM thread_state
+  GROUP BY 1
+),
+summed AS (
+  SELECT
+    utid,
+    state,
+    sum(dur) AS time_in_state
+  FROM thread_state group by 1, 2
+)
+SELECT
+  utid,
+  sum_dur AS total_runtime,
+  state,
+  time_in_state,
+  (time_in_state*100)/(sum_dur) AS percentage_in_state
+FROM summed JOIN total_dur USING (utid);
+
+CREATE PERFETTO MACRO _case_for_state(state Expr)
+RETURNS Expr AS
+MAX(CASE WHEN state = $state THEN percentage_in_state END);
+
+-- Summary of time spent by thread in each scheduling state, in percentage ([0, 100]
+-- ranges). Sum of all states might be smaller than 100, as those values
+-- are rounded down.
+CREATE PERFETTO TABLE sched_percentage_of_time_in_state(
+  -- Utid of the thread.
+  utid INT,
+  -- Percentage of time thread spent in running ('Running') state in [0, 100]
+  -- range.
+  running INT,
+  -- Percentage of time thread spent in runnable ('R') state in [0, 100]
+  -- range.
+  runnable INT,
+  -- Percentage of time thread spent in preempted runnable ('R+') state in
+  -- [0, 100] range.
+  runnable_preempted INT,
+  -- Percentage of time thread spent in sleeping ('S') state in [0, 100] range.
+  sleeping INT,
+  -- Percentage of time thread spent in uninterruptible sleep ('D') state in
+  -- [0, 100] range.
+  uninterruptible_sleep INT,
+  -- Percentage of time thread spent in other ('T', 't', 'X', 'Z', 'x', 'I',
+  -- 'K', 'W', 'P', 'N') states in [0, 100] range.
+  other INT
+) AS
+SELECT
+  utid,
+  _case_for_state!('Running') AS running,
+  _case_for_state!('R') AS runnable,
+  _case_for_state!('R+') AS runnable_preempted,
+  _case_for_state!('S') AS sleeping,
+  _case_for_state!('D') AS uninterruptible_sleep,
+  SUM(
+    CASE WHEN state IN ('T', 't', 'X', 'Z', 'x', 'I', 'K', 'W', 'P', 'N')
+    THEN time_in_state END
+  ) * 100/total_runtime AS other
+FROM sched_thread_time_in_state
+GROUP BY utid;
\ No newline at end of file
diff --git a/test/trace_processor/diff_tests/stdlib/sched/tests.py b/test/trace_processor/diff_tests/stdlib/sched/tests.py
index 48a8814..2438dea 100644
--- a/test/trace_processor/diff_tests/stdlib/sched/tests.py
+++ b/test/trace_processor/diff_tests/stdlib/sched/tests.py
@@ -166,7 +166,7 @@
     return DiffTestBlueprint(
         trace=DataPath('example_android_trace_30s.pb'),
         query="""
-        INCLUDE PERFETTO MODULE sched.states;
+        INCLUDE PERFETTO MODULE sched.time_in_state;
 
         SELECT *
         FROM sched_thread_time_in_state
@@ -191,7 +191,7 @@
     return DiffTestBlueprint(
         trace=DataPath('example_android_trace_30s.pb'),
         query="""
-        INCLUDE PERFETTO MODULE sched.states;
+        INCLUDE PERFETTO MODULE sched.time_in_state;
 
         SELECT *
         FROM sched_percentage_of_time_in_state