Add ftrace support for DMA-BUF heap tracepoints

These changes add ftrace support for DMA-BUF heap tracepoints.

Test: out/ftrace_proto_gen_build/trace_processor_shell  mytrace
Change-Id: I6fe8c28de30c4f8a86243b55ebf3498efc2881d0
diff --git a/Android.bp b/Android.bp
index 6d2115d..c83a523 100644
--- a/Android.bp
+++ b/Android.bp
@@ -4126,6 +4126,7 @@
     "protos/perfetto/trace/ftrace/clk.proto",
     "protos/perfetto/trace/ftrace/compaction.proto",
     "protos/perfetto/trace/ftrace/cpuhp.proto",
+    "protos/perfetto/trace/ftrace/dmabuf_heap.proto",
     "protos/perfetto/trace/ftrace/dpu.proto",
     "protos/perfetto/trace/ftrace/ext4.proto",
     "protos/perfetto/trace/ftrace/f2fs.proto",
@@ -4176,6 +4177,7 @@
     "external/perfetto/protos/perfetto/trace/ftrace/clk.gen.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/compaction.gen.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/cpuhp.gen.cc",
+    "external/perfetto/protos/perfetto/trace/ftrace/dmabuf_heap.gen.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/dpu.gen.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/ext4.gen.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/f2fs.gen.cc",
@@ -4226,6 +4228,7 @@
     "protos/perfetto/trace/ftrace/clk.proto",
     "protos/perfetto/trace/ftrace/compaction.proto",
     "protos/perfetto/trace/ftrace/cpuhp.proto",
+    "protos/perfetto/trace/ftrace/dmabuf_heap.proto",
     "protos/perfetto/trace/ftrace/dpu.proto",
     "protos/perfetto/trace/ftrace/ext4.proto",
     "protos/perfetto/trace/ftrace/f2fs.proto",
@@ -4276,6 +4279,7 @@
     "external/perfetto/protos/perfetto/trace/ftrace/clk.gen.h",
     "external/perfetto/protos/perfetto/trace/ftrace/compaction.gen.h",
     "external/perfetto/protos/perfetto/trace/ftrace/cpuhp.gen.h",
+    "external/perfetto/protos/perfetto/trace/ftrace/dmabuf_heap.gen.h",
     "external/perfetto/protos/perfetto/trace/ftrace/dpu.gen.h",
     "external/perfetto/protos/perfetto/trace/ftrace/ext4.gen.h",
     "external/perfetto/protos/perfetto/trace/ftrace/f2fs.gen.h",
@@ -4330,6 +4334,7 @@
     "protos/perfetto/trace/ftrace/clk.proto",
     "protos/perfetto/trace/ftrace/compaction.proto",
     "protos/perfetto/trace/ftrace/cpuhp.proto",
+    "protos/perfetto/trace/ftrace/dmabuf_heap.proto",
     "protos/perfetto/trace/ftrace/dpu.proto",
     "protos/perfetto/trace/ftrace/ext4.proto",
     "protos/perfetto/trace/ftrace/f2fs.proto",
@@ -4379,6 +4384,7 @@
     "external/perfetto/protos/perfetto/trace/ftrace/clk.pb.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/compaction.pb.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/cpuhp.pb.cc",
+    "external/perfetto/protos/perfetto/trace/ftrace/dmabuf_heap.pb.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/dpu.pb.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/ext4.pb.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/f2fs.pb.cc",
@@ -4429,6 +4435,7 @@
     "protos/perfetto/trace/ftrace/clk.proto",
     "protos/perfetto/trace/ftrace/compaction.proto",
     "protos/perfetto/trace/ftrace/cpuhp.proto",
+    "protos/perfetto/trace/ftrace/dmabuf_heap.proto",
     "protos/perfetto/trace/ftrace/dpu.proto",
     "protos/perfetto/trace/ftrace/ext4.proto",
     "protos/perfetto/trace/ftrace/f2fs.proto",
