Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 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" |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 21 | #include "thread_list.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | namespace gc { |
| 25 | namespace space { |
| 26 | |
| 27 | BumpPointerSpace* 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, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 32 | PROT_READ | PROT_WRITE, true, &error_msg)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 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 | |
| 41 | BumpPointerSpace::BumpPointerSpace(const std::string& name, byte* begin, byte* limit) |
| 42 | : ContinuousMemMapAllocSpace(name, nullptr, begin, begin, limit, |
| 43 | kGcRetentionPolicyAlwaysCollect), |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 44 | growth_end_(limit), |
| 45 | objects_allocated_(0), bytes_allocated_(0), |
| 46 | block_lock_("Block lock"), |
Mathieu Chartier | fc4c27e | 2014-02-11 11:05:41 -0800 | [diff] [blame] | 47 | main_block_size_(0), |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 48 | num_blocks_(0) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | BumpPointerSpace::BumpPointerSpace(const std::string& name, MemMap* mem_map) |
| 52 | : ContinuousMemMapAllocSpace(name, mem_map, mem_map->Begin(), mem_map->Begin(), mem_map->End(), |
| 53 | kGcRetentionPolicyAlwaysCollect), |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 54 | growth_end_(mem_map->End()), |
| 55 | objects_allocated_(0), bytes_allocated_(0), |
| 56 | block_lock_("Block lock"), |
Mathieu Chartier | fc4c27e | 2014-02-11 11:05:41 -0800 | [diff] [blame] | 57 | main_block_size_(0), |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 58 | num_blocks_(0) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 61 | void BumpPointerSpace::Clear() { |
| 62 | // Release the pages back to the operating system. |
| 63 | CHECK_NE(madvise(Begin(), Limit() - Begin(), MADV_DONTNEED), -1) << "madvise failed"; |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void BumpPointerSpace::Reset() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 67 | // Reset the end of the space back to the beginning, we move the end forward as we allocate |
| 68 | // objects. |
Mathieu Chartier | fc4c27e | 2014-02-11 11:05:41 -0800 | [diff] [blame] | 69 | SetEnd(Begin()); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 70 | objects_allocated_ = 0; |
| 71 | bytes_allocated_ = 0; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 72 | growth_end_ = Limit(); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 73 | { |
| 74 | MutexLock mu(Thread::Current(), block_lock_); |
| 75 | num_blocks_ = 0; |
Mathieu Chartier | fc4c27e | 2014-02-11 11:05:41 -0800 | [diff] [blame] | 76 | main_block_size_ = 0; |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 77 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void BumpPointerSpace::Dump(std::ostream& os) const { |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 81 | os << GetName() << " " |
| 82 | << reinterpret_cast<void*>(Begin()) << "-" << reinterpret_cast<void*>(End()) << " - " |
| 83 | << reinterpret_cast<void*>(Limit()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | mirror::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 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 91 | void BumpPointerSpace::RevokeThreadLocalBuffers(Thread* thread) { |
| 92 | MutexLock mu(Thread::Current(), block_lock_); |
| 93 | RevokeThreadLocalBuffersLocked(thread); |
| 94 | } |
| 95 | |
| 96 | void 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 | |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 107 | void BumpPointerSpace::AssertThreadLocalBuffersAreRevoked(Thread* thread) { |
| 108 | if (kIsDebugBuild) { |
| 109 | MutexLock mu(Thread::Current(), block_lock_); |
| 110 | DCHECK(!thread->HasTlab()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void BumpPointerSpace::AssertAllThreadLocalBuffersAreRevoked() { |
| 115 | if (kIsDebugBuild) { |
| 116 | Thread* self = Thread::Current(); |
| 117 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 118 | MutexLock mu2(self, *Locks::thread_list_lock_); |
| 119 | // TODO: Not do a copy of the thread list? |
| 120 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
| 121 | for (Thread* thread : thread_list) { |
| 122 | AssertThreadLocalBuffersAreRevoked(thread); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 127 | void BumpPointerSpace::UpdateMainBlock() { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 128 | DCHECK_EQ(num_blocks_, 0U); |
Mathieu Chartier | fc4c27e | 2014-02-11 11:05:41 -0800 | [diff] [blame] | 129 | main_block_size_ = Size(); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // Returns the start of the storage. |
| 133 | byte* BumpPointerSpace::AllocBlock(size_t bytes) { |
| 134 | bytes = RoundUp(bytes, kAlignment); |
| 135 | if (!num_blocks_) { |
| 136 | UpdateMainBlock(); |
| 137 | } |
| 138 | byte* storage = reinterpret_cast<byte*>( |
| 139 | AllocNonvirtualWithoutAccounting(bytes + sizeof(BlockHeader))); |
| 140 | if (LIKELY(storage != nullptr)) { |
| 141 | BlockHeader* header = reinterpret_cast<BlockHeader*>(storage); |
| 142 | header->size_ = bytes; // Write out the block header. |
| 143 | storage += sizeof(BlockHeader); |
| 144 | ++num_blocks_; |
| 145 | } |
| 146 | return storage; |
| 147 | } |
| 148 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 149 | void BumpPointerSpace::Walk(ObjectCallback* callback, void* arg) { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 150 | byte* pos = Begin(); |
Mathieu Chartier | 8544b46 | 2014-02-12 17:47:42 -0800 | [diff] [blame] | 151 | byte* end = End(); |
Mathieu Chartier | fc4c27e | 2014-02-11 11:05:41 -0800 | [diff] [blame] | 152 | byte* main_end = pos; |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 153 | { |
| 154 | MutexLock mu(Thread::Current(), block_lock_); |
| 155 | // If we have 0 blocks then we need to update the main header since we have bump pointer style |
| 156 | // allocation into an unbounded region (actually bounded by Capacity()). |
| 157 | if (num_blocks_ == 0) { |
| 158 | UpdateMainBlock(); |
| 159 | } |
Mathieu Chartier | 8544b46 | 2014-02-12 17:47:42 -0800 | [diff] [blame] | 160 | main_end = Begin() + main_block_size_; |
| 161 | if (num_blocks_ == 0) { |
| 162 | // We don't have any other blocks, this means someone else may be allocating into the main |
| 163 | // block. In this case, we don't want to try and visit the other blocks after the main block |
| 164 | // since these could actually be part of the main block. |
| 165 | end = main_end; |
| 166 | } |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 167 | } |
Mathieu Chartier | fc4c27e | 2014-02-11 11:05:41 -0800 | [diff] [blame] | 168 | // Walk all of the objects in the main block first. |
| 169 | while (pos < main_end) { |
| 170 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(pos); |
Mathieu Chartier | 8544b46 | 2014-02-12 17:47:42 -0800 | [diff] [blame] | 171 | if (obj->GetClass() == nullptr) { |
| 172 | // There is a race condition where a thread has just allocated an object but not set the |
| 173 | // class. We can't know the size of this object, so we don't visit it and exit the function |
| 174 | // since there is guaranteed to be not other blocks. |
| 175 | return; |
| 176 | } else { |
| 177 | callback(obj, arg); |
| 178 | pos = reinterpret_cast<byte*>(GetNextObject(obj)); |
| 179 | } |
Mathieu Chartier | fc4c27e | 2014-02-11 11:05:41 -0800 | [diff] [blame] | 180 | } |
| 181 | // Walk the other blocks (currently only TLABs). |
Mathieu Chartier | 8544b46 | 2014-02-12 17:47:42 -0800 | [diff] [blame] | 182 | while (pos < end) { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 183 | BlockHeader* header = reinterpret_cast<BlockHeader*>(pos); |
| 184 | size_t block_size = header->size_; |
| 185 | pos += sizeof(BlockHeader); // Skip the header so that we know where the objects |
| 186 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(pos); |
| 187 | const mirror::Object* end = reinterpret_cast<const mirror::Object*>(pos + block_size); |
| 188 | CHECK_LE(reinterpret_cast<const byte*>(end), End()); |
| 189 | // We don't know how many objects are allocated in the current block. When we hit a null class |
| 190 | // assume its the end. TODO: Have a thread update the header when it flushes the block? |
| 191 | while (obj < end && obj->GetClass() != nullptr) { |
| 192 | callback(obj, arg); |
| 193 | obj = GetNextObject(obj); |
| 194 | } |
| 195 | pos += block_size; |
| 196 | } |
| 197 | } |
| 198 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 199 | accounting::SpaceBitmap::SweepCallback* BumpPointerSpace::GetSweepCallback() { |
| 200 | LOG(FATAL) << "Unimplemented"; |
| 201 | return nullptr; |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | uint64_t BumpPointerSpace::GetBytesAllocated() { |
| 205 | // Start out pre-determined amount (blocks which are not being allocated into). |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 206 | uint64_t total = static_cast<uint64_t>(bytes_allocated_.Load()); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 207 | Thread* self = Thread::Current(); |
| 208 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 209 | MutexLock mu2(self, *Locks::thread_list_lock_); |
| 210 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
| 211 | MutexLock mu3(Thread::Current(), block_lock_); |
| 212 | // If we don't have any blocks, we don't have any thread local buffers. This check is required |
| 213 | // since there can exist multiple bump pointer spaces which exist at the same time. |
| 214 | if (num_blocks_ > 0) { |
| 215 | for (Thread* thread : thread_list) { |
| 216 | total += thread->thread_local_pos_ - thread->thread_local_start_; |
| 217 | } |
| 218 | } |
| 219 | return total; |
| 220 | } |
| 221 | |
| 222 | uint64_t BumpPointerSpace::GetObjectsAllocated() { |
| 223 | // Start out pre-determined amount (blocks which are not being allocated into). |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 224 | uint64_t total = static_cast<uint64_t>(objects_allocated_.Load()); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 225 | Thread* self = Thread::Current(); |
| 226 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 227 | MutexLock mu2(self, *Locks::thread_list_lock_); |
| 228 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
| 229 | MutexLock mu3(Thread::Current(), block_lock_); |
| 230 | // If we don't have any blocks, we don't have any thread local buffers. This check is required |
| 231 | // since there can exist multiple bump pointer spaces which exist at the same time. |
| 232 | if (num_blocks_ > 0) { |
| 233 | for (Thread* thread : thread_list) { |
| 234 | total += thread->thread_local_objects_; |
| 235 | } |
| 236 | } |
| 237 | return total; |
| 238 | } |
| 239 | |
| 240 | void BumpPointerSpace::RevokeThreadLocalBuffersLocked(Thread* thread) { |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 241 | objects_allocated_.FetchAndAdd(thread->thread_local_objects_); |
| 242 | bytes_allocated_.FetchAndAdd(thread->thread_local_pos_ - thread->thread_local_start_); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 243 | thread->SetTlab(nullptr, nullptr); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 246 | bool BumpPointerSpace::AllocNewTlab(Thread* self, size_t bytes) { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 247 | MutexLock mu(Thread::Current(), block_lock_); |
| 248 | RevokeThreadLocalBuffersLocked(self); |
| 249 | byte* start = AllocBlock(bytes); |
| 250 | if (start == nullptr) { |
| 251 | return false; |
| 252 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 253 | self->SetTlab(start, start + bytes); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 254 | return true; |
| 255 | } |
| 256 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 257 | } // namespace space |
| 258 | } // namespace gc |
| 259 | } // namespace art |