Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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_LARGE_OBJECT_SPACE_H_ |
| 18 | #define ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_ |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 19 | |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 20 | #include "gc/accounting/gc_allocator.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 21 | #include "dlmalloc_space.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "safe_map.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 23 | #include "space.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | |
| 25 | #include <set> |
| 26 | #include <vector> |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 29 | namespace gc { |
| 30 | namespace space { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 31 | |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 32 | // Abstraction implemented by all large object spaces. |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 33 | class LargeObjectSpace : public DiscontinuousSpace, public AllocSpace { |
| 34 | public: |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 35 | SpaceType GetType() const OVERRIDE { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 36 | return kSpaceTypeLargeObjectSpace; |
| 37 | } |
| 38 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 39 | void SwapBitmaps(); |
| 40 | void CopyLiveToMarked(); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 41 | virtual void Walk(DlMallocSpace::WalkCallback, void* arg) = 0; |
| 42 | virtual ~LargeObjectSpace() {} |
| 43 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 44 | uint64_t GetBytesAllocated() OVERRIDE { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 45 | return num_bytes_allocated_; |
| 46 | } |
| 47 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 48 | uint64_t GetObjectsAllocated() OVERRIDE { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 49 | return num_objects_allocated_; |
| 50 | } |
| 51 | |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame^] | 52 | uint64_t GetTotalBytesAllocated() const { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 53 | return total_bytes_allocated_; |
| 54 | } |
| 55 | |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame^] | 56 | uint64_t GetTotalObjectsAllocated() const { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 57 | return total_objects_allocated_; |
| 58 | } |
| 59 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 60 | size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 61 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 62 | // LargeObjectSpaces don't have thread local state. |
| 63 | void RevokeThreadLocalBuffers(art::Thread*) OVERRIDE { |
| 64 | } |
| 65 | void RevokeAllThreadLocalBuffers() OVERRIDE { |
| 66 | } |
| 67 | |
| 68 | bool IsAllocSpace() const OVERRIDE { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 69 | return true; |
| 70 | } |
| 71 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 72 | AllocSpace* AsAllocSpace() OVERRIDE { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 73 | return this; |
| 74 | } |
| 75 | |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame^] | 76 | void Sweep(bool swap_bitmaps, size_t* out_freed_objects, size_t* out_freed_bytes); |
Mathieu Chartier | db7f37d | 2014-01-10 11:09:06 -0800 | [diff] [blame] | 77 | |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 78 | virtual bool CanMoveObjects() const OVERRIDE { |
| 79 | return false; |
| 80 | } |
| 81 | |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame^] | 82 | // Current address at which the space begins, which may vary as the space is filled. |
| 83 | byte* Begin() const { |
| 84 | return begin_; |
| 85 | } |
| 86 | |
| 87 | // Current address at which the space ends, which may vary as the space is filled. |
| 88 | byte* End() const { |
| 89 | return end_; |
| 90 | } |
| 91 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 92 | protected: |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame^] | 93 | explicit LargeObjectSpace(const std::string& name, byte* begin, byte* end); |
| 94 | |
| 95 | static void SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 96 | |
| 97 | // Approximate number of bytes which have been allocated into the space. |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame^] | 98 | uint64_t num_bytes_allocated_; |
| 99 | uint64_t num_objects_allocated_; |
| 100 | uint64_t total_bytes_allocated_; |
| 101 | uint64_t total_objects_allocated_; |
| 102 | |
| 103 | // Begin and end, may change as more large objects are allocated. |
| 104 | byte* begin_; |
| 105 | byte* end_; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 106 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 107 | friend class Space; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 108 | |
| 109 | private: |
| 110 | DISALLOW_COPY_AND_ASSIGN(LargeObjectSpace); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 113 | // A discontinuous large object space implemented by individual mmap/munmap calls. |
Mathieu Chartier | 0767c9a | 2014-03-26 12:53:19 -0700 | [diff] [blame] | 114 | class LargeObjectMapSpace : public LargeObjectSpace { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 115 | public: |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 116 | // Creates a large object space. Allocations into the large object space use memory maps instead |
| 117 | // of malloc. |
| 118 | static LargeObjectMapSpace* Create(const std::string& name); |
| 119 | |
| 120 | // Return the storage space required by obj. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 121 | size_t AllocationSize(mirror::Object* obj, size_t* usable_size); |
| 122 | mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 123 | size_t* usable_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 124 | size_t Free(Thread* self, mirror::Object* ptr); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 125 | void Walk(DlMallocSpace::WalkCallback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_); |
Ian Rogers | a3dd0b3 | 2013-03-19 19:30:59 -0700 | [diff] [blame] | 126 | // TODO: disabling thread safety analysis as this may be called when we already hold lock_. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 127 | bool Contains(const mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS; |
| 128 | |
Mathieu Chartier | 0767c9a | 2014-03-26 12:53:19 -0700 | [diff] [blame] | 129 | protected: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 130 | explicit LargeObjectMapSpace(const std::string& name); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 131 | virtual ~LargeObjectMapSpace() {} |
| 132 | |
| 133 | // Used to ensure mutual exclusion when the allocation spaces data structures are being modified. |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 134 | mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 135 | std::vector<mirror::Object*, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 136 | accounting::GcAllocator<mirror::Object*> > large_objects_ GUARDED_BY(lock_); |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 137 | typedef SafeMap<mirror::Object*, MemMap*, std::less<mirror::Object*>, |
Mathieu Chartier | 0767c9a | 2014-03-26 12:53:19 -0700 | [diff] [blame] | 138 | accounting::GcAllocator<std::pair<mirror::Object*, MemMap*> > > MemMaps; |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 139 | MemMaps mem_maps_ GUARDED_BY(lock_); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 142 | // A continuous large object space with a free-list to handle holes. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 143 | class FreeListSpace FINAL : public LargeObjectSpace { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 144 | public: |
| 145 | virtual ~FreeListSpace(); |
| 146 | static FreeListSpace* Create(const std::string& name, byte* requested_begin, size_t capacity); |
| 147 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 148 | size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE |
| 149 | EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 150 | mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 151 | size_t* usable_size) OVERRIDE; |
| 152 | size_t Free(Thread* self, mirror::Object* obj) OVERRIDE; |
| 153 | bool Contains(const mirror::Object* obj) const OVERRIDE; |
| 154 | void Walk(DlMallocSpace::WalkCallback callback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 155 | |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 156 | // Address at which the space begins. |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 157 | byte* Begin() const { |
| 158 | return begin_; |
| 159 | } |
| 160 | |
| 161 | // Address at which the space ends, which may vary as the space is filled. |
| 162 | byte* End() const { |
| 163 | return end_; |
| 164 | } |
| 165 | |
| 166 | // Current size of space |
| 167 | size_t Size() const { |
| 168 | return End() - Begin(); |
| 169 | } |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 170 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 171 | void Dump(std::ostream& os) const; |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 172 | |
Mathieu Chartier | 0767c9a | 2014-03-26 12:53:19 -0700 | [diff] [blame] | 173 | protected: |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 174 | static const size_t kAlignment = kPageSize; |
| 175 | |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 176 | class AllocationHeader { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 177 | public: |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 178 | // Returns the allocation size, includes the header. |
| 179 | size_t AllocationSize() const { |
| 180 | return alloc_size_; |
| 181 | } |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 182 | |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 183 | // Updates the allocation size in the header, the allocation size includes the header itself. |
| 184 | void SetAllocationSize(size_t size) { |
| 185 | DCHECK(IsAligned<kPageSize>(size)); |
| 186 | alloc_size_ = size; |
| 187 | } |
| 188 | |
| 189 | bool IsFree() const { |
| 190 | return AllocationSize() == 0; |
| 191 | } |
| 192 | |
| 193 | // Returns the previous free allocation header by using the prev_free_ member to figure out |
| 194 | // where it is. If prev free is 0 then we just return ourself. |
| 195 | AllocationHeader* GetPrevFreeAllocationHeader() { |
| 196 | return reinterpret_cast<AllocationHeader*>(reinterpret_cast<uintptr_t>(this) - prev_free_); |
| 197 | } |
| 198 | |
| 199 | // Returns the address of the object associated with this allocation header. |
| 200 | mirror::Object* GetObjectAddress() { |
| 201 | return reinterpret_cast<mirror::Object*>(reinterpret_cast<uintptr_t>(this) + sizeof(*this)); |
| 202 | } |
| 203 | |
| 204 | // Returns the next allocation header after the object associated with this allocation header. |
| 205 | AllocationHeader* GetNextAllocationHeader() { |
| 206 | DCHECK_NE(alloc_size_, 0U); |
| 207 | return reinterpret_cast<AllocationHeader*>(reinterpret_cast<uintptr_t>(this) + alloc_size_); |
| 208 | } |
| 209 | |
| 210 | // Returns how many free bytes there is before the block. |
| 211 | size_t GetPrevFree() const { |
| 212 | return prev_free_; |
| 213 | } |
| 214 | |
| 215 | // Update the size of the free block prior to the allocation. |
| 216 | void SetPrevFree(size_t prev_free) { |
| 217 | DCHECK(IsAligned<kPageSize>(prev_free)); |
| 218 | prev_free_ = prev_free; |
| 219 | } |
| 220 | |
| 221 | // Finds and returns the next non free allocation header after ourself. |
| 222 | // TODO: Optimize, currently O(n) for n free following pages. |
| 223 | AllocationHeader* GetNextNonFree(); |
| 224 | |
| 225 | // Used to implement best fit object allocation. Each allocation has an AllocationHeader which |
| 226 | // contains the size of the previous free block preceding it. Implemented in such a way that we |
| 227 | // can also find the iterator for any allocation header pointer. |
| 228 | class SortByPrevFree { |
| 229 | public: |
| 230 | bool operator()(const AllocationHeader* a, const AllocationHeader* b) const { |
| 231 | if (a->GetPrevFree() < b->GetPrevFree()) return true; |
| 232 | if (a->GetPrevFree() > b->GetPrevFree()) return false; |
| 233 | if (a->AllocationSize() < b->AllocationSize()) return true; |
| 234 | if (a->AllocationSize() > b->AllocationSize()) return false; |
| 235 | return reinterpret_cast<uintptr_t>(a) < reinterpret_cast<uintptr_t>(b); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 236 | } |
| 237 | }; |
| 238 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 239 | private: |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 240 | // Contains the size of the previous free block, if 0 then the memory preceding us is an |
| 241 | // allocation. |
| 242 | size_t prev_free_; |
| 243 | |
| 244 | // Allocation size of this object, 0 means that the allocation header is free memory. |
| 245 | size_t alloc_size_; |
| 246 | |
| 247 | friend class FreeListSpace; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | FreeListSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 251 | |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 252 | // Removes header from the free blocks set by finding the corresponding iterator and erasing it. |
| 253 | void RemoveFreePrev(AllocationHeader* header) EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 254 | |
| 255 | // Finds the allocation header corresponding to obj. |
| 256 | AllocationHeader* GetAllocationHeader(const mirror::Object* obj); |
| 257 | |
| 258 | typedef std::set<AllocationHeader*, AllocationHeader::SortByPrevFree, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 259 | accounting::GcAllocator<AllocationHeader*> > FreeBlocks; |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 260 | |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 261 | // There is not footer for any allocations at the end of the space, so we keep track of how much |
| 262 | // free space there is at the end manually. |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 263 | UniquePtr<MemMap> mem_map_; |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 264 | Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 265 | size_t free_end_ GUARDED_BY(lock_); |
| 266 | FreeBlocks free_blocks_ GUARDED_BY(lock_); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 267 | }; |
| 268 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 269 | } // namespace space |
| 270 | } // namespace gc |
| 271 | } // namespace art |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 272 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 273 | #endif // ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_ |