blob: 348a812e444baf530869e1c267666f83822078c8 [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
19#include <sys/mman.h>
20
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000021#include <algorithm>
Vladimir Markof44d36c2017-03-14 14:18:46 +000022#include <cstddef>
Ian Rogers6f3dbba2014-10-14 17:41:57 -070023#include <iomanip>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000024#include <numeric>
25
Andreas Gampe57943812017-12-06 21:39:13 -080026#include <android-base/logging.h>
27
buzbee862a7602013-04-05 10:58:54 -070028namespace art {
29
Vladimir Markof44d36c2017-03-14 14:18:46 +000030constexpr size_t kMemoryToolRedZoneBytes = 8;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070031
Vladimir Markobd9e9db2014-03-07 19:41:05 +000032template <bool kCount>
Vladimir Marko8dea81c2014-06-06 14:50:36 +010033const char* const ArenaAllocatorStatsImpl<kCount>::kAllocNames[] = {
Igor Murashkind01745e2017-04-05 16:40:31 -070034 // Every name should have the same width and end with a space. Abbreviate if necessary:
Vladimir Markof9f64412015-09-02 14:05:49 +010035 "Misc ",
Vladimir Markof9f64412015-09-02 14:05:49 +010036 "SwitchTbl ",
Vladimir Markof9f64412015-09-02 14:05:49 +010037 "SlowPaths ",
Vladimir Markof9f64412015-09-02 14:05:49 +010038 "GrowBitMap ",
Vladimir Markof9f64412015-09-02 14:05:49 +010039 "STL ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010040 "GraphBuilder ",
Vladimir Markof9f64412015-09-02 14:05:49 +010041 "Graph ",
42 "BasicBlock ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010043 "BlockList ",
44 "RevPostOrder ",
45 "LinearOrder ",
46 "ConstantsMap ",
Vladimir Marko60584552015-09-03 13:35:12 +000047 "Predecessors ",
48 "Successors ",
49 "Dominated ",
Vladimir Markof9f64412015-09-02 14:05:49 +010050 "Instruction ",
Igor Murashkind01745e2017-04-05 16:40:31 -070051 "CtorFenceIns ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010052 "InvokeInputs ",
53 "PhiInputs ",
Vladimir Marko175e7862018-03-27 09:03:13 +000054 "TypeCheckIns ",
Vladimir Markof9f64412015-09-02 14:05:49 +010055 "LoopInfo ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010056 "LIBackEdges ",
Vladimir Markof9f64412015-09-02 14:05:49 +010057 "TryCatchInf ",
58 "UseListNode ",
59 "Environment ",
Vladimir Markofa6b93c2015-09-15 10:15:55 +010060 "EnvVRegs ",
61 "EnvLocations ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010062 "LocSummary ",
Vladimir Marko71bf8092015-09-15 15:33:14 +010063 "SsaBuilder ",
Vladimir Markof9f64412015-09-02 14:05:49 +010064 "MoveOperands ",
65 "CodeBuffer ",
66 "StackMaps ",
Vladimir Markof9f64412015-09-02 14:05:49 +010067 "Optimization ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010068 "GVN ",
Vladimir Marko5233f932015-09-29 19:01:15 +010069 "InductionVar ",
70 "BCE ",
Vladimir Markof6a35de2016-03-21 12:01:50 +000071 "DCE ",
Vladimir Marko009d1662017-10-10 13:21:15 +010072 "LSA ",
Vladimir Markof6a35de2016-03-21 12:01:50 +000073 "LSE ",
Igor Murashkindd018df2017-08-09 10:38:31 -070074 "CFRE ",
Vladimir Markof6a35de2016-03-21 12:01:50 +000075 "LICM ",
Aart Bik96202302016-10-04 17:33:56 -070076 "LoopOpt ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010077 "SsaLiveness ",
78 "SsaPhiElim ",
79 "RefTypeProp ",
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010080 "SideEffects ",
81 "RegAllocator ",
Vladimir Markof6a35de2016-03-21 12:01:50 +000082 "RegAllocVldt ",
Vladimir Marko225b6462015-09-28 12:17:40 +010083 "StackMapStm ",
Vladimir Markod38ba0a2017-04-13 13:43:43 +010084 "VectorNode ",
Vladimir Marko225b6462015-09-28 12:17:40 +010085 "CodeGen ",
Vladimir Marko93205e32016-04-13 11:59:46 +010086 "Assembler ",
Vladimir Marko225b6462015-09-28 12:17:40 +010087 "ParallelMove ",
Vladimir Marko655e5852015-10-12 10:38:28 +010088 "GraphChecker ",
Mathieu Chartierde40d472015-10-15 17:47:48 -070089 "Verifier ",
Vladimir Marko93205e32016-04-13 11:59:46 +010090 "CallingConv ",
Vladimir Marko4e335d02016-12-19 16:04:33 +000091 "CHA ",
Alexandre Rames22aa54b2016-10-18 09:32:29 +010092 "Scheduler ",
Calin Juravlecc3171a2017-05-19 16:47:53 -070093 "Profile ",
Artem Serov7f4aff62017-06-21 17:02:18 +010094 "SBCloner ",
buzbee862a7602013-04-05 10:58:54 -070095};
96
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000097template <bool kCount>
98ArenaAllocatorStatsImpl<kCount>::ArenaAllocatorStatsImpl()
Vladimir Marko4e335d02016-12-19 16:04:33 +000099 : num_allocations_(0u),
100 alloc_stats_(kNumArenaAllocKinds, 0u) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000101}
102
103template <bool kCount>
104void ArenaAllocatorStatsImpl<kCount>::Copy(const ArenaAllocatorStatsImpl& other) {
105 num_allocations_ = other.num_allocations_;
Vladimir Marko4e335d02016-12-19 16:04:33 +0000106 std::copy_n(other.alloc_stats_.begin(), kNumArenaAllocKinds, alloc_stats_.begin());
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000107}
108
109template <bool kCount>
110void ArenaAllocatorStatsImpl<kCount>::RecordAlloc(size_t bytes, ArenaAllocKind kind) {
111 alloc_stats_[kind] += bytes;
112 ++num_allocations_;
113}
114
115template <bool kCount>
116size_t ArenaAllocatorStatsImpl<kCount>::NumAllocations() const {
117 return num_allocations_;
118}
119
120template <bool kCount>
121size_t ArenaAllocatorStatsImpl<kCount>::BytesAllocated() const {
122 const size_t init = 0u; // Initial value of the correct type.
Vladimir Marko4e335d02016-12-19 16:04:33 +0000123 return std::accumulate(alloc_stats_.begin(), alloc_stats_.end(), init);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000124}
125
126template <bool kCount>
127void ArenaAllocatorStatsImpl<kCount>::Dump(std::ostream& os, const Arena* first,
128 ssize_t lost_bytes_adjustment) const {
129 size_t malloc_bytes = 0u;
130 size_t lost_bytes = 0u;
131 size_t num_arenas = 0u;
132 for (const Arena* arena = first; arena != nullptr; arena = arena->next_) {
133 malloc_bytes += arena->Size();
134 lost_bytes += arena->RemainingSpace();
135 ++num_arenas;
136 }
137 // The lost_bytes_adjustment is used to make up for the fact that the current arena
138 // may not have the bytes_allocated_ updated correctly.
139 lost_bytes += lost_bytes_adjustment;
140 const size_t bytes_allocated = BytesAllocated();
141 os << " MEM: used: " << bytes_allocated << ", allocated: " << malloc_bytes
142 << ", lost: " << lost_bytes << "\n";
Vladimir Markobd9e9db2014-03-07 19:41:05 +0000143 size_t num_allocations = NumAllocations();
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000144 if (num_allocations != 0) {
145 os << "Number of arenas allocated: " << num_arenas << ", Number of allocations: "
146 << num_allocations << ", avg size: " << bytes_allocated / num_allocations << "\n";
147 }
148 os << "===== Allocation by kind\n";
Andreas Gampe785d2f22014-11-03 22:57:30 -0800149 static_assert(arraysize(kAllocNames) == kNumArenaAllocKinds, "arraysize of kAllocNames");
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000150 for (int i = 0; i < kNumArenaAllocKinds; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100151 // Reduce output by listing only allocation kinds that actually have allocations.
152 if (alloc_stats_[i] != 0u) {
Vladimir Markobd9e9db2014-03-07 19:41:05 +0000153 os << kAllocNames[i] << std::setw(10) << alloc_stats_[i] << "\n";
Vladimir Markoca6fff82017-10-03 14:49:14 +0100154 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000155 }
156}
157
Stephen Hines47b22472017-01-26 00:50:31 -0800158#pragma GCC diagnostic push
Jayant Chowdhary5f9da552017-02-02 12:48:10 -0800159#if __clang_major__ >= 4
Stephen Hines47b22472017-01-26 00:50:31 -0800160#pragma GCC diagnostic ignored "-Winstantiation-after-specialization"
Jayant Chowdhary5f9da552017-02-02 12:48:10 -0800161#endif
Vladimir Markod38ba0a2017-04-13 13:43:43 +0100162// We're going to use ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> which needs
163// to be explicitly instantiated if kArenaAllocatorCountAllocations is true. Explicit
164// instantiation of the specialization ArenaAllocatorStatsImpl<false> does not do anything
165// but requires the warning "-Winstantiation-after-specialization" to be turned off.
166//
167// To avoid bit-rot of the ArenaAllocatorStatsImpl<true>, instantiate it also in debug builds
168// (but keep the unnecessary code out of release builds) as we do not usually compile with
169// kArenaAllocatorCountAllocations set to true.
170template class ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations || kIsDebugBuild>;
Stephen Hines47b22472017-01-26 00:50:31 -0800171#pragma GCC diagnostic pop
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000172
Vladimir Marko7bda3b62015-10-07 12:44:31 +0000173void ArenaAllocatorMemoryTool::DoMakeDefined(void* ptr, size_t size) {
174 MEMORY_TOOL_MAKE_DEFINED(ptr, size);
175}
176
177void ArenaAllocatorMemoryTool::DoMakeUndefined(void* ptr, size_t size) {
178 MEMORY_TOOL_MAKE_UNDEFINED(ptr, size);
179}
180
181void ArenaAllocatorMemoryTool::DoMakeInaccessible(void* ptr, size_t size) {
182 MEMORY_TOOL_MAKE_NOACCESS(ptr, size);
183}
184
Andreas Gamped9911ee2017-03-27 13:27:24 -0700185Arena::Arena() : bytes_allocated_(0), memory_(nullptr), size_(0), next_(nullptr) {
Ian Rogerse7a5b7d2013-04-18 20:09:02 -0700186}
187
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700188size_t ArenaAllocator::BytesAllocated() const {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000189 return ArenaAllocatorStats::BytesAllocated();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700190}
191
Mathieu Chartierc7853442015-03-27 14:35:38 -0700192size_t ArenaAllocator::BytesUsed() const {
193 size_t total = ptr_ - begin_;
194 if (arena_head_ != nullptr) {
195 for (Arena* cur_arena = arena_head_->next_; cur_arena != nullptr;
196 cur_arena = cur_arena->next_) {
197 total += cur_arena->GetBytesAllocated();
198 }
199 }
200 return total;
201}
202
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700203ArenaAllocator::ArenaAllocator(ArenaPool* pool)
204 : pool_(pool),
205 begin_(nullptr),
206 end_(nullptr),
207 ptr_(nullptr),
Vladimir Marko2a408a32015-09-18 14:11:00 +0100208 arena_head_(nullptr) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700209}
210
211void ArenaAllocator::UpdateBytesAllocated() {
212 if (arena_head_ != nullptr) {
213 // Update how many bytes we have allocated into the arena so that the arena pool knows how
214 // much memory to zero out.
215 arena_head_->bytes_allocated_ = ptr_ - begin_;
216 }
217}
218
Vladimir Marko2a408a32015-09-18 14:11:00 +0100219void* ArenaAllocator::AllocWithMemoryTool(size_t bytes, ArenaAllocKind kind) {
Vladimir Marko75001932015-11-10 20:54:22 +0000220 // We mark all memory for a newly retrieved arena as inaccessible and then
221 // mark only the actually allocated memory as defined. That leaves red zones
222 // and padding between allocations marked as inaccessible.
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700223 size_t rounded_bytes = RoundUp(bytes + kMemoryToolRedZoneBytes, 8);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000224 ArenaAllocatorStats::RecordAlloc(rounded_bytes, kind);
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100225 uint8_t* ret;
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100226 if (UNLIKELY(rounded_bytes > static_cast<size_t>(end_ - ptr_))) {
Vladimir Markof44d36c2017-03-14 14:18:46 +0000227 ret = AllocFromNewArenaWithMemoryTool(rounded_bytes);
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100228 } else {
229 ret = ptr_;
230 ptr_ += rounded_bytes;
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100231 }
Vladimir Marko2a408a32015-09-18 14:11:00 +0100232 MEMORY_TOOL_MAKE_DEFINED(ret, bytes);
Vladimir Marko75001932015-11-10 20:54:22 +0000233 // Check that the memory is already zeroed out.
234 DCHECK(std::all_of(ret, ret + bytes, [](uint8_t val) { return val == 0u; }));
Mathieu Chartier75165d02013-09-12 14:00:31 -0700235 return ret;
236}
237
Vladimir Markof44d36c2017-03-14 14:18:46 +0000238void* ArenaAllocator::AllocWithMemoryToolAlign16(size_t bytes, ArenaAllocKind kind) {
239 // We mark all memory for a newly retrieved arena as inaccessible and then
240 // mark only the actually allocated memory as defined. That leaves red zones
241 // and padding between allocations marked as inaccessible.
242 size_t rounded_bytes = bytes + kMemoryToolRedZoneBytes;
243 DCHECK_ALIGNED(rounded_bytes, 8); // `bytes` is 16-byte aligned, red zone is 8-byte aligned.
244 uintptr_t padding =
245 ((reinterpret_cast<uintptr_t>(ptr_) + 15u) & 15u) - reinterpret_cast<uintptr_t>(ptr_);
246 ArenaAllocatorStats::RecordAlloc(rounded_bytes, kind);
247 uint8_t* ret;
248 if (UNLIKELY(padding + rounded_bytes > static_cast<size_t>(end_ - ptr_))) {
249 static_assert(kArenaAlignment >= 16, "Expecting sufficient alignment for new Arena.");
250 ret = AllocFromNewArenaWithMemoryTool(rounded_bytes);
251 } else {
252 ptr_ += padding; // Leave padding inaccessible.
253 ret = ptr_;
254 ptr_ += rounded_bytes;
255 }
256 MEMORY_TOOL_MAKE_DEFINED(ret, bytes);
257 // Check that the memory is already zeroed out.
258 DCHECK(std::all_of(ret, ret + bytes, [](uint8_t val) { return val == 0u; }));
259 return ret;
260}
261
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700262ArenaAllocator::~ArenaAllocator() {
263 // Reclaim all the arenas by giving them back to the thread pool.
264 UpdateBytesAllocated();
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000265 pool_->FreeArenaChain(arena_head_);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700266}
267
Vladimir Marko3f84f2c2016-04-25 19:40:34 +0100268uint8_t* ArenaAllocator::AllocFromNewArena(size_t bytes) {
Andreas Gampe121f1482017-05-12 10:28:35 -0700269 Arena* new_arena = pool_->AllocArena(std::max(arena_allocator::kArenaDefaultSize, bytes));
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100270 DCHECK(new_arena != nullptr);
271 DCHECK_LE(bytes, new_arena->Size());
272 if (static_cast<size_t>(end_ - ptr_) > new_arena->Size() - bytes) {
273 // The old arena has more space remaining than the new one, so keep using it.
274 // This can happen when the requested size is over half of the default size.
275 DCHECK(arena_head_ != nullptr);
276 new_arena->bytes_allocated_ = bytes; // UpdateBytesAllocated() on the new_arena.
277 new_arena->next_ = arena_head_->next_;
278 arena_head_->next_ = new_arena;
279 } else {
280 UpdateBytesAllocated();
281 new_arena->next_ = arena_head_;
282 arena_head_ = new_arena;
283 // Update our internal data structures.
284 begin_ = new_arena->Begin();
Andreas Gampef6dd8292016-08-19 20:22:19 -0700285 DCHECK_ALIGNED(begin_, kAlignment);
Vladimir Marko3e0e7172016-04-22 18:07:13 +0100286 ptr_ = begin_ + bytes;
287 end_ = new_arena->End();
288 }
289 return new_arena->Begin();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700290}
291
Vladimir Markof44d36c2017-03-14 14:18:46 +0000292uint8_t* ArenaAllocator::AllocFromNewArenaWithMemoryTool(size_t bytes) {
293 uint8_t* ret = AllocFromNewArena(bytes);
294 uint8_t* noaccess_begin = ret + bytes;
295 uint8_t* noaccess_end;
296 if (ret == arena_head_->Begin()) {
297 DCHECK(ptr_ - bytes == ret);
298 noaccess_end = end_;
299 } else {
300 // We're still using the old arena but `ret` comes from a new one just after it.
301 DCHECK(arena_head_->next_ != nullptr);
302 DCHECK(ret == arena_head_->next_->Begin());
303 DCHECK_EQ(bytes, arena_head_->next_->GetBytesAllocated());
304 noaccess_end = arena_head_->next_->End();
305 }
306 MEMORY_TOOL_MAKE_NOACCESS(noaccess_begin, noaccess_end - noaccess_begin);
307 return ret;
308}
309
Mathieu Chartiere401d142015-04-22 13:56:20 -0700310bool ArenaAllocator::Contains(const void* ptr) const {
311 if (ptr >= begin_ && ptr < end_) {
312 return true;
313 }
314 for (const Arena* cur_arena = arena_head_; cur_arena != nullptr; cur_arena = cur_arena->next_) {
315 if (cur_arena->Contains(ptr)) {
316 return true;
317 }
318 }
319 return false;
320}
321
Vladimir Markof44d36c2017-03-14 14:18:46 +0000322MemStats::MemStats(const char* name,
323 const ArenaAllocatorStats* stats,
324 const Arena* first_arena,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000325 ssize_t lost_bytes_adjustment)
326 : name_(name),
327 stats_(stats),
328 first_arena_(first_arena),
329 lost_bytes_adjustment_(lost_bytes_adjustment) {
330}
331
332void MemStats::Dump(std::ostream& os) const {
333 os << name_ << " stats:\n";
334 stats_->Dump(os, first_arena_, lost_bytes_adjustment_);
335}
336
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700337// Dump memory usage stats.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000338MemStats ArenaAllocator::GetMemStats() const {
339 ssize_t lost_bytes_adjustment =
340 (arena_head_ == nullptr) ? 0 : (end_ - ptr_) - arena_head_->RemainingSpace();
341 return MemStats("ArenaAllocator", this, arena_head_, lost_bytes_adjustment);
buzbee862a7602013-04-05 10:58:54 -0700342}
343
344} // namespace art