metrics: Add wattson estimate for marker window

Add wattson estimate for time window defined by wattson markers.

Test: tools/diff_test_trace_processor.py out/linux/trace_processor_shell --name-filter '.*wattson.*'
Bug: 352627297
Change-Id: I0a34ba8f6feca7f8951b88097bf33ae43f50cceb
Signed-off-by: Samuel Wu <wusamuel@google.com>
diff --git a/Android.bp b/Android.bp
index 8b8de37..e5c5c4c 100644
--- a/Android.bp
+++ b/Android.bp
@@ -12957,6 +12957,7 @@
         "src/trace_processor/metrics/sql/android/sysui_update_notif_on_ui_mode_changed_metric.sql",
         "src/trace_processor/metrics/sql/android/unsymbolized_frames.sql",
         "src/trace_processor/metrics/sql/android/wattson_app_startup_rails.sql",
+        "src/trace_processor/metrics/sql/android/wattson_markers_rails.sql",
         "src/trace_processor/metrics/sql/android/wattson_markers_threads.sql",
         "src/trace_processor/metrics/sql/android/wattson_rail_relations.sql",
         "src/trace_processor/metrics/sql/android/wattson_tasks_attribution.sql",
diff --git a/BUILD b/BUILD
index 1356efc..5c2393c 100644
--- a/BUILD
+++ b/BUILD
@@ -2206,6 +2206,7 @@
         "src/trace_processor/metrics/sql/android/sysui_update_notif_on_ui_mode_changed_metric.sql",
         "src/trace_processor/metrics/sql/android/unsymbolized_frames.sql",
         "src/trace_processor/metrics/sql/android/wattson_app_startup_rails.sql",
+        "src/trace_processor/metrics/sql/android/wattson_markers_rails.sql",
         "src/trace_processor/metrics/sql/android/wattson_markers_threads.sql",
         "src/trace_processor/metrics/sql/android/wattson_rail_relations.sql",
         "src/trace_processor/metrics/sql/android/wattson_tasks_attribution.sql",
diff --git a/protos/perfetto/metrics/metrics.proto b/protos/perfetto/metrics/metrics.proto
index 2436035..f663db0 100644
--- a/protos/perfetto/metrics/metrics.proto
+++ b/protos/perfetto/metrics/metrics.proto
@@ -124,7 +124,7 @@
 
 // Root message for all Perfetto-based metrics.
 //
