OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #include "wtf/Partitions.h" | |
32 | |
33 #include "base/debug/alias.h" | |
34 #include "wtf/PartitionAllocator.h" | |
35 | |
36 namespace WTF { | |
37 | |
38 const char* const Partitions::kAllocatedObjectPoolName = "partition_alloc/alloca
ted_objects"; | |
39 | |
40 SpinLock Partitions::s_initializationLock; | |
41 bool Partitions::s_initialized = false; | |
42 | |
43 PartitionAllocatorGeneric Partitions::m_fastMallocAllocator; | |
44 PartitionAllocatorGeneric Partitions::m_bufferAllocator; | |
45 SizeSpecificPartitionAllocator<1024> Partitions::m_layoutAllocator; | |
46 Partitions::ReportPartitionAllocSizeFunction Partitions::m_reportSizeFunction =
nullptr; | |
47 | |
48 void Partitions::initialize(ReportPartitionAllocSizeFunction reportSizeFunction) | |
49 { | |
50 SpinLock::Guard guard(s_initializationLock); | |
51 | |
52 if (!s_initialized) { | |
53 partitionAllocGlobalInit(&Partitions::handleOutOfMemory); | |
54 m_fastMallocAllocator.init(); | |
55 m_bufferAllocator.init(); | |
56 m_layoutAllocator.init(); | |
57 m_reportSizeFunction = reportSizeFunction; | |
58 s_initialized = true; | |
59 } | |
60 } | |
61 | |
62 void Partitions::shutdown() | |
63 { | |
64 SpinLock::Guard guard(s_initializationLock); | |
65 | |
66 // We could ASSERT here for a memory leak within the partition, but it leads | |
67 // to very hard to diagnose ASSERTs, so it's best to leave leak checking for | |
68 // the valgrind and heapcheck bots, which run without partitions. | |
69 if (s_initialized) { | |
70 (void) m_layoutAllocator.shutdown(); | |
71 (void) m_bufferAllocator.shutdown(); | |
72 (void) m_fastMallocAllocator.shutdown(); | |
73 } | |
74 } | |
75 | |
76 void Partitions::decommitFreeableMemory() | |
77 { | |
78 RELEASE_ASSERT(isMainThread()); | |
79 if (!s_initialized) | |
80 return; | |
81 | |
82 partitionPurgeMemoryGeneric(bufferPartition(), PartitionPurgeDecommitEmptyPa
ges); | |
83 partitionPurgeMemoryGeneric(fastMallocPartition(), PartitionPurgeDecommitEmp
tyPages); | |
84 partitionPurgeMemory(layoutPartition(), PartitionPurgeDecommitEmptyPages); | |
85 } | |
86 | |
87 void Partitions::reportMemoryUsageHistogram() | |
88 { | |
89 static size_t observedMaxSizeInMB = 0; | |
90 | |
91 if (!m_reportSizeFunction) | |
92 return; | |
93 // We only report the memory in the main thread. | |
94 if (!isMainThread()) | |
95 return; | |
96 // +1 is for rounding up the sizeInMB. | |
97 size_t sizeInMB = Partitions::totalSizeOfCommittedPages() / 1024 / 1024 + 1; | |
98 if (sizeInMB > observedMaxSizeInMB) { | |
99 m_reportSizeFunction(sizeInMB); | |
100 observedMaxSizeInMB = sizeInMB; | |
101 } | |
102 } | |
103 | |
104 void Partitions::dumpMemoryStats(bool isLightDump, PartitionStatsDumper* partiti
onStatsDumper) | |
105 { | |
106 // Object model and rendering partitions are not thread safe and can be | |
107 // accessed only on the main thread. | |
108 ASSERT(isMainThread()); | |
109 | |
110 decommitFreeableMemory(); | |
111 partitionDumpStatsGeneric(fastMallocPartition(), "fast_malloc", isLightDump,
partitionStatsDumper); | |
112 partitionDumpStatsGeneric(bufferPartition(), "buffer", isLightDump, partitio
nStatsDumper); | |
113 partitionDumpStats(layoutPartition(), "layout", isLightDump, partitionStatsD
umper); | |
114 } | |
115 | |
116 static NEVER_INLINE void partitionsOutOfMemoryUsing2G() | |
117 { | |
118 size_t signature = 2UL * 1024 * 1024 * 1024; | |
119 base::debug::Alias(&signature); | |
120 IMMEDIATE_CRASH(); | |
121 } | |
122 | |
123 static NEVER_INLINE void partitionsOutOfMemoryUsing1G() | |
124 { | |
125 size_t signature = 1UL * 1024 * 1024 * 1024; | |
126 base::debug::Alias(&signature); | |
127 IMMEDIATE_CRASH(); | |
128 } | |
129 | |
130 static NEVER_INLINE void partitionsOutOfMemoryUsing512M() | |
131 { | |
132 size_t signature = 512 * 1024 * 1024; | |
133 base::debug::Alias(&signature); | |
134 IMMEDIATE_CRASH(); | |
135 } | |
136 | |
137 static NEVER_INLINE void partitionsOutOfMemoryUsing256M() | |
138 { | |
139 size_t signature = 256 * 1024 * 1024; | |
140 base::debug::Alias(&signature); | |
141 IMMEDIATE_CRASH(); | |
142 } | |
143 | |
144 static NEVER_INLINE void partitionsOutOfMemoryUsing128M() | |
145 { | |
146 size_t signature = 128 * 1024 * 1024; | |
147 base::debug::Alias(&signature); | |
148 IMMEDIATE_CRASH(); | |
149 } | |
150 | |
151 static NEVER_INLINE void partitionsOutOfMemoryUsing64M() | |
152 { | |
153 size_t signature = 64 * 1024 * 1024; | |
154 base::debug::Alias(&signature); | |
155 IMMEDIATE_CRASH(); | |
156 } | |
157 | |
158 static NEVER_INLINE void partitionsOutOfMemoryUsing32M() | |
159 { | |
160 size_t signature = 32 * 1024 * 1024; | |
161 base::debug::Alias(&signature); | |
162 IMMEDIATE_CRASH(); | |
163 } | |
164 | |
165 static NEVER_INLINE void partitionsOutOfMemoryUsing16M() | |
166 { | |
167 size_t signature = 16 * 1024 * 1024; | |
168 base::debug::Alias(&signature); | |
169 IMMEDIATE_CRASH(); | |
170 } | |
171 | |
172 static NEVER_INLINE void partitionsOutOfMemoryUsingLessThan16M() | |
173 { | |
174 size_t signature = 16 * 1024 * 1024 - 1; | |
175 base::debug::Alias(&signature); | |
176 DLOG(FATAL) << "ParitionAlloc: out of memory with < 16M usage (error:" << ge
tAllocPageErrorCode() << ")"; | |
177 } | |
178 | |
179 void Partitions::handleOutOfMemory() | |
180 { | |
181 volatile size_t totalUsage = totalSizeOfCommittedPages(); | |
182 uint32_t allocPageErrorCode = getAllocPageErrorCode(); | |
183 base::debug::Alias(&allocPageErrorCode); | |
184 | |
185 if (totalUsage >= 2UL * 1024 * 1024 * 1024) | |
186 partitionsOutOfMemoryUsing2G(); | |
187 if (totalUsage >= 1UL * 1024 * 1024 * 1024) | |
188 partitionsOutOfMemoryUsing1G(); | |
189 if (totalUsage >= 512 * 1024 * 1024) | |
190 partitionsOutOfMemoryUsing512M(); | |
191 if (totalUsage >= 256 * 1024 * 1024) | |
192 partitionsOutOfMemoryUsing256M(); | |
193 if (totalUsage >= 128 * 1024 * 1024) | |
194 partitionsOutOfMemoryUsing128M(); | |
195 if (totalUsage >= 64 * 1024 * 1024) | |
196 partitionsOutOfMemoryUsing64M(); | |
197 if (totalUsage >= 32 * 1024 * 1024) | |
198 partitionsOutOfMemoryUsing32M(); | |
199 if (totalUsage >= 16 * 1024 * 1024) | |
200 partitionsOutOfMemoryUsing16M(); | |
201 partitionsOutOfMemoryUsingLessThan16M(); | |
202 } | |
203 | |
204 } // namespace WTF | |
OLD | NEW |