Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_GC_SPACE_SPACE_H_ |
| 18 | #define ART_RUNTIME_GC_SPACE_SPACE_H_ |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 19 | |
| 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 | |
| 30 | namespace art { |
| 31 | namespace mirror { |
| 32 | class Object; |
| 33 | } // namespace mirror |
| 34 | |
| 35 | namespace gc { |
| 36 | |
| 37 | namespace accounting { |
| 38 | class SpaceBitmap; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 39 | } // namespace accounting |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 40 | |
| 41 | class Heap; |
| 42 | |
| 43 | namespace space { |
| 44 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 45 | class AllocSpace; |
Mathieu Chartier | 7410f29 | 2013-11-24 13:17:35 -0800 | [diff] [blame] | 46 | class BumpPointerSpace; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 47 | class ContinuousSpace; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 48 | class DiscontinuousSpace; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 49 | class MallocSpace; |
| 50 | class DlMallocSpace; |
| 51 | class RosAllocSpace; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 52 | class ImageSpace; |
| 53 | class LargeObjectSpace; |
| 54 | |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 55 | static constexpr bool kDebugSpaces = kIsDebugBuild; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 56 | |
| 57 | // See Space::GetGcRetentionPolicy. |
| 58 | enum GcRetentionPolicy { |
| 59 | // Objects are retained forever with this policy for a space. |
| 60 | kGcRetentionPolicyNeverCollect, |
| 61 | // Every GC cycle will attempt to collect objects in this space. |
| 62 | kGcRetentionPolicyAlwaysCollect, |
| 63 | // Objects will be considered for collection only in "full" GC cycles, ie faster partial |
| 64 | // collections won't scan these areas such as the Zygote. |
| 65 | kGcRetentionPolicyFullCollect, |
| 66 | }; |
| 67 | std::ostream& operator<<(std::ostream& os, const GcRetentionPolicy& policy); |
| 68 | |
| 69 | enum SpaceType { |
| 70 | kSpaceTypeImageSpace, |
| 71 | kSpaceTypeAllocSpace, |
| 72 | kSpaceTypeZygoteSpace, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 73 | kSpaceTypeBumpPointerSpace, |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 74 | kSpaceTypeLargeObjectSpace, |
| 75 | }; |
| 76 | std::ostream& operator<<(std::ostream& os, const SpaceType& space_type); |
| 77 | |
| 78 | // A space contains memory allocated for managed objects. |
| 79 | class Space { |
| 80 | public: |
| 81 | // Dump space. Also key method for C++ vtables. |
| 82 | virtual void Dump(std::ostream& os) const; |
| 83 | |
| 84 | // Name of the space. May vary, for example before/after the Zygote fork. |
| 85 | const char* GetName() const { |
| 86 | return name_.c_str(); |
| 87 | } |
| 88 | |
| 89 | // The policy of when objects are collected associated with this space. |
| 90 | GcRetentionPolicy GetGcRetentionPolicy() const { |
| 91 | return gc_retention_policy_; |
| 92 | } |
| 93 | |
| 94 | // Does the space support allocation? |
| 95 | virtual bool CanAllocateInto() const { |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | // Is the given object contained within this space? |
| 100 | virtual bool Contains(const mirror::Object* obj) const = 0; |
| 101 | |
| 102 | // The kind of space this: image, alloc, zygote, large object. |
| 103 | virtual SpaceType GetType() const = 0; |
| 104 | |
| 105 | // Is this an image space, ie one backed by a memory mapped image file. |
| 106 | bool IsImageSpace() const { |
| 107 | return GetType() == kSpaceTypeImageSpace; |
| 108 | } |
| 109 | ImageSpace* AsImageSpace(); |
| 110 | |
| 111 | // Is this a dlmalloc backed allocation space? |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 112 | bool IsMallocSpace() const { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 113 | SpaceType type = GetType(); |
| 114 | return type == kSpaceTypeAllocSpace || type == kSpaceTypeZygoteSpace; |
| 115 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 116 | MallocSpace* AsMallocSpace(); |
| 117 | |
| 118 | virtual bool IsDlMallocSpace() const { |
| 119 | return false; |
| 120 | } |
| 121 | virtual DlMallocSpace* AsDlMallocSpace() { |
| 122 | LOG(FATAL) << "Unreachable"; |
| 123 | return NULL; |
| 124 | } |
| 125 | virtual bool IsRosAllocSpace() const { |
| 126 | return false; |
| 127 | } |
| 128 | virtual RosAllocSpace* AsRosAllocSpace() { |
| 129 | LOG(FATAL) << "Unreachable"; |
| 130 | return NULL; |
| 131 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 132 | |
| 133 | // Is this the space allocated into by the Zygote and no-longer in use? |
| 134 | bool IsZygoteSpace() const { |
| 135 | return GetType() == kSpaceTypeZygoteSpace; |
| 136 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 137 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 138 | // Is this space a bump pointer space? |
| 139 | bool IsBumpPointerSpace() const { |
| 140 | return GetType() == kSpaceTypeBumpPointerSpace; |
| 141 | } |
Mathieu Chartier | 7410f29 | 2013-11-24 13:17:35 -0800 | [diff] [blame] | 142 | virtual BumpPointerSpace* AsBumpPointerSpace() { |
| 143 | LOG(FATAL) << "Unreachable"; |
| 144 | return NULL; |
| 145 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 146 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 147 | // Does this space hold large objects and implement the large object space abstraction? |
| 148 | bool IsLargeObjectSpace() const { |
| 149 | return GetType() == kSpaceTypeLargeObjectSpace; |
| 150 | } |
| 151 | LargeObjectSpace* AsLargeObjectSpace(); |
| 152 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 153 | virtual bool IsContinuousSpace() const { |
| 154 | return false; |
| 155 | } |
| 156 | ContinuousSpace* AsContinuousSpace(); |
| 157 | |
| 158 | virtual bool IsDiscontinuousSpace() const { |
| 159 | return false; |
| 160 | } |
| 161 | DiscontinuousSpace* AsDiscontinuousSpace(); |
| 162 | |
| 163 | virtual bool IsAllocSpace() const { |
| 164 | return false; |
| 165 | } |
| 166 | virtual AllocSpace* AsAllocSpace() { |
| 167 | LOG(FATAL) << "Unimplemented"; |
| 168 | return nullptr; |
| 169 | } |
| 170 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 171 | virtual ~Space() {} |
| 172 | |
| 173 | protected: |
| 174 | Space(const std::string& name, GcRetentionPolicy gc_retention_policy); |
| 175 | |
| 176 | void SetGcRetentionPolicy(GcRetentionPolicy gc_retention_policy) { |
| 177 | gc_retention_policy_ = gc_retention_policy; |
| 178 | } |
| 179 | |
| 180 | // Name of the space that may vary due to the Zygote fork. |
| 181 | std::string name_; |
| 182 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 183 | protected: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 184 | // When should objects within this space be reclaimed? Not constant as we vary it in the case |
| 185 | // of Zygote forking. |
| 186 | GcRetentionPolicy gc_retention_policy_; |
| 187 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 188 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 189 | friend class art::gc::Heap; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 190 | DISALLOW_COPY_AND_ASSIGN(Space); |
| 191 | }; |
| 192 | std::ostream& operator<<(std::ostream& os, const Space& space); |
| 193 | |
| 194 | // AllocSpace interface. |
| 195 | class AllocSpace { |
| 196 | public: |
| 197 | // Number of bytes currently allocated. |
Hiroshi Yamauchi | be031ff | 2013-10-08 16:42:37 -0700 | [diff] [blame] | 198 | virtual uint64_t GetBytesAllocated() = 0; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 199 | // Number of objects currently allocated. |
Hiroshi Yamauchi | be031ff | 2013-10-08 16:42:37 -0700 | [diff] [blame] | 200 | virtual uint64_t GetObjectsAllocated() = 0; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 201 | |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 202 | // Allocate num_bytes without allowing growth. If the allocation |
| 203 | // succeeds, the output parameter bytes_allocated will be set to the |
| 204 | // actually allocated bytes which is >= num_bytes. |
| 205 | virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated) = 0; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 206 | |
| 207 | // Return the storage space required by obj. |
| 208 | virtual size_t AllocationSize(const mirror::Object* obj) = 0; |
| 209 | |
| 210 | // Returns how many bytes were freed. |
| 211 | virtual size_t Free(Thread* self, mirror::Object* ptr) = 0; |
| 212 | |
| 213 | // Returns how many bytes were freed. |
| 214 | virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) = 0; |
| 215 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 216 | // Revoke any sort of thread-local buffers that are used to speed up |
| 217 | // allocations for the given thread, if the alloc space |
| 218 | // implementation uses any. No-op by default. |
| 219 | virtual void RevokeThreadLocalBuffers(Thread* /*thread*/) {} |
| 220 | |
| 221 | // Revoke any sort of thread-local buffers that are used to speed up |
| 222 | // allocations for all the threads, if the alloc space |
| 223 | // implementation uses any. No-op by default. |
| 224 | virtual void RevokeAllThreadLocalBuffers() {} |
| 225 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 226 | 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. |
| 236 | class ContinuousSpace : public Space { |
| 237 | public: |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 238 | // Address at which the space begins. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 239 | byte* Begin() const { |
| 240 | return begin_; |
| 241 | } |
| 242 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 243 | // Current address at which the space ends, which may vary as the space is filled. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 244 | byte* End() const { |
| 245 | return end_; |
| 246 | } |
| 247 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 248 | // 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 Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 263 | // Current size of space |
| 264 | size_t Size() const { |
| 265 | return End() - Begin(); |
| 266 | } |
| 267 | |
| 268 | virtual accounting::SpaceBitmap* GetLiveBitmap() const = 0; |
| 269 | virtual accounting::SpaceBitmap* GetMarkBitmap() const = 0; |
| 270 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 271 | // Maximum which the mapped space can grow to. |
| 272 | virtual size_t Capacity() const { |
| 273 | return Limit() - Begin(); |
| 274 | } |
| 275 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 276 | // 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 Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 280 | return byte_ptr >= Begin() && byte_ptr < Limit(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | bool Contains(const mirror::Object* obj) const { |
| 284 | return HasAddress(obj); |
| 285 | } |
| 286 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 287 | virtual bool IsContinuousSpace() const { |
| 288 | return true; |
| 289 | } |
| 290 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 291 | virtual ~ContinuousSpace() {} |
| 292 | |
| 293 | protected: |
| 294 | ContinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 295 | byte* begin, byte* end, byte* limit) : |
| 296 | Space(name, gc_retention_policy), begin_(begin), end_(end), limit_(limit) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 299 | // The beginning of the storage for fast access. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 300 | byte* begin_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 301 | |
| 302 | // Current end of the space. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 303 | byte* volatile end_; |
| 304 | |
| 305 | // Limit of the space. |
| 306 | byte* limit_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 307 | |
| 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. |
| 315 | class DiscontinuousSpace : public Space { |
| 316 | public: |
| 317 | accounting::SpaceSetMap* GetLiveObjects() const { |
| 318 | return live_objects_.get(); |
| 319 | } |
| 320 | |
| 321 | accounting::SpaceSetMap* GetMarkObjects() const { |
| 322 | return mark_objects_.get(); |
| 323 | } |
| 324 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 325 | virtual bool IsDiscontinuousSpace() const { |
| 326 | return true; |
| 327 | } |
| 328 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 329 | virtual ~DiscontinuousSpace() {} |
| 330 | |
| 331 | protected: |
| 332 | DiscontinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy); |
| 333 | |
| 334 | UniquePtr<accounting::SpaceSetMap> live_objects_; |
| 335 | UniquePtr<accounting::SpaceSetMap> mark_objects_; |
| 336 | |
| 337 | private: |
| 338 | DISALLOW_COPY_AND_ASSIGN(DiscontinuousSpace); |
| 339 | }; |
| 340 | |
| 341 | class MemMapSpace : public ContinuousSpace { |
| 342 | public: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 343 | // 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 Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 349 | MemMap* GetMemMap() { |
| 350 | return mem_map_.get(); |
| 351 | } |
| 352 | |
| 353 | const MemMap* GetMemMap() const { |
| 354 | return mem_map_.get(); |
| 355 | } |
| 356 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 357 | protected: |
| 358 | MemMapSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end, byte* limit, |
| 359 | GcRetentionPolicy gc_retention_policy) |
| 360 | : ContinuousSpace(name, gc_retention_policy, begin, end, limit), |
| 361 | mem_map_(mem_map) { |
| 362 | } |
| 363 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 364 | // Underlying storage of the space |
| 365 | UniquePtr<MemMap> mem_map_; |
| 366 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 367 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 368 | DISALLOW_COPY_AND_ASSIGN(MemMapSpace); |
| 369 | }; |
| 370 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 371 | // Used by the heap compaction interface to enable copying from one type of alloc space to another. |
| 372 | class ContinuousMemMapAllocSpace : public MemMapSpace, public AllocSpace { |
| 373 | public: |
| 374 | virtual bool IsAllocSpace() const { |
| 375 | return true; |
| 376 | } |
| 377 | |
| 378 | virtual AllocSpace* AsAllocSpace() { |
| 379 | return this; |
| 380 | } |
| 381 | |
| 382 | virtual void Clear() { |
| 383 | LOG(FATAL) << "Unimplemented"; |
| 384 | } |
| 385 | |
| 386 | protected: |
| 387 | ContinuousMemMapAllocSpace(const std::string& name, MemMap* mem_map, byte* begin, |
| 388 | byte* end, byte* limit, GcRetentionPolicy gc_retention_policy) |
| 389 | : MemMapSpace(name, mem_map, begin, end, limit, gc_retention_policy) { |
| 390 | } |
| 391 | |
| 392 | private: |
| 393 | DISALLOW_COPY_AND_ASSIGN(ContinuousMemMapAllocSpace); |
| 394 | }; |
| 395 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 396 | } // namespace space |
| 397 | } // namespace gc |
| 398 | } // namespace art |
| 399 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 400 | #endif // ART_RUNTIME_GC_SPACE_SPACE_H_ |