-// Next id: 74
+// Next id: 75
 message TraceMetrics {
   reserved 4, 10, 13, 14, 16, 19;
 
@@ -334,6 +334,9 @@
   // Android Wattson thread attribution during markers time window.
   optional AndroidWattsonTasksAttributionMetric wattson_markers_threads = 73;
 
+  // Android Wattson estimate during markers time window.
+  optional AndroidWattsonTimePeriodMetric wattson_markers_rails = 74;
+
   // Android
   // Demo extensions.
   extensions 450 to 499;
diff --git a/protos/perfetto/metrics/perfetto_merged_metrics.proto b/protos/perfetto/metrics/perfetto_merged_metrics.proto
index 588c214..7ef002f 100644
--- a/protos/perfetto/metrics/perfetto_merged_metrics.proto
+++ b/protos/perfetto/metrics/perfetto_merged_metrics.proto
@@ -2998,7 +2998,7 @@
 
 // Root message for all Perfetto-based metrics.
 //
-// Next id: 74
+// Next id: 75
 message TraceMetrics {
   reserved 4, 10, 13, 14, 16, 19;
 
@@ -3208,6 +3208,9 @@
   // Android Wattson thread attribution during markers time window.
   optional AndroidWattsonTasksAttributionMetric wattson_markers_threads = 73;
 
+  // Android Wattson estimate during markers time window.
+  optional AndroidWattsonTimePeriodMetric wattson_markers_rails = 74;
+
   // Android
   // Demo extensions.
   extensions 450 to 499;
diff --git a/python/perfetto/trace_processor/metrics.descriptor b/python/perfetto/trace_processor/metrics.descriptor
index c5de567..7b9052d 100644
--- a/python/perfetto/trace_processor/metrics.descriptor
+++ b/python/perfetto/trace_processor/metrics.descriptor
Binary files differ
diff --git a/src/trace_processor/metrics/sql/android/BUILD.gn b/src/trace_processor/metrics/sql/android/BUILD.gn
index 4d2c537..04f3116 100644
--- a/src/trace_processor/metrics/sql/android/BUILD.gn
+++ b/src/trace_processor/metrics/sql/android/BUILD.gn
@@ -144,6 +144,7 @@
     "sysui_update_notif_on_ui_mode_changed_metric.sql",
     "unsymbolized_frames.sql",
     "wattson_app_startup_rails.sql",
+    "wattson_markers_rails.sql",
     "wattson_markers_threads.sql",
     "wattson_rail_relations.sql",
     "wattson_tasks_attribution.sql",
diff --git a/src/trace_processor/metrics/sql/android/wattson_markers_rails.sql b/src/trace_processor/metrics/sql/android/wattson_markers_rails.sql
new file mode 100644
index 0000000..e96ad8d
--- /dev/null
+++ b/src/trace_processor/metrics/sql/android/wattson_markers_rails.sql
@@ -0,0 +1,47 @@
+
+-- 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.
+
+INCLUDE PERFETTO MODULE wattson.curves.ungrouped;
+
+DROP VIEW IF EXISTS _wattson_period_windows;
+CREATE PERFETTO VIEW _wattson_period_windows AS
+SELECT
+  -- Requirement is there is exactly one pair of start/stop
+  (SELECT ts FROM slice WHERE name == 'wattson_start') as ts,
+  (SELECT ts FROM slice WHERE name == 'wattson_stop')
+  - (SELECT ts FROM slice WHERE name == 'wattson_start') as dur,
+  1 as period_id;
+
+SELECT RUN_METRIC(
+  'android/wattson_rail_relations.sql',
+  'window_table',
+  '_wattson_period_windows'
+);
+
+DROP VIEW IF EXISTS wattson_markers_rails_output;
+CREATE PERFETTO VIEW wattson_markers_rails_output AS
+SELECT AndroidWattsonTimePeriodMetric(
+  'metric_version', 2,
+  'period_info', (
+    SELECT RepeatedField(
+      AndroidWattsonEstimateInfo(
+        'period_id', period_id,
+        'period_dur', period_dur,
+        'cpu_subsystem', proto
+      )
+    )
+    FROM _estimate_cpu_subsystem_sum
+  )
+);
diff --git a/test/trace_processor/diff_tests/metrics/android/tests.py b/test/trace_processor/diff_tests/metrics/android/tests.py
index fafea14..6908a0b 100644
--- a/test/trace_processor/diff_tests/metrics/android/tests.py
+++ b/test/trace_processor/diff_tests/metrics/android/tests.py
@@ -474,3 +474,38 @@
         trace=DataPath('wattson_w_packages_Imarkers.pb'),
         query=Metric("wattson_markers_threads"),
         out=Path('wattson_markers_threads.out'))
+
+  def test_wattson_markers_rails_output(self):
+    return DiffTestBlueprint(
+        trace=DataPath('wattson_w_packages_Imarkers.pb'),
+        query=Metric("wattson_markers_rails"),
+        out=Csv("""
+        wattson_markers_rails {
+          metric_version: 2
+          period_info {
+            period_id: 1
+            period_dur: 2031870211
+            cpu_subsystem {
+              estimate_mw: 46.524994
+              policy0 {
+                estimate_mw: 34.021542
+                cpu0 {
+                  estimate_mw: 14.416650
+                }
+                cpu1 {
+                  estimate_mw: 6.641433
+                }
+                cpu2 {
+                  estimate_mw: 8.134795
+                }
+                cpu3 {
+                  estimate_mw: 4.828665
+                }
+              }
+              dsu_scu {
+                estimate_mw: 12.503452
+              }
+            }
+          }
+        }
+        """))