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