blob: 0e7f6cceb387bfb4bb6b86812fe1022798998621 [file] [log] [blame]
buzbee862a7602013-04-05 10:58:54 -07001/*
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
Andreas Gamped4901292017-05-30 18:41:34 -070017#include "arena_allocator-inl.h"
18
Andreas Gamped4901292017-05-30 18:41:34 -070019
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000020#include <algorithm>
Vladimir Markof44d36c2017-03-14 14:18:46 +000021#include <cstddef>
Ian Rogers6f3dbba2014-10-14 17:41:57 -070022#include <iomanip>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000023#include <numeric>
24
Andreas Gampe57943812017-12-06 21:39:13 -080025#include <android-base/logging.h>
26
David Sehr10db8fe2018-07-18 11:01:20 -070027#include "mman.h"
28
buzbee862a7602013-04-05 10:58:54 -070029namespace art {
30
Vladimir Markof44d36c2017-03-14 14:18:46 +000031constexpr size_t kMemoryToolRedZoneBytes = 8;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070032
Vladimir Markobd9e9db2014-03-07 19:41:05 +000033template <bool kCount>
Vladimir Marko8dea81c2014-06-06 14:50:36 +010034const char* const ArenaAllocatorStatsImpl<kCount>::kAllocNames[] = {
Igor Murashkind01745e2017-04-05 16:40:31 -070035 // Every name should have the same width and end with a space. Abbreviate if necessary:
Vladimir Markof9f64412015-09-02 14:05:49 +010036 "Misc ",
Vladimir Markof9f64412015-09-02 14:05:49 +010037 "SwitchTbl ",
Vladimir Markof9f64412015-09-02 14:05:49 +010038 "SlowPaths ",
Vladimir Markof9f64412015-09-02 14:05:49 +010039 "GrowBitMap ",
Vladimir Markof9f64412015-09-02 14:05:49 +010040 "STL ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010041 "GraphBuilder ",
Vladimir Markof9f64412015-09-02 14:05:49 +010042 "Graph ",
43 "BasicBlock ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010044 "BlockList ",
45 "RevPostOrder ",
46 "LinearOrder ",
47 "ConstantsMap ",
Vladimir Marko60584552015-09-03 13:35:12 +000048 "Predecessors ",
49 "Successors ",
50 "Dominated ",
Vladimir Markof9f64412015-09-02 14:05:49 +010051 "Instruction ",
Igor Murashkind01745e2017-04-05 16:40:31 -070052 "CtorFenceIns ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010053 "InvokeInputs ",
54 "PhiInputs ",
Vladimir Marko175e7862018-03-27 09:03:13 +000055 "TypeCheckIns ",
Vladimir Markof9f64412015-09-02 14:05:49 +010056 "LoopInfo ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010057 "LIBackEdges ",
Vladimir Markof9f64412015-09-02 14:05:49 +010058 "TryCatchInf ",
59 "UseListNode ",
60 "Environment ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010061 "EnvVRegs ",
62 "EnvLocations ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010063 "LocSummary ",
Vladimir Marko71bf8092015-09-15 15:33:14 +010064 "SsaBuilder ",
Vladimir Markof9f64412015-09-02 14:05:49 +010065 "MoveOperands ",
66 "CodeBuffer ",
67 "StackMaps ",
Vladimir Markof9f64412015-09-02 14:05:49 +010068 "Optimization ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010069 "GVN ",
Vladimir Marko5233f932015-09-29 19:01:15 +010070 "InductionVar ",
71 "BCE ",
Vladimir Markof6a35de2016-03-21 12:01:50 +000072 "DCE ",
Vladimir Marko009d1662017-10-10 13:21:15 +010073 "LSA ",
Vladimir Markof6a35de2016-03-21 12:01:50 +000074 "LSE ",
Igor Murashkindd018df2017-08-09 10:38:31 -070075 "CFRE ",
Vladimir Markof6a35de2016-03-21 12:01:50 +000076 "LICM ",
Aart Bik96202302016-10-04 17:33:56 -070077 "LoopOpt ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010078 "SsaLiveness ",
79 "SsaPhiElim ",
80 "RefTypeProp ",
Aart Bik6d057002018-04-09 15:39:58 -070081 "SelectGen ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010082 "SideEffects ",
83 "RegAllocator ",
Vladimir Markof6a35de2016-03-21 12:01:50 +000084 "RegAllocVldt ",
Vladimir Marko225b6462015-09-28 12:17:40 +010085 "StackMapStm ",
David Srbeckydd966bc2018-05-24 13:55:52 +010086 "BitTableBld ",
Vladimir Markod38ba0a2017-04-13 13:43:43 +010087 "VectorNode ",
Vladimir Marko225b6462015-09-28 12:17:40 +010088 "CodeGen ",
Vladimir Marko93205e32016-04-13 11:59:46 +010089 "Assembler ",
Vladimir Marko225b6462015-09-28 12:17:40 +010090 "ParallelMove ",
Vladimir Marko655e5852015-10-12 10:38:28 +010091 "GraphChecker ",
Mathieu Chartierde40d472015-10-15 17:47:48 -070092 "Verifier ",
Vladimir Marko93205e32016-04-13 11:59:46 +010093 "CallingConv ",
Vladimir Marko4e335d02016-12-19 16:04:33 +000094 "CHA ",
Alexandre Rames22aa54b2016-10-18 09:32:29 +010095 "Scheduler ",
Calin Juravlecc3171a2017-05-19 16:47:53 -070096 "Profile ",
Artem Serov7f4aff62017-06-21 17:02:18 +010097 "SBCloner ",
buzbee862a7602013-04-05 10:58:54 -070098};
99
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000100template <bool kCount>
101ArenaAllocatorStatsImpl<kCount>::ArenaAllocatorStatsImpl()
Vladimir Marko4e335d02016-12-19 16:04:33 +0000102 : num_allocations_(0u),
103 alloc_stats_(kNumArenaAllocKinds, 0u) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000104}
105
106template <bool kCount>
107void ArenaAllocatorStatsImpl<kCount>::Copy(const ArenaAllocatorStatsImpl& other) {
108 num_allocations_ = other.num_allocations_;
Vladimir Marko4e335d02016-12-19 16:04:33 +0000109 std::copy_n(other.alloc_stats_.begin(), kNumArenaAllocKinds, alloc_stats_.begin());
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000110}
111
112template <bool kCount>
113void ArenaAllocatorStatsImpl<kCount>::RecordAlloc(size_t bytes, ArenaAllocKind kind) {
114 alloc_stats_[kind] += bytes;
115 ++num_allocations_;
116}
117
118template <bool kCount>
119size_t ArenaAllocatorStatsImpl<kCount>::NumAllocations() const {
120 return num_allocations_;
121}
122
123template <bool kCount>
124size_t ArenaAllocatorStatsImpl<kCount>::BytesAllocated() const {
125 const size_t init = 0u; // Initial value of the correct type.
Vladimir Marko4e335d02016-12-19 16:04:33 +0000126 return std::accumulate(alloc_stats_.begin(), alloc_stats_.end(), init);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000127}
128
129template <bool kCount>
130void ArenaAllocatorStatsImpl<kCount>::Dump(std::ostream& os, const Arena* first,
131 ssize_t lost_bytes_adjustment) const {
132 size_t malloc_bytes = 0u;
133 size_t lost_bytes = 0u;
134 size_t num_arenas = 0u;
135 for (const Arena* arena = first; arena != nullptr; arena = arena->next_) {
136 malloc_bytes += arena->Size();
137 lost_bytes += arena->RemainingSpace();
138 ++num_arenas;
139 }
140 // The lost_bytes_adjustment is used to make up for the fact that the current arena
141 // may not have the bytes_allocated_ updated correctly.
142 lost_bytes += lost_bytes_adjustment;
143 const size_t bytes_allocated = BytesAllocated();
144 os << " MEM: used: " << bytes_allocated << ", allocated: " << malloc_bytes
145 << ", lost: " << lost_bytes << "\n";
Vladimir Markobd9e9db2014-03-07 19:41:05 +0000146 size_t num_allocations = NumAllocations();
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000147 if (num_allocations != 0) {
148 os << "Number of arenas allocated: " << num_arenas << ", Number of allocations: "
149 << num_allocations << ", avg size: " << bytes_allocated / num_allocations << "\n";
150 }
151 os << "===== Allocation by kind\n";
Andreas Gampe785d2f22014-11-03 22:57:30 -0800152 static_assert(arraysize(kAllocNames) == kNumArenaAllocKinds, "arraysize of kAllocNames");
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000153 for (int i = 0; i < kNumArenaAllocKinds; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100154 // Reduce output by listing only allocation kinds that actually have allocations.
155 if (alloc_stats_[i] != 0u) {
Vladimir Markobd9e9db2014-03-07 19:41:05 +0000156 os << kAllocNames[i] << std::setw(10) << alloc_stats_[i] << "\n";
Vladimir Markoca6fff82017-10-03 14:49:14 +0100157 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000158 }
159}
160
Stephen Hines47b22472017-01-26 00:50:31 -0800161#pragma GCC diagnostic push
Jayant Chowdhary5f9da552017-02-02 12:48:10 -0800162#if __clang_major__ >= 4
Stephen Hines47b22472017-01-26 00:50:31 -0800163#pragma GCC diagnostic ignored "-Winstantiation-after-specialization"
Jayant Chowdhary5f9da552017-02-02 12:48:10 -0800164#endif
Vladimir Markod38ba0a2017-04-13 13:43:43 +0100165// We're going to use ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> which needs
166// to be explicitly instantiated if kArenaAllocatorCountAllocations is true. Explicit
167// instantiation of the specialization ArenaAllocatorStatsImpl<false> does not do anything
168// but requires the warning "-Winstantiation-after-specialization" to be turned off.
169//
170// To avoid bit-rot of the ArenaAllocatorStatsImpl<true>, instantiate it also in debug builds
171// (but keep the unnecessary code out of release builds) as we do not usually compile with
172// kArenaAllocatorCountAllocations set to true.
173template class ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations || kIsDebugBuild>;
Stephen Hines47b22472017-01-26 00:50:31 -0800174#pragma GCC diagnostic pop
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000175
Vladimir Marko7bda3b62015-10-07 12:44:31 +0000176void ArenaAllocatorMemoryTool::DoMakeDefined(void* ptr, size_t size) {
177 MEMORY_TOOL_MAKE_DEFINED(ptr, size);
178}
179
180void ArenaAllocatorMemoryTool::DoMakeUndefined(void* ptr, size_t size) {
181 MEMORY_TOOL_MAKE_UNDEFINED(ptr, size);
182}
183
184void ArenaAllocatorMemoryTool::DoMakeInaccessible(void* ptr, size_t size) {
185 MEMORY_TOOL_MAKE_NOACCESS(ptr, size);
186}
187
Andreas Gamped9911ee2017-03-27 13:27:24 -0700188Arena::Arena() : bytes_allocated_(0), memory_(nullptr), size_(0), next_(nullptr) {
Ian Rogerse7a5b7d2013-04-18 20:09:02 -0700189}
190
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700191size_t ArenaAllocator::BytesAllocated() const {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000192 return ArenaAllocatorStats::BytesAllocated();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700193}
194
Mathieu Chartierc7853442015-03-27 14:35:38 -0700195size_t ArenaAllocator::BytesUsed() const {
196 size_t total = ptr_ - begin_;
197 if (arena_head_ != nullptr) {
198 for (Arena* cur_arena = arena_head_->next_; cur_arena != nullptr;
199 cur_arena = cur_arena->next_) {
200 total += cur_arena->GetBytesAllocated();
201 }
202 }
203 return total;
204}
205
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700206ArenaAllocator::ArenaAllocator(ArenaPool* pool)
207 : pool_(pool),
208 begin_(nullptr),
209 end_(nullptr),
210 ptr_(nullptr),
Vladimir Marko2a408a32015-09-18 14:11:00 +0100211 arena_head_(nullptr) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700212}
213
214void ArenaAllocator::UpdateBytesAllocated() {
215 if (arena_head_ != nullptr) {
216 // Update how many bytes we have allocated into the arena so that the arena pool knows how
217 // much memory to zero out.
218 arena_head_->bytes_allocated_ = ptr_ - begin_;
219 }
220}
221
Vladimir Marko2a408a32015-09-18 14:11:00 +0100222void* ArenaAllocator::AllocWithMemoryTool(size_t bytes, ArenaAllocKind kind) {
Vladimir Marko75001932015-11-10 20:54:22 +0000223 // We mark all memory for a newly retrieved arena as inaccessible and then
224 // mark only the actually allocated memory as defined. That leaves red zones
225 // and padding between allocations marked as inaccessible.
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700226 size_t rounded_bytes = RoundUp(bytes + kMemoryToolRedZoneBytes, 8);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000227 ArenaAllocatorStats::RecordAlloc(rounded_bytes, kind);
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100228 uint8_t* ret;
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100229 if (UNLIKELY(rounded_bytes > static_cast<size_t>(end_ - ptr_))) {
Vladimir Markof44d36c2017-03-14 14:18:46 +0000230 ret = AllocFromNewArenaWithMemoryTool(rounded_bytes);
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100231 } else {
232 ret = ptr_;
233 ptr_ += rounded_bytes;
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100234 }
Vladimir Marko2a408a32015-09-18 14:11:00 +0100235 MEMORY_TOOL_MAKE_DEFINED(ret, bytes);
Vladimir Marko75001932015-11-10 20:54:22 +0000236 // Check that the memory is already zeroed out.
237 DCHECK(std::all_of(ret, ret + bytes, [](uint8_t val) { return val == 0u; }));
Mathieu Chartier75165d02013-09-12 14:00:31 -0700238 return ret;
239}
240
Vladimir Markof44d36c2017-03-14 14:18:46 +0000241void* ArenaAllocator::AllocWithMemoryToolAlign16(size_t bytes, ArenaAllocKind kind) {
242 // We mark all memory for a newly retrieved arena as inaccessible and then
243 // mark only the actually allocated memory as defined. That leaves red zones
244 // and padding between allocations marked as inaccessible.
245 size_t rounded_bytes = bytes + kMemoryToolRedZoneBytes;
246 DCHECK_ALIGNED(rounded_bytes, 8); // `bytes` is 16-byte aligned, red zone is 8-byte aligned.
247 uintptr_t padding =
248 ((reinterpret_cast<uintptr_t>(ptr_) + 15u) & 15u) - reinterpret_cast<uintptr_t>(ptr_);
249 ArenaAllocatorStats::RecordAlloc(rounded_bytes, kind);
250 uint8_t* ret;
251 if (UNLIKELY(padding + rounded_bytes > static_cast<size_t>(end_ - ptr_))) {
252 static_assert(kArenaAlignment >= 16, "Expecting sufficient alignment for new Arena.");
253 ret = AllocFromNewArenaWithMemoryTool(rounded_bytes);
254 } else {
255 ptr_ += padding; // Leave padding inaccessible.
256 ret = ptr_;
257 ptr_ += rounded_bytes;
258 }
259 MEMORY_TOOL_MAKE_DEFINED(ret, bytes);
260 // Check that the memory is already zeroed out.
261 DCHECK(std::all_of(ret, ret + bytes, [](uint8_t val) { return val == 0u; }));
262 return ret;
263}
264
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700265ArenaAllocator::~ArenaAllocator() {
266 // Reclaim all the arenas by giving them back to the thread pool.
267 UpdateBytesAllocated();
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000268 pool_->FreeArenaChain(arena_head_);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700269}
270
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100271uint8_t* ArenaAllocator::AllocFromNewArena(size_t bytes) {
Andreas Gampe121f1482017-05-12 10:28:35 -0700272 Arena* new_arena = pool_->AllocArena(std::max(arena_allocator::kArenaDefaultSize, bytes));
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100273 DCHECK(new_arena != nullptr);
274 DCHECK_LE(bytes, new_arena->Size());
275 if (static_cast<size_t>(end_ - ptr_) > new_arena->Size() - bytes) {
276 // The old arena has more space remaining than the new one, so keep using it.
277 // This can happen when the requested size is over half of the default size.
278 DCHECK(arena_head_ != nullptr);
279 new_arena->bytes_allocated_ = bytes; // UpdateBytesAllocated() on the new_arena.
280 new_arena->next_ = arena_head_->next_;
281 arena_head_->next_ = new_arena;
282 } else {
283 UpdateBytesAllocated();
284 new_arena->next_ = arena_head_;
285 arena_head_ = new_arena;
286 // Update our internal data structures.
287 begin_ = new_arena->Begin();
Andreas Gampef6dd8292016-08-19 20:22:19 -0700288 DCHECK_ALIGNED(begin_, kAlignment);
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100289 ptr_ = begin_ + bytes;
290 end_ = new_arena->End();
291 }
292 return new_arena->Begin();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700293}
294
Vladimir Markof44d36c2017-03-14 14:18:46 +0000295uint8_t* ArenaAllocator::AllocFromNewArenaWithMemoryTool(size_t bytes) {
296 uint8_t* ret = AllocFromNewArena(bytes);
297 uint8_t* noaccess_begin = ret + bytes;
298 uint8_t* noaccess_end;
299 if (ret == arena_head_->Begin()) {
300 DCHECK(ptr_ - bytes == ret);
301 noaccess_end = end_;
302 } else {
303 // We're still using the old arena but `ret` comes from a new one just after it.
304 DCHECK(arena_head_->next_ != nullptr);
305 DCHECK(ret == arena_head_->next_->Begin());
306 DCHECK_EQ(bytes, arena_head_->next_->GetBytesAllocated());
307 noaccess_end = arena_head_->next_->End();
308 }
309 MEMORY_TOOL_MAKE_NOACCESS(noaccess_begin, noaccess_end - noaccess_begin);
310 return ret;
311}
312
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313bool ArenaAllocator::Contains(const void* ptr) const {
314 if (ptr >= begin_ && ptr < end_) {
315 return true;
316 }
317 for (const Arena* cur_arena = arena_head_; cur_arena != nullptr; cur_arena = cur_arena->next_) {
318 if (cur_arena->Contains(ptr)) {
319 return true;
320 }
321 }
322 return false;
323}
324
Vladimir Markof44d36c2017-03-14 14:18:46 +0000325MemStats::MemStats(const char* name,
326 const ArenaAllocatorStats* stats,
327 const Arena* first_arena,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000328 ssize_t lost_bytes_adjustment)
329 : name_(name),
330 stats_(stats),
331 first_arena_(first_arena),
332 lost_bytes_adjustment_(lost_bytes_adjustment) {
333}
334
335void MemStats::Dump(std::ostream& os) const {
336 os << name_ << " stats:\n";
337 stats_->Dump(os, first_arena_, lost_bytes_adjustment_);
338}
339
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700340// Dump memory usage stats.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000341MemStats ArenaAllocator::GetMemStats() const {
342 ssize_t lost_bytes_adjustment =
343 (arena_head_ == nullptr) ? 0 : (end_ - ptr_) - arena_head_->RemainingSpace();
344 return MemStats("ArenaAllocator", this, arena_head_, lost_bytes_adjustment);
buzbee862a7602013-04-05 10:58:54 -0700345}
346
347} // namespace art