Merge "debian: Add traced_perf into Debian package"
diff --git a/Android.bp b/Android.bp
index 8c9f234..d954988 100644
--- a/Android.bp
+++ b/Android.bp
@@ -3559,6 +3559,7 @@
     "protos/perfetto/metrics/android/display_metrics.proto",
     "protos/perfetto/metrics/android/gpu_metric.proto",
     "protos/perfetto/metrics/android/heap_profile_callsites.proto",
+    "protos/perfetto/metrics/android/hwcomposer.proto",
     "protos/perfetto/metrics/android/hwui_metric.proto",
     "protos/perfetto/metrics/android/ion_metric.proto",
     "protos/perfetto/metrics/android/java_heap_histogram.proto",
@@ -3599,6 +3600,7 @@
     "protos/perfetto/metrics/android/display_metrics.proto",
     "protos/perfetto/metrics/android/gpu_metric.proto",
     "protos/perfetto/metrics/android/heap_profile_callsites.proto",
+    "protos/perfetto/metrics/android/hwcomposer.proto",
     "protos/perfetto/metrics/android/hwui_metric.proto",
     "protos/perfetto/metrics/android/ion_metric.proto",
     "protos/perfetto/metrics/android/java_heap_histogram.proto",
@@ -7517,6 +7519,7 @@
     "src/trace_processor/metrics/android/android_cpu_agg.sql",
     "src/trace_processor/metrics/android/android_cpu_raw_metrics_per_core.sql",
     "src/trace_processor/metrics/android/android_gpu.sql",
+    "src/trace_processor/metrics/android/android_hwcomposer.sql",
     "src/trace_processor/metrics/android/android_hwui_metric.sql",
     "src/trace_processor/metrics/android/android_ion.sql",
     "src/trace_processor/metrics/android/android_lmk.sql",
@@ -7533,6 +7536,7 @@
     "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",
     "src/trace_processor/metrics/android/display_metrics.sql",
     "src/trace_processor/metrics/android/frame_missed.sql",
diff --git a/BUILD b/BUILD
index 3303844..68cead7 100644
--- a/BUILD
+++ b/BUILD
@@ -884,6 +884,7 @@
         "src/trace_processor/metrics/android/android_cpu_agg.sql",
         "src/trace_processor/metrics/android/android_cpu_raw_metrics_per_core.sql",
         "src/trace_processor/metrics/android/android_gpu.sql",
+        "src/trace_processor/metrics/android/android_hwcomposer.sql",
         "src/trace_processor/metrics/android/android_hwui_metric.sql",
         "src/trace_processor/metrics/android/android_ion.sql",
         "src/trace_processor/metrics/android/android_lmk.sql",
@@ -900,6 +901,7 @@
         "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",
         "src/trace_processor/metrics/android/display_metrics.sql",
         "src/trace_processor/metrics/android/frame_missed.sql",
@@ -2279,6 +2281,7 @@
         "protos/perfetto/metrics/android/display_metrics.proto",
         "protos/perfetto/metrics/android/gpu_metric.proto",
         "protos/perfetto/metrics/android/heap_profile_callsites.proto",
+        "protos/perfetto/metrics/android/hwcomposer.proto",
         "protos/perfetto/metrics/android/hwui_metric.proto",
         "protos/perfetto/metrics/android/ion_metric.proto",
         "protos/perfetto/metrics/android/java_heap_histogram.proto",
diff --git a/protos/perfetto/metrics/android/BUILD.gn b/protos/perfetto/metrics/android/BUILD.gn
index 82eb6cb..de8f70b 100644
--- a/protos/perfetto/metrics/android/BUILD.gn
+++ b/protos/perfetto/metrics/android/BUILD.gn
@@ -25,6 +25,7 @@
     "display_metrics.proto",
     "gpu_metric.proto",
     "heap_profile_callsites.proto",
+    "hwcomposer.proto",
     "hwui_metric.proto",
     "ion_metric.proto",
     "java_heap_histogram.proto",
