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/allocator/Partitions.h" | |
32 | |
33 #include "base/debug/alias.h" | |
34 #include "wtf/allocator/PartitionAllocator.h" | |
35 | |
36 namespace WTF { | |
37 | |
38 const char* const Partitions::kAllocatedObjectPoolName = | |
39 "partition_alloc/allocated_objects"; | |
40 | |
41 SpinLock Partitions::s_initializationLock; | |
42 bool Partitions::s_initialized = false; | |
43 | |
44 PartitionAllocatorGeneric Partitions::m_fastMallocAllocator; | |
45 PartitionAllocatorGeneric Partitions::m_bufferAllocator; | |
46 SizeSpecificPartitionAllocator<1024> Partitions::m_layoutAllocator; | |
47 Partitions::ReportPartitionAllocSizeFunction Partitions::m_reportSizeFunction = | |
48 nullptr; | |
49 | |
50 void Partitions::initialize( | |
51 ReportPartitionAllocSizeFunction reportSizeFunction) { | |
52 SpinLock::Guard guard(s_initializationLock); | |
53 | |
54 if (!s_initialized) { | |
55 partitionAllocGlobalInit(&Partitions::handleOutOfMemory); | |
56 m_fastMallocAllocator.init(); | |
57 m_bufferAllocator.init(); | |
58 m_layoutAllocator.init(); | |
59 m_reportSizeFunction = reportSizeFunction; | |
60 s_initialized = true; | |
61 } | |
62 } | |
63 | |
64 void Partitions::shutdown() { | |
65 SpinLock::Guard guard(s_initializationLock); | |
66 | |
67 // We could ASSERT here for a memory leak within the partition, but it leads | |
68 // to very hard to diagnose ASSERTs, so it's best to leave leak checking for | |
69 // the valgrind and heapcheck bots, which run without partitions. | |
70 if (s_initialized) { | |
71 (void)m_layoutAllocator.shutdown(); | |
72 (void)m_bufferAllocator.shutdown(); | |
73 (void)m_fastMallocAllocator.shutdown(); | |
74 } | |
75 } | |
76 | |
77 void Partitions::decommitFreeableMemory() { | |
78 RELEASE_ASSERT(isMainThread()); | |
79 if (!s_initialized) | |
80 return; | |
81 | |
82 partitionPurgeMemoryGeneric(bufferPartition(), | |
83 PartitionPurgeDecommitEmptyPages); | |
84 partitionPurgeMemoryGeneric(fastMallocPartition(), | |
85 PartitionPurgeDecommitEmptyPages); | |
86 partitionPurgeMemory(layoutPartition(), PartitionPurgeDecommitEmptyPages); | |
87 } | |
88 | |
89 void Partitions::reportMemoryUsageHistogram() { | |
90 static size_t observedMaxSizeInMB = 0; | |
91 | |
92 if (!m_reportSizeFunction) | |
93 return; | |
94 // We only report the memory in the main thread. | |
95 if (!isMainThread()) | |
96 return; | |
97 // +1 is for rounding up the sizeInMB. | |
98 size_t sizeInMB = Partitions::totalSizeOfCommittedPages() / 1024 / 1024 + 1; | |
99 if (sizeInMB > observedMaxSizeInMB) { | |
100 m_reportSizeFunction(sizeInMB); | |
101 observedMaxSizeInMB = sizeInMB; | |
102 } | |
103 } | |
104 | |
105 void Partitions::dumpMemoryStats(bool isLightDump, | |
106 PartitionStatsDumper* partitionStatsDumper) { | |
107 // Object model and rendering partitions are not thread safe and can be | |
108 // accessed only on the main thread. | |
109 ASSERT(isMainThread()); | |
110 | |
111 decommitFreeableMemory(); | |
112 partitionDumpStatsGeneric(fastMallocPartition(), "fast_malloc", isLightDump, | |
113 partitionStatsDumper); | |
114 partitionDumpStatsGeneric(bufferPartition(), "buffer", isLightDump, | |
115 partitionStatsDumper); | |
116 partitionDumpStats(layoutPartition(), "layout", isLightDump, | |
117 partitionStatsDumper); | |
118 } | |
119 | |
120 static NEVER_INLINE void partitionsOutOfMemoryUsing2G() { | |
121 size_t signature = 2UL * 1024 * 1024 * 1024; | |
122 base::debug::Alias(&signature); | |
123 OOM_CRASH(); | |
124 } | |
125 | |
126 static NEVER_INLINE void partitionsOutOfMemoryUsing1G() { | |
127 size_t signature = 1UL * 1024 * 1024 * 1024; | |
128 base::debug::Alias(&signature); | |
129 OOM_CRASH(); | |
130 } | |
131 | |
132 static NEVER_INLINE void partitionsOutOfMemoryUsing512M() { | |
133 size_t signature = 512 * 1024 * 1024; | |
134 base::debug::Alias(&signature); | |
135 OOM_CRASH(); | |
136 } | |
137 | |
138 static NEVER_INLINE void partitionsOutOfMemoryUsing256M() { | |
139 size_t signature = 256 * 1024 * 1024; | |
140 base::debug::Alias(&signature); | |
141 OOM_CRASH(); | |
142 } | |
143 | |
144 static NEVER_INLINE void partitionsOutOfMemoryUsing128M() { | |
145 size_t signature = 128 * 1024 * 1024; | |
146 base::debug::Alias(&signature); | |
147 OOM_CRASH(); | |
148 } | |
149 | |
150 static NEVER_INLINE void partitionsOutOfMemoryUsing64M() { | |
151 size_t signature = 64 * 1024 * 1024; | |
152 base::debug::Alias(&signature); | |
153 OOM_CRASH(); | |
154 } | |
155 | |
156 static NEVER_INLINE void partitionsOutOfMemoryUsing32M() { | |
157 size_t signature = 32 * 1024 * 1024; | |
158 base::debug::Alias(&signature); | |
159 OOM_CRASH(); | |
160 } | |
161 | |
162 static NEVER_INLINE void partitionsOutOfMemoryUsing16M() { | |
163 size_t signature = 16 * 1024 * 1024; | |
164 base::debug::Alias(&signature); | |
165 OOM_CRASH(); | |
166 } | |
167 | |
168 static NEVER_INLINE void partitionsOutOfMemoryUsingLessThan16M() { | |
169 size_t signature = 16 * 1024 * 1024 - 1; | |
170 base::debug::Alias(&signature); | |
171 DLOG(FATAL) << "ParitionAlloc: out of memory with < 16M usage (error:" | |
172 << getAllocPageErrorCode() << ")"; | |
173 } | |
174 | |
175 void Partitions::handleOutOfMemory() { | |
176 volatile size_t totalUsage = totalSizeOfCommittedPages(); | |
177 uint32_t allocPageErrorCode = getAllocPageErrorCode(); | |
178 base::debug::Alias(&allocPageErrorCode); | |
179 | |
180 if (totalUsage >= 2UL * 1024 * 1024 * 1024) | |
181 partitionsOutOfMemoryUsing2G(); | |
182 if (totalUsage >= 1UL * 1024 * 1024 * 1024) | |
183 partitionsOutOfMemoryUsing1G(); | |
184 if (totalUsage >= 512 * 1024 * 1024) | |
185 partitionsOutOfMemoryUsing512M(); | |
186 if (totalUsage >= 256 * 1024 * 1024) | |
187 partitionsOutOfMemoryUsing256M(); | |
188 if (totalUsage >= 128 * 1024 * 1024) | |
189 partitionsOutOfMemoryUsing128M(); | |
190 if (totalUsage >= 64 * 1024 * 1024) | |
191 partitionsOutOfMemoryUsing64M(); | |
192 if (totalUsage >= 32 * 1024 * 1024) | |
193 partitionsOutOfMemoryUsing32M(); | |
194 if (totalUsage >= 16 * 1024 * 1024) | |
195 partitionsOutOfMemoryUsing16M(); | |
196 partitionsOutOfMemoryUsingLessThan16M(); | |
197 } | |
198 | |
199 } // namespace WTF | |
OLD | NEW |