blob: 0a87a160b3814990a7db1811511230d70ee47f26 [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
Ian Rogers1d54e732013-05-02 21:10:01 -070037class Heap;
38
39namespace space {
40
Mathieu Chartier590fee92013-09-13 13:46:47 -070041class AllocSpace;
Mathieu Chartier7410f292013-11-24 13:17:35 -080042class BumpPointerSpace;
Mathieu Chartiera1602f22014-01-13 17:19:19 -080043class ContinuousMemMapAllocSpace;
Mathieu Chartier590fee92013-09-13 13:46:47 -070044class ContinuousSpace;
Mathieu Chartier590fee92013-09-13 13:46:47 -070045class DiscontinuousSpace;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070046class MallocSpace;
47class DlMallocSpace;
48class RosAllocSpace;
Ian Rogers1d54e732013-05-02 21:10:01 -070049class ImageSpace;
50class LargeObjectSpace;
Mathieu Chartiera1602f22014-01-13 17:19:19 -080051class ZygoteSpace;
Ian Rogers1d54e732013-05-02 21:10:01 -070052
Mathieu Chartier0f72e412013-09-06 16:40:01 -070053static constexpr bool kDebugSpaces = kIsDebugBuild;
Ian Rogers1d54e732013-05-02 21:10:01 -070054
55// See Space::GetGcRetentionPolicy.
56enum GcRetentionPolicy {
57 // Objects are retained forever with this policy for a space.
58 kGcRetentionPolicyNeverCollect,
59 // Every GC cycle will attempt to collect objects in this space.
60 kGcRetentionPolicyAlwaysCollect,
61 // Objects will be considered for collection only in "full" GC cycles, ie faster partial
62 // collections won't scan these areas such as the Zygote.
63 kGcRetentionPolicyFullCollect,
64};
65std::ostream& operator<<(std::ostream& os, const GcRetentionPolicy& policy);
66
67enum SpaceType {
68 kSpaceTypeImageSpace,
Mathieu Chartiera1602f22014-01-13 17:19:19 -080069 kSpaceTypeMallocSpace,
Ian Rogers1d54e732013-05-02 21:10:01 -070070 kSpaceTypeZygoteSpace,
Mathieu Chartier590fee92013-09-13 13:46:47 -070071 kSpaceTypeBumpPointerSpace,
Ian Rogers1d54e732013-05-02 21:10:01 -070072 kSpaceTypeLargeObjectSpace,
73};
74std::ostream& operator<<(std::ostream& os, const SpaceType& space_type);
75
76// A space contains memory allocated for managed objects.
77class Space {
78 public:
79 // Dump space. Also key method for C++ vtables.
80 virtual void Dump(std::ostream& os) const;
81
82 // Name of the space. May vary, for example before/after the Zygote fork.
83 const char* GetName() const {
84 return name_.c_str();
85 }
86
87 // The policy of when objects are collected associated with this space.
88 GcRetentionPolicy GetGcRetentionPolicy() const {
89 return gc_retention_policy_;
90 }
91
Ian Rogers1d54e732013-05-02 21:10:01 -070092 // Is the given object contained within this space?
93 virtual bool Contains(const mirror::Object* obj) const = 0;
94
95 // The kind of space this: image, alloc, zygote, large object.
96 virtual SpaceType GetType() const = 0;
97
98 // Is this an image space, ie one backed by a memory mapped image file.
99 bool IsImageSpace() const {
100 return GetType() == kSpaceTypeImageSpace;
101 }
102 ImageSpace* AsImageSpace();
103
104 // Is this a dlmalloc backed allocation space?
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700105 bool IsMallocSpace() const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700106 SpaceType type = GetType();
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800107 return type == kSpaceTypeMallocSpace;
Ian Rogers1d54e732013-05-02 21:10:01 -0700108 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700109 MallocSpace* AsMallocSpace();
110
111 virtual bool IsDlMallocSpace() const {
112 return false;
113 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800114 virtual DlMallocSpace* AsDlMallocSpace();
115
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700116 virtual bool IsRosAllocSpace() const {
117 return false;
118 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800119 virtual RosAllocSpace* AsRosAllocSpace();
Ian Rogers1d54e732013-05-02 21:10:01 -0700120
Ian Rogers6fac4472014-02-25 17:01:10 -0800121 // Is this the space allocated into by the Zygote and no-longer in use for allocation?
Ian Rogers1d54e732013-05-02 21:10:01 -0700122 bool IsZygoteSpace() const {
123 return GetType() == kSpaceTypeZygoteSpace;
124 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800125 virtual ZygoteSpace* AsZygoteSpace();
Ian Rogers1d54e732013-05-02 21:10:01 -0700126
Mathieu Chartier590fee92013-09-13 13:46:47 -0700127 // Is this space a bump pointer space?
128 bool IsBumpPointerSpace() const {
129 return GetType() == kSpaceTypeBumpPointerSpace;
130 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800131 virtual BumpPointerSpace* AsBumpPointerSpace();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700132
Ian Rogers1d54e732013-05-02 21:10:01 -0700133 // Does this space hold large objects and implement the large object space abstraction?
134 bool IsLargeObjectSpace() const {
135 return GetType() == kSpaceTypeLargeObjectSpace;
136 }
137 LargeObjectSpace* AsLargeObjectSpace();
138
Mathieu Chartier590fee92013-09-13 13:46:47 -0700139 virtual bool IsContinuousSpace() const {
140 return false;
141 }
142 ContinuousSpace* AsContinuousSpace();
143
144 virtual bool IsDiscontinuousSpace() const {
145 return false;
146 }
147 DiscontinuousSpace* AsDiscontinuousSpace();
148
149 virtual bool IsAllocSpace() const {
150 return false;
151 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800152 virtual AllocSpace* AsAllocSpace();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700153
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800154 virtual bool IsContinuousMemMapAllocSpace() const {
155 return false;
156 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800157 virtual ContinuousMemMapAllocSpace* AsContinuousMemMapAllocSpace();
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800158
Mathieu Chartier31f44142014-04-08 14:40:03 -0700159 // Returns true if objects in the space are movable.
160 virtual bool CanMoveObjects() const = 0;
161
Ian Rogers1d54e732013-05-02 21:10:01 -0700162 virtual ~Space() {}
163
164 protected:
165 Space(const std::string& name, GcRetentionPolicy gc_retention_policy);
166
167 void SetGcRetentionPolicy(GcRetentionPolicy gc_retention_policy) {
168 gc_retention_policy_ = gc_retention_policy;
169 }
170
171 // Name of the space that may vary due to the Zygote fork.
172 std::string name_;
173
Mathieu Chartier590fee92013-09-13 13:46:47 -0700174 protected:
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800175 struct SweepCallbackContext {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700176 public:
177 SweepCallbackContext(bool swap_bitmaps, space::Space* space);
178 const bool swap_bitmaps;
179 space::Space* const space;
180 Thread* const self;
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800181 size_t freed_objects;
182 size_t freed_bytes;
183 };
184
Ian Rogers1d54e732013-05-02 21:10:01 -0700185 // When should objects within this space be reclaimed? Not constant as we vary it in the case
186 // of Zygote forking.
187 GcRetentionPolicy gc_retention_policy_;
188
Mathieu Chartier590fee92013-09-13 13:46:47 -0700189 private:
Ian Rogers1d54e732013-05-02 21:10:01 -0700190 friend class art::gc::Heap;
Ian Rogers1d54e732013-05-02 21:10:01 -0700191 DISALLOW_COPY_AND_ASSIGN(Space);
192};
193std::ostream& operator<<(std::ostream& os, const Space& space);
194
195// AllocSpace interface.
196class AllocSpace {
197 public:
198 // Number of bytes currently allocated.
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700199 virtual uint64_t GetBytesAllocated() = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700200 // Number of objects currently allocated.
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700201 virtual uint64_t GetObjectsAllocated() = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700202
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700203 // Allocate num_bytes without allowing growth. If the allocation
204 // succeeds, the output parameter bytes_allocated will be set to the
205 // actually allocated bytes which is >= num_bytes.
Ian Rogers6fac4472014-02-25 17:01:10 -0800206 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
207 size_t* usable_size) = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700208
209 // Return the storage space required by obj.
Ian Rogers6fac4472014-02-25 17:01:10 -0800210 virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700211
212 // Returns how many bytes were freed.
213 virtual size_t Free(Thread* self, mirror::Object* ptr) = 0;
214
215 // Returns how many bytes were freed.
216 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) = 0;
217
Ian Rogers6fac4472014-02-25 17:01:10 -0800218 // Revoke any sort of thread-local buffers that are used to speed up allocations for the given
219 // thread, if the alloc space implementation uses any.
220 virtual void RevokeThreadLocalBuffers(Thread* thread) = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700221
Ian Rogers6fac4472014-02-25 17:01:10 -0800222 // Revoke any sort of thread-local buffers that are used to speed up allocations for all the
223 // threads, if the alloc space implementation uses any.
224 virtual void RevokeAllThreadLocalBuffers() = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700225
Ian Rogers1d54e732013-05-02 21:10:01 -0700226 protected:
227 AllocSpace() {}
228 virtual ~AllocSpace() {}
229
230 private:
231 DISALLOW_COPY_AND_ASSIGN(AllocSpace);
232};
233
234// Continuous spaces have bitmaps, and an address range. Although not required, objects within
235// continuous spaces can be marked in the card table.
236class ContinuousSpace : public Space {
237 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -0700238 // Address at which the space begins.
Ian Rogers1d54e732013-05-02 21:10:01 -0700239 byte* Begin() const {
240 return begin_;
241 }
242
Mathieu Chartier590fee92013-09-13 13:46:47 -0700243 // Current address at which the space ends, which may vary as the space is filled.
Ian Rogers1d54e732013-05-02 21:10:01 -0700244 byte* End() const {
245 return end_;
246 }
247
Mathieu Chartier590fee92013-09-13 13:46:47 -0700248 // The end of the address range covered by the space.
249 byte* Limit() const {
250 return limit_;
251 }
252
253 // Change the end of the space. Be careful with use since changing the end of a space to an
254 // invalid value may break the GC.
255 void SetEnd(byte* end) {
256 end_ = end;
257 }
258
259 void SetLimit(byte* limit) {
260 limit_ = limit;
261 }
262
Ian Rogers1d54e732013-05-02 21:10:01 -0700263 // Current size of space
264 size_t Size() const {
265 return End() - Begin();
266 }
267
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700268 virtual accounting::ContinuousSpaceBitmap* GetLiveBitmap() const = 0;
269 virtual accounting::ContinuousSpaceBitmap* GetMarkBitmap() const = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700270
Mathieu Chartier590fee92013-09-13 13:46:47 -0700271 // Maximum which the mapped space can grow to.
272 virtual size_t Capacity() const {
273 return Limit() - Begin();
274 }
275
Ian Rogers1d54e732013-05-02 21:10:01 -0700276 // Is object within this space? We check to see if the pointer is beyond the end first as
277 // continuous spaces are iterated over from low to high.
278 bool HasAddress(const mirror::Object* obj) const {
279 const byte* byte_ptr = reinterpret_cast<const byte*>(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700280 return byte_ptr >= Begin() && byte_ptr < Limit();
Ian Rogers1d54e732013-05-02 21:10:01 -0700281 }
282
283 bool Contains(const mirror::Object* obj) const {
284 return HasAddress(obj);
285 }
286
Mathieu Chartier590fee92013-09-13 13:46:47 -0700287 virtual bool IsContinuousSpace() const {
288 return true;
289 }
290
Ian Rogers1d54e732013-05-02 21:10:01 -0700291 virtual ~ContinuousSpace() {}
292
293 protected:
294 ContinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700295 byte* begin, byte* end, byte* limit) :
296 Space(name, gc_retention_policy), begin_(begin), end_(end), limit_(limit) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700297 }
298
Ian Rogers1d54e732013-05-02 21:10:01 -0700299 // The beginning of the storage for fast access.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700300 byte* begin_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700301
302 // Current end of the space.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700303 byte* volatile end_;
304
305 // Limit of the space.
306 byte* limit_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700307
308 private:
309 DISALLOW_COPY_AND_ASSIGN(ContinuousSpace);
310};
311
312// A space where objects may be allocated higgledy-piggledy throughout virtual memory. Currently
313// the card table can't cover these objects and so the write barrier shouldn't be triggered. This
314// is suitable for use for large primitive arrays.
315class DiscontinuousSpace : public Space {
316 public:
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700317 accounting::LargeObjectBitmap* GetLiveBitmap() const {
318 return live_bitmap_.get();
Ian Rogers1d54e732013-05-02 21:10:01 -0700319 }
320
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700321 accounting::LargeObjectBitmap* GetMarkBitmap() const {
322 return mark_bitmap_.get();
Ian Rogers1d54e732013-05-02 21:10:01 -0700323 }
324
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700325 virtual bool IsDiscontinuousSpace() const OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700326 return true;
327 }
328
Ian Rogers1d54e732013-05-02 21:10:01 -0700329 virtual ~DiscontinuousSpace() {}
330
331 protected:
332 DiscontinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy);
333
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700334 UniquePtr<accounting::LargeObjectBitmap> live_bitmap_;
335 UniquePtr<accounting::LargeObjectBitmap> mark_bitmap_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700336
337 private:
338 DISALLOW_COPY_AND_ASSIGN(DiscontinuousSpace);
339};
340
341class MemMapSpace : public ContinuousSpace {
342 public:
Ian Rogers1d54e732013-05-02 21:10:01 -0700343 // Size of the space without a limit on its growth. By default this is just the Capacity, but
344 // for the allocation space we support starting with a small heap and then extending it.
345 virtual size_t NonGrowthLimitCapacity() const {
346 return Capacity();
347 }
348
Ian Rogers1d54e732013-05-02 21:10:01 -0700349 MemMap* GetMemMap() {
350 return mem_map_.get();
351 }
352
353 const MemMap* GetMemMap() const {
354 return mem_map_.get();
355 }
356
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800357 MemMap* ReleaseMemMap() {
358 return mem_map_.release();
359 }
360
Mathieu Chartier590fee92013-09-13 13:46:47 -0700361 protected:
362 MemMapSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end, byte* limit,
363 GcRetentionPolicy gc_retention_policy)
364 : ContinuousSpace(name, gc_retention_policy, begin, end, limit),
365 mem_map_(mem_map) {
366 }
367
Ian Rogers1d54e732013-05-02 21:10:01 -0700368 // Underlying storage of the space
369 UniquePtr<MemMap> mem_map_;
370
Mathieu Chartier590fee92013-09-13 13:46:47 -0700371 private:
Ian Rogers1d54e732013-05-02 21:10:01 -0700372 DISALLOW_COPY_AND_ASSIGN(MemMapSpace);
373};
374
Mathieu Chartier590fee92013-09-13 13:46:47 -0700375// Used by the heap compaction interface to enable copying from one type of alloc space to another.
376class ContinuousMemMapAllocSpace : public MemMapSpace, public AllocSpace {
377 public:
Ian Rogers6fac4472014-02-25 17:01:10 -0800378 bool IsAllocSpace() const OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700379 return true;
380 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800381 AllocSpace* AsAllocSpace() OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700382 return this;
383 }
384
Ian Rogers6fac4472014-02-25 17:01:10 -0800385 bool IsContinuousMemMapAllocSpace() const OVERRIDE {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800386 return true;
387 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800388 ContinuousMemMapAllocSpace* AsContinuousMemMapAllocSpace() {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800389 return this;
390 }
391
392 bool HasBoundBitmaps() const EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
393 void BindLiveToMarkBitmap()
394 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
395 void UnBindBitmaps() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier1f3b5352014-02-03 14:00:42 -0800396 // Swap the live and mark bitmaps of this space. This is used by the GC for concurrent sweeping.
397 void SwapBitmaps();
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800398
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700399 // Clear the space back to an empty space.
Ian Rogers6fac4472014-02-25 17:01:10 -0800400 virtual void Clear() = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700401
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700402 accounting::ContinuousSpaceBitmap* GetLiveBitmap() const {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800403 return live_bitmap_.get();
404 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800405
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700406 accounting::ContinuousSpaceBitmap* GetMarkBitmap() const {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800407 return mark_bitmap_.get();
408 }
409
Ian Rogers6fac4472014-02-25 17:01:10 -0800410 void Sweep(bool swap_bitmaps, size_t* freed_objects, size_t* freed_bytes);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700411 virtual accounting::ContinuousSpaceBitmap::SweepCallback* GetSweepCallback() = 0;
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800412
Mathieu Chartier590fee92013-09-13 13:46:47 -0700413 protected:
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700414 UniquePtr<accounting::ContinuousSpaceBitmap> live_bitmap_;
415 UniquePtr<accounting::ContinuousSpaceBitmap> mark_bitmap_;
416 UniquePtr<accounting::ContinuousSpaceBitmap> temp_bitmap_;
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800417
Mathieu Chartier590fee92013-09-13 13:46:47 -0700418 ContinuousMemMapAllocSpace(const std::string& name, MemMap* mem_map, byte* begin,
419 byte* end, byte* limit, GcRetentionPolicy gc_retention_policy)
420 : MemMapSpace(name, mem_map, begin, end, limit, gc_retention_policy) {
421 }
422
423 private:
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800424 friend class gc::Heap;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700425 DISALLOW_COPY_AND_ASSIGN(ContinuousMemMapAllocSpace);
426};
427
Ian Rogers1d54e732013-05-02 21:10:01 -0700428} // namespace space
429} // namespace gc
430} // namespace art
431
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700432#endif // ART_RUNTIME_GC_SPACE_SPACE_H_