Add proto for managed heap graphs.

Change-Id: I8da868e4b2416d86cf9db8989f865e71e2fda35f
Bug: 136210868
diff --git a/Android.bp b/Android.bp
index 550e67c..269dba3 100644
--- a/Android.bp
+++ b/Android.bp
@@ -2361,6 +2361,7 @@
 genrule {
   name: "perfetto_protos_perfetto_trace_profiling_lite_gen",
   srcs: [
+    "protos/perfetto/trace/profiling/heap_graph.proto",
     "protos/perfetto/trace/profiling/profile_common.proto",
     "protos/perfetto/trace/profiling/profile_packet.proto",
   ],
@@ -2369,6 +2370,7 @@
   ],
   cmd: "mkdir -p $(genDir)/external/perfetto/protos && $(location aprotoc) --cpp_out=$(genDir)/external/perfetto/protos --proto_path=external/perfetto/protos $(in)",
   out: [
+    "external/perfetto/protos/perfetto/trace/profiling/heap_graph.pb.cc",
     "external/perfetto/protos/perfetto/trace/profiling/profile_common.pb.cc",
     "external/perfetto/protos/perfetto/trace/profiling/profile_packet.pb.cc",
   ],
@@ -2378,6 +2380,7 @@
 genrule {
   name: "perfetto_protos_perfetto_trace_profiling_lite_gen_headers",
   srcs: [
+    "protos/perfetto/trace/profiling/heap_graph.proto",
     "protos/perfetto/trace/profiling/profile_common.proto",
     "protos/perfetto/trace/profiling/profile_packet.proto",
   ],
@@ -2386,6 +2389,7 @@
   ],
   cmd: "mkdir -p $(genDir)/external/perfetto/protos && $(location aprotoc) --cpp_out=$(genDir)/external/perfetto/protos --proto_path=external/perfetto/protos $(in)",
   out: [
+    "external/perfetto/protos/perfetto/trace/profiling/heap_graph.pb.h",
     "external/perfetto/protos/perfetto/trace/profiling/profile_common.pb.h",
     "external/perfetto/protos/perfetto/trace/profiling/profile_packet.pb.h",
   ],
@@ -2398,6 +2402,7 @@
 genrule {
   name: "perfetto_protos_perfetto_trace_profiling_zero_gen",
   srcs: [
+    "protos/perfetto/trace/profiling/heap_graph.proto",
     "protos/perfetto/trace/profiling/profile_common.proto",
     "protos/perfetto/trace/profiling/profile_packet.proto",
   ],
@@ -2407,6 +2412,7 @@
   ],
   cmd: "mkdir -p $(genDir)/external/perfetto/protos && $(location aprotoc) --cpp_out=$(genDir)/external/perfetto/protos --proto_path=external/perfetto/protos --plugin=protoc-gen-plugin=$(location perfetto_src_protozero_protoc_plugin_protozero_plugin___gn_standalone_toolchain_gcc_like_host_) --plugin_out=wrapper_namespace=pbzero:$(genDir)/external/perfetto/protos $(in)",
   out: [
+    "external/perfetto/protos/perfetto/trace/profiling/heap_graph.pbzero.cc",
     "external/perfetto/protos/perfetto/trace/profiling/profile_common.pbzero.cc",
     "external/perfetto/protos/perfetto/trace/profiling/profile_packet.pbzero.cc",
   ],
