blob: 9436c3f700136cdfa10e31d80a12f70b50ded4f0 [file] [log] [blame]
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -07001/*
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 Rogers1d54e732013-05-02 21:10:01 -070017#include "large_object_space.h"
18
Ian Rogers700a4022014-05-19 16:49:03 -070019#include <memory>
20
Mathieu Chartierc8980de2015-04-19 13:36:11 -070021#include "gc/accounting/heap_bitmap-inl.h"
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070022#include "gc/accounting/space_bitmap-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070024#include "base/mutex-inl.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080025#include "base/stl_util.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070026#include "image.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070027#include "os.h"
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070028#include "space-inl.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080029#include "thread-inl.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070030
31namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070032namespace gc {
33namespace space {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070034
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070035class ValgrindLargeObjectMapSpace FINAL : public LargeObjectMapSpace {
36 public:
37 explicit ValgrindLargeObjectMapSpace(const std::string& name) : LargeObjectMapSpace(name) {
38 }
39
Mathieu Chartier9086b652015-04-14 09:35:18 -070040 ~ValgrindLargeObjectMapSpace() OVERRIDE {
41 // Keep valgrind happy if there is any large objects such as dex cache arrays which aren't
42 // freed since they are held live by the class linker.
43 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -070044 for (auto& m : large_objects_) {
45 delete m.second.mem_map;
Mathieu Chartier9086b652015-04-14 09:35:18 -070046 }
47 }
48
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070049 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070050 size_t* usable_size, size_t* bytes_tl_bulk_allocated)
51 OVERRIDE {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070052 mirror::Object* obj =
53 LargeObjectMapSpace::Alloc(self, num_bytes + kValgrindRedZoneBytes * 2, bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070054 usable_size, bytes_tl_bulk_allocated);
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070055 mirror::Object* object_without_rdz = reinterpret_cast<mirror::Object*>(
56 reinterpret_cast<uintptr_t>(obj) + kValgrindRedZoneBytes);
57 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<void*>(obj), kValgrindRedZoneBytes);
Ian Rogers13735952014-10-08 12:43:28 -070058 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<uint8_t*>(object_without_rdz) + num_bytes,
Mathieu Chartier0767c9a2014-03-26 12:53:19 -070059 kValgrindRedZoneBytes);
60 if (usable_size != nullptr) {
61 *usable_size = num_bytes; // Since we have redzones, shrink the usable size.
62 }
63 return object_without_rdz;
64 }
65
66 virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE {
67 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
68 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
69 return LargeObjectMapSpace::AllocationSize(object_with_rdz, usable_size);
70 }
71
72 virtual size_t Free(Thread* self, mirror::Object* obj) OVERRIDE {
73 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
74 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
75 VALGRIND_MAKE_MEM_UNDEFINED(object_with_rdz, AllocationSize(obj, nullptr));
76 return LargeObjectMapSpace::Free(self, object_with_rdz);
77 }
78
79 bool Contains(const mirror::Object* obj) const OVERRIDE {
80 mirror::Object* object_with_rdz = reinterpret_cast<mirror::Object*>(
81 reinterpret_cast<uintptr_t>(obj) - kValgrindRedZoneBytes);
82 return LargeObjectMapSpace::Contains(object_with_rdz);
83 }
84
85 private:
86 static constexpr size_t kValgrindRedZoneBytes = kPageSize;
87};
88
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070089void LargeObjectSpace::SwapBitmaps() {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070090 live_bitmap_.swap(mark_bitmap_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070091 // Swap names to get more descriptive diagnostics.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070092 std::string temp_name = live_bitmap_->GetName();
93 live_bitmap_->SetName(mark_bitmap_->GetName());
94 mark_bitmap_->SetName(temp_name);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070095}
96
Ian Rogers13735952014-10-08 12:43:28 -070097LargeObjectSpace::LargeObjectSpace(const std::string& name, uint8_t* begin, uint8_t* end)
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070098 : DiscontinuousSpace(name, kGcRetentionPolicyAlwaysCollect),
99 num_bytes_allocated_(0), num_objects_allocated_(0), total_bytes_allocated_(0),
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700100 total_objects_allocated_(0), begin_(begin), end_(end) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700101}
102
103
104void LargeObjectSpace::CopyLiveToMarked() {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700105 mark_bitmap_->CopyFrom(live_bitmap_.get());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700106}
107
108LargeObjectMapSpace::LargeObjectMapSpace(const std::string& name)
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700109 : LargeObjectSpace(name, nullptr, nullptr),
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700110 lock_("large object map space lock", kAllocSpaceLock) {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700111
112LargeObjectMapSpace* LargeObjectMapSpace::Create(const std::string& name) {
Mathieu Chartierda44d772014-04-01 15:01:46 -0700113 if (Runtime::Current()->RunningOnValgrind()) {
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700114 return new ValgrindLargeObjectMapSpace(name);
115 } else {
116 return new LargeObjectMapSpace(name);
117 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700118}
119
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700120mirror::Object* LargeObjectMapSpace::Alloc(Thread* self, size_t num_bytes,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700121 size_t* bytes_allocated, size_t* usable_size,
122 size_t* bytes_tl_bulk_allocated) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700123 std::string error_msg;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000124 MemMap* mem_map = MemMap::MapAnonymous("large object space allocation", nullptr, num_bytes,
125 PROT_READ | PROT_WRITE, true, false, &error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700126 if (UNLIKELY(mem_map == nullptr)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700127 LOG(WARNING) << "Large object allocation failed: " << error_msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700128 return nullptr;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700129 }
Mathieu Chartierc8980de2015-04-19 13:36:11 -0700130 mirror::Object* const obj = reinterpret_cast<mirror::Object*>(mem_map->Begin());
131 if (kIsDebugBuild) {
132 ReaderMutexLock mu2(Thread::Current(), *Locks::heap_bitmap_lock_);
133 auto* heap = Runtime::Current()->GetHeap();
134 auto* live_bitmap = heap->GetLiveBitmap();
135 auto* space_bitmap = live_bitmap->GetContinuousSpaceBitmap(obj);
136 CHECK(space_bitmap == nullptr) << obj << " overlaps with bitmap " << *space_bitmap;
137 auto* obj_end = reinterpret_cast<mirror::Object*>(mem_map->End());
138 space_bitmap = live_bitmap->GetContinuousSpaceBitmap(obj_end - 1);
139 CHECK(space_bitmap == nullptr) << obj_end << " overlaps with bitmap " << *space_bitmap;
140 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700141 MutexLock mu(self, lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700142 large_objects_.Put(obj, LargeObject {mem_map, false /* not zygote */});
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700143 const size_t allocation_size = mem_map->BaseSize();
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700144 DCHECK(bytes_allocated != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700145 begin_ = std::min(begin_, reinterpret_cast<uint8_t*>(obj));
146 uint8_t* obj_end = reinterpret_cast<uint8_t*>(obj) + allocation_size;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700147 if (end_ == nullptr || obj_end > end_) {
148 end_ = obj_end;
149 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700150 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800151 if (usable_size != nullptr) {
152 *usable_size = allocation_size;
153 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700154 DCHECK(bytes_tl_bulk_allocated != nullptr);
155 *bytes_tl_bulk_allocated = allocation_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700156 num_bytes_allocated_ += allocation_size;
157 total_bytes_allocated_ += allocation_size;
158 ++num_objects_allocated_;
159 ++total_objects_allocated_;
160 return obj;
161}
162
Mathieu Chartiere7158112015-06-03 13:32:15 -0700163bool LargeObjectMapSpace::IsZygoteLargeObject(Thread* self, mirror::Object* obj) const {
164 MutexLock mu(self, lock_);
165 auto it = large_objects_.find(obj);
166 CHECK(it != large_objects_.end());
167 return it->second.is_zygote;
168}
169
170void LargeObjectMapSpace::SetAllLargeObjectsAsZygoteObjects(Thread* self) {
171 MutexLock mu(self, lock_);
172 for (auto& pair : large_objects_) {
173 pair.second.is_zygote = true;
174 }
175}
176
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177size_t LargeObjectMapSpace::Free(Thread* self, mirror::Object* ptr) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700178 MutexLock mu(self, lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700179 auto it = large_objects_.find(ptr);
180 if (UNLIKELY(it == large_objects_.end())) {
181 Runtime::Current()->GetHeap()->DumpSpaces(LOG(INTERNAL_FATAL));
Mathieu Chartierd07a9132014-05-23 16:42:20 -0700182 LOG(FATAL) << "Attempted to free large object " << ptr << " which was not live";
183 }
Mathieu Chartiere7158112015-06-03 13:32:15 -0700184 MemMap* mem_map = it->second.mem_map;
185 const size_t map_size = mem_map->BaseSize();
Mathieu Chartier2dbe6272014-09-16 10:43:23 -0700186 DCHECK_GE(num_bytes_allocated_, map_size);
187 size_t allocation_size = map_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700188 num_bytes_allocated_ -= allocation_size;
189 --num_objects_allocated_;
Mathieu Chartiere7158112015-06-03 13:32:15 -0700190 delete mem_map;
191 large_objects_.erase(it);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700192 return allocation_size;
193}
194
Ian Rogers6fac4472014-02-25 17:01:10 -0800195size_t LargeObjectMapSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700196 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700197 auto it = large_objects_.find(obj);
198 CHECK(it != large_objects_.end()) << "Attempted to get size of a large object which is not live";
199 size_t alloc_size = it->second.mem_map->BaseSize();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700200 if (usable_size != nullptr) {
201 *usable_size = alloc_size;
202 }
203 return alloc_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700204}
205
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206size_t LargeObjectSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700207 size_t total = 0;
208 for (size_t i = 0; i < num_ptrs; ++i) {
209 if (kDebugSpaces) {
210 CHECK(Contains(ptrs[i]));
211 }
212 total += Free(self, ptrs[i]);
213 }
214 return total;
215}
216
217void LargeObjectMapSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
218 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700219 for (auto& pair : large_objects_) {
220 MemMap* mem_map = pair.second.mem_map;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700221 callback(mem_map->Begin(), mem_map->End(), mem_map->Size(), arg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700222 callback(nullptr, nullptr, 0, arg);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700223 }
224}
225
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226bool LargeObjectMapSpace::Contains(const mirror::Object* obj) const {
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700227 Thread* self = Thread::Current();
228 if (lock_.IsExclusiveHeld(self)) {
229 // We hold lock_ so do the check.
Mathieu Chartiere7158112015-06-03 13:32:15 -0700230 return large_objects_.find(const_cast<mirror::Object*>(obj)) != large_objects_.end();
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700231 } else {
232 MutexLock mu(self, lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700233 return large_objects_.find(const_cast<mirror::Object*>(obj)) != large_objects_.end();
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700234 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700235}
236
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700237// Keeps track of allocation sizes + whether or not the previous allocation is free.
Mathieu Chartiere7158112015-06-03 13:32:15 -0700238// Used to coalesce free blocks and find the best fit block for an allocation for best fit object
239// allocation. Each allocation has an AllocationInfo which contains the size of the previous free
240// block preceding it. Implemented in such a way that we can also find the iterator for any
241// allocation info pointer.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700242class AllocationInfo {
243 public:
244 AllocationInfo() : prev_free_(0), alloc_size_(0) {
245 }
246 // Return the number of pages that the allocation info covers.
247 size_t AlignSize() const {
Mathieu Chartiere7158112015-06-03 13:32:15 -0700248 return alloc_size_ & kFlagsMask;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700249 }
250 // Returns the allocation size in bytes.
251 size_t ByteSize() const {
252 return AlignSize() * FreeListSpace::kAlignment;
253 }
254 // Updates the allocation size and whether or not it is free.
255 void SetByteSize(size_t size, bool free) {
256 DCHECK_ALIGNED(size, FreeListSpace::kAlignment);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700257 alloc_size_ = (size / FreeListSpace::kAlignment) | (free ? kFlagFree : 0u);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700258 }
Mathieu Chartiere7158112015-06-03 13:32:15 -0700259 // Returns true if the block is free.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700260 bool IsFree() const {
261 return (alloc_size_ & kFlagFree) != 0;
262 }
Mathieu Chartiere7158112015-06-03 13:32:15 -0700263 // Return true if the large object is a zygote object.
264 bool IsZygoteObject() const {
265 return (alloc_size_ & kFlagZygote) != 0;
266 }
267 // Change the object to be a zygote object.
268 void SetZygoteObject() {
269 alloc_size_ |= kFlagZygote;
270 }
271 // Return true if this is a zygote large object.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700272 // Finds and returns the next non free allocation info after ourself.
273 AllocationInfo* GetNextInfo() {
274 return this + AlignSize();
275 }
276 const AllocationInfo* GetNextInfo() const {
277 return this + AlignSize();
278 }
279 // Returns the previous free allocation info by using the prev_free_ member to figure out
280 // where it is. This is only used for coalescing so we only need to be able to do it if the
281 // previous allocation info is free.
282 AllocationInfo* GetPrevFreeInfo() {
283 DCHECK_NE(prev_free_, 0U);
284 return this - prev_free_;
285 }
286 // Returns the address of the object associated with this allocation info.
287 mirror::Object* GetObjectAddress() {
288 return reinterpret_cast<mirror::Object*>(reinterpret_cast<uintptr_t>(this) + sizeof(*this));
289 }
290 // Return how many kAlignment units there are before the free block.
291 size_t GetPrevFree() const {
292 return prev_free_;
293 }
294 // Returns how many free bytes there is before the block.
295 size_t GetPrevFreeBytes() const {
296 return GetPrevFree() * FreeListSpace::kAlignment;
297 }
298 // Update the size of the free block prior to the allocation.
299 void SetPrevFreeBytes(size_t bytes) {
300 DCHECK_ALIGNED(bytes, FreeListSpace::kAlignment);
301 prev_free_ = bytes / FreeListSpace::kAlignment;
302 }
303
304 private:
Mathieu Chartiere7158112015-06-03 13:32:15 -0700305 static constexpr uint32_t kFlagFree = 0x80000000; // If block is free.
306 static constexpr uint32_t kFlagZygote = 0x40000000; // If the large object is a zygote object.
307 static constexpr uint32_t kFlagsMask = ~(kFlagFree | kFlagZygote); // Combined flags for masking.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700308 // Contains the size of the previous free block with kAlignment as the unit. If 0 then the
309 // allocation before us is not free.
310 // These variables are undefined in the middle of allocations / free blocks.
311 uint32_t prev_free_;
312 // Allocation size of this object in kAlignment as the unit.
313 uint32_t alloc_size_;
314};
315
316size_t FreeListSpace::GetSlotIndexForAllocationInfo(const AllocationInfo* info) const {
317 DCHECK_GE(info, allocation_info_);
318 DCHECK_LT(info, reinterpret_cast<AllocationInfo*>(allocation_info_map_->End()));
319 return info - allocation_info_;
320}
321
322AllocationInfo* FreeListSpace::GetAllocationInfoForAddress(uintptr_t address) {
323 return &allocation_info_[GetSlotIndexForAddress(address)];
324}
325
326const AllocationInfo* FreeListSpace::GetAllocationInfoForAddress(uintptr_t address) const {
327 return &allocation_info_[GetSlotIndexForAddress(address)];
328}
329
330inline bool FreeListSpace::SortByPrevFree::operator()(const AllocationInfo* a,
331 const AllocationInfo* b) const {
332 if (a->GetPrevFree() < b->GetPrevFree()) return true;
333 if (a->GetPrevFree() > b->GetPrevFree()) return false;
334 if (a->AlignSize() < b->AlignSize()) return true;
335 if (a->AlignSize() > b->AlignSize()) return false;
336 return reinterpret_cast<uintptr_t>(a) < reinterpret_cast<uintptr_t>(b);
337}
338
Ian Rogers13735952014-10-08 12:43:28 -0700339FreeListSpace* FreeListSpace::Create(const std::string& name, uint8_t* requested_begin, size_t size) {
Brian Carlstrom42748892013-07-18 18:04:08 -0700340 CHECK_EQ(size % kAlignment, 0U);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700341 std::string error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700342 MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, size,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000343 PROT_READ | PROT_WRITE, true, false, &error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700344 CHECK(mem_map != nullptr) << "Failed to allocate large object space mem map: " << error_msg;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700345 return new FreeListSpace(name, mem_map, mem_map->Begin(), mem_map->End());
346}
347
Ian Rogers13735952014-10-08 12:43:28 -0700348FreeListSpace::FreeListSpace(const std::string& name, MemMap* mem_map, uint8_t* begin, uint8_t* end)
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700349 : LargeObjectSpace(name, begin, end),
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700350 mem_map_(mem_map),
351 lock_("free list space lock", kAllocSpaceLock) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700352 const size_t space_capacity = end - begin;
353 free_end_ = space_capacity;
354 CHECK_ALIGNED(space_capacity, kAlignment);
355 const size_t alloc_info_size = sizeof(AllocationInfo) * (space_capacity / kAlignment);
356 std::string error_msg;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000357 allocation_info_map_.reset(
358 MemMap::MapAnonymous("large object free list space allocation info map",
359 nullptr, alloc_info_size, PROT_READ | PROT_WRITE,
360 false, false, &error_msg));
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700361 CHECK(allocation_info_map_.get() != nullptr) << "Failed to allocate allocation info map"
362 << error_msg;
363 allocation_info_ = reinterpret_cast<AllocationInfo*>(allocation_info_map_->Begin());
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700364}
365
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700366FreeListSpace::~FreeListSpace() {}
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700367
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700368void FreeListSpace::Walk(DlMallocSpace::WalkCallback callback, void* arg) {
369 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700370 const uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
371 AllocationInfo* cur_info = &allocation_info_[0];
372 const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start);
373 while (cur_info < end_info) {
374 if (!cur_info->IsFree()) {
375 size_t alloc_size = cur_info->ByteSize();
Ian Rogers13735952014-10-08 12:43:28 -0700376 uint8_t* byte_start = reinterpret_cast<uint8_t*>(GetAddressForAllocationInfo(cur_info));
377 uint8_t* byte_end = byte_start + alloc_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700378 callback(byte_start, byte_end, alloc_size, arg);
379 callback(nullptr, nullptr, 0, arg);
380 }
381 cur_info = cur_info->GetNextInfo();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700382 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700383 CHECK_EQ(cur_info, end_info);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700384}
385
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700386void FreeListSpace::RemoveFreePrev(AllocationInfo* info) {
387 CHECK_GT(info->GetPrevFree(), 0U);
388 auto it = free_blocks_.lower_bound(info);
389 CHECK(it != free_blocks_.end());
390 CHECK_EQ(*it, info);
391 free_blocks_.erase(it);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700392}
393
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800394size_t FreeListSpace::Free(Thread* self, mirror::Object* obj) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700395 MutexLock mu(self, lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700396 DCHECK(Contains(obj)) << reinterpret_cast<void*>(Begin()) << " " << obj << " "
397 << reinterpret_cast<void*>(End());
398 DCHECK_ALIGNED(obj, kAlignment);
399 AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
400 DCHECK(!info->IsFree());
401 const size_t allocation_size = info->ByteSize();
402 DCHECK_GT(allocation_size, 0U);
403 DCHECK_ALIGNED(allocation_size, kAlignment);
404 info->SetByteSize(allocation_size, true); // Mark as free.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700405 // Look at the next chunk.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700406 AllocationInfo* next_info = info->GetNextInfo();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700407 // Calculate the start of the end free block.
408 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700409 size_t prev_free_bytes = info->GetPrevFreeBytes();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700410 size_t new_free_size = allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700411 if (prev_free_bytes != 0) {
412 // Coalesce with previous free chunk.
413 new_free_size += prev_free_bytes;
414 RemoveFreePrev(info);
415 info = info->GetPrevFreeInfo();
416 // The previous allocation info must not be free since we are supposed to always coalesce.
417 DCHECK_EQ(info->GetPrevFreeBytes(), 0U) << "Previous allocation was free";
Ian Rogers22a20862013-03-16 16:34:57 -0700418 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700419 uintptr_t next_addr = GetAddressForAllocationInfo(next_info);
420 if (next_addr >= free_end_start) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700421 // Easy case, the next chunk is the end free region.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700422 CHECK_EQ(next_addr, free_end_start);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700423 free_end_ += new_free_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700424 } else {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700425 AllocationInfo* new_free_info;
426 if (next_info->IsFree()) {
427 AllocationInfo* next_next_info = next_info->GetNextInfo();
428 // Next next info can't be free since we always coalesce.
429 DCHECK(!next_next_info->IsFree());
430 DCHECK(IsAligned<kAlignment>(next_next_info->ByteSize()));
431 new_free_info = next_next_info;
432 new_free_size += next_next_info->GetPrevFreeBytes();
433 RemoveFreePrev(next_next_info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700434 } else {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700435 new_free_info = next_info;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700436 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700437 new_free_info->SetPrevFreeBytes(new_free_size);
438 free_blocks_.insert(new_free_info);
439 info->SetByteSize(new_free_size, true);
440 DCHECK_EQ(info->GetNextInfo(), new_free_info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700441 }
442 --num_objects_allocated_;
443 DCHECK_LE(allocation_size, num_bytes_allocated_);
444 num_bytes_allocated_ -= allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700445 madvise(obj, allocation_size, MADV_DONTNEED);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700446 if (kIsDebugBuild) {
447 // Can't disallow reads since we use them to find next chunks during coalescing.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700448 mprotect(obj, allocation_size, PROT_READ);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700449 }
450 return allocation_size;
451}
452
Ian Rogers6fac4472014-02-25 17:01:10 -0800453size_t FreeListSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700454 DCHECK(Contains(obj));
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700455 AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
456 DCHECK(!info->IsFree());
457 size_t alloc_size = info->ByteSize();
Ian Rogers6fac4472014-02-25 17:01:10 -0800458 if (usable_size != nullptr) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700459 *usable_size = alloc_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800460 }
461 return alloc_size;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700462}
463
Ian Rogers6fac4472014-02-25 17:01:10 -0800464mirror::Object* FreeListSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700465 size_t* usable_size, size_t* bytes_tl_bulk_allocated) {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700466 MutexLock mu(self, lock_);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700467 const size_t allocation_size = RoundUp(num_bytes, kAlignment);
468 AllocationInfo temp_info;
469 temp_info.SetPrevFreeBytes(allocation_size);
470 temp_info.SetByteSize(0, false);
471 AllocationInfo* new_info;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700472 // Find the smallest chunk at least num_bytes in size.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700473 auto it = free_blocks_.lower_bound(&temp_info);
474 if (it != free_blocks_.end()) {
475 AllocationInfo* info = *it;
476 free_blocks_.erase(it);
477 // Fit our object in the previous allocation info free space.
478 new_info = info->GetPrevFreeInfo();
479 // Remove the newly allocated block from the info and update the prev_free_.
480 info->SetPrevFreeBytes(info->GetPrevFreeBytes() - allocation_size);
481 if (info->GetPrevFreeBytes() > 0) {
482 AllocationInfo* new_free = info - info->GetPrevFree();
483 new_free->SetPrevFreeBytes(0);
484 new_free->SetByteSize(info->GetPrevFreeBytes(), true);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700485 // If there is remaining space, insert back into the free set.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700486 free_blocks_.insert(info);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700487 }
488 } else {
489 // Try to steal some memory from the free space at the end of the space.
490 if (LIKELY(free_end_ >= allocation_size)) {
491 // Fit our object at the start of the end free block.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700492 new_info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(End()) - free_end_);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700493 free_end_ -= allocation_size;
494 } else {
Ian Rogers6fac4472014-02-25 17:01:10 -0800495 return nullptr;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700496 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700497 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800498 DCHECK(bytes_allocated != nullptr);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700499 *bytes_allocated = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800500 if (usable_size != nullptr) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700501 *usable_size = allocation_size;
Ian Rogers6fac4472014-02-25 17:01:10 -0800502 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700503 DCHECK(bytes_tl_bulk_allocated != nullptr);
504 *bytes_tl_bulk_allocated = allocation_size;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700505 // Need to do these inside of the lock.
506 ++num_objects_allocated_;
507 ++total_objects_allocated_;
508 num_bytes_allocated_ += allocation_size;
509 total_bytes_allocated_ += allocation_size;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700510 mirror::Object* obj = reinterpret_cast<mirror::Object*>(GetAddressForAllocationInfo(new_info));
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700511 // We always put our object at the start of the free block, there can not be another free block
512 // before it.
513 if (kIsDebugBuild) {
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700514 mprotect(obj, allocation_size, PROT_READ | PROT_WRITE);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700515 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700516 new_info->SetPrevFreeBytes(0);
517 new_info->SetByteSize(allocation_size, false);
518 return obj;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700519}
520
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700521void FreeListSpace::Dump(std::ostream& os) const {
Mathieu Chartiere7158112015-06-03 13:32:15 -0700522 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700523 os << GetName() << " -"
524 << " begin: " << reinterpret_cast<void*>(Begin())
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700525 << " end: " << reinterpret_cast<void*>(End()) << "\n";
526 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700527 const AllocationInfo* cur_info =
528 GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(Begin()));
529 const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start);
530 while (cur_info < end_info) {
531 size_t size = cur_info->ByteSize();
532 uintptr_t address = GetAddressForAllocationInfo(cur_info);
533 if (cur_info->IsFree()) {
534 os << "Free block at address: " << reinterpret_cast<const void*>(address)
535 << " of length " << size << " bytes\n";
536 } else {
537 os << "Large object at address: " << reinterpret_cast<const void*>(address)
538 << " of length " << size << " bytes\n";
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700539 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700540 cur_info = cur_info->GetNextInfo();
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700541 }
542 if (free_end_) {
543 os << "Free block at address: " << reinterpret_cast<const void*>(free_end_start)
544 << " of length " << free_end_ << " bytes\n";
545 }
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700546}
547
Mathieu Chartiere7158112015-06-03 13:32:15 -0700548bool FreeListSpace::IsZygoteLargeObject(Thread* self ATTRIBUTE_UNUSED, mirror::Object* obj) const {
549 const AllocationInfo* info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(obj));
550 DCHECK(info != nullptr);
551 return info->IsZygoteObject();
552}
553
554void FreeListSpace::SetAllLargeObjectsAsZygoteObjects(Thread* self) {
555 MutexLock mu(self, lock_);
556 uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
557 for (AllocationInfo* cur_info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(Begin())),
558 *end_info = GetAllocationInfoForAddress(free_end_start); cur_info < end_info;
559 cur_info = cur_info->GetNextInfo()) {
560 if (!cur_info->IsFree()) {
561 cur_info->SetZygoteObject();
562 }
563 }
564}
565
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700566void LargeObjectSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
567 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
568 space::LargeObjectSpace* space = context->space->AsLargeObjectSpace();
569 Thread* self = context->self;
570 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
571 // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
572 // the bitmaps as an optimization.
573 if (!context->swap_bitmaps) {
574 accounting::LargeObjectBitmap* bitmap = space->GetLiveBitmap();
575 for (size_t i = 0; i < num_ptrs; ++i) {
576 bitmap->Clear(ptrs[i]);
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800577 }
578 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700579 context->freed.objects += num_ptrs;
580 context->freed.bytes += space->FreeList(self, num_ptrs, ptrs);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700581}
582
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700583collector::ObjectBytePair LargeObjectSpace::Sweep(bool swap_bitmaps) {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700584 if (Begin() >= End()) {
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700585 return collector::ObjectBytePair(0, 0);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700586 }
587 accounting::LargeObjectBitmap* live_bitmap = GetLiveBitmap();
588 accounting::LargeObjectBitmap* mark_bitmap = GetMarkBitmap();
589 if (swap_bitmaps) {
590 std::swap(live_bitmap, mark_bitmap);
591 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700592 AllocSpace::SweepCallbackContext scc(swap_bitmaps, this);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700593 accounting::LargeObjectBitmap::SweepWalk(*live_bitmap, *mark_bitmap,
594 reinterpret_cast<uintptr_t>(Begin()),
595 reinterpret_cast<uintptr_t>(End()), SweepCallback, &scc);
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700596 return scc.freed;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800597}
598
Mathieu Chartierb363f662014-07-16 13:28:58 -0700599void LargeObjectSpace::LogFragmentationAllocFailure(std::ostream& /*os*/,
600 size_t /*failed_alloc_bytes*/) {
601 UNIMPLEMENTED(FATAL);
602}
603
Ian Rogers1d54e732013-05-02 21:10:01 -0700604} // namespace space
605} // namespace gc
606} // namespace art