@@ -4478,6 +4485,7 @@
     "external/perfetto/protos/perfetto/trace/ftrace/clk.pb.h",
     "external/perfetto/protos/perfetto/trace/ftrace/compaction.pb.h",
     "external/perfetto/protos/perfetto/trace/ftrace/cpuhp.pb.h",
+    "external/perfetto/protos/perfetto/trace/ftrace/dmabuf_heap.pb.h",
     "external/perfetto/protos/perfetto/trace/ftrace/dpu.pb.h",
     "external/perfetto/protos/perfetto/trace/ftrace/ext4.pb.h",
     "external/perfetto/protos/perfetto/trace/ftrace/f2fs.pb.h",
@@ -4532,6 +4540,7 @@
     "protos/perfetto/trace/ftrace/clk.proto",
     "protos/perfetto/trace/ftrace/compaction.proto",
     "protos/perfetto/trace/ftrace/cpuhp.proto",
+    "protos/perfetto/trace/ftrace/dmabuf_heap.proto",
     "protos/perfetto/trace/ftrace/dpu.proto",
     "protos/perfetto/trace/ftrace/ext4.proto",
     "protos/perfetto/trace/ftrace/f2fs.proto",
@@ -4582,6 +4591,7 @@
     "external/perfetto/protos/perfetto/trace/ftrace/clk.pbzero.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/compaction.pbzero.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/cpuhp.pbzero.cc",
+    "external/perfetto/protos/perfetto/trace/ftrace/dmabuf_heap.pbzero.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/dpu.pbzero.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/ext4.pbzero.cc",
     "external/perfetto/protos/perfetto/trace/ftrace/f2fs.pbzero.cc",
@@ -4632,6 +4642,7 @@
     "protos/perfetto/trace/ftrace/clk.proto",
     "protos/perfetto/trace/ftrace/compaction.proto",
     "protos/perfetto/trace/ftrace/cpuhp.proto",
+    "protos/perfetto/trace/ftrace/dmabuf_heap.proto",
     "protos/perfetto/trace/ftrace/dpu.proto",
     "protos/perfetto/trace/ftrace/ext4.proto",
     "protos/perfetto/trace/ftrace/f2fs.proto",
@@ -4682,6 +4693,7 @@
     "external/perfetto/protos/perfetto/trace/ftrace/clk.pbzero.h",
     "external/perfetto/protos/perfetto/trace/ftrace/compaction.pbzero.h",
     "external/perfetto/protos/perfetto/trace/ftrace/cpuhp.pbzero.h",
+    "external/perfetto/protos/perfetto/trace/ftrace/dmabuf_heap.pbzero.h",
     "external/perfetto/protos/perfetto/trace/ftrace/dpu.pbzero.h",
     "external/perfetto/protos/perfetto/trace/ftrace/ext4.pbzero.h",
     "external/perfetto/protos/perfetto/trace/ftrace/f2fs.pbzero.h",
diff --git a/BUILD b/BUILD
index 7d25fc3..804e98d 100644
--- a/BUILD
+++ b/BUILD
@@ -2563,6 +2563,7 @@
         "protos/perfetto/trace/ftrace/clk.proto",
         "protos/perfetto/trace/ftrace/compaction.proto",
         "protos/perfetto/trace/ftrace/cpuhp.proto",
+        "protos/perfetto/trace/ftrace/dmabuf_heap.proto",
         "protos/perfetto/trace/ftrace/dpu.proto",
         "protos/perfetto/trace/ftrace/ext4.proto",
         "protos/perfetto/trace/ftrace/f2fs.proto",
diff --git a/protos/perfetto/trace/ftrace/all_protos.gni b/protos/perfetto/trace/ftrace/all_protos.gni
index b65e892..8910853 100644
--- a/protos/perfetto/trace/ftrace/all_protos.gni
+++ b/protos/perfetto/trace/ftrace/all_protos.gni
@@ -26,6 +26,7 @@
   "clk.proto",
   "compaction.proto",
   "cpuhp.proto",
+  "dmabuf_heap.proto",
   "dpu.proto",
   "ext4.proto",
   "f2fs.proto",
