Merge "Do not use sample size for idle page tracking."
diff --git a/src/profiling/memory/bookkeeping.cc b/src/profiling/memory/bookkeeping.cc
index 587a1a6..1eddc60 100644
--- a/src/profiling/memory/bookkeeping.cc
+++ b/src/profiling/memory/bookkeeping.cc
@@ -30,7 +30,8 @@
 
 void HeapTracker::RecordMalloc(const std::vector<FrameData>& callstack,
                                uint64_t address,
-                               uint64_t size,
+                               uint64_t sample_size,
+                               uint64_t alloc_size,
                                uint64_t sequence_number,
                                uint64_t timestamp) {
   auto it = allocations_.find(address);
@@ -54,14 +55,15 @@
 
       alloc.SubtractFromCallstackAllocations();
       GlobalCallstackTrie::Node* node = callsites_->CreateCallsite(callstack);
-      alloc.total_size = size;
+      alloc.sample_size = sample_size;
+      alloc.alloc_size = alloc_size;
       alloc.sequence_number = sequence_number;
       alloc.callstack_allocations = MaybeCreateCallstackAllocations(node);
     }
   } else {
     GlobalCallstackTrie::Node* node = callsites_->CreateCallsite(callstack);
     allocations_.emplace(address,
-                         Allocation(size, sequence_number,
+                         Allocation(sample_size, alloc_size, sequence_number,
                                     MaybeCreateCallstackAllocations(node)));
   }
 
diff --git a/src/profiling/memory/bookkeeping.h b/src/profiling/memory/bookkeeping.h
index 238bd2f..7b562f3 100644
--- a/src/profiling/memory/bookkeeping.h
+++ b/src/profiling/memory/bookkeeping.h
@@ -235,7 +235,8 @@
 
   void RecordMalloc(const std::vector<FrameData>& stack,
                     uint64_t address,
-                    uint64_t size,
+                    uint64_t sample_size,
+                    uint64_t alloc_size,
                     uint64_t sequence_number,
                     uint64_t timestamp);
 
@@ -269,7 +270,7 @@
   void GetAllocations(F fn) {
     for (const auto& addr_and_allocation : allocations_) {
       const Allocation& alloc = addr_and_allocation.second;
-      fn(addr_and_allocation.first, alloc.total_size,
+      fn(addr_and_allocation.first, alloc.sample_size, alloc.alloc_size,
          alloc.callstack_allocations->node->id());
     }
   }
@@ -287,15 +288,22 @@
 
  private:
   struct Allocation {
-    Allocation(uint64_t size, uint64_t seq, CallstackAllocations* csa)
-        : total_size(size), sequence_number(seq), callstack_allocations(csa) {
+    Allocation(uint64_t size,
+               uint64_t asize,
+               uint64_t seq,
+               CallstackAllocations* csa)
+        : sample_size(size),
+          alloc_size(asize),
+          sequence_number(seq),
+          callstack_allocations(csa) {
       callstack_allocations->allocs++;
     }
 
     Allocation() = default;
     Allocation(const Allocation&) = delete;
     Allocation(Allocation&& other) noexcept {
-      total_size = other.total_size;
+      sample_size = other.sample_size;
+      alloc_size = other.alloc_size;
       sequence_number = other.sequence_number;
       callstack_allocations = other.callstack_allocations;
       other.callstack_allocations = nullptr;
@@ -303,12 +311,12 @@
 
     void AddToCallstackAllocations() {
       callstack_allocations->allocation_count++;
-      callstack_allocations->allocated += total_size;
+      callstack_allocations->allocated += sample_size;
     }
 
     void SubtractFromCallstackAllocations() {
       callstack_allocations->free_count++;
-      callstack_allocations->freed += total_size;
+      callstack_allocations->freed += sample_size;
     }
 
     ~Allocation() {
@@ -316,7 +324,8 @@
         callstack_allocations->allocs--;
     }
 
-    uint64_t total_size;
+    uint64_t sample_size;
+    uint64_t alloc_size;
     uint64_t sequence_number;
     CallstackAllocations* callstack_allocations;
   };
diff --git a/src/profiling/memory/bookkeeping_unittest.cc b/src/profiling/memory/bookkeeping_unittest.cc
index be64c99..9dbb1f6 100644
--- a/src/profiling/memory/bookkeeping_unittest.cc
+++ b/src/profiling/memory/bookkeeping_unittest.cc
@@ -58,9 +58,9 @@
   GlobalCallstackTrie c;
   HeapTracker hd(&c);
 
-  hd.RecordMalloc(stack(), 1, 5, sequence_number, 100 * sequence_number);
+  hd.RecordMalloc(stack(), 1, 5, 5, sequence_number, 100 * sequence_number);
   sequence_number++;
-  hd.RecordMalloc(stack2(), 2, 2, sequence_number, 100 * sequence_number);
+  hd.RecordMalloc(stack2(), 2, 2, 2, sequence_number, 100 * sequence_number);
   sequence_number++;
   ASSERT_EQ(hd.GetSizeForTesting(stack()), 5);
   ASSERT_EQ(hd.GetSizeForTesting(stack2()), 2);
@@ -84,8 +84,8 @@
   {
     HeapTracker hd2(&c);
 
-    hd.RecordMalloc(stack(), 1, 5, sequence_number, 100 * sequence_number);
-    hd2.RecordMalloc(stack(), 2, 2, sequence_number, 100 * sequence_number);
+    hd.RecordMalloc(stack(), 1, 5, 5, sequence_number, 100 * sequence_number);
+    hd2.RecordMalloc(stack(), 2, 2, 2, sequence_number, 100 * sequence_number);
     sequence_number++;
     ASSERT_EQ(hd2.GetSizeForTesting(stack()), 2);
     ASSERT_EQ(hd.GetSizeForTesting(stack()), 5);
@@ -99,9 +99,9 @@
   GlobalCallstackTrie c;
   HeapTracker hd(&c);
 
-  hd.RecordMalloc(stack(), 1, 5, sequence_number, 100 * sequence_number);
+  hd.RecordMalloc(stack(), 1, 5, 5, sequence_number, 100 * sequence_number);
   sequence_number++;
-  hd.RecordMalloc(stack2(), 1, 2, sequence_number, 100 * sequence_number);
+  hd.RecordMalloc(stack2(), 1, 2, 2, sequence_number, 100 * sequence_number);
   sequence_number++;
   EXPECT_EQ(hd.GetSizeForTesting(stack()), 0);
   EXPECT_EQ(hd.GetSizeForTesting(stack2()), 2);
@@ -112,8 +112,8 @@
   GlobalCallstackTrie c;
   HeapTracker hd(&c);
 
-  hd.RecordMalloc(stack(), 1, 5, 2, 2);
-  hd.RecordMalloc(stack2(), 1, 2, 1, 1);
+  hd.RecordMalloc(stack(), 1, 5, 5, 2, 2);
+  hd.RecordMalloc(stack2(), 1, 2, 2, 1, 1);
   EXPECT_EQ(hd.GetSizeForTesting(stack()), 5);
   EXPECT_EQ(hd.GetSizeForTesting(stack2()), 0);
 }
@@ -132,7 +132,7 @@
     }
 
     uint64_t addr = sequence_number;
-    hd.RecordMalloc(stack(), addr, 5, sequence_number, sequence_number);
+    hd.RecordMalloc(stack(), addr, 5, 5, sequence_number, sequence_number);
     sequence_number++;
     batch_frees.emplace_back(addr, sequence_number++);
     ASSERT_THAT(hd.GetSizeForTesting(stack()), AnyOf(Eq(0), Eq(5)));
@@ -178,7 +178,7 @@
                       100 * operation.sequence_number);
       } else {
         hd.RecordMalloc(*operation.stack, operation.address, operation.bytes,
-                        operation.sequence_number,
+                        operation.bytes, operation.sequence_number,
                         100 * operation.sequence_number);
       }
     }