@@ -2416,6 +2422,7 @@
 genrule {
   name: "perfetto_protos_perfetto_trace_profiling_zero_gen_headers",
   srcs: [
+    "protos/perfetto/trace/profiling/heap_graph.proto",
     "protos/perfetto/trace/profiling/profile_common.proto",
     "protos/perfetto/trace/profiling/profile_packet.proto",
   ],
@@ -2425,6 +2432,7 @@
   ],
   cmd: "mkdir -p $(genDir)/external/perfetto/protos && $(location aprotoc) --cpp_out=$(genDir)/external/perfetto/protos --proto_path=external/perfetto/protos --plugin=protoc-gen-plugin=$(location perfetto_src_protozero_protoc_plugin_protozero_plugin___gn_standalone_toolchain_gcc_like_host_) --plugin_out=wrapper_namespace=pbzero:$(genDir)/external/perfetto/protos $(in)",
   out: [
+    "external/perfetto/protos/perfetto/trace/profiling/heap_graph.pbzero.h",
     "external/perfetto/protos/perfetto/trace/profiling/profile_common.pbzero.h",
     "external/perfetto/protos/perfetto/trace/profiling/profile_packet.pbzero.h",
   ],
diff --git a/protos/BUILD b/protos/BUILD
index 5e28c72..2c13660 100644
--- a/protos/BUILD
+++ b/protos/BUILD
@@ -1095,6 +1095,7 @@
 proto_library(
     name = "trace_profiling",
     srcs = [
+        "perfetto/trace/profiling/heap_graph.proto",
         "perfetto/trace/profiling/profile_common.proto",
         "perfetto/trace/profiling/profile_packet.proto",
     ],
@@ -1132,6 +1133,7 @@
 proto_library(
     name = "trace_profiling_zero",
     srcs = [
+        "perfetto/trace/profiling/heap_graph.proto",
         "perfetto/trace/profiling/profile_common.proto",
         "perfetto/trace/profiling/profile_packet.proto",
     ],
diff --git a/protos/perfetto/trace/perfetto_trace.proto b/protos/perfetto/trace/perfetto_trace.proto
index 83e9486..119897e 100644
--- a/protos/perfetto/trace/perfetto_trace.proto
+++ b/protos/perfetto/trace/perfetto_trace.proto
@@ -2717,6 +2717,43 @@
 
 // End of protos/perfetto/trace/power/power_rails.proto
 
+// Begin of protos/perfetto/trace/profiling/heap_graph.proto
+
+message HeapGraphObject {
+  optional uint64 id = 1;
+
+  // Index for InternedData.type_names for the name of the type of this object.
+  optional uint64 type_id = 2;
+
+  // Bytes occupied by this objects.
+  optional uint64 self_size = 3;
+
+  // Indices for InternedData.field_names for the name of the field referring
+  // to the object.
+  repeated uint64 reference_field_id = 4;
+
+  // Ids of the Object that is referred to.
+  repeated uint64 reference_object_id = 5;
+}
+
+message HeapGraph {
+  optional int32 pid = 1;
+
+  // Live objects at the time this dump was taken.
+  repeated HeapGraphObject objects = 2;
+
+  // Type names used in managed heap graph.
+  repeated InternedString type_names = 3;
+
+  // Field names for references in managed heap graph.
+  repeated InternedString field_names = 4;
+
+  optional bool continued = 5;
+  optional uint64 index = 6;
+}
+
+// End of protos/perfetto/trace/profiling/heap_graph.proto
+
 // Begin of protos/perfetto/trace/profiling/profile_common.proto
 
 // The interning fields in this file can refer to 2 different intern tables,
@@ -3077,7 +3114,7 @@
 // TracePacket(s).
 //
 // Next reserved id: 13 (up to 15).
-// Next id: 56.
+// Next id: 57.
 message TracePacket {
   // TODO(primiano): in future we should add a timestamp_clock_domain field to
   // allow mixing timestamps from different clock domains.
@@ -3112,6 +3149,7 @@
     GpuCounterEvent gpu_counter_event = 52;
     GpuRenderStageEvent gpu_render_stage_event = 53;
     StreamingProfilePacket streaming_profile_packet = 54;
+    HeapGraph heap_graph = 56;
 
     // Only used in profile packets.
     ProfiledFrameSymbols profiled_frame_symbols = 55;
diff --git a/protos/perfetto/trace/profiling/BUILD.gn b/protos/perfetto/trace/profiling/BUILD.gn
index 6439a2c..395ef57 100644
--- a/protos/perfetto/trace/profiling/BUILD.gn
+++ b/protos/perfetto/trace/profiling/BUILD.gn
@@ -19,6 +19,7 @@
 profiling_proto_names = [
   "profile_common.proto",
   "profile_packet.proto",
+  "heap_graph.proto",
 ]
 
 proto_library("lite") {
diff --git a/protos/perfetto/trace/profiling/heap_graph.proto b/protos/perfetto/trace/profiling/heap_graph.proto
new file mode 100644
index 0000000..3e44640
--- /dev/null
+++ b/protos/perfetto/trace/profiling/heap_graph.proto
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2019 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";
+option optimize_for = LITE_RUNTIME;
+
+import "perfetto/trace/profiling/profile_common.proto";
+
+// These messages encode a graph of objects that retain one another. Currently
+// this is used for Android Runtime (i.e. Java and Kotlin) heap graphs.
+
+package perfetto.protos;
+
+message HeapGraphObject {
+  optional uint64 id = 1;
+
+  // Index for InternedData.type_names for the name of the type of this object.
+  optional uint64 type_id = 2;
+
+  // Bytes occupied by this objects.
+  optional uint64 self_size = 3;
+
+  // Indices for InternedData.field_names for the name of the field referring
+  // to the object.
+  repeated uint64 reference_field_id = 4;
+
+  // Ids of the Object that is referred to.
+  repeated uint64 reference_object_id = 5;
+}
+
+message HeapGraph {
+  optional int32 pid = 1;
+
+  // Live objects at the time this dump was taken.
+  repeated HeapGraphObject objects = 2;
+
+  // Type names used in managed heap graph.
+  repeated InternedString type_names = 3;
+
+  // Field names for references in managed heap graph.
+  repeated InternedString field_names = 4;
+
+  optional bool continued = 5;
+  optional uint64 index = 6;
+}
diff --git a/protos/perfetto/trace/trace_packet.proto b/protos/perfetto/trace/trace_packet.proto
index c2a138e..8b79a22 100644
--- a/protos/perfetto/trace/trace_packet.proto
+++ b/protos/perfetto/trace/trace_packet.proto
@@ -34,6 +34,7 @@
 import "perfetto/trace/perfetto/perfetto_metatrace.proto";
 import "perfetto/trace/power/battery_counters.proto";
 import "perfetto/trace/power/power_rails.proto";
+import "perfetto/trace/profiling/heap_graph.proto";
 import "perfetto/trace/profiling/profile_common.proto";
 import "perfetto/trace/profiling/profile_packet.proto";
 import "perfetto/trace/ps/process_stats.proto";
@@ -52,7 +53,7 @@
 // TracePacket(s).
 //
 // Next reserved id: 13 (up to 15).
-// Next id: 56.
+// Next id: 57.
 message TracePacket {
   // TODO(primiano): in future we should add a timestamp_clock_domain field to
   // allow mixing timestamps from different clock domains.
@@ -87,6 +88,7 @@
     GpuCounterEvent gpu_counter_event = 52;
     GpuRenderStageEvent gpu_render_stage_event = 53;
     StreamingProfilePacket streaming_profile_packet = 54;
+    HeapGraph heap_graph = 56;
 
     // Only used in profile packets.
     ProfiledFrameSymbols profiled_frame_symbols = 55;
diff --git a/tools/gen_merged_protos b/tools/gen_merged_protos
index ff56fe1..d187e13 100755
--- a/tools/gen_merged_protos
+++ b/tools/gen_merged_protos
@@ -76,6 +76,7 @@
   'protos/perfetto/trace/perfetto/perfetto_metatrace.proto',
   'protos/perfetto/trace/power/battery_counters.proto',
   'protos/perfetto/trace/power/power_rails.proto',
+  'protos/perfetto/trace/profiling/heap_graph.proto',
   'protos/perfetto/trace/profiling/profile_common.proto',
   'protos/perfetto/trace/profiling/profile_packet.proto',
   'protos/perfetto/trace/ps/process_stats.proto',