diff --git a/protos/perfetto/trace/ftrace/dmabuf_heap.proto b/protos/perfetto/trace/ftrace/dmabuf_heap.proto
new file mode 100644
index 0000000..5b7e70f
--- /dev/null
+++ b/protos/perfetto/trace/ftrace/dmabuf_heap.proto
@@ -0,0 +1,12 @@
+// Autogenerated by:
+// ../../tools/ftrace_proto_gen/ftrace_proto_gen.cc
+// Do not edit.
+
+syntax = "proto2";
+package perfetto.protos;
+
+message DmaHeapStatFtraceEvent {
+  optional uint64 inode = 1;
+  optional int64 len = 2;
+  optional uint64 total_allocated = 3;
+}
diff --git a/protos/perfetto/trace/ftrace/ftrace_event.proto b/protos/perfetto/trace/ftrace/ftrace_event.proto
index 46b419c..e8ac22a 100644
--- a/protos/perfetto/trace/ftrace/ftrace_event.proto
+++ b/protos/perfetto/trace/ftrace/ftrace_event.proto
@@ -26,6 +26,7 @@
 import "protos/perfetto/trace/ftrace/clk.proto";
 import "protos/perfetto/trace/ftrace/compaction.proto";
 import "protos/perfetto/trace/ftrace/cpuhp.proto";
+import "protos/perfetto/trace/ftrace/dmabuf_heap.proto";
 import "protos/perfetto/trace/ftrace/dpu.proto";
 import "protos/perfetto/trace/ftrace/ext4.proto";
 import "protos/perfetto/trace/ftrace/f2fs.proto";
@@ -438,5 +439,6 @@
     DpuTracingMarkWriteFtraceEvent dpu_tracing_mark_write = 348;
     G2dTracingMarkWriteFtraceEvent g2d_tracing_mark_write = 349;
     MaliTracingMarkWriteFtraceEvent mali_tracing_mark_write = 350;
+    DmaHeapStatFtraceEvent dma_heap_stat = 351;
   }
 }
diff --git a/protos/perfetto/trace/perfetto_trace.proto b/protos/perfetto/trace/perfetto_trace.proto
index cbeaed0..6b01cd3 100644
--- a/protos/perfetto/trace/perfetto_trace.proto
+++ b/protos/perfetto/trace/perfetto_trace.proto
@@ -3101,6 +3101,16 @@
 
 // End of protos/perfetto/trace/ftrace/cpuhp.proto
 
+// Begin of protos/perfetto/trace/ftrace/dmabuf_heap.proto
+
+message DmaHeapStatFtraceEvent {
+  optional uint64 inode = 1;
+  optional int64 len = 2;
+  optional uint64 total_allocated = 3;
+}
+
+// End of protos/perfetto/trace/ftrace/dmabuf_heap.proto
+
 // Begin of protos/perfetto/trace/ftrace/dpu.proto
 
 message DpuTracingMarkWriteFtraceEvent {
@@ -5360,6 +5370,7 @@
     DpuTracingMarkWriteFtraceEvent dpu_tracing_mark_write = 348;
     G2dTracingMarkWriteFtraceEvent g2d_tracing_mark_write = 349;
     MaliTracingMarkWriteFtraceEvent mali_tracing_mark_write = 350;
+    DmaHeapStatFtraceEvent dma_heap_stat = 351;
   }
 }
 
diff --git a/src/trace_processor/importers/ftrace/ftrace_parser.cc b/src/trace_processor/importers/ftrace/ftrace_parser.cc
index 752fd06..3059502 100644
--- a/src/trace_processor/importers/ftrace/ftrace_parser.cc
+++ b/src/trace_processor/importers/ftrace/ftrace_parser.cc
@@ -29,6 +29,7 @@
 
 #include "protos/perfetto/common/gpu_counter_descriptor.pbzero.h"
 #include "protos/perfetto/trace/ftrace/binder.pbzero.h"