diff --git a/src/profiling/memory/client.cc b/src/profiling/memory/client.cc
index 86550b2..fd20cd1 100644
--- a/src/profiling/memory/client.cc
+++ b/src/profiling/memory/client.cc
@@ -251,8 +251,8 @@
 //               +------------+    |
 //               |  main      |    v
 // stackbase +-> +------------+ 0xffff
-bool Client::RecordMalloc(uint64_t alloc_size,
-                          uint64_t total_size,
+bool Client::RecordMalloc(uint64_t sample_size,
+                          uint64_t alloc_size,
                           uint64_t alloc_address) {
   if (PERFETTO_UNLIKELY(getpid() != pid_at_creation_)) {
     PERFETTO_LOG("Detected post-fork child situation, stopping profiling.");
@@ -270,7 +270,7 @@
   }
 
   uint64_t stack_size = static_cast<uint64_t>(stackbase - stacktop);
-  metadata.total_size = total_size;
+  metadata.sample_size = sample_size;
   metadata.alloc_size = alloc_size;
   metadata.alloc_address = alloc_address;
   metadata.stack_pointer = reinterpret_cast<uint64_t>(stacktop);
diff --git a/src/profiling/memory/client.h b/src/profiling/memory/client.h
index 5b4a95e..d32f2e8 100644
--- a/src/profiling/memory/client.h
+++ b/src/profiling/memory/client.h
@@ -65,8 +65,8 @@
   static base::Optional<base::UnixSocketRaw> ConnectToHeapprofd(
       const std::string& sock_name);
 
-  bool RecordMalloc(uint64_t alloc_size,
-                    uint64_t total_size,
+  bool RecordMalloc(uint64_t sample_size,
+                    uint64_t alloc_size,
                     uint64_t alloc_address);
 
   // Add address to buffer of deallocations. Flushes the buffer if necessary.
diff --git a/src/profiling/memory/heapprofd_producer.cc b/src/profiling/memory/heapprofd_producer.cc
index 63fb8f3..2c4222d 100644
--- a/src/profiling/memory/heapprofd_producer.cc
+++ b/src/profiling/memory/heapprofd_producer.cc
@@ -562,9 +562,10 @@
     if (process_state.page_idle_checker) {
       PageIdleChecker& page_idle_checker = *process_state.page_idle_checker;
       heap_tracker.GetAllocations([&dump_state, &page_idle_checker](
-                                      uint64_t addr, uint64_t size,
+                                      uint64_t addr, uint64_t,
+                                      uint64_t alloc_size,
                                       uintptr_t callstack_id) {
-        int64_t idle = page_idle_checker.OnIdlePage(addr, size);
+        int64_t idle = page_idle_checker.OnIdlePage(addr, alloc_size);
         if (idle < 0) {
           PERFETTO_PLOG("OnIdlePage.");
           return;
@@ -851,7 +852,8 @@
   process_state.total_unwinding_time_us += alloc_rec.unwinding_time_us;
 
   heap_tracker.RecordMalloc(alloc_rec.frames, alloc_metadata.alloc_address,
-                            alloc_metadata.total_size,
+                            alloc_metadata.sample_size,
+                            alloc_metadata.alloc_size,
                             alloc_metadata.sequence_number,
                             alloc_metadata.clock_monotonic_coarse_timestamp);
 }
diff --git a/src/profiling/memory/malloc_hooks.cc b/src/profiling/memory/malloc_hooks.cc
index a50b662..66e0d58 100644
--- a/src/profiling/memory/malloc_hooks.cc
+++ b/src/profiling/memory/malloc_hooks.cc
@@ -451,7 +451,7 @@
     client = g_client.ref();  // owning copy
   }                           // unlock
 
-  if (!client->RecordMalloc(size, sampled_alloc_sz,
+  if (!client->RecordMalloc(sampled_alloc_sz, size,
                             reinterpret_cast<uint64_t>(addr))) {
     ShutdownLazy();
   }
@@ -557,7 +557,7 @@
   // sampled_alloc_sz == 0.
   PERFETTO_DCHECK(client);
 
-  if (!client->RecordMalloc(size, sampled_alloc_sz,
+  if (!client->RecordMalloc(sampled_alloc_sz, size,
                             reinterpret_cast<uint64_t>(addr))) {
     ShutdownLazy();
   }
diff --git a/src/profiling/memory/wire_protocol.h b/src/profiling/memory/wire_protocol.h
index 9de58d6..656135c 100644
--- a/src/profiling/memory/wire_protocol.h
+++ b/src/profiling/memory/wire_protocol.h
@@ -90,7 +90,7 @@
   // Size of the allocation that was made.
   uint64_t alloc_size;
   // Total number of bytes attributed to this allocation.
-  uint64_t total_size;
+  uint64_t sample_size;
   // Pointer returned by malloc(2) for this allocation.
   uint64_t alloc_address;
   // Current value of the stack pointer.