OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/trace_event/malloc_dump_provider.h" | 5 #include "base/trace_event/malloc_dump_provider.h" |
6 | 6 |
7 #if defined(OS_MACOSX) | 7 #if defined(OS_MACOSX) |
8 #include <malloc/malloc.h> | 8 #include <malloc/malloc.h> |
9 #else | 9 #else |
10 #include <malloc.h> | 10 #include <malloc.h> |
11 #endif | 11 #endif |
12 | 12 |
13 #include "base/allocator/allocator_extension_thunks.h" | 13 #include "base/allocator/allocator_extension.h" |
14 #include "base/trace_event/process_memory_dump.h" | 14 #include "base/trace_event/process_memory_dump.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 namespace trace_event { | 17 namespace trace_event { |
18 | 18 |
19 // static | 19 // static |
20 const char MallocDumpProvider::kAllocatedObjects[] = "malloc/allocated_objects"; | 20 const char MallocDumpProvider::kAllocatedObjects[] = "malloc/allocated_objects"; |
21 | 21 |
22 // static | 22 // static |
23 MallocDumpProvider* MallocDumpProvider::GetInstance() { | 23 MallocDumpProvider* MallocDumpProvider::GetInstance() { |
(...skipping 11 matching lines...) Expand all Loading... |
35 ProcessMemoryDump* pmd) { | 35 ProcessMemoryDump* pmd) { |
36 size_t total_virtual_size = 0; | 36 size_t total_virtual_size = 0; |
37 size_t resident_size = 0; | 37 size_t resident_size = 0; |
38 size_t allocated_objects_size = 0; | 38 size_t allocated_objects_size = 0; |
39 #if defined(OS_MACOSX) || defined(OS_IOS) | 39 #if defined(OS_MACOSX) || defined(OS_IOS) |
40 malloc_statistics_t stats = {0}; | 40 malloc_statistics_t stats = {0}; |
41 malloc_zone_statistics(nullptr, &stats); | 41 malloc_zone_statistics(nullptr, &stats); |
42 total_virtual_size = stats.size_allocated; | 42 total_virtual_size = stats.size_allocated; |
43 resident_size = stats.size_in_use; | 43 resident_size = stats.size_in_use; |
44 allocated_objects_size = stats.size_in_use; | 44 allocated_objects_size = stats.size_in_use; |
| 45 #elif defined(USE_TCMALLOC) |
| 46 bool res = |
| 47 allocator::GetNumericProperty("generic.heap_size", &total_virtual_size); |
| 48 DCHECK(res); |
| 49 res = allocator::GetNumericProperty("generic.total_physical_bytes", |
| 50 &resident_size); |
| 51 DCHECK(res); |
| 52 res = allocator::GetNumericProperty("generic.current_allocated_bytes", |
| 53 &allocated_objects_size); |
| 54 DCHECK(res); |
45 #else | 55 #else |
46 allocator::thunks::GetNumericPropertyFunction get_property_function = | 56 struct mallinfo info = mallinfo(); |
47 allocator::thunks::GetGetNumericPropertyFunction(); | 57 DCHECK_GE(info.arena + info.hblkhd, info.uordblks); |
48 if (get_property_function) { | |
49 // If the function is not null then tcmalloc is used. See | |
50 // MallocExtension::getNumericProperty. | |
51 bool res = get_property_function("generic.heap_size", &total_virtual_size); | |
52 DCHECK(res); | |
53 res = get_property_function("generic.total_physical_bytes", &resident_size); | |
54 DCHECK(res); | |
55 res = get_property_function("generic.current_allocated_bytes", | |
56 &allocated_objects_size); | |
57 DCHECK(res); | |
58 } else { | |
59 struct mallinfo info = mallinfo(); | |
60 DCHECK_GE(info.arena + info.hblkhd, info.uordblks); | |
61 | 58 |
62 // In case of Android's jemalloc |arena| is 0 and the outer pages size is | 59 // In case of Android's jemalloc |arena| is 0 and the outer pages size is |
63 // reported by |hblkhd|. In case of dlmalloc the total is given by | 60 // reported by |hblkhd|. In case of dlmalloc the total is given by |
64 // |arena| + |hblkhd|. For more details see link: http://goo.gl/fMR8lF. | 61 // |arena| + |hblkhd|. For more details see link: http://goo.gl/fMR8lF. |
65 total_virtual_size = info.arena + info.hblkhd; | 62 total_virtual_size = info.arena + info.hblkhd; |
66 resident_size = info.uordblks; | 63 resident_size = info.uordblks; |
67 allocated_objects_size = info.uordblks; | 64 allocated_objects_size = info.uordblks; |
68 } | |
69 #endif | 65 #endif |
70 | 66 |
71 MemoryAllocatorDump* outer_dump = pmd->CreateAllocatorDump("malloc"); | 67 MemoryAllocatorDump* outer_dump = pmd->CreateAllocatorDump("malloc"); |
72 outer_dump->AddScalar("virtual_size", MemoryAllocatorDump::kUnitsBytes, | 68 outer_dump->AddScalar("virtual_size", MemoryAllocatorDump::kUnitsBytes, |
73 total_virtual_size); | 69 total_virtual_size); |
74 outer_dump->AddScalar(MemoryAllocatorDump::kNameSize, | 70 outer_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
75 MemoryAllocatorDump::kUnitsBytes, resident_size); | 71 MemoryAllocatorDump::kUnitsBytes, resident_size); |
76 | 72 |
77 // Total allocated space is given by |uordblks|. | 73 // Total allocated space is given by |uordblks|. |
78 MemoryAllocatorDump* inner_dump = pmd->CreateAllocatorDump(kAllocatedObjects); | 74 MemoryAllocatorDump* inner_dump = pmd->CreateAllocatorDump(kAllocatedObjects); |
79 inner_dump->AddScalar(MemoryAllocatorDump::kNameSize, | 75 inner_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
80 MemoryAllocatorDump::kUnitsBytes, | 76 MemoryAllocatorDump::kUnitsBytes, |
81 allocated_objects_size); | 77 allocated_objects_size); |
82 | 78 |
83 return true; | 79 return true; |
84 } | 80 } |
85 | 81 |
86 } // namespace trace_event | 82 } // namespace trace_event |
87 } // namespace base | 83 } // namespace base |
OLD | NEW |