+#include "protos/perfetto/trace/ftrace/dmabuf_heap.pbzero.h"
 #include "protos/perfetto/trace/ftrace/dpu.pbzero.h"
 #include "protos/perfetto/trace/ftrace/fastrpc.pbzero.h"
 #include "protos/perfetto/trace/ftrace/ftrace.pbzero.h"
@@ -104,6 +105,9 @@
       cpu_idle_name_id_(context->storage->InternString("cpuidle")),
       ion_total_id_(context->storage->InternString("mem.ion")),
       ion_change_id_(context->storage->InternString("mem.ion_change")),
+      dma_heap_total_id_(context->storage->InternString("mem.dma_heap")),
+      dma_heap_change_id_(
+          context->storage->InternString("mem.dma_heap_change")),
       ion_total_unknown_id_(context->storage->InternString("mem.ion.unknown")),
       ion_change_unknown_id_(
           context->storage->InternString("mem.ion_change.unknown")),
@@ -398,6 +402,10 @@
         ParseIonStat(ts, pid, data);
         break;
       }
+      case FtraceEvent::kDmaHeapStatFieldNumber: {
+        ParseDmaHeapStat(ts, pid, data);
+        break;
+      }
       case FtraceEvent::kSignalGenerateFieldNumber: {
         ParseSignalGenerate(ts, data);
         break;
@@ -903,6 +911,25 @@
   context_->event_tracker->PushCounter(ts, static_cast<double>(ion.len()),
                                        track);
 }
+void FtraceParser::ParseDmaHeapStat(int64_t ts,
+                                    uint32_t pid,
+                                    protozero::ConstBytes data) {
+  protos::pbzero::DmaHeapStatFtraceEvent::Decoder dma_heap(data.data,
+                                                           data.size);
+  // Push the global counter.
+  TrackId track =
+      context_->track_tracker->InternGlobalCounterTrack(dma_heap_total_id_);
+  context_->event_tracker->PushCounter(
+      ts, static_cast<double>(dma_heap.total_allocated()), track);
+
+  // Push the change counter.
+  // TODO(b/121331269): these should really be instant events.
+  UniqueTid utid = context_->process_tracker->GetOrCreateThread(pid);
+  track = context_->track_tracker->InternThreadCounterTrack(dma_heap_change_id_,
+                                                            utid);
+  context_->event_tracker->PushCounter(ts, static_cast<double>(dma_heap.len()),
+                                       track);
+}
 
 // This event has both the pid of the thread that sent the signal and the
 // destination of the signal. Currently storing the pid of the destination.
diff --git a/src/trace_processor/importers/ftrace/ftrace_parser.h b/src/trace_processor/importers/ftrace/ftrace_parser.h
index e790b96..466ac3f 100644
--- a/src/trace_processor/importers/ftrace/ftrace_parser.h
+++ b/src/trace_processor/importers/ftrace/ftrace_parser.h
@@ -74,6 +74,7 @@
                                 protozero::ConstBytes,
                                 bool grow);
   void ParseIonStat(int64_t ts, uint32_t pid, protozero::ConstBytes);
+  void ParseDmaHeapStat(int64_t ts, uint32_t pid, protozero::ConstBytes);
   void ParseSignalGenerate(int64_t ts, protozero::ConstBytes);
   void ParseSignalDeliver(int64_t ts, uint32_t pid, protozero::ConstBytes);
   void ParseLowmemoryKill(int64_t ts, protozero::ConstBytes);
@@ -152,6 +153,8 @@
   const StringId cpu_idle_name_id_;
   const StringId ion_total_id_;
   const StringId ion_change_id_;
+  const StringId dma_heap_total_id_;
+  const StringId dma_heap_change_id_;
   const StringId ion_total_unknown_id_;
   const StringId ion_change_unknown_id_;
   const StringId signal_generate_id_;
diff --git a/src/traced/probes/ftrace/event_info.cc b/src/traced/probes/ftrace/event_info.cc
index e3195ae..0bd85ba 100644
--- a/src/traced/probes/ftrace/event_info.cc
+++ b/src/traced/probes/ftrace/event_info.cc
@@ -1085,6 +1085,22 @@
        kUnsetFtraceId,
        346,
        kUnsetSize},
