large_object_space.h revision 4460a84be92b5a94ecfb5c650aef4945ab849c93
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 17#ifndef ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_ 18#define ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_ 19 20#include "base/allocator.h" 21#include "dlmalloc_space.h" 22#include "safe_map.h" 23#include "space.h" 24 25#include <set> 26#include <vector> 27 28namespace art { 29namespace gc { 30namespace space { 31 32class AllocationInfo; 33 34enum class LargeObjectSpaceType { 35 kDisabled, 36 kMap, 37 kFreeList, 38}; 39 40// Abstraction implemented by all large object spaces. 41class LargeObjectSpace : public DiscontinuousSpace, public AllocSpace { 42 public: 43 SpaceType GetType() const OVERRIDE { 44 return kSpaceTypeLargeObjectSpace; 45 } 46 void SwapBitmaps(); 47 void CopyLiveToMarked(); 48 virtual void Walk(DlMallocSpace::WalkCallback, void* arg) = 0; 49 virtual ~LargeObjectSpace() {} 50 51 uint64_t GetBytesAllocated() OVERRIDE { 52 return num_bytes_allocated_; 53 } 54 uint64_t GetObjectsAllocated() OVERRIDE { 55 return num_objects_allocated_; 56 } 57 uint64_t GetTotalBytesAllocated() const { 58 return total_bytes_allocated_; 59 } 60 uint64_t GetTotalObjectsAllocated() const { 61 return total_objects_allocated_; 62 } 63 size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE; 64 // LargeObjectSpaces don't have thread local state. 65 size_t RevokeThreadLocalBuffers(art::Thread*) OVERRIDE { 66 return 0U; 67 } 68 size_t RevokeAllThreadLocalBuffers() OVERRIDE { 69 return 0U; 70 } 71 bool IsAllocSpace() const OVERRIDE { 72 return true; 73 } 74 AllocSpace* AsAllocSpace() OVERRIDE { 75 return this; 76 } 77 collector::ObjectBytePair Sweep(bool swap_bitmaps); 78 virtual bool CanMoveObjects() const OVERRIDE { 79 return false; 80 } 81 // Current address at which the space begins, which may vary as the space is filled. 82 uint8_t* Begin() const { 83 return begin_; 84 } 85 // Current address at which the space ends, which may vary as the space is filled. 86 uint8_t* End() const { 87 return end_; 88 } 89 // Current size of space 90 size_t Size() const { 91 return End() - Begin(); 92 } 93 // Return true if we contain the specified address. 94 bool Contains(const mirror::Object* obj) const { 95 const uint8_t* byte_obj = reinterpret_cast<const uint8_t*>(obj); 96 return Begin() <= byte_obj && byte_obj < End(); 97 } 98 void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) OVERRIDE 99 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 100 101 protected: 102 explicit LargeObjectSpace(const std::string& name, uint8_t* begin, uint8_t* end); 103 static void SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg); 104 105 // Approximate number of bytes which have been allocated into the space. 106 uint64_t num_bytes_allocated_; 107 uint64_t num_objects_allocated_; 108 uint64_t total_bytes_allocated_; 109 uint64_t total_objects_allocated_; 110 // Begin and end, may change as more large objects are allocated. 111 uint8_t* begin_; 112 uint8_t* end_; 113 114 friend class Space; 115 116 private: 117 DISALLOW_COPY_AND_ASSIGN(LargeObjectSpace); 118}; 119 120// A discontinuous large object space implemented by individual mmap/munmap calls. 121class LargeObjectMapSpace : public LargeObjectSpace { 122 public: 123 // Creates a large object space. Allocations into the large object space use memory maps instead 124 // of malloc. 125 static LargeObjectMapSpace* Create(const std::string& name); 126 // Return the storage space required by obj. 127 size_t AllocationSize(mirror::Object* obj, size_t* usable_size); 128 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, 129 size_t* usable_size, size_t* bytes_tl_bulk_allocated); 130 size_t Free(Thread* self, mirror::Object* ptr); 131 void Walk(DlMallocSpace::WalkCallback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_); 132 // TODO: disabling thread safety analysis as this may be called when we already hold lock_. 133 bool Contains(const mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS; 134 135 protected: 136 explicit LargeObjectMapSpace(const std::string& name); 137 virtual ~LargeObjectMapSpace() {} 138 139 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified. 140 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 141 std::vector<mirror::Object*, TrackingAllocator<mirror::Object*, kAllocatorTagLOS>> large_objects_ 142 GUARDED_BY(lock_); 143 typedef SafeMap<mirror::Object*, MemMap*, std::less<mirror::Object*>, 144 TrackingAllocator<std::pair<mirror::Object*, MemMap*>, kAllocatorTagLOSMaps>> MemMaps; 145 MemMaps mem_maps_ GUARDED_BY(lock_); 146}; 147 148// A continuous large object space with a free-list to handle holes. 149class FreeListSpace FINAL : public LargeObjectSpace { 150 public: 151 static constexpr size_t kAlignment = kPageSize; 152 153 virtual ~FreeListSpace(); 154 static FreeListSpace* Create(const std::string& name, uint8_t* requested_begin, size_t capacity); 155 size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE 156 EXCLUSIVE_LOCKS_REQUIRED(lock_); 157 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, 158 size_t* usable_size, size_t* bytes_tl_bulk_allocated) OVERRIDE; 159 size_t Free(Thread* self, mirror::Object* obj) OVERRIDE; 160 void Walk(DlMallocSpace::WalkCallback callback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_); 161 void Dump(std::ostream& os) const; 162 163 protected: 164 FreeListSpace(const std::string& name, MemMap* mem_map, uint8_t* begin, uint8_t* end); 165 size_t GetSlotIndexForAddress(uintptr_t address) const { 166 DCHECK(Contains(reinterpret_cast<mirror::Object*>(address))); 167 return (address - reinterpret_cast<uintptr_t>(Begin())) / kAlignment; 168 } 169 size_t GetSlotIndexForAllocationInfo(const AllocationInfo* info) const; 170 AllocationInfo* GetAllocationInfoForAddress(uintptr_t address); 171 const AllocationInfo* GetAllocationInfoForAddress(uintptr_t address) const; 172 uintptr_t GetAllocationAddressForSlot(size_t slot) const { 173 return reinterpret_cast<uintptr_t>(Begin()) + slot * kAlignment; 174 } 175 uintptr_t GetAddressForAllocationInfo(const AllocationInfo* info) const { 176 return GetAllocationAddressForSlot(GetSlotIndexForAllocationInfo(info)); 177 } 178 // Removes header from the free blocks set by finding the corresponding iterator and erasing it. 179 void RemoveFreePrev(AllocationInfo* info) EXCLUSIVE_LOCKS_REQUIRED(lock_); 180 181 class SortByPrevFree { 182 public: 183 bool operator()(const AllocationInfo* a, const AllocationInfo* b) const; 184 }; 185 typedef std::set<AllocationInfo*, SortByPrevFree, 186 TrackingAllocator<AllocationInfo*, kAllocatorTagLOSFreeList>> FreeBlocks; 187 188 // There is not footer for any allocations at the end of the space, so we keep track of how much 189 // free space there is at the end manually. 190 std::unique_ptr<MemMap> mem_map_; 191 // Side table for allocation info, one per page. 192 std::unique_ptr<MemMap> allocation_info_map_; 193 AllocationInfo* allocation_info_; 194 195 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 196 // Free bytes at the end of the space. 197 size_t free_end_ GUARDED_BY(lock_); 198 FreeBlocks free_blocks_ GUARDED_BY(lock_); 199}; 200 201} // namespace space 202} // namespace gc 203} // namespace art 204 205#endif // ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_ 206