diff --git a/protos/perfetto/metrics/android/hwcomposer.proto b/protos/perfetto/metrics/android/hwcomposer.proto
new file mode 100644
index 0000000..22644ce
--- /dev/null
+++ b/protos/perfetto/metrics/android/hwcomposer.proto
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2021 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
+ *
+ *      http://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.
+ */
+
+syntax = "proto2";
+
+package perfetto.protos;
+
+message AndroidHwcomposerMetrics {
+  // Counts the number of composition total layers in the trace. (non-weighted average)
+  optional double composition_total_layers = 1;
+
+  // Counts the number of composition dpu layers in the trace. (non-weighted average)
+  optional double composition_dpu_layers = 2;
+
+  // Counts the number of composition gpu layers in the trace. (non-weighted average)
+  optional double composition_gpu_layers = 3;
+
+  // Counts the number of composition dpu cached layers in the trace. (non-weighted average)
+  optional double composition_dpu_cached_layers = 4;
+}
diff --git a/protos/perfetto/metrics/metrics.proto b/protos/perfetto/metrics/metrics.proto
index 244629c..8986b58 100644
--- a/protos/perfetto/metrics/metrics.proto
+++ b/protos/perfetto/metrics/metrics.proto
@@ -23,6 +23,7 @@
 import "protos/perfetto/metrics/android/display_metrics.proto";
 import "protos/perfetto/metrics/android/gpu_metric.proto";
 import "protos/perfetto/metrics/android/heap_profile_callsites.proto";
+import "protos/perfetto/metrics/android/hwcomposer.proto";
 import "protos/perfetto/metrics/android/hwui_metric.proto";
 import "protos/perfetto/metrics/android/ion_metric.proto";
 import "protos/perfetto/metrics/android/java_heap_histogram.proto";
@@ -55,7 +56,7 @@
 
 // Root message for all Perfetto-based metrics.
 //
