Add DMA-BUF heap perfetto metrics

Add average/total/min/max metrics for DMA-BUF heaps.

Test: out/ftrace_proto_gen_build/trace_processor_shell  --run-metrics
android_dma_heap mytrace
Test: tools/diff_test_trace_processor.py
--trace-filter='android_dma_heap'
out/ftrace_proto_gen_build/trace_processor_shell

Change-Id: Iebff526bc28ab5bc8630bbe851b07fc6e72b4714
diff --git a/Android.bp b/Android.bp
index c83a523..d0eb9b0 100644
--- a/Android.bp
+++ b/Android.bp
@@ -3634,6 +3634,7 @@
     "protos/perfetto/metrics/android/batt_metric.proto",
     "protos/perfetto/metrics/android/cpu_metric.proto",
     "protos/perfetto/metrics/android/display_metrics.proto",
+    "protos/perfetto/metrics/android/dma_heap_metric.proto",
     "protos/perfetto/metrics/android/fastrpc_metric.proto",
     "protos/perfetto/metrics/android/g2d_metric.proto",
     "protos/perfetto/metrics/android/gpu_metric.proto",
@@ -3680,6 +3681,7 @@
     "protos/perfetto/metrics/android/batt_metric.proto",
     "protos/perfetto/metrics/android/cpu_metric.proto",
     "protos/perfetto/metrics/android/display_metrics.proto",
+    "protos/perfetto/metrics/android/dma_heap_metric.proto",
     "protos/perfetto/metrics/android/fastrpc_metric.proto",
     "protos/perfetto/metrics/android/g2d_metric.proto",
     "protos/perfetto/metrics/android/gpu_metric.proto",
@@ -7702,6 +7704,7 @@
     "src/trace_processor/metrics/android/android_cpu.sql",
     "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_dma_heap.sql",
     "src/trace_processor/metrics/android/android_fastrpc.sql",
     "src/trace_processor/metrics/android/android_gpu.sql",
     "src/trace_processor/metrics/android/android_hwcomposer.sql",
diff --git a/BUILD b/BUILD
index 804e98d..3b09ac8 100644
--- a/BUILD
+++ b/BUILD
@@ -901,6 +901,7 @@
         "src/trace_processor/metrics/android/android_cpu.sql",
         "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_dma_heap.sql",
         "src/trace_processor/metrics/android/android_fastrpc.sql",
         "src/trace_processor/metrics/android/android_gpu.sql",
         "src/trace_processor/metrics/android/android_hwcomposer.sql",
@@ -2353,6 +2354,7 @@
         "protos/perfetto/metrics/android/batt_metric.proto",
         "protos/perfetto/metrics/android/cpu_metric.proto",
         "protos/perfetto/metrics/android/display_metrics.proto",
+        "protos/perfetto/metrics/android/dma_heap_metric.proto",
         "protos/perfetto/metrics/android/fastrpc_metric.proto",
         "protos/perfetto/metrics/android/g2d_metric.proto",
         "protos/perfetto/metrics/android/gpu_metric.proto",
diff --git a/protos/perfetto/metrics/android/BUILD.gn b/protos/perfetto/metrics/android/BUILD.gn
index 1896e32..514901b 100644
--- a/protos/perfetto/metrics/android/BUILD.gn
+++ b/protos/perfetto/metrics/android/BUILD.gn
@@ -23,6 +23,7 @@
     "batt_metric.proto",
     "cpu_metric.proto",
     "display_metrics.proto",
+    "dma_heap_metric.proto",
     "fastrpc_metric.proto",
     "g2d_metric.proto",
     "gpu_metric.proto",
