namespace art HIDDEN { namespace gc { namespace space {
class MemoryToolLargeObjectMapSpace final : public LargeObjectMapSpace { public: explicit MemoryToolLargeObjectMapSpace(const std::string& name) : LargeObjectMapSpace(name) {
}
~MemoryToolLargeObjectMapSpace() override { // Historical note: We were deleting large objects to keep Valgrind happy if there were // any large objects such as Dex cache arrays which aren't freed since they are held live // by the class linker.
}
size_t LargeObjectMapSpace::Free(Thread* self, mirror::Object* ptr) {
MutexLock mu(self, lock_); auto it = large_objects_.find(ptr); if (UNLIKELY(it == large_objects_.end())) {
Runtime::Current()->GetHeap()->DumpSpaces(LOG_STREAM(FATAL_WITHOUT_ABORT));
LOG(FATAL) << "Attempted to free large object " << ptr << " which was not live";
} const size_t map_size = it->second.mem_map.BaseSize();
DCHECK_GE(num_bytes_allocated_, map_size);
size_t allocation_size = map_size;
num_bytes_allocated_ -= allocation_size;
--num_objects_allocated_;
large_objects_.erase(it); return allocation_size;
}
size_t LargeObjectMapSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
MutexLock mu(Thread::Current(), lock_); auto it = large_objects_.find(obj);
CHECK(it != large_objects_.end()) << "Attempted to get size of a large object which is not live";
size_t alloc_size = it->second.mem_map.BaseSize(); if (usable_size != nullptr) {
*usable_size = alloc_size;
} return alloc_size;
}
size_t LargeObjectSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
size_t total = 0; for (size_t i = 0; i < num_ptrs; ++i) { if (kDebugSpaces) {
CHECK(Contains(ptrs[i]));
}
total += Free(self, ptrs[i]);
} return total;
}
bool LargeObjectMapSpace::Contains(const mirror::Object* obj) const {
Thread* self = Thread::Current(); if (lock_.IsExclusiveHeld(self)) { // We hold lock_ so do the check. return large_objects_.find(const_cast<mirror::Object*>(obj)) != large_objects_.end();
} else {
MutexLock mu(self, lock_); return large_objects_.find(const_cast<mirror::Object*>(obj)) != large_objects_.end();
}
}
// Keeps track of allocation sizes + whether or not the previous allocation is free. // Used to coalesce free blocks and find the best fit block for an allocation for best fit object // allocation. Each allocation has an AllocationInfo which contains the size of the previous free // block preceding it. Implemented in such a way that we can also find the iterator for any // allocation info pointer. class AllocationInfo { public:
AllocationInfo() : prev_free_(0), alloc_size_(0) {
} // Return the number of blocks, of the large-object alignment in size each, that the allocation // info covers.
size_t AlignSize() const { return alloc_size_ & kFlagsMask;
} // Returns the allocation size in bytes.
size_t ByteSize() const { return AlignSize() * LargeObjectSpace::ObjectAlignment();
} // Updates the allocation size and whether or not it is free. void SetByteSize(size_t size, bool free) {
DCHECK_EQ(size & ~kFlagsMask, 0u);
DCHECK_ALIGNED_PARAM(size, LargeObjectSpace::ObjectAlignment());
alloc_size_ = (size / LargeObjectSpace::ObjectAlignment()) | (free ? kFlagFree : 0u);
} // Returns true if the block is free. bool IsFree() const { return (alloc_size_ & kFlagFree) != 0;
} // Return true if the large object is a zygote object. bool IsZygoteObject() const { return (alloc_size_ & kFlagZygote) != 0;
} // Change the object to be a zygote object. void SetZygoteObject() {
alloc_size_ |= kFlagZygote;
} // Return true if this is a zygote large object. // Finds and returns the next non free allocation info after ourself.
AllocationInfo* GetNextInfo() { returnthis + AlignSize();
} const AllocationInfo* GetNextInfo() const { returnthis + AlignSize();
} // Returns the previous free allocation info by using the prev_free_ member to figure out // where it is. This is only used for coalescing so we only need to be able to do it if the // previous allocation info is free.
AllocationInfo* GetPrevFreeInfo() {
DCHECK_NE(prev_free_, 0U); returnthis - prev_free_;
} // Returns the address of the object associated with this allocation info.
mirror::Object* GetObjectAddress() { returnreinterpret_cast<mirror::Object*>(reinterpret_cast<uintptr_t>(this) + sizeof(*this));
} // Return how many units, the large-object alignment value in size, // there are before the free block.
size_t GetPrevFree() const { return prev_free_;
} // Returns how many free bytes there are before the block.
size_t GetPrevFreeBytes() const { return GetPrevFree() * LargeObjectSpace::ObjectAlignment();
} // Update the size of the free block prior to the allocation. void SetPrevFreeBytes(size_t bytes) {
DCHECK_ALIGNED_PARAM(bytes, LargeObjectSpace::ObjectAlignment());
prev_free_ = bytes / LargeObjectSpace::ObjectAlignment();
}
private: static constexpr uint32_t kFlagFree = 0x80000000; // If block is free. static constexpr uint32_t kFlagZygote = 0x40000000; // If the large object is a zygote object. static constexpr uint32_t kFlagsMask = ~(kFlagFree | kFlagZygote); // Combined flags for masking. // Contains the size of the previous free block with the large-object alignment value as the // unit. If 0 then the allocation before us is not free. // These variables are undefined in the middle of allocations / free blocks.
uint32_t prev_free_; // Allocation size of this object in the large-object alignment value as the unit.
uint32_t alloc_size_;
};
inlinebool FreeListSpace::SortByPrevFree::operator()(const AllocationInfo* a, const AllocationInfo* b) const { if (a->GetPrevFree() < b->GetPrevFree()) returntrue; if (a->GetPrevFree() > b->GetPrevFree()) returnfalse; if (a->AlignSize() < b->AlignSize()) returntrue; if (a->AlignSize() > b->AlignSize()) returnfalse; returnreinterpret_cast<uintptr_t>(a) < reinterpret_cast<uintptr_t>(b);
}
FreeListSpace* FreeListSpace::Create(const std::string& name, size_t size, uint8_t* hint_addr) {
CHECK_ALIGNED_PARAM(size, ObjectAlignment());
DCHECK_LE(gPageSize, ObjectAlignment())
<< "MapAnonymousAligned() should be used if the large-object alignment is larger than the " "runtime page size";
constexpr size_t kMinHeapSize = 2 * MB; // Keep trying to map smaller size, in case we don't succeed due to fragmentation. while (size > kMinHeapSize) { // We don't pass error_msg string to ensure that we retain errno originating out // of mmap(), if it fails.
MemMap mem_map = MemMap::MapAnonymous(name.c_str(),
hint_addr,
size,
PROT_READ | PROT_WRITE, /*low_4gb=*/true, /*reuse=*/false, /*reservation=*/nullptr, /*error_msg=*/nullptr); if (mem_map.IsValid()) { returnnew FreeListSpace(name, std::move(mem_map), mem_map.Begin(), mem_map.End());
} if (hint_addr == nullptr) {
CHECK_EQ(errno, ENOMEM) << "mmap failed for large object space: " << strerror(errno);
size = RoundUp(size >> 1, gPageSize);
}
hint_addr = nullptr;
}
LOG(WARNING) << "Failed to allocate large object space mem map: " << strerror(errno); return nullptr;
}
FreeListSpace::FreeListSpace(const std::string& name,
MemMap&& mem_map,
uint8_t* begin,
uint8_t* end)
: LargeObjectSpace(name, begin, end, "free list space lock"),
mem_map_(std::move(mem_map)) { const size_t space_capacity = end - begin;
free_end_ = space_capacity;
CHECK_ALIGNED_PARAM(space_capacity, ObjectAlignment()); const size_t alloc_info_size = sizeof(AllocationInfo) * (space_capacity / ObjectAlignment());
std::string error_msg;
allocation_info_map_ =
MemMap::MapAnonymous("large object free list space allocation info map",
alloc_info_size,
PROT_READ | PROT_WRITE, /*low_4gb=*/ false,
&error_msg);
CHECK(allocation_info_map_.IsValid()) << "Failed to allocate allocation info map" << error_msg;
allocation_info_ = reinterpret_cast<AllocationInfo*>(allocation_info_map_.Begin());
}
void FreeListSpace::ClampGrowthLimit(size_t new_capacity) {
MutexLock mu(Thread::Current(), lock_);
new_capacity = RoundUp(new_capacity, ObjectAlignment()); if (new_capacity >= Size()) { return;
}
size_t diff = Size() - new_capacity; // If we don't have enough free-bytes at the end to clamp, then do the best // that we can. if (diff > free_end_) {
new_capacity = Size() - free_end_;
diff = free_end_;
}
size_t alloc_info_size = sizeof(AllocationInfo) * (new_capacity / ObjectAlignment());
allocation_info_map_.SetSize(alloc_info_size);
mem_map_.SetSize(new_capacity); // We don't need to change anything in 'free_blocks_' as the free block at // the end of the space isn't in there.
free_end_ -= diff;
end_ -= diff;
}
// madvise the pages without lock
madvise(obj, allocation_size, MADV_DONTNEED); if (kIsDebugBuild) { // Can't disallow reads since we use them to find next chunks during coalescing.
CheckedCall(mprotect, __FUNCTION__, obj, allocation_size, PROT_READ);
}
MutexLock mu(self, lock_);
info->SetByteSize(allocation_size, true); // Mark as free. // Look at the next chunk.
AllocationInfo* next_info = info->GetNextInfo(); // Calculate the start of the end free block.
uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_;
size_t prev_free_bytes = info->GetPrevFreeBytes();
size_t new_free_size = allocation_size; if (prev_free_bytes != 0) { // Coalesce with previous free chunk.
new_free_size += prev_free_bytes;
RemoveFreePrev(info);
info = info->GetPrevFreeInfo(); // The previous allocation info must not be free since we are supposed to always coalesce.
DCHECK_EQ(info->GetPrevFreeBytes(), 0U) << "Previous allocation was free";
} // NOTE: next_info could be pointing right after the allocation_info_map_ // when freeing object in the very end of the space. But that's safe // as we don't dereference it in that case. We only use it to calculate // next_addr using offset within the map.
uintptr_t next_addr = GetAddressForAllocationInfo(next_info); if (next_addr >= free_end_start) { // Easy case, the next chunk is the end free region.
CHECK_EQ(next_addr, free_end_start);
free_end_ += new_free_size;
} else {
AllocationInfo* new_free_info; if (next_info->IsFree()) {
AllocationInfo* next_next_info = next_info->GetNextInfo(); // Next next info can't be free since we always coalesce.
DCHECK(!next_next_info->IsFree());
DCHECK_ALIGNED_PARAM(next_next_info->ByteSize(), ObjectAlignment());
new_free_info = next_next_info;
new_free_size += next_next_info->GetPrevFreeBytes();
RemoveFreePrev(next_next_info);
} else {
new_free_info = next_info;
}
new_free_info->SetPrevFreeBytes(new_free_size);
free_blocks_.insert(new_free_info);
info->SetByteSize(new_free_size, true);
DCHECK_EQ(info->GetNextInfo(), new_free_info);
}
--num_objects_allocated_;
DCHECK_LE(allocation_size, num_bytes_allocated_);
num_bytes_allocated_ -= allocation_size; return allocation_size;
}
mirror::Object* FreeListSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
size_t* usable_size, size_t* bytes_tl_bulk_allocated) {
MutexLock mu(self, lock_); const size_t allocation_size = RoundUp(num_bytes, ObjectAlignment());
AllocationInfo temp_info;
temp_info.SetPrevFreeBytes(allocation_size);
temp_info.SetByteSize(0, false);
AllocationInfo* new_info; // Find the smallest chunk at least num_bytes in size. auto it = free_blocks_.lower_bound(&temp_info); if (it != free_blocks_.end()) {
AllocationInfo* info = *it;
free_blocks_.erase(it); // Fit our object in the previous allocation info free space.
new_info = info->GetPrevFreeInfo(); // Remove the newly allocated block from the info and update the prev_free_.
info->SetPrevFreeBytes(info->GetPrevFreeBytes() - allocation_size); if (info->GetPrevFreeBytes() > 0) {
AllocationInfo* new_free = info - info->GetPrevFree();
new_free->SetPrevFreeBytes(0);
new_free->SetByteSize(info->GetPrevFreeBytes(), true); // If there is remaining space, insert back into the free set.
free_blocks_.insert(info);
}
} else { // Try to steal some memory from the free space at the end of the space. if (LIKELY(free_end_ >= allocation_size)) { // Fit our object at the start of the end free block.
new_info = GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(End()) - free_end_);
free_end_ -= allocation_size;
} else { return nullptr;
}
}
DCHECK(bytes_allocated != nullptr);
*bytes_allocated = allocation_size; if (usable_size != nullptr) {
*usable_size = allocation_size;
}
DCHECK(bytes_tl_bulk_allocated != nullptr);
*bytes_tl_bulk_allocated = allocation_size; // Need to do these inside of the lock.
++num_objects_allocated_;
++total_objects_allocated_;
num_bytes_allocated_ += allocation_size;
total_bytes_allocated_ += allocation_size;
mirror::Object* obj = reinterpret_cast<mirror::Object*>(GetAddressForAllocationInfo(new_info)); // We always put our object at the start of the free block, there cannot be another free block // before it. if (kIsDebugBuild) {
CheckedCall(mprotect, __FUNCTION__, obj, allocation_size, PROT_READ | PROT_WRITE);
}
new_info->SetPrevFreeBytes(0);
new_info->SetByteSize(allocation_size, false); return obj;
}
void FreeListSpace::Dump(std::ostream& os) const {
MutexLock mu(Thread::Current(), lock_);
os << GetName() << " -"
<< " begin: " << reinterpret_cast<void*>(Begin())
<< " end: " << reinterpret_cast<void*>(End()) << "\n";
uintptr_t free_end_start = reinterpret_cast<uintptr_t>(end_) - free_end_; const AllocationInfo* cur_info =
GetAllocationInfoForAddress(reinterpret_cast<uintptr_t>(Begin())); const AllocationInfo* end_info = GetAllocationInfoForAddress(free_end_start); while (cur_info < end_info) {
size_t size = cur_info->ByteSize();
uintptr_t address = GetAddressForAllocationInfo(cur_info); if (cur_info->IsFree()) {
os << "Free block at address: " << reinterpret_cast<constvoid*>(address)
<< " of length " << size << " bytes\n";
} else {
os << "Large object at address: " << reinterpret_cast<constvoid*>(address)
<< " of length " << size << " bytes\n";
}
cur_info = cur_info->GetNextInfo();
} if (free_end_) {
os << "Free block at address: " << reinterpret_cast<constvoid*>(free_end_start)
<< " of length " << free_end_ << " bytes\n";
}
}
void LargeObjectSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
space::LargeObjectSpace* space = context->space->AsLargeObjectSpace();
Thread* self = context->self;
Locks::heap_bitmap_lock_->AssertExclusiveHeld(self); // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap // the bitmaps as an optimization. if (!context->swap_bitmaps) {
accounting::LargeObjectBitmap* bitmap = space->GetLiveBitmap(); for (size_t i = 0; i < num_ptrs; ++i) {
bitmap->Clear(ptrs[i]);
}
}
context->freed.objects += num_ptrs;
context->freed.bytes += space->FreeList(self, num_ptrs, ptrs);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.