bump_pointer_space.cc revision 15d3402bbf8265eb1165694da2e4117eb128f3bc
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 61void BumpPointerSpace::Clear() { 62 // Release the pages back to the operating system. 63 CHECK_NE(madvise(Begin(), Limit() - Begin(), MADV_DONTNEED), -1) << "madvise failed"; 64} 65 66void BumpPointerSpace::Reset() { 67 // Reset the end of the space back to the beginning, we move the end forward as we allocate 68 // objects. 69 SetEnd(Begin()); 70 objects_allocated_ = 0; 71 bytes_allocated_ = 0; 72 growth_end_ = Limit(); 73 { 74 MutexLock mu(Thread::Current(), block_lock_); 75 num_blocks_ = 0; 76 main_block_size_ = 0; 77 } 78} 79 80void BumpPointerSpace::Dump(std::ostream& os) const { 81 os << GetName() << " " 82 << reinterpret_cast<void*>(Begin()) << "-" << reinterpret_cast<void*>(End()) << " - " 83 << reinterpret_cast<void*>(Limit()); 84} 85 86mirror::Object* BumpPointerSpace::GetNextObject(mirror::Object* obj) { 87 const uintptr_t position = reinterpret_cast<uintptr_t>(obj) + obj->SizeOf(); 88 return reinterpret_cast<mirror::Object*>(RoundUp(position, kAlignment)); 89} 90 91void BumpPointerSpace::RevokeThreadLocalBuffers(Thread* thread) { 92 MutexLock mu(Thread::Current(), block_lock_); 93 RevokeThreadLocalBuffersLocked(thread); 94} 95 96void BumpPointerSpace::RevokeAllThreadLocalBuffers() { 97 Thread* self = Thread::Current(); 98 MutexLock mu(self, *Locks::runtime_shutdown_lock_); 99 MutexLock mu2(self, *Locks::thread_list_lock_); 100 // TODO: Not do a copy of the thread list? 101 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); 102 for (Thread* thread : thread_list) { 103 RevokeThreadLocalBuffers(thread); 104 } 105} 106 107void BumpPointerSpace::UpdateMainBlock() { 108 DCHECK_EQ(num_blocks_, 0U); 109 main_block_size_ = Size(); 110} 111 112// Returns the start of the storage. 113byte* BumpPointerSpace::AllocBlock(size_t bytes) { 114 bytes = RoundUp(bytes, kAlignment); 115 if (!num_blocks_) { 116 UpdateMainBlock(); 117 } 118 byte* storage = reinterpret_cast<byte*>( 119 AllocNonvirtualWithoutAccounting(bytes + sizeof(BlockHeader))); 120 if (LIKELY(storage != nullptr)) { 121 BlockHeader* header = reinterpret_cast<BlockHeader*>(storage); 122 header->size_ = bytes; // Write out the block header. 123 storage += sizeof(BlockHeader); 124 ++num_blocks_; 125 } 126 return storage; 127} 128 129void BumpPointerSpace::Walk(ObjectCallback* callback, void* arg) { 130 byte* pos = Begin(); 131 byte* end = End(); 132 byte* main_end = pos; 133 { 134 MutexLock mu(Thread::Current(), block_lock_); 135 // If we have 0 blocks then we need to update the main header since we have bump pointer style 136 // allocation into an unbounded region (actually bounded by Capacity()). 137 if (num_blocks_ == 0) { 138 UpdateMainBlock(); 139 } 140 main_end = Begin() + main_block_size_; 141 if (num_blocks_ == 0) { 142 // We don't have any other blocks, this means someone else may be allocating into the main 143 // block. In this case, we don't want to try and visit the other blocks after the main block 144 // since these could actually be part of the main block. 145 end = main_end; 146 } 147 } 148 // Walk all of the objects in the main block first. 149 while (pos < main_end) { 150 mirror::Object* obj = reinterpret_cast<mirror::Object*>(pos); 151 if (obj->GetClass() == nullptr) { 152 // There is a race condition where a thread has just allocated an object but not set the 153 // class. We can't know the size of this object, so we don't visit it and exit the function 154 // since there is guaranteed to be not other blocks. 155 return; 156 } else { 157 callback(obj, arg); 158 pos = reinterpret_cast<byte*>(GetNextObject(obj)); 159 } 160 } 161 // Walk the other blocks (currently only TLABs). 162 while (pos < end) { 163 BlockHeader* header = reinterpret_cast<BlockHeader*>(pos); 164 size_t block_size = header->size_; 165 pos += sizeof(BlockHeader); // Skip the header so that we know where the objects 166 mirror::Object* obj = reinterpret_cast<mirror::Object*>(pos); 167 const mirror::Object* end = reinterpret_cast<const mirror::Object*>(pos + block_size); 168 CHECK_LE(reinterpret_cast<const byte*>(end), End()); 169 // We don't know how many objects are allocated in the current block. When we hit a null class 170 // assume its the end. TODO: Have a thread update the header when it flushes the block? 171 while (obj < end && obj->GetClass() != nullptr) { 172 callback(obj, arg); 173 obj = GetNextObject(obj); 174 } 175 pos += block_size; 176 } 177} 178 179accounting::SpaceBitmap::SweepCallback* BumpPointerSpace::GetSweepCallback() { 180 LOG(FATAL) << "Unimplemented"; 181 return nullptr; 182} 183 184uint64_t BumpPointerSpace::GetBytesAllocated() { 185 // Start out pre-determined amount (blocks which are not being allocated into). 186 uint64_t total = static_cast<uint64_t>(bytes_allocated_.Load()); 187 Thread* self = Thread::Current(); 188 MutexLock mu(self, *Locks::runtime_shutdown_lock_); 189 MutexLock mu2(self, *Locks::thread_list_lock_); 190 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); 191 MutexLock mu3(Thread::Current(), block_lock_); 192 // If we don't have any blocks, we don't have any thread local buffers. This check is required 193 // since there can exist multiple bump pointer spaces which exist at the same time. 194 if (num_blocks_ > 0) { 195 for (Thread* thread : thread_list) { 196 total += thread->thread_local_pos_ - thread->thread_local_start_; 197 } 198 } 199 return total; 200} 201 202uint64_t BumpPointerSpace::GetObjectsAllocated() { 203 // Start out pre-determined amount (blocks which are not being allocated into). 204 uint64_t total = static_cast<uint64_t>(objects_allocated_.Load()); 205 Thread* self = Thread::Current(); 206 MutexLock mu(self, *Locks::runtime_shutdown_lock_); 207 MutexLock mu2(self, *Locks::thread_list_lock_); 208 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); 209 MutexLock mu3(Thread::Current(), block_lock_); 210 // If we don't have any blocks, we don't have any thread local buffers. This check is required 211 // since there can exist multiple bump pointer spaces which exist at the same time. 212 if (num_blocks_ > 0) { 213 for (Thread* thread : thread_list) { 214 total += thread->thread_local_objects_; 215 } 216 } 217 return total; 218} 219 220void BumpPointerSpace::RevokeThreadLocalBuffersLocked(Thread* thread) { 221 objects_allocated_.FetchAndAdd(thread->thread_local_objects_); 222 bytes_allocated_.FetchAndAdd(thread->thread_local_pos_ - thread->thread_local_start_); 223 thread->SetTlab(nullptr, nullptr); 224} 225 226bool BumpPointerSpace::AllocNewTlab(Thread* self, size_t bytes) { 227 MutexLock mu(Thread::Current(), block_lock_); 228 RevokeThreadLocalBuffersLocked(self); 229 byte* start = AllocBlock(bytes); 230 if (start == nullptr) { 231 return false; 232 } 233 self->SetTlab(start, start + bytes); 234 return true; 235} 236 237} // namespace space 238} // namespace gc 239} // namespace art 240