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