| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/memory/malloc_statistics.h" |
| 6 |
| 7 #include "base/allocator/allocator_extension.h" |
| 8 #include "base/logging.h" |
| 9 |
| 10 #if defined(OS_MACOSX) |
| 11 #include <malloc/malloc.h> |
| 12 #else |
| 13 #include <malloc.h> |
| 14 #endif |
| 15 #if defined(OS_WIN) |
| 16 #include <windows.h> |
| 17 #endif |
| 18 |
| 19 namespace base { |
| 20 namespace memory { |
| 21 |
| 22 MallocStatistics MallocStatistics::GetStatistics() { |
| 23 MallocStatistics malloc_stats; |
| 24 |
| 25 #if defined(USE_TCMALLOC) |
| 26 bool res = allocator::GetNumericProperty("generic.heap_size", |
| 27 &malloc_stats.total_virtual_size); |
| 28 DCHECK(res); |
| 29 res = allocator::GetNumericProperty("generic.total_physical_bytes", |
| 30 &malloc_stats.resident_size); |
| 31 DCHECK(res); |
| 32 res = allocator::GetNumericProperty("generic.current_allocated_bytes", |
| 33 &malloc_stats.allocated_objects_size); |
| 34 DCHECK(res); |
| 35 #elif defined(OS_MACOSX) || defined(OS_IOS) |
| 36 malloc_statistics_t stats = {0}; |
| 37 malloc_zone_statistics(nullptr, &stats); |
| 38 malloc_stats.total_virtual_size = stats.size_allocated; |
| 39 malloc_stats.allocated_objects_size = stats.size_in_use; |
| 40 |
| 41 // The resident size is approximated to the max size in use, which would count |
| 42 // the total size of all regions other than the free bytes at the end of each |
| 43 // region. In each allocation region the allocations are rounded off to a |
| 44 // fixed quantum, so the excess region will not be resident. |
| 45 // See crrev.com/1531463004 for detailed explanation. |
| 46 malloc_stats.resident_size = stats.max_size_in_use; |
| 47 #elif defined(OS_WIN) |
| 48 WinHeapInfo all_heap_info = {}; |
| 49 WinHeapMemoryDumpImpl(&all_heap_info); |
| 50 malloc_stats.total_virtual_size = |
| 51 all_heap_info.committed_size + all_heap_info.uncommitted_size; |
| 52 // Resident size is approximated with committed heap size. Note that it is |
| 53 // possible to do this with better accuracy on windows by intersecting the |
| 54 // working set with the virtual memory ranges occuipied by the heap. It's not |
| 55 // clear that this is worth it, as it's fairly expensive to do. |
| 56 malloc_stats.resident_size = all_heap_info.committed_size; |
| 57 malloc_stats.allocated_objects_size = all_heap_info.allocated_size; |
| 58 malloc_stats.allocated_objects_count = all_heap_info.block_count; |
| 59 #else |
| 60 struct mallinfo info = mallinfo(); |
| 61 DCHECK_GE(info.arena + info.hblkhd, info.uordblks); |
| 62 |
| 63 // In case of Android's jemalloc |arena| is 0 and the outer pages size is |
| 64 // reported by |hblkhd|. In case of dlmalloc the total is given by |
| 65 // |arena| + |hblkhd|. For more details see link: http://goo.gl/fMR8lF. |
| 66 malloc_stats.total_virtual_size = info.arena + info.hblkhd; |
| 67 malloc_stats.resident_size = info.uordblks; |
| 68 |
| 69 // Total allocated space is given by |uordblks|. |
| 70 malloc_stats.allocated_objects_size = info.uordblks; |
| 71 #endif |
| 72 if (malloc_stats.resident_size > malloc_stats.allocated_objects_size) { |
| 73 // Explicitly specify why is extra memory resident. In tcmalloc it accounts |
| 74 // for free lists and caches. In mac and ios it accounts for the |
| 75 // fragmentation and metadata. |
| 76 malloc_stats.allocated_objects_size = |
| 77 malloc_stats.resident_size - malloc_stats.allocated_objects_size; |
| 78 } |
| 79 return malloc_stats; |
| 80 } |
| 81 |
| 82 } // namespace trace_event |
| 83 } // namespace base |
| OLD | NEW |