bump_pointer_space.cc revision fc4c27e4d68707271bd7578ae5c8bef93a3ea66b
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "bump_pointer_space.h" 18#include "bump_pointer_space-inl.h" 19#include "mirror/object-inl.h" 20#include "mirror/class-inl.h" 21#include "thread_list.h" 22 23namespace art { 24namespace gc { 25namespace space { 26 27BumpPointerSpace* BumpPointerSpace::Create(const std::string& name, size_t capacity, 28 byte* requested_begin) { 29 capacity = RoundUp(capacity, kPageSize); 30 std::string error_msg; 31 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), requested_begin, capacity, 32 PROT_READ | PROT_WRITE, true, &error_msg)); 33 if (mem_map.get() == nullptr) { 34 LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size " 35 << PrettySize(capacity) << " with message " << error_msg; 36 return nullptr; 37 } 38 return new BumpPointerSpace(name, mem_map.release()); 39} 40 41BumpPointerSpace::BumpPointerSpace(const std::string& name, byte* begin, byte* limit) 42 : ContinuousMemMapAllocSpace(name, nullptr, begin, begin, limit, 43 kGcRetentionPolicyAlwaysCollect), 44 growth_end_(limit), 45 objects_allocated_(0), bytes_allocated_(0), 46 block_lock_("Block lock"), 47 main_block_size_(0), 48 num_blocks_(0) { 49} 50 51BumpPointerSpace::BumpPointerSpace(const std::string& name, MemMap* mem_map) 52 : ContinuousMemMapAllocSpace(name, mem_map, mem_map->Begin(), mem_map->Begin(), mem_map->End(), 53 kGcRetentionPolicyAlwaysCollect), 54 growth_end_(mem_map->End()), 55 objects_allocated_(0), bytes_allocated_(0), 56 block_lock_("Block lock"), 57 main_block_size_(0), 58 num_blocks_(0) { 59} 60 61mirror::Object* BumpPointerSpace::Alloc(Thread*, size_t num_bytes, size_t* bytes_allocated) { 62 num_bytes = RoundUp(num_bytes, kAlignment); 63 mirror::Object* ret = AllocNonvirtual(num_bytes); 64 if (LIKELY(ret != nullptr)) { 65 *bytes_allocated = num_bytes; 66 } 67 return ret; 68} 69 70size_t BumpPointerSpace::AllocationSize(mirror::Object* obj) { 71 return AllocationSizeNonvirtual(obj); 72} 73 74void BumpPointerSpace::Clear() { 75 // Release the pages back to the operating system. 76 CHECK_NE(madvise(Begin(), Limit() - Begin(), MADV_DONTNEED), -1) << "madvise failed"; 77 // Reset the end of the space back to the beginning, we move the end forward as we allocate 78 // objects. 79 SetEnd(Begin()); 80 objects_allocated_ = 0; 81 bytes_allocated_ = 0; 82 growth_end_ = Limit(); 83 { 84 MutexLock mu(Thread::Current(), block_lock_); 85 num_blocks_ = 0; 86 main_block_size_ = 0; 87 } 88} 89 90void BumpPointerSpace::Dump(std::ostream& os) const { 91 os << reinterpret_cast<void*>(Begin()) << "-" << reinterpret_cast<void*>(End()) << " - " 92 << reinterpret_cast<void*>(Limit()); 93} 94 95mirror::Object* BumpPointerSpace::GetNextObject(mirror::Object* obj) { 96 const uintptr_t position = reinterpret_cast<uintptr_t>(obj) + obj->SizeOf(); 97 return reinterpret_cast<mirror::Object*>(RoundUp(position, kAlignment)); 98} 99 100void BumpPointerSpace::RevokeThreadLocalBuffers(Thread* thread) { 101 MutexLock mu(Thread::Current(), block_lock_); 102 RevokeThreadLocalBuffersLocked(thread); 103} 104 105void BumpPointerSpace::RevokeAllThreadLocalBuffers() { 106 Thread* self = Thread::Current(); 107 MutexLock mu(self, *Locks::runtime_shutdown_lock_); 108 MutexLock mu2(self, *Locks::thread_list_lock_); 109 // TODO: Not do a copy of the thread list? 110 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); 111 for (Thread* thread : thread_list) { 112 RevokeThreadLocalBuffers(thread); 113 } 114} 115 116void BumpPointerSpace::UpdateMainBlock() { 117 DCHECK_EQ(num_blocks_, 0U); 118 main_block_size_ = Size(); 119} 120 121// Returns the start of the storage. 122byte* BumpPointerSpace::AllocBlock(size_t bytes) { 123 bytes = RoundUp(bytes, kAlignment); 124 if (!num_blocks_) { 125 UpdateMainBlock(); 126 } 127 byte* storage = reinterpret_cast<byte*>( 128 AllocNonvirtualWithoutAccounting(bytes + sizeof(BlockHeader))); 129 if (LIKELY(storage != nullptr)) { 130 BlockHeader* header = reinterpret_cast<BlockHeader*>(storage); 131 header->size_ = bytes; // Write out the block header. 132 storage += sizeof(BlockHeader); 133 ++num_blocks_; 134 } 135 return storage; 136} 137 138void BumpPointerSpace::Walk(ObjectCallback* callback, void* arg) { 139 byte* pos = Begin(); 140 byte* main_end = pos; 141 { 142 MutexLock mu(Thread::Current(), block_lock_); 143 // If we have 0 blocks then we need to update the main header since we have bump pointer style 144 // allocation into an unbounded region (actually bounded by Capacity()). 145 if (num_blocks_ == 0) { 146 UpdateMainBlock(); 147 } 148 main_end += main_block_size_; 149 } 150 // Walk all of the objects in the main block first. 151 while (pos < main_end) { 152 mirror::Object* obj = reinterpret_cast<mirror::Object*>(pos); 153 callback(obj, arg); 154 pos = reinterpret_cast<byte*>(GetNextObject(obj)); 155 } 156 // Walk the other blocks (currently only TLABs). 157 while (pos < End()) { 158 BlockHeader* header = reinterpret_cast<BlockHeader*>(pos); 159 size_t block_size = header->size_; 160 pos += sizeof(BlockHeader); // Skip the header so that we know where the objects 161 mirror::Object* obj = reinterpret_cast<mirror::Object*>(pos); 162 const mirror::Object* end = reinterpret_cast<const mirror::Object*>(pos + block_size); 163 CHECK_LE(reinterpret_cast<const byte*>(end), End()); 164 // We don't know how many objects are allocated in the current block. When we hit a null class 165 // assume its the end. TODO: Have a thread update the header when it flushes the block? 166 while (obj < end && obj->GetClass() != nullptr) { 167 callback(obj, arg); 168 obj = GetNextObject(obj); 169 } 170 pos += block_size; 171 } 172} 173 174bool BumpPointerSpace::IsEmpty() const { 175 return Begin() == End(); 176} 177 178uint64_t BumpPointerSpace::GetBytesAllocated() { 179 // Start out pre-determined amount (blocks which are not being allocated into). 180 uint64_t total = static_cast<uint64_t>(bytes_allocated_.Load()); 181 Thread* self = Thread::Current(); 182 MutexLock mu(self, *Locks::runtime_shutdown_lock_); 183 MutexLock mu2(self, *Locks::thread_list_lock_); 184 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); 185 MutexLock mu3(Thread::Current(), block_lock_); 186 // If we don't have any blocks, we don't have any thread local buffers. This check is required 187 // since there can exist multiple bump pointer spaces which exist at the same time. 188 if (num_blocks_ > 0) { 189 for (Thread* thread : thread_list) { 190 total += thread->thread_local_pos_ - thread->thread_local_start_; 191 } 192 } 193 return total; 194} 195 196uint64_t BumpPointerSpace::GetObjectsAllocated() { 197 // Start out pre-determined amount (blocks which are not being allocated into). 198 uint64_t total = static_cast<uint64_t>(objects_allocated_.Load()); 199 Thread* self = Thread::Current(); 200 MutexLock mu(self, *Locks::runtime_shutdown_lock_); 201 MutexLock mu2(self, *Locks::thread_list_lock_); 202 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); 203 MutexLock mu3(Thread::Current(), block_lock_); 204 // If we don't have any blocks, we don't have any thread local buffers. This check is required 205 // since there can exist multiple bump pointer spaces which exist at the same time. 206 if (num_blocks_ > 0) { 207 for (Thread* thread : thread_list) { 208 total += thread->thread_local_objects_; 209 } 210 } 211 return total; 212} 213 214void BumpPointerSpace::RevokeThreadLocalBuffersLocked(Thread* thread) { 215 objects_allocated_.FetchAndAdd(thread->thread_local_objects_); 216 bytes_allocated_.FetchAndAdd(thread->thread_local_pos_ - thread->thread_local_start_); 217 thread->SetTlab(nullptr, nullptr); 218} 219 220bool BumpPointerSpace::AllocNewTlab(Thread* self, size_t bytes) { 221 MutexLock mu(Thread::Current(), block_lock_); 222 RevokeThreadLocalBuffersLocked(self); 223 byte* start = AllocBlock(bytes); 224 if (start == nullptr) { 225 return false; 226 } 227 self->SetTlab(start, start + bytes); 228 return true; 229} 230 231} // namespace space 232} // namespace gc 233} // namespace art 234