Add generic.total_physical_bytes property to MallocExtension

The actual physical memory usage of tcmalloc cannot be obtained by
GetNumericProperty. This accounts for the current_allocated_bytes,
fragmentation and malloc metadata, and excludes the unmapped memory
regions. This helps the user to understand how much memory is actually
being used for the allocations that were made.

This change is done only in chromium tree since the
metadata_unmapped_bytes is added only in the chromium's tcmalloc in CL:
www.crrev.com/10499004.

This is to be used by malloc dump provider for tracing.

BUG=546491

Review URL: https://codereview.chromium.org/1410353005

Cr-Commit-Position: refs/heads/master@{#357155}
diff --git a/base/trace_event/malloc_dump_provider.cc b/base/trace_event/malloc_dump_provider.cc
index 6f15a04..914c684 100644
--- a/base/trace_event/malloc_dump_provider.cc
+++ b/base/trace_event/malloc_dump_provider.cc
@@ -40,23 +40,13 @@
   if (get_property_function) {
     // If the function is not null then tcmalloc is used. See
     // MallocExtension::getNumericProperty.
-    size_t pageheap_unmapped_bytes = 0;
     bool res = get_property_function("generic.heap_size", &total_virtual_size);
     DCHECK(res);
-    res = get_property_function("tcmalloc.pageheap_unmapped_bytes",
-                                &pageheap_unmapped_bytes);
+    res = get_property_function("generic.total_physical_bytes", &resident_size);
     DCHECK(res);
     res = get_property_function("generic.current_allocated_bytes",
                                 &allocated_objects_size);
     DCHECK(res);
-
-    // Please see TCMallocImplementation::GetStats implementation for
-    // explanation
-    // about this math.
-    // TODO(ssid): Usage of metadata is not included in page heap bytes
-    // (crbug.com/546491). MallocExtension::GetNumericProperty will be extended
-    // to get this value.
-    resident_size = total_virtual_size - pageheap_unmapped_bytes;
   } else {
     struct mallinfo info = mallinfo();
     DCHECK_GE(info.arena + info.hblkhd, info.uordblks);
diff --git a/third_party/tcmalloc/README.chromium b/third_party/tcmalloc/README.chromium
index 10800df..5ab67d0 100644
--- a/third_party/tcmalloc/README.chromium
+++ b/third_party/tcmalloc/README.chromium
@@ -99,3 +99,4 @@
 - Changed DEFINE_string to define const char*s instead of strings
 - Disabled HEAPPROFILE envvar unless ENABLE_PROFILING is defined
 - Add "ARMv8-a" to the supporting list of ARM architecture
+- Add generic.total_physical_bytes property to MallocExtension
diff --git a/third_party/tcmalloc/chromium/src/gperftools/malloc_extension.h b/third_party/tcmalloc/chromium/src/gperftools/malloc_extension.h
index d1148de..0a9b4ed 100644
--- a/third_party/tcmalloc/chromium/src/gperftools/malloc_extension.h
+++ b/third_party/tcmalloc/chromium/src/gperftools/malloc_extension.h
@@ -159,6 +159,14 @@
   //            freed memory regions
   //      This property is not writable.
   //
+  //  "generic.total_physical_bytes"
+  //      Estimate of total bytes of the physical memory usage by the
+  //      allocator ==
+  //            current_allocated_bytes +
+  //            fragmentation +
+  //            metadata
+  //      This property is not writable.
+  //
   // tcmalloc
   // --------
   // "tcmalloc.max_total_thread_cache_bytes"
diff --git a/third_party/tcmalloc/chromium/src/tcmalloc.cc b/third_party/tcmalloc/chromium/src/tcmalloc.cc
index c8a705f..4709411 100644
--- a/third_party/tcmalloc/chromium/src/tcmalloc.cc
+++ b/third_party/tcmalloc/chromium/src/tcmalloc.cc
@@ -678,6 +678,14 @@
       return true;
     }
 
+    if (strcmp(name, "generic.total_physical_bytes") == 0) {
+      TCMallocStats stats;
+      ExtractStats(&stats, NULL, NULL, NULL);
+      *value = stats.pageheap.system_bytes + stats.metadata_bytes -
+               stats.pageheap.unmapped_bytes - stats.metadata_unmapped_bytes;
+      return true;
+    }
+
     if (strcmp(name, "tcmalloc.slack_bytes") == 0) {
       // Kept for backwards compatibility.  Now defined externally as:
       //    pageheap_free_bytes + pageheap_unmapped_bytes.