Pull Mapping / Frame / Callstack into profile_common.proto

This is so that Chrome can use the same protos.

Change-Id: Ic1c42bfb5d8a88c3a7498d7fc8faa06c53353fb1
diff --git a/Android.bp b/Android.bp
index b061677..8bf5918 100644
--- a/Android.bp
+++ b/Android.bp
@@ -486,6 +486,7 @@
     "src/tracing/core/trace_stats.cc",
     "src/tracing/core/trace_writer_impl.cc",
     "src/tracing/core/tracing_service_impl.cc",
+    "src/tracing/core/tracing_service_state.cc",
     "src/tracing/core/virtual_destructors.cc",
     "src/tracing/data_source.cc",
     "src/tracing/internal/in_process_tracing_backend.cc",
@@ -2191,6 +2192,7 @@
 genrule {
   name: "perfetto_protos_perfetto_trace_profiling_lite_gen",
   srcs: [
+    "protos/perfetto/trace/profiling/profile_common.proto",
     "protos/perfetto/trace/profiling/profile_packet.proto",
   ],
   tools: [
@@ -2198,6 +2200,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/profile_common.pb.cc",
     "external/perfetto/protos/perfetto/trace/profiling/profile_packet.pb.cc",
   ],
 }
@@ -2206,6 +2209,7 @@
 genrule {
   name: "perfetto_protos_perfetto_trace_profiling_lite_gen_headers",
   srcs: [
+    "protos/perfetto/trace/profiling/profile_common.proto",
     "protos/perfetto/trace/profiling/profile_packet.proto",
   ],
   tools: [
@@ -2213,6 +2217,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/profile_common.pb.h",
     "external/perfetto/protos/perfetto/trace/profiling/profile_packet.pb.h",
   ],
   export_include_dirs: [
@@ -2224,6 +2229,7 @@
 genrule {
   name: "perfetto_protos_perfetto_trace_profiling_zero_gen",
   srcs: [
+    "protos/perfetto/trace/profiling/profile_common.proto",
     "protos/perfetto/trace/profiling/profile_packet.proto",
   ],
   tools: [
@@ -2232,6 +2238,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_protoc_plugin___gn_standalone_toolchain_gcc_like_host_) --plugin_out=wrapper_namespace=pbzero:$(genDir)/external/perfetto/protos $(in)",
   out: [
+    "external/perfetto/protos/perfetto/trace/profiling/profile_common.pbzero.cc",
     "external/perfetto/protos/perfetto/trace/profiling/profile_packet.pbzero.cc",
   ],
 }
@@ -2240,6 +2247,7 @@
 genrule {
   name: "perfetto_protos_perfetto_trace_profiling_zero_gen_headers",
   srcs: [
+    "protos/perfetto/trace/profiling/profile_common.proto",
     "protos/perfetto/trace/profiling/profile_packet.proto",
   ],
   tools: [
@@ -2248,6 +2256,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_protoc_plugin___gn_standalone_toolchain_gcc_like_host_) --plugin_out=wrapper_namespace=pbzero:$(genDir)/external/perfetto/protos $(in)",
   out: [
+    "external/perfetto/protos/perfetto/trace/profiling/profile_common.pbzero.h",
     "external/perfetto/protos/perfetto/trace/profiling/profile_packet.pbzero.h",
   ],
   export_include_dirs: [
diff --git a/protos/BUILD b/protos/BUILD
index 1adcb01..30844c5 100644
--- a/protos/BUILD
+++ b/protos/BUILD
@@ -1004,6 +1004,7 @@
 proto_library(
     name = "trace_profiling",
     srcs = [
+        "perfetto/trace/profiling/profile_common.proto",
         "perfetto/trace/profiling/profile_packet.proto",
     ],
     has_services = 1,
@@ -1040,6 +1041,7 @@
 proto_library(
     name = "trace_profiling_zero",
     srcs = [
+        "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 c9ebbf5..095a6a2 100644
--- a/protos/perfetto/trace/perfetto_trace.proto
+++ b/protos/perfetto/trace/perfetto_trace.proto
@@ -2620,6 +2620,35 @@
 
 // End of protos/perfetto/trace/power/power_rails.proto
 
+// Begin of protos/perfetto/trace/profiling/profile_common.proto
+
+message Mapping {
+  optional uint64 id = 1;        // Interning key.
+  optional uint64 build_id = 2;  // Interning key.
+  optional uint64 offset = 3;
+  optional uint64 start = 4;
+  optional uint64 end = 5;
+  optional uint64 load_bias = 6;
+  // E.g. ["system", "lib64", "libc.so"]
+  repeated uint64 path_string_ids = 7;  // id of string.
+}
+
+message Frame {
+  optional uint64 id = 1;  // Interning key
+  // E.g. "fopen"
+  optional uint64 function_name_id = 2;  // id of string.
+  optional uint64 mapping_id = 3;
+  optional uint64 rel_pc = 4;
+}
+
+message Callstack {
+  optional uint64 id = 1;
+  // Frames of this callstack. Bottom frame first.
+  repeated uint64 frame_ids = 2;
+}
+
+// End of protos/perfetto/trace/profiling/profile_common.proto
+
 // Begin of protos/perfetto/trace/profiling/profile_packet.proto
 
 message ProfilePacket {
@@ -2630,32 +2659,8 @@
   }
 
   repeated Mapping mappings = 4;
-  message Mapping {
-    optional uint64 id = 1;        // Interning key.
-    optional uint64 build_id = 2;  // Interning key.
-    optional uint64 offset = 3;
-    optional uint64 start = 4;
-    optional uint64 end = 5;
-    optional uint64 load_bias = 6;
-    // E.g. ["system", "lib64", "libc.so"]
-    repeated uint64 path_string_ids = 7;  // id of string.
-  }
-
   repeated Frame frames = 2;
-  message Frame {
-    optional uint64 id = 1;  // Interning key
-    // E.g. "fopen"
-    optional uint64 function_name_id = 2;  // id of string.
-    optional uint64 mapping_id = 3;
-    optional uint64 rel_pc = 4;
-  }
-
   repeated Callstack callstacks = 3;
-  message Callstack {
-    optional uint64 id = 1;
-    // Frames of this callstack. Bottom frame first.
-    repeated uint64 frame_ids = 2;
-  }
 
   message HeapSample {
     optional uint64 callstack_id = 1;
diff --git a/protos/perfetto/trace/profiling/BUILD.gn b/protos/perfetto/trace/profiling/BUILD.gn
index a16d5eb..6439a2c 100644
--- a/protos/perfetto/trace/profiling/BUILD.gn
+++ b/protos/perfetto/trace/profiling/BUILD.gn
@@ -16,7 +16,10 @@
 import("../../../../gn/proto_library.gni")
 import("../../../../gn/protozero_library.gni")
 
-profiling_proto_names = [ "profile_packet.proto" ]
+profiling_proto_names = [
+  "profile_common.proto",
+  "profile_packet.proto",
+]
 
 proto_library("lite") {
   generate_python = false
diff --git a/protos/perfetto/trace/profiling/profile_common.proto b/protos/perfetto/trace/profiling/profile_common.proto
new file mode 100644
index 0000000..3df4b29
--- /dev/null
+++ b/protos/perfetto/trace/profiling/profile_common.proto
@@ -0,0 +1,45 @@
+/*
+ * 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;
+
+package perfetto.protos;
+
+message Mapping {
+  optional uint64 id = 1;        // Interning key.
+  optional uint64 build_id = 2;  // Interning key.
+  optional uint64 offset = 3;
+  optional uint64 start = 4;
+  optional uint64 end = 5;
+  optional uint64 load_bias = 6;
+  // E.g. ["system", "lib64", "libc.so"]
+  repeated uint64 path_string_ids = 7;  // id of string.
+}
+
+message Frame {
+  optional uint64 id = 1;  // Interning key
+  // E.g. "fopen"
+  optional uint64 function_name_id = 2;  // id of string.
+  optional uint64 mapping_id = 3;
+  optional uint64 rel_pc = 4;
+}
+
+message Callstack {
+  optional uint64 id = 1;
+  // Frames of this callstack. Bottom frame first.
+  repeated uint64 frame_ids = 2;
+}
diff --git a/protos/perfetto/trace/profiling/profile_packet.proto b/protos/perfetto/trace/profiling/profile_packet.proto
index 81dea30..72a644e 100644
--- a/protos/perfetto/trace/profiling/profile_packet.proto
+++ b/protos/perfetto/trace/profiling/profile_packet.proto
@@ -17,6 +17,8 @@
 syntax = "proto2";
 option optimize_for = LITE_RUNTIME;
 
+import "perfetto/trace/profiling/profile_common.proto";
+
 package perfetto.protos;
 
 message ProfilePacket {
@@ -27,32 +29,8 @@
   }
 
   repeated Mapping mappings = 4;
-  message Mapping {
-    optional uint64 id = 1;        // Interning key.
-    optional uint64 build_id = 2;  // Interning key.
-    optional uint64 offset = 3;
-    optional uint64 start = 4;
-    optional uint64 end = 5;
-    optional uint64 load_bias = 6;
-    // E.g. ["system", "lib64", "libc.so"]
-    repeated uint64 path_string_ids = 7;  // id of string.
-  }
-
   repeated Frame frames = 2;
-  message Frame {
-    optional uint64 id = 1;  // Interning key
-    // E.g. "fopen"
-    optional uint64 function_name_id = 2;  // id of string.
-    optional uint64 mapping_id = 3;
-    optional uint64 rel_pc = 4;
-  }
-
   repeated Callstack callstacks = 3;
-  message Callstack {
-    optional uint64 id = 1;
-    // Frames of this callstack. Bottom frame first.
-    repeated uint64 frame_ids = 2;
-  }
 
   message HeapSample {
     optional uint64 callstack_id = 1;
diff --git a/src/profiling/memory/bookkeeping_dump.cc b/src/profiling/memory/bookkeeping_dump.cc
index 34af1f1..4a283e7 100644
--- a/src/profiling/memory/bookkeeping_dump.cc
+++ b/src/profiling/memory/bookkeeping_dump.cc
@@ -19,6 +19,7 @@
 namespace perfetto {
 namespace profiling {
 namespace {
+using ::perfetto::protos::pbzero::Callstack;
 using ::perfetto::protos::pbzero::ProfilePacket;
 // This needs to be lower than the maximum acceptable chunk size, because this
 // is checked *before* writing another submessage. We conservatively assume
@@ -112,8 +113,7 @@
     auto built_callstack = callsites->BuildCallstack(node);
     for (const Interned<Frame>& frame : built_callstack)
       WriteFrame(frame);
-    ProfilePacket::Callstack* callstack =
-        current_profile_packet_->add_callstacks();
+    Callstack* callstack = current_profile_packet_->add_callstacks();
     callstack->set_id(node->id());
     for (const Interned<Frame>& frame : built_callstack)
       callstack->add_frame_ids(frame.id());
diff --git a/src/profiling/memory/bookkeeping_dump.h b/src/profiling/memory/bookkeeping_dump.h
index 9b45560..9bc2384 100644
--- a/src/profiling/memory/bookkeeping_dump.h
+++ b/src/profiling/memory/bookkeeping_dump.h
@@ -22,6 +22,7 @@
 
 #include <inttypes.h>
 
+#include "perfetto/trace/profiling/profile_common.pbzero.h"
 #include "perfetto/trace/profiling/profile_packet.pbzero.h"
 #include "perfetto/trace/trace_packet.pbzero.h"
 
diff --git a/src/trace_processor/heap_profile_tracker.h b/src/trace_processor/heap_profile_tracker.h
index 13a2672..a32c42a 100644
--- a/src/trace_processor/heap_profile_tracker.h
+++ b/src/trace_processor/heap_profile_tracker.h
@@ -19,6 +19,7 @@
 
 #include <deque>
 
+#include "perfetto/trace/profiling/profile_common.pbzero.h"
 #include "perfetto/trace/profiling/profile_packet.pbzero.h"
 #include "src/trace_processor/trace_storage.h"
 
diff --git a/src/trace_processor/proto_trace_parser.cc b/src/trace_processor/proto_trace_parser.cc
index b13c41e..c550e11 100644
--- a/src/trace_processor/proto_trace_parser.cc
+++ b/src/trace_processor/proto_trace_parser.cc
@@ -63,6 +63,7 @@
 #include "perfetto/trace/perfetto/perfetto_metatrace.pbzero.h"
 #include "perfetto/trace/power/battery_counters.pbzero.h"
 #include "perfetto/trace/power/power_rails.pbzero.h"
+#include "perfetto/trace/profiling/profile_common.pbzero.h"
 #include "perfetto/trace/profiling/profile_packet.pbzero.h"
 #include "perfetto/trace/ps/process_stats.pbzero.h"
 #include "perfetto/trace/ps/process_tree.pbzero.h"
@@ -1246,8 +1247,7 @@
   }
 
   for (auto it = packet.mappings(); it; ++it) {
-    protos::pbzero::ProfilePacket::Mapping::Decoder entry(it->data(),
-                                                          it->size());
+    protos::pbzero::Mapping::Decoder entry(it->data(), it->size());
     HeapProfileTracker::SourceMapping src_mapping;
     src_mapping.build_id = entry.build_id();
     src_mapping.offset = entry.offset();
@@ -1262,7 +1262,7 @@
   }
 
   for (auto it = packet.frames(); it; ++it) {
-    protos::pbzero::ProfilePacket::Frame::Decoder entry(it->data(), it->size());
+    protos::pbzero::Frame::Decoder entry(it->data(), it->size());
     HeapProfileTracker::SourceFrame src_frame;
     src_frame.name_id = entry.function_name_id();
     src_frame.mapping_id = entry.mapping_id();
@@ -1272,8 +1272,7 @@
   }
 
   for (auto it = packet.callstacks(); it; ++it) {
-    protos::pbzero::ProfilePacket::Callstack::Decoder entry(it->data(),
-                                                            it->size());
+    protos::pbzero::Callstack::Decoder entry(it->data(), it->size());
     HeapProfileTracker::SourceCallstack src_callstack;
     for (auto frame_it = entry.frame_ids(); frame_it; ++frame_it)
       src_callstack.emplace_back(frame_it->as_uint64());
diff --git a/tools/gen_merged_protos b/tools/gen_merged_protos
index c226713..c96f5fc 100755
--- a/tools/gen_merged_protos
+++ b/tools/gen_merged_protos
@@ -72,6 +72,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/profile_common.proto',
   'protos/perfetto/trace/profiling/profile_packet.proto',
   'protos/perfetto/trace/ps/process_stats.proto',
   'protos/perfetto/trace/ps/process_tree.proto',
diff --git a/tools/trace_to_text/trace_to_profile.cc b/tools/trace_to_text/trace_to_profile.cc
index 1170a39..c60eaa4 100644
--- a/tools/trace_to_text/trace_to_profile.cc
+++ b/tools/trace_to_text/trace_to_profile.cc
@@ -31,6 +31,7 @@
 #include "perfetto/ext/base/temp_file.h"
 #include "perfetto/ext/base/utils.h"
 
+#include "perfetto/trace/profiling/profile_common.pb.h"
 #include "perfetto/trace/profiling/profile_packet.pb.h"
 #include "perfetto/trace/trace.pb.h"
 #include "perfetto/trace/trace_packet.pb.h"
@@ -60,6 +61,9 @@
   return tmp;
 }
 
+using ::perfetto::protos::Callstack;
+using ::perfetto::protos::Frame;
+using ::perfetto::protos::Mapping;
 using ::perfetto::protos::ProfilePacket;
 
 using GLine = ::perftools::profiles::Line;
@@ -103,7 +107,7 @@
 
   std::map<uint64_t, const std::vector<uint64_t>> callstack_lookup;
   for (const ProfilePacket& packet : packet_fragments) {
-    for (const ProfilePacket::Callstack& callstack : packet.callstacks()) {
+    for (const Callstack& callstack : packet.callstacks()) {
       std::vector<uint64_t> frame_ids(
           static_cast<size_t>(callstack.frame_ids().size()));
       std::reverse_copy(callstack.frame_ids().cbegin(),
@@ -145,7 +149,7 @@
   value_type->set_unit(kBytes);
 
   for (const ProfilePacket& packet : packet_fragments) {
-    for (const ProfilePacket::Mapping& mapping : packet.mappings()) {
+    for (const Mapping& mapping : packet.mappings()) {
       GMapping* gmapping = profile.add_mapping();
       gmapping->set_id(mapping.id());
       gmapping->set_memory_start(mapping.start());
@@ -181,7 +185,7 @@
 
   std::set<uint64_t> functions_to_dump;
   for (const ProfilePacket& packet : packet_fragments) {
-    for (const ProfilePacket::Frame& frame : packet.frames()) {
+    for (const Frame& frame : packet.frames()) {
       GLocation* glocation = profile.add_location();
       glocation->set_id(frame.id());
       glocation->set_mapping_id(frame.mapping_id());