buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 18 | #include <iomanip> |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 19 | #include <numeric> |
| 20 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 21 | #include "arena_allocator.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 22 | #include "logging.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 23 | #include "mem_map.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 24 | #include "mutex.h" |
Ian Rogers | 02ed4c0 | 2013-09-06 13:10:04 -0700 | [diff] [blame] | 25 | #include "thread-inl.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 26 | #include "systrace.h" |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 30 | static constexpr size_t kMemoryToolRedZoneBytes = 8; |
Mark Mendell | 45c1165 | 2013-12-11 12:27:35 -0800 | [diff] [blame] | 31 | constexpr size_t Arena::kDefaultSize; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 32 | |
Vladimir Marko | bd9e9db | 2014-03-07 19:41:05 +0000 | [diff] [blame] | 33 | template <bool kCount> |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame] | 34 | const char* const ArenaAllocatorStatsImpl<kCount>::kAllocNames[] = { |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 35 | "Misc ", |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 36 | "SwitchTbl ", |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 37 | "SlowPaths ", |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 38 | "GrowBitMap ", |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 39 | "STL ", |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 40 | "GraphBuilder ", |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 41 | "Graph ", |
| 42 | "BasicBlock ", |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 43 | "BlockList ", |
| 44 | "RevPostOrder ", |
| 45 | "LinearOrder ", |
| 46 | "ConstantsMap ", |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 47 | "Predecessors ", |
| 48 | "Successors ", |
| 49 | "Dominated ", |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 50 | "Instruction ", |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 51 | "InvokeInputs ", |
| 52 | "PhiInputs ", |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 53 | "LoopInfo ", |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 54 | "LIBackEdges ", |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 55 | "TryCatchInf ", |
| 56 | "UseListNode ", |
| 57 | "Environment ", |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 58 | "EnvVRegs ", |
| 59 | "EnvLocations ", |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 60 | "LocSummary ", |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 61 | "SsaBuilder ", |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 62 | "MoveOperands ", |
| 63 | "CodeBuffer ", |
| 64 | "StackMaps ", |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 65 | "Optimization ", |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 66 | "GVN ", |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 67 | "InductionVar ", |
| 68 | "BCE ", |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 69 | "DCE ", |
| 70 | "LSE ", |
| 71 | "LICM ", |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 72 | "SsaLiveness ", |
| 73 | "SsaPhiElim ", |
| 74 | "RefTypeProp ", |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 75 | "SideEffects ", |
| 76 | "RegAllocator ", |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 77 | "RegAllocVldt ", |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 78 | "StackMapStm ", |
| 79 | "CodeGen ", |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 80 | "Assembler ", |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 81 | "ParallelMove ", |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 82 | "GraphChecker ", |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 83 | "Verifier ", |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 84 | "CallingConv ", |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 87 | template <bool kCount> |
| 88 | ArenaAllocatorStatsImpl<kCount>::ArenaAllocatorStatsImpl() |
| 89 | : num_allocations_(0u) { |
| 90 | std::fill_n(alloc_stats_, arraysize(alloc_stats_), 0u); |
| 91 | } |
| 92 | |
| 93 | template <bool kCount> |
| 94 | void ArenaAllocatorStatsImpl<kCount>::Copy(const ArenaAllocatorStatsImpl& other) { |
| 95 | num_allocations_ = other.num_allocations_; |
| 96 | std::copy(other.alloc_stats_, other.alloc_stats_ + arraysize(alloc_stats_), alloc_stats_); |
| 97 | } |
| 98 | |
| 99 | template <bool kCount> |
| 100 | void ArenaAllocatorStatsImpl<kCount>::RecordAlloc(size_t bytes, ArenaAllocKind kind) { |
| 101 | alloc_stats_[kind] += bytes; |
| 102 | ++num_allocations_; |
| 103 | } |
| 104 | |
| 105 | template <bool kCount> |
| 106 | size_t ArenaAllocatorStatsImpl<kCount>::NumAllocations() const { |
| 107 | return num_allocations_; |
| 108 | } |
| 109 | |
| 110 | template <bool kCount> |
| 111 | size_t ArenaAllocatorStatsImpl<kCount>::BytesAllocated() const { |
| 112 | const size_t init = 0u; // Initial value of the correct type. |
| 113 | return std::accumulate(alloc_stats_, alloc_stats_ + arraysize(alloc_stats_), init); |
| 114 | } |
| 115 | |
| 116 | template <bool kCount> |
| 117 | void ArenaAllocatorStatsImpl<kCount>::Dump(std::ostream& os, const Arena* first, |
| 118 | ssize_t lost_bytes_adjustment) const { |
| 119 | size_t malloc_bytes = 0u; |
| 120 | size_t lost_bytes = 0u; |
| 121 | size_t num_arenas = 0u; |
| 122 | for (const Arena* arena = first; arena != nullptr; arena = arena->next_) { |
| 123 | malloc_bytes += arena->Size(); |
| 124 | lost_bytes += arena->RemainingSpace(); |
| 125 | ++num_arenas; |
| 126 | } |
| 127 | // The lost_bytes_adjustment is used to make up for the fact that the current arena |
| 128 | // may not have the bytes_allocated_ updated correctly. |
| 129 | lost_bytes += lost_bytes_adjustment; |
| 130 | const size_t bytes_allocated = BytesAllocated(); |
| 131 | os << " MEM: used: " << bytes_allocated << ", allocated: " << malloc_bytes |
| 132 | << ", lost: " << lost_bytes << "\n"; |
Vladimir Marko | bd9e9db | 2014-03-07 19:41:05 +0000 | [diff] [blame] | 133 | size_t num_allocations = NumAllocations(); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 134 | if (num_allocations != 0) { |
| 135 | os << "Number of arenas allocated: " << num_arenas << ", Number of allocations: " |
| 136 | << num_allocations << ", avg size: " << bytes_allocated / num_allocations << "\n"; |
| 137 | } |
| 138 | os << "===== Allocation by kind\n"; |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 139 | static_assert(arraysize(kAllocNames) == kNumArenaAllocKinds, "arraysize of kAllocNames"); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 140 | for (int i = 0; i < kNumArenaAllocKinds; i++) { |
Vladimir Marko | bd9e9db | 2014-03-07 19:41:05 +0000 | [diff] [blame] | 141 | os << kAllocNames[i] << std::setw(10) << alloc_stats_[i] << "\n"; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | // Explicitly instantiate the used implementation. |
| 146 | template class ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations>; |
| 147 | |
Vladimir Marko | 7bda3b6 | 2015-10-07 12:44:31 +0000 | [diff] [blame] | 148 | void ArenaAllocatorMemoryTool::DoMakeDefined(void* ptr, size_t size) { |
| 149 | MEMORY_TOOL_MAKE_DEFINED(ptr, size); |
| 150 | } |
| 151 | |
| 152 | void ArenaAllocatorMemoryTool::DoMakeUndefined(void* ptr, size_t size) { |
| 153 | MEMORY_TOOL_MAKE_UNDEFINED(ptr, size); |
| 154 | } |
| 155 | |
| 156 | void ArenaAllocatorMemoryTool::DoMakeInaccessible(void* ptr, size_t size) { |
| 157 | MEMORY_TOOL_MAKE_NOACCESS(ptr, size); |
| 158 | } |
| 159 | |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 160 | Arena::Arena() : bytes_allocated_(0), next_(nullptr) { |
Ian Rogers | e7a5b7d | 2013-04-18 20:09:02 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 163 | MallocArena::MallocArena(size_t size) { |
| 164 | memory_ = reinterpret_cast<uint8_t*>(calloc(1, size)); |
| 165 | size_ = size; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 168 | MallocArena::~MallocArena() { |
| 169 | free(reinterpret_cast<void*>(memory_)); |
| 170 | } |
| 171 | |
Nicolas Geoffray | 25e0456 | 2016-03-01 13:17:58 +0000 | [diff] [blame] | 172 | MemMapArena::MemMapArena(size_t size, bool low_4gb, const char* name) { |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 173 | std::string error_msg; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 174 | map_.reset(MemMap::MapAnonymous( |
Nicolas Geoffray | 25e0456 | 2016-03-01 13:17:58 +0000 | [diff] [blame] | 175 | name, nullptr, size, PROT_READ | PROT_WRITE, low_4gb, false, &error_msg)); |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 176 | CHECK(map_.get() != nullptr) << error_msg; |
| 177 | memory_ = map_->Begin(); |
| 178 | size_ = map_->Size(); |
| 179 | } |
| 180 | |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 181 | MemMapArena::~MemMapArena() { |
| 182 | // Destroys MemMap via std::unique_ptr<>. |
| 183 | } |
| 184 | |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 185 | void MemMapArena::Release() { |
| 186 | if (bytes_allocated_ > 0) { |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 187 | map_->MadviseDontNeedAndZero(); |
| 188 | bytes_allocated_ = 0; |
| 189 | } |
| 190 | } |
| 191 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 192 | void Arena::Reset() { |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 193 | if (bytes_allocated_ > 0) { |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 194 | memset(Begin(), 0, bytes_allocated_); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 195 | bytes_allocated_ = 0; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 196 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Nicolas Geoffray | 25e0456 | 2016-03-01 13:17:58 +0000 | [diff] [blame] | 199 | ArenaPool::ArenaPool(bool use_malloc, bool low_4gb, const char* name) |
| 200 | : use_malloc_(use_malloc), |
| 201 | lock_("Arena pool lock", kArenaPoolLock), |
| 202 | free_arenas_(nullptr), |
| 203 | low_4gb_(low_4gb), |
| 204 | name_(name) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 205 | if (low_4gb) { |
| 206 | CHECK(!use_malloc) << "low4gb must use map implementation"; |
| 207 | } |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 208 | if (!use_malloc) { |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 209 | MemMap::Init(); |
| 210 | } |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | ArenaPool::~ArenaPool() { |
Jean-Philippe Halimi | ca76a1a | 2016-02-02 19:48:52 +0100 | [diff] [blame] | 214 | ReclaimMemory(); |
| 215 | } |
| 216 | |
| 217 | void ArenaPool::ReclaimMemory() { |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 218 | while (free_arenas_ != nullptr) { |
| 219 | auto* arena = free_arenas_; |
| 220 | free_arenas_ = free_arenas_->next_; |
| 221 | delete arena; |
| 222 | } |
| 223 | } |
| 224 | |
Jean-Philippe Halimi | ca76a1a | 2016-02-02 19:48:52 +0100 | [diff] [blame] | 225 | void ArenaPool::LockReclaimMemory() { |
| 226 | MutexLock lock(Thread::Current(), lock_); |
| 227 | ReclaimMemory(); |
| 228 | } |
| 229 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 230 | Arena* ArenaPool::AllocArena(size_t size) { |
| 231 | Thread* self = Thread::Current(); |
| 232 | Arena* ret = nullptr; |
| 233 | { |
| 234 | MutexLock lock(self, lock_); |
| 235 | if (free_arenas_ != nullptr && LIKELY(free_arenas_->Size() >= size)) { |
| 236 | ret = free_arenas_; |
| 237 | free_arenas_ = free_arenas_->next_; |
| 238 | } |
| 239 | } |
| 240 | if (ret == nullptr) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 241 | ret = use_malloc_ ? static_cast<Arena*>(new MallocArena(size)) : |
Nicolas Geoffray | 25e0456 | 2016-03-01 13:17:58 +0000 | [diff] [blame] | 242 | new MemMapArena(size, low_4gb_, name_); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 243 | } |
| 244 | ret->Reset(); |
| 245 | return ret; |
| 246 | } |
| 247 | |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 248 | void ArenaPool::TrimMaps() { |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 249 | if (!use_malloc_) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 250 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | c6201fa | 2015-03-12 10:06:33 -0700 | [diff] [blame] | 251 | // Doesn't work for malloc. |
| 252 | MutexLock lock(Thread::Current(), lock_); |
| 253 | for (auto* arena = free_arenas_; arena != nullptr; arena = arena->next_) { |
| 254 | arena->Release(); |
| 255 | } |
Mathieu Chartier | 9b34b24 | 2015-03-09 11:30:17 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Mathieu Chartier | 49285c5 | 2014-12-02 15:43:48 -0800 | [diff] [blame] | 259 | size_t ArenaPool::GetBytesAllocated() const { |
| 260 | size_t total = 0; |
| 261 | MutexLock lock(Thread::Current(), lock_); |
| 262 | for (Arena* arena = free_arenas_; arena != nullptr; arena = arena->next_) { |
| 263 | total += arena->GetBytesAllocated(); |
| 264 | } |
| 265 | return total; |
| 266 | } |
| 267 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 268 | void ArenaPool::FreeArenaChain(Arena* first) { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 269 | if (UNLIKELY(RUNNING_ON_MEMORY_TOOL > 0)) { |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 270 | for (Arena* arena = first; arena != nullptr; arena = arena->next_) { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 271 | MEMORY_TOOL_MAKE_UNDEFINED(arena->memory_, arena->bytes_allocated_); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 272 | } |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 273 | } |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 274 | if (first != nullptr) { |
| 275 | Arena* last = first; |
| 276 | while (last->next_ != nullptr) { |
| 277 | last = last->next_; |
| 278 | } |
| 279 | Thread* self = Thread::Current(); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 280 | MutexLock lock(self, lock_); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 281 | last->next_ = free_arenas_; |
| 282 | free_arenas_ = first; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | size_t ArenaAllocator::BytesAllocated() const { |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 287 | return ArenaAllocatorStats::BytesAllocated(); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 290 | size_t ArenaAllocator::BytesUsed() const { |
| 291 | size_t total = ptr_ - begin_; |
| 292 | if (arena_head_ != nullptr) { |
| 293 | for (Arena* cur_arena = arena_head_->next_; cur_arena != nullptr; |
| 294 | cur_arena = cur_arena->next_) { |
| 295 | total += cur_arena->GetBytesAllocated(); |
| 296 | } |
| 297 | } |
| 298 | return total; |
| 299 | } |
| 300 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 301 | ArenaAllocator::ArenaAllocator(ArenaPool* pool) |
| 302 | : pool_(pool), |
| 303 | begin_(nullptr), |
| 304 | end_(nullptr), |
| 305 | ptr_(nullptr), |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 306 | arena_head_(nullptr) { |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void ArenaAllocator::UpdateBytesAllocated() { |
| 310 | if (arena_head_ != nullptr) { |
| 311 | // Update how many bytes we have allocated into the arena so that the arena pool knows how |
| 312 | // much memory to zero out. |
| 313 | arena_head_->bytes_allocated_ = ptr_ - begin_; |
| 314 | } |
| 315 | } |
| 316 | |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 317 | void* ArenaAllocator::AllocWithMemoryTool(size_t bytes, ArenaAllocKind kind) { |
Vladimir Marko | 7500193 | 2015-11-10 20:54:22 +0000 | [diff] [blame] | 318 | // We mark all memory for a newly retrieved arena as inaccessible and then |
| 319 | // mark only the actually allocated memory as defined. That leaves red zones |
| 320 | // and padding between allocations marked as inaccessible. |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 321 | size_t rounded_bytes = RoundUp(bytes + kMemoryToolRedZoneBytes, 8); |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 322 | if (UNLIKELY(ptr_ + rounded_bytes > end_)) { |
| 323 | // Obtain a new block. |
| 324 | ObtainNewArenaForAllocation(rounded_bytes); |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 325 | CHECK(ptr_ != nullptr); |
Vladimir Marko | 7500193 | 2015-11-10 20:54:22 +0000 | [diff] [blame] | 326 | MEMORY_TOOL_MAKE_NOACCESS(ptr_, end_ - ptr_); |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 327 | } |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 328 | ArenaAllocatorStats::RecordAlloc(rounded_bytes, kind); |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 329 | uint8_t* ret = ptr_; |
| 330 | ptr_ += rounded_bytes; |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 331 | MEMORY_TOOL_MAKE_DEFINED(ret, bytes); |
Vladimir Marko | 7500193 | 2015-11-10 20:54:22 +0000 | [diff] [blame] | 332 | // Check that the memory is already zeroed out. |
| 333 | DCHECK(std::all_of(ret, ret + bytes, [](uint8_t val) { return val == 0u; })); |
Mathieu Chartier | 75165d0 | 2013-09-12 14:00:31 -0700 | [diff] [blame] | 334 | return ret; |
| 335 | } |
| 336 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 337 | ArenaAllocator::~ArenaAllocator() { |
| 338 | // Reclaim all the arenas by giving them back to the thread pool. |
| 339 | UpdateBytesAllocated(); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 340 | pool_->FreeArenaChain(arena_head_); |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void ArenaAllocator::ObtainNewArenaForAllocation(size_t allocation_size) { |
| 344 | UpdateBytesAllocated(); |
| 345 | Arena* new_arena = pool_->AllocArena(std::max(Arena::kDefaultSize, allocation_size)); |
| 346 | new_arena->next_ = arena_head_; |
| 347 | arena_head_ = new_arena; |
| 348 | // Update our internal data structures. |
| 349 | ptr_ = begin_ = new_arena->Begin(); |
| 350 | end_ = new_arena->End(); |
| 351 | } |
| 352 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 353 | bool ArenaAllocator::Contains(const void* ptr) const { |
| 354 | if (ptr >= begin_ && ptr < end_) { |
| 355 | return true; |
| 356 | } |
| 357 | for (const Arena* cur_arena = arena_head_; cur_arena != nullptr; cur_arena = cur_arena->next_) { |
| 358 | if (cur_arena->Contains(ptr)) { |
| 359 | return true; |
| 360 | } |
| 361 | } |
| 362 | return false; |
| 363 | } |
| 364 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 365 | MemStats::MemStats(const char* name, const ArenaAllocatorStats* stats, const Arena* first_arena, |
| 366 | ssize_t lost_bytes_adjustment) |
| 367 | : name_(name), |
| 368 | stats_(stats), |
| 369 | first_arena_(first_arena), |
| 370 | lost_bytes_adjustment_(lost_bytes_adjustment) { |
| 371 | } |
| 372 | |
| 373 | void MemStats::Dump(std::ostream& os) const { |
| 374 | os << name_ << " stats:\n"; |
| 375 | stats_->Dump(os, first_arena_, lost_bytes_adjustment_); |
| 376 | } |
| 377 | |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 378 | // Dump memory usage stats. |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 379 | MemStats ArenaAllocator::GetMemStats() const { |
| 380 | ssize_t lost_bytes_adjustment = |
| 381 | (arena_head_ == nullptr) ? 0 : (end_ - ptr_) - arena_head_->RemainingSpace(); |
| 382 | return MemStats("ArenaAllocator", this, arena_head_, lost_bytes_adjustment); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | } // namespace art |