blob: dd29404cd92ce032e0eba9e00a2f15841650735b [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
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000017#include <algorithm>
Ian Rogers6f3dbba2014-10-14 17:41:57 -070018#include <iomanip>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000019#include <numeric>
20
buzbee862a7602013-04-05 10:58:54 -070021#include "arena_allocator.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080022#include "logging.h"
23#include "mutex.h"
Ian Rogers02ed4c02013-09-06 13:10:04 -070024#include "thread-inl.h"
Mathieu Chartier75165d02013-09-12 14:00:31 -070025#include <memcheck/memcheck.h>
buzbee862a7602013-04-05 10:58:54 -070026
27namespace art {
28
Mathieu Chartier75165d02013-09-12 14:00:31 -070029static constexpr size_t kValgrindRedZoneBytes = 8;
Mark Mendell45c11652013-12-11 12:27:35 -080030constexpr size_t Arena::kDefaultSize;
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[] = {
buzbee862a7602013-04-05 10:58:54 -070034 "Misc ",
35 "BasicBlock ",
Vladimir Marko850cd162015-03-17 11:05:20 +000036 "BBList ",
Vladimir Markoe39c54e2014-09-22 14:50:02 +010037 "BBPreds ",
38 "DfsPreOrd ",
39 "DfsPostOrd ",
40 "DomPostOrd ",
41 "TopoOrd ",
42 "Lowering ",
buzbee862a7602013-04-05 10:58:54 -070043 "LIR ",
Vladimir Marko8dea81c2014-06-06 14:50:36 +010044 "LIR masks ",
Vladimir Markoe39c54e2014-09-22 14:50:02 +010045 "SwitchTbl ",
46 "FillArray ",
47 "SlowPaths ",
buzbee862a7602013-04-05 10:58:54 -070048 "MIR ",
49 "DataFlow ",
50 "GrowList ",
51 "GrowBitMap ",
Vladimir Markoe39c54e2014-09-22 14:50:02 +010052 "SSA2Dalvik ",
buzbee862a7602013-04-05 10:58:54 -070053 "Dalvik2SSA ",
54 "DebugInfo ",
55 "Successor ",
56 "RegAlloc ",
57 "Data ",
58 "Preds ",
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000059 "STL ",
buzbee862a7602013-04-05 10:58:54 -070060};
61
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000062template <bool kCount>
63ArenaAllocatorStatsImpl<kCount>::ArenaAllocatorStatsImpl()
64 : num_allocations_(0u) {
65 std::fill_n(alloc_stats_, arraysize(alloc_stats_), 0u);
66}
67
68template <bool kCount>
69void ArenaAllocatorStatsImpl<kCount>::Copy(const ArenaAllocatorStatsImpl& other) {
70 num_allocations_ = other.num_allocations_;
71 std::copy(other.alloc_stats_, other.alloc_stats_ + arraysize(alloc_stats_), alloc_stats_);
72}
73
74template <bool kCount>
75void ArenaAllocatorStatsImpl<kCount>::RecordAlloc(size_t bytes, ArenaAllocKind kind) {
76 alloc_stats_[kind] += bytes;
77 ++num_allocations_;
78}
79
80template <bool kCount>
81size_t ArenaAllocatorStatsImpl<kCount>::NumAllocations() const {
82 return num_allocations_;
83}
84
85template <bool kCount>
86size_t ArenaAllocatorStatsImpl<kCount>::BytesAllocated() const {
87 const size_t init = 0u; // Initial value of the correct type.
88 return std::accumulate(alloc_stats_, alloc_stats_ + arraysize(alloc_stats_), init);
89}
90
91template <bool kCount>
92void ArenaAllocatorStatsImpl<kCount>::Dump(std::ostream& os, const Arena* first,
93 ssize_t lost_bytes_adjustment) const {
94 size_t malloc_bytes = 0u;
95 size_t lost_bytes = 0u;
96 size_t num_arenas = 0u;
97 for (const Arena* arena = first; arena != nullptr; arena = arena->next_) {
98 malloc_bytes += arena->Size();
99 lost_bytes += arena->RemainingSpace();
100 ++num_arenas;
101 }
102 // The lost_bytes_adjustment is used to make up for the fact that the current arena
103 // may not have the bytes_allocated_ updated correctly.
104 lost_bytes += lost_bytes_adjustment;
105 const size_t bytes_allocated = BytesAllocated();
106 os << " MEM: used: " << bytes_allocated << ", allocated: " << malloc_bytes
107 << ", lost: " << lost_bytes << "\n";
Vladimir Markobd9e9db2014-03-07 19:41:05 +0000108 size_t num_allocations = NumAllocations();
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000109 if (num_allocations != 0) {
110 os << "Number of arenas allocated: " << num_arenas << ", Number of allocations: "
111 << num_allocations << ", avg size: " << bytes_allocated / num_allocations << "\n";
112 }
113 os << "===== Allocation by kind\n";
Andreas Gampe785d2f22014-11-03 22:57:30 -0800114 static_assert(arraysize(kAllocNames) == kNumArenaAllocKinds, "arraysize of kAllocNames");
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000115 for (int i = 0; i < kNumArenaAllocKinds; i++) {
Vladimir Markobd9e9db2014-03-07 19:41:05 +0000116 os << kAllocNames[i] << std::setw(10) << alloc_stats_[i] << "\n";
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000117 }
118}
119
120// Explicitly instantiate the used implementation.
121template class ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations>;
122
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700123Arena::Arena() : bytes_allocated_(0), next_(nullptr) {
Ian Rogerse7a5b7d2013-04-18 20:09:02 -0700124}
125
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700126MallocArena::MallocArena(size_t size) {
127 memory_ = reinterpret_cast<uint8_t*>(calloc(1, size));
128 size_ = size;
buzbee862a7602013-04-05 10:58:54 -0700129}
130
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700131MallocArena::~MallocArena() {
132 free(reinterpret_cast<void*>(memory_));
133}
134
135MemMapArena::MemMapArena(size_t size) {
136 std::string error_msg;
137 map_.reset(
138 MemMap::MapAnonymous("dalvik-LinearAlloc", nullptr, size, PROT_READ | PROT_WRITE, false,
139 false, &error_msg));
140 CHECK(map_.get() != nullptr) << error_msg;
141 memory_ = map_->Begin();
142 size_ = map_->Size();
143}
144
145void MemMapArena::Release() {
146 if (bytes_allocated_ > 0) {
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700147 map_->MadviseDontNeedAndZero();
148 bytes_allocated_ = 0;
149 }
150}
151
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700152void Arena::Reset() {
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700153 if (bytes_allocated_ > 0) {
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700154 memset(Begin(), 0, bytes_allocated_);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700155 bytes_allocated_ = 0;
buzbee862a7602013-04-05 10:58:54 -0700156 }
buzbee862a7602013-04-05 10:58:54 -0700157}
158
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700159ArenaPool::ArenaPool(bool use_malloc)
160 : use_malloc_(use_malloc), lock_("Arena pool lock"), free_arenas_(nullptr) {
161 if (!use_malloc) {
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700162 MemMap::Init();
163 }
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700164}
165
166ArenaPool::~ArenaPool() {
167 while (free_arenas_ != nullptr) {
168 auto* arena = free_arenas_;
169 free_arenas_ = free_arenas_->next_;
170 delete arena;
171 }
172}
173
174Arena* ArenaPool::AllocArena(size_t size) {
175 Thread* self = Thread::Current();
176 Arena* ret = nullptr;
177 {
178 MutexLock lock(self, lock_);
179 if (free_arenas_ != nullptr && LIKELY(free_arenas_->Size() >= size)) {
180 ret = free_arenas_;
181 free_arenas_ = free_arenas_->next_;
182 }
183 }
184 if (ret == nullptr) {
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700185 ret = use_malloc_ ? static_cast<Arena*>(new MallocArena(size)) : new MemMapArena(size);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700186 }
187 ret->Reset();
188 return ret;
189}
190
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700191void ArenaPool::TrimMaps() {
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700192 if (!use_malloc_) {
193 // Doesn't work for malloc.
194 MutexLock lock(Thread::Current(), lock_);
195 for (auto* arena = free_arenas_; arena != nullptr; arena = arena->next_) {
196 arena->Release();
197 }
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700198 }
199}
200
Mathieu Chartier49285c52014-12-02 15:43:48 -0800201size_t ArenaPool::GetBytesAllocated() const {
202 size_t total = 0;
203 MutexLock lock(Thread::Current(), lock_);
204 for (Arena* arena = free_arenas_; arena != nullptr; arena = arena->next_) {
205 total += arena->GetBytesAllocated();
206 }
207 return total;
208}
209
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000210void ArenaPool::FreeArenaChain(Arena* first) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800211 if (UNLIKELY(RUNNING_ON_VALGRIND > 0)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000212 for (Arena* arena = first; arena != nullptr; arena = arena->next_) {
213 VALGRIND_MAKE_MEM_UNDEFINED(arena->memory_, arena->bytes_allocated_);
214 }
Mathieu Chartier75165d02013-09-12 14:00:31 -0700215 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000216 if (first != nullptr) {
217 Arena* last = first;
218 while (last->next_ != nullptr) {
219 last = last->next_;
220 }
221 Thread* self = Thread::Current();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700222 MutexLock lock(self, lock_);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000223 last->next_ = free_arenas_;
224 free_arenas_ = first;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700225 }
226}
227
228size_t ArenaAllocator::BytesAllocated() const {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000229 return ArenaAllocatorStats::BytesAllocated();
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700230}
231
232ArenaAllocator::ArenaAllocator(ArenaPool* pool)
233 : pool_(pool),
234 begin_(nullptr),
235 end_(nullptr),
236 ptr_(nullptr),
237 arena_head_(nullptr),
Mathieu Chartier661974a2014-01-09 11:23:53 -0800238 running_on_valgrind_(RUNNING_ON_VALGRIND > 0) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700239}
240
241void ArenaAllocator::UpdateBytesAllocated() {
242 if (arena_head_ != nullptr) {
243 // Update how many bytes we have allocated into the arena so that the arena pool knows how
244 // much memory to zero out.
245 arena_head_->bytes_allocated_ = ptr_ - begin_;
246 }
247}
248
Mathieu Chartier75165d02013-09-12 14:00:31 -0700249void* ArenaAllocator::AllocValgrind(size_t bytes, ArenaAllocKind kind) {
Vladimir Marko22a0ef82014-06-10 14:47:51 +0100250 size_t rounded_bytes = RoundUp(bytes + kValgrindRedZoneBytes, 8);
Mathieu Chartier75165d02013-09-12 14:00:31 -0700251 if (UNLIKELY(ptr_ + rounded_bytes > end_)) {
252 // Obtain a new block.
253 ObtainNewArenaForAllocation(rounded_bytes);
254 if (UNLIKELY(ptr_ == nullptr)) {
255 return nullptr;
256 }
257 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000258 ArenaAllocatorStats::RecordAlloc(rounded_bytes, kind);
Mathieu Chartier75165d02013-09-12 14:00:31 -0700259 uint8_t* ret = ptr_;
260 ptr_ += rounded_bytes;
261 // Check that the memory is already zeroed out.
262 for (uint8_t* ptr = ret; ptr < ptr_; ++ptr) {
263 CHECK_EQ(*ptr, 0U);
264 }
265 VALGRIND_MAKE_MEM_NOACCESS(ret + bytes, rounded_bytes - bytes);
266 return ret;
267}
268
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700269ArenaAllocator::~ArenaAllocator() {
270 // Reclaim all the arenas by giving them back to the thread pool.
271 UpdateBytesAllocated();
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000272 pool_->FreeArenaChain(arena_head_);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700273}
274
275void ArenaAllocator::ObtainNewArenaForAllocation(size_t allocation_size) {
276 UpdateBytesAllocated();
277 Arena* new_arena = pool_->AllocArena(std::max(Arena::kDefaultSize, allocation_size));
278 new_arena->next_ = arena_head_;
279 arena_head_ = new_arena;
280 // Update our internal data structures.
281 ptr_ = begin_ = new_arena->Begin();
282 end_ = new_arena->End();
283}
284
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000285MemStats::MemStats(const char* name, const ArenaAllocatorStats* stats, const Arena* first_arena,
286 ssize_t lost_bytes_adjustment)
287 : name_(name),
288 stats_(stats),
289 first_arena_(first_arena),
290 lost_bytes_adjustment_(lost_bytes_adjustment) {
291}
292
293void MemStats::Dump(std::ostream& os) const {
294 os << name_ << " stats:\n";
295 stats_->Dump(os, first_arena_, lost_bytes_adjustment_);
296}
297
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700298// Dump memory usage stats.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000299MemStats ArenaAllocator::GetMemStats() const {
300 ssize_t lost_bytes_adjustment =
301 (arena_head_ == nullptr) ? 0 : (end_ - ptr_) - arena_head_->RemainingSpace();
302 return MemStats("ArenaAllocator", this, arena_head_, lost_bytes_adjustment);
buzbee862a7602013-04-05 10:58:54 -0700303}
304
305} // namespace art