diff --git a/protos/perfetto/metrics/android/dma_heap_metric.proto b/protos/perfetto/metrics/android/dma_heap_metric.proto
new file mode 100644
index 0000000..c9f95c2
--- /dev/null
+++ b/protos/perfetto/metrics/android/dma_heap_metric.proto
@@ -0,0 +1,30 @@
+/*
+ * 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;
+
+// dma-buf heap memory stats on Android.
+message AndroidDmaHeapMetric {
+    optional double avg_size_bytes = 1;
+    optional double min_size_bytes = 2;
+    optional double max_size_bytes = 3;
+
+    // Total allocation size.
+    // Essentially the sum of positive allocs.
+    optional double total_alloc_size_bytes = 4;
+}
diff --git a/protos/perfetto/metrics/metrics.proto b/protos/perfetto/metrics/metrics.proto
index a572fcb..ccff68c 100644
--- a/protos/perfetto/metrics/metrics.proto
+++ b/protos/perfetto/metrics/metrics.proto
@@ -21,6 +21,7 @@
 import "protos/perfetto/metrics/android/batt_metric.proto";
 import "protos/perfetto/metrics/android/cpu_metric.proto";
 import "protos/perfetto/metrics/android/display_metrics.proto";
+import "protos/perfetto/metrics/android/dma_heap_metric.proto";
 import "protos/perfetto/metrics/android/fastrpc_metric.proto";
 import "protos/perfetto/metrics/android/g2d_metric.proto";
 import "protos/perfetto/metrics/android/gpu_metric.proto";
@@ -60,7 +61,7 @@
 
 // Root message for all Perfetto-based metrics.
 //
-// Next id: 32
+// Next id: 33
 message TraceMetrics {
   reserved 4, 10, 13, 14, 19;
 
@@ -141,6 +142,9 @@
   // G2D metrics.
   optional G2dMetrics g2d = 30;
 
+  // Dmabuf heap metrics.
+  optional AndroidDmaHeapMetric android_dma_heap = 32;
+
   // 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 dd63c8f..678c85e 100644
--- a/protos/perfetto/metrics/perfetto_merged_metrics.proto
+++ b/protos/perfetto/metrics/perfetto_merged_metrics.proto
@@ -137,6 +137,21 @@
 
 // End of protos/perfetto/metrics/android/display_metrics.proto
 
+// Begin of protos/perfetto/metrics/android/dma_heap_metric.proto
+
+// dma-buf heap memory stats on Android.
+message AndroidDmaHeapMetric {
+    optional double avg_size_bytes = 1;
+    optional double min_size_bytes = 2;
+    optional double max_size_bytes = 3;
+
+    // Total allocation size.
+    // Essentially the sum of positive allocs.
+    optional double total_alloc_size_bytes = 4;
+}
+
+// End of protos/perfetto/metrics/android/dma_heap_metric.proto
+
 // Begin of protos/perfetto/metrics/android/fastrpc_metric.proto
 
 // fastrpc memory stats on Android.
@@ -947,7 +962,7 @@
 
 // Root message for all Perfetto-based metrics.
 //
-// Next id: 32
+// Next id: 33
 message TraceMetrics {
   reserved 4, 10, 13, 14, 19;
 
@@ -1028,6 +1043,9 @@
   // G2D metrics.
   optional G2dMetrics g2d = 30;
 
+  // Dmabuf heap metrics.
+  optional AndroidDmaHeapMetric android_dma_heap = 32;
+
   // Demo extensions.
   extensions 450 to 499;
 
diff --git a/src/trace_processor/metrics/BUILD.gn b/src/trace_processor/metrics/BUILD.gn
index a63b8e7..c20d65a 100644
--- a/src/trace_processor/metrics/BUILD.gn
+++ b/src/trace_processor/metrics/BUILD.gn
@@ -22,6 +22,7 @@
   "android/android_surfaceflinger.sql",
   "android/android_cpu_agg.sql",
   "android/android_cpu_raw_metrics_per_core.sql",
+  "android/android_dma_heap.sql",
   "android/android_fastrpc.sql",
   "android/android_gpu.sql",
   "android/android_hwui_threads.sql",
diff --git a/src/trace_processor/metrics/android/android_dma_heap.sql b/src/trace_processor/metrics/android/android_dma_heap.sql
new file mode 100644
index 0000000..afdab08
--- /dev/null
+++ b/src/trace_processor/metrics/android/android_dma_heap.sql
@@ -0,0 +1,78 @@
+--
+-- 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.
+--
+
+DROP VIEW IF EXISTS dma_heap_timeline;
+CREATE VIEW dma_heap_timeline AS
+SELECT
+  ts,
+  LEAD(ts, 1, (SELECT end_ts FROM trace_bounds))
+    OVER(PARTITION BY track_id ORDER BY ts) - ts AS dur,
+  track_id,
+  value
+FROM counter JOIN counter_track
+  ON counter.track_id = counter_track.id
+WHERE (name = 'mem.dma_heap');
+
+DROP VIEW IF EXISTS dma_heap_stats;
+CREATE VIEW dma_heap_stats AS
+SELECT
+  SUM(value * dur) / SUM(dur) AS avg_size,
+  MIN(value) AS min_size,
+  MAX(value) AS max_size
+FROM dma_heap_timeline;
+
+DROP VIEW IF EXISTS dma_heap_raw_allocs;
+CREATE VIEW dma_heap_raw_allocs AS
+SELECT
+  ts,
+  value AS instant_value,
+  SUM(value) OVER win AS value
+FROM counter c JOIN thread_counter_track t ON c.track_id = t.id
+WHERE (name = 'mem.dma_heap_change') AND value > 0
+WINDOW win AS (
+  PARTITION BY name ORDER BY ts
+  ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
+);
+
+DROP VIEW IF EXISTS dma_heap_total_stats;
+CREATE VIEW dma_heap_total_stats AS
+SELECT
+  SUM(instant_value) AS total_alloc_size_bytes
+FROM dma_heap_raw_allocs;
+
+-- We need to group by ts here as we can have two ion events from
+-- different processes occurring at the same timestamp. We take the
+-- max as this will take both allocations into account at that
+-- timestamp.
+DROP VIEW IF EXISTS android_dma_heap_event;
+CREATE VIEW android_dma_heap_event AS
+SELECT
+  'counter' AS track_type,
+  printf('Buffers created from DMA-BUF heaps: ') AS track_name,
+  ts,
+  MAX(value) AS value
+FROM dma_heap_raw_allocs
+GROUP BY 1, 2, 3;
+
+DROP VIEW IF EXISTS android_dma_heap_output;
+CREATE VIEW android_dma_heap_output AS
+SELECT AndroidDmaHeapMetric(
+      'avg_size_bytes', avg_size,
+      'min_size_bytes', min_size,
+      'max_size_bytes', max_size,
+      'total_alloc_size_bytes', total_alloc_size_bytes
+  )
+FROM dma_heap_stats JOIN dma_heap_total_stats;
diff --git a/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor b/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor
index 7ad0b1d..c1af116 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 f65e8d0..4f63bb1 100644
--- a/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor.sha1
+++ b/src/trace_processor/python/perfetto/trace_processor/metrics.descriptor.sha1
@@ -2,5 +2,5 @@
 // SHA1(tools/gen_binary_descriptors)
 // 30f9a74885dae344b1a42f7ba94d8909c9d07ad0
 // SHA1(protos/perfetto/metrics/metrics.proto)
-// fa31abee83a994781f70cb6a2af1427ebad3a465
+// 166ccca20db3d62751907ad690c7bba09e37cb31
   
\ No newline at end of file
diff --git a/test/trace_processor/memory/android_dma_heap_stat.out b/test/trace_processor/memory/android_dma_heap_stat.out
new file mode 100644
index 0000000..13366ed
--- /dev/null
+++ b/test/trace_processor/memory/android_dma_heap_stat.out
@@ -0,0 +1,6 @@
+android_dma_heap {
+    avg_size_bytes: 2000.0
+    min_size_bytes: 1000.0
+    max_size_bytes: 2000.0
+    total_alloc_size_bytes: 1000.0
+}
diff --git a/test/trace_processor/memory/android_dma_heap_stat.textproto b/test/trace_processor/memory/android_dma_heap_stat.textproto
new file mode 100644
index 0000000..ddbfa82
--- /dev/null
+++ b/test/trace_processor/memory/android_dma_heap_stat.textproto
@@ -0,0 +1,28 @@
+packet {
+  ftrace_events {
+    cpu: 0
+    event {
+      timestamp: 100
+      pid: 1
+      dma_heap_stat {
+        inode: 123
+        len: 1000
+        total_allocated: 2000
+      }
+    }
+  }
+}
+packet {
+  ftrace_events {
+    cpu: 0
+    event {
+      timestamp: 200
+      pid: 1
+      dma_heap_stat {
+        inode: 123
+        len: -1000
+        total_allocated: 1000
+      }
+    }
+  }
+}
diff --git a/test/trace_processor/memory/index b/test/trace_processor/memory/index
index 6763dd2..b836149 100644
--- a/test/trace_processor/memory/index
+++ b/test/trace_processor/memory/index
@@ -12,5 +12,8 @@
 android_ion.py android_ion android_ion.out
 android_ion_stat.textproto android_ion android_ion_stat.out
 
+# DMA-BUF heap Metric
+android_dma_heap_stat.textproto android_dma_heap android_dma_heap_stat.out
+
 # fastrpc metric
 android_fastrpc_dma_stat.textproto android_fastrpc android_fastrpc_dma_stat.out
diff --git a/ui/src/controller/trace_controller.ts b/ui/src/controller/trace_controller.ts
index 26365fd..a7b1e38 100644
--- a/ui/src/controller/trace_controller.ts
+++ b/ui/src/controller/trace_controller.ts
@@ -496,6 +496,7 @@
     for (const metric
              of ['android_startup',
                  'android_ion',
+                 'android_dma_heap',
                  'android_thread_time_in_state',
                  'android_surfaceflinger',
                  'android_batt',