Merge "Add heapprofd protos to perfetto_trace.proto."
diff --git a/protos/perfetto/trace/perfetto_trace.proto b/protos/perfetto/trace/perfetto_trace.proto
index b0f73a1..e5faf43 100644
--- a/protos/perfetto/trace/perfetto_trace.proto
+++ b/protos/perfetto/trace/perfetto_trace.proto
@@ -2480,7 +2480,7 @@
     // removed field with id 33
     FtraceStats ftrace_stats = 34;
     // removed field with id 35
-    // removed field with id 37
+    ProfilePacket profile_packet = 37;
     BatteryCounters battery = 38;
     AndroidLogPacket android_log = 39;
 
@@ -2500,3 +2500,64 @@
 }
 
 // End of protos/perfetto/trace/trace_packet.proto
+
+// Begin of protos/perfetto/trace/profiling/profile_packet.proto
+
+message ProfilePacket {
+  // either a function or library name.
+  repeated InternedString strings = 1;
+  message InternedString {
+    optional uint64 id = 1;
+    optional string str = 2;
+  }
+
+  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;
+  }
+
+  repeated Mapping mappings = 4;
+  message Mapping {
+    optional uint64 id = 1;  // Interning key.
+    optional bytes build_id = 2;
+    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 HeapSample {
+    optional uint64 callstack_id = 1;
+    // bytes allocated at this frame.
+    optional uint64 self_allocated = 2;
+    // bytes freed at this frame.
+    optional uint64 self_freed = 3;
+    optional uint64 timestamp = 4;  // timestamp [opt]
+    optional uint64 alloc_count = 5;
+    optional uint64 free_count = 6;
+  }
+
+  repeated ProcessHeapSamples process_dumps = 5;
+  message ProcessHeapSamples {
+    optional uint64 pid = 1;
+    repeated HeapSample samples = 2;
+  }
+
+  optional bool continued = 6;
+  optional uint64 index = 7;
+}
+
+// End of protos/perfetto/trace/profiling/profile_packet.proto
diff --git a/protos/perfetto/trace/profiling/profile_packet.proto b/protos/perfetto/trace/profiling/profile_packet.proto
index 49638f1..b1d1bf8 100644
--- a/protos/perfetto/trace/profiling/profile_packet.proto
+++ b/protos/perfetto/trace/profiling/profile_packet.proto
@@ -57,10 +57,10 @@
 
   message HeapSample {
     optional uint64 callstack_id = 1;
-    // bytes allocated at this frame and in functions called by it.
-    optional uint64 cumulative_allocated = 2;
-    // bytes freed at this frame and in functions called by it.
-    optional uint64 cumulative_freed = 3;
+    // bytes allocated at this frame.
+    optional uint64 self_allocated = 2;
+    // bytes freed at this frame.
+    optional uint64 self_freed = 3;
     optional uint64 timestamp = 4;  // timestamp [opt]
     optional uint64 alloc_count = 5;
     optional uint64 free_count = 6;
diff --git a/src/profiling/memory/bookkeeping.cc b/src/profiling/memory/bookkeeping.cc
index 619b3d0..e037514 100644
--- a/src/profiling/memory/bookkeeping.cc
+++ b/src/profiling/memory/bookkeeping.cc
@@ -158,8 +158,8 @@
     dump_state->callstacks_to_dump.emplace(alloc.node);
     ProfilePacket::HeapSample* sample = proto->add_samples();
     sample->set_callstack_id(alloc.node->id());
-    sample->set_cumulative_allocated(alloc.allocated);
-    sample->set_cumulative_freed(alloc.freed);
+    sample->set_self_allocated(alloc.allocated);
+    sample->set_self_freed(alloc.freed);
     sample->set_alloc_count(alloc.allocation_count);
     sample->set_free_count(alloc.free_count);
 
diff --git a/src/profiling/memory/heapprofd_end_to_end_test.cc b/src/profiling/memory/heapprofd_end_to_end_test.cc
index 026a2df..5e506bd 100644
--- a/src/profiling/memory/heapprofd_end_to_end_test.cc
+++ b/src/profiling/memory/heapprofd_end_to_end_test.cc
@@ -193,10 +193,10 @@
       EXPECT_EQ(dump.samples().size(), 1);
       for (const auto& sample : dump.samples()) {
         samples++;
-        EXPECT_EQ(sample.cumulative_allocated() % kAllocSize, 0);
-        EXPECT_EQ(sample.cumulative_freed() % kAllocSize, 0);
-        last_allocated = sample.cumulative_allocated();
-        last_freed = sample.cumulative_freed();
+        EXPECT_EQ(sample.self_allocated() % kAllocSize, 0);
+        EXPECT_EQ(sample.self_freed() % kAllocSize, 0);
+        last_allocated = sample.self_allocated();
+        last_freed = sample.self_freed();
       }
       profile_packets++;
     }
@@ -251,10 +251,10 @@
       EXPECT_EQ(dump.samples().size(), 1);
       for (const auto& sample : dump.samples()) {
         samples++;
-        EXPECT_EQ(sample.cumulative_allocated() % kAllocSize, 0);
-        EXPECT_EQ(sample.cumulative_freed() % kAllocSize, 0);
-        last_allocated = sample.cumulative_allocated();
-        last_freed = sample.cumulative_freed();
+        EXPECT_EQ(sample.self_allocated() % kAllocSize, 0);
+        EXPECT_EQ(sample.self_freed() % kAllocSize, 0);
+        last_allocated = sample.self_allocated();
+        last_freed = sample.self_freed();
       }
       profile_packets++;
     }
@@ -330,8 +330,8 @@
       profile_packets++;
       for (const auto& sample : dump.samples()) {
         samples++;
-        total_allocated += sample.cumulative_allocated();
-        total_freed += sample.cumulative_freed();
+        total_allocated += sample.self_allocated();
+        total_freed += sample.self_freed();
       }
     }
   }
diff --git a/tools/gen_merged_protos b/tools/gen_merged_protos
index 44544d5..d96ced8 100755
--- a/tools/gen_merged_protos
+++ b/tools/gen_merged_protos
@@ -69,6 +69,7 @@
   'protos/perfetto/trace/sys_stats/sys_stats.proto',
   'protos/perfetto/trace/trace.proto',
   'protos/perfetto/trace/trace_packet.proto',
+  'protos/perfetto/trace/profiling/profile_packet.proto',
 )
 
 MERGED_TRACE_PROTO = 'protos/perfetto/trace/perfetto_trace.proto'
diff --git a/tools/trace_to_text/trace_to_profile.cc b/tools/trace_to_text/trace_to_profile.cc
index 3c764a5..a2846a9 100644
--- a/tools/trace_to_text/trace_to_profile.cc
+++ b/tools/trace_to_text/trace_to_profile.cc
@@ -179,8 +179,8 @@
         }
         for (uint64_t frame_id : it->second)
           gsample->add_location_id(frame_id);
-        gsample->add_value(static_cast<int64_t>(sample.cumulative_allocated() -
-                                                sample.cumulative_freed()));
+        gsample->add_value(static_cast<int64_t>(sample.self_allocated() -
+                                                sample.self_freed()));
       }
     }
     std::string filename = file_prefix + std::to_string(pid) + ".pb";