From 2c369084f8f22b7041faee69f64f3e119803e864 Mon Sep 17 00:00:00 2001 From: Benoit Lize Date: Fri, 14 Jan 2022 12:03:57 +0000 Subject: [PATCH] [PA-E] Don't double-count fragmentation in memory-infra This is expected to improve reported memory footprint according to the various malloc metrics for perf bots, and *.Malloc.* for UMA. This does not correspond to any real memory footprint change, and as a consequence Private Memory Footprint (PMF) should not move. Note that this metric regressed on some platforms when switching to PartitionAlloc, because of this double-counting. However, since it was known that this footprint was not directly comparable across different allocators, it was not used as an evaluation criterion. Change-Id: If7640e86499f3d497e9ea5904003e3dc0f9433ea Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3383815 Reviewed-by: Thiabaud Engelbrecht Reviewed-by: Siddhartha S Commit-Queue: Benoit Lize Cr-Commit-Position: refs/heads/main@{#959122} --- base/trace_event/malloc_dump_provider.cc | 25 +++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/base/trace_event/malloc_dump_provider.cc b/base/trace_event/malloc_dump_provider.cc index a34b68a31bc518..d471a77387d01d 100644 --- a/base/trace_event/malloc_dump_provider.cc +++ b/base/trace_event/malloc_dump_provider.cc @@ -176,6 +176,8 @@ bool MallocDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, size_t allocated_objects_count = 0; #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) uint64_t syscall_count = 0; + uint64_t pa_only_resident_size; + uint64_t pa_only_allocated_objects_size; #endif #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) @@ -183,6 +185,9 @@ bool MallocDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, &resident_size, &allocated_objects_size, &syscall_count); + pa_only_resident_size = resident_size; + pa_only_allocated_objects_size = allocated_objects_size; + // Even when PartitionAlloc is used, WinHeap is still used as well, report // its statistics. #if OS_WIN @@ -251,14 +256,28 @@ bool MallocDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, allocated_objects_count); } - if (resident_size > allocated_objects_size) { + int64_t waste = static_cast(resident_size) - allocated_objects_size; + + // With PartitionAlloc, reported size under malloc/partitions is the resident + // size, so it already includes fragmentation. Meaning that "malloc/"'s size + // would double-count fragmentation if we report it under + // "malloc/metadata_fragmentation_caches" as well. + // + // Still report waste, as on some platforms, PartitionAlloc doesn't capture + // all of malloc()'s memory footprint. +#if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) + int64_t pa_waste = static_cast(pa_only_resident_size) - + pa_only_allocated_objects_size; + waste -= pa_waste; +#endif + + if (waste > 0) { // Explicitly specify why is extra memory resident. In mac and ios it // accounts for the fragmentation and metadata. MemoryAllocatorDump* other_dump = pmd->CreateAllocatorDump("malloc/metadata_fragmentation_caches"); other_dump->AddScalar(MemoryAllocatorDump::kNameSize, - MemoryAllocatorDump::kUnitsBytes, - resident_size - allocated_objects_size); + MemoryAllocatorDump::kUnitsBytes, waste); } #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)