-// Next id: 28
+// Next id: 29
 message TraceMetrics {
   reserved 4, 10, 13, 14, 19;
 
@@ -124,6 +125,9 @@
   // Frame timing and jank root causes for system UI interactions.
   optional AndroidSysUiCujMetrics android_sysui_cuj = 27;
 
+  // Metric associated with hwcomposer.
+  optional AndroidHwcomposerMetrics android_hwcomposer = 28;
+
   // 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 2d3e51f..36ac53a 100644
--- a/protos/perfetto/metrics/perfetto_merged_metrics.proto
+++ b/protos/perfetto/metrics/perfetto_merged_metrics.proto
@@ -251,6 +251,24 @@
 
 // End of protos/perfetto/metrics/android/heap_profile_callsites.proto
 
+// Begin of protos/perfetto/metrics/android/hwcomposer.proto
+
+message AndroidHwcomposerMetrics {
+  // Counts the number of composition total layers in the trace. (non-weighted average)
+  optional double composition_total_layers = 1;
+
+  // Counts the number of composition dpu layers in the trace. (non-weighted average)
+  optional double composition_dpu_layers = 2;
+
+  // Counts the number of composition gpu layers in the trace. (non-weighted average)
+  optional double composition_gpu_layers = 3;
+
+  // Counts the number of composition dpu cached layers in the trace. (non-weighted average)
+  optional double composition_dpu_cached_layers = 4;
+}
+
+// End of protos/perfetto/metrics/android/hwcomposer.proto
+
 // Begin of protos/perfetto/metrics/android/hwui_metric.proto
 
 // Android HWUI graphics performance and graphics memory usage metrics.
@@ -811,7 +829,7 @@
 
 // Root message for all Perfetto-based metrics.
 //
-// Next id: 28
+// Next id: 29
 message TraceMetrics {
   reserved 4, 10, 13, 14, 19;
 
@@ -880,6 +898,9 @@
   // Frame timing and jank root causes for system UI interactions.
   optional AndroidSysUiCujMetrics android_sysui_cuj = 27;
 
+  // Metric associated with hwcomposer.
+  optional AndroidHwcomposerMetrics android_hwcomposer = 28;
+
   // Demo extensions.
   extensions 450 to 499;
 
diff --git a/src/trace_processor/metrics/BUILD.gn b/src/trace_processor/metrics/BUILD.gn
index 0017914..b7bef7b 100644
--- a/src/trace_processor/metrics/BUILD.gn
+++ b/src/trace_processor/metrics/BUILD.gn
@@ -26,6 +26,7 @@
   "android/android_mem.sql",
   "android/android_mem_unagg.sql",
   "android/android_ion.sql",
+  "android/composition_layers.sql",
   "android/frame_missed.sql",
   "android/android_lmk_reason.sql",
   "android/android_lmk.sql",
@@ -41,6 +42,7 @@
   "android/display_metrics.sql",
   "android/heap_profile_callsites.sql",
   "android/hsc_startups.sql",
+  "android/android_hwcomposer.sql",
   "android/android_hwui_metric.sql",
   "android/java_heap_histogram.sql",
   "android/java_heap_stats.sql",
diff --git a/src/trace_processor/metrics/android/android_hwcomposer.sql b/src/trace_processor/metrics/android/android_hwcomposer.sql
new file mode 100644
index 0000000..769c659
--- /dev/null
+++ b/src/trace_processor/metrics/android/android_hwcomposer.sql
@@ -0,0 +1,50 @@
+--
+-- Copyright 2021 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.
+
+SELECT RUN_METRIC(
+  'android/composition_layers.sql',
+  'track_name', 'HWComposer: Total Layer',
+  'output', 'total_layers'
+);
+
+SELECT RUN_METRIC(
+  'android/composition_layers.sql',
+  'track_name', 'HWComposer: DPU Layer',
+  'output', 'dpu_layers'
+);
+
+SELECT RUN_METRIC(
+  'android/composition_layers.sql',
+  'track_name', 'HWComposer: GPU Layer',
+  'output', 'gpu_layers'
+);
+
+SELECT RUN_METRIC(
+  'android/composition_layers.sql',
+  'track_name', 'HWComposer: Cached Layer',
+  'output', 'cached_layers'
+);
+
+CREATE VIEW android_hwcomposer_output AS
+SELECT AndroidHwcomposerMetrics(
+    'composition_total_layers', (SELECT AVG(value)
+                            FROM total_layers),
+    'composition_dpu_layers', (SELECT AVG(value)
+                            FROM dpu_layers),
+    'composition_gpu_layers', (SELECT AVG(value)
+                            FROM gpu_layers),
+    'composition_dpu_cached_layers', (SELECT AVG(value)
+                            FROM cached_layers)
+);
diff --git a/src/trace_processor/metrics/android/composition_layers.sql b/src/trace_processor/metrics/android/composition_layers.sql
new file mode 100644
index 0000000..f63a6d3
--- /dev/null
+++ b/src/trace_processor/metrics/android/composition_layers.sql
@@ -0,0 +1,29 @@
+--
+-- Copyright 2021 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 VIEW IF NOT EXISTS {{output}} AS
+WITH composition_layer_counts AS (
+  SELECT
+    LAG(ts) OVER (ORDER BY ts) AS ts,
+    value
+  FROM counter c
+  JOIN process_counter_track t ON c.track_id = t.id
+  WHERE t.name = '{{track_name}}'
+)
+SELECT
+  ts,
+  value
+FROM composition_layer_counts
+WHERE value >= 0 AND ts IS NOT NULL;
diff --git a/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor b/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor
index c973e4c..659de3c 100644
--- a/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor
+++ b/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor
Binary files differ
diff --git a/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor.sha1 b/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor.sha1
index 427c6ca..dd4358f 100644
--- a/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor.sha1
+++ b/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor.sha1
@@ -2,4 +2,4 @@
 // SHA1(tools/gen_binary_descriptors)
 // 70978e4b6e0d773dd222715b1c7e74c25d344da0
 // SHA1(protos/perfetto/metrics/metrics.proto)
-// 88089959b7be61227a18d46e91e635df4fca535c
+// e0033c91fc2ae9cd9730bbf5684f5661c16e4d5c
diff --git a/test/trace_processor/graphics/composition_layer.py b/test/trace_processor/graphics/composition_layer.py
new file mode 100644
index 0000000..f25aec6
--- /dev/null
+++ b/test/trace_processor/graphics/composition_layer.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+# Copyright (C) 2021 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
+#
+#      http://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.
+
+# This synthetic trace tests handling of the mm_id field in the rss_stat
+# event when mm_structs are reused on process death.
+
+from os import sys, path
+
+import synth_common
+
+trace = synth_common.create_trace()
+
+trace.add_packet(ts=1)
+trace.add_process(10, 1, "parent_process")
+trace.add_process(11, 10, "child_process")
+
+trace.add_ftrace_packet(1)
+
+trace.add_print(ts=99, tid=11, buf='C|10|HWComposer: Total Layer|7')
+trace.add_print(ts=100, tid=11, buf='C|10|HWComposer: Total Layer|5')
+trace.add_print(ts=101, tid=11, buf='C|10|HWComposer: Total Layer|6')
+trace.add_print(ts=102, tid=11, buf='C|10|HWComposer: Total Layer|0')
+trace.add_print(ts=103, tid=11, buf='C|10|HWComposer: Total Layer|4')
+trace.add_print(ts=104, tid=11, buf='C|10|HWComposer: Total Layer|6')
+trace.add_print(ts=105, tid=11, buf='C|10|HWComposer: Total Layer|0')
+trace.add_print(ts=106, tid=11, buf='C|10|HWComposer: Total Layer|4')
+trace.add_print(ts=107, tid=11, buf='C|10|HWComposer: Total Layer|5')
+trace.add_print(ts=108, tid=11, buf='C|10|HWComposer: Total Layer|0')
+trace.add_print(ts=108, tid=11, buf='C|10|HWComposer: Total Layer|3')
+trace.add_print(ts=108, tid=11, buf='C|10|HWComposer: Total Layer|0')
+
+sys.stdout.buffer.write(trace.trace.SerializeToString())
diff --git a/test/trace_processor/graphics/composition_layer_count.out b/test/trace_processor/graphics/composition_layer_count.out
new file mode 100644
index 0000000..6c47b9e
--- /dev/null
+++ b/test/trace_processor/graphics/composition_layer_count.out
@@ -0,0 +1,3 @@
+
+"AVG(value)"
+3.000000
diff --git a/test/trace_processor/graphics/composition_layer_count.sql b/test/trace_processor/graphics/composition_layer_count.sql
new file mode 100644
index 0000000..30a92e6
--- /dev/null
+++ b/test/trace_processor/graphics/composition_layer_count.sql
@@ -0,0 +1,19 @@
+--
+-- Copyright 2021 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.
+
+SELECT RUN_METRIC('android/android_hwcomposer.sql') AS suppress_query_output;
+
+SELECT AVG(value)
+FROM total_layers;
diff --git a/test/trace_processor/graphics/index b/test/trace_processor/graphics/index
index c6e21c0..256e72d 100644
--- a/test/trace_processor/graphics/index
+++ b/test/trace_processor/graphics/index
@@ -29,3 +29,6 @@
 # Frame Timeline event trace tests
 frame_timeline_events.py expected_frame_timeline_events.sql expected_frame_timeline_events.out
 frame_timeline_events.py actual_frame_timeline_events.sql actual_frame_timeline_events.out
+
+# Composition layer
+composition_layer.py composition_layer_count.sql composition_layer_count.out
diff --git a/tools/trace_to_text/trace_to_profile.cc b/tools/trace_to_text/trace_to_profile.cc
index 5a5d261..a394e2b 100644
--- a/tools/trace_to_text/trace_to_profile.cc
+++ b/tools/trace_to_text/trace_to_profile.cc
@@ -16,6 +16,7 @@
 
 #include "tools/trace_to_text/trace_to_profile.h"
 
+#include <random>
 #include <string>
 #include <vector>
 
@@ -49,6 +50,17 @@
 namespace trace_to_text {
 namespace {
 
+std::string GetRandomString(size_t n) {
+  std::random_device r;
+  auto rng = std::default_random_engine(r());
+  std::uniform_int_distribution<char> dist('a', 'z');
+  std::string result(n, ' ');
+  for (size_t i = 0; i < n; ++i) {
+    result[i] = dist(rng);
+  }
+  return result;
+}
+
 void MaybeSymbolize(trace_processor::TraceProcessor* tp) {
   std::unique_ptr<profiling::Symbolizer> symbolizer =
       profiling::LocalSymbolizerOrDie(profiling::GetPerfettoBinaryPath(),
@@ -99,8 +111,8 @@
     return 0;
   }
 
-  std::string temp_dir =
-      GetTemp() + "/" + dirname_prefix + base::GetTimeFmt("%y%m%d%H%M%S");
+  std::string temp_dir = GetTemp() + "/" + dirname_prefix +
+                         base::GetTimeFmt("%y%m%d%H%M%S") + GetRandomString(5);
   PERFETTO_CHECK(base::Mkdir(temp_dir));
   for (const auto& profile : profiles) {
     std::string filename = temp_dir + "/" + filename_fn(profile);