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 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 17 | #include "large_object_space.h" |
| 18 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 19 | #include <memory> |
| 20 | |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 21 | #include "gc/accounting/space_bitmap-inl.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 22 | #include "base/logging.h" |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 23 | #include "base/mutex-inl.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 24 | #include "base/stl_util.h" |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 25 | #include "image.h" |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 26 | #include "os.h" |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 27 | #include "space-inl.h" |
Brian Carlstrom | a3d2718 | 2013-11-05 23:22:27 -0800 | [diff] [blame] | 28 | #include "thread-inl.h" |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 29 | #include "utils.h" |
| 30 | |
| 31 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 32 | namespace gc { |
| 33 | namespace space { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 34 | |
Mathieu Chartier | 0767c9a | 2014-03-26 12:53:19 -0700 | [diff] [blame] | 35 | class ValgrindLargeObjectMapSpace FINAL : public LargeObjectMapSpace { |
| 36 | public: |
| 37 | explicit ValgrindLargeObjectMapSpace(const std::string& name) : LargeObjectMapSpace(name) { |
| 38 | } |
| 39 | |
| 40 | virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 41 | size_t* usable_size) OVERRIDE { |
| 42 | mirror::Object* obj = |
| 43 | LargeObjectMapSpace::Alloc(self, num_bytes + kValgrindRedZoneBytes * 2, bytes_allocated, |
| 44 | usable_size); |
| 45 | mirror::Object* object_without_rdz = reinterpret_cast<mirror::Object*>( |
| 46 | reinterpret_cast<uintptr_t>(obj) + kValgrindRedZoneBytes); |
| 47 | VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<void*>(obj), kValgrindRedZoneBytes); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 48 | VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<uint8_t*>(object_without_rdz) + num_bytes, |
Mathieu Chartier | 0767c9a | 2014-03-26 12:53:19 -0700 | [diff] [blame] | 49 | kValgrindRedZoneBytes); |
| 50 | if (usable_size != nullptr) { |
| 51 | *usable_size = num_bytes; // Since we have redzones, shrink the usable size. |
| 52 | } |
| 53 | return object_without_rdz; |
| 54 | } |
| 55 | |
| 56 | virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE { |
| 57 | mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>( |
| 58 | reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes); |
| 59 | return LargeObjectMapSpace::AllocationSize(object_with_rdz, usable_size); |
| 60 | } |
| 61 | |
| 62 | virtual size_t Free(Thread* self, mirror::Object* obj) OVERRIDE { |
| 63 | mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>( |
| 64 | reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes); |
| 65 | VALGRIND_MAKE_MEM_UNDEFINED(object_with_rdz, AllocationSize(obj, nullptr)); |
| 66 | return LargeObjectMapSpace::Free(self, object_with_rdz); |
| 67 | } |
| 68 | |
| 69 | bool Contains(const mirror::Object* obj) const OVERRIDE { |
| 70 | mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>( |
| 71 | reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes); |
| 72 | return LargeObjectMapSpace::Contains(object_with_rdz); |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | static constexpr size_t kValgrindRedZoneBytes = kPageSize; |
| 77 | }; |
| 78 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 79 | void LargeObjectSpace::SwapBitmaps() { |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 80 | live_bitmap_.swap(mark_bitmap_); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 81 | // Swap names to get more descriptive diagnostics. |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 82 | std::string temp_name = live_bitmap_->GetName(); |
| 83 | live_bitmap_->SetName(mark_bitmap_->GetName()); |
| 84 | mark_bitmap_->SetName(temp_name); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 87 | LargeObjectSpace::LargeObjectSpace(const std::string& name, uint8_t* begin, uint8_t* end) |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 88 | : DiscontinuousSpace(name, kGcRetentionPolicyAlwaysCollect), |
| 89 | num_bytes_allocated_(0), num_objects_allocated_(0), total_bytes_allocated_(0), |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 90 | total_objects_allocated_(0), begin_(begin), end_(end) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
| 94 | void LargeObjectSpace::CopyLiveToMarked() { |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 95 | mark_bitmap_->CopyFrom(live_bitmap_.get()); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | LargeObjectMapSpace::LargeObjectMapSpace(const std::string& name) |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 99 | : LargeObjectSpace(name, nullptr, nullptr), |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 100 | lock_("large object map space lock", kAllocSpaceLock) {} |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 101 | |
| 102 | LargeObjectMapSpace* LargeObjectMapSpace::Create(const std::string& name) { |
Mathieu Chartier | da44d77 | 2014-04-01 15:01:46 -0700 | [diff] [blame] | 103 | if (Runtime::Current()->RunningOnValgrind()) { |
Mathieu Chartier | 0767c9a | 2014-03-26 12:53:19 -0700 | [diff] [blame] | 104 | return new ValgrindLargeObjectMapSpace(name); |
| 105 | } else { |
| 106 | return new LargeObjectMapSpace(name); |
| 107 | } |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 110 | mirror::Object* LargeObjectMapSpace::Alloc(Thread* self, size_t num_bytes, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 111 | size_t* bytes_allocated, size_t* usable_size) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 112 | std::string error_msg; |
Ian Rogers | a40307e | 2013-02-22 11:32:44 -0800 | [diff] [blame] | 113 | MemMap* mem_map = MemMap::MapAnonymous("large object space allocation", NULL, num_bytes, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 114 | PROT_READ | PROT_WRITE, true, &error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 115 | if (UNLIKELY(mem_map == NULL)) { |
| 116 | LOG(WARNING) << "Large object allocation failed: " << error_msg; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 117 | return NULL; |
| 118 | } |
| 119 | MutexLock mu(self, lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 120 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(mem_map->Begin()); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 121 | large_objects_.push_back(obj); |
| 122 | mem_maps_.Put(obj, mem_map); |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 123 | const size_t allocation_size = mem_map->BaseSize(); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 124 | DCHECK(bytes_allocated != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 125 | begin_ = std::min(begin_, reinterpret_cast<uint8_t*>(obj)); |
| 126 | uint8_t* obj_end = reinterpret_cast<uint8_t*>(obj) + allocation_size; |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 127 | if (end_ == nullptr || obj_end > end_) { |
| 128 | end_ = obj_end; |
| 129 | } |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 130 | *bytes_allocated = allocation_size; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 131 | if (usable_size != nullptr) { |
| 132 | *usable_size = allocation_size; |
| 133 | } |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 134 | num_bytes_allocated_ += allocation_size; |
| 135 | total_bytes_allocated_ += allocation_size; |
| 136 | ++num_objects_allocated_; |
| 137 | ++total_objects_allocated_; |
| 138 | return obj; |
| 139 | } |
| 140 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 141 | size_t LargeObjectMapSpace::Free(Thread* self, mirror::Object* ptr) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 142 | MutexLock mu(self, lock_); |
| 143 | MemMaps::iterator found = mem_maps_.find(ptr); |
Mathieu Chartier | d07a913 | 2014-05-23 16:42:20 -0700 | [diff] [blame] | 144 | if (UNLIKELY(found == mem_maps_.end())) { |
| 145 | Runtime::Current()->GetHeap()->DumpSpaces(LOG(ERROR)); |
| 146 | LOG(FATAL) << "Attempted to free large object " << ptr << " which was not live"; |
| 147 | } |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 148 | const size_t map_size = found->second->BaseSize(); |
| 149 | DCHECK_GE(num_bytes_allocated_, map_size); |
| 150 | size_t allocation_size = map_size; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 151 | num_bytes_allocated_ -= allocation_size; |
| 152 | --num_objects_allocated_; |
| 153 | delete found->second; |
| 154 | mem_maps_.erase(found); |
| 155 | return allocation_size; |
| 156 | } |
| 157 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 158 | size_t LargeObjectMapSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 159 | MutexLock mu(Thread::Current(), lock_); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 160 | auto found = mem_maps_.find(obj); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 161 | CHECK(found != mem_maps_.end()) << "Attempted to get size of a large object which is not live"; |
Mathieu Chartier | 2dbe627 | 2014-09-16 10:43:23 -0700 | [diff] [blame] | 162 | return found->second->BaseSize(); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 165 | size_t LargeObjectSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 166 | size_t total = 0; |
| 167 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 168 | if (kDebugSpaces) { |
| 169 | CHECK(Contains(ptrs[i])); |
| 170 | } |
| 171 | total += Free(self, ptrs[i]); |
| 172 | } |
| 173 | return total; |
| 174 | } |
| 175 | |
| 176 | void LargeObjectMapSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) { |
| 177 | MutexLock mu(Thread::Current(), lock_); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 178 | for (auto it = mem_maps_.begin(); it != mem_maps_.end(); ++it) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 179 | MemMap* mem_map = it->second; |
| 180 | callback(mem_map->Begin(), mem_map->End(), mem_map->Size(), arg); |
| 181 | callback(NULL, NULL, 0, arg); |
| 182 | } |
| 183 | } |
| 184 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 185 | bool LargeObjectMapSpace::Contains(const mirror::Object* obj) const { |
Ian Rogers | a3dd0b3 | 2013-03-19 19:30:59 -0700 | [diff] [blame] | 186 | Thread* self = Thread::Current(); |
| 187 | if (lock_.IsExclusiveHeld(self)) { |
| 188 | // We hold lock_ so do the check. |
| 189 | return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end(); |
| 190 | } else { |
| 191 | MutexLock mu(self, lock_); |
| 192 | return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end(); |
| 193 | } |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 196 | // Keeps track of allocation sizes + whether or not the previous allocation is free. |
| 197 | // Used to coalesce free blocks and find the best fit block for an allocation. |
| 198 | class AllocationInfo { |
| 199 | public: |
| 200 | AllocationInfo() : prev_free_(0), alloc_size_(0) { |
| 201 | } |
| 202 | // Return the number of pages that the allocation info covers. |
| 203 | size_t AlignSize() const { |
| 204 | return alloc_size_ & ~kFlagFree; |
| 205 | } |
| 206 | // Returns the allocation size in bytes. |
| 207 | size_t ByteSize() const { |
| 208 | return AlignSize() * FreeListSpace::kAlignment; |
| 209 | } |
| 210 | // Updates the allocation size and whether or not it is free. |
| 211 | void SetByteSize(size_t size, bool free) { |
| 212 | DCHECK_ALIGNED(size, FreeListSpace::kAlignment); |
| 213 | alloc_size_ = (size / FreeListSpace::kAlignment) | (free ? kFlagFree : 0U); |
| 214 | } |
| 215 | bool IsFree() const { |
| 216 | return (alloc_size_ & kFlagFree) != 0; |
| 217 | } |
| 218 | // Finds and returns the next non free allocation info after ourself. |
| 219 | AllocationInfo* GetNextInfo() { |
| 220 | return this + AlignSize(); |
| 221 | } |
| 222 | const AllocationInfo* GetNextInfo() const { |
| 223 | return this + AlignSize(); |
| 224 | } |
| 225 | // Returns the previous free allocation info by using the prev_free_ member to figure out |
| 226 | // where it is. This is only used for coalescing so we only need to be able to do it if the |
| 227 | // previous allocation info is free. |
| 228 | AllocationInfo* GetPrevFreeInfo() { |
| 229 | DCHECK_NE(prev_free_, 0U); |
| 230 | return this - prev_free_; |
| 231 | } |
| 232 | // Returns the address of the object associated with this allocation info. |
| 233 | mirror::Object* GetObjectAddress() { |
| 234 | return reinterpret_cast<mirror::Object*>(reinterpret_cast<uintptr_t>(this) + sizeof(*this)); |
| 235 | } |
| 236 | // Return how many kAlignment units there are before the free block. |
| 237 | size_t GetPrevFree() const { |
| 238 | return prev_free_; |
| 239 | } |
| 240 | // Returns how many free bytes there is before the block. |
| 241 | size_t GetPrevFreeBytes() const { |
| 242 | return GetPrevFree() * FreeListSpace::kAlignment; |
| 243 | } |
| 244 | // Update the size of the free block prior to the allocation. |
| 245 | void SetPrevFreeBytes(size_t bytes) { |
| 246 | DCHECK_ALIGNED(bytes, FreeListSpace::kAlignment); |
| 247 | prev_free_ = bytes / FreeListSpace::kAlignment; |
| 248 | } |
| 249 | |
| 250 | private: |
| 251 | // Used to implement best fit object allocation. Each allocation has an AllocationInfo which |
| 252 | // contains the size of the previous free block preceding it. Implemented in such a way that we |
| 253 | // can also find the iterator for any allocation info pointer. |
| 254 | static constexpr uint32_t kFlagFree = 0x8000000; |
| 255 | // Contains the size of the previous free block with kAlignment as the unit. If 0 then the |
| 256 | // allocation before us is not free. |
| 257 | // These variables are undefined in the middle of allocations / free blocks. |
| 258 | uint32_t prev_free_; |
| 259 | // Allocation size of this object in kAlignment as the unit. |
| 260 | uint32_t alloc_size_; |
| 261 | }; |
| 262 | |
| 263 | size_t FreeListSpace::GetSlotIndexForAllocationInfo(const AllocationInfo* info) const { |
| 264 | DCHECK_GE(info, allocation_info_); |
| 265 | DCHECK_LT(info, reinterpret_cast<AllocationInfo*>(allocation_info_map_->End())); |
| 266 | return info - allocation_info_; |
| 267 | } |
| 268 | |
| 269 | AllocationInfo* FreeListSpace::GetAllocationInfoForAddress(uintptr_t address) { |
| 270 | return &allocation_info_[GetSlotIndexForAddress(address)]; |
| 271 | } |
| 272 | |
| 273 | const AllocationInfo* FreeListSpace::GetAllocationInfoForAddress(uintptr_t address) const { |
| 274 | return &allocation_info_[GetSlotIndexForAddress(address)]; |
| 275 | } |
| 276 | |
| 277 | inline bool FreeListSpace::SortByPrevFree::operator()(const AllocationInfo* a, |
| 278 | const AllocationInfo* b) const { |
| 279 | if (a->GetPrevFree() < b->GetPrevFree()) return true; |
| 280 | if (a->GetPrevFree() > b->GetPrevFree()) return false; |
| 281 | if (a->AlignSize() < b->AlignSize()) return true; |
| 282 | if (a->AlignSize() > b->AlignSize()) return false; |
| 283 | return reinterpret_cast<uintptr_t>(a) < reinterpret_cast<uintptr_t>(b); |
| 284 | } |
| 285 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 286 | FreeListSpace* FreeListSpace::Create(const std::string& name, uint8_t* requested_begin, size_t size) { |
Brian Carlstrom | 4274889 | 2013-07-18 18:04:08 -0700 | [diff] [blame] | 287 | CHECK_EQ(size % kAlignment, 0U); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 288 | std::string error_msg; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 289 | MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, size, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 290 | PROT_READ | PROT_WRITE, true, &error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 291 | CHECK(mem_map != NULL) << "Failed to allocate large object space mem map: " << error_msg; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 292 | return new FreeListSpace(name, mem_map, mem_map->Begin(), mem_map->End()); |
| 293 | } |
| 294 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 295 | FreeListSpace::FreeListSpace(const std::string& name, MemMap* mem_map, uint8_t* begin, uint8_t* end) |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 296 | : LargeObjectSpace(name, begin, end), |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 297 | mem_map_(mem_map), |
| 298 | lock_("free list space lock", kAllocSpaceLock) { |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 299 | const size_t space_capacity = end - begin; |
| 300 | free_end_ = space_capacity; |
| 301 | CHECK_ALIGNED(space_capacity, kAlignment); |
| 302 | const size_t alloc_info_size = sizeof(AllocationInfo) * (space_capacity / kAlignment); |
| 303 | std::string error_msg; |
| 304 | allocation_info_map_.reset(MemMap::MapAnonymous("large object free list space allocation info map", |
| 305 | nullptr, alloc_info_size, PROT_READ | PROT_WRITE, |
| 306 | false, &error_msg)); |
| 307 | CHECK(allocation_info_map_.get() != nullptr) << "Failed to allocate allocation info map" |
| 308 | << error_msg; |
| 309 | allocation_info_ = reinterpret_cast<AllocationInfo*>(allocation_info_map_->Begin()); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 312 | FreeListSpace::~FreeListSpace() {} |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 313 | |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 314 | void FreeListSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) { |
| 315 | MutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 316 | const uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_; |
| 317 | AllocationInfo* cur_info = &allocation_info_[0]; |
| 318 | const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start); |
| 319 | while (cur_info < end_info) { |
| 320 | if (!cur_info->IsFree()) { |
| 321 | size_t alloc_size = cur_info->ByteSize(); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 322 | uint8_t* byte_start = reinterpret_cast<uint8_t*>(GetAddressForAllocationInfo(cur_info)); |
| 323 | uint8_t* byte_end = byte_start + alloc_size; |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 324 | callback(byte_start, byte_end, alloc_size, arg); |
| 325 | callback(nullptr, nullptr, 0, arg); |
| 326 | } |
| 327 | cur_info = cur_info->GetNextInfo(); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 328 | } |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 329 | CHECK_EQ(cur_info, end_info); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 332 | void FreeListSpace::RemoveFreePrev(AllocationInfo* info) { |
| 333 | CHECK_GT(info->GetPrevFree(), 0U); |
| 334 | auto it = free_blocks_.lower_bound(info); |
| 335 | CHECK(it != free_blocks_.end()); |
| 336 | CHECK_EQ(*it, info); |
| 337 | free_blocks_.erase(it); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 340 | size_t FreeListSpace::Free(Thread* self, mirror::Object* obj) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 341 | MutexLock mu(self, lock_); |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 342 | DCHECK(Contains(obj)) << reinterpret_cast<void*>(Begin()) << " " << obj << " " |
| 343 | << reinterpret_cast<void*>(End()); |
| 344 | DCHECK_ALIGNED(obj, kAlignment); |
| 345 | AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj)); |
| 346 | DCHECK(!info->IsFree()); |
| 347 | const size_t allocation_size = info->ByteSize(); |
| 348 | DCHECK_GT(allocation_size, 0U); |
| 349 | DCHECK_ALIGNED(allocation_size, kAlignment); |
| 350 | info->SetByteSize(allocation_size, true); // Mark as free. |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 351 | // Look at the next chunk. |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 352 | AllocationInfo* next_info = info->GetNextInfo(); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 353 | // Calculate the start of the end free block. |
| 354 | uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_; |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 355 | size_t prev_free_bytes = info->GetPrevFreeBytes(); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 356 | size_t new_free_size = allocation_size; |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 357 | if (prev_free_bytes != 0) { |
| 358 | // Coalesce with previous free chunk. |
| 359 | new_free_size += prev_free_bytes; |
| 360 | RemoveFreePrev(info); |
| 361 | info = info->GetPrevFreeInfo(); |
| 362 | // The previous allocation info must not be free since we are supposed to always coalesce. |
| 363 | DCHECK_EQ(info->GetPrevFreeBytes(), 0U) << "Previous allocation was free"; |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 364 | } |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 365 | uintptr_t next_addr = GetAddressForAllocationInfo(next_info); |
| 366 | if (next_addr >= free_end_start) { |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 367 | // Easy case, the next chunk is the end free region. |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 368 | CHECK_EQ(next_addr, free_end_start); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 369 | free_end_ += new_free_size; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 370 | } else { |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 371 | AllocationInfo* new_free_info; |
| 372 | if (next_info->IsFree()) { |
| 373 | AllocationInfo* next_next_info = next_info->GetNextInfo(); |
| 374 | // Next next info can't be free since we always coalesce. |
| 375 | DCHECK(!next_next_info->IsFree()); |
| 376 | DCHECK(IsAligned<kAlignment>(next_next_info->ByteSize())); |
| 377 | new_free_info = next_next_info; |
| 378 | new_free_size += next_next_info->GetPrevFreeBytes(); |
| 379 | RemoveFreePrev(next_next_info); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 380 | } else { |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 381 | new_free_info = next_info; |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 382 | } |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 383 | new_free_info->SetPrevFreeBytes(new_free_size); |
| 384 | free_blocks_.insert(new_free_info); |
| 385 | info->SetByteSize(new_free_size, true); |
| 386 | DCHECK_EQ(info->GetNextInfo(), new_free_info); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 387 | } |
| 388 | --num_objects_allocated_; |
| 389 | DCHECK_LE(allocation_size, num_bytes_allocated_); |
| 390 | num_bytes_allocated_ -= allocation_size; |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 391 | madvise(obj, allocation_size, MADV_DONTNEED); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 392 | if (kIsDebugBuild) { |
| 393 | // Can't disallow reads since we use them to find next chunks during coalescing. |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 394 | mprotect(obj, allocation_size, PROT_READ); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 395 | } |
| 396 | return allocation_size; |
| 397 | } |
| 398 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 399 | size_t FreeListSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) { |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 400 | DCHECK(Contains(obj)); |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 401 | AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj)); |
| 402 | DCHECK(!info->IsFree()); |
| 403 | size_t alloc_size = info->ByteSize(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 404 | if (usable_size != nullptr) { |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 405 | *usable_size = alloc_size; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 406 | } |
| 407 | return alloc_size; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 410 | mirror::Object* FreeListSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 411 | size_t* usable_size) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 412 | MutexLock mu(self, lock_); |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 413 | const size_t allocation_size = RoundUp(num_bytes, kAlignment); |
| 414 | AllocationInfo temp_info; |
| 415 | temp_info.SetPrevFreeBytes(allocation_size); |
| 416 | temp_info.SetByteSize(0, false); |
| 417 | AllocationInfo* new_info; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 418 | // Find the smallest chunk at least num_bytes in size. |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 419 | auto it = free_blocks_.lower_bound(&temp_info); |
| 420 | if (it != free_blocks_.end()) { |
| 421 | AllocationInfo* info = *it; |
| 422 | free_blocks_.erase(it); |
| 423 | // Fit our object in the previous allocation info free space. |
| 424 | new_info = info->GetPrevFreeInfo(); |
| 425 | // Remove the newly allocated block from the info and update the prev_free_. |
| 426 | info->SetPrevFreeBytes(info->GetPrevFreeBytes() - allocation_size); |
| 427 | if (info->GetPrevFreeBytes() > 0) { |
| 428 | AllocationInfo* new_free = info - info->GetPrevFree(); |
| 429 | new_free->SetPrevFreeBytes(0); |
| 430 | new_free->SetByteSize(info->GetPrevFreeBytes(), true); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 431 | // If there is remaining space, insert back into the free set. |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 432 | free_blocks_.insert(info); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 433 | } |
| 434 | } else { |
| 435 | // Try to steal some memory from the free space at the end of the space. |
| 436 | if (LIKELY(free_end_ >= allocation_size)) { |
| 437 | // Fit our object at the start of the end free block. |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 438 | new_info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(End()) - free_end_); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 439 | free_end_ -= allocation_size; |
| 440 | } else { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 441 | return nullptr; |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 442 | } |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 443 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 444 | DCHECK(bytes_allocated != nullptr); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 445 | *bytes_allocated = allocation_size; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 446 | if (usable_size != nullptr) { |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 447 | *usable_size = allocation_size; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 448 | } |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 449 | // Need to do these inside of the lock. |
| 450 | ++num_objects_allocated_; |
| 451 | ++total_objects_allocated_; |
| 452 | num_bytes_allocated_ += allocation_size; |
| 453 | total_bytes_allocated_ += allocation_size; |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 454 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(GetAddressForAllocationInfo(new_info)); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 455 | // We always put our object at the start of the free block, there can not be another free block |
| 456 | // before it. |
| 457 | if (kIsDebugBuild) { |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 458 | mprotect(obj, allocation_size, PROT_READ | PROT_WRITE); |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 459 | } |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 460 | new_info->SetPrevFreeBytes(0); |
| 461 | new_info->SetByteSize(allocation_size, false); |
| 462 | return obj; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 465 | void FreeListSpace::Dump(std::ostream& os) const { |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 466 | MutexLock mu(Thread::Current(), const_cast<Mutex&>(lock_)); |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 467 | os << GetName() << " -" |
| 468 | << " begin: " << reinterpret_cast<void*>(Begin()) |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 469 | << " end: " << reinterpret_cast<void*>(End()) << "\n"; |
| 470 | uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_; |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 471 | const AllocationInfo* cur_info = |
| 472 | GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(Begin())); |
| 473 | const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start); |
| 474 | while (cur_info < end_info) { |
| 475 | size_t size = cur_info->ByteSize(); |
| 476 | uintptr_t address = GetAddressForAllocationInfo(cur_info); |
| 477 | if (cur_info->IsFree()) { |
| 478 | os << "Free block at address: " << reinterpret_cast<const void*>(address) |
| 479 | << " of length " << size << " bytes\n"; |
| 480 | } else { |
| 481 | os << "Large object at address: " << reinterpret_cast<const void*>(address) |
| 482 | << " of length " << size << " bytes\n"; |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 483 | } |
Mathieu Chartier | af4edbd | 2014-09-08 17:42:48 -0700 | [diff] [blame] | 484 | cur_info = cur_info->GetNextInfo(); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 485 | } |
| 486 | if (free_end_) { |
| 487 | os << "Free block at address: " << reinterpret_cast<const void*>(free_end_start) |
| 488 | << " of length " << free_end_ << " bytes\n"; |
| 489 | } |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 492 | void LargeObjectSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) { |
| 493 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
| 494 | space::LargeObjectSpace* space = context->space->AsLargeObjectSpace(); |
| 495 | Thread* self = context->self; |
| 496 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(self); |
| 497 | // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap |
| 498 | // the bitmaps as an optimization. |
| 499 | if (!context->swap_bitmaps) { |
| 500 | accounting::LargeObjectBitmap* bitmap = space->GetLiveBitmap(); |
| 501 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 502 | bitmap->Clear(ptrs[i]); |
Mathieu Chartier | db7f37d | 2014-01-10 11:09:06 -0800 | [diff] [blame] | 503 | } |
| 504 | } |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 505 | context->freed.objects += num_ptrs; |
| 506 | context->freed.bytes += space->FreeList(self, num_ptrs, ptrs); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 509 | collector::ObjectBytePair LargeObjectSpace::Sweep(bool swap_bitmaps) { |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 510 | if (Begin() >= End()) { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 511 | return collector::ObjectBytePair(0, 0); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 512 | } |
| 513 | accounting::LargeObjectBitmap* live_bitmap = GetLiveBitmap(); |
| 514 | accounting::LargeObjectBitmap* mark_bitmap = GetMarkBitmap(); |
| 515 | if (swap_bitmaps) { |
| 516 | std::swap(live_bitmap, mark_bitmap); |
| 517 | } |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 518 | AllocSpace::SweepCallbackContext scc(swap_bitmaps, this); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 519 | accounting::LargeObjectBitmap::SweepWalk(*live_bitmap, *mark_bitmap, |
| 520 | reinterpret_cast<uintptr_t>(Begin()), |
| 521 | reinterpret_cast<uintptr_t>(End()), SweepCallback, &scc); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 522 | return scc.freed; |
Mathieu Chartier | db7f37d | 2014-01-10 11:09:06 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 525 | void LargeObjectSpace::LogFragmentationAllocFailure(std::ostream& /*os*/, |
| 526 | size_t /*failed_alloc_bytes*/) { |
| 527 | UNIMPLEMENTED(FATAL); |
| 528 | } |
| 529 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 530 | } // namespace space |
| 531 | } // namespace gc |
| 532 | } // namespace art |