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 | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 20 | #include "base/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 | |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 32 | class AllocationInfo; |
| 33 | |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 34 | // Abstraction implemented by all large object spaces. |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 35 | class LargeObjectSpace : public DiscontinuousSpace, public AllocSpace { |
| 36 | public: |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 37 | SpaceType GetType() const OVERRIDE { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 38 | return kSpaceTypeLargeObjectSpace; |
| 39 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 40 | void SwapBitmaps(); |
| 41 | void CopyLiveToMarked(); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 42 | virtual void Walk(DlMallocSpace::WalkCallback, void* arg) = 0; |
| 43 | virtual ~LargeObjectSpace() {} |
| 44 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 45 | uint64_t GetBytesAllocated() OVERRIDE { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 46 | return num_bytes_allocated_; |
| 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 | } |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 51 | uint64_t GetTotalBytesAllocated() const { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 52 | return total_bytes_allocated_; |
| 53 | } |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 54 | uint64_t GetTotalObjectsAllocated() const { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 55 | return total_objects_allocated_; |
| 56 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 57 | size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 58 | // LargeObjectSpaces don't have thread local state. |
| 59 | void RevokeThreadLocalBuffers(art::Thread*) OVERRIDE { |
| 60 | } |
| 61 | void RevokeAllThreadLocalBuffers() OVERRIDE { |
| 62 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 63 | bool IsAllocSpace() const OVERRIDE { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 64 | return true; |
| 65 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 66 | AllocSpace* AsAllocSpace() OVERRIDE { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 67 | return this; |
| 68 | } |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 69 | collector::ObjectBytePair Sweep(bool swap_bitmaps); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 70 | virtual bool CanMoveObjects() const OVERRIDE { |
| 71 | return false; |
| 72 | } |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 73 | // Current address at which the space begins, which may vary as the space is filled. |
| 74 | byte* Begin() const { |
| 75 | return begin_; |
| 76 | } |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 77 | // Current address at which the space ends, which may vary as the space is filled. |
| 78 | byte* End() const { |
| 79 | return end_; |
| 80 | } |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 81 | // Current size of space |
| 82 | size_t Size() const { |
| 83 | return End() - Begin(); |
| 84 | } |
| 85 | // Return true if we contain the specified address. |
| 86 | bool Contains(const mirror::Object* obj) const { |
| 87 | const byte* byte_obj = reinterpret_cast<const byte*>(obj); |
| 88 | return Begin() <= byte_obj && byte_obj < End(); |
| 89 | } |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 90 | void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) OVERRIDE |
| 91 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 92 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 93 | protected: |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 94 | explicit LargeObjectSpace(const std::string& name, byte* begin, byte* end); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 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_; |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 102 | // Begin and end, may change as more large objects are allocated. |
| 103 | byte* begin_; |
| 104 | byte* end_; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 105 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 106 | friend class Space; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 107 | |
| 108 | private: |
| 109 | DISALLOW_COPY_AND_ASSIGN(LargeObjectSpace); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 112 | // A discontinuous large object space implemented by individual mmap/munmap calls. |
Mathieu Chartier | 0767c9a | 2014-03-26 12:53:19 -0700 | [diff] [blame] | 113 | class LargeObjectMapSpace : public LargeObjectSpace { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 114 | public: |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 115 | // Creates a large object space. Allocations into the large object space use memory maps instead |
| 116 | // of malloc. |
| 117 | static LargeObjectMapSpace* Create(const std::string& name); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 118 | // Return the storage space required by obj. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 119 | size_t AllocationSize(mirror::Object* obj, size_t* usable_size); |
| 120 | mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 121 | size_t* usable_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 122 | size_t Free(Thread* self, mirror::Object* ptr); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 123 | void Walk(DlMallocSpace::WalkCallback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_); |
Ian Rogers | a3dd0b3 | 2013-03-19 19:30:59 -0700 | [diff] [blame] | 124 | // 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] | 125 | bool Contains(const mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS; |
| 126 | |
Mathieu Chartier | 0767c9a | 2014-03-26 12:53:19 -0700 | [diff] [blame] | 127 | protected: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 128 | explicit LargeObjectMapSpace(const std::string& name); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 129 | virtual ~LargeObjectMapSpace() {} |
| 130 | |
| 131 | // 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] | 132 | mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 133 | std::vector<mirror::Object*, TrackingAllocator<mirror::Object*, kAllocatorTagLOS>> large_objects_ |
| 134 | GUARDED_BY(lock_); |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 135 | typedef SafeMap<mirror::Object*, MemMap*, std::less<mirror::Object*>, |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 136 | TrackingAllocator<std::pair<mirror::Object*, MemMap*>, kAllocatorTagLOSMaps>> MemMaps; |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 137 | MemMaps mem_maps_ GUARDED_BY(lock_); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 140 | // A continuous large object space with a free-list to handle holes. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 141 | class FreeListSpace FINAL : public LargeObjectSpace { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 142 | public: |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 143 | static constexpr size_t kAlignment = kPageSize; |
| 144 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 145 | virtual ~FreeListSpace(); |
| 146 | static FreeListSpace* Create(const std::string& name, byte* requested_begin, size_t capacity); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 147 | size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE |
| 148 | EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 149 | mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 150 | size_t* usable_size) OVERRIDE; |
| 151 | size_t Free(Thread* self, mirror::Object* obj) OVERRIDE; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 152 | void Walk(DlMallocSpace::WalkCallback callback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 153 | void Dump(std::ostream& os) const; |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 154 | |
Mathieu Chartier | 0767c9a | 2014-03-26 12:53:19 -0700 | [diff] [blame] | 155 | protected: |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 156 | FreeListSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end); |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 157 | size_t GetSlotIndexForAddress(uintptr_t address) const { |
| 158 | DCHECK(Contains(reinterpret_cast<mirror::Object*>(address))); |
| 159 | return (address - reinterpret_cast<uintptr_t>(Begin())) / kAlignment; |
| 160 | } |
| 161 | size_t GetSlotIndexForAllocationInfo(const AllocationInfo* info) const; |
| 162 | AllocationInfo* GetAllocationInfoForAddress(uintptr_t address); |
| 163 | const AllocationInfo* GetAllocationInfoForAddress(uintptr_t address) const; |
| 164 | uintptr_t GetAllocationAddressForSlot(size_t slot) const { |
| 165 | return reinterpret_cast<uintptr_t>(Begin()) + slot * kAlignment; |
| 166 | } |
| 167 | uintptr_t GetAddressForAllocationInfo(const AllocationInfo* info) const { |
| 168 | return GetAllocationAddressForSlot(GetSlotIndexForAllocationInfo(info)); |
| 169 | } |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 170 | // Removes header from the free blocks set by finding the corresponding iterator and erasing it. |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 171 | void RemoveFreePrev(AllocationInfo* info) EXCLUSIVE_LOCKS_REQUIRED(lock_); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 172 | |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 173 | class SortByPrevFree { |
| 174 | public: |
| 175 | bool operator()(const AllocationInfo* a, const AllocationInfo* b) const; |
| 176 | }; |
| 177 | typedef std::set<AllocationInfo*, SortByPrevFree, |
| 178 | TrackingAllocator<AllocationInfo*, kAllocatorTagLOSFreeList>> FreeBlocks; |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 179 | |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 180 | // There is not footer for any allocations at the end of the space, so we keep track of how much |
| 181 | // free space there is at the end manually. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 182 | std::unique_ptr<MemMap> mem_map_; |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 183 | // Side table for allocation info, one per page. |
| 184 | std::unique_ptr<MemMap> allocation_info_map_; |
| 185 | AllocationInfo* allocation_info_; |
| 186 | |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 187 | Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 188 | // Free bytes at the end of the space. |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 189 | size_t free_end_ GUARDED_BY(lock_); |
| 190 | FreeBlocks free_blocks_ GUARDED_BY(lock_); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 193 | } // namespace space |
| 194 | } // namespace gc |
| 195 | } // namespace art |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 196 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 197 | #endif // ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_ |