+      {"dma_heap_stat",
+       "dmabuf_heap",
+       {
+           {kUnsetOffset, kUnsetSize, FtraceFieldType::kInvalidFtraceFieldType,
+            "inode", 1, ProtoSchemaType::kUint64,
+            TranslationStrategy::kInvalidTranslationStrategy},
+           {kUnsetOffset, kUnsetSize, FtraceFieldType::kInvalidFtraceFieldType,
+            "len", 2, ProtoSchemaType::kInt64,
+            TranslationStrategy::kInvalidTranslationStrategy},
+           {kUnsetOffset, kUnsetSize, FtraceFieldType::kInvalidFtraceFieldType,
+            "total_allocated", 3, ProtoSchemaType::kUint64,
+            TranslationStrategy::kInvalidTranslationStrategy},
+       },
+       kUnsetFtraceId,
+       351,
+       kUnsetSize},
       {"tracing_mark_write",
        "dpu",
        {
diff --git a/src/traced/probes/ftrace/ftrace_config_muxer.cc b/src/traced/probes/ftrace/ftrace_config_muxer.cc
index 52912fd..fa789ae 100644
--- a/src/traced/probes/ftrace/ftrace_config_muxer.cc
+++ b/src/traced/probes/ftrace/ftrace_config_muxer.cc
@@ -401,6 +401,7 @@
         // ion_stat supersedes ion_heap_grow / shrink for kernel 4.19+
         events.insert(GroupAndName("ion", "ion_stat"));
         events.insert(GroupAndName("mm_event", "mm_event_record"));
+        events.insert(GroupAndName("dmabuf_heap", "dma_heap_stat"));
         continue;
       }
 
diff --git a/src/traced/probes/ftrace/test/data/synthetic/events/dmabuf_heap/dma_heap_stat/format b/src/traced/probes/ftrace/test/data/synthetic/events/dmabuf_heap/dma_heap_stat/format
new file mode 100644
index 0000000..cf23ff8
--- /dev/null
+++ b/src/traced/probes/ftrace/test/data/synthetic/events/dmabuf_heap/dma_heap_stat/format
@@ -0,0 +1,13 @@
+name: dma_heap_stat
+ID: 1304
+format:
+	field:unsigned short common_type;	offset:0;	size:2;	signed:0;
+	field:unsigned char common_flags;	offset:2;	size:1;	signed:0;
+	field:unsigned char common_preempt_count;	offset:3;	size:1;	signed:0;
+	field:int common_pid;	offset:4;	size:4;	signed:1;
+
+	field:unsigned long inode;	offset:8;	size:8;	signed:0;
+	field:long len;	offset:16;	size:8;	signed:1;
+	field:unsigned long total_allocated;	offset:24;	size:8;	signed:0;
+
+print fmt: "inode=%lu len=%ldB total_allocated=%luB", REC->inode, REC->len, REC->total_allocated
diff --git a/tools/ftrace_proto_gen/event_list b/tools/ftrace_proto_gen/event_list
index b0cbcf5..45727f0 100644
--- a/tools/ftrace_proto_gen/event_list
+++ b/tools/ftrace_proto_gen/event_list
@@ -345,3 +345,4 @@
 dpu/tracing_mark_write
 g2d/tracing_mark_write
 mali/tracing_mark_write
+dmabuf_heap/dma_heap_stat
diff --git a/ui/src/controller/record_controller.ts b/ui/src/controller/record_controller.ts
index 2339b32..e3eb3b6 100644
--- a/ui/src/controller/record_controller.ts
+++ b/ui/src/controller/record_controller.ts
@@ -212,6 +212,7 @@
     ftraceEvents.add('mm_event/mm_event_record');
     ftraceEvents.add('kmem/rss_stat');
     ftraceEvents.add('ion/ion_stat');
+    ftraceEvents.add('dmabuf_heap/dma_heap_stat');
     ftraceEvents.add('kmem/ion_heap_grow');
     ftraceEvents.add('kmem/ion_heap_shrink');
   }