Support ion_stat

ion/ion_stat is the new tracepoint tracking ion buffer allocations for 4.19+ kernels.
It deprecates ion_heap_grow / ion_heap_shrink.

This change ensures the existing metrics work for the ion_stat events.
Given we no longer support the specific heap name, we use 'all' as the placeholder

Bug: 156492506
Change-Id: I9b9dbe92a342fab26db9e2cd3d93fc8d0bb310d9
diff --git a/src/trace_processor/metrics/android/android_ion.sql b/src/trace_processor/metrics/android/android_ion.sql
index 98a6fc9..b2a72fe 100644
--- a/src/trace_processor/metrics/android/android_ion.sql
+++ b/src/trace_processor/metrics/android/android_ion.sql
@@ -19,12 +19,15 @@
   ts,
   LEAD(ts, 1, (SELECT end_ts FROM trace_bounds))
     OVER(PARTITION BY track_id ORDER BY ts) - ts AS dur,
-  SUBSTR(name, 9) AS heap_name,
+  CASE name
+    WHEN 'mem.ion' THEN 'all'
+    ELSE SUBSTR(name, 9)
+  END AS heap_name,
   track_id,
   value
 FROM counter JOIN counter_track
   ON counter.track_id = counter_track.id
-WHERE name LIKE 'mem.ion.%';
+WHERE (name LIKE 'mem.ion.%' OR name = 'mem.ion');
 
 CREATE VIEW IF NOT EXISTS ion_heap_stats AS
 SELECT
@@ -37,14 +40,17 @@
 
 CREATE VIEW IF NOT EXISTS ion_raw_allocs AS
 SELECT
-  SUBSTR(name, 16) AS heap_name,
+  CASE name
+    WHEN 'mem.ion_change' THEN 'all'
+    ELSE SUBSTR(name, 16)
+  END AS heap_name,
   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 LIKE 'mem.ion_change.%' AND value > 0
+WHERE (name LIKE 'mem.ion_change.%' OR name = 'mem.ion_change') AND value > 0
 WINDOW win AS (
-  PARTITION BY SUBSTR(name, 16) ORDER BY ts
+  PARTITION BY name ORDER BY ts
   ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
 );
 
diff --git a/test/metrics/android_ion_stat.out b/test/metrics/android_ion_stat.out
new file mode 100644
index 0000000..f9ac727
--- /dev/null
+++ b/test/metrics/android_ion_stat.out
@@ -0,0 +1,9 @@
+android_ion {
+  buffer {
+    name: "all"
+    avg_size_bytes: 2000.0
+    min_size_bytes: 1000.0
+    max_size_bytes: 2000.0
+    total_alloc_size_bytes: 1000.0
+  }
+}
\ No newline at end of file
diff --git a/test/metrics/android_ion_stat.textproto b/test/metrics/android_ion_stat.textproto
new file mode 100644
index 0000000..e573712
--- /dev/null
+++ b/test/metrics/android_ion_stat.textproto
@@ -0,0 +1,28 @@
+packet {
+  ftrace_events {
+    cpu: 0
+    event {
+      timestamp: 100
+      pid: 1
+      ion_stat {
+        buffer_id: 123
+        len: 1000
+        total_allocated: 2000
+      }
+    }
+  }
+}
+packet {
+  ftrace_events {
+    cpu: 0
+    event {
+      timestamp: 200
+      pid: 1
+      ion_stat {
+        buffer_id: 123
+        len: -1000
+        total_allocated: 1000
+      }
+    }
+  }
+}
diff --git a/test/metrics/index b/test/metrics/index
index 5483ba4..3dcc53f 100644
--- a/test/metrics/index
+++ b/test/metrics/index
@@ -9,6 +9,7 @@
 ../trace_processor/oom_kill.textproto android_lmk android_lmk_oom.out
 
 android_ion.py android_ion android_ion.out
+android_ion_stat.textproto android_ion android_ion_stat.out
 
 android_startup.py android_startup android_startup.out
 android_startup_breakdown.py android_startup android_startup_breakdown.out