blob: 47defb4ba26f26e496c05e391c59cb384ce927fe [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
Mathieu Chartierb666f482015-02-18 14:33:14 -080017#ifndef ART_RUNTIME_BASE_ARENA_ALLOCATOR_H_
18#define ART_RUNTIME_BASE_ARENA_ALLOCATOR_H_
buzbee862a7602013-04-05 10:58:54 -070019
20#include <stdint.h>
21#include <stddef.h>
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070022
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "base/bit_utils.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080024#include "debug_stack.h"
25#include "macros.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "mutex.h"
buzbee862a7602013-04-05 10:58:54 -070027
28namespace art {
29
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070030class Arena;
31class ArenaPool;
32class ArenaAllocator;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000033class ArenaStack;
34class ScopedArenaAllocator;
Vladimir Marko3481ba22015-04-13 12:22:36 +010035class MemMap;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000036class MemStats;
37
Vladimir Marko8081d2b2014-07-31 15:33:43 +010038template <typename T>
39class ArenaAllocatorAdapter;
40
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000041static constexpr bool kArenaAllocatorCountAllocations = false;
42
43// Type of allocation for memory tuning.
44enum ArenaAllocKind {
45 kArenaAllocMisc,
Vladimir Markoe39c54e2014-09-22 14:50:02 +010046 kArenaAllocBBList,
47 kArenaAllocBBPredecessors,
48 kArenaAllocDfsPreOrder,
49 kArenaAllocDfsPostOrder,
50 kArenaAllocDomPostOrder,
51 kArenaAllocTopologicalSortOrder,
52 kArenaAllocLoweringInfo,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000053 kArenaAllocLIR,
Vladimir Marko8dea81c2014-06-06 14:50:36 +010054 kArenaAllocLIRResourceMask,
Vladimir Markoe39c54e2014-09-22 14:50:02 +010055 kArenaAllocSwitchTable,
56 kArenaAllocFillArrayData,
57 kArenaAllocSlowPaths,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000058 kArenaAllocMIR,
59 kArenaAllocDFInfo,
60 kArenaAllocGrowableArray,
61 kArenaAllocGrowableBitMap,
Vladimir Markoe39c54e2014-09-22 14:50:02 +010062 kArenaAllocSSAToDalvikMap,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000063 kArenaAllocDalvikToSSAMap,
64 kArenaAllocDebugInfo,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000065 kArenaAllocRegAlloc,
66 kArenaAllocData,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000067 kArenaAllocSTL,
Vladimir Markof9f64412015-09-02 14:05:49 +010068 kArenaAllocGraph,
69 kArenaAllocBasicBlock,
Vladimir Marko60584552015-09-03 13:35:12 +000070 kArenaAllocPredecessors,
71 kArenaAllocSuccessors,
72 kArenaAllocDominated,
Vladimir Markof9f64412015-09-02 14:05:49 +010073 kArenaAllocInstruction,
74 kArenaAllocLoopInfo,
75 kArenaAllocTryCatchInfo,
76 kArenaAllocUseListNode,
77 kArenaAllocEnvironment,
78 kArenaAllocMoveOperands,
79 kArenaAllocCodeBuffer,
80 kArenaAllocStackMaps,
81 kArenaAllocBaselineMaps,
82 kArenaAllocOptimization,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000083 kNumArenaAllocKinds
84};
85
86template <bool kCount>
87class ArenaAllocatorStatsImpl;
88
89template <>
90class ArenaAllocatorStatsImpl<false> {
91 public:
92 ArenaAllocatorStatsImpl() = default;
93 ArenaAllocatorStatsImpl(const ArenaAllocatorStatsImpl& other) = default;
94 ArenaAllocatorStatsImpl& operator = (const ArenaAllocatorStatsImpl& other) = delete;
95
96 void Copy(const ArenaAllocatorStatsImpl& other) { UNUSED(other); }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070097 void RecordAlloc(size_t bytes, ArenaAllocKind kind) { UNUSED(bytes, kind); }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000098 size_t NumAllocations() const { return 0u; }
99 size_t BytesAllocated() const { return 0u; }
100 void Dump(std::ostream& os, const Arena* first, ssize_t lost_bytes_adjustment) const {
101 UNUSED(os); UNUSED(first); UNUSED(lost_bytes_adjustment);
102 }
103};
104
105template <bool kCount>
106class ArenaAllocatorStatsImpl {
107 public:
108 ArenaAllocatorStatsImpl();
109 ArenaAllocatorStatsImpl(const ArenaAllocatorStatsImpl& other) = default;
110 ArenaAllocatorStatsImpl& operator = (const ArenaAllocatorStatsImpl& other) = delete;
111
112 void Copy(const ArenaAllocatorStatsImpl& other);
113 void RecordAlloc(size_t bytes, ArenaAllocKind kind);
114 size_t NumAllocations() const;
115 size_t BytesAllocated() const;
116 void Dump(std::ostream& os, const Arena* first, ssize_t lost_bytes_adjustment) const;
117
118 private:
119 size_t num_allocations_;
120 // TODO: Use std::array<size_t, kNumArenaAllocKinds> from C++11 when we upgrade the STL.
121 size_t alloc_stats_[kNumArenaAllocKinds]; // Bytes used by various allocation kinds.
Vladimir Markobd9e9db2014-03-07 19:41:05 +0000122
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100123 static const char* const kAllocNames[];
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000124};
125
126typedef ArenaAllocatorStatsImpl<kArenaAllocatorCountAllocations> ArenaAllocatorStats;
buzbee862a7602013-04-05 10:58:54 -0700127
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700128class Arena {
129 public:
130 static constexpr size_t kDefaultSize = 128 * KB;
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700131 Arena();
132 virtual ~Arena() { }
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700133 // Reset is for pre-use and uses memset for performance.
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700134 void Reset();
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700135 // Release is used inbetween uses and uses madvise for memory usage.
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700136 virtual void Release() { }
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700137 uint8_t* Begin() {
138 return memory_;
buzbee862a7602013-04-05 10:58:54 -0700139 }
140
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700141 uint8_t* End() {
142 return memory_ + size_;
143 }
144
145 size_t Size() const {
146 return size_;
147 }
148
149 size_t RemainingSpace() const {
150 return Size() - bytes_allocated_;
151 }
152
Mathieu Chartier49285c52014-12-02 15:43:48 -0800153 size_t GetBytesAllocated() const {
154 return bytes_allocated_;
155 }
156
Mathieu Chartiere401d142015-04-22 13:56:20 -0700157 // Return true if ptr is contained in the arena.
158 bool Contains(const void* ptr) const {
159 return memory_ <= ptr && ptr < memory_ + bytes_allocated_;
160 }
161
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700162 protected:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700163 size_t bytes_allocated_;
164 uint8_t* memory_;
165 size_t size_;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700166 Arena* next_;
167 friend class ArenaPool;
168 friend class ArenaAllocator;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000169 friend class ArenaStack;
170 friend class ScopedArenaAllocator;
171 template <bool kCount> friend class ArenaAllocatorStatsImpl;
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700172
173 private:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700174 DISALLOW_COPY_AND_ASSIGN(Arena);
175};
176
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700177class MallocArena FINAL : public Arena {
178 public:
179 explicit MallocArena(size_t size = Arena::kDefaultSize);
180 virtual ~MallocArena();
181};
182
183class MemMapArena FINAL : public Arena {
184 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100185 MemMapArena(size_t size, bool low_4gb);
Vladimir Marko3481ba22015-04-13 12:22:36 +0100186 virtual ~MemMapArena();
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700187 void Release() OVERRIDE;
188
189 private:
190 std::unique_ptr<MemMap> map_;
191};
192
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700193class ArenaPool {
194 public:
Mathieu Chartierc7853442015-03-27 14:35:38 -0700195 explicit ArenaPool(bool use_malloc = true, bool low_4gb = false);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700196 ~ArenaPool();
Mathieu Chartier90443472015-07-16 20:32:27 -0700197 Arena* AllocArena(size_t size) REQUIRES(!lock_);
198 void FreeArenaChain(Arena* first) REQUIRES(!lock_);
199 size_t GetBytesAllocated() const REQUIRES(!lock_);
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700200 // Trim the maps in arenas by madvising, used by JIT to reduce memory usage. This only works
201 // use_malloc is false.
Mathieu Chartier90443472015-07-16 20:32:27 -0700202 void TrimMaps() REQUIRES(!lock_);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700203
204 private:
Mathieu Chartierc6201fa2015-03-12 10:06:33 -0700205 const bool use_malloc_;
Mathieu Chartier49285c52014-12-02 15:43:48 -0800206 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700207 Arena* free_arenas_ GUARDED_BY(lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700208 const bool low_4gb_;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700209 DISALLOW_COPY_AND_ASSIGN(ArenaPool);
210};
211
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100212class ArenaAllocator : private DebugStackRefCounter, private ArenaAllocatorStats {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700213 public:
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700214 explicit ArenaAllocator(ArenaPool* pool);
215 ~ArenaAllocator();
216
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100217 // Get adapter for use in STL containers. See arena_containers.h .
218 ArenaAllocatorAdapter<void> Adapter(ArenaAllocKind kind = kArenaAllocSTL);
219
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700220 // Returns zeroed memory.
Vladimir Markoe4fcc5b2015-02-13 10:28:29 +0000221 void* Alloc(size_t bytes, ArenaAllocKind kind = kArenaAllocMisc) ALWAYS_INLINE {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700222 if (UNLIKELY(is_running_on_memory_tool_)) {
Mathieu Chartier75165d02013-09-12 14:00:31 -0700223 return AllocValgrind(bytes, kind);
224 }
Mathieu Chartierb666f482015-02-18 14:33:14 -0800225 bytes = RoundUp(bytes, kAlignment);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700226 if (UNLIKELY(ptr_ + bytes > end_)) {
227 // Obtain a new block.
228 ObtainNewArenaForAllocation(bytes);
229 if (UNLIKELY(ptr_ == nullptr)) {
230 return nullptr;
231 }
232 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000233 ArenaAllocatorStats::RecordAlloc(bytes, kind);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700234 uint8_t* ret = ptr_;
235 ptr_ += bytes;
236 return ret;
237 }
238
Mathieu Chartiere401d142015-04-22 13:56:20 -0700239 // Realloc never frees the input pointer, it is the caller's job to do this if necessary.
240 void* Realloc(void* ptr, size_t ptr_size, size_t new_size,
241 ArenaAllocKind kind = kArenaAllocMisc) ALWAYS_INLINE {
242 DCHECK_GE(new_size, ptr_size);
243 DCHECK_EQ(ptr == nullptr, ptr_size == 0u);
244 auto* end = reinterpret_cast<uint8_t*>(ptr) + ptr_size;
245 // If we haven't allocated anything else, we can safely extend.
246 if (end == ptr_) {
247 const size_t size_delta = new_size - ptr_size;
248 // Check remain space.
249 const size_t remain = end_ - ptr_;
250 if (remain >= size_delta) {
251 ptr_ += size_delta;
252 ArenaAllocatorStats::RecordAlloc(size_delta, kind);
253 return ptr;
254 }
255 }
256 auto* new_ptr = Alloc(new_size, kind);
257 memcpy(new_ptr, ptr, ptr_size);
258 // TODO: Call free on ptr if linear alloc supports free.
259 return new_ptr;
260 }
261
Vladimir Markoe4fcc5b2015-02-13 10:28:29 +0000262 template <typename T>
263 T* AllocArray(size_t length, ArenaAllocKind kind = kArenaAllocMisc) {
264 return static_cast<T*>(Alloc(length * sizeof(T), kind));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100265 }
266
Mathieu Chartier75165d02013-09-12 14:00:31 -0700267 void* AllocValgrind(size_t bytes, ArenaAllocKind kind);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700269 void ObtainNewArenaForAllocation(size_t allocation_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700270
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700271 size_t BytesAllocated() const;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000273 MemStats GetMemStats() const;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700274
Mathieu Chartierc7853442015-03-27 14:35:38 -0700275 // The BytesUsed method sums up bytes allocated from arenas in arena_head_ and nodes.
276 // TODO: Change BytesAllocated to this behavior?
277 size_t BytesUsed() const;
buzbee862a7602013-04-05 10:58:54 -0700278
Mathieu Chartiere401d142015-04-22 13:56:20 -0700279 ArenaPool* GetArenaPool() const {
280 return pool_;
281 }
282
283 bool Contains(const void* ptr) const;
284
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700285 private:
Mathieu Chartierb666f482015-02-18 14:33:14 -0800286 static constexpr size_t kAlignment = 8;
287
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700288 void UpdateBytesAllocated();
buzbee862a7602013-04-05 10:58:54 -0700289
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700290 ArenaPool* pool_;
291 uint8_t* begin_;
292 uint8_t* end_;
293 uint8_t* ptr_;
294 Arena* arena_head_;
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700295 bool is_running_on_memory_tool_;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700296
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100297 template <typename U>
298 friend class ArenaAllocatorAdapter;
299
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700300 DISALLOW_COPY_AND_ASSIGN(ArenaAllocator);
buzbee862a7602013-04-05 10:58:54 -0700301}; // ArenaAllocator
302
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000303class MemStats {
304 public:
305 MemStats(const char* name, const ArenaAllocatorStats* stats, const Arena* first_arena,
306 ssize_t lost_bytes_adjustment = 0);
307 void Dump(std::ostream& os) const;
308
309 private:
310 const char* const name_;
311 const ArenaAllocatorStats* const stats_;
312 const Arena* const first_arena_;
313 const ssize_t lost_bytes_adjustment_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700314}; // MemStats
buzbee862a7602013-04-05 10:58:54 -0700315
316} // namespace art
317
Mathieu Chartierb666f482015-02-18 14:33:14 -0800318#endif // ART_RUNTIME_BASE_ARENA_ALLOCATOR_H_