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); |
| 48 | VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<byte*>(object_without_rdz) + num_bytes, |
| 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 | |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 87 | LargeObjectSpace::LargeObjectSpace(const std::string& name, byte* begin, byte* 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); |
| 123 | size_t allocation_size = mem_map->Size(); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 124 | DCHECK(bytes_allocated != nullptr); |
| 125 | begin_ = std::min(begin_, reinterpret_cast<byte*>(obj)); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 126 | byte* obj_end = reinterpret_cast<byte*>(obj) + allocation_size; |
| 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 | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 148 | DCHECK_GE(num_bytes_allocated_, found->second->Size()); |
| 149 | size_t allocation_size = found->second->Size(); |
| 150 | num_bytes_allocated_ -= allocation_size; |
| 151 | --num_objects_allocated_; |
| 152 | delete found->second; |
| 153 | mem_maps_.erase(found); |
| 154 | return allocation_size; |
| 155 | } |
| 156 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 157 | size_t LargeObjectMapSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 158 | MutexLock mu(Thread::Current(), lock_); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 159 | auto found = mem_maps_.find(obj); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 160 | CHECK(found != mem_maps_.end()) << "Attempted to get size of a large object which is not live"; |
| 161 | return found->second->Size(); |
| 162 | } |
| 163 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 164 | 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] | 165 | size_t total = 0; |
| 166 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 167 | if (kDebugSpaces) { |
| 168 | CHECK(Contains(ptrs[i])); |
| 169 | } |
| 170 | total += Free(self, ptrs[i]); |
| 171 | } |
| 172 | return total; |
| 173 | } |
| 174 | |
| 175 | void LargeObjectMapSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) { |
| 176 | MutexLock mu(Thread::Current(), lock_); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 177 | for (auto it = mem_maps_.begin(); it != mem_maps_.end(); ++it) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 178 | MemMap* mem_map = it->second; |
| 179 | callback(mem_map->Begin(), mem_map->End(), mem_map->Size(), arg); |
| 180 | callback(NULL, NULL, 0, arg); |
| 181 | } |
| 182 | } |
| 183 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 184 | bool LargeObjectMapSpace::Contains(const mirror::Object* obj) const { |
Ian Rogers | a3dd0b3 | 2013-03-19 19:30:59 -0700 | [diff] [blame] | 185 | Thread* self = Thread::Current(); |
| 186 | if (lock_.IsExclusiveHeld(self)) { |
| 187 | // We hold lock_ so do the check. |
| 188 | return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end(); |
| 189 | } else { |
| 190 | MutexLock mu(self, lock_); |
| 191 | return mem_maps_.find(const_cast<mirror::Object*>(obj)) != mem_maps_.end(); |
| 192 | } |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | FreeListSpace* FreeListSpace::Create(const std::string& name, byte* requested_begin, size_t size) { |
Brian Carlstrom | 4274889 | 2013-07-18 18:04:08 -0700 | [diff] [blame] | 196 | CHECK_EQ(size % kAlignment, 0U); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 197 | std::string error_msg; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 198 | MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, size, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 199 | PROT_READ | PROT_WRITE, true, &error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 200 | 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] | 201 | return new FreeListSpace(name, mem_map, mem_map->Begin(), mem_map->End()); |
| 202 | } |
| 203 | |
| 204 | FreeListSpace::FreeListSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end) |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 205 | : LargeObjectSpace(name, begin, end), |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 206 | mem_map_(mem_map), |
| 207 | lock_("free list space lock", kAllocSpaceLock) { |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 208 | free_end_ = end - begin; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 211 | FreeListSpace::~FreeListSpace() {} |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 212 | |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 213 | void FreeListSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) { |
| 214 | MutexLock mu(Thread::Current(), lock_); |
| 215 | uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_; |
| 216 | AllocationHeader* cur_header = reinterpret_cast<AllocationHeader*>(Begin()); |
| 217 | while (reinterpret_cast<uintptr_t>(cur_header) < free_end_start) { |
| 218 | cur_header = cur_header->GetNextNonFree(); |
| 219 | size_t alloc_size = cur_header->AllocationSize(); |
| 220 | byte* byte_start = reinterpret_cast<byte*>(cur_header->GetObjectAddress()); |
| 221 | byte* byte_end = byte_start + alloc_size - sizeof(AllocationHeader); |
| 222 | callback(byte_start, byte_end, alloc_size, arg); |
| 223 | callback(NULL, NULL, 0, arg); |
| 224 | cur_header = reinterpret_cast<AllocationHeader*>(byte_end); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 228 | void FreeListSpace::RemoveFreePrev(AllocationHeader* header) { |
| 229 | CHECK(!header->IsFree()); |
| 230 | CHECK_GT(header->GetPrevFree(), size_t(0)); |
| 231 | FreeBlocks::iterator found = free_blocks_.lower_bound(header); |
| 232 | CHECK(found != free_blocks_.end()); |
| 233 | CHECK_EQ(*found, header); |
| 234 | free_blocks_.erase(found); |
| 235 | } |
| 236 | |
| 237 | FreeListSpace::AllocationHeader* FreeListSpace::GetAllocationHeader(const mirror::Object* obj) { |
| 238 | DCHECK(Contains(obj)); |
| 239 | return reinterpret_cast<AllocationHeader*>(reinterpret_cast<uintptr_t>(obj) - |
| 240 | sizeof(AllocationHeader)); |
| 241 | } |
| 242 | |
| 243 | FreeListSpace::AllocationHeader* FreeListSpace::AllocationHeader::GetNextNonFree() { |
| 244 | // We know that there has to be at least one object after us or else we would have |
| 245 | // coalesced with the free end region. May be worth investigating a better way to do this |
| 246 | // as it may be expensive for large allocations. |
| 247 | for (uintptr_t pos = reinterpret_cast<uintptr_t>(this);; pos += kAlignment) { |
| 248 | AllocationHeader* cur = reinterpret_cast<AllocationHeader*>(pos); |
| 249 | if (!cur->IsFree()) return cur; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 253 | size_t FreeListSpace::Free(Thread* self, mirror::Object* obj) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 254 | MutexLock mu(self, lock_); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 255 | DCHECK(Contains(obj)); |
| 256 | AllocationHeader* header = GetAllocationHeader(obj); |
| 257 | CHECK(IsAligned<kAlignment>(header)); |
| 258 | size_t allocation_size = header->AllocationSize(); |
| 259 | DCHECK_GT(allocation_size, size_t(0)); |
| 260 | DCHECK(IsAligned<kAlignment>(allocation_size)); |
| 261 | // Look at the next chunk. |
| 262 | AllocationHeader* next_header = header->GetNextAllocationHeader(); |
| 263 | // Calculate the start of the end free block. |
| 264 | uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_; |
| 265 | size_t header_prev_free = header->GetPrevFree(); |
| 266 | size_t new_free_size = allocation_size; |
| 267 | if (header_prev_free) { |
| 268 | new_free_size += header_prev_free; |
| 269 | RemoveFreePrev(header); |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 270 | } |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 271 | if (reinterpret_cast<uintptr_t>(next_header) >= free_end_start) { |
| 272 | // Easy case, the next chunk is the end free region. |
| 273 | CHECK_EQ(reinterpret_cast<uintptr_t>(next_header), free_end_start); |
| 274 | free_end_ += new_free_size; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 275 | } else { |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 276 | AllocationHeader* new_free_header; |
| 277 | DCHECK(IsAligned<kAlignment>(next_header)); |
| 278 | if (next_header->IsFree()) { |
| 279 | // Find the next chunk by reading each page until we hit one with non-zero chunk. |
| 280 | AllocationHeader* next_next_header = next_header->GetNextNonFree(); |
| 281 | DCHECK(IsAligned<kAlignment>(next_next_header)); |
| 282 | DCHECK(IsAligned<kAlignment>(next_next_header->AllocationSize())); |
| 283 | RemoveFreePrev(next_next_header); |
| 284 | new_free_header = next_next_header; |
| 285 | new_free_size += next_next_header->GetPrevFree(); |
| 286 | } else { |
| 287 | new_free_header = next_header; |
| 288 | } |
| 289 | new_free_header->prev_free_ = new_free_size; |
| 290 | free_blocks_.insert(new_free_header); |
| 291 | } |
| 292 | --num_objects_allocated_; |
| 293 | DCHECK_LE(allocation_size, num_bytes_allocated_); |
| 294 | num_bytes_allocated_ -= allocation_size; |
| 295 | madvise(header, allocation_size, MADV_DONTNEED); |
| 296 | if (kIsDebugBuild) { |
| 297 | // Can't disallow reads since we use them to find next chunks during coalescing. |
| 298 | mprotect(header, allocation_size, PROT_READ); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 299 | } |
| 300 | return allocation_size; |
| 301 | } |
| 302 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 303 | bool FreeListSpace::Contains(const mirror::Object* obj) const { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 304 | return mem_map_->HasAddress(obj); |
| 305 | } |
| 306 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 307 | size_t FreeListSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) { |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 308 | AllocationHeader* header = GetAllocationHeader(obj); |
| 309 | DCHECK(Contains(obj)); |
| 310 | DCHECK(!header->IsFree()); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 311 | size_t alloc_size = header->AllocationSize(); |
| 312 | if (usable_size != nullptr) { |
| 313 | *usable_size = alloc_size - sizeof(AllocationHeader); |
| 314 | } |
| 315 | return alloc_size; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 318 | mirror::Object* FreeListSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 319 | size_t* usable_size) { |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 320 | MutexLock mu(self, lock_); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 321 | size_t allocation_size = RoundUp(num_bytes + sizeof(AllocationHeader), kAlignment); |
| 322 | AllocationHeader temp; |
| 323 | temp.SetPrevFree(allocation_size); |
| 324 | temp.SetAllocationSize(0); |
| 325 | AllocationHeader* new_header; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 326 | // Find the smallest chunk at least num_bytes in size. |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 327 | FreeBlocks::iterator found = free_blocks_.lower_bound(&temp); |
| 328 | if (found != free_blocks_.end()) { |
| 329 | AllocationHeader* header = *found; |
| 330 | free_blocks_.erase(found); |
| 331 | |
| 332 | // Fit our object in the previous free header space. |
| 333 | new_header = header->GetPrevFreeAllocationHeader(); |
| 334 | |
| 335 | // Remove the newly allocated block from the header and update the prev_free_. |
| 336 | header->prev_free_ -= allocation_size; |
| 337 | if (header->prev_free_ > 0) { |
| 338 | // If there is remaining space, insert back into the free set. |
| 339 | free_blocks_.insert(header); |
| 340 | } |
| 341 | } else { |
| 342 | // Try to steal some memory from the free space at the end of the space. |
| 343 | if (LIKELY(free_end_ >= allocation_size)) { |
| 344 | // Fit our object at the start of the end free block. |
| 345 | new_header = reinterpret_cast<AllocationHeader*>(end_ - free_end_); |
| 346 | free_end_ -= allocation_size; |
| 347 | } else { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 348 | return nullptr; |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 349 | } |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 352 | DCHECK(bytes_allocated != nullptr); |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 353 | *bytes_allocated = allocation_size; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 354 | if (usable_size != nullptr) { |
| 355 | *usable_size = allocation_size - sizeof(AllocationHeader); |
| 356 | } |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 357 | // Need to do these inside of the lock. |
| 358 | ++num_objects_allocated_; |
| 359 | ++total_objects_allocated_; |
| 360 | num_bytes_allocated_ += allocation_size; |
| 361 | total_bytes_allocated_ += allocation_size; |
| 362 | |
| 363 | // We always put our object at the start of the free block, there can not be another free block |
| 364 | // before it. |
| 365 | if (kIsDebugBuild) { |
| 366 | mprotect(new_header, allocation_size, PROT_READ | PROT_WRITE); |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 367 | } |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 368 | new_header->SetPrevFree(0); |
| 369 | new_header->SetAllocationSize(allocation_size); |
| 370 | return new_header->GetObjectAddress(); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 373 | void FreeListSpace::Dump(std::ostream& os) const { |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 374 | MutexLock mu(Thread::Current(), const_cast<Mutex&>(lock_)); |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 375 | os << GetName() << " -" |
| 376 | << " begin: " << reinterpret_cast<void*>(Begin()) |
Mathieu Chartier | eb5710e | 2013-07-25 15:19:42 -0700 | [diff] [blame] | 377 | << " end: " << reinterpret_cast<void*>(End()) << "\n"; |
| 378 | uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_; |
| 379 | AllocationHeader* cur_header = reinterpret_cast<AllocationHeader*>(Begin()); |
| 380 | while (reinterpret_cast<uintptr_t>(cur_header) < free_end_start) { |
| 381 | byte* free_start = reinterpret_cast<byte*>(cur_header); |
| 382 | cur_header = cur_header->GetNextNonFree(); |
| 383 | byte* free_end = reinterpret_cast<byte*>(cur_header); |
| 384 | if (free_start != free_end) { |
| 385 | os << "Free block at address: " << reinterpret_cast<const void*>(free_start) |
| 386 | << " of length " << free_end - free_start << " bytes\n"; |
| 387 | } |
| 388 | size_t alloc_size = cur_header->AllocationSize(); |
| 389 | byte* byte_start = reinterpret_cast<byte*>(cur_header->GetObjectAddress()); |
| 390 | byte* byte_end = byte_start + alloc_size - sizeof(AllocationHeader); |
| 391 | os << "Large object at address: " << reinterpret_cast<const void*>(free_start) |
| 392 | << " of length " << byte_end - byte_start << " bytes\n"; |
| 393 | cur_header = reinterpret_cast<AllocationHeader*>(byte_end); |
| 394 | } |
| 395 | if (free_end_) { |
| 396 | os << "Free block at address: " << reinterpret_cast<const void*>(free_end_start) |
| 397 | << " of length " << free_end_ << " bytes\n"; |
| 398 | } |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 401 | void LargeObjectSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) { |
| 402 | SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg); |
| 403 | space::LargeObjectSpace* space = context->space->AsLargeObjectSpace(); |
| 404 | Thread* self = context->self; |
| 405 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(self); |
| 406 | // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap |
| 407 | // the bitmaps as an optimization. |
| 408 | if (!context->swap_bitmaps) { |
| 409 | accounting::LargeObjectBitmap* bitmap = space->GetLiveBitmap(); |
| 410 | for (size_t i = 0; i < num_ptrs; ++i) { |
| 411 | bitmap->Clear(ptrs[i]); |
Mathieu Chartier | db7f37d | 2014-01-10 11:09:06 -0800 | [diff] [blame] | 412 | } |
| 413 | } |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 414 | context->freed.objects += num_ptrs; |
| 415 | context->freed.bytes += space->FreeList(self, num_ptrs, ptrs); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 418 | collector::ObjectBytePair LargeObjectSpace::Sweep(bool swap_bitmaps) { |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 419 | if (Begin() >= End()) { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 420 | return collector::ObjectBytePair(0, 0); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 421 | } |
| 422 | accounting::LargeObjectBitmap* live_bitmap = GetLiveBitmap(); |
| 423 | accounting::LargeObjectBitmap* mark_bitmap = GetMarkBitmap(); |
| 424 | if (swap_bitmaps) { |
| 425 | std::swap(live_bitmap, mark_bitmap); |
| 426 | } |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 427 | AllocSpace::SweepCallbackContext scc(swap_bitmaps, this); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 428 | accounting::LargeObjectBitmap::SweepWalk(*live_bitmap, *mark_bitmap, |
| 429 | reinterpret_cast<uintptr_t>(Begin()), |
| 430 | reinterpret_cast<uintptr_t>(End()), SweepCallback, &scc); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 431 | return scc.freed; |
Mathieu Chartier | db7f37d | 2014-01-10 11:09:06 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 434 | } // namespace space |
| 435 | } // namespace gc |
| 436 | } // namespace art |