blob: c9022f15c38e9a131f8c8e07b980fbf8b6bd817b [file] [log] [blame]
Ian Rogers1d54e732013-05-02 21:10:01 -07001/*
2 * Copyright (C) 2011 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_GC_SPACE_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_SPACE_H_
Ian Rogers1d54e732013-05-02 21:10:01 -070019
20#include <string>
21
22#include "UniquePtr.h"
23#include "base/macros.h"
24#include "base/mutex.h"
25#include "gc/accounting/space_bitmap.h"
26#include "globals.h"
27#include "image.h"
28#include "mem_map.h"
29
30namespace art {
31namespace mirror {
32 class Object;
33} // namespace mirror
34
35namespace gc {
36
37namespace accounting {
38 class SpaceBitmap;
Brian Carlstrom7934ac22013-07-26 10:54:15 -070039} // namespace accounting
Ian Rogers1d54e732013-05-02 21:10:01 -070040
41class Heap;
42
43namespace space {
44
Mathieu Chartier590fee92013-09-13 13:46:47 -070045class AllocSpace;
Mathieu Chartier7410f292013-11-24 13:17:35 -080046class BumpPointerSpace;
Mathieu Chartiera1602f22014-01-13 17:19:19 -080047class ContinuousMemMapAllocSpace;
Mathieu Chartier590fee92013-09-13 13:46:47 -070048class ContinuousSpace;
Mathieu Chartier590fee92013-09-13 13:46:47 -070049class DiscontinuousSpace;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070050class MallocSpace;
51class DlMallocSpace;
52class RosAllocSpace;
Ian Rogers1d54e732013-05-02 21:10:01 -070053class ImageSpace;
54class LargeObjectSpace;
Mathieu Chartiera1602f22014-01-13 17:19:19 -080055class ZygoteSpace;
Ian Rogers1d54e732013-05-02 21:10:01 -070056
Mathieu Chartier0f72e412013-09-06 16:40:01 -070057static constexpr bool kDebugSpaces = kIsDebugBuild;
Ian Rogers1d54e732013-05-02 21:10:01 -070058
59// See Space::GetGcRetentionPolicy.
60enum GcRetentionPolicy {
61 // Objects are retained forever with this policy for a space.
62 kGcRetentionPolicyNeverCollect,
63 // Every GC cycle will attempt to collect objects in this space.
64 kGcRetentionPolicyAlwaysCollect,
65 // Objects will be considered for collection only in "full" GC cycles, ie faster partial
66 // collections won't scan these areas such as the Zygote.
67 kGcRetentionPolicyFullCollect,
68};
69std::ostream& operator<<(std::ostream& os, const GcRetentionPolicy& policy);
70
71enum SpaceType {
72 kSpaceTypeImageSpace,
Mathieu Chartiera1602f22014-01-13 17:19:19 -080073 kSpaceTypeMallocSpace,
Ian Rogers1d54e732013-05-02 21:10:01 -070074 kSpaceTypeZygoteSpace,
Mathieu Chartier590fee92013-09-13 13:46:47 -070075 kSpaceTypeBumpPointerSpace,
Ian Rogers1d54e732013-05-02 21:10:01 -070076 kSpaceTypeLargeObjectSpace,
77};
78std::ostream& operator<<(std::ostream& os, const SpaceType& space_type);
79
80// A space contains memory allocated for managed objects.
81class Space {
82 public:
83 // Dump space. Also key method for C++ vtables.
84 virtual void Dump(std::ostream& os) const;
85
86 // Name of the space. May vary, for example before/after the Zygote fork.
87 const char* GetName() const {
88 return name_.c_str();
89 }
90
91 // The policy of when objects are collected associated with this space.
92 GcRetentionPolicy GetGcRetentionPolicy() const {
93 return gc_retention_policy_;
94 }
95
Ian Rogers1d54e732013-05-02 21:10:01 -070096 // Is the given object contained within this space?
97 virtual bool Contains(const mirror::Object* obj) const = 0;
98
99 // The kind of space this: image, alloc, zygote, large object.
100 virtual SpaceType GetType() const = 0;
101
102 // Is this an image space, ie one backed by a memory mapped image file.
103 bool IsImageSpace() const {
104 return GetType() == kSpaceTypeImageSpace;
105 }
106 ImageSpace* AsImageSpace();
107
108 // Is this a dlmalloc backed allocation space?
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700109 bool IsMallocSpace() const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700110 SpaceType type = GetType();
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800111 return type == kSpaceTypeMallocSpace;
Ian Rogers1d54e732013-05-02 21:10:01 -0700112 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700113 MallocSpace* AsMallocSpace();
114
115 virtual bool IsDlMallocSpace() const {
116 return false;
117 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800118 virtual DlMallocSpace* AsDlMallocSpace();
119
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700120 virtual bool IsRosAllocSpace() const {
121 return false;
122 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800123 virtual RosAllocSpace* AsRosAllocSpace();
Ian Rogers1d54e732013-05-02 21:10:01 -0700124
Ian Rogers6fac4472014-02-25 17:01:10 -0800125 // Is this the space allocated into by the Zygote and no-longer in use for allocation?
Ian Rogers1d54e732013-05-02 21:10:01 -0700126 bool IsZygoteSpace() const {
127 return GetType() == kSpaceTypeZygoteSpace;
128 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800129 virtual ZygoteSpace* AsZygoteSpace();
Ian Rogers1d54e732013-05-02 21:10:01 -0700130
Mathieu Chartier590fee92013-09-13 13:46:47 -0700131 // Is this space a bump pointer space?
132 bool IsBumpPointerSpace() const {
133 return GetType() == kSpaceTypeBumpPointerSpace;
134 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800135 virtual BumpPointerSpace* AsBumpPointerSpace();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700136
Ian Rogers1d54e732013-05-02 21:10:01 -0700137 // Does this space hold large objects and implement the large object space abstraction?
138 bool IsLargeObjectSpace() const {
139 return GetType() == kSpaceTypeLargeObjectSpace;
140 }
141 LargeObjectSpace* AsLargeObjectSpace();
142
Mathieu Chartier590fee92013-09-13 13:46:47 -0700143 virtual bool IsContinuousSpace() const {
144 return false;
145 }
146 ContinuousSpace* AsContinuousSpace();
147
148 virtual bool IsDiscontinuousSpace() const {
149 return false;
150 }
151 DiscontinuousSpace* AsDiscontinuousSpace();
152
153 virtual bool IsAllocSpace() const {
154 return false;
155 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800156 virtual AllocSpace* AsAllocSpace();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700157
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800158 virtual bool IsContinuousMemMapAllocSpace() const {
159 return false;
160 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800161 virtual ContinuousMemMapAllocSpace* AsContinuousMemMapAllocSpace();
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800162
Mathieu Chartier31f44142014-04-08 14:40:03 -0700163 // Returns true if objects in the space are movable.
164 virtual bool CanMoveObjects() const = 0;
165
Ian Rogers1d54e732013-05-02 21:10:01 -0700166 virtual ~Space() {}
167
168 protected:
169 Space(const std::string& name, GcRetentionPolicy gc_retention_policy);
170
171 void SetGcRetentionPolicy(GcRetentionPolicy gc_retention_policy) {
172 gc_retention_policy_ = gc_retention_policy;
173 }
174
175 // Name of the space that may vary due to the Zygote fork.
176 std::string name_;
177
Mathieu Chartier590fee92013-09-13 13:46:47 -0700178 protected:
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800179 struct SweepCallbackContext {
180 bool swap_bitmaps;
181 Heap* heap;
182 space::Space* space;
183 Thread* self;
184 size_t freed_objects;
185 size_t freed_bytes;
186 };
187
Ian Rogers1d54e732013-05-02 21:10:01 -0700188 // When should objects within this space be reclaimed? Not constant as we vary it in the case
189 // of Zygote forking.
190 GcRetentionPolicy gc_retention_policy_;
191
Mathieu Chartier590fee92013-09-13 13:46:47 -0700192 private:
Ian Rogers1d54e732013-05-02 21:10:01 -0700193 friend class art::gc::Heap;
Ian Rogers1d54e732013-05-02 21:10:01 -0700194 DISALLOW_COPY_AND_ASSIGN(Space);
195};
196std::ostream& operator<<(std::ostream& os, const Space& space);
197
198// AllocSpace interface.
199class AllocSpace {
200 public:
201 // Number of bytes currently allocated.
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700202 virtual uint64_t GetBytesAllocated() = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700203 // Number of objects currently allocated.
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700204 virtual uint64_t GetObjectsAllocated() = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700205
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700206 // Allocate num_bytes without allowing growth. If the allocation
207 // succeeds, the output parameter bytes_allocated will be set to the
208 // actually allocated bytes which is >= num_bytes.
Ian Rogers6fac4472014-02-25 17:01:10 -0800209 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
210 size_t* usable_size) = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700211
212 // Return the storage space required by obj.
Ian Rogers6fac4472014-02-25 17:01:10 -0800213 virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700214
215 // Returns how many bytes were freed.
216 virtual size_t Free(Thread* self, mirror::Object* ptr) = 0;
217
218 // Returns how many bytes were freed.
219 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) = 0;
220
Ian Rogers6fac4472014-02-25 17:01:10 -0800221 // Revoke any sort of thread-local buffers that are used to speed up allocations for the given
222 // thread, if the alloc space implementation uses any.
223 virtual void RevokeThreadLocalBuffers(Thread* thread) = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700224
Ian Rogers6fac4472014-02-25 17:01:10 -0800225 // Revoke any sort of thread-local buffers that are used to speed up allocations for all the
226 // threads, if the alloc space implementation uses any.
227 virtual void RevokeAllThreadLocalBuffers() = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700228
Ian Rogers1d54e732013-05-02 21:10:01 -0700229 protected:
230 AllocSpace() {}
231 virtual ~AllocSpace() {}
232
233 private:
234 DISALLOW_COPY_AND_ASSIGN(AllocSpace);
235};
236
237// Continuous spaces have bitmaps, and an address range. Although not required, objects within
238// continuous spaces can be marked in the card table.
239class ContinuousSpace : public Space {
240 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -0700241 // Address at which the space begins.
Ian Rogers1d54e732013-05-02 21:10:01 -0700242 byte* Begin() const {
243 return begin_;
244 }
245
Mathieu Chartier590fee92013-09-13 13:46:47 -0700246 // Current address at which the space ends, which may vary as the space is filled.
Ian Rogers1d54e732013-05-02 21:10:01 -0700247 byte* End() const {
248 return end_;
249 }
250
Mathieu Chartier590fee92013-09-13 13:46:47 -0700251 // The end of the address range covered by the space.
252 byte* Limit() const {
253 return limit_;
254 }
255
256 // Change the end of the space. Be careful with use since changing the end of a space to an
257 // invalid value may break the GC.
258 void SetEnd(byte* end) {
259 end_ = end;
260 }
261
262 void SetLimit(byte* limit) {
263 limit_ = limit;
264 }
265
Ian Rogers1d54e732013-05-02 21:10:01 -0700266 // Current size of space
267 size_t Size() const {
268 return End() - Begin();
269 }
270
271 virtual accounting::SpaceBitmap* GetLiveBitmap() const = 0;
272 virtual accounting::SpaceBitmap* GetMarkBitmap() const = 0;
273
Mathieu Chartier590fee92013-09-13 13:46:47 -0700274 // Maximum which the mapped space can grow to.
275 virtual size_t Capacity() const {
276 return Limit() - Begin();
277 }
278
Ian Rogers1d54e732013-05-02 21:10:01 -0700279 // Is object within this space? We check to see if the pointer is beyond the end first as
280 // continuous spaces are iterated over from low to high.
281 bool HasAddress(const mirror::Object* obj) const {
282 const byte* byte_ptr = reinterpret_cast<const byte*>(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700283 return byte_ptr >= Begin() && byte_ptr < Limit();
Ian Rogers1d54e732013-05-02 21:10:01 -0700284 }
285
286 bool Contains(const mirror::Object* obj) const {
287 return HasAddress(obj);
288 }
289
Mathieu Chartier590fee92013-09-13 13:46:47 -0700290 virtual bool IsContinuousSpace() const {
291 return true;
292 }
293
Ian Rogers1d54e732013-05-02 21:10:01 -0700294 virtual ~ContinuousSpace() {}
295
296 protected:
297 ContinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700298 byte* begin, byte* end, byte* limit) :
299 Space(name, gc_retention_policy), begin_(begin), end_(end), limit_(limit) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700300 }
301
Ian Rogers1d54e732013-05-02 21:10:01 -0700302 // The beginning of the storage for fast access.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700303 byte* begin_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700304
305 // Current end of the space.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700306 byte* volatile end_;
307
308 // Limit of the space.
309 byte* limit_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700310
311 private:
312 DISALLOW_COPY_AND_ASSIGN(ContinuousSpace);
313};
314
315// A space where objects may be allocated higgledy-piggledy throughout virtual memory. Currently
316// the card table can't cover these objects and so the write barrier shouldn't be triggered. This
317// is suitable for use for large primitive arrays.
318class DiscontinuousSpace : public Space {
319 public:
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800320 accounting::ObjectSet* GetLiveObjects() const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700321 return live_objects_.get();
322 }
323
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800324 accounting::ObjectSet* GetMarkObjects() const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700325 return mark_objects_.get();
326 }
327
Mathieu Chartier590fee92013-09-13 13:46:47 -0700328 virtual bool IsDiscontinuousSpace() const {
329 return true;
330 }
331
Ian Rogers1d54e732013-05-02 21:10:01 -0700332 virtual ~DiscontinuousSpace() {}
333
334 protected:
335 DiscontinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy);
336
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800337 UniquePtr<accounting::ObjectSet> live_objects_;
338 UniquePtr<accounting::ObjectSet> mark_objects_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700339
340 private:
341 DISALLOW_COPY_AND_ASSIGN(DiscontinuousSpace);
342};
343
344class MemMapSpace : public ContinuousSpace {
345 public:
Ian Rogers1d54e732013-05-02 21:10:01 -0700346 // Size of the space without a limit on its growth. By default this is just the Capacity, but
347 // for the allocation space we support starting with a small heap and then extending it.
348 virtual size_t NonGrowthLimitCapacity() const {
349 return Capacity();
350 }
351
Ian Rogers1d54e732013-05-02 21:10:01 -0700352 MemMap* GetMemMap() {
353 return mem_map_.get();
354 }
355
356 const MemMap* GetMemMap() const {
357 return mem_map_.get();
358 }
359
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800360 MemMap* ReleaseMemMap() {
361 return mem_map_.release();
362 }
363
Mathieu Chartier590fee92013-09-13 13:46:47 -0700364 protected:
365 MemMapSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end, byte* limit,
366 GcRetentionPolicy gc_retention_policy)
367 : ContinuousSpace(name, gc_retention_policy, begin, end, limit),
368 mem_map_(mem_map) {
369 }
370
Ian Rogers1d54e732013-05-02 21:10:01 -0700371 // Underlying storage of the space
372 UniquePtr<MemMap> mem_map_;
373
Mathieu Chartier590fee92013-09-13 13:46:47 -0700374 private:
Ian Rogers1d54e732013-05-02 21:10:01 -0700375 DISALLOW_COPY_AND_ASSIGN(MemMapSpace);
376};
377
Mathieu Chartier590fee92013-09-13 13:46:47 -0700378// Used by the heap compaction interface to enable copying from one type of alloc space to another.
379class ContinuousMemMapAllocSpace : public MemMapSpace, public AllocSpace {
380 public:
Ian Rogers6fac4472014-02-25 17:01:10 -0800381 bool IsAllocSpace() const OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700382 return true;
383 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800384 AllocSpace* AsAllocSpace() OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700385 return this;
386 }
387
Ian Rogers6fac4472014-02-25 17:01:10 -0800388 bool IsContinuousMemMapAllocSpace() const OVERRIDE {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800389 return true;
390 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800391 ContinuousMemMapAllocSpace* AsContinuousMemMapAllocSpace() {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800392 return this;
393 }
394
395 bool HasBoundBitmaps() const EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
396 void BindLiveToMarkBitmap()
397 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
398 void UnBindBitmaps() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier1f3b5352014-02-03 14:00:42 -0800399 // Swap the live and mark bitmaps of this space. This is used by the GC for concurrent sweeping.
400 void SwapBitmaps();
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800401
Mathieu Chartier31f44142014-04-08 14:40:03 -0700402 // Reset the space back to an empty space and release memory.
Ian Rogers6fac4472014-02-25 17:01:10 -0800403 virtual void Clear() = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700404
Ian Rogers6fac4472014-02-25 17:01:10 -0800405 accounting::SpaceBitmap* GetLiveBitmap() const {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800406 return live_bitmap_.get();
407 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800408
409 accounting::SpaceBitmap* GetMarkBitmap() const {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800410 return mark_bitmap_.get();
411 }
412
Ian Rogers6fac4472014-02-25 17:01:10 -0800413 void Sweep(bool swap_bitmaps, size_t* freed_objects, size_t* freed_bytes);
414 virtual accounting::SpaceBitmap::SweepCallback* GetSweepCallback() = 0;
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800415
Mathieu Chartier590fee92013-09-13 13:46:47 -0700416 protected:
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800417 UniquePtr<accounting::SpaceBitmap> live_bitmap_;
418 UniquePtr<accounting::SpaceBitmap> mark_bitmap_;
419 UniquePtr<accounting::SpaceBitmap> temp_bitmap_;
420
Mathieu Chartier590fee92013-09-13 13:46:47 -0700421 ContinuousMemMapAllocSpace(const std::string& name, MemMap* mem_map, byte* begin,
422 byte* end, byte* limit, GcRetentionPolicy gc_retention_policy)
423 : MemMapSpace(name, mem_map, begin, end, limit, gc_retention_policy) {
424 }
425
426 private:
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800427 friend class gc::Heap;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700428 DISALLOW_COPY_AND_ASSIGN(ContinuousMemMapAllocSpace);
429};
430
Ian Rogers1d54e732013-05-02 21:10:01 -0700431} // namespace space
432} // namespace gc
433} // namespace art
434
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700435#endif // ART_RUNTIME_GC_SPACE